int symcopy(struct sym *from, struct sym *to, struct allocator *al) { iter_t iter; void *key, *data; to->flags = from->flags & 0xFFFF; to->idl_type = dupstr(from->idl_type, al); to->ndr_type = dupstr(from->ndr_type, al); to->interface = from->interface; hashmap_iterate(&from->attrs, &iter); while ((key = hashmap_next(&from->attrs, &iter))) { if ((data = hashmap_get(&from->attrs, key)) == NULL || hashmap_put(&to->attrs, key, data) == -1) { AMSG(""); return -1; } } linkedlist_iterate(&from->mems, &iter); while ((data = linkedlist_next(&from->mems, &iter))) { if (linkedlist_add(&to->mems, data) == -1) { AMSG(""); return -1; } } to->name = dupstr(from->name, al); to->value = dupstr(from->value, al); to->ptr = from->ptr; to->align = from->align; return 0; }
void freeStuff() { iter_t iter; Expr el; linkedlist_iterate(&exprPool, &iter); while ((el = linkedlist_next(&exprPool, &iter))){ vc_deleteExpr(el); } linkedlist_clear(&exprPool, NULL, NULL); }
void freeStuff() { iter_t iter; char* varname; hashmap_iterate(&vars, &iter); while ((varname = (char*) hashmap_next(&vars, &iter))) free(varname); hashmap_clear(&vars, NULL, NULL, NULL); Expr el; linkedlist_iterate(&exprPool, &iter); while ((el = linkedlist_next(&exprPool, &iter))){ vc_deleteExpr(el); } linkedlist_clear(&exprPool, NULL, NULL); }
struct sym * get_descriminant(struct sym *u) { const char *switch_is = hashmap_get(&u->attrs, "switch_is"); iter_t iter; struct sym *mem; linkedlist_iterate(&u->parent->mems, &iter); while ((mem = linkedlist_next(&u->parent->mems, &iter))) { if (ident_index(switch_is, mem->name)) { return mem; } } return NULL; }
int print_tree(struct idl *idl, struct sym *sym, int indent) { char buf[1024]; iter_t iter; struct sym *mem; sprint_sym(buf, sym, indent, 14); fputs(buf, stderr); if (*buf) fputc('\n', stderr); linkedlist_iterate(&sym->mems, &iter); while ((mem = linkedlist_next(&sym->mems, &iter))) { print_tree(idl, mem, indent + 2); } return 0; }
int inherit_iface(struct idl *idl, struct sym *sym, char *name) { iter_t iter; struct sym *mem; if (!sym->interface) { sym->interface = name; } if (strcmp(idl->interface, sym->interface) != 0) { sym->flags |= FLAGS_IMPORTED; } linkedlist_iterate(&sym->mems, &iter); while ((mem = linkedlist_next(&sym->mems, &iter))) { if (inherit_iface(idl, mem, name) == -1) { AMSG(""); return -1; } } return 0; }
int symresolve(struct idl *idl, struct sym *sym) { struct sym *s; char *key; iter_t iter; int align; size_t size; s = symlook(idl, sym->idl_type); if (s) { /* merge stuff */ sym->flags |= s->flags & 0xFFFF; sym->ndr_type = s->ndr_type; if (sym->ptr && (!IS_PRIMATIVE(sym) || hashmap_get(&sym->attrs, "string"))) { sym->ndr_size = 4; } else { sym->ndr_size = s->ndr_size; } sym->align = sym->ptr ? 4 : sym->ndr_size; /* inherit attributes */ hashmap_iterate(&s->attrs, &iter); while ((key = hashmap_next(&s->attrs, &iter))) { /* Only add attributes that the symbol does not have already */ if (hashmap_get(&sym->attrs, key) == NULL) { char *data; data = hashmap_get(&s->attrs, key); hashmap_put(&sym->attrs, key, data); } } if (sym->out_type == NULL) { if (s->out_type) { sym->out_type = s->out_type; } else if (IS_TYPEDEFD(sym)) { sym->out_type = sym->idl_type = sym->name; } else if (strncmp(sym->idl_type, "struct ", 7) == 0 || strncmp(sym->idl_type, "union ", 6) == 0) { char buf[255]; if (*sym->idl_type == 's') { sprintf(buf, "struct_%s", sym->idl_type + 7); } else { sprintf(buf, "union_%s", sym->idl_type + 7); } sym->out_type = dupstr(buf, idl->al); } if ((strcmp(idl->type, "java") == 0 || strcmp(idl->type, "jcifs") == 0) && IS_IMPORTED(sym)) { char buf[255]; sprintf(buf, "%s.%s", sym->interface, sym->out_type); sym->out_type = dupstr(buf, idl->al); } } } align = 0; size = 0; linkedlist_iterate(&sym->mems, &iter); while ((s = linkedlist_next(&sym->mems, &iter))) { size_t msiz, mali, m; symresolve(idl, s); if (s->align > align) { align = s->align; } if (s->ptr) { msiz = mali = 4; } else { msiz = s->ndr_size; mali = s->align; } m = mali - 1; size = (size + m) & ~m; /* align the type */ s->offset = size; if (IS_ENUM(sym)) { size = msiz; /* size only size of member not sum of mems */ } else if (IS_UNION(sym)) { /* size is largest of mems (not really used for any logic so far) */ if (msiz > size) { size = msiz; } } else { size += msiz; /* and size of type */ } } if (align) { sym->align = align; } if (size) { sym->ndr_size = size; } /* check array conformance */ hashmap_iterate(&sym->attrs, &iter); while ((key = hashmap_next(&sym->attrs, &iter))) { if (!IS_FIXED(sym) && (strcmp(key, "size_is") == 0 || strcmp(key, "max_is") == 0)) { sym->flags |= FLAGS_CONFORMANT; break; } } if (IS_PRIMATIVE(sym)) { if (!IS_OPERATION(sym)) { sym->interface = NULL; } sym->flags &= ~FLAGS_IMPORTED; } return 0; }
int symexpand(struct idl *idl, struct sym *sym) { char *key; struct sym *mem; iter_t iter; if (IS_EXPANDED(sym)) { return 0; } sym->flags |= FLAGS_EXPANDED; sym->orig = sym; if (IS_INTERFACE(sym)) { const char *pd = hashmap_get(&sym->attrs, "pointer_default"); idl->ptr_default = PTR_TYPE_UNIQUE; if (pd) { if (strcmp(pd, "ptr") == 0) { idl->ptr_default = PTR_TYPE_PTR; } else if (strcmp(pd, "ref") == 0) { idl->ptr_default = PTR_TYPE_REF; } } } else if (IS_ENUM(sym)) { char buf[16]; int val = 0; linkedlist_iterate(&sym->mems, &iter); while ((mem = linkedlist_next(&sym->mems, &iter))) { mem->flags = FLAGS_CONST | FLAGS_PRIMATIVE; if (mem->value) { val = strtoul(mem->value, NULL, 0); } sprintf(buf, "%d", val++); mem->value = dupstr(buf, idl->al); } } else if (sym->ptr) { if (hashmap_get(&sym->attrs, "unique")) { sym->ptr_type = PTR_TYPE_UNIQUE; } else if (hashmap_get(&sym->attrs, "ptr")) { sym->ptr_type = PTR_TYPE_PTR; } else if (hashmap_get(&sym->attrs, "ref")) { sym->ptr_type = PTR_TYPE_REF; } else if (IS_PARAMETER(sym) || IS_OPERATION(sym)) { sym->ptr_type = PTR_TYPE_REF; } else { sym->ptr_type = idl->ptr_default; } } /* If the symbol is typedef'd add it to the table using the * typedef'd name too */ if (IS_TYPEDEFD(sym) && sym->name) { if (IS_ENUM(sym) && (mem = hashmap_get(idl->syms, sym->idl_type))) { mem->noemit = 1; /* supress redundant enum */ } key = sym->name; } else { key = sym->idl_type; } if (hashmap_get(idl->syms, key) == NULL) { if (hashmap_put(idl->syms, key, sym) == -1) { AMSG(""); } } /* If the symbol has members it is already expanded */ if (linkedlist_size(&sym->mems) == 0) { struct sym *s = symlook(idl, sym->idl_type); if (s) { sym->interface = s->interface; linkedlist_iterate(&s->mems, &iter); while ((mem = linkedlist_next(&s->mems, &iter))) { struct sym *cpy = symnew(idl->al); symcopy(mem, cpy, idl->al); linkedlist_add(&sym->mems, cpy); } } else { AMSG(""); return -1; } } sym->id = idl->symid++; /* Perform expansion recursively on all symbols */ linkedlist_iterate(&sym->mems, &iter); while ((mem = linkedlist_next(&sym->mems, &iter))) { symexpand(idl, mem); mem->parent = sym; } return 0; }
static int run(int argc, char **argv, const char *filename, const char *outname, const char *type, const char *symtabpath, struct hashmap *macros, int verbose) { struct idl idl; struct sym iface; unsigned char _outname[PATH_MAX]; memset(&idl, 0, sizeof(idl)); idl.argc = argc; idl.argv = argv; idl.type = type; idl.macros = macros; idl.verbose = verbose; idl.al = NULL; if ((idl.syms = hashmap_new(hash_str, cmp_str, NULL, idl.al)) == NULL || (idl.consts = hashmap_new(hash_str, cmp_str, NULL, idl.al)) == NULL || (idl.tmp = hashmap_new(hash_str, cmp_str, NULL, idl.al)) == NULL) { AMSG(""); return -1; } if (symload(&idl, symtabpath) == -1) { if (errno != ENOENT || symload(&idl, path_filename(symtabpath)) == -1) { AMSG(""); return -1; } } /* generate parse tree in iface */ syminit(&iface, idl.al); if (idl_process_file(&idl, filename, &iface) == -1) { AMSG(""); return -1; } if (idl.verbose > 1) { print_tree(&idl, &iface, 0); /* print everything */ } else if (idl.verbose) { iter_t iter; struct sym *mem; linkedlist_iterate(&iface.mems, &iter); while ((mem = linkedlist_next(&iface.mems, &iter))) { if (IS_OPERATION(mem) == 0) { continue; } print_tree(&idl, mem, 0); /* only print operations and their params */ } } if (idl.verbose) fprintf(stderr, " No Flg Type Ptr Name OutType NdrType Siz Aln Off Attributes\n"); mkoutname(_outname, outname ? outname : filename, ""); idl.outname = dupstr(path_filename(_outname), NULL); if (strcmp(type, "jcifs") == 0) { mkoutname(_outname, outname ? outname : filename, ".java"); if (run_one(&idl, &iface, _outname, emit_stub_jcifs) == -1) { AMSG(""); return -1; } } else if (strcmp(type, "java") == 0) { mkoutname(_outname, outname ? outname : filename, ".java"); if (run_one(&idl, &iface, _outname, emit_stub_java) == -1) { AMSG(""); return -1; } } else if (*type == 's') { mkoutname(_outname, outname ? outname : filename, ".c"); if (run_one(&idl, &iface, _outname, emit_stub_samba) == -1) { AMSG(""); return -1; } } else if (*type == 'c') { mkoutname(_outname, outname ? outname : filename, ".h"); if (run_one(&idl, &iface, _outname, emit_hdr_c) == -1) { AMSG(""); return -1; } mkoutname(_outname, outname ? outname : filename, "_s.c"); if (run_one(&idl, &iface, _outname, emit_svr_stub_c) == -1) { AMSG(""); return -1; } mkoutname(_outname, outname ? outname : filename, "_c.c"); } return 0; }
int LinkedlistExercise(int verbose, struct cfg *cfg, char *args[]) { int rate, i, n = 0, idx; char *str; struct linkedlist *l = linkedlist_new(EXERCISE_MED_COUNT, NULL); cfg = NULL; args[0] = NULL; if (l == NULL) { AMSG(""); return -1; } rate = EXERCISE_R0; for (i = 0; i < EXERCISE_MED_COUNT; i++) { if (i == EXERCISE_MED_P1) { rate = EXERCISE_R1; } else if (i == EXERCISE_MED_P2) { rate = EXERCISE_R2; } else if (i == EXERCISE_MED_P3) { rate = EXERCISE_R3; } if (rand() % 10 < rate) { idx = 0; str = malloc(8); sprintf(str, "%07d", n++); if (rand() % 5) { idx = linkedlist_size(l); if (idx) { idx = rand() % idx; } } if (linkedlist_insert(l, idx, str) == -1) { PMNO(errno); return -1; } tcase_printf(verbose, "INSERT: %s size now %d\n", str, linkedlist_size(l)); } else { if (linkedlist_is_empty(l)) { tcase_printf(verbose, "EMPTY\n"); } else { idx = rand() % linkedlist_size(l); str = linkedlist_get(l, idx); if (linkedlist_remove_data(l, str) == NULL) { PMNO(errno); return -1; } if ((idx % 10) == 0) { unsigned int count = 0; iter_t iter; linkedlist_iterate(l, &iter); while (linkedlist_next(l, &iter)) { count++; } if (count != linkedlist_size(l)) { PMSG("count=%u,linkedlist_size=%u\n", count, linkedlist_size(l)); return -1; } } if (str) { tcase_printf(verbose, "REMOVE: %s %d\n", str, linkedlist_size(l)); free(str); } else { PMSG("remove failure"); return -1; } } } } linkedlist_del(l, allocator_free, NULL); return 0; }