⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 executor.cpp

📁 基于单片机的 snmp协议解析的一些原代码 给有用的 同行
💻 CPP
📖 第 1 页 / 共 4 页
字号:
//  Copyright (c) 1996  Federal Highway Administration
//
//  This software has been developed for the Federal Highway Administration
//  by Viggen Corporation under contract with Oak Ridge National Lab.
//
//  Permission to use, copy, and distribute this software for any purpose
//  without fee is hereby granted, provided that the above copyright notice
//  appears in all copies and tFhat both the copyright and this permission notice
//  appear in the supporting documentation.
//
//  Permission to modify this software is granted provided that the above
//  copyright and this permission notice appears in the modified software.
//
//  This software is provided "as is" with no warranty expressed or implied.
//
//  For additional information, please go to the Web site www.ntcip.org.
//
/*******************************************************************************
 *
 * Copyright (c) 1997 Viggen Corporation
 *
 * Permission to use, copy, and distribute this software for any purpose
 * without fee is hereby granted, provided that this copyright notice
 * appears in all copies and this permission notice appears in the
 * supporting documentation.
 *
 * Permission to modify this software is granted provided that the above
 * copyright and this permission notice appears in the modified software.
 *
 ******************************************************************************/


 /*********************************************
 *
 * executor.cpp
 *
 * 9/29/97		Glenn Pruitt
 *********************************************/
#include <string>
using namespace std;

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include "api.h"
#include "symboltab.h"
#include "stattab.h"
#include "statstak.h"
#include "lexical.h"
#include "blokstak.h"
#include "funchand.h"
#include "executor.h"

#define TOKEN_SIZE 256

//Local function prototypes
void handle_declaration(int stat_ndx);
void handle_assignment(int stat_ndx);
void handle_functioncall(int stat_ndx);
void handle_label(int stat_ndx);
void handle_if(int stat_ndx);
void handle_while(int stat_ndx);
void handle_goto(int stat_ndx);

int  evaluate_expression(FILE *fp, long *ires, float *fres,
		char **sres);
struct PEND_OP *evaluate_eprime(FILE *fp);
int  evaluate_term(FILE *fp, long *ires, float *fres,
		char **sres);
struct PEND_OP *evaluate_tprime(FILE *fp);
int  evaluate_fneg(FILE *fp, long *ires, float *fres,
		char **sres);
int  evaluate_factor(FILE *fp, long *ires, float *fres,
		char **sres);

int  evaluate_logic_expression(FILE *fp);
struct PEND_OP *evaluate_bool1(FILE *fp);
int  evaluate_bool2(FILE *fp);
struct PEND_OP *evaluate_bool3(FILE *fp);
int  evaluate_bool4(FILE *fp);
int  evaluate_bool5(FILE *fp);
int  evaluate_bool6(FILE *fp);

int  function_wrapper(FILE *fp, long *ival, float *fval, char **sval);


//Global variables
struct SYTAB **sym_tab;
struct STTAB **stat_tab;
struct STSTK *stat_stak;
char *fname;
char *object_list_ptr;
time_t t;

//Runtime exerciser structures
struct nodeInfo *nodePtr;
struct mystruct *sendStruct;
struct mystruct *recvStruct;
char *last_node_name;
int _stdcall (* LineActivityPtr) (char * theMessage);
int _stdcall (* EditByteStreamPtr) (pmppStruct *msg, char *header, char *trailer);


