/* * Create a new macro: then create some random instances */ static void test_macro(const char *name, uint32_t n, type_t *vars, type_t body) { uint32_t i; int32_t id; printf("Test: create macro %s[", name); for (i=0; i<n; i++) { if (i > 0) fputs(", ", stdout); print_type(stdout, &types, vars[i]); } printf("] = "); print_type(stdout, &types, body); printf("\n"); add_type_macro(&types, clone_string(name), n, vars, body); id = get_type_macro_by_name(&types, name); printf("Result: "); show_macro(stdout, &types, type_macro(&types, id)); printf("\n"); for (i=0; i<10; i++) { test_instance(id, n); } printf("----\n"); show_macro_instances(stdout, &types, id); printf("\n"); }
Errcode create_celfli_start(char *tempname, char *fliname, Flicel **pfcel, Rcel *rc) { Errcode err; Flicel *fc; if((err = alloc_fcel(pfcel)) < Success) return(err); fc = *pfcel; fc->rc = rc; fc->flags |= FCEL_OWNS_RAST; if(tempname) { if(NULL == (fc->tpath = clone_string(tempname))) { err = Err_no_memory; goto error; } } if(fliname) { if((err = pj_fli_create(fliname,&fc->flif)) < Success) goto error; } fc->flif.hdr.width = rc->width; fc->flif.hdr.height = rc->height; fc->flif.hdr.bits_a_pixel = 8; /* rc->pdepth; */ fc->flif.hdr.speed = 71; /* fudge for aspect ratio */ fc->flif.hdr.aspect_dx = vb.pencel->aspect_dx; fc->flif.hdr.aspect_dy = vb.pencel->aspect_dy; set_fcel_center(fc,rc->x + (rc->width>>1),rc->y + (rc->height>>1)); if(fliname) { if((err = alloc_flipath(fliname,&fc->flif,&fc->cpath)) < Success) goto error; } return(Success); error: fc->rc = NULL; fc->flags &= ~FCEL_OWNS_RAST; free_fcel(pfcel); if(fliname) pj_delete(fliname); return(err); }
/* * Map name[i] to a new scope */ static void map(uint32_t i) { char *clone; assert(i < NSYMBOLS); if (scope[i] < UINT32_MAX) { clone = clone_string(name[i]); string_incref(clone); scope[i] ++; stbl_add(&sym_table, clone, scope[i]); } }
void add_sub_file(char *name) /****************************************************************** * Add a file to the list of files to do substitutions on. ******************************************************************/ { Name *n; n = begmem(sizeof(*n)); n->name = clone_string(name); n->next = plist; plist = n; }
/* * Initialize the type variables */ static void init_variables(void) { char name[2]; uint32_t i; name[0] = 'A'; name[1] = '\0'; for (i=0; i<NVARS; i++) { var[i] = type_variable(&types, i); set_type_name(&types, var[i], clone_string(name)); name[0] ++; } }
static Popot po_stristr(Popot s1, Popot s2) /***************************************************************************** * char *stristr(char *string, char *substring) ****************************************************************************/ { char *p1 = NULL, *p2 = NULL; char *res; if (s1.pt == NULL || s2.pt == NULL) { builtin_err = Err_null_ref; goto ERR; } if ((p1 = clone_string(s1.pt)) == NULL) { builtin_err = Err_no_memory; goto ERR; } if ((p2 = clone_string(s2.pt)) == NULL) { builtin_err = Err_no_memory; goto ERR; } upc(p1); upc(p2); if ((res = strstr(p1,p2)) == NULL) goto ERR; s1.pt = OPTR(s1.pt,res-p1); goto OUT; ERR: s1.pt = s1.min = s1.max = NULL; OUT: pj_gentle_free(p1); pj_gentle_free(p2); return(s1); }
/* * Create a new type constructor + some instances */ static void test_constructor(const char *name, uint32_t n) { int32_t id; uint32_t i; printf("Test: create constructor %s, arity %"PRIu32"\n", name, n); add_type_constructor(&types, clone_string(name), n); id = get_type_macro_by_name(&types, name); printf("Result: "); show_macro(stdout, &types, type_macro(&types, id)); printf("\n"); for (i=0; i<10; i++) { test_instance(id, n); } }
void sub_file(char *t) /****************************************************************** * Do our substitutions on a single file. ******************************************************************/ { Name *ll, *n; FILE *sfile; sfile = must_open(t, "r"); printf("Subbing %s\n", t); ll = NULL; llcount = 0; ltitle = t; dirty = FALSE; for (;;) { if (fgets(b1, sizeof(b1)-1, sfile) == NULL) break; if (embedded) embedded_subline(); else csym_subline(); n = begmem(sizeof(*n)); n->name = clone_string(b2); n->next = ll; ll = n; llcount += 1; } fclose(sfile); if (dirty && writeit) { printf("saving new %s\n", t); ll = reverse_list(ll); n = ll; if (backup) backitup(t); sfile = must_open(t, "w"); while (n != NULL) { fputs(n->name, sfile); n = n->next; } fclose(sfile); } free_names(ll); }
/* * Find or create record with the given key * - set is_new to true if that's a new record * - set is_new to false otherwise */ strmap_rec_t *strmap_get(strmap_t *hmap, const char *key, bool *is_new) { uint32_t mask, i, h; strmap_rec_t *d, *aux; char *clone; assert(is_power_of_two(hmap->size) && hmap->nelems + hmap->ndeleted < hmap->size); mask = hmap->size - 1; h = jenkins_hash_string(key); i = h & mask; for (;;) { d = hmap->data + i; if (! valid_key(d->key)) break; if (d->hash == h && strcmp(d->key, key) == 0) goto found; i ++; i &= mask; } aux = d; // this is where the new record will go if needed while(d->key != NULL) { i ++; i &= mask; if (d->key != DELETED_KEY && d->hash == h && strcmp(d->key, key) == 0) goto found; } // not found: add a new record hmap->nelems ++; clone = clone_string(key); string_incref(clone); if (hmap->nelems + hmap->ndeleted > hmap->resize_threshold) { // resize: we can't use the current aux strmap_extend(hmap); aux = strmap_get_clean(hmap, h); } aux->key = clone; aux->hash = h; aux->val = 0; *is_new = true; return aux; found: *is_new = false; return d; }
Errcode load_fli_fcel(char *flipath,char *tempname,char *celfli_name, Flicel **pfc) /* loads a fli file as a newly allocated flicel, if tempname is non-null * builds a temp fli from the fli or points a tempname file to the fli * if the tempname is NULL it will not build a temp file and will point the * ram cel to the fli even if the fli is on a removable device */ { Errcode err; Flifile flif; Flicel *fc; LONG chunksize; Chunkparse_data pd; Boolean make_flicopy; Boolean found_celdata; char device[DEV_NAME_LEN]; if((err = alloc_fcel(pfc)) < Success) return(err); fc = *pfc; if(tempname) { if(NULL == (fc->tpath = clone_string(tempname))) { err = Err_no_memory; goto error; } } clear_struct(&flif); if((err = get_path_device(flipath,device)) < Success) goto error; /* if allowed make sure cel fli file is not on removable drive */ make_flicopy = (tempname != NULL && celfli_name != NULL && !pj_is_fixed(device)); /* attempth to open fli requested as cel */ if (make_flicopy) { err = pj_fli_open(flipath, &flif, XREADONLY); if (err < Success) goto error; } else { err = pj_fli_open(flipath, &flif, XREADWRITE_OPEN); if (err < Success) goto error; /* we've got to have a valid update time ! */ if(flif.hdr.id.update_time == 0) { if((err = pj_i_flush_head(&flif)) < Success) goto error; } } found_celdata = FALSE; init_chunkparse(&pd, flif.xf, FCID_PREFIX, sizeof(Fli_head), 0, 0); while(get_next_chunk(&pd)) { if(pd.type == FP_CELDATA) { if(pd.fchunk.size == sizeof(Celdata)) { /* try to read it */ pd.error = read_parsed_chunk(&pd,&fc->cd,-1); found_celdata = TRUE; } break; } } if(pd.error < Success && pd.error != Err_no_chunk) { err = pd.error; goto error; } if(!found_celdata) { /* No positioning chunk found. Just put cel in upper left corner */ fc->cd.cent.x = flif.hdr.width>>1; fc->cd.cent.y = flif.hdr.height>>1; }
void build_sub_file(char *sname, Boolean embed) /****************************************************************** * go look for lines of format : * in|out * to tell us what to substitute * in| * is ok if embed is false, and will delete all ins. * * if embed is true, we'll skip all white space. * ******************************************************************/ { FILE *sfile; int line; char buf[256]; char *s; char word1[256]; char word2[256]; char *d; char c; Sub *sub; sfile = must_open(sname, "r"); line = 0; for (;;) { if (fgets(buf, sizeof(buf)-1, sfile) == NULL) break; line++; s = buf; if (!embed) s = skip_space(s); d = word1; for (;;) { c = *s++; if (c == 0) { fatal("%s %d - Line with no separator", sname, line); } if (!embed) { if (isspace(c)) break; } else if (c == separator) break; *d++ = c; } *d++ = 0; d = word2; if (!embed) s = skip_space(s); for (;;) { c = *s++; if (!embed) { if (isspace(c) || c == 0) break; } else { if (c == 0 || c == '\r' || c == '\n') break; } *d++ = c; } *d++ = 0; if (!embed && (word2[0] == 0)) fatal("%s %d - Line with no substitution", sname, line); sub = begmem(sizeof(*sub)); sub->in = clone_string(word1); sub->insize = strlen(word1); sub->out = clone_string(word2); sub->outsize = strlen(word2); add_hash(global_hash, sub); } fclose(sfile); }
static char* clone_string_with_default(const char* str,const char* default_str) { if (str != NULL) return clone_string( str ); return clone_string( default_str ); }
static Errcode load_hostlibs(char *name, Rexlib *rex_header, Hostlib *first_hl,Libhead **hostlibs) { USHORT type; Libhead *hosthead; Hostlib *prev_hl, *hl; Hostlib *hostpath = NULL; if(hostlibs) hosthead = *hostlibs; else hosthead = NULL; while(first_hl) { hl = first_hl; if(hosthead == NULL) { /* no more host librarys see if what rexlib still wants is optional * if they are optional set them to null otherwise Bomb */ if(hl->type == AA_LOADPATH) { if((hl->version & ~(HLIB_OPTIONAL)) != AA_LOADPATH_VERSION) return(Err_library_version); hostpath = hl; } else if(!(hl->version & HLIB_OPTIONAL)) return(Err_host_library); first_hl = hl->next; hl->next = NULL; continue; } type = hosthead->type; if(type == AA_NOT_AVAILABLE) goto get_next_hosthead; if(type == AA_LOADPATH) return(Err_bad_input); /* search list from rex code and see if we have what it wants */ /* this will make prev_hl->next point to first_hl */ prev_hl = TOSTRUCT(Hostlib,next,&first_hl); for(;;) { if(type == hl->type) { if(hosthead->version < (hl->version & ~(HLIB_OPTIONAL))) return(Err_library_version); /* if we have a match load hostlib header pointer in next of * field hl after detatching it from list */ prev_hl->next = hl->next; hl->next = hosthead; break; } prev_hl = hl; if((hl = (Hostlib *)(hl->next)) == NULL) break; } get_next_hosthead: hosthead = *hostlibs++; /* get next libhead input by host */ } /* if rexlib has requested it we clone the path rexfile loaded from * and put it in the "first_hostlib" field for later freeing and * in the _a_a_loadpath.next for rexlib access */ if(hostpath != NULL) { if(NULL == (hostpath->next = clone_string(name))) return(Err_no_memory); rex_header->first_hostlib = (Hostlib *)(hostpath->next); } return(Success); }