void HRA_fetch_a(SV *self, SV *attr, char *t) { dXSARGS; SP -= 3; if(GIMME_V == G_VOID) { XSRETURN(0); } SV *aobj = attr_get(self, attr, t, 0); if(!aobj) { HR_DEBUG("Can't find attribute!"); XSRETURN_EMPTY; } else { HR_DEBUG("Found aobj=%p", aobj); } hrattr_simple *aptr = attr_from_sv(SvRV(aobj)); HR_DEBUG("Attrhash=%p", aptr->attrhash); int nkeys = hv_iterinit(aptr->attrhash); HR_DEBUG("We have %d keys", nkeys); if(GIMME_V == G_SCALAR) { HR_DEBUG("Scalar return value requested"); XSRETURN_IV(nkeys); } HR_DEBUG("Will do some stack voodoo"); EXTEND(sp, nkeys); HE *cur = hv_iternext(aptr->attrhash); for(; cur != NULL; cur = hv_iternext(aptr->attrhash)) { XPUSHs(sv_mortalcopy(hv_iterval(aptr->attrhash, cur))); } PUTBACK; }
void dump_hash(pTHX_ HV* hash, Buffer* buf) { int count = 0; if (!hash) { return; } buffer_append(buf, "{", 1); hv_iterinit(hash); while (1) { I32 len = 0; char* key = 0; SV* val = 0; HE* entry = hv_iternext(hash); if (!entry) { break; } if (count++) { buffer_append(buf, ",", 1); } key = hv_iterkey(entry, &len); val = hv_iterval(hash, entry); buffer_append(buf, "\"", 1); buffer_append(buf, key, len); buffer_append(buf, "\":", 2); dump_value(aTHX_ val, buf); } buffer_append(buf, "}", 1); }
long SvFlagsHash(SV * name, char * optname, HV * o) { int i; int val=0; if (!name || !SvOK(name)) return 0; if (SvRV(name) && (SvTYPE(SvRV(name)) == SVt_PVAV)) { AV * r = (AV*)SvRV(name); for(i=0;i<=av_len(r);i++) val |= SvOptsHash(*av_fetch(r, i, 0), optname, o); } else if (SvRV(name) && (SvTYPE(SvRV(name)) == SVt_PVHV)) { HV * r = (HV*)SvRV(name); HE * h; hv_iterinit(r); while((h = hv_iternext(r))) { I32 len; char * key = hv_iterkey(h, &len); SV ** f; if (*key == '-') { key++; len--; } f = hv_fetch(o, key, len, 0); if (f) val |= SvIV(hv_iterval(o, h)); else CroakOptsHash(optname, key, o); } } else val |= SvOptsHash(name, optname, o); return val; }
/* XXX TODO: Messages should round-trip properly between message2hashref and hashref2message. Currently we lose zephyr-specific properties stored in the ZNotice_t */ CALLER_OWN owl_message *owl_perlconfig_hashref2message(SV *msg) { owl_message * m; HE * ent; I32 len; const char *key,*val; HV * hash; struct tm tm; hash = (HV*)SvRV(msg); m = g_new(owl_message, 1); owl_message_init(m); hv_iterinit(hash); while((ent = hv_iternext(hash))) { key = hv_iterkey(ent, &len); val = SvPV_nolen(hv_iterval(hash, ent)); if(!strcmp(key, "type")) { owl_message_set_type(m, val); } else if(!strcmp(key, "direction")) { owl_message_set_direction(m, owl_message_parse_direction(val)); } else if(!strcmp(key, "private")) { SV * v = hv_iterval(hash, ent); if(SvTRUE(v)) { owl_message_set_isprivate(m); } } else if (!strcmp(key, "hostname")) { owl_message_set_hostname(m, val); } else if (!strcmp(key, "zwriteline")) { owl_message_set_zwriteline(m, val); } else if (!strcmp(key, "time")) { g_free(m->timestr); m->timestr = g_strdup(val); strptime(val, "%a %b %d %T %Y", &tm); m->time = mktime(&tm); } else { owl_message_set_attribute(m, key, val); } } if(owl_message_is_type_admin(m)) { if(!owl_message_get_attribute_value(m, "adminheader")) owl_message_set_attribute(m, "adminheader", ""); } return m; }
static void tn_encode_hash(SV *data, struct tn_buffer *buf) { HV *hash = (HV *)data; HE *entry; SV *key; hv_iterinit(hash); while(entry = hv_iternext(hash)) { key = hv_iterkeysv(entry); SvPOK_on(key); tn_encode(hv_iterval(hash, entry), buf); tn_encode(key, buf); } }
KHARON_DECL int encode_map_next(void *data, void **key, void **val) { HV *in = data; HE *elem; elem = hv_iternext(in); if (!elem) return 0; *key = hv_iterkeysv(elem); *val = hv_iterval(in, elem); D(fprintf(stderr, "map_next = (key = %p, val = %p)\n", *key, *val)); return 1; }
SV * newSVOptsHash(long value, char * optname, HV * o) { int i; HE * h; SV * result = 0; hv_iterinit(o); while((h = hv_iternext(o))) { SV * s = hv_iterval(o, h); if (SvIV(s) == value) { I32 len; char * p = hv_iterkey(h, &len); result = newSVpv(p, len); } } if (result) return result; croak("invalid %s value %d", optname, value); }
void perl_ht_to_ht(HV *perl_ht, struct hashtable *ht) { HE *he; STRLEN len; I32 len2; hv_iterinit(perl_ht); while ((he = hv_iternext(perl_ht))) { SV *val = hv_iterval(perl_ht, he); char *val_str = strdup(SvPV(val, len)); char *key_str = strdup(hv_iterkey(he, &len2)); if (hashtable_search(ht, key_str)) { warnx("key '%s' already exists in ht, ignoring", key_str); continue; } hashtable_insert(ht, key_str, val_str); } }
int find_hooks(pTHX_ const char *type, HV *hooks, TypeHooks *pTH) { HE *h; int i, num; assert(type != NULL); assert(hooks != NULL); assert(pTH != NULL); (void) hv_iterinit(hooks); while ((h = hv_iternext(hooks)) != NULL) { const char *key; I32 keylen; SV *sub; enum HookId id; key = hv_iterkey(h, &keylen); sub = hv_iterval(hooks, h); id = get_hook_id(key); if (id >= HOOKID_COUNT) { if (id == HOOKID_INVALID) Perl_croak(aTHX_ "Invalid hook type '%s'", key); else fatal("Invalid hook id %d for hook '%s'", id, key); } single_hook_fill(aTHX_ key, type, &pTH->hooks[id], sub, SHF_ALLOW_ARG_SELF | SHF_ALLOW_ARG_TYPE | SHF_ALLOW_ARG_DATA | SHF_ALLOW_ARG_HOOK); } for (i = num = 0; i < HOOKID_COUNT; i++) if (pTH->hooks[i].sub) num++; return num; }
void VAstEnt::import(VAstEnt* pkgEntp, const string& id_or_star) { if (id_or_star != "*") { // Import single entry if (VAstEnt* idEntp = pkgEntp->findSym(id_or_star)) { // We can just add a second reference to the same AstEnt object if (debug()) cout<<"VAstEnt::import under="<<this<<" "<<idEntp->ascii()<<"\n"; replaceInsert(idEntp, id_or_star); } } else { // Walk old sym table HV* hvp = pkgEntp->subhash(); assert(hvp); hv_iterinit(hvp); while (HE* hep = hv_iternext(hvp)) { I32 retlen; const char* namep = hv_iterkey(hep, &retlen); string name = string(namep,retlen); SV* svp = hv_iterval(hvp, hep); VAstEnt* idEntp = avToSymEnt((AV*)(SvRV(svp))); if (debug()) cout<<"VAstEnt::import under="<<this<<" "<<idEntp->ascii(name)<<"\n"; replaceInsert(idEntp, name); } } }
static void modperl_package_clear_stash(pTHX_ const char *package) { HV *stash; if ((stash = gv_stashpv(package, FALSE))) { HE *he; I32 len; char *key; hv_iterinit(stash); while ((he = hv_iternext(stash))) { key = hv_iterkey(he, &len); if (MP_SAFE_STASH(key, len)) { SV *val = hv_iterval(stash, he); /* The safe thing to do is to skip over stash entries * that don't come from the package we are trying to * unload */ if (GvSTASH(val) == stash) { (void)hv_delete(stash, key, len, G_DISCARD); } } } } }
static void bitfields_option(pTHX_ BitfieldLayouter *layouter, SV *sv_val, SV **rval) { BitfieldLayouter bl_new = NULL; BitfieldLayouter bl = *layouter; if(sv_val) { if (SvROK(sv_val)) { sv_val = SvRV(sv_val); if (SvTYPE(sv_val) == SVt_PVHV) { HV *hv = (HV *) sv_val; HE *entry; SV **engine = hv_fetch(hv, "Engine", 6, 0); int noptions; const BLOption *options; if (engine && *engine) { const char *name = SvPV_nolen(*engine); bl = bl_new = bl_create(name); if (bl_new == NULL) Perl_croak(aTHX_ "Unknown bitfield layout engine '%s'", name); } (void) hv_iterinit(hv); options = bl->m->options(bl, &noptions); while ((entry = hv_iternext(hv)) != NULL) { SV *value; I32 keylen; int i; const char *prop_string = hv_iterkey(entry, &keylen); BLProperty prop; BLPropValue prop_value; const BLOption *opt = NULL; enum BLError error; if (strEQ(prop_string, "Engine")) continue; prop = bl_property(prop_string); for (i = 0; i < noptions; i++) if (options[i].prop == prop) { opt = &options[i]; break; } if (opt == NULL) FAIL_CLEAN((aTHX_ "Invalid option '%s' for bitfield layout engine '%s'", prop_string, bl->m->class_name(bl))); value = hv_iterval(hv, entry); prop_value.type = opt->type; switch (opt->type) { case BLPVT_INT: prop_value.v.v_int = SvIV(value); if (opt->nval) { const BLPropValInt *pval = opt->pval; for (i = 0; i < opt->nval; i++) if (pval[i] == prop_value.v.v_int) break; } break; case BLPVT_STR: prop_value.v.v_str = bl_propval(SvPV_nolen(value)); if (opt->nval) { const BLPropValStr *pval = opt->pval; for (i = 0; i < opt->nval; i++) if (pval[i] == prop_value.v.v_str) break; } break; default: fatal("unknown opt->type (%d) in bitfields_option()", opt->type); break; } if (opt->nval && i == opt->nval) FAIL_CLEAN((aTHX_ "Invalid value '%s' for option '%s'", SvPV_nolen(value), prop_string)); error = bl->m->set(bl, prop, &prop_value); switch (error) { case BLE_NO_ERROR: break; case BLE_INVALID_PROPERTY: FAIL_CLEAN((aTHX_ "Invalid value '%s' for option '%s'", SvPV_nolen(value), prop_string)); break; default: fatal("unknown error code (%d) returned by set method", error); break; } } if (bl_new) { (*layouter)->m->destroy(*layouter); *layouter = bl_new; } } else Perl_croak(aTHX_ "Bitfields wants a hash reference"); } else Perl_croak(aTHX_ "Bitfields wants a hash reference"); } if (rval) { int noptions; const BLOption *opt; int i; HV *hv = newHV(); SV *sv = newSVpv(bl->m->class_name(bl), 0); if (hv_store(hv, "Engine", 6, sv, 0) == NULL) SvREFCNT_dec(sv); opt = bl->m->options(bl, &noptions); for (i = 0; i < noptions; i++, opt++) { BLPropValue value; enum BLError error; const char *prop_string; error = bl->m->get(bl, opt->prop, &value); if (error != BLE_NO_ERROR) fatal("unexpected error (%d) returned by get method", error); assert(value.type == opt->type); switch (opt->type) { case BLPVT_INT: sv = newSViv(value.v.v_int); break; case BLPVT_STR: { const char *valstr = bl_propval_string(value.v.v_str); assert(valstr != NULL); sv = newSVpv(valstr, 0); } break; default: fatal("unknown opt->type (%d) in bitfields_option()", opt->type); break; } prop_string = bl_property_string(opt->prop); assert(prop_string != NULL); if (hv_store(hv, prop_string, strlen(prop_string), sv, 0) == NULL) SvREFCNT_dec(sv); } *rval = newRV_noinc((SV *) hv); } }
static void keyword_map(pTHX_ HashTable *current, SV *sv, SV **rval) { HashTable keyword_map = NULL; if(sv) { if (SvROK(sv)) { sv = SvRV(sv); if (SvTYPE(sv) == SVt_PVHV) { HV *hv = (HV *) sv; HE *entry; keyword_map = HT_new_ex(4, HT_AUTOGROW); (void) hv_iterinit(hv); while ((entry = hv_iternext(hv)) != NULL) { SV *value; I32 keylen; const char *key, *c; const CKeywordToken *pTok; c = key = hv_iterkey(entry, &keylen); if (*c == '\0') FAIL_CLEAN((aTHX_ "Cannot use empty string as a keyword")); while (*c == '_' || isALPHA(*c)) c++; if (*c != '\0') FAIL_CLEAN((aTHX_ "Cannot use '%s' as a keyword", key)); value = hv_iterval(hv, entry); if (!SvOK(value)) pTok = get_skip_token(); else { const char *map; if (SvROK(value)) FAIL_CLEAN((aTHX_ "Cannot use a reference as a keyword")); map = SvPV_nolen(value); if ((pTok = get_c_keyword_token(map)) == NULL) FAIL_CLEAN((aTHX_ "Cannot use '%s' as a keyword", map)); } (void) HT_store(keyword_map, key, (int) keylen, 0, (CKeywordToken *) pTok); } if (current != NULL) { HT_destroy(*current, NULL); *current = keyword_map; } } else Perl_croak(aTHX_ "KeywordMap wants a hash reference"); } else Perl_croak(aTHX_ "KeywordMap wants a hash reference"); } if (rval) { HashIterator hi; HV *hv = newHV(); CKeywordToken *tok; const char *key; int keylen; HI_init(&hi, *current); while (HI_next(&hi, &key, &keylen, (void **) &tok)) { SV *val; val = tok->name == NULL ? newSV(0) : newSVpv(CONST_CHAR(tok->name), 0); if (hv_store(hv, key, keylen, val, 0) == NULL) SvREFCNT_dec(val); } *rval = newRV_noinc((SV *) hv); } }
static gpointer sv_to_ghash (GITransfer transfer, GITypeInfo *type_info, SV *sv) { HV *hv; HE *he; GITransfer item_transfer; gpointer hash; GITypeInfo *key_param_info, *value_param_info; GITypeTag key_type_tag; GHashFunc hash_func; GEqualFunc equal_func; I32 n_keys; dwarn ("%s: sv %p\n", G_STRFUNC, sv); if (!gperl_sv_is_defined (sv)) return NULL; if (!gperl_sv_is_hash_ref (sv)) ccroak ("need an hash ref to convert to GHashTable"); hv = (HV *) SvRV (sv); item_transfer = GI_TRANSFER_NOTHING; switch (transfer) { case GI_TRANSFER_EVERYTHING: item_transfer = GI_TRANSFER_EVERYTHING; break; case GI_TRANSFER_CONTAINER: /* nothing special to do */ break; case GI_TRANSFER_NOTHING: /* FIXME: need to free hash after call */ break; } key_param_info = g_type_info_get_param_type (type_info, 0); value_param_info = g_type_info_get_param_type (type_info, 1); key_type_tag = g_type_info_get_tag (key_param_info); switch (key_type_tag) { case GI_TYPE_TAG_FILENAME: case GI_TYPE_TAG_UTF8: hash_func = g_str_hash; equal_func = g_str_equal; break; default: hash_func = NULL; equal_func = NULL; break; } dwarn (" GHashTable with transfer %d\n" " key_param_info %p with type tag %d (%s)\n" " value_param_info %p with type tag %d (%s)\n", transfer, key_param_info, g_type_info_get_tag (key_param_info), g_type_tag_to_string (g_type_info_get_tag (key_param_info)), value_param_info, g_type_info_get_tag (value_param_info), g_type_tag_to_string (g_type_info_get_tag (value_param_info))); hash = g_hash_table_new (hash_func, equal_func); n_keys = hv_iterinit (hv); if (n_keys == 0) goto out; while ((he = hv_iternext (hv)) != NULL) { SV *sv; GIArgument arg = { 0, }; gpointer key_p, value_p; key_p = value_p = NULL; sv = hv_iterkeysv (he); if (sv && gperl_sv_is_defined (sv)) { dwarn (" converting key SV %p\n", sv); /* FIXME: Is it OK to always allow undef here? */ sv_to_arg (sv, &arg, NULL, key_param_info, item_transfer, TRUE, NULL); key_p = arg.v_pointer; } sv = hv_iterval (hv, he); if (sv && gperl_sv_is_defined (sv)) { dwarn (" converting value SV %p\n", sv); sv_to_arg (sv, &arg, NULL, key_param_info, item_transfer, TRUE, NULL); value_p = arg.v_pointer; } if (key_p != NULL && value_p != NULL) g_hash_table_insert (hash, key_p, value_p); } out: dwarn (" -> hash %p of size %d\n", hash, g_hash_table_size (hash)); g_base_info_unref ((GIBaseInfo *) key_param_info); g_base_info_unref ((GIBaseInfo *) value_param_info); return hash; }
SV *p5_hv_iterval(PerlInterpreter *my_perl, HV *hv, HE *entry) { PERL_SET_CONTEXT(my_perl); return hv_iterval(hv, entry); }
JSPropertySpec *PJS_add_class_properties(PJS_Class *pcls, HV *ps, U8 flags) { JSPropertySpec *ps_list, *current_ps; PJS_Property *pprop; HE *entry; char *name; I32 len; AV *callbacks; SV **getter, **setter; I32 number_of_keys = hv_iterinit(ps); Newz(1, ps_list, number_of_keys + 1, JSPropertySpec); current_ps = ps_list; while((entry = hv_iternext(ps)) != NULL) { name = hv_iterkey(entry, &len); callbacks = (AV *) SvRV(hv_iterval(ps, entry)); len = strlen(name); Newz(1, pprop, 1, PJS_Property); if (pprop == NULL) { /* We might need to free more memory stuff here */ croak("Failed to allocate memory for PJS_Property"); } /* Setup JSFunctionSpec */ Newz(1, current_ps->name, len + 1, char); if (current_ps->name == NULL) { Safefree(pprop); croak("Failed to allocate memory for JSPropertySpec name"); } Copy(name, current_ps->name, len, char); getter = av_fetch(callbacks, 0, 0); setter = av_fetch(callbacks, 1, 0); pprop->getter = getter != NULL && SvTRUE(*getter) ? SvREFCNT_inc(*getter) : NULL; pprop->setter = setter != NULL && SvTRUE(*setter) ? SvREFCNT_inc(*setter) : NULL; current_ps->getter = PJS_invoke_perl_property_getter; current_ps->setter = PJS_invoke_perl_property_setter; current_ps->tinyid = pcls->next_property_id++; current_ps->flags = JSPROP_ENUMERATE; if (setter == NULL) { current_ps->flags |= JSPROP_READONLY; } pprop->tinyid = current_ps->tinyid; pprop->_next = pcls->properties; pcls->properties = pprop; current_ps++; } current_ps->name = 0; current_ps->tinyid = 0; current_ps->flags = 0; current_ps->getter = 0; current_ps->setter = 0; return ps_list; }
/* XXX TODO: Messages should round-trip properly between message2hashref and hashref2message. Currently we lose zephyr-specific properties stored in the ZNotice_t This has been somewhat addressed, but is still not lossless. */ owl_message * owl_perlconfig_hashref2message(SV *msg) { owl_message * m; HE * ent; I32 count, len; const char *key,*val; HV * hash; struct tm tm; hash = (HV*)SvRV(msg); m = owl_malloc(sizeof(owl_message)); owl_message_init(m); count = hv_iterinit(hash); while((ent = hv_iternext(hash))) { key = hv_iterkey(ent, &len); val = SvPV_nolen(hv_iterval(hash, ent)); if(!strcmp(key, "type")) { owl_message_set_type(m, val); } else if(!strcmp(key, "direction")) { owl_message_set_direction(m, owl_message_parse_direction(val)); } else if(!strcmp(key, "private")) { SV * v = hv_iterval(hash, ent); if(SvTRUE(v)) { owl_message_set_isprivate(m); } } else if (!strcmp(key, "hostname")) { owl_message_set_hostname(m, val); } else if (!strcmp(key, "zwriteline")) { owl_message_set_zwriteline(m, val); } else if (!strcmp(key, "time")) { m->timestr = owl_strdup(val); strptime(val, "%a %b %d %T %Y", &tm); m->time = mktime(&tm); } else { owl_message_set_attribute(m, key, val); } } if(owl_message_is_type_admin(m)) { if(!owl_message_get_attribute_value(m, "adminheader")) owl_message_set_attribute(m, "adminheader", ""); } #ifdef HAVE_LIBZEPHYR if (owl_message_is_type_zephyr(m)) { ZNotice_t *n = &(m->notice); n->z_kind = ACKED; n->z_port = 0; n->z_auth = ZAUTH_NO; n->z_checked_auth = 0; n->z_class = zstr(owl_message_get_class(m)); n->z_class_inst = zstr(owl_message_get_instance(m)); n->z_opcode = zstr(owl_message_get_opcode(m)); n->z_sender = zstr(owl_message_get_sender(m)); n->z_recipient = zstr(owl_message_get_recipient(m)); n->z_default_format = zstr("[zephyr created from perl]"); n->z_multinotice = zstr("[zephyr created from perl]"); n->z_num_other_fields = 0; n->z_message = owl_sprintf("%s%c%s", owl_message_get_zsig(m), '\0', owl_message_get_body(m)); n->z_message_len = strlen(owl_message_get_zsig(m)) + strlen(owl_message_get_body(m)) + 1; } #endif return m; }
JSFunctionSpec *PJS_add_class_functions(PJS_Class *pcls, HV *fs, U8 flags) { JSFunctionSpec *fs_list, *current_fs; PJS_Function *pfunc; HE *entry; char *name; I32 len; SV *callback; I32 number_of_keys = hv_iterinit(fs); Newz(1, fs_list, number_of_keys + 1, JSFunctionSpec); current_fs = fs_list; while((entry = hv_iternext(fs)) != NULL) { name = hv_iterkey(entry, &len); callback = hv_iterval(fs, entry); len = strlen(name); Newz(1, pfunc, 1, PJS_Function); if (pfunc == NULL) { /* We might need to free more memory stuff here */ croak("Failed to allocate memory for PJS_Function"); } /* Name of function */ Newz(1, pfunc->name, len + 1, char); if (pfunc->name == NULL) { Safefree(pfunc); croak("Failed to allocate memory for PJS_Function name"); } Copy(name, pfunc->name, len, char); /* Setup JSFunctionSpec */ Newz(1, current_fs->name, len + 1, char); if (current_fs->name == NULL) { Safefree(pfunc->name); Safefree(pfunc); croak("Failed to allocate memory for JSFunctionSpec name"); } Copy(name, current_fs->name, len, char); current_fs->call = PJS_invoke_perl_object_method; current_fs->nargs = 0; current_fs->flags = 0; current_fs->extra = 0; pfunc->callback = SvREFCNT_inc(callback); /* Add entry to linked list */ pfunc->_next = pcls->methods; pcls->methods = pfunc; /* Get next function */ current_fs++; } current_fs->name = 0; current_fs->call = 0; current_fs->nargs = 0; current_fs->flags = 0; current_fs->extra = 0; return fs_list; }
SV *p5_hv_iterval(PerlInterpreter *my_perl, HV *hv, HE *entry) { return hv_iterval(hv, entry); }