void executor(char *filename, struct SYTAB **syt,
					struct STTAB **stt, int standalone,
               int _stdcall (* LineActivityInsert) (char * theMessage),
               int _stdcall (* EditByteStream) (pmppStruct *msg, char *header, char *trailer))
{
	int i;
	LineActivityPtr = LineActivityInsert;
   EditByteStreamPtr = EditByteStream;

	sym_tab = syt;
   stat_tab = stt;
   fname = filename;
	stat_stak = NULL;
	srand((unsigned) (time(&t) % 100));

   nodePtr = (struct nodeInfo *)safe_alloc(1,sizeof(struct nodeInfo));
   nodePtr->syntax=NULL;
   nodePtr->access=NULL;
   nodePtr->status=NULL;
   nodePtr->description=NULL;
   nodePtr->OID=NULL;
   nodePtr->value=NULL;
   nodePtr->name=NULL;

   sendStruct = (struct mystruct *)safe_alloc(1,sizeof(struct mystruct));
	recvStruct = (struct mystruct *)safe_alloc(1,sizeof(struct mystruct));
   for(i=0;i<26;i++)
   {
   	sendStruct->arraylist[i] = (char *)safe_alloc(64,sizeof(char));
      sendStruct->valuelist[i] = (char *)safe_alloc(1024,sizeof(char));
   	recvStruct->arraylist[i] = (char *)safe_alloc(64,sizeof(char));
      recvStruct->valuelist[i] = (char *)safe_alloc(1024,sizeof(char));
   }
   sendStruct->pdu = (char *)safe_alloc(5,sizeof(char));
   strcpy(sendStruct->pdu,"SNMP");
   sendStruct->classtype = (char *)safe_alloc(8,sizeof(char));
   strcpy(sendStruct->classtype,"Class B");
   sendStruct->community = (char *)safe_alloc(7,sizeof(char));
   strcpy(sendStruct->community,"PUBLIC");
   sendStruct->AddressDrop = 1;
   sendStruct->GroupAddress = 0;
   sendStruct->EncodingDynamicObject = 0;
   sendStruct->EncodingValue = 0;
   sendStruct->FromNEMA = 1;
   last_node_name = (char *)safe_alloc(200,sizeof(char));

	//add infile and outfile to symbol table
	add_identifier_to_st(sym_tab,  "infile", SY_STRING_TYPE, 5);
   add_identifier_to_st(sym_tab, "outfile", SY_STRING_TYPE, 5);

   //Initialize exerciser if needed
   if(standalone)
   {
   }

   //place first statement onto the stack;
   if( count_statements(*stat_tab) > 0)
   {
		push(&stat_stak, 0, 0);
      while( ! stack_empty(stat_stak))
      	eval_stak();
   }

	//cleanup and exit
   safe_free(sendStruct->pdu);
   safe_free(sendStruct->classtype);
   safe_free(sendStruct->community);
   for(i=0;i<26;i++)
   {
   	safe_free(sendStruct->arraylist[i]);
      safe_free(sendStruct->valuelist[i]);
   	safe_free(recvStruct->arraylist[i]);
      safe_free(recvStruct->valuelist[i]);
   }
   safe_free(recvStruct);
   safe_free(sendStruct);
   safe_free(nodePtr);
   safe_free(last_node_name);
   delete_symbol_table(syt);
   delete_statement_table(stt);
   return;
}

void eval_stak()
{
	int stat_ndx;
   int stat_typ;

   stat_ndx = get_top_index(stat_stak);
   //dump_structs("source.txt",stat_ndx);
   stat_typ = get_statement_type(*stat_tab, stat_ndx);

	//switch statement based on statement type
   switch(stat_typ)
   {
     	case DECLARATION_TYPE  :
        	handle_declaration(stat_ndx);
      break;
      case ASSIGNMENT_TYPE   :
        	handle_assignment(stat_ndx);
      break;
      case FUNCTIONCALL_TYPE :
        	handle_functioncall(stat_ndx);
      break;
      case LABEL_TYPE        :
        	handle_label(stat_ndx);
      break;
      case IF_TYPE           :
        	handle_if(stat_ndx);
      break;
      case WHILE_TYPE        :
        	handle_while(stat_ndx);
      break;
      case GOTO_TYPE         :
        	handle_goto(stat_ndx);
      break;
   }
	return;
}

