gboolean cfg_run_parser(GlobalConfig *self, CfgLexer *lexer, CfgParser *parser, gpointer *result, gpointer arg) { gboolean res; GlobalConfig *old_cfg; CfgLexer *old_lexer; old_cfg = configuration; configuration = self; old_lexer = self->lexer; self->lexer = lexer; cfg_args_set(self->lexer->globals, "syslog-ng-root", PATH_PREFIX); cfg_args_set(self->lexer->globals, "syslog-ng-data", PATH_DATADIR); cfg_args_set(self->lexer->globals, "module-path", module_path); cfg_args_set(self->lexer->globals, "include-path", PATH_SYSCONFDIR); cfg_args_set(self->lexer->globals, "autoload-compiled-modules", "1"); res = cfg_parser_parse(parser, lexer, result, arg); cfg_lexer_free(lexer); self->lexer = NULL; self->lexer = old_lexer; configuration = old_cfg; return res; }
void cfgi_parse(struct cfgi_t *cih, char *filename, void *ctx) { cih->ctx = ctx; cfg_parser_parse(cih->parser, filename, (void*) cih); strmap_iterate(cih->types, cfgi_iterate_objects, cih); strmap_iterate(cih->types, cfgi_destroy_objects, NULL); cih->ctx = NULL; }
gboolean cfg_run_parser(GlobalConfig *self, CfgLexer *lexer, CfgParser *parser, gpointer *result, gpointer arg) { gboolean res; GlobalConfig *old_cfg; CfgLexer *old_lexer; old_cfg = configuration; configuration = self; old_lexer = self->lexer; self->lexer = lexer; cfg_set_global_paths(self); res = cfg_parser_parse(parser, lexer, result, arg); cfg_lexer_free(lexer); self->lexer = NULL; self->lexer = old_lexer; configuration = old_cfg; return res; }
/* construct a plugin instance by parsing a configuration file */ gpointer plugin_parse_config(Plugin *self, GlobalConfig *cfg, YYLTYPE *yylloc, gpointer arg) { gpointer instance = NULL; g_assert(self->construct == NULL); /* make sure '_' and '-' are handled equally in plugin name */ if (!self->setup_context) { CfgTokenBlock *block; YYSTYPE token; block = cfg_token_block_new(); memset(&token, 0, sizeof(token)); token.type = LL_TOKEN; token.token = self->type; cfg_token_block_add_token(block, &token); cfg_lexer_push_context(cfg->lexer, self->parser->context, self->parser->keywords, self->parser->name); cfg_lexer_lookup_keyword(cfg->lexer, &token, yylloc, self->name); cfg_lexer_pop_context(cfg->lexer); cfg_token_block_add_token(block, &token); cfg_lexer_inject_token_block(cfg->lexer, block); } else { (self->setup_context)(self, cfg, self->type, self->name); } if (!cfg_parser_parse(self->parser, cfg->lexer, &instance, arg)) { cfg_parser_cleanup(self->parser, instance); instance = NULL; } return instance; }
int cfg_lexer_lex(CfgLexer *self, YYSTYPE *yylval, YYLTYPE *yylloc) { CfgBlockGenerator *gen; gint tok; gboolean injected; relex: injected = cfg_lexer_consume_next_injected_token(self, &tok, yylval, yylloc); if (!injected) { if (cfg_lexer_get_context_type(self) == LL_CONTEXT_BLOCK_CONTENT) cfg_lexer_start_block_state(self, "{}"); else if (cfg_lexer_get_context_type(self) == LL_CONTEXT_BLOCK_ARG) cfg_lexer_start_block_state(self, "()"); yylval->type = 0; g_string_truncate(self->token_text, 0); g_string_truncate(self->token_pretext, 0); tok = _invoke__cfg_lexer_lex(self, yylval, yylloc); if (yylval->type == 0) yylval->type = tok; if (self->preprocess_output) g_string_append_printf(self->preprocess_output, "%s", self->token_pretext->str); } /* NOTE: most of the code below is a monster, which should be factored out * to tiny little functions. This is not very simple and I am in the * middle of something that I would rather close than doing the * refactoring desperately needed here. I am silencing my conscience with * this note and also take the time to document some of the quirks below. * * 1) This code is deeply coupled with GlobalConfig and most of it does * not make sense to execute if self->cfg is NULL. Thus, some of the * conditionals contain an explicit self->cfg check, in other cases it is * implicitly checked by the first conditional of a series of if-then-else * statements. * * 2) the role of the relex label is to restart the lexing process once * new tokens were injected into the input stream. (e.g. after a * generator was called). This should really be a loop, and quite * possible any refactors should start here by eliminating that * loop-using-goto * * 3) make note that string tokens are allocated by malloc/free and not * g_malloc/g_free, this is significant. The grammar contains the free() * call, so getting rid of that would require a lot of changes to the * grammar. (on Windows glib, malloc/g_malloc are NOT equivalent) * */ if (tok == LL_IDENTIFIER && self->cfg && (gen = cfg_lexer_find_generator(self, self->cfg, cfg_lexer_get_context_type(self), yylval->cptr))) { CfgArgs *args; CfgIncludeLevel *level = &self->include_stack[self->include_depth]; self->preprocess_suppress_tokens++; gint saved_line = level->lloc.first_line; gint saved_column = level->lloc.first_column; if (cfg_parser_parse(&block_ref_parser, self, (gpointer *) &args, NULL)) { gboolean success; gchar buf[256]; GString *result = g_string_sized_new(256); level->lloc.first_line = saved_line; level->lloc.first_column = saved_column; self->preprocess_suppress_tokens--; success = cfg_block_generator_generate(gen, self->cfg, args, result, cfg_lexer_format_location(self, &level->lloc, buf, sizeof(buf))); free(yylval->cptr); cfg_args_unref(args); if (!success) { g_string_free(result, TRUE); return LL_ERROR; } cfg_block_generator_format_name(gen, buf, sizeof(buf)); if (gen->suppress_backticks) success = cfg_lexer_include_buffer_without_backtick_substitution(self, buf, result->str, result->len); else success = cfg_lexer_include_buffer(self, buf, result->str, result->len); g_string_free(result, TRUE); if (!success) return LL_ERROR; goto relex; } else { level->lloc.first_line = saved_line; level->lloc.first_column = saved_column; free(yylval->cptr); self->preprocess_suppress_tokens--; return LL_ERROR; } } if (self->ignore_pragma || self->cfg == NULL) { /* only process @pragma/@include tokens in case pragma allowed is set * and the associated configuration is not NULL */ ; } else if (tok == LL_PRAGMA) { gpointer dummy; if (self->preprocess_output) g_string_append_printf(self->preprocess_output, "@"); if (!cfg_parser_parse(&pragma_parser, self, &dummy, NULL)) { return LL_ERROR; } goto relex; } else if (tok == KW_INCLUDE && cfg_lexer_get_context_type(self) != LL_CONTEXT_PRAGMA) { gchar *include_file; self->preprocess_suppress_tokens++; tok = cfg_lexer_lex(self, yylval, yylloc); if (tok != LL_STRING && tok != LL_IDENTIFIER) { self->preprocess_suppress_tokens--; return LL_ERROR; } include_file = g_strdup(yylval->cptr); free(yylval->cptr); tok = cfg_lexer_lex(self, yylval, yylloc); if (tok != ';') { self->preprocess_suppress_tokens--; g_free(include_file); return LL_ERROR; } if (!cfg_lexer_include_file(self, include_file)) { g_free(include_file); self->preprocess_suppress_tokens--; return LL_ERROR; } self->preprocess_suppress_tokens--; g_free(include_file); goto relex; } else if (self->cfg->user_version == 0 && self->cfg->parsed_version != 0) { if (!cfg_set_version(self->cfg, configuration->parsed_version)) return LL_ERROR; } else if (cfg_lexer_get_context_type(self) != LL_CONTEXT_PRAGMA && !self->non_pragma_seen) { /* first non-pragma token */ if (self->cfg->user_version == 0 && self->cfg->parsed_version == 0) { msg_error("ERROR: configuration files without a version number has become unsupported in " VERSION_3_13 ", please specify a version number using @version and update your configuration accordingly"); return LL_ERROR; } cfg_load_candidate_modules(self->cfg); cfg_load_forced_modules(self->cfg); self->non_pragma_seen = TRUE; } if (!injected) { if (self->preprocess_suppress_tokens == 0 && self->preprocess_output) { g_string_append_printf(self->preprocess_output, "%s", self->token_text->str); } } return tok; }
int main(int argc, char *argv[]) { char cfg[MAX_FILE_NAME_SIZE]="config.json"; float lPow,rPow; int state=STOP, stoppedState=RUN; int beaconToFollow=0; int ret = 0; rob_cfg_t rob_cfg; rob_state_t rob_state; struct beaconMeasure beacon; int totalBeacons = 0,curGroundSensor = -1; double elapsed1 = 0.0, elapsed2 = 0.0, realTotal = 0.0; struct timeval t1, t2, t3; bool firstTimeStart = 1; memset(&rob_state, 0, sizeof(rob_state_t)); /* processing arguments */ while (argc > 2) { if (strcmp(argv[1], "-cfg") == 0) { strncpy(cfg, argv[2], 99); cfg[MAX_FILE_NAME_SIZE-1]='\0'; } else { break; /* the while */ } argc -= 2; argv += 2; } cfg_parser_parse(cfg, &rob_cfg); // int i; // for(i = 0; i < rob_cfg.rob_viewer_size; i++) // printf("Viewer: %s:%d\n", rob_cfg.rob_viewers[i].hostname, rob_cfg.rob_viewers[i].port); InitJoystick(rob_cfg.joys_dev); cfg_parser_connect_viewers(&rob_cfg); /* Connect Robot to simulator */ if( InitRobot(rob_cfg.robo_name, rob_cfg.robo_pos, rob_cfg.hostname) == -1) { ret = 1; printf( "%s Failed to connect\n", rob_cfg.robo_name); } else { totalBeacons = GetNumberOfBeacons(); printf( "Connected: %s, Total beacons: %d\n", rob_cfg.robo_name, totalBeacons); state=STOP; while(1) { /* Reading next values from Sensors */ ReadSensors(); if(GetFinished()) /* Simulator has received Finish() or Robot Removed */ { printf( "Exiting: %s\n", rob_cfg.robo_name ); state = FINISHED; gettimeofday(&t3, NULL); elapsed2 = _get_elapsed_secs(&t2, &t3); realTotal = _get_elapsed_secs(&t1, &t3); printf("to beacon | to start | total | real total\n"); printf("& %.2f & %.2f & %.2f & %.2f \n", elapsed1, elapsed2, elapsed1 + elapsed2, realTotal); break; } if(state==STOP && GetStartButton()) { state=stoppedState; /* Restart */ if( firstTimeStart ) { firstTimeStart = 0; printf("Started counting elapsed time\n"); gettimeofday(&t1, NULL); } } if(state!=STOP && GetStopButton()) { stoppedState=state; state=STOP; /* Interrupt */ } curGroundSensor = GetGroundSensor(); switch (state) { case RUN: /* Go */ if( GetVisitingLed() ) { gettimeofday(&t2, NULL); elapsed1 = _get_elapsed_secs(&t1, &t2); printf("Elapsed from origin to beacon: %f\n", elapsed1); state = WAIT; DriveMotors(0.0,0.0); } else { if( curGroundSensor == beaconToFollow ) { beaconToFollow++; SetVisitingLed(1); printf("%s visited target at %d\n", rob_cfg.robo_name, GetTime()); } else { DetermineAction(beaconToFollow, &lPow, &rPow); DriveMotors(lPow, rPow); } } break; case RETURN: /* Go */ if( curGroundSensor == totalBeacons ) { printf("%s found home at %d\n", rob_cfg.robo_name, GetTime()); Finish(); } else { DetermineAction(beaconToFollow, &lPow, &rPow); DriveMotors(lPow, rPow); } break; case WAIT: /* Wait for others to visit target */ if(GetReturningLed()) { SetVisitingLed(0); state = RETURN; gettimeofday(&t2, NULL); } DriveMotors(0.0,0.0); break; } //Say(rob_cfg.robo_name); rob_state.state = state; if( (rob_state.leftAvail = IsObstacleReady(LEFT)) ) rob_state.left = GetObstacleSensor(LEFT); if( (rob_state.rightAvail = IsObstacleReady(RIGHT)) ) rob_state.right = GetObstacleSensor(RIGHT); if( (rob_state.centerAvail = IsObstacleReady(CENTER)) ) rob_state.center = GetObstacleSensor(CENTER); if(IsGPSReady()) { rob_state.x = GetX(); rob_state.y = GetY(); } // if( IsGPSDirReady() ) // rob_state.dir = GetDir(); if( IsCompassReady() ) rob_state.dir = GetCompassSensor(); if( ( rob_state.beaconVis = IsBeaconReady(beaconToFollow) ) ) { beacon = GetBeaconSensor(beaconToFollow); if( ( rob_state.beaconVis = beacon.beaconVisible ) ) rob_state.beaconDir = beacon.beaconDir; } send_all_viewer_state_message(&rob_cfg, &rob_state); RequestCompassSensor(); //Request Sensors for next cycle if(GetTime() % 2 == 0) { RequestObstacleSensor(CENTER); if( (GetTime() % 8) == 0 || beaconToFollow == totalBeacons ) RequestGroundSensor(); else RequestBeaconSensor(beaconToFollow); } else { RequestSensors(2, "IRSensor1", "IRSensor2"); } } send_all_viewer_state_message(&rob_cfg, &rob_state); } printf("Doing cleanup: %s\n", rob_cfg.robo_name); CloseAndFreeJoystick(); cfg_parser_close(&rob_cfg); return ret; }