static errr finish_parse_quest(struct parser *p) { struct quest *quest, *next = NULL; int count; /* Count the entries */ z_info->quest_max = 0; quest = parser_priv(p); while (quest) { z_info->quest_max++; quest = quest->next; } /* Allocate the direct access list and copy the data to it */ quests = mem_zalloc(z_info->quest_max * sizeof(*quest)); count = z_info->quest_max - 1; for (quest = parser_priv(p); quest; quest = next, count--) { memcpy(&quests[count], quest, sizeof(*quest)); quests[count].index = count; next = quest->next; if (count < z_info->quest_max - 1) quests[count].next = &quests[count + 1]; else quests[count].next = NULL; mem_free(quest); } parser_destroy(p); return 0; }
static errr finish_parse_summon(struct parser *p) { struct summon *summon, *next; int count = 0; /* Count the entries */ summon_max = 0; summon = parser_priv(p); while (summon) { summon_max++; summon = summon->next; } /* Allocate the direct access list and copy the data to it */ summons = mem_zalloc((summon_max + 1) * sizeof(*summon)); for (summon = parser_priv(p); summon; summon = next, count++) { memcpy(&summons[count], summon, sizeof(*summon)); next = summon->next; summons[count].next = NULL; mem_free(summon); } summon_max += 1; /* Add indices of fallback summons */ for (count = 0; count < summon_max; count++) { char *name = summons[count].fallback_name; summons[count].fallback = summon_name_to_idx(name); } parser_destroy(p); return 0; }
errr finish_parse_grafmode(struct parser *p) { graphics_mode *mode, *n; int max = 0; int count = 0; int i; /* see how many graphics modes we have and what the highest index is */ if (p) { mode = parser_priv(p); while (mode) { if (mode->grafID > max) { max = mode->grafID; } count++; mode = mode->pNext; } } /* copy the loaded modes to the global variable */ if (graphics_modes) { close_graphics_modes(); } graphics_modes = mem_zalloc(sizeof(graphics_mode) * (count+1)); if (p) { mode = parser_priv(p); for (i = count-1; i >= 0; i--, mode = mode->pNext) { memcpy(&(graphics_modes[i]), mode, sizeof(graphics_mode)); graphics_modes[i].pNext = &(graphics_modes[i+1]); } } /* hardcode the no graphics option */ graphics_modes[count].pNext = NULL; graphics_modes[count].grafID = GRAPHICS_NONE; graphics_modes[count].alphablend = 0; graphics_modes[count].overdrawRow = 0; graphics_modes[count].overdrawMax = 0; strncpy(graphics_modes[count].pref, "none", 8); strncpy(graphics_modes[count].file, "", 32); strncpy(graphics_modes[count].menuname, "None", 32); graphics_mode_high_id = max; /* set the default graphics mode to be no graphics */ current_graphics_mode = &(graphics_modes[count]); if (p) { mode = parser_priv(p); while (mode) { n = mode->pNext; mem_free(mode); mode = n; } parser_setpriv(p, NULL); parser_destroy(p); } return PARSE_ERROR_NONE; }
void index_file(char * file, struct hash_table * table) { /* get a reverse hash table of the terms in the file */ FILE * file_fd; struct Parser * parser; char * word; file_fd = fopen(file, "r"); parser = parser_new(file_fd); if (file_fd == NULL) { fprintf(stderr, "couldn't get handler for %s\n", file); free_hash_table(table); } while ( (word = parser_next_word(parser)) ) { strtolower(word); struct hash_node * tmp = hash_table_get(table, word); if(tmp) { hash_node_add_occurrence(tmp, file); } else { struct hash_node * node = new_hash_node(word); node->appears_in = new_file_node(file); hash_table_store(table, word, node); } free(word); } parser_destroy(parser); }
int teardown_tests(void *state) { struct ego_item *e = parser_priv(state); string_free(e->name); string_free(e->text); mem_free(e); parser_destroy(state); return 0; }
void us_destroy(US* us) { LOG(INFO, ("US: destroying %p", us)); // env_destroy(us->env); parser_destroy(us->parser); arena_destroy(us->arena); MEM_FREE_TYPE(us, 1, US); }
errr process_pref_file_command(const char *s) { struct parser *p = init_parse_prefs(TRUE); errr e = parser_parse(p, s); mem_free(parser_priv(p)); parser_destroy(p); return e; }
int teardown_tests(void *state) { struct monster_race *mr = parser_priv(state); string_free(mr->name); string_free(mr->text); mem_free(mr); parser_destroy(state); mem_free(z_info); return 0; }
static errr finish_parse_r(struct parser *p) { struct monster_race *r, *n; size_t i; /* scan the list for the max id */ z_info->r_max -= 1; /*z_info->r_max = 0; fails to load existing save file because of * too high value in old limits.txt. Change to this line when save file * compatibility changes and remove line from limits.txt */ r = parser_priv(p); while (r) { if (r->ridx > z_info->r_max) z_info->r_max = r->ridx; r = r->next; } /* allocate the direct access list and copy the data to it */ r_info = mem_zalloc((z_info->r_max+1) * sizeof(*r)); for (r = parser_priv(p); r; r = n) { memcpy(&r_info[r->ridx], r, sizeof(*r)); n = r->next; if (n) r_info[r->ridx].next = &r_info[n->ridx]; else r_info[r->ridx].next = NULL; mem_free(r); } z_info->r_max += 1; /* convert friend names into race pointers */ for (i = 0; i < z_info->r_max; i++) { struct monster_race *r = &r_info[i]; struct monster_friends *f; for (f = r->friends; f; f = f->next) { if (!my_stricmp(f->name, "same")) f->race = r; else f->race = lookup_monster(f->name); if (!f->race) quit_fmt("Monster '%s' has friend '%s' but I couldn't find any monster of that name", r->name, f->name); string_free(f->name); } } eval_r_power(r_info); parser_destroy(p); return 0; }
/* ** prepares and run the selector for the game */ int start(parser_t *parser) { selector_t *stor; int ret = bstrap_stor(&stor, parser); if (ret) return (ret); selector_loop(stor); parser_destroy(parser); selector_delete(stor); return (0); }
int teardown_tests(void *state) { struct feature *f = parser_priv(state); string_free(f->die_msg); string_free(f->hurt_msg); string_free(f->run_msg); string_free(f->walk_msg); string_free(f->mimic); string_free(f->desc); string_free(f->name); mem_free(f); parser_destroy(state); return 0; }
/* * Process the user pref file with the given name. * "quiet" means "don't complain about not finding the file. * * Returns TRUE if everything worked OK, false otherwise */ bool process_pref_file(const char *name, bool quiet) { char buf[1024]; ang_file *f; struct parser *p; errr e = 0; int line_no = 0; /* Build the filename */ path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, name); if (!file_exists(buf)) path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name); f = file_open(buf, MODE_READ, -1); if (!f) { if (!quiet) msg("Cannot open '%s'.", buf); } else { char line[1024]; p = init_parse_prefs(); while (file_getl(f, line, sizeof line)) { line_no++; e = parser_parse(p, line); if (e != PARSE_ERROR_NONE) { print_error(buf, p); break; } } file_close(f); mem_free(parser_priv(p)); parser_destroy(p); } /* Result */ return e == PARSE_ERROR_NONE; }
int cmd_parse_expr(FILE *file, const char *filename, const char *cmd) { log_set_unit(basename(filename)); lexer_init(file); parser_init(); generator_init(); struct node* node = NULL; symtable_t symtable = NULL; code_t code = NULL; if (strcmp(cmd, "parse_expr") == 0) { parser_flags_set(0); node = parser_parse_expr(); print_node(node, 0, 0); } else if (strcmp(cmd, "parse_stmt") == 0) { parser_flags_set(0); node = parser_parse_statement(); print_node(node, 0, 0); } else if (strcmp(cmd, "parse") == 0) { parser_flags_set(PF_RESOLVE_NAMES); symtable = parser_parse(); print_symtable(symtable, 0); } else if (strcmp(cmd, "compile") == 0) { symtable = parser_parse(); if (symtable != NULL) { code = generator_process(symtable); optimizer_optimize(code); generator_print_code(code); } else { print_symtable(symtable, 0); } } parser_free_node(node); symtable_destroy(symtable, 1); generator_free_code(code); generator_destroy(); parser_destroy(); lexer_destroy(); log_close(); return EXIT_SUCCESS; }
/** Main test */ int main( int argc, char **argv ) { setlocale( LC_ALL, "" ); srand( time( 0 ) ); program_name=L"(ignore)"; say( L"Testing low-level functionality"); say( L"Lines beginning with '(ignore):' are not errors, they are warning messages\ngenerated by the fish parser library when given broken input, and can be\nignored. All actual errors begin with 'Error:'." ); proc_init(); halloc_util_init(); event_init(); parser_init(); function_init(); builtin_init(); reader_init(); env_init(); test_util(); test_escape(); test_convert(); test_tok(); test_parser(); test_expand(); test_path(); say( L"Encountered %d errors in low-level tests", err_count ); /* Skip performance tests for now, since they seem to hang when running from inside make (?) */ // say( L"Testing performance" ); // perf_complete(); env_destroy(); reader_destroy(); parser_destroy(); function_destroy(); builtin_destroy(); wutil_destroy(); event_destroy(); proc_destroy(); halloc_util_destroy(); }
static void clear_buffer(ape_socket *co, int *tfd) { free(co->buffer_in.data); co->buffer_in.size = 0; co->buffer_in.length = 0; co->buffer_in.data = NULL; co->buffer_in.slot = NULL; co->buffer_in.islot = 0; co->ip_client[0] = '\0'; parser_destroy(&co->parser); co->attach = NULL; co->data = NULL; co->burn_after_writing = 0; (*tfd)--; }
static errr finish_parse_r(struct parser *p) { struct monster_race *r, *n; r_info = mem_zalloc(sizeof(*r) * z_info->r_max); for (r = parser_priv(p); r; r = r->next) { if (r->ridx >= z_info->r_max) continue; memcpy(&r_info[r->ridx], r, sizeof(*r)); } eval_r_power(r_info); r = parser_priv(p); while (r) { n = r->next; mem_free(r); r = n; } parser_destroy(p); return 0; }
/* Close socket but preserve ape_socket struct */ void close_socket(int fd, acetables *g_ape) { ape_socket *co = g_ape->co[fd]; if (g_ape->bufout[fd].buf != NULL) { free(g_ape->bufout[fd].buf); g_ape->bufout[fd].buflen = 0; g_ape->bufout[fd].buf = NULL; g_ape->bufout[fd].allocsize = 0; } if (co->buffer_in.data != NULL) { free(co->buffer_in.data); } if (co->parser.data != NULL) { parser_destroy(&co->parser); } close(fd); }
int main(int argc, char *argv[]) { if (argc != 2) { printf("Usage: alic <source>\n"); return 0; } if (strcmp(argv[1], "-") != 0) { /* Open source file */ if (freopen(argv[1], "rt", stdin) == NULL) fatal("Unable to open file \"%s\" for reading."); } parser_create(); yyparse(); create_object_file(); parser_destroy(); return 0; }
/** * Destroys the modules */ static void mod_destroy(void) { int do_destroy=0; LOG(L_INFO,"INFO:"M_NAME":mod_destroy: child exit\n"); lock_get(process_lock); if((*shutdown_singleton)==0){ *shutdown_singleton=1; do_destroy=1; } lock_release(process_lock); if (do_destroy){ if (scscf_persistency_mode!=NO_PERSISTENCY){ /* First let's snapshot everything */ make_snapshot_authdata(); make_snapshot_dialogs(); make_snapshot_registrar(); } /* Then nuke it all */ auth_data_destroy(); parser_destroy(); r_notify_destroy(); r_storage_destroy(); s_dialogs_destroy(); lock_get(scscf_dialog_count_lock); shm_free(scscf_dialog_count); lock_destroy(scscf_dialog_count_lock); pkg_free(scscf_service_route.s); } if ( scscf_persistency_mode==WITH_DATABASE_BULK || scscf_persistency_mode==WITH_DATABASE_CACHE) { DBG("INFO:"M_NAME": ... closing db connection\n"); scscf_db_close(); } #ifdef WITH_IMS_PM ims_pm_destroy(); #endif /* WITH_IMS_PM */ }
/** * Parses a DiameterPeer configuration file. * @param filename - path to the file * @returns the dp_config* structure containing the parsed configuration */ dp_config* parse_dp_config(xmlDocPtr doc) { dp_config *x=0; xmlNodePtr root=0,child=0,nephew=0; xmlChar *xc=0; int k; routing_entry *re,*rei; routing_realm *rr,*rri; if (!doc) goto error; x = new_dp_config(); root = xmlDocGetRootElement(doc); if (!root){ LOG(L_ERR,"ERR:parse_dp_config(): Empty XML \n"); goto error; } k = xmlStrlen(root->name); if (k>12) k = 12; if (strncasecmp((char*)root->name,"DiameterPeer",k)!=0){ LOG(L_ERR,"ERR:parse_dp_config(): XML Root is not <DiameterPeer>\n"); goto error; } xc = xmlGetProp(root,(xmlChar*)"FQDN"); if (xc){ quote_trim_dup(&(x->fqdn),(char*)xc); quote_trim_dup(&(x->identity),(char*)xc); xmlFree(xc); } xc = xmlGetProp(root,(xmlChar*)"Realm"); if (xc){ quote_trim_dup(&(x->realm),(char*)xc); xmlFree(xc); } xc = xmlGetProp(root,(xmlChar*)"Vendor_Id"); if (xc) x->vendor_id = atoi((char*)xc); else x->vendor_id = 0; xc = xmlGetProp(root,(xmlChar*)"Product_Name"); if (xc){ quote_trim_dup(&(x->product_name),(char*)xc); xmlFree(xc); } xc = xmlGetProp(root,(xmlChar*)"AcceptUnknownPeers"); if (xc) {x->accept_unknown_peers = atoi((char*)xc);xmlFree(xc);} else x->accept_unknown_peers = 1; xc = xmlGetProp(root,(xmlChar*)"DropUnknownOnDisconnect"); if (xc) {x->drop_unknown_peers = atoi((char*)xc);xmlFree(xc);} else x->drop_unknown_peers = 1; xc = xmlGetProp(root,(xmlChar*)"Tc"); if (xc) {x->tc = atoi((char*)xc);xmlFree(xc);} else x->tc = 30; xc = xmlGetProp(root,(xmlChar*)"Workers"); if (xc) {x->workers = atoi((char*)xc);xmlFree(xc);} else x->workers = 4; xc = xmlGetProp(root,(xmlChar*)"QueueLength"); if (xc) {x->queue_length = atoi((char*)xc);xmlFree(xc);} else x->queue_length = 32; xc = xmlGetProp(root,(xmlChar*)"TransactionTimeout"); if (xc) {x->transaction_timeout = atoi((char*)xc);xmlFree(xc);} else x->transaction_timeout = 5; xc = xmlGetProp(root,(xmlChar*)"SessionsHashSize"); if (xc) {x->sessions_hash_size = atoi((char*)xc);xmlFree(xc);} else x->sessions_hash_size = 128; for(child = root->children; child; child = child->next) if (child->type == XML_ELEMENT_NODE) { if (xmlStrlen(child->name)==4 && strncasecmp((char*)child->name,"Peer",4)==0){ //PEER x->peers_cnt++; } else if (xmlStrlen(child->name)==8 && strncasecmp((char*)child->name,"Acceptor",8)==0){ //Acceptor x->acceptors_cnt++; } else if (xmlStrlen(child->name)==4 && (strncasecmp((char*)child->name,"Auth",4)==0|| strncasecmp((char*)child->name,"Acct",4)==0)){ //Application x->applications_cnt++; } } x->peers = shm_malloc(x->peers_cnt*sizeof(peer_config)); if (!x->peers){ LOG_NO_MEM("shm",x->peers_cnt*sizeof(peer_config)); goto error; } memset(x->peers,0,x->peers_cnt*sizeof(peer_config)); x->peers_cnt=0; x->acceptors = shm_malloc(x->acceptors_cnt*sizeof(acceptor_config)); if (!x->acceptors){ LOG_NO_MEM("shm",x->acceptors_cnt*sizeof(acceptor_config)); goto error; } memset(x->acceptors,0,x->acceptors_cnt*sizeof(acceptor_config)); x->acceptors_cnt=0; x->applications = shm_malloc(x->applications_cnt*sizeof(app_config)); if (!x->applications){ LOG_NO_MEM("shm",x->applications_cnt*sizeof(app_config)); goto error; } memset(x->applications,0,x->applications_cnt*sizeof(app_config)); x->applications_cnt=0; for(child = root->children; child; child = child->next) if (child->type == XML_ELEMENT_NODE) { if (xmlStrlen(child->name)==4 && strncasecmp((char*)child->name,"Peer",4)==0){ //PEER xc = xmlGetProp(child,(xmlChar*)"FQDN"); if (xc){ quote_trim_dup(&(x->peers[x->peers_cnt].fqdn),(char*)xc); xmlFree(xc); } xc = xmlGetProp(child,(xmlChar*)"Realm"); if (xc){ quote_trim_dup(&(x->peers[x->peers_cnt].realm),(char*)xc); xmlFree(xc); } xc = xmlGetProp(child,(xmlChar*)"port"); if (xc){ x->peers[x->peers_cnt].port = atoi((char*)xc); xmlFree(xc); } x->peers_cnt++; } else if (xmlStrlen(child->name)==8 && strncasecmp((char*)child->name,"Acceptor",8)==0){ //Acceptor xc = xmlGetProp(child,(xmlChar*)"bind"); if (xc){ quote_trim_dup(&(x->acceptors[x->acceptors_cnt].bind),(char*)xc); xmlFree(xc); } xc = xmlGetProp(child,(xmlChar*)"port"); if (xc){ x->acceptors[x->acceptors_cnt].port = atoi((char*)xc); xmlFree(xc); } x->acceptors_cnt++; } else if (xmlStrlen(child->name)==4 && ((char*)strncasecmp((char*)child->name,"Auth",4)==0|| strncasecmp((char*)child->name,"Acct",4)==0)){ //Application xc = xmlGetProp(child,(xmlChar*)"id"); if (xc){ x->applications[x->applications_cnt].id = atoi((char*)xc); xmlFree(xc); } xc = xmlGetProp(child,(xmlChar*)"vendor"); if (xc){ x->applications[x->applications_cnt].vendor = atoi((char*)xc); xmlFree(xc); } if (child->name[1]=='u'||child->name[1]=='U') x->applications[x->applications_cnt].type = DP_AUTHORIZATION; else x->applications[x->applications_cnt].type = DP_ACCOUNTING; x->applications_cnt++; } else if (xmlStrlen(child->name)==12 && ((char*)strncasecmp((char*)child->name,"DefaultRoute",12)==0)){ if (!x->r_table) { x->r_table = shm_malloc(sizeof(routing_table)); memset(x->r_table,0,sizeof(routing_table)); } re = new_routing_entry(); if (re){ xc = xmlGetProp(child,(xmlChar*)"FQDN"); if (xc){ quote_trim_dup(&(re->fqdn),(char*)xc); xmlFree(xc); } xc = xmlGetProp(child,(xmlChar*)"metric"); if (xc){ re->metric = atoi((char*)xc); xmlFree(xc); } /* add it the list in ascending order */ if (! x->r_table->routes || re->metric <= x->r_table->routes->metric){ re->next = x->r_table->routes; x->r_table->routes = re; }else{ for(rei=x->r_table->routes;rei;rei=rei->next) if (!rei->next){ rei->next = re; break; }else{ if (re->metric <= rei->next->metric){ re->next = rei->next; rei->next = re; break; } } } } } else if (xmlStrlen(child->name)==5 && ((char*)strncasecmp((char*)child->name,"Realm",5)==0)){ if (!x->r_table) { x->r_table = shm_malloc(sizeof(routing_table)); memset(x->r_table,0,sizeof(routing_table)); } rr = new_routing_realm(); if (rr){ xc = xmlGetProp(child,(xmlChar*)"name"); quote_trim_dup(&(rr->realm),(char*)xc); if (!x->r_table->realms) { x->r_table->realms = rr; }else{ for(rri=x->r_table->realms;rri->next;rri=rri->next); rri->next = rr; } for(nephew = child->children; nephew; nephew = nephew->next) if (nephew->type == XML_ELEMENT_NODE){ if (xmlStrlen(nephew->name)==5 && ((char*)strncasecmp((char*)nephew->name,"Route",5)==0)) { re = new_routing_entry(); if (re) { xc = xmlGetProp(nephew,(xmlChar*)"FQDN"); if (xc){ quote_trim_dup(&(re->fqdn),(char*)xc); xmlFree(xc); } xc = xmlGetProp(nephew,(xmlChar*)"metric"); if (xc){ re->metric = atoi((char*)xc); xmlFree(xc); } /* add it the list in ascending order */ if (! rr->routes || re->metric <= rr->routes->metric){ re->next = rr->routes; rr->routes = re; }else{ for(rei=rr->routes;rei;rei=rei->next) if (!rei->next){ rei->next = re; break; }else{ if (re->metric <= rei->next->metric){ re->next = rei->next; rei->next = re; break; } } } } } } } } } if (doc) xmlFreeDoc(doc); parser_destroy(); return x; error: if (doc) xmlFreeDoc(doc); parser_destroy(); if (x) free_dp_config(x); return 0; }
int teardown_tests(void *state) { parser_destroy(state); return 0; }
static errr finish_parse_stores(struct parser *p) { stores = parser_priv(p); parser_destroy(p); return 0; }
static int dive_cb(const unsigned char *data, unsigned int size, const unsigned char *fingerprint, unsigned int fsize, void *userdata) { int rc; parser_t *parser = NULL; device_data_t *devdata = userdata; dc_datetime_t dt = {0}; struct tm tm; struct dive *dive; rc = create_parser(devdata, &parser); if (rc != PARSER_STATUS_SUCCESS) { fprintf(stderr, "Unable to create parser for %s", devdata->name); return rc; } rc = parser_set_data(parser, data, size); if (rc != PARSER_STATUS_SUCCESS) { fprintf(stderr, "Error registering the data."); parser_destroy(parser); return rc; } dive = alloc_dive(); rc = parser_get_datetime(parser, &dt); if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) { fprintf(stderr, "Error parsing the datetime."); parser_destroy (parser); return rc; } tm.tm_year = dt.year; tm.tm_mon = dt.month-1; tm.tm_mday = dt.day; tm.tm_hour = dt.hour; tm.tm_min = dt.minute; tm.tm_sec = dt.second; dive->when = utc_mktime(&tm); // Parse the divetime. printf("Parsing the divetime.\n"); unsigned int divetime = 0; rc = parser_get_field (parser, FIELD_TYPE_DIVETIME, 0, &divetime); if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) { fprintf(stderr, "Error parsing the divetime."); parser_destroy(parser); return rc; } dive->duration.seconds = divetime; // Parse the maxdepth. printf("Parsing the maxdepth.\n"); double maxdepth = 0.0; rc = parser_get_field(parser, FIELD_TYPE_MAXDEPTH, 0, &maxdepth); if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) { fprintf(stderr, "Error parsing the maxdepth."); parser_destroy(parser); return rc; } dive->maxdepth.mm = maxdepth * 1000 + 0.5; // Parse the gas mixes. printf("Parsing the gas mixes.\n"); unsigned int ngases = 0; rc = parser_get_field(parser, FIELD_TYPE_GASMIX_COUNT, 0, &ngases); if (rc != PARSER_STATUS_SUCCESS && rc != PARSER_STATUS_UNSUPPORTED) { fprintf(stderr, "Error parsing the gas mix count."); parser_destroy(parser); return rc; } rc = parse_gasmixes(dive, parser, ngases); if (rc != PARSER_STATUS_SUCCESS) { fprintf(stderr, "Error parsing the gas mix."); parser_destroy(parser); return rc; } // Initialize the sample data. rc = parse_samples(&dive, parser); if (rc != PARSER_STATUS_SUCCESS) { fprintf(stderr, "Error parsing the samples."); parser_destroy(parser); return rc; } parser_destroy(parser); /* If we already saw this dive, abort. */ if (find_dive(dive, devdata)) return 0; record_dive(dive); return 1; }
/* * Command-line front end for compiler. */ int main(int argc, char **argv) { int action; int arg; buffer_t *in_buffer; buffer_t *out_buffer; int is_done; parser_t *parser; lexer_t *lexer; token_t *token; /* Set default settings. */ action = ACT_TRANS; in_buffer = buffer_create(stdin); out_buffer = buffer_create(stdout); /* Parse command-line arguments. */ for (arg = 1; arg < argc; arg++) { if ((strcmp(argv[arg], "--help") == 0) || (strcmp(argv[arg], "-h") == 0)) { action = ACT_USAGE; } else if ((strcmp(argv[arg], "--lex") == 0) && (action <= ACT_LEX)) { action = ACT_LEX; } else if ((strcmp(argv[arg], "--parse") == 0) && (action <= ACT_PARSE)) { action = ACT_PARSE; } else if ((strcmp(argv[arg], "--translate") == 0) && (action <= ACT_TRANS)) { action = ACT_TRANS; } else { fprintf(stderr, "Invalid argument: %s\n", argv[arg]); /* Stop parsing command-line. */ arg = argc; action = ACT_USAGE; } } /* Take action. */ if (action == ACT_USAGE) { printf( "Usage: compiler [option...]\n" "\n" " Options:\n" "\n" " -h, --help Display this help text.\n" " --lex Run the lexer.\n" " --parse Run the parser. (Calls the lexer.)\n" " --translate Run the translator. (Calls the parser.)\n" ); } else if (action == ACT_LEX) { is_done = 0; lexer = lexer_create(in_buffer); token = token_create(); while (!is_done) { lexer_lex(lexer, token); token_print(token, stdout); printf("\n"); if (token_get_class(token) == T_EOF) is_done = 1; } token_destroy(token); lexer_destroy(lexer); return EXIT_SUCCESS; } else if (action == ACT_PARSE) { parser = parser_create(in_buffer); parser_parse(parser); parser_destroy(parser); return EXIT_SUCCESS; } else if (action == ACT_TRANS) { parser = parser_create(in_buffer); parser_parse(parser); translator_translate(parser_get_tree(parser)); parser_destroy(parser); return EXIT_SUCCESS; } return EXIT_SUCCESS; }
static errr finish_parse_v(struct parser *p) { vaults = parser_priv(p); parser_destroy(p); return 0; }
static errr finish_parse_room(struct parser *p) { room_templates = parser_priv(p); parser_destroy(p); return 0; }
static errr finish_parse_profile(struct parser *p) { struct cave_profile *n, *c = parser_priv(p); int i, num; z_info->profile_max = 0; /* Count the list */ while (c) { struct room_profile *r = c->room_profiles; c->n_room_profiles = 0; z_info->profile_max++; c = c->next; while (r) { c->n_room_profiles++; r = r->next; } } /* Allocate the array and copy the records to it */ cave_profiles = mem_zalloc(z_info->profile_max * sizeof(*c)); num = z_info->profile_max - 1; for (c = parser_priv(p); c; c = n) { struct room_profile *r_new = NULL; /* Main record */ memcpy(&cave_profiles[num], c, sizeof(*c)); n = c->next; if (num < z_info->profile_max - 1) cave_profiles[num].next = &cave_profiles[num + 1]; else cave_profiles[num].next = NULL; /* Count the room profiles */ if (c->room_profiles) { struct room_profile *r = c->room_profiles; c->n_room_profiles = 0; while (r) { c->n_room_profiles++; r = r->next; } } /* Now allocate the room profile array */ if (c->room_profiles) { struct room_profile *r_temp, *r_old = c->room_profiles; /* Allocate space and copy */ r_new = mem_zalloc(c->n_room_profiles * sizeof(*r_new)); for (i = 0; i < c->n_room_profiles; i++) { memcpy(&r_new[i], r_old, sizeof(*r_old)); r_old = r_old->next; if (!r_old) break; } /* Make next point correctly */ for (i = 0; i < c->n_room_profiles; i++) if (r_new[i].next) r_new[i].next = &r_new[i + 1]; /* Tidy up */ r_old = c->room_profiles; r_temp = r_old; while (r_temp) { r_temp = r_old->next; mem_free(r_old); r_old = r_temp; } } cave_profiles[num].room_profiles = r_new; cave_profiles[num].n_room_profiles = c->n_room_profiles; mem_free(c); num--; } parser_destroy(p); return 0; }
/* Create parse object * * It assumes that the ini collection * has been precreated. */ static int parser_create(struct ini_cfgobj *co, FILE *file, const char *config_filename, int error_level, uint32_t collision_flags, uint32_t parse_flags, struct parser_obj **po) { int error = EOK; struct parser_obj *new_po = NULL; unsigned count = 0; TRACE_FLOW_ENTRY(); /* Make sure that all the parts are initialized */ if ((!po) || (!co) || (!(co->cfg)) || (!file) || (!config_filename)) { TRACE_ERROR_NUMBER("Invalid argument", EINVAL); return EINVAL; } error = col_get_collection_count(co->cfg, &count); if (error) { TRACE_ERROR_NUMBER("Failed to check object size", error); return error; } if (count != 1) { TRACE_ERROR_NUMBER("Configuration is not empty", EINVAL); return EINVAL; } new_po = malloc(sizeof(struct parser_obj)); if (!new_po) { TRACE_ERROR_NUMBER("No memory", ENOMEM); return ENOMEM; } /* Save external data */ new_po->file = file; new_po->el = co->error_list; new_po->filename = config_filename; new_po->error_level = error_level; new_po->collision_flags = collision_flags; new_po->parse_flags = parse_flags; new_po->boundary = co->boundary; new_po->co = co; /* Initialize internal varibles */ new_po->sec = NULL; new_po->merge_sec = NULL; new_po->ic = NULL; new_po->last_error = 0; new_po->linenum = 0; new_po->keylinenum = 0; new_po->seclinenum = 0; new_po->last_read = NULL; new_po->last_read_len = 0; new_po->inside_comment = 0; new_po->key = NULL; new_po->key_len = 0; new_po->raw_lines = NULL; new_po->raw_lengths = NULL; new_po->ret = EOK; new_po->merge_key = NULL; new_po->merge_vo = NULL; new_po->merge_error = 0; new_po->top = NULL; new_po->queue = NULL; /* Create top collection */ error = col_create_collection(&(new_po->top), INI_CONFIG_NAME, COL_CLASS_INI_CONFIG); if (error) { TRACE_ERROR_NUMBER("Failed to create top collection", error); parser_destroy(new_po); return error; } /* Create a queue */ error = col_create_queue(&(new_po->queue)); if (error) { TRACE_ERROR_NUMBER("Failed to create queue", error); parser_destroy(new_po); return error; } error = col_enqueue_unsigned_property(new_po->queue, PARSE_ACTION, PARSE_READ); if (error) { TRACE_ERROR_NUMBER("Failed to create queue", error); parser_destroy(new_po); return error; } *po = new_po; TRACE_FLOW_EXIT(); return error; }
/* Top level wrapper around the parser */ int ini_config_parse(struct ini_cfgfile *file_ctx, int error_level, uint32_t collision_flags, uint32_t parse_flags, struct ini_cfgobj *ini_config) { int error = EOK; struct parser_obj *po = NULL; uint32_t fl1, fl2, fl3; TRACE_FLOW_ENTRY(); if ((!ini_config) || (!(ini_config->cfg))) { TRACE_ERROR_NUMBER("Invalid argument", EINVAL); return EINVAL; } if (!file_ctx) { TRACE_ERROR_NUMBER("Invalid file context", EINVAL); return EINVAL; } if (!valid_collision_flags(collision_flags)) { TRACE_ERROR_NUMBER("Invalid flags.", EINVAL); return EINVAL; } if ((error_level != INI_STOP_ON_ANY) && (error_level != INI_STOP_ON_NONE) && (error_level != INI_STOP_ON_ERROR)) { TRACE_ERROR_NUMBER("Invalid argument", EINVAL); return EINVAL; } error = parser_create(ini_config, file_ctx->file, file_ctx->filename, error_level, collision_flags, parse_flags, &po); if (error) { TRACE_ERROR_NUMBER("Failed to perform an action", error); return error; } error = parser_run(po); if (error) { fl1 = collision_flags & INI_MS_MASK; fl2 = collision_flags & INI_MV1S_MASK; fl3 = collision_flags & INI_MV2S_MASK; if ((error == EEXIST) && (((fl1 == INI_MS_DETECT) && (fl2 != INI_MV1S_ERROR) && (fl3 != INI_MV2S_ERROR)) || ((fl2 == INI_MV1S_DETECT) && (fl1 != INI_MS_ERROR) && (fl3 != INI_MV2S_ERROR)) || ((fl3 == INI_MV2S_DETECT) && (fl1 != INI_MS_ERROR) && (fl2 != INI_MV1S_ERROR)))) { TRACE_ERROR_NUMBER("No error in detect mode", error); /* Fall through */ } else { TRACE_ERROR_NUMBER("Failed to parse file", error); TRACE_ERROR_NUMBER("Mode", collision_flags); col_get_collection_count(ini_config->error_list, &(ini_config->count)); if(ini_config->count) (ini_config->count)--; parser_destroy(po); return error; } } /* If should be empty anyways */ col_destroy_collection_with_cb(ini_config->cfg, ini_cleanup_cb, NULL); ini_config->cfg = po->top; po->top = NULL; parser_destroy(po); TRACE_FLOW_EXIT(); return error; }
static int teardown(void *state) { parser_destroy(state); return 0; }