void handle_declaration(int stat_ndx)
{
	long fpos;
   FILE *fp;
   char *token;
   char *identifier;
   int symbol_type;
   int dimension;
   int symbol_index;
   int next_index;

	//get the starting file position,
   fpos = get_start_position(*stat_tab, stat_ndx);
   //open file and set pointer
   fp = fopen(fname,"rt");
   fseek(fp,fpos,SEEK_SET);
   //read keyword determine symbol type
	token = (char *)safe_alloc(TOKEN_SIZE,sizeof(char));
   token = get_next_token(fp,token);
   if(strcmp(token,"int")==0)
   {
   	symbol_type = SY_INT_TYPE;
   }
   if(strcmp(token,"float")==0)
   {
   	symbol_type = SY_FLOAT_TYPE;
   }
   if(strcmp(token,"string")==0)
   {
   	symbol_type = SY_STRING_TYPE;
   }
   //read identifier
   memset(token,'\0',strlen(token));
   token = get_next_token(fp,token);
   identifier = (char *)safe_alloc(strlen(token)+1,sizeof(char));
   strcpy(identifier,token);
   //if string, read dimension
   if(symbol_type == SY_STRING_TYPE)
   {
   	//read bracket
      memset(token,'\0',strlen(token));
      token = get_next_token(fp,token);
      //read dimension
      memset(token,'\0',strlen(token));
      token = get_next_token(fp,token);
      dimension = atoi(token);
   }
   //close file
   fclose(fp);
   //add entry to symbol table
   symbol_index = add_identifier_to_st(sym_tab, identifier,
   											   symbol_type, dimension);
   //initialize values
	set_value_in_st(*sym_tab, symbol_index, 0, 0.0, "", 0);
   //get next statement index
	next_index = get_next_index(*stat_tab, stat_ndx);
   //pop stack
   pop(&stat_stak);
   //push next statement if necessary
   if(next_index != -1)
   	push(&stat_stak, next_index, NOT_YET_EVALUATED);

   safe_free(identifier);
   safe_free(token);
	return;
}

void handle_assignment(int stat_ndx)
{
	long fpos;
   FILE *fp;
   char *token;
   char *identifier;
   int	id_index;
   int	id_type;
   int next_index;
   int val_type;
   long ival;
   float fval;
   char *sval;

	//get the starting file position
   fpos = get_start_position(*stat_tab, stat_ndx);
   //open file and set pointer
   fp = fopen(fname,"rt");
   fseek(fp,fpos,SEEK_SET);
   //read identifier
   token = (char *)safe_alloc(TOKEN_SIZE,sizeof(char));
   token = get_next_token(fp,token);
   identifier = (char *)safe_alloc(strlen(token)+1,sizeof(char));
   strcpy(identifier,token);
   //consume assignment operator
   memset(token,'\0',strlen(token));
   token = get_next_token(fp,token);
   //evaluate expression
	val_type = evaluate_expression(fp, &ival, &fval, &sval);
   //close file
   fclose(fp);
   //check type matching
	id_index = symbol_table_lookup(*sym_tab,identifier);
   id_type = get_symbol_type(*sym_tab,id_index);

   //update identifier value
   if(id_type == SY_STRING_TYPE && val_type != SY_STRING_TYPE)
   	info_message("Cannot assign numeric to string.\n");

   if(id_type != SY_STRING_TYPE && val_type == SY_STRING_TYPE)
   {
   	info_message("Cannot assign string to numeric.\n");
      safe_free(sval);
   }

   if(id_type == SY_STRING_TYPE && val_type == SY_STRING_TYPE)
   {
   	set_value_in_st(*sym_tab,id_index,0,0.0,sval,0);
      safe_free(sval);
   }

   if(id_type == SY_FLOAT_TYPE && val_type == SY_FLOAT_TYPE)
   	set_value_in_st(*sym_tab,id_index,0,fval,"",0);

   if(id_type == SY_FLOAT_TYPE && val_type == SY_INT_TYPE)
   	set_value_in_st(*sym_tab,id_index,0,(float)(ival),"",0);

   if(id_type == SY_INT_TYPE && val_type == SY_INT_TYPE)
   	set_value_in_st(*sym_tab,id_index,ival,0.0,"",0);

   if(id_type == SY_INT_TYPE && val_type == SY_FLOAT_TYPE)
   	set_value_in_st(*sym_tab,id_index,(long)(fval),0.0,"",0);

   //get next statement index
   next_index = get_next_index(*stat_tab, stat_ndx);
   //pop stack
	pop(&stat_stak);
   //push next statement if necessary
   if(next_index != -1)
   	push(&stat_stak, next_index, NOT_YET_EVALUATED);
	safe_free(identifier);
   safe_free(token);
	return;
}

