bool Nuria::Tokenizer::readAndHandleTokens () { auto end = this->d_ptr->currentSet->actions.constEnd (); while (readTokens ()) { auto it = this->d_ptr->currentSet->actions.constFind (this->d_ptr->token.tokenId); // Token action handler found? if (it == end) { return true; } // Invoke handler if (!(*it) (this->d_ptr->token, this)) { return false; } // Accept token? if (this->d_ptr->token.tokenId >= 0) { return true; } } // Error return false; }
/* * * name: main * * Receives information from the user of the input and output files, and then * makes appropriate calls. * * @param argc the number of arguments passed (including the program) * @param argv the argument array of the program call * @return error code */ int main(int argc, char** argv){ line current; sourceContainer source; source.current = ¤t; char input[MAX_FILE_LEN]; int i; token tokenList[MAX_TOKENS]; // The user can pass a parameter to the program for the file name. // If no parameter is given, the program will ask explicitly. if(argc == 2){ strcpy(input, argv[1]); } else{ printf("\n Name of your input file (%d characters max): ", MAX_FILE_LEN); scanf("%s", input); } source.infile = fopen(input, "r"); if(source.infile == NULL){ printf("Could not open input file!\n"); exit(1); } // prepare and build the token table for(i=0;i<HASH_TABLE_SIZE;i++){ source.hashTable[i] = NULL; source.symbolTable[i] = NULL; } readTokens(tokenList, "tokens"); buildHashes(source.hashTable, tokenList); source.current->scanIndex = 0; source.current->lineNumber = 0; source.current->atEOF = 0; memset(source.current->line, '\0', MAX_LINE_LEN); // parse the source if(prog(&source)){ printf("\n\nParse successful!\n"); } else{ while(!source.current->atEOF){ getLine(source.current, source.infile); } printf("\n\nParse failure!\n"); } printf("\nSymbol table:\n"); printHash(source.symbolTable); return 0; }
int HSPReadConfigFile(HSP *sp) { EnumHSPObject level[HSP_MAX_CONFIG_DEPTH + 5]; int depth = 0; level[depth] = HSPOBJ_HSP; // could have used something like bison to make a complete parser with // strict rules, but for simplicity we just allow the current object // to double as a state variable that determines what is allowed next. for(HSPToken *tok = readTokens(sp); tok; tok = tok->nxt) { if(depth > HSP_MAX_CONFIG_DEPTH) { // depth overrun parseError(sp, tok, "too many '{'s", ""); return NO; } else if(tok->stok == HSPTOKEN_ENDOBJ) { // end of level, pop the stack if(depth > 0) --depth; else { parseError(sp, tok, "too many '}'s ", ""); return NO; } } else switch(level[depth]) { case HSPOBJ_HSP: // must start by opening an sFlow object if((tok = expectToken(sp, tok, HSPTOKEN_SFLOW)) == NULL) return NO; if((tok = expectToken(sp, tok, HSPTOKEN_STARTOBJ)) == NULL) return NO; newSFlow(sp); level[++depth] = HSPOBJ_SFLOW; break; case HSPOBJ_SFLOW: switch(tok->stok) { case HSPTOKEN_LOOPBACK: if((tok = expectLoopback(sp, tok)) == NULL) return NO; break; case HSPTOKEN_DNSSD: if((tok = expectDNSSD(sp, tok)) == NULL) return NO; break; case HSPTOKEN_DNSSD_DOMAIN: if((tok = expectDNSSD_domain(sp, tok)) == NULL) return NO; break; case HSPTOKEN_COLLECTOR: if((tok = expectToken(sp, tok, HSPTOKEN_STARTOBJ)) == NULL) return NO; newCollector(sp->sFlow->sFlowSettings_file); level[++depth] = HSPOBJ_COLLECTOR; break; case HSPTOKEN_SAMPLING: case HSPTOKEN_PACKETSAMPLINGRATE: if((tok = expectInteger32(sp, tok, &sp->sFlow->sFlowSettings_file->samplingRate, 0, 65535)) == NULL) return NO; break; case HSPTOKEN_POLLING: case HSPTOKEN_COUNTERPOLLINGINTERVAL: if((tok = expectInteger32(sp, tok, &sp->sFlow->sFlowSettings_file->pollingInterval, 0, 300)) == NULL) return NO; break; case HSPTOKEN_AGENTIP: if((tok = expectIP(sp, tok, &sp->sFlow->agentIP, NULL)) == NULL) return NO; sp->sFlow->explicitAgentIP = YES; break; case HSPTOKEN_AGENTCIDR: { HSPCIDR cidr = { 0 }; if((tok = expectCIDR(sp, tok, &cidr)) == NULL) return NO; addAgentCIDR(sp->sFlow->sFlowSettings_file, &cidr); } break; case HSPTOKEN_AGENT: if((tok = expectDevice(sp, tok, &sp->sFlow->agentDevice)) == NULL) return NO; sp->sFlow->explicitAgentDevice = YES; break; case HSPTOKEN_SUBAGENTID: if((tok = expectInteger32(sp, tok, &sp->sFlow->subAgentId, 0, HSP_MAX_SUBAGENTID)) == NULL) return NO; break; case HSPTOKEN_UUID: if((tok = expectUUID(sp, tok, sp->uuid)) == NULL) return NO; break; case HSPTOKEN_HEADERBYTES: if((tok = expectInteger32(sp, tok, &sp->sFlow->sFlowSettings_file->headerBytes, 0, HSP_MAX_HEADER_BYTES)) == NULL) return NO; break; case HSPTOKEN_ULOGGROUP: if((tok = expectInteger32(sp, tok, &sp->sFlow->sFlowSettings_file->ulogGroup, 1, 32)) == NULL) return NO; break; case HSPTOKEN_ULOGPROBABILITY: if((tok = expectDouble(sp, tok, &sp->sFlow->sFlowSettings_file->ulogProbability, 0.0, 1.0)) == NULL) return NO; break; case HSPTOKEN_JSONPORT: if((tok = expectInteger32(sp, tok, &sp->sFlow->sFlowSettings_file->jsonPort, 1025, 65535)) == NULL) return NO; break; default: // handle wildcards here - allow sampling.<app>=<n> and polling.<app>=<secs> if(tok->str && strncasecmp(tok->str, "sampling.", 9) == 0) { char *app = tok->str + 9; uint32_t sampling_n=0; if((tok = expectInteger32(sp, tok, &sampling_n, 0, 65535)) == NULL) return NO; setApplicationSampling(sp->sFlow->sFlowSettings_file, app, sampling_n); } else if(tok->str && strncasecmp(tok->str, "polling.", 8) == 0) { char *app = tok->str + 8; uint32_t polling_secs=0; if((tok = expectInteger32(sp, tok, &polling_secs, 0, 300)) == NULL) return NO; setApplicationPolling(sp->sFlow->sFlowSettings_file, app, polling_secs); } else { parseError(sp, tok, "unexpected sFlow setting", ""); return NO; } break; } break; case HSPOBJ_COLLECTOR: { HSPCollector *col = sp->sFlow->sFlowSettings_file->collectors; switch(tok->stok) { case HSPTOKEN_IP: if((tok = expectIP(sp, tok, &col->ipAddr, (struct sockaddr *)&col->sendSocketAddr)) == NULL) return NO; break; case HSPTOKEN_UDPPORT: if((tok = expectInteger32(sp, tok, &col->udpPort, 1, 65535)) == NULL) return NO; break; default: parseError(sp, tok, "unexpected collector setting", ""); return NO; break; } } break; default: parseError(sp, tok, "unexpected state", ""); } } // OK we consumed all the tokens, but we still need to run some sanity checks to make sure // we have a usable configuration... int parseOK = YES; if(sp->sFlow == NULL) { myLog(LOG_ERR, "parse error in %s : sFlow not found", sp->configFile); parseOK = NO; } else { if(sp->sFlow->sFlowSettings_file->numCollectors == 0 && sp->DNSSD == NO) { myLog(LOG_ERR, "parse error in %s : DNS-SD is off and no collectors are defined", sp->configFile); parseOK = NO; } for(HSPCollector *coll = sp->sFlow->sFlowSettings_file->collectors; coll; coll = coll->nxt) { //////////////////////// collector ///////////////////////// if(coll->ipAddr.type == 0) { myLog(LOG_ERR, "parse error in %s : collector has no IP", sp->configFile); parseOK = NO; } } if(sp->sFlow->sFlowSettings_file->ulogProbability > 0) { sp->sFlow->sFlowSettings_file->ulogSamplingRate = (uint32_t)(1.0 / sp->sFlow->sFlowSettings_file->ulogProbability); } } return parseOK; }
int HSPReadConfigFile(HSP *sp) { EnumHSPObject level[HSP_MAX_CONFIG_DEPTH + 5]; int depth = 0; level[depth] = HSPOBJ_HSP; // could have used something like bison to make a complete parser with // strict rules, but for simplicity we just allow the current object // to double as a state variable that determines what is allowed next. for(HSPToken *tok = readTokens(sp); tok; tok = tok->nxt) { if(depth > HSP_MAX_CONFIG_DEPTH) { // depth overrun parseError(sp, tok, "too many '{'s", ""); return NO; } else if(tok->stok == HSPTOKEN_ENDOBJ) { // end of level, pop the stack if(depth > 0) --depth; else { parseError(sp, tok, "too many '}'s ", ""); return NO; } } else switch(level[depth]) { case HSPOBJ_HSP: // must start by opening an sFlow object if((tok = expectToken(sp, tok, HSPTOKEN_SFLOW)) == NULL) return NO; if((tok = expectToken(sp, tok, HSPTOKEN_STARTOBJ)) == NULL) return NO; newSFlow(sp); level[++depth] = HSPOBJ_SFLOW; break; case HSPOBJ_SFLOW: switch(tok->stok) { case HSPTOKEN_DNSSD: if((tok = expectDNSSD(sp, tok)) == NULL) return NO; break; case HSPTOKEN_DNSSD_DOMAIN: if((tok = expectDNSSD_domain(sp, tok)) == NULL) return NO; break; case HSPTOKEN_COLLECTOR: if((tok = expectToken(sp, tok, HSPTOKEN_STARTOBJ)) == NULL) return NO; newCollector(sp->sFlow->sFlowSettings_file); level[++depth] = HSPOBJ_COLLECTOR; break; case HSPTOKEN_SAMPLING: case HSPTOKEN_PACKETSAMPLINGRATE: if((tok = expectInteger32(sp, tok, &sp->sFlow->sFlowSettings_file->samplingRate, 0, 65535)) == NULL) return NO; break; case HSPTOKEN_POLLING: case HSPTOKEN_COUNTERPOLLINGINTERVAL: if((tok = expectInteger32(sp, tok, &sp->sFlow->sFlowSettings_file->pollingInterval, 0, 300)) == NULL) return NO; break; case HSPTOKEN_AGENTIP: if((tok = expectIP(sp, tok, &sp->sFlow->agentIP, NULL)) == NULL) return NO; break; case HSPTOKEN_AGENT: if((tok = expectDevice(sp, tok, &sp->sFlow->agentDevice)) == NULL) return NO; break; case HSPTOKEN_SUBAGENTID: if((tok = expectInteger32(sp, tok, &sp->sFlow->subAgentId, 0, HSP_MAX_SUBAGENTID)) == NULL) return NO; break; case HSPTOKEN_UUID: if((tok = expectUUID(sp, tok, sp->uuid)) == NULL) return NO; break; case HSPTOKEN_HEADERBYTES: if((tok = expectInteger32(sp, tok, &sp->sFlow->sFlowSettings_file->headerBytes, 0, HSP_MAX_HEADER_BYTES)) == NULL) return NO; break; case HSPTOKEN_ULOGGROUP: if((tok = expectInteger32(sp, tok, &sp->sFlow->sFlowSettings_file->ulogGroup, 1, 32)) == NULL) return NO; break; case HSPTOKEN_ULOGPROBABILITY: if((tok = expectDouble(sp, tok, &sp->sFlow->sFlowSettings_file->ulogProbability, 0.0, 1.0)) == NULL) return NO; break; default: parseError(sp, tok, "unexpected sFlow setting", ""); return NO; break; } break; case HSPOBJ_COLLECTOR: { HSPCollector *col = sp->sFlow->sFlowSettings_file->collectors; switch(tok->stok) { case HSPTOKEN_IP: if((tok = expectIP(sp, tok, &col->ipAddr, (struct sockaddr *)&col->sendSocketAddr)) == NULL) return NO; break; case HSPTOKEN_UDPPORT: if((tok = expectInteger32(sp, tok, &col->udpPort, 1, 65535)) == NULL) return NO; break; default: parseError(sp, tok, "unexpected collector setting", ""); return NO; break; } } break; default: parseError(sp, tok, "unexpected state", ""); } } // OK we consumed all the tokens, but we still need to run some sanity checks to make sure // we have a usable configuration... int parseOK = YES; if(sp->sFlow == NULL) { myLog(LOG_ERR, "parse error in %s : sFlow not found", sp->configFile); parseOK = NO; } else { //////////////////////// sFlow ///////////////////////// if(sp->sFlow->agentIP.type == 0) { // it may have been defined as agent=<device> if(sp->sFlow->agentDevice) { SFLAdaptor *ad = adaptorListGet(sp->adaptorList, sp->sFlow->agentDevice); if(ad && ad->ipAddr.addr) { sp->sFlow->agentIP.type = SFLADDRESSTYPE_IP_V4; sp->sFlow->agentIP.address.ip_v4 = ad->ipAddr; } } } if(sp->sFlow->agentIP.type == 0) { // nae luck - try to automatically choose the first non-loopback IP address for(uint32_t i = 0; i < sp->adaptorList->num_adaptors; i++) { SFLAdaptor *adaptor = sp->adaptorList->adaptors[i]; // only the non-loopback devices should be listed here, so just take the first if(adaptor && adaptor->ipAddr.addr) { sp->sFlow->agentIP.type = SFLADDRESSTYPE_IP_V4; sp->sFlow->agentIP.address.ip_v4 = adaptor->ipAddr; // fill in the device that we picked too sp->sFlow->agentDevice = strdup(adaptor->deviceName); break; } } } if(sp->sFlow->agentIP.type == SFLADDRESSTYPE_IP_V4 && sp->sFlow->agentDevice == NULL) { // try to fill in the device field too (because we need to give that one to open vswitch). for(uint32_t i = 0; i < sp->adaptorList->num_adaptors; i++) { SFLAdaptor *adaptor = sp->adaptorList->adaptors[i]; if(adaptor && (adaptor->ipAddr.addr == sp->sFlow->agentIP.address.ip_v4.addr)) { sp->sFlow->agentDevice = strdup(adaptor->deviceName); break; } } } if(sp->sFlow->agentIP.type == 0) { // still no agentIP. That's a showstopper. myLog(LOG_ERR, "parse error in %s : agentIP not defined", sp->configFile); parseOK = NO; } if(sp->sFlow->sFlowSettings_file->numCollectors == 0 && sp->DNSSD == NO) { myLog(LOG_ERR, "parse error in %s : DNS-SD is off and no collectors are defined", sp->configFile); parseOK = NO; } for(HSPCollector *coll = sp->sFlow->sFlowSettings_file->collectors; coll; coll = coll->nxt) { //////////////////////// collector ///////////////////////// if(coll->ipAddr.type == 0) { myLog(LOG_ERR, "parse error in %s : collector has no IP", sp->configFile); parseOK = NO; } } if(sp->sFlow->sFlowSettings_file->ulogProbability > 0) { sp->sFlow->sFlowSettings_file->ulogSamplingRate = (uint32_t)(1.0 / sp->sFlow->sFlowSettings_file->ulogProbability); } } return parseOK; }
LinkedList * readFile(const char * filename, int count, int * samplesRead) { printf(DEBUG_HEADER_INFO"Calling readFile on file %s, count %d\n", filename, count); //int lines = countLines(filename); if (INVALID_NEGATIVE_PARAM(count)) { printf(DEBUG_HEADER_INFO"Err: Negative count parameter: %d.", count); return NULL; } // open the file FILE * fp = fopen(filename, "r"); if (!fp) { char errorMessage[100]; strcat(strcpy(errorMessage, " Error while opening file "), filename); perror(errorMessage); printf(DEBUG_HEADER_INFO"Returning NULL for readFile %s count %d", filename, count); return NULL; } //TODO free LinkedList * list = makeList(); int samplesCounter = 0; //allocate memory for analysing samples char * currentSample = (char *)malloc(SAMPLE_BYTES); //TODO free char **tokens = (char **)malloc(SAMPLE_COMPONENTS_COUNT * sizeof(char *)); ////time //tokens[0] = (char *)malloc(SAMPLE_COMPONENT_SIZE); //// order //tokens[1] = (char *)malloc(SAMPLE_COMPONENT_SIZE); ////x y z //tokens[2] = (char *)malloc(SAMPLE_COMPONENT_SIZE); //tokens[3] = (char *)malloc(SAMPLE_COMPONENT_SIZE); //tokens[4] = (char *)malloc(SAMPLE_COMPONENT_SIZE); for (int i = 0; i < SAMPLE_COMPONENTS_COUNT; i++) { tokens[i] = (char *)malloc(SAMPLE_COMPONENT_SIZE); } // loop over lines while (fgets(currentSample, SAMPLE_BYTES - 1, fp)) { //printf(" Next sample read is: %s\n", currentSample); if (readTokens(tokens, currentSample)) { samplesCounter++; printf(DEBUG_HEADER_INFO); for (int i = 0; i < SAMPLE_COMPONENTS_COUNT; i++) { printf("token %d: %-15s // ", i, tokens[i]); } printf("\n"); /*DO THE DATA PARSING*/ // tl needed char * tl; /*============================================================= parse the time ==============================================================*/ // old time received, does not work anymore //CAWAX_TIME_MSM mili = 0; //CAWAX_TIME_MSM sec = 0; //CAWAX_TIME_MSM minute = 0; //char * mili_s = (char *) malloc(4); // //sscanf(tokens[0], "%d:%d.%s", &minute, &sec, mili_s); ////printf("mili_s is: %s\n", mili_s); //mili = strtol(mili_s, &tl, 10) * pow(10, 3 - strlen(mili_s)); //free(mili_s); //printf("min: %d, sec: %d, %mili: %d\n", minute, sec, mili); //CAWAX_TIME_MSM time = CAWAX_TIME_FROM_MSM(minute, sec, mili); //printf("\n********** Time extracted is: %d, revalidate: min is: %d, sec is: %d, mili is: %d \n", time, // CAWAX_TIME_GET_MINUTE(time), CAWAX_TIME_GET_SECOND(time), CAWAX_TIME_GET_MILLISECOND(time)); // correct new time (internal time of the imu) INTERNAL_TIME second = 0; INTERNAL_TIME micro = 0; // needs to handle round micro values char * micro_s = (char *) malloc(7); sscanf(tokens[TIME_INTERNAL_BUF_INDEX], "%d.%s", &second, micro_s); micro = strtol(micro_s, &tl, 10) * pow(10, 6 - strlen(micro_s)); free(micro_s); INTERNAL_TIME internalTime = INTERNAL_TIME_FROM_S(second, micro); //printf("\n********** Time extracted is: %d, revalidate: sec is: %d, micro is: %d \n", internalTime, INTERNAL_TIME_GET_SECOND(internalTime), INTERNAL_TIME_GET_MICRO(internalTime)); /* ============================================================= parse the order ================================================================*/ sample_th order; sscanf(tokens[SAMPLE_INDEX_BUF_INDEX], "%d", &order); //printf("sample order: %d\n", order); /* ============================================================= parse the accelerometer values x, y, z ================================================================*/ acc x; acc y; acc z; sscanf(tokens[X_BUF_INDEX], "%lf", &x); sscanf(tokens[Y_BUF_IDNEX], "%lf", &y); sscanf(tokens[Z_BUF_INDEX], "%lf", &z); //printf("x: %.6f, y: %.6f, z: %.6f \n", x, y, z); // todo add a param for optional computation Sample sample = { internalTime, order, x, y, z, 0 }; sample.rmq = rootMeanSquare(&sample); add(sample, list); } //printf("Samples read so far: %d\n", ++samplesCounter); } *samplesRead = samplesCounter; printf(DEBUG_HEADER_INFO"Calling readFile on file %s finished. %d samples read.\n", filename, samplesCounter); //free char** tokens and elements free(currentSample); for (int i = 0; i < SAMPLE_COMPONENTS_COUNT; i++) { free(tokens[i]); } free(tokens); return list; }