void owl_select_dispatch(fd_set *fds, int max_fd) { int i, len; owl_dispatch *d; owl_list *dl; dl = owl_global_get_dispatchlist(&g); len = owl_select_dispatch_count(); dispatch_active = 1; for(i = 0; i < len; i++) { d = (owl_dispatch*)owl_list_get_element(dl, i); /* While d shouldn't normally be null, the list may be altered by * functions we dispatch to. */ if (d != NULL && !d->needs_gc && FD_ISSET(d->fd, fds)) { if (d->cfunc != NULL) { d->cfunc(d); } } } dispatch_active = 0; owl_select_gc(); }
static void _owl_keymap_format_bindings(const owl_keymap *km, owl_fmtext *fm) { int i, nbindings; const owl_keybinding *kb; nbindings = owl_list_get_size(&km->bindings); for (i=0; i<nbindings; i++) { char *kbstr; const owl_cmd *cmd; const char *tmpdesc, *desc = ""; kb = owl_list_get_element(&km->bindings, i); kbstr = owl_keybinding_tostring(kb); owl_fmtext_append_normal(fm, OWL_TABSTR); owl_fmtext_append_normal(fm, kbstr); owl_fmtext_append_spaces(fm, 11-strlen(kbstr)); g_free(kbstr); owl_fmtext_append_normal(fm, " - "); if (kb->desc && *kb->desc) { desc = kb->desc; } else if ((cmd=owl_function_get_cmd(kb->command)) && (tmpdesc = owl_cmd_get_summary(cmd))) { desc = tmpdesc; } owl_fmtext_append_normal(fm, desc); if (kb->command && *(kb->command)) { owl_fmtext_append_normal(fm, " ["); owl_fmtext_append_normal(fm, kb->command); owl_fmtext_append_normal(fm, "]"); } owl_fmtext_append_normal(fm, "\n"); } }
/* return the buddy with index N. If out of range, return NULL */ owl_buddy *owl_buddylist_get_buddy_n(const owl_buddylist *bl, int index) { if (index<0) return(NULL); if (index>(owl_buddylist_get_size(bl)-1)) return(NULL); return(owl_list_get_element(&(bl->buddies), index)); }
void owl_log_outgoing_zephyr_error(const owl_zwrite *zw, const char *text) { FILE *file; char *filename, *logpath; char *tobuff; owl_message *m; /* create a present message so we can pass it to * owl_log_shouldlog_message(void) */ m = g_new(owl_message, 1); owl_message_create_from_zwrite(m, zw, text); if (!owl_log_shouldlog_message(m)) { owl_message_delete(m); return; } owl_message_delete(m); /* chop off a local realm */ tobuff = short_zuser(owl_list_get_element(&(zw->recips), 0)); /* expand ~ in path names */ logpath = owl_util_makepath(owl_global_get_logpath(&g)); filename = g_strdup_printf("%s/%s", logpath, tobuff); file=fopen(filename, "a"); g_free(filename); if (!file) { owl_function_error("Unable to open file for outgoing logging"); g_free(logpath); g_free(tobuff); return; } fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text); if (text[strlen(text)-1]!='\n') { fprintf(file, "\n"); } fclose(file); filename = g_strdup_printf("%s/all", logpath); g_free(logpath); file=fopen(filename, "a"); g_free(filename); if (!file) { owl_function_error("Unable to open file for outgoing logging"); g_free(tobuff); return; } fprintf(file, "ERROR (owl): %s\n%s\n", tobuff, text); if (text[strlen(text)-1]!='\n') { fprintf(file, "\n"); } fclose(file); g_free(tobuff); }
/* fmtext should already be initialized */ void owl_errqueue_to_fmtext(const owl_errqueue *eq, owl_fmtext *fm) { int i, j; j=owl_list_get_size(&(eq->errlist)); for (i=0; i<j; i++) { owl_fmtext_append_normal(fm, owl_list_get_element(&(eq->errlist), i)); owl_fmtext_append_normal(fm, "\n"); } }
void owl_select_remove_dispatch_at(int elt) /* noproto */ { owl_list *dl; owl_dispatch *d; dl = owl_global_get_dispatchlist(&g); d = (owl_dispatch*)owl_list_get_element(dl, elt); owl_list_remove_element(dl, elt); if (d->destroy) { d->destroy(d); } }
/* return the AIM buddy with screenname 'name'. If * no such buddy is logged in, return NULL. */ owl_buddy *owl_buddylist_get_aim_buddy(const owl_buddylist *bl, const char *name) { int i, j; owl_buddy *b; j=owl_list_get_size(&(bl->buddies)); for (i=0; i<j; i++) { b=owl_list_get_element(&(bl->buddies), i); if (!strcasecmp(name, owl_buddy_get_name(b))) return(b); } return(NULL); }
/* Returns the valid owl_io_dispatch for a given file descriptor. */ static owl_io_dispatch *owl_select_find_valid_io_dispatch_by_fd(const int fd) { int i, len; const owl_list *dl; owl_io_dispatch *d; dl = owl_global_get_io_dispatch_list(&g); len = owl_list_get_size(dl); for(i = 0; i < len; i++) { d = owl_list_get_element(dl, i); if (d->fd == fd && d->valid) return d; } return NULL; }
static owl_io_dispatch *owl_select_find_perl_io_dispatch(int fd) { int i, len; const owl_list *dl; owl_io_dispatch *d; dl = owl_global_get_io_dispatch_list(&g); len = owl_list_get_size(dl); for(i = 0; i < len; i++) { d = owl_list_get_element(dl, i); if (d->fd == fd && d->callback == owl_perlconfig_io_dispatch) return d; } return NULL; }
/* Returns the index of the dispatch for the file descriptor. */ int owl_select_find_dispatch(int fd) { int i, len; owl_list *dl; owl_dispatch *d; dl = owl_global_get_dispatchlist(&g); len = owl_list_get_size(dl); for(i = 0; i < len; i++) { d = (owl_dispatch*)owl_list_get_element(dl, i); if (d->fd == fd) return i; } return -1; }
int owl_dict_regtest(void) { owl_dict d; owl_list l; int numfailed=0; char *av="aval", *bv="bval", *cv="cval", *dv="dval"; printf("# BEGIN testing owl_dict\n"); FAIL_UNLESS("create", 0==owl_dict_create(&d)); FAIL_UNLESS("insert b", 0==owl_dict_insert_element(&d, "b", bv, owl_dict_noop_delete)); FAIL_UNLESS("insert d", 0==owl_dict_insert_element(&d, "d", dv, owl_dict_noop_delete)); FAIL_UNLESS("insert a", 0==owl_dict_insert_element(&d, "a", av, owl_dict_noop_delete)); FAIL_UNLESS("insert c", 0==owl_dict_insert_element(&d, "c", cv, owl_dict_noop_delete)); FAIL_UNLESS("reinsert d (no replace)", -2==owl_dict_insert_element(&d, "d", dv, 0)); FAIL_UNLESS("find a", av==owl_dict_find_element(&d, "a")); FAIL_UNLESS("find b", bv==owl_dict_find_element(&d, "b")); FAIL_UNLESS("find c", cv==owl_dict_find_element(&d, "c")); FAIL_UNLESS("find d", dv==owl_dict_find_element(&d, "d")); FAIL_UNLESS("find e (non-existent)", NULL==owl_dict_find_element(&d, "e")); FAIL_UNLESS("remove d", dv==owl_dict_remove_element(&d, "d")); FAIL_UNLESS("find d (post-removal)", NULL==owl_dict_find_element(&d, "d")); FAIL_UNLESS("get_size", 3==owl_dict_get_size(&d)); FAIL_UNLESS("get_keys", 0==owl_dict_get_keys(&d, &l)); FAIL_UNLESS("get_keys result size", 3==owl_list_get_size(&l)); /* these assume the returned keys are sorted */ FAIL_UNLESS("get_keys result val",0==strcmp("a",owl_list_get_element(&l,0))); FAIL_UNLESS("get_keys result val",0==strcmp("b",owl_list_get_element(&l,1))); FAIL_UNLESS("get_keys result val",0==strcmp("c",owl_list_get_element(&l,2))); owl_list_cleanup(&l, owl_free); owl_dict_cleanup(&d, NULL); /* if (numfailed) printf("*** WARNING: failures encountered with owl_dict\n"); */ printf("# END testing owl_dict (%d failures)\n", numfailed); return(numfailed); }
int owl_select_remove_perl_dispatch(int fd) { int elt; owl_dispatch *d; elt = owl_select_find_dispatch(fd); if (elt != -1) { d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt); if (d->cfunc == owl_perlconfig_dispatch) { owl_select_remove_dispatch_at(elt); return 0; } } return 1; }
AV *owl_new_av(const owl_list *l, SV *(*to_sv)(const void *)) { AV *ret; int i; void *element; ret = newAV(); for (i = 0; i < owl_list_get_size(l); i++) { element = owl_list_get_element(l, i); av_push(ret, to_sv(element)); } return ret; }
static int owl_select_find_io_dispatch(const owl_io_dispatch *in) { int i, len; const owl_list *dl; if (in != NULL) { dl = owl_global_get_io_dispatch_list(&g); len = owl_list_get_size(dl); for(i = 0; i < len; i++) { const owl_io_dispatch *d = owl_list_get_element(dl, i); if (d == in) return i; } } return -1; }
/* remove an AIM buddy from the buddy list */ int owl_buddylist_remove_aim_buddy(owl_buddylist *bl, const char *name) { int i, j; owl_buddy *b; j=owl_list_get_size(&(bl->buddies)); for (i=0; i<j; i++) { b=owl_list_get_element(&(bl->buddies), i); if (!strcasecmp(name, owl_buddy_get_name(b)) && owl_buddy_is_proto_aim(b)) { owl_list_remove_element(&(bl->buddies), i); owl_buddy_delete(b); return(0); } } return(1); }
static void owl_select_io_dispatch_gc(void) { int i; owl_list *dl; dl = owl_global_get_io_dispatch_list(&g); /* * Count down so we aren't set off by removing items from the list * during the iteration. */ for(i = owl_list_get_size(dl) - 1; i >= 0; i--) { owl_io_dispatch *d = owl_list_get_element(dl, i); if(d->needs_gc) { owl_select_remove_io_dispatch(d); } } }
int owl_zbuddylist_adduser(owl_zbuddylist *zb, const char *name) { int i, j; char *user; user=long_zuser(name); j=owl_list_get_size(&(zb->zusers)); for (i=0; i<j; i++) { if (!strcasecmp(user, owl_list_get_element(&(zb->zusers), i))) { owl_free(user); return(-1); } } owl_list_append_element(&(zb->zusers), user); return(0); }
/* Removes an owl_dispatch to the list, based on it's file descriptor. */ void owl_select_remove_dispatch(int fd) { int elt; owl_list *dl; owl_dispatch *d; elt = owl_select_find_dispatch(fd); if(elt == -1) { return; } else if(dispatch_active) { /* Defer the removal until dispatch is done walking the list */ dl = owl_global_get_dispatchlist(&g); d = (owl_dispatch*)owl_list_get_element(dl, elt); d->needs_gc = 1; } else { owl_select_remove_dispatch_at(elt); } }
static gboolean owl_io_dispatch_check(GSource *source) { int i, len; const owl_list *dl; dl = owl_global_get_io_dispatch_list(&g); len = owl_list_get_size(dl); for(i = 0; i < len; i++) { owl_io_dispatch *d = owl_list_get_element(dl, i); if (!d->valid) continue; if (d->pollfd.revents & G_IO_NVAL) { owl_function_debugmsg("Pruning defunct dispatch on fd %d.", d->fd); owl_select_invalidate_io_dispatch(d); } if (d->pollfd.revents & d->pollfd.events) return TRUE; } return FALSE; }
int owl_select_dispatch_prepare_fd_sets(fd_set *r, fd_set *e) { int i, len, max_fd; owl_dispatch *d; owl_list *dl; dl = owl_global_get_dispatchlist(&g); FD_ZERO(r); FD_ZERO(e); max_fd = 0; len = owl_select_dispatch_count(g); for(i = 0; i < len; i++) { d = (owl_dispatch*)owl_list_get_element(dl, i); FD_SET(d->fd, r); FD_SET(d->fd, e); if (max_fd < d->fd) max_fd = d->fd; } return max_fd + 1; }
static gboolean owl_io_dispatch_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) { int i, len; const owl_list *dl; dispatch_active = 1; dl = owl_global_get_io_dispatch_list(&g); len = owl_list_get_size(dl); for (i = 0; i < len; i++) { owl_io_dispatch *d = owl_list_get_element(dl, i); if (!d->valid) continue; if ((d->pollfd.revents & d->pollfd.events) && d->callback != NULL) { d->callback(d, d->data); } } dispatch_active = 0; owl_select_io_dispatch_gc(); return TRUE; }
void owl_select_remove_io_dispatch(const owl_io_dispatch *in) { int elt; if (in != NULL) { elt = owl_select_find_io_dispatch(in); if (elt != -1) { owl_list *dl = owl_global_get_io_dispatch_list(&g); owl_io_dispatch *d = owl_list_get_element(dl, elt); if (dispatch_active) d->needs_gc = 1; else { owl_select_invalidate_io_dispatch(d); owl_list_remove_element(dl, elt); if (d->destroy) d->destroy(d); g_free(d); } } } }
int owl_zbuddylist_deluser(owl_zbuddylist *zb, const char *name) { int i, j; char *user, *ptr; user=long_zuser(name); j=owl_list_get_size(&(zb->zusers)); for (i=0; i<j; i++) { ptr=owl_list_get_element(&(zb->zusers), i); if (!strcasecmp(user, ptr)) { owl_list_remove_element(&(zb->zusers), i); owl_free(ptr); owl_free(user); return(0); } } owl_free(user); return(-1); }
/* Adds a new owl_dispatch to the list, replacing existing ones if needed. */ void owl_select_add_dispatch(owl_dispatch *d) { int elt; owl_list *dl; d->needs_gc = 0; elt = owl_select_find_dispatch(d->fd); dl = owl_global_get_dispatchlist(&g); if (elt != -1) { /* If we have a dispatch for this FD */ owl_dispatch *d_old; d_old = (owl_dispatch*)owl_list_get_element(dl, elt); /* Ignore if we're adding the same dispatch again. Otherwise replace the old dispatch. */ if (d_old != d) { owl_select_remove_dispatch_at(elt); } } owl_list_append_element(dl, d); }
int owl_select_add_perl_dispatch(int fd, SV *cb) { int elt; owl_dispatch *d; elt = owl_select_find_dispatch(fd); if (elt != -1) { d = (owl_dispatch*)owl_list_get_element(owl_global_get_dispatchlist(&g), elt); if (d->cfunc != owl_perlconfig_dispatch) { /* don't mess with non-perl dispatch functions from here. */ return 1; } } d = malloc(sizeof(owl_dispatch)); d->fd = fd; d->cfunc = owl_perlconfig_dispatch; d->destroy = owl_perlconfig_dispatch_free; d->data = cb; owl_select_add_dispatch(d); return 0; }
HV *owl_new_hv(const owl_dict *d, SV *(*to_sv)(const void *)) { HV *ret; owl_list l; const char *key; void *element; int i; ret = newHV(); /* TODO: add an iterator-like interface to owl_dict */ owl_dict_get_keys(d, &l); for (i = 0; i < owl_list_get_size(&l); i++) { key = owl_list_get_element(&l, i); element = owl_dict_find_element(d, key); (void)hv_store(ret, key, strlen(key), to_sv(element), 0); } owl_list_cleanup(&l, owl_free); return ret; }
/* creates and adds a key binding */ int owl_keymap_create_binding(owl_keymap *km, const char *keyseq, const char *command, void (*function_fn)(void), const char *desc) { owl_keybinding *kb, *curkb; int i; kb = owl_keybinding_new(keyseq, command, function_fn, desc); if (kb == NULL) return -1; /* see if another matching binding, and if so remove it. * otherwise just add this one. */ for (i = owl_list_get_size(&km->bindings)-1; i>=0; i--) { curkb = owl_list_get_element(&km->bindings, i); if (owl_keybinding_equal(curkb, kb)) { owl_list_remove_element(&km->bindings, i); owl_keybinding_delete(curkb); } } return owl_list_append_element(&km->bindings, kb); }
/* removes the binding associated with the keymap */ int owl_keymap_remove_binding(owl_keymap *km, const char *keyseq) { owl_keybinding *kb, *curkb; int i; kb = owl_keybinding_new(keyseq, NULL, NULL, NULL); if (kb == NULL) return -1; for (i = owl_list_get_size(&km->bindings)-1; i >= 0; i--) { curkb = owl_list_get_element(&km->bindings, i); if (owl_keybinding_equal(curkb, kb)) { owl_list_remove_element(&km->bindings, i); owl_keybinding_delete(curkb); owl_keybinding_delete(kb); return(0); } } owl_keybinding_delete(kb); return(-2); }
/* requires that the list values are strings or NULL. * joins the elements together with join_with. * If format_fn is specified, passes it the list element value * and it will return a string which this needs to free. */ void owl_fmtext_append_list(owl_fmtext *f, const owl_list *l, const char *join_with, char *(format_fn)(const char *)) { int i, size; const char *elem; char *text; size = owl_list_get_size(l); for (i=0; i<size; i++) { elem = owl_list_get_element(l,i); if (elem && format_fn) { text = format_fn(elem); if (text) { owl_fmtext_append_normal(f, text); owl_free(text); } } else if (elem) { owl_fmtext_append_normal(f, elem); } if ((i < size-1) && join_with) { owl_fmtext_append_normal(f, join_with); } } }
/* 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); }