void handle_functioncall(int stat_ndx)
{
	long fpos;
   FILE *fp;
   int next_index;
   long ival;
   float fval;
   char *sval;
   HANDLE inputFile;
   int err;

	    //get the starting file position
    fpos = get_start_position(*stat_tab, stat_ndx);
        //open file and set pointer
    fp = fopen(fname,"rt");
    if (fp!=NULL)
    {
        fseek(fp,fpos,SEEK_SET);
           //call function wrapper, pass in file pointer
	    function_wrapper(fp,&ival,&fval,&sval);
           //close file
        err = fclose(fp);
        while (err != 0)
            err = fclose(fp);
               /* inputFile = CreateFile(fname, GENERIC_WRITE | GENERIC_READ, 0, NULL,
                    OPEN_EXISTING, NULL, NULL);
                if (inputFile)
                {
                    SetFilePointer(inputFile, fpos, NULL, FILE_BEGIN);
                    function_wrapper(inputFile,&ival,&fval,&sval);
                    CloseHandle(inputFile);
                }
                else
                {
                    info_message("Error opening infile");
                }*/
            //get next statement index
        next_index = get_next_index(*stat_tab, stat_ndx);
            //pop stack
        pop(&stat_stak);
            //push next statement if necessary
        if(next_index != -1)
   	        push(&stat_stak, next_index, NOT_YET_EVALUATED);
        return;
    }
    return;
}

void handle_label(int stat_ndx)
{
	int next_index;

	//get next statement index
   next_index = get_next_index(*stat_tab, stat_ndx);
   //pop stack
   pop(&stat_stak);
   //push next statement if necessary
   if(next_index != -1)
   	push(&stat_stak, next_index, NOT_YET_EVALUATED);
	return;
}

void handle_if(int stat_ndx)
{
	long fpos;
   FILE *fp;
   char *token;
	int  bool_val;
   int  next_index;
   int  jump_index;

	//get the starting file position
   fpos = get_start_position(*stat_tab, stat_ndx);
   //open file and set pointer
   fp = fopen(fname,"rt");
   fseek(fp,fpos,SEEK_SET);

   if(! get_top_evaluated(stat_stak))
   {
   	token = (char *)safe_alloc(TOKEN_SIZE,sizeof(char));
	   //consume "if"
   	token = get_next_token(fp,token);
	   //consume "("
      memset(token,'\0',strlen(token));
   	token = get_next_token(fp,token);
		safe_free(token);

	   //	evaluate logical expression
   	bool_val = evaluate_logic_expression(fp);

	   //close file
		fclose(fp);
 	}
   else
   {
   	//already evaluated so automatically false
   	bool_val = 0;
      fclose(fp);
   }
   //if expression is true
	if(bool_val)
   {
   	//		get jump statement index
		jump_index = get_jump_index(*stat_tab, stat_ndx);
   	//		set top of stack to ALREADY_EVALUATED
      set_top_evaluated(stat_stak, ALREADY_EVALUATED);
	   //		push jump statement if it exists
      if(jump_index != -1)
	     	push(&stat_stak, jump_index, NOT_YET_EVALUATED);
   }
   else
   {
	   //if expression is false
   	//		get next statement index
      next_index = get_next_index(*stat_tab, stat_ndx);
	   //		pop stack
      pop(&stat_stak);
   	//		push next statement if it exists
   	if(next_index != -1)
      	push(&stat_stak, next_index, NOT_YET_EVALUATED);
   }
	return;
}

void handle_while(int stat_ndx)
{
	long fpos;
   FILE *fp;
   char *token;
	int  bool_val;
   int  next_index;
   int  jump_index;

   token = (char *)safe_alloc(TOKEN_SIZE,sizeof(char));

	//get the starting file position
   fpos = get_start_position(*stat_tab, stat_ndx);
   //open file and set pointer
   fp = fopen(fname,"rt");
   fseek(fp,fpos,SEEK_SET);

   //consume "while"
   token = get_next_token(fp,token);
   //consume "("
   memset(token,'\0',strlen(token));
   token = get_next_token(fp,token);
   safe_free(token);

   //		evaluate logical expression
   bool_val = evaluate_logic_expression(fp);

   //close file
	fclose(fp);

   //if expression is true
	if(bool_val)
   {
   	//		get jump statement index
		jump_index = get_jump_index(*stat_tab, stat_ndx);
	   //		push jump statement if it exists
      if(jump_index != -1)
	     	push(&stat_stak, jump_index, NOT_YET_EVALUATED);
   }
   else
   {
	   //if expression is false
   	//		get next statement index
      next_index = get_next_index(*stat_tab, stat_ndx);
	   //		pop stack
      pop(&stat_stak);
   	//		push next statement if it exists

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -