/* * Initialize a "*_info" array * * Note that we let each entry have a unique "name" and "text" string, * even if the string happens to be empty (everyone has a unique '\0'). */ static errr init_info(cptr filename, header *head) { ang_file *fh; errr err = 1; char txt_file[1024]; char buf[1024]; void *fake_name; void *fake_text; /* Build the filename */ path_build(txt_file, sizeof(txt_file), ANGBAND_DIR_EDIT, format("%s.txt", filename)); /* Allocate the "*_info" array */ head->info_ptr = C_ZNEW(head->info_size, char); /* MegaHack -- make "fake" arrays */ if (z_info) { head->name_ptr = C_ZNEW(z_info->fake_name_size, char); head->text_ptr = C_ZNEW(z_info->fake_text_size, char); } /*** Load the ascii template file ***/ /* Open the file */ fh = file_open(txt_file, MODE_READ, -1); if (!fh) quit(format("Cannot open '%s.txt' file.", filename)); /* Parse the file */ err = init_info_txt(fh, buf, head, head->parse_info_txt); file_close(fh); /* Errors */ if (err) display_parse_error(filename, err, buf); /* Copy the parsed data into the real array from the fakes */ fake_name = head->name_ptr; head->name_ptr = C_ZNEW(head->name_size, char); memcpy(head->name_ptr, fake_name, head->name_size); fake_text = head->text_ptr; head->text_ptr = C_ZNEW(head->text_size, char); memcpy(head->text_ptr, fake_text, head->text_size); /* Free the fake arrays */ if (z_info) { FREE(fake_name); FREE(fake_text); } /* Success */ return (0); }
int main(void) { GC_INIT(); struct env* env = new_env(); insert(env, "define", SCMPRIM(scm_define)); insert(env, "quote", SCMPRIM(scm_quote)); insert(env, "eval", SCMPRIM(scm_eval)); insert(env, "lambda", SCMPRIM(scm_lambda)); insert(env, "cons", SCMPRIM(scm_cons)); insert(env, "car", SCMPRIM(scm_car)); insert(env, "cdr", SCMPRIM(scm_cdr)); // A read-eval loop! char* buff; size_t bufflen = 0; while (true) { printf("> "); fflush(stdout); if (getline(&buff, &bufflen, stdin) == -1) break; /* int len; fdisplay(stdout, eval(parse(buff, &len)[0], &env)); printf("\n"); */ struct parse_error parse_err; struct value* vals; int len; if (PARSED(parse_err = parse(buff, &vals, &len))) { struct value ret; struct error err = eval(vals[0], &env, &ret); if (!SUCCEEDED(err)) { display_error(err); } else { fdisplay(stdout, ret); printf("\n"); } } else { display_parse_error(parse_err); printf("\n"); } } return 0; }
void display_ast(struct ast_t *a, const char *type_strings[]) { if (a == NULL) { printf("NULL"); return; } switch (a->type) { case AST_ERROR: { display_parse_error(a, type_strings); break; } case AST_CHAR: case AST_TREE: { display_ast_iter(0, a, type_strings); break; } default: break; } }
/* * Initialise random name fragments, from the edit file. */ static void init_names(void) { errr err; char filename[1024]; char buf[1024]; ang_file *fh; path_build(filename, sizeof(filename), ANGBAND_DIR_EDIT, "names.txt"); /* Open the file */ fh = file_open(filename, MODE_READ, -1); if (!fh) quit("Cannot open 'names.txt' file."); /* Parse the file */ err = init_names_txt(fh, buf); file_close(fh); /* Errors */ if (err) display_parse_error("names", err, buf); return; }
t_data *set_cam(char **input, int line) { static t_data *cam = NULL; if ((cam && input) || (input && count_array(input) != 10)) return ((t_data*)display_parse_error(CAM, line)); if (!cam && input) { cam = (t_data *)safe_malloc(sizeof(t_data)); cam->obj = CAM; cam->pos.x = ft_atoi(input[1]); cam->pos.y = ft_atoi(input[2]); cam->pos.z = ft_atoi(input[3]); cam->vect.x = ft_atoi(input[4]); cam->vect.y = ft_atoi(input[5]); cam->vect.z = ft_atoi(input[6]); cam->angle.x = ft_atoi(input[7]); cam->angle.y = ft_atoi(input[8]); cam->angle.z = ft_atoi(input[9]); } return (cam); }
/* * Initialize a "*_info" array * * Note that we let each entry have a unique "name" and "text" string, * even if the string happens to be empty (everyone has a unique '\0'). */ static errr init_info(cptr filename, header *head) { ang_file *fh; errr err = 1; char txt_file[1024]; char buf[1024]; void *fake_name; void *fake_text; /* Build the filename */ path_build(txt_file, sizeof(txt_file), ANGBAND_DIR_EDIT, format("%s.txt", filename)); /* Allocate the "*_info" array */ head->info_ptr = C_ZNEW(head->info_size, char); /* MegaHack -- make "fake" arrays */ if (z_info) { head->name_ptr = C_ZNEW(z_info->fake_name_size, char); head->text_ptr = C_ZNEW(z_info->fake_text_size, char); } /*** Load the ascii template file ***/ /* Open the file */ fh = file_open(txt_file, MODE_READ, -1); if (!fh) quit(format("Cannot open '%s.txt' file.", filename)); /* Parse the file */ err = init_info_txt(fh, buf, head, head->parse_info_txt); file_close(fh); /* Errors */ if (err) display_parse_error(filename, err, buf); /* Post processing the data */ if (head->eval_info_post) eval_info(head->eval_info_post, head); /*** Output a 'parsable' ascii template file ***/ if ((head->emit_info_txt_index) || (head->emit_info_txt_always)) { char user_file[1024]; ang_file *fout; /* Open the original */ fh = file_open(txt_file, MODE_READ, -1); if (!fh) quit(format("Cannot open '%s.txt' file for re-parsing.", filename)); /* Open for output */ path_build(user_file, 1024, ANGBAND_DIR_USER, format("%s.txt", filename)); fout = file_open(user_file, MODE_WRITE, FTYPE_TEXT); if (!fout) quit(format("Cannot open '%s.txt' file for output.", filename)); /* Parse and output the files */ err = emit_info_txt(fout, fh, user_file, head, head->emit_info_txt_index, head->emit_info_txt_always); /* Close both files */ file_close(fh); file_close(fout); } /* Copy the parsed data into the real array from the fakes */ fake_name = head->name_ptr; head->name_ptr = C_ZNEW(head->name_size, char); memcpy(head->name_ptr, fake_name, head->name_size); fake_text = head->text_ptr; head->text_ptr = C_ZNEW(head->text_size, char); memcpy(head->text_ptr, fake_text, head->text_size); /* Free the fake arrays */ if (z_info) { FREE(fake_name); FREE(fake_text); } /* Success */ return (0); }
/* * Initialize a "*_info" array * * Note that we let each entry have a unique "name" and "text" string, * even if the string happens to be empty (everyone has a unique '\0'). */ static errr init_info(cptr filename, header *head) { int fd; errr err = 1; FILE *fp; /* General buffer */ char buf[1024]; #ifdef ALLOW_TEMPLATES /*** Load the binary image file ***/ /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s.raw", filename)); /* Attempt to open the "raw" file */ fd = fd_open(buf, O_RDONLY); /* Process existing "raw" file */ if (fd >= 0) { #ifdef CHECK_MODIFICATION_TIME err = check_modification_date(fd, format("%s.txt", filename)); #endif /* CHECK_MODIFICATION_TIME */ /* Attempt to parse the "raw" file */ if (!err) err = init_info_raw(fd, head); /* Close it */ fd_close(fd); } /* Do we have to parse the *.txt file? */ if (err) { /*** Make the fake arrays ***/ /* Allocate the "*_info" array */ C_MAKE(head->info_ptr, head->info_size, char); /* MegaHack -- make "fake" arrays */ if (z_info) { C_MAKE(head->name_ptr, z_info->fake_name_size, char); C_MAKE(head->text_ptr, z_info->fake_text_size, char); } /*** Load the ascii template file ***/ /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_EDIT, format("%s.txt", filename)); /* Open the file */ fp = my_fopen(buf, "r"); /* Parse it */ if (!fp) quit(format("Cannot open '%s.txt' file.", filename)); /* Parse the file */ err = init_info_txt(fp, buf, head, head->parse_info_txt); /* Close it */ my_fclose(fp); /* Errors */ if (err) display_parse_error(filename, err, buf); /*** Dump the binary image file ***/ /* File type is "DATA" */ FILE_TYPE(FILE_TYPE_DATA); /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s.raw", filename)); /* Attempt to open the file */ fd = fd_open(buf, O_RDONLY); /* Failure */ if (fd < 0) { int mode = 0644; /* Grab permissions */ safe_setuid_grab(); /* Create a new file */ fd = fd_make(buf, mode); /* Drop permissions */ safe_setuid_drop(); /* Failure */ if (fd < 0) { char why[1024]; /* Message */ strnfmt(why, sizeof(why), "Cannot create the '%s' file!", buf); /* Crash and burn */ quit(why); } } /* Close it */ fd_close(fd); /* Grab permissions */ safe_setuid_grab(); /* Attempt to create the raw file */ fd = fd_open(buf, O_WRONLY); /* Drop permissions */ safe_setuid_drop(); /* Dump to the file */ if (fd >= 0) { /* Dump it */ fd_write(fd, (cptr)head, head->head_size); /* Dump the "*_info" array */ fd_write(fd, head->info_ptr, head->info_size); /* Dump the "*_name" array */ fd_write(fd, head->name_ptr, head->name_size); /* Dump the "*_text" array */ fd_write(fd, head->text_ptr, head->text_size); /* Close */ fd_close(fd); } /*** Kill the fake arrays ***/ /* Free the "*_info" array */ KILL(head->info_ptr); /* MegaHack -- Free the "fake" arrays */ if (z_info) { KILL(head->name_ptr); KILL(head->text_ptr); } #endif /* ALLOW_TEMPLATES */ /*** Load the binary image file ***/ /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, format("%s.raw", filename)); /* Attempt to open the "raw" file */ fd = fd_open(buf, O_RDONLY); /* Process existing "raw" file */ if (fd < 0) quit(format("Cannot load '%s.raw' file.", filename)); /* Attempt to parse the "raw" file */ err = init_info_raw(fd, head); /* Close it */ fd_close(fd); /* Error */ if (err) quit(format("Cannot parse '%s.raw' file.", filename)); #ifdef ALLOW_TEMPLATES } #endif /* ALLOW_TEMPLATES */ /* Success */ return (0); }