/* 
*	********************************************
*	debug.c                                    *
*	Author:	Li Long                            *
*	Verified by Liu Zhiguo 2003-04-21          *
*	Verified by Wang Jian 2005-01-17           *
*	Verified by liu  Wei  2005-05-15           *
*	2005-05-15                                 *
*	Function:                                  *
*	1.      Monitor other modules status;      *
*	2.      Use serial port;                   *
*	3.      Use telnet;                        *
*	4.      Use SNMP Agent set MIB tree        *
* 5.      SNMP sernior agent                 *
* 6.      Custmor setting menu               *
*	********************************************
*/ 


/*
printf("\033[1;33m Hello World. \033[0m \n");

颜色如下:
none         = "\033[0m"
black        = "\033[0;30m"
dark_gray    = "\033[1;30m"
blue         = "\033[0;34m"
light_blue   = "\033[1;34m"
green        = "\033[0;32m"
light_green -= "\033[1;32m"
cyan         = "\033[0;36m"
light_cyan   = "\033[1;36m"
red          = "\033[0;31m"
light_red    = "\033[1;31m"
purple       = "\033[0;35m"
light_purple = "\033[1;35m"
brown        = "\033[0;33m"
yellow       = "\033[1;33m"
light_gray   = "\033[0;37m"
white        = "\033[1;37m"

 
字背景颜色范围: 40--49                   字颜色: 30--39 
            40: 黑                          30: 黑
        41: 红                          31: 红
        42: 绿                          32: 绿
        43: 黄                          33: 黄
        44: 蓝                          34: 蓝
        45: 紫                          35: 紫
        46: 深绿                        36: 深绿
        47: 白色                        37: 白色 
ANSI控制码: 
QUOTE:
   \033[0m   关闭所有属性   
  \033[1m   设置高亮度   
  \03[4m   下划线   
  \033[5m   闪烁   
  \033[7m   反显   
  \033[8m   消隐   
  \033[30m   --   \033[37m   设置前景色   
  \033[40m   --   \033[47m   设置背景色   
  \033[nA   光标上移n行   
  \03[nB   光标下移n行   
  \033[nC   光标右移n行   
  \033[nD   光标左移n行   
  \033[y;xH设置光标位置   
  \033[2J   清屏   
  \033[K   清除从光标到行尾的内容   
  \033[s   保存光标位置   
  \033[u   恢复光标位置   
  \033[?25l   隐藏光标   
  \33[?25h   显示光标



\033=\x1b
*/

#include "./include/debug.h"  
#include "../../public/src/include/pub_log.h"
#include "../../public/src/include/pub_debug.h"

// fix multiple definition
struct	debug_shm_struct	*debug_shm_ptr; 
mib_node   *mib_root_node;
mib_node			   *mib_curr_node;
menu_node   		   *menu_root_node; 
menu_node   		   *menu_curr_node;
mib_node		     	mib_nodes[MIB_MAX_NODES];
menu_node               menu_nodes[MENU_MAX_NODES];
mib_child_node		    mib_child_nodes[MIB_MAX_CHILD_NODES];
debug_snmp_agent        debug_snmp_agent_buf[MAX_PACKET_NUM];


#define	DEBUG_VERSION	  "V1.0.01"  
#define	DEBUG_PATCH		  1
/*@ignore@*/
static char ascii[16] =  
    {
        0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,
        0x38,0x39,0x41,0x42,0x43,0x44,0x45,0x46
    };

static char        title_top[]	= {0xc,0x1b,'[','H',0x1b,'[','2','J','\0'};

static char        prompt_line25[]	= {0x1b,'[','2','5','H',0x1b,'[','0','J','>','\0'};

static char        max_access[7][32]={"invalid", "not-accessible", "accessible-for-notify", "read-only","read-write","read-create "," write-only"};

static char        title1[TITLE_CHAR*DEBUG_MAX_PAGE] = "\0";		// show the menu page from conf file
char		       title1_show[TITLE_CHAR*DEBUG_MAX_PAGE] ="\0";	// show the menu page

unsigned int	   com_port;
char			   recser_buf[SERIAL_BUF_LEN];
int				   recserbuf_h;
int				   recserbuf_t;
char			   sendser_buf[SERIAL_BUF_LEN];
int				   sendserbuf_h;
int				   sendserbuf_t;

INSTANCE_ARR       curr_instance_arr[256];
char               instance_eable_name[80];
int                temp_instance;
BYTE               curr_module;
BYTE               host_group=0;
debug_snmp_agent   snmp_agent_packet;
debug_snmp_agent   snmp_agent_packet1;
DWORD              debug_mem_size;
DWORD              debug_alloc_mem;

BYTE               remark_meet_lf=0;

debug_shm_struct   debug_shared_memory;

DWORD              heap_remark_index; 
void               *heap_remark_curr;
void               *heap_remark;
char	           heap_work_buf[300*1024];

char               curr_instance_value[256];
char               set_ins_value[256];
BYTE               none_instance=0;
BYTE               set_str_len=0; 
BYTE               set_all=0;
char	             set_val_str[256] ;
BYTE	             SendTelnetFlag=0;
BYTE			         disp_main_flag;			// display the main page
BYTE	             disp_sub_module_flag;	// current module
BYTE	             disp_page_flag;			// current page of each module
BYTE			         disp_main_page_num;			//main page number
BYTE	             command_flag;			// whether in command mode
BYTE               first_page;             
BYTE               bind_num;
BYTE	           comp_disp[500];
BYTE	           m_state;
BYTE	           m_state_sub_module;
WORD	           title_top_pt;
WORD 	           title_pt;
WORD	           title_sub_module_pt;
WORD	           ascii_in_pt;
WORD	           space_counter;
WORD	           row_ptr,col_ptr;
int                list_flag,list_start,list_end;   
int                show_all_value;
int                show_type;
int                show_expend;
int                show_range;
int                get_show_state;
int                save_flag;
int                show_empty=0;
int                del_flag;
int                expend_view=0;
int                list_field=0;
int                show_snmp_state;
int                show_list;
int                list_once;
BYTE               set_after_get=0;
BYTE               show_field=0;
DWORD              list_field_arr[64];
BYTE               Module_Count_Index=0;
BYTE               Init_Page=0;
BYTE               send_char_flag=0;

BYTE               debug_agent_index;
BYTE               snmp_agent_flag=0;  // 0 idel 1 get 2 set

static BYTE 	           *disp_ptr;
static WORD 	           disp_length;/* displaying length of each row */
WORD 	           disp_index;	/* row number */
WORD 	           char_ptr;	/* how many hex code in a row, < disp_length */
WORD 	           hex_state;	/* the state in function disp_hex */
BYTE 	           cursor_p[8]; /* control characters as 'ESC[**;**H' */

WORD 	           ascii_out_ph,ascii_out_pt;
static	BYTE 	   ascii_out_array[MAX_ASCII_LEN];
static	BYTE	   ascii_in_array[MAX_ASCII_LEN];

static	BYTE	   debug_ascii_in_array[MAX_ASCII_LEN];
static	BYTE	   debug_ascii_out_array[MAX_ASCII_LEN];
int              debug_ascii_out_ph=0;
int              debug_ascii_out_pt=0;



BYTE               list_msg[128];

static	BYTE	   test_array[20];
WORD 	           t10s_flag;
WORD 	           ascii_out_len;
DWORD 	           debug_counter;
BYTE 	           debug_heartbeat[8];
char 	           time_buf[64] = "\0";
char 	           hostip_buf[64] = "\0";
char 	           current_timer[64]="\0";


BYTE             bind_index;
BYTE 	           get_state;
BYTE 	           set_state;
DWORD 	         set_val;


DWORD  	           Local_IP;
DWORD 	           Peer_IP[16];
char   	           Remote_Host_Name[16][64];
char 	           Remote_Host_IP[16][16];
BYTE 	           IP_Selected[16];
BYTE 	           ip_num;
BYTE               ip_index;
snmp_pdu  	       send_msgbuf,recv_msgbuf;
snmp_addr  	       send_addr, recv_addr;
WORD 	           t5s_flag;
WORD 	           t100ms_flag;
mib_node_param 	   *param_node;
char               paraname[128];
mib_node           *temp_mib_node;
WORD               mib_line_count;
mib_node           temp_leaf_node;


DWORD 		       root_oid[32] = {1,3,6,1,4,1};		//SNMPv2
DWORD              cur_oid[32] = {1,3,6,1,4,1,1373,1,3};
BYTE			   root_oid_len = 6;
WORD               treehost;
BYTE         debug_remark_flag;
WORD         debug_remark_ptr;


struct _wxc2_debug_info wxc2_debuginfo;

//mib_node		  *mib_root_node;
//mib_node		  *mib_curr_node;
WORD			   mib_node_index=0;
WORD         menu_node_index=0;
WORD			   mib_child_node_index;
int				   mib_type_num;

BYTE                leaf_compute_field_num=0;
WORD			   mib_param_select_index;
WORD			   mib_param_input_index;
WORD			   mib_node_param_index;

BYTE			   debug_cfg;     //0: command status, 1: configure status
BYTE			   debug_log_all;	// 0: log none , 1: log all
BYTE			   debug_log_error_on;	//0: log error off, 1: log erro on
BYTE			   debug_module_num;	//if in command status, debug_module_num shows which module's ascii in buf the command send to
BYTE			   debug_cfg_end;	// 0: configure command not over, 1: configure command over
int			       debug_get_all_value;	// 0: not get all value,  or means how much children value have to get

WORD                        gobal_module_index;
BYTE                        node_save_flag;
module_command_save_flag    node_save_module;

mib_subsys_module          subsys_module[MIB_SUBSYS_MODULE_MAX_NUM];
mib_host                   module_host[MIB_HOST_MAX_NUM];
module_command_save_flag   module_save_flag[MIB_SUBSYS_MODULE_MAX_NUM];


mib_param_select          mib_param_select_array[MIB_MAX_PARAM_SELECTS];
mib_param_input	          mib_param_input_array[MIB_MAX_PARAM_INPUTS];
mib_node_param	          mib_node_param_array[MIB_MAX_NODE_PARAMS];

mibfile_imports_data 	  imports_data;
mibfile_module_data 	  module_data;
mibfile_object_data		  object_data;
mibfile_type_data		  type_data;

BYTE               set_ok_once=0;   


char *field_input_type[7]={"toDec high 0","KeepHex high 0","KeepHex low 0",
	                    "KeepHex Low E","KeepHex Low F","ToAscii Low 0","KeepAscii Low F"}; 


//add for compute field
BYTE                compute_field_num;  //for all compute
compute_field       compute_field_arr[16];  //for all compute
compute_list        compute_field_list[6];      //unit list
BYTE                compute_list_num;   //unit list//curr node compute_list_num=0
char                OID_name_str[5][65];        //unit read
DWORD               OID_val[5][32];     //unit read
BYTE                OID_index=0;  //unit read
BYTE                line_index; //unit read
BYTE                print_line_index=0;;
BYTE                compute_field_list_index;
compute_field       compute_curr_unit;  //unit read
BYTE                read_syntax_nor_list=0;       //unit read
BYTE                compute_state=0;
WORD                compute_curr_instance;
mib_node            *temp_compute_node;
char                temp_value[256][32];
BYTE                print_once=0; 
//0    idle
//1    print once(list all once)
//2    print again 
BYTE                field_compute_state=0;
//field_compute_state
//0    idle
//1    find compute field
//2    translate [v] [l]
//3    list curr node self
//4    list "OID"
//5    list over
//6    translate [c] [p]
//7    translate over
//8    print over

//end of define for compute field



struct timeval tv_start_rt, tv_end_rt;
mib_node       *agent_tmp_node;
menu_node      *tmp_menu_node;


BYTE   ShowRegister=0;
BYTE   debug_busy_alarm=0;


static char switch_stat[][32] = {"off", "on"};

///////////////////////////////////declaration////////////////////////
void debug_init_serial();

void debug_init_shm();

void debug_init_var();

void debug_ascii_in_cmd_handle();




void debug_read_main_title();

void debug_change_title1();

void debug_module_page(int count);

void debug_disp_hex(); 

void debug_set_para();




int OBJECT_found(char *name, FILE *fp, mib_node *father_node_ptr, DWORD *last_node_oid);


int BEGIN_found(FILE *fp);

int TYPE_found(char *name, FILE *fp, mib_node *father_node_ptr, DWORD 
*last_node_oid);

mib_node *IMPORTS_found(FILE *fp);

int MODULE_found(char *name, FILE *fp, mib_node *father_node_ptr, DWORD 
*last_node_oid);

int Analyze_Leaf_Description(FILE * fp, mib_node * leaf_node);

//int analyze_setTemplate(FILE *fp, mib_node *father_node_ptr);

int MIB_create_tree(char *conf_dir,	char *conf_file);

//int MIB_create_leaves(char *conf_dir,	char *sysConf_file,		char *param_list_file);

mib_child_node	*create_mib_child_node(char *name, DWORD oid);

mib_node	*create_mib_node(char *name, BYTE oid_len, DWORD *oid, WORD total_instance);


mib_node *create_node(char *line, char *nod_name, int instance, mib_node *father_ptr, DWORD *last_node_oid);

int register_node(mib_node *mib_node_ptr, DWORD *oid, BYTE oid_len);

int register_child_node(mib_node *mib_node_ptr, mib_child_node *mib_child_node_ptr);




void debug_mmi_remark();


void debug_mmi_log_status();

int debug_mmi_log(char *log_str);

void debug_mmi_option(char *str);

int 	debug_mmi_select_host(char *host_str);



void show_node(mib_node *node_ptr,char *separation);

int find_OID(int obj_num, char *sysConf_file_name, DWORD *OID);


void debug_mmi_view();


void pick_name(char *str_ptr, char *word_ptr,char *temp_ch);


void debug_send_serial();

int debug_send_char(BYTE ch);

void debug_read_serial();

int debug_read_char();

void debug_recv_cmd();



int debug_mmi_cd(char 	*path);

int debug_mmi_contents(char *str);

int debug_mmi_parent();

int debug_mmi_child(char *name);

int debug_mmi_setins(char *ins);

void debug_mmi_save();

void register_host();

int debug_mmi_pwd();

int debug_mmi_ip(char 	*ips);

void debug_mmi_list();

void register_instance();

void debug_mmi_create(char *str);

void debug_mmi_del(char *str);

void debug_mmi_option(char *str);

void debug_mmi_remark();

void debug_mmi_tree(mib_node *node_ptr,char *separation, BYTE node_levels);

void debug_mmi_find_host();

int debug_mmi_show_host();

void debug_mmi_cmm_help();

void debug_mmi_cfg_help();

int debug_mmi_get(char	*child_name);

int debug_mmi_set(char	*set_str);

int debug_mmi_get_all();

int debug_snmp_get(BYTE oidlen,DWORD *oid,BYTE *data_ptr,BYTE *vartype);

int debug_snmp_set(BYTE oidlen,DWORD *oid,BYTE *data_ptr,WORD data_len);

void 	debug_snmp_get_rt();

void 	debug_snmp_set_rt();


int default_value(DWORD *oid,BYTE oid_len,char *value);//,char *default_Value);

int debug_mmi_select_host(char *host_str);





void debug_set_currenttime();

void del_space(char *str,BYTE type);

int read_line(FILE *fp, char *line_ptr);

int read_begin(FILE *fp, char *stop_ch);

int read_until(FILE *fp, char *data_ptr, char *stop_ch);

int read_part(FILE *fp, char *data_ptr,char *start_ch, char *stop_ch);

char *pick_first_word(char *str_ptr, char *word_ptr);

int take_str(char *data_str, FILE *fp, char *data_ptr);

void debug_color_out(int attr,int color,int bg,char *str);

void help_cmm(int index,char *str1,char *str2);

void formatstr(int len,char *str,BYTE type);

void switchchar(char *out_text,BYTE num);

void debug_ascii_out_proc(const char *str_ptr);

DWORD  my_pow(int x, int y);	// y \u4E2A x \u76F8\u4E58

BYTE test_str(char *str,BYTE type);

void  AsciiToRbcd (BYTE *bcd_buf, const char *ascii_buf, int len);

void strpcat(char *dest_str,DWORD *dest_p,const char *src_str);

//void  BcdToAscii (char *ascii_buf, const BYTE *bcd_buf, int len);


int create_menu_tree(char *conf_dir,char *menu_page,mib_node  *sys_node);

void debug_mmi_view();

void debug_mmi_mcd(char *str);  

void main_menu(char *str);

int debug_send_string(const char *str_send);




BYTE create_compute_field(FILE * fp, mib_node * node_ptr, BYTE field_id,char *name);

void translate_compute_field();

BYTE take_compute_value(char *str,char *temp_value);

void compute_field_WatchDog();

void print_compute_field();


//////////////////////////////////end of declaration//////////////////




void debug_netcap_start()
{
	char str[] = "net cap start";
	int i;
	
	NcStart(0,0);
	for(i=0; i < MODULE_COUNT; i++) 
    {
        if( (i !='n'-'a') && (debug_shm_ptr->module_d[i].ascii_in_set_flag == 1))
        {
            debug_shm_ptr->module_d[i].ascii_in[0] = 1;
            strcpy((char *)debug_shm_ptr->module_d[i].ascii_in+1,str);
        }
    }
}

void debug_netcap_stop()
{
	char str[] = "net cap stop";
	int i;
	
	NcStop();
	for(i=0; i < MODULE_COUNT; i++) 
    {
        if( (i !='n'-'a') && (debug_shm_ptr->module_d[i].ascii_in_set_flag == 1))
        {
            debug_shm_ptr->module_d[i].ascii_in[0] = 1;
            strcpy((char *)debug_shm_ptr->module_d[i].ascii_in+1,str);
        }
    }
}


////////////////////////////////////Intial////////////////////////////
/*
*
*       Fun:    debug_read_conf
*
*       Desc:   debug read conf
*
*       Ret:    void
*
*       File:   debug.c
*
*/ 
void debug_read_conf ()
{
    char file_name[32];
    char *p, *q, line[512];
    BYTE conf_flag = 0;
    FILE *fp_conf;
    DIR *pdir;

    strcpy ( file_name, "./conf/debug.conf" );

    wxc2_debuginfo.mmi_switch = 0;      //off 

    if( ( pdir = opendir ( MIB_FILES_DIR ) ) == NULL && errno == ENOENT )
    {
        return;
    }
    else
    {

        closedir ( pdir );
        if( ( fp_conf = fopen ( file_name, "rw" ) ) == NULL )
        {
            printf ( "[debug] Error: cannot open debug conf_file: [%s].\r\n", file_name );
            return;
        }
    }

    while ( !feof ( fp_conf ) )
    {
        if( fgets ( line, 256, fp_conf ) == NULL )
        {
            break;
        }
        line[128] = '\0';
        p = line;
        while ( *p == ' ' )
            p++;
        if( ( q = strstr ( p, "//" ) ) != NULL )
            *q = '\0';
        if( strstr ( p, "System Type" ) )
        {
            p = p + 11;
            if( ( p = strchr ( p, '=' ) ) && ( q = strchr ( p, ';' ) ) )
            {
                p++;
                *q = '\0';

                if( strstr ( p, "CDMA" ) )
                {
                    wxc2_debuginfo.sys_type = 1;
                    conf_flag |= 0x01;
                }
                else if( strstr ( p, "GSM" ) )
                {
                    wxc2_debuginfo.sys_type = 2;
                    conf_flag |= 0x01;
                }
                else if( strstr ( p, "SSW" ) )
                {
                    wxc2_debuginfo.sys_type = 3;
                    conf_flag |= 0x01;
                }
            }

        }
        else if( strstr ( p, "MMI Switch" ) )
        {
            p = p + 10;
            if( ( p = strchr ( p, '=' ) ) && ( q = strchr ( p, ';' ) ) )
            {
                p++;
                *q = '\0';

                if( strstr ( p, "ON" ) || strstr ( p, "On" ) || strstr ( p, "on" ) )
                {
                    wxc2_debuginfo.mmi_switch = 1;
                    conf_flag |= 0x02;
                }
                else if( strstr ( p, "off" ) || strstr ( p, "Off" ) || strstr ( p, "OFF" ) )
                {
                    wxc2_debuginfo.mmi_switch = 0;
                    conf_flag |= 0x02;
                }
            }

        }

    }
    fclose ( fp_conf );
    if( conf_flag != 0x03 )
    {
        printf ( "[debug] Error: debug conf_file ([%s])is incorrect .\r\n", file_name );
        return;
    }
    return; 

}

/*
*
*       Fun:    debug_init
*
*       Desc:   debug intial function
*
*       Ret:    void
*
*       File:   debug.c
*
*/ 
extern void WxcDump(int nSigno);

void debug_init()
{

    
    int     i,j;
    char    temp_str[64];
    char    file_name[64];
    FILE      *fp_conf;
    mib_node  *sys_base_node;
    
//    signal(SIGSEGV, &WxcDump);
    NcInit();
    
		//heap_remark=calloc(sizeof(char),1024*300);
		heap_remark=heap_work_buf;
		heap_remark_index=0;
		heap_remark_curr=heap_remark;
    
		for(i=0;i<MIB_SUBSYS_MODULE_MAX_NUM;i++)
		{
		        subsys_module[i].index=0xFF;
		}
		for(i=0;i<MIB_HOST_MAX_NUM;i++)
		{
		        module_host[i].index=0xFF;
		}
    
    debug_read_conf(); 
    temp_str[0]='\0';
    if( wxc2_debuginfo.sys_type == 1 )
    	   strcpy( temp_str , "CDMA");         
    else if( wxc2_debuginfo.sys_type == 2 )
    	   strcpy( temp_str , "GSM");
    else if( wxc2_debuginfo.sys_type == 3 )
    	   strcpy( temp_str , "SSW");
   
    sprintf( wxc2_debuginfo.wxc2_ver , "%s Version Unset" ,temp_str );
		printf("\r\nDebug Inital start ...\r\n System type : %s MMI Switch : %s \r\n",temp_str, switch_stat[wxc2_debuginfo.mmi_switch]);
		if( wxc2_debuginfo.mmi_switch )
		{
				if(MIB_create_tree(MIB_FILES_DIR,	MIB_LIST_FILE) != 1)	//wangjian add
				{
				   printf("Fail    : create MIB tree fail!\r\n");
				   exit(1);
				}
    }
    iopl(3); 
    //debug_init_serial();
    debug_init_shm();
    
    //if(init_flag)
    {
    	debug_init_var();
    	memset(debug_shm_ptr,0,sizeof(debug_shm_struct));
    	debug_read_main_title();
    	debug_set_para();
    }
    
    

    sys_base_node=search_mib_node(mib_root_node,cur_oid,9);
    memset(&debug_snmp_agent_buf,0,MAX_PACKET_NUM*sizeof(debug_snmp_agent));
    if( wxc2_debuginfo.mmi_switch )
		{
		    if(create_menu_tree(MIB_FILES_DIR,MENU_TREE_FILE,sys_base_node))
		    {
		    	 printf("Create the menu tree                                       [");
		       sprintf(temp_str," OK ");
		       TxtColor(BRIGHT,YELLOW,BLACK,temp_str);
		       printf("%s", temp_str);
		       printf("]\r\n");
		    }
		    else
		    {
		        printf("[debug] :debug menu tree create fial\n");
		        exit(1);
		    }
		    
		    register_host();
    
    
    strcpy(file_name, 	"");

    strcpy(file_name,	MIB_FILES_DIR);
    strcat(file_name,	MIB_WRITE_TREE ); 
     

     
    if((fp_conf = fopen(file_name,"a+")) == NULL)
    {
        printf("[debug] Error: cannot open conf_file: [%s].\r\n",file_name);
        exit(1);
    } 
    
    fprintf(fp_conf,"\r\n\r\n\r\nSubSystem Module\r\n");
    for(i=0;i<MIB_SUBSYS_MODULE_MAX_NUM && subsys_module[i].index!=0xFF;i++)
    {
       fprintf(fp_conf,"Index:%d    ",subsys_module[i].index);
       fprintf(fp_conf,"OID: ");
       for(j=0;j<6;j++)
        		fprintf(fp_conf,"%u.",cur_oid[j]);
       for(j=6;j<14 && subsys_module[i].oid[j-6]!=0 ;j++)
       			fprintf(fp_conf,"%u.",subsys_module[i].oid[j-6]);
       fprintf(fp_conf,"   name:%s",subsys_module[i].name);
       fprintf(fp_conf,"   MaxGroup:%d",subsys_module[i].max_group); 
       fprintf(fp_conf,"\r\n");   
    }
    fprintf(fp_conf,"\r\n\r\n\r\n");
    
    fprintf(fp_conf,"\r\n\r\n\r\nHost IP\r\n");
    for(i=0;i<MIB_HOST_MAX_NUM && module_host[i].index!=0xFF;i++)
    {
       fprintf(fp_conf,"Index:%d    ", module_host[i].index); 
       fprintf(fp_conf,"Group index:%d    ", module_host[i].group_index);
       fprintf(fp_conf,"Membre index:%d  ",module_host[i].member_index);
       fprintf(fp_conf,"Host name:%s  ",module_host[i].host_name); 
       fprintf(fp_conf,"Host ip:");
       for(j=0;j<4;j++)
       {
       	fprintf(fp_conf,"%d.",module_host[i].host_ip[j]);     
       }  
       fprintf(fp_conf,"\r\n");
    }
    fprintf(fp_conf,"\r\n\r\n\r\n");
    
    
    fprintf(fp_conf,"\r\n\r\n\r\nModule Save flag\r\n");
    for(i=0;i<MIB_SUBSYS_MODULE_MAX_NUM && module_save_flag[i].module_index!=0xFF;i++)
    {
       fprintf(fp_conf,"Module Index:%d    ",module_save_flag[i].module_index);
       fprintf(fp_conf,"OID: ");
       for(j=0;j<32 && module_save_flag[i].oid[j]!=0 ;j++) 
       			fprintf(fp_conf,"%u.",module_save_flag[i].oid[j]);
       fprintf(fp_conf,"   oid len:%d",module_save_flag[i].oid_len);		
       fprintf(fp_conf,"   select id:%d",module_save_flag[i].save_select_id);
       fprintf(fp_conf,"   field id:%d",module_save_flag[i].save_field_id);    
       fprintf(fp_conf,"\r\n");
    }
    fprintf(fp_conf,"\r\n\r\n\r\n");
    fclose(fp_conf);   
         
    debug_mem_size=sizeof(mib_node) * mib_node_index+sizeof(mib_child_node) * mib_child_node_index;
    debug_mem_size+=sizeof(mib_param_select) * mib_param_select_index+sizeof(mib_param_input) * mib_param_input_index;
    debug_mem_size+=sizeof(mib_node_param) * mib_node_param_index+MAX_PACKET_NUM*sizeof(debug_snmp_agent);
    debug_mem_size+=sizeof(char)*1024*300+sizeof(debug_shm_struct);    
    debug_mem_size+=menu_node_index*sizeof(menu_node);
    debug_mem_size+=102400;
    
    debug_alloc_mem=sizeof(mib_node)*MIB_MAX_NODES	+sizeof(mib_child_node) * MIB_MAX_CHILD_NODES;
    debug_alloc_mem+=sizeof(mib_param_select) * MIB_MAX_PARAM_SELECTS+sizeof(mib_param_input) * MIB_MAX_PARAM_INPUTS;
    debug_alloc_mem+=sizeof(mib_node_param) * MIB_MAX_NODE_PARAMS	+MAX_PACKET_NUM*sizeof(debug_snmp_agent);
    debug_alloc_mem+=sizeof(char)*1024*300+sizeof(debug_shm_struct);
    debug_alloc_mem+=MENU_MAX_NODES*sizeof(menu_node);
    debug_alloc_mem+=102400;    
    
    printf("\r\n");
    sprintf(temp_str,"Debug memery statices");
    TxtColor(BRIGHT,BLACK,WHITE,temp_str);
    printf("%s  ",temp_str);    
    sprintf(temp_str,"[        In Used       ]");
    TxtColor(BRIGHT,BLACK,WHITE,temp_str);
    printf("%s     ",temp_str);
    sprintf(temp_str,"[        Allocated     ]");
    TxtColor(BRIGHT,BLACK,WHITE,temp_str);
    printf("%s\r\n",temp_str);
    printf("mib_nodes              Num: %-5d size: %-7lu     Num: %-5d size: %-7lu\r\n",mib_node_index, sizeof(mib_node) * mib_node_index,MIB_MAX_NODES,sizeof(mib_node) *MIB_MAX_NODES);
    printf("mib_child_nodes        Num: %-5d size: %-7lu     Num: %-5d size: %-7lu\r\n",mib_child_node_index, sizeof(mib_child_node) * mib_child_node_index,MIB_MAX_CHILD_NODES, sizeof(mib_child_node) *MIB_MAX_CHILD_NODES);
    printf("menu_node              Num: %-5d size: %-7lu     Num: %-5d size: %-7lu\r\n",menu_node_index,menu_node_index*sizeof(menu_node),MENU_MAX_NODES ,MENU_MAX_NODES*sizeof(menu_node));
    printf("mib_param_select       Num: %-5d size: %-7lu     Num: %-5d size: %-7lu\r\n",mib_param_select_index, sizeof(mib_param_select) * mib_param_select_index,MIB_MAX_PARAM_SELECTS,sizeof(mib_param_select) * MIB_MAX_PARAM_SELECTS);
    printf("mib_param_input        Num: %-5d size: %-7lu     Num: %-5d size: %-7lu\r\n",mib_param_input_index, sizeof(mib_param_input) * mib_param_input_index,MIB_MAX_PARAM_INPUTS,sizeof(mib_param_input) *MIB_MAX_PARAM_INPUTS);
    printf("mib_node_param         Num: %-5d size: %-7lu     Num: %-5d size: %-7lu\r\n",mib_node_param_index, sizeof(mib_node_param) * mib_node_param_index,MIB_MAX_NODE_PARAMS	,sizeof(mib_node_param) *MIB_MAX_NODE_PARAMS );
    printf("remark size                      %-7u                         %-7lu\r\n",heap_remark_index,sizeof(char)*1024*300); 
    printf("debug total memery               %-7u                         %-7u\r\n",debug_mem_size,debug_alloc_mem);
    printf("\r\nParent node  :  %d           Leaf node  :  %d\r\n", (mib_node_index-mib_type_num),mib_type_num);
  }
    printf("\r\ndebug module %s T%03d init ......... Complete.\r\n\r\n",DEBUG_VERSION,DEBUG_PATCH);
}

/*
*
*       Fun:    debug_init
*
*       Desc:   debug intial share memery
*
*       Ret:    void
*
*       File:   debug.c
*
*/
void debug_init_shm()
{
    DWORD	oid_prefix[] = {1,3,6,1,4,1,1373,1,3,1,1,1};
    int	shmid;
    void *shared_memory	= (void *) 0;

    shmid = shmget(DEBUG_SHM_KEY+wxc2_get_shm_offset(),
    			sizeof(debug_shm_struct),DEBUG_SHM_PERM|IPC_CREAT);
    if (shmid == -1)
    {
        printf("debug share memory init fail.\n");
        exit(0);
    }
    shared_memory = shmat(shmid,(void *)0,0);
    if (shared_memory == (void *) -1)
    {
        printf("debug share memory fail.\n");
        exit(0);
    }
        debug_shm_ptr = NULL;
//    debug_shm_ptr = (debug_shm_struct *)shared_memory;
    debug_shm_ptr = (debug_shm_struct *)&debug_shared_memory;

    inquire_setmsg(12,oid_prefix,debug_snmp_set);
    inquire_getmsg(12,oid_prefix,debug_snmp_get);
    set_alarm(5,0); // normal
    debug_heartbeat[4] = debug_counter;
    set_status(5,7,debug_heartbeat);
}

void debug_init_wxc2_ver(char *ver_info)
{
	strcpy(wxc2_debuginfo.wxc2_ver,ver_info);
}

/*
*
*       Fun:    debug_init
*
*       Desc:   debug intial serial port
*
*       Ret:    void
*
*       File:   debug.c
*
*/
/*
void debug_init_serial()
{
    WORD	baudrate = B19200;
    char	*ttyname = "/dev/ttyS0";
    struct termio	new_termio;

    if ((com_port = open(ttyname,O_RDWR|O_NONBLOCK|O_NDELAY)) < 0)
    {
        printf("Initiate serial port fail!\n%d",com_port);
        exit(1);
    }
    new_termio.c_iflag = IGNBRK;
    new_termio.c_oflag = 0;
    new_termio.c_cflag = baudrate|CS8|CREAD|HUPCL|CLOCAL;
    new_termio.c_lflag = 0;
    new_termio.c_line = 0;
    new_termio.c_cc[VMIN] = 1;
    new_termio.c_cc[VTIME] = 0;
    ioctl(com_port, TCSETA, &new_termio);
}
*/
/*
*
*       Fun:    debug_init
*
*       Desc:   debug intial var
*
*       Ret:    void
*
*       File:   debug.c
*
*/
void debug_init_var()
{
    int		i,j,k;
    char	hostname[128];
    char	*host_ip;
    struct hostent	*host;
    time_t	tick;
    struct in_addr    hostAddr;

    save_flag=0;
    del_flag=0;
    temp_instance=256;
    debug_agent_index=0;
    strcpy(paraname,"");
    strcpy(set_ins_value,"");
    strcpy(set_val_str,"");
    tick = time(NULL);
    strcpy(instance_eable_name,"");
    snprintf(time_buf,sizeof(time_buf),"%.24s",ctime(&tick));
    if (gethostname(hostname,128) == 0)
    {
        if ((host = gethostbyname(hostname)) != NULL)
        {
	      Local_IP =  GetLocalIP();
	      hostAddr.s_addr = Local_IP;
      	      host_ip = inet_ntoa(hostAddr);
        //    host_ip = inet_ntoa(*(struct in_addr *)(*host->h_addr_list));
            sprintf(hostip_buf,"\x1b[22;10HLocal IP: %s\x1b[22;35H   hostname: %s",host_ip,hostname);
          //  Local_IP = ((struct in_addr *)(*host->h_addr_list))->s_addr;
            Peer_IP[0] = Local_IP;
            for(i=1; i<16; i++)
            {
                Peer_IP[i] = 0;
                IP_Selected[i] = 1;
            }
        }
    }


    debug_counter = 0;
    show_all_value=0;
    show_type=0;
    show_expend=1;
    show_range=0;
    get_show_state=0; 
    show_list=1;
    
    
    
    show_snmp_state=0;
    list_once=0;
    for(i=0;i<=255;i++)
    {
        curr_instance_arr[i].ins=i;
        curr_instance_arr[i].eable=0;
    }
    for(i=0;i<64;i++)
    {
        list_field_arr[i]=0xFF;
    }
    list_flag=list_start=list_end=0;
    debug_get_all_value = 0;
    debug_cfg = 0;
    debug_cfg_end = 1;
    debug_log_all = 0;
    debug_log_error_on = 0;
    debug_module_num = 13;
    t10s_flag = 0;
    t5s_flag=0;
    t100ms_flag = 0;
    get_state = 0;
    set_state = 0;
    ip_num=0;
    ip_index=0;
    set_val = 0;
    param_node = NULL;
    bind_num=0;

    debug_heartbeat[0] = 3;
    debug_heartbeat[1] = 3;
    debug_heartbeat[2] = 2;
    debug_heartbeat[3] = 1; //status 1-normal 0-abnormal
    debug_heartbeat[4] = 0; //module count
    debug_heartbeat[5] = 0; //sub module status
    debug_heartbeat[6] = 0; //sub module status
    debug_heartbeat[7] = 0;
    first_page=1;           
    disp_main_flag = 0;     //--lw
    disp_main_page_num	=	0;
    treehost=1;            //--lw
    disp_sub_module_flag = 13;
    disp_page_flag = 0;
    m_state = 0;
    command_flag = 0;
    ascii_out_len = 0;
    strcpy((char *)test_array,"");
    ascii_in_pt	= 1;
    ascii_out_ph = 0;
    ascii_out_pt = 0;
    title_top_pt = 0;
    cursor_p[0]	= 0x1b;
    cursor_p[1] = '[';
    cursor_p[4] = ';';
    cursor_p[7] = 'H';
    memset(comp_disp,0x80,500);
    for(i = 0;i < MODULE_COUNT;i ++)
    {
        strcpy(debug_shm_ptr->module_d[i].name,"");
        strcpy(debug_shm_ptr->module_d[i].status,"");
        strcpy(debug_shm_ptr->module_d[i].version,"");
        debug_shm_ptr->module_d[i].maxpage = 0;
        debug_shm_ptr->module_d[i].ascii_in_set_flag = 0;
        debug_shm_ptr->module_d[i].ascii_out_set_flag = 0;
        debug_shm_ptr->module_d[i].ascii_in = NULL;
        debug_shm_ptr->module_d[i].ascii_out = NULL;
        for(j = 0;j < PAGE_COUNT;j ++)
        {
            strcpy(debug_shm_ptr->module_d[i].page_d[j].title,title_top);
            debug_shm_ptr->module_d[i].page_d[j].pointer_set_flag = 0;
            for (k = 0;k < LINE_COUNT;k ++)
            {
                debug_shm_ptr->module_d[i].page_d[j].line_d[k].length = 0;
                debug_shm_ptr->module_d[i].page_d[j].line_d[k].pt_vartype = 0;
                debug_shm_ptr->module_d[i].page_d[j].line_d[k].pointer = NULL;
            }
        }
        debug_shm_ptr->module_d[i].seqid_h = 0;
        debug_shm_ptr->module_d[i].seqid_t = 0;
        for (j = 0;j < DEBUG_SEQID_LEN;j ++)
            strcpy((char *)debug_shm_ptr->module_d[i].omc_outbuf[j],"");
    }
    debug_shm_ptr->TelnetSendData.telsend_datah = 0;
    memset(debug_shm_ptr->TelnetSendData.telsend_data,0,sizeof(BYTE)*TELNET_SEND_BUF_LEN);
    memset(debug_snmp_agent_buf,0,8*sizeof(debug_snmp_agent));
    memset(&snmp_agent_packet,0,sizeof(debug_snmp_agent));
}

////////////////////////////End of Intial///////////////////////////////





////////////////////////////////////Main/////////////////////////////
/*
*
*       Fun:    debug_rt
*
*       Desc:   debug main function
*
*       Ret:    void
*
*       File:   debug.c
*
*/
void debug_rt()
{
    register int	jj,kk;
    int		ii;
    int		otherAsciiOutLen=0;
    int		localAsciiOutLen=0;
    struct	debug_module_struct		*module_ptr;
    //int i;
       
       
    if(Init_Page)
    {   
		    if( disp_sub_module_flag == ('n' - 'a') )  // in debug_module
		    {
		    	    	
		        //receive the ascii out buffer of debug module
		        module_ptr = (struct debug_module_struct *) &debug_shm_ptr->module_d['n'-'a'];
		        if (module_ptr->ascii_out_set_flag == 1)
		        {
		            if (debug_ascii_out_ph < debug_ascii_out_pt)
		                otherAsciiOutLen = MAX_ASCII_LEN - 1 + debug_ascii_out_ph - debug_ascii_out_pt;
		            else
		                otherAsciiOutLen = debug_ascii_out_ph - debug_ascii_out_pt;
		            
		            if (ascii_out_ph < ascii_out_pt)
		                localAsciiOutLen = MAX_ASCII_LEN - 1 + ascii_out_ph - ascii_out_pt;
		            else
		                localAsciiOutLen = ascii_out_ph - ascii_out_pt;
		            if ((otherAsciiOutLen > 0) && (otherAsciiOutLen+localAsciiOutLen < MAX_ASCII_LEN))
		            {
		                kk = ascii_out_ph;                  	           
		                for (jj = 0;jj < otherAsciiOutLen;jj ++)
		                {
		                    ascii_out_array[kk] = *(module_ptr->ascii_out+debug_ascii_out_pt);
		                    kk = (kk + 1) % MAX_ASCII_LEN;
		                    debug_ascii_out_pt=(debug_ascii_out_pt+1) % MAX_ASCII_LEN;
		                }                                                    
		                ascii_out_ph = kk % MAX_ASCII_LEN;
		            }
		            else if((localAsciiOutLen+MAX_SEND_CHAR_NUM<MAX_ASCII_LEN) && (otherAsciiOutLen > 0))
		            {
		            	 kk = ascii_out_ph;                  	           
		                for (jj = 0;jj < MAX_SEND_CHAR_NUM;jj ++)
		                {
		                    ascii_out_array[kk] = *(module_ptr->ascii_out+debug_ascii_out_pt);
		                    kk = (kk + 1) % MAX_ASCII_LEN;
		                    debug_ascii_out_pt=(debug_ascii_out_pt+1) % MAX_ASCII_LEN;
		                }                                                    
		                ascii_out_ph = kk % MAX_ASCII_LEN;
		            }
		                    
		        }
		        //0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 15
		        //a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z  
		        //a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  r  s  t  u  v  w  y  z
		        if(debug_cfg == 0 && Init_Page==2)  // in debug cmm status, receive the ascii out buffer of all the other modules
		        {
		           for(ii=0;ii< MODULE_COUNT;ii++)
		            {   
		            	  if(ii=='l'-'a' || ii=='a'-'a')
		            	     continue;
		                module_ptr = (struct debug_module_struct *) &debug_shm_ptr->module_d[ii];
		                if (module_ptr->ascii_out_set_flag == 1) 
		                { 
		                    otherAsciiOutLen=strlen((char *)module_ptr->ascii_out)+strlen(module_ptr->name)+5; 
		                    if (disp_sub_module_flag != ii)
		                    {
		                    	  
		                        if (ascii_out_ph < ascii_out_pt)
		                            localAsciiOutLen = MAX_ASCII_LEN - 1 + ascii_out_ph - ascii_out_pt;
		                        else
		                            localAsciiOutLen = ascii_out_ph - ascii_out_pt;
		                        if ((otherAsciiOutLen > (strlen(module_ptr->name)+5)) && (otherAsciiOutLen+localAsciiOutLen < MAX_ASCII_LEN))
		                        {
		                        	  
		                            ascii_out_array[ascii_out_ph] = '\r';
		                            ascii_out_ph = (ascii_out_ph + 1) % MAX_ASCII_LEN;
		                            ascii_out_array[ascii_out_ph] = '\n';
		                            ascii_out_ph = (ascii_out_ph + 1) % MAX_ASCII_LEN;
		                            ascii_out_array[ascii_out_ph] = '[';
		                            ascii_out_ph = (ascii_out_ph + 1) % MAX_ASCII_LEN;
		                            for (jj = 0;jj < strlen(module_ptr->name);jj ++)
		                            {
		                                ascii_out_array[ascii_out_ph] = module_ptr->name[jj];
		                                ascii_out_ph = (ascii_out_ph + 1) % MAX_ASCII_LEN;
		                            }
		                            ascii_out_array[ascii_out_ph] = ']';
		                            ascii_out_ph = (ascii_out_ph + 1) % MAX_ASCII_LEN;
		                            ascii_out_array[ascii_out_ph] = ':';
		                            ascii_out_ph = (ascii_out_ph + 1) % MAX_ASCII_LEN;
		                            ascii_out_array[ascii_out_ph] = '\r';
		                            ascii_out_ph = (ascii_out_ph + 1) % MAX_ASCII_LEN;
		                            ascii_out_array[ascii_out_ph] = '\n';
		                            ascii_out_ph = (ascii_out_ph + 1) % MAX_ASCII_LEN;
		
		                            kk = ascii_out_ph;                                   
		               							for (jj = 0;jj < otherAsciiOutLen;jj ++)
								                {
								                    ascii_out_array[kk] = module_ptr->ascii_out[jj];
								                    module_ptr->ascii_out[jj] = '\0';
								                    kk = (kk + 1) % MAX_ASCII_LEN;
								                }                            
		                            ascii_out_ph = kk % MAX_ASCII_LEN;
		                        }
		                    }
		                }
		               
		            }
		        }                 
		    }
		    else
		    {	
		        for (ii = 0;ii < MODULE_COUNT;ii ++)	// not in debug_module
		        {
		            module_ptr = (struct debug_module_struct *) &debug_shm_ptr->module_d[ii];
		            if (module_ptr->ascii_out_set_flag == 1)
		            {
		                otherAsciiOutLen = strlen((char *)module_ptr->ascii_out);
		                if (disp_sub_module_flag == ii)
		                {
		                    if (ascii_out_ph < ascii_out_pt)
		                        localAsciiOutLen = MAX_ASCII_LEN - 1 + ascii_out_ph - ascii_out_pt;
		                    else
		                        localAsciiOutLen = ascii_out_ph - ascii_out_pt;
		                    if ((otherAsciiOutLen > 0) && (otherAsciiOutLen+localAsciiOutLen < MAX_ASCII_LEN))
		                    {
		                        kk = ascii_out_ph;
		                        for (jj = 0;jj < otherAsciiOutLen;jj ++)
		                        {
		                            ascii_out_array[kk] = module_ptr->ascii_out[jj];
		                            module_ptr->ascii_out[jj] = '\0';
		                            kk = (kk + 1) % MAX_ASCII_LEN;
		                        }
		                        ascii_out_ph = kk % MAX_ASCII_LEN;
		                    }
		                }
		                if ((otherAsciiOutLen > 0) && (otherAsciiOutLen < DEBUG_OMCBUF_LEN-1))
		                {
		                    ii = module_ptr->seqid_h % DEBUG_SEQID_LEN;
		                    strcpy((char *)module_ptr->omc_outbuf[ii], (char *)module_ptr->ascii_out);
		                    if (((module_ptr->seqid_h+1)%DEBUG_SEQID_LEN) == (module_ptr->seqid_t%DEBUG_SEQID_LEN))
		                        if (disp_sub_module_flag == ii)
		                            module_ptr->seqid_t = (module_ptr->seqid_t+1) % DEBUG_MAX_SEQID;
		                    module_ptr->seqid_h = (module_ptr->seqid_h+1) % DEBUG_MAX_SEQID;
		                    for (jj = 0;jj < otherAsciiOutLen;jj ++)
		                        module_ptr->ascii_out[jj] = '\0';
		                }
		            }
		        }
		    }
    }
    //else
    //   debug_mmi_log("none"); 
              				   
        
    // in debug_module, in the configure state, not in command mode, and there is nothing to send by serial, there is nothing received,
    if ( (disp_sub_module_flag == ('n'-'a')) && (debug_cfg > 0) && (command_flag == 0) && (ascii_out_ph == ascii_out_pt) && (sendserbuf_h == sendserbuf_t) && (recserbuf_h == recserbuf_t) && (debug_cfg_end == 1) )
    {
        recser_buf[recserbuf_h] = 27;	// set the debug module in command mode
        recserbuf_h = (recserbuf_h + 1) % SERIAL_BUF_LEN;

    }
    if((disp_main_flag==0) && (first_page==1) && (command_flag == 0) && (ascii_out_ph == ascii_out_pt) && (sendserbuf_h == sendserbuf_t) && (recserbuf_h == recserbuf_t) )
    {
        recser_buf[recserbuf_h] ='n';	// set the debug module in command mode
        recserbuf_h = (recserbuf_h + 1) % SERIAL_BUF_LEN;
        first_page = 0;
    }  
    debug_read_serial();    
    
    for (ii = 0;ii < MAX_SEND_CHAR_NUM ;ii ++) 
    {
    	  debug_recv_cmd();
    	  if(send_char_flag)
    	  {
    	     send_char_flag=0;
    	     break; 
    	  }
        debug_module_page(ii); 
    } 
          
    debug_send_serial();
    
    if(disp_sub_module_flag == ('n'-'a'))
        debug_ascii_in_cmd_handle();	//  handle the ascii command of the debug module
    for(ii = 0;ii < 20;ii ++)
        test_array[ii] = test_array[ii] + 1;
    if ((t10s_flag++) >= 500)
    {
        t10s_flag = 0;
        debug_heartbeat[4] = debug_counter;
        set_status(5,7,debug_heartbeat);
    }
    if( wxc2_debuginfo.mmi_switch )
    {
      if((t100ms_flag++) >= 1)//20 ms
      {
        t100ms_flag=0;
        if(get_show_state==0 && list_flag==0)
        {
            show_all_value=0;
            show_type=0; 
            show_expend=1;
            show_range=0;
            show_empty=0;
            show_snmp_state=0;
        }
        if(t5s_flag > 250)
            t5s_flag=0;
        else
            t5s_flag++; 
        debug_get_agent();
        debug_set_agent();
	      //if((field_compute_state>0) && print_once==1) 
		    //     compute_field_WatchDog(); 		  	 		
		    
        debug_snmp_get_rt();	// get command state machine
 
        debug_snmp_set_rt();	// set command state machine
        if(list_flag>0 )        //&& debug_cfg_end==0 && debug_cfg==1)
        {
            debug_mmi_list();                     
        }
        if(debug_remark_flag>0)
           debug_mmi_remark();
      }
    }
    
}
//////////////////////////////End of Main////////////////////////////////



char *StrTrimCh( char *pSrc , char cTrim  );
 char *StrToLower( char *pStr );
////////////////////////////////////Shell///////////////////////////
/*
*
*       Fun:    debug_ascii_in_cmd_handle
*
*       Desc:   handle the command in the debug_ascii_in_array and clear the debug_ascii_in_array
*
*       Ret:    REVOID
*
*       File:   debug.c
*
*/
void debug_ascii_in_cmd_handle() 
{
    BYTE       cmd_valid,	i;
    char	     temp_str[256]="";
    BYTE       index_len;
    mib_node  *find_node_ptr ;
    /*
     for(i=0;i<80;i++)
     {
         if(*(debug_ascii_in_array+i)!=' ')
             for(j=i;j<80;j++)
                 debug_ascii_in_array[j]=debug_ascii_in_array[j+1];
     } 
     */
    if(snmp_agent_flag>0 && debug_cfg>0 && *debug_ascii_in_array!=0)
    {
    	if(!debug_busy_alarm)
    	{
       debug_ascii_out_proc("Busy    : Busy now,Debug is working for ews now!\r\n");
       debug_ascii_in_array[0]='\0';
      }
       debug_busy_alarm=1;
       return;
    }else if(snmp_agent_flag==0)
       debug_busy_alarm=0;
    StrToLower( (char *)debug_ascii_in_array +1 );
    if ( *debug_ascii_in_array)
    {
        cmd_valid = 1;
        debug_ascii_in_array[79] = '\0';
        if(debug_cfg == 0)
        {
            if(debug_module_num != 13)
            {
                if(strncasecmp((char *)debug_ascii_in_array+1, "DEBUG", 5) == 0)
                {
                    debug_module_num= 13;
                    debug_shm_ptr->module_d[13].ascii_in[0] = 1;
                    strcpy((char *)debug_shm_ptr->module_d[13].ascii_in+1, "help");
                    debug_color_out(BRIGHT,WHITE,BLACK,"\r\nOK\r\n");///
                    return;
                }
                else
                {
                    i = debug_module_num;
                    if(debug_shm_ptr->module_d[i].ascii_in_set_flag != 1)
                    {
                        sprintf(temp_str, "The module %s did not register\r\nFail\r\n", debug_shm_ptr->module_d[i].name);///
                        debug_color_out(BRIGHT,RED,BLACK,temp_str);
                        return;
                    }
                    else
                    {
                        debug_shm_ptr->module_d[i].ascii_in[0] = 1;
                        strcpy((char *)debug_shm_ptr->module_d[i].ascii_in+1, (char *)debug_ascii_in_array+1);
                        strcpy(temp_str,"OK!\r\n");
                        debug_color_out(BRIGHT,WHITE,BLACK,temp_str);
                        strcpy((char *)debug_ascii_in_array, "");
                    }
                    return;
                }
            }

            for (i=0; i < MODULE_COUNT; i++)
            {
                if(debug_shm_ptr->module_d[i].ascii_in_set_flag == 1)
                {
                	  char module_name[10] , *chpos;
                	  int  len = strlen(debug_shm_ptr->module_d[i].name) ;
                	  
                	  
                	  if( len == 0  || len > 8 )
                	  	  continue;
                	  strcpy( module_name , debug_shm_ptr->module_d[i].name );
                	  StrToLower( module_name );
                	  if( strstr( (char *)debug_ascii_in_array+1, module_name ) != NULL )
                    {
                    	  if( ( ( chpos = strstr( (char *)debug_ascii_in_array+1 , "log all" ) ) != NULL )
                    	  	||( ( chpos = strstr( (char *)debug_ascii_in_array+1 , "log none" )) != NULL )
                          ||( ( chpos = strstr( (char *)debug_ascii_in_array+1 , "log error" )) != NULL )
                          ||( ( chpos = strstr( (char *)debug_ascii_in_array+1 , "log status" )) != NULL )  )
                        {
                        	  debug_shm_ptr->module_d[i].ascii_in[0] = 1;
                            strcpy((char *)debug_shm_ptr->module_d[i].ascii_in+1,chpos );
                            strcpy(temp_str,"OK!\r\n");
                            debug_color_out(BRIGHT,WHITE,BLACK,temp_str);
                            cmd_valid = 5;
                            continue;
                        }
                        else
                        {                 	  	
                            if(strlen((char *)debug_ascii_in_array) > (2+strlen(debug_shm_ptr->module_d[i].name)))
                            {
                                debug_shm_ptr->module_d[i].ascii_in[0] = 1;
                                strcpy((char *)debug_shm_ptr->module_d[i].ascii_in+1, (char *)debug_ascii_in_array+2+strlen(debug_shm_ptr->module_d[i].name));
                                strcpy(temp_str,"OK!\r\n");
                                debug_color_out(BRIGHT,WHITE,BLACK,temp_str);
                                strcpy((char *)debug_ascii_in_array, "");
                                debug_module_num = 13;
                            }
                            else
                            {
                                debug_shm_ptr->module_d[i].ascii_in[0] = 1;
                                strcpy((char *)debug_shm_ptr->module_d[i].ascii_in+1, "help");
                                strcpy(temp_str,"OK!\r\n");
                                debug_color_out(BRIGHT,WHITE,BLACK,temp_str);
                                strcpy((char *)debug_ascii_in_array, "");
                                debug_module_num = i;
                            }
                            return;
                        }
                    }
                }
            }
            if( cmd_valid == 5 )
            {
            	  strcpy((char *)debug_ascii_in_array, "");
            	  return;
            }
            if (strncasecmp((char *)debug_ascii_in_array+1,"help",4) == 0)
            {
                debug_mmi_cmm_help(debug_ascii_in_array+5);

            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"log status",10) == 0)
            {
                debug_mmi_log_status();
            }
            else if( strncasecmp((char *)debug_ascii_in_array+ 1 , "net cap start" ,13) == 0)
            {
            	int  ip;
            	char tmpStr[512];
            	
            	strcpy( tmpStr , (char *)debug_ascii_in_array+14);
            	StrTrimCh(tmpStr, ' ');
            	ip = inet_addr(tmpStr);
            	if( -1 == ip )
            	{
            	  sprintf(tmpStr , "invalid Ip Addr : %s ", debug_ascii_in_array+14);
            	  debug_color_out(BRIGHT,RED,BLACK,tmpStr);
            	  cmd_valid = 2;
            	}
            	NcSetIP(ip);
            	debug_netcap_start();
            	debug_color_out(BRIGHT,RED,BLACK,"net cap start!\r\n");
            }
             else if( strncasecmp((char *)debug_ascii_in_array+ 1 , "net cap stop" ,12)== 0)
            {
            	debug_netcap_stop();
            	debug_color_out(BRIGHT,RED,BLACK,"net cap stop!\r\n");
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"log ",4) == 0)
            {
                if( debug_mmi_log((char *)debug_ascii_in_array+5) == 0)
                    cmd_valid = 2;
            }else if(strncasecmp((char *)debug_ascii_in_array+1, "cfg", 3) == 0)
            {               			  
            	if( !wxc2_debuginfo.mmi_switch )  
            	{
            		  debug_ascii_out_proc("[debug] : The debug MMI is not configurated!\r\n");
                  cmd_valid = 3;
            	}
            	else
            	{
				        strcpy(temp_str, "log none");
				        debug_log_all = 0;  
						    for (i=0; i < MODULE_COUNT; i++)
						    {
						        if( (i != 13) && (debug_shm_ptr->module_d[i].ascii_in_set_flag == 1))
						        {
						            debug_shm_ptr->module_d[i].ascii_in[0] = 1;
						            strcpy((char *)debug_shm_ptr->module_d[i].ascii_in+1,temp_str);
						        }
						    }
	              debug_cfg = 1;
	              debug_cfg_end = 1;
	              debug_mmi_cfg_help(" "); 
	              index_len=9;
	              find_node_ptr = search_mib_node(mib_root_node, cur_oid, index_len);
	              if(find_node_ptr)
	                 mib_curr_node = find_node_ptr;
	              else
	              { 
	                  debug_ascii_out_proc("[debug] : The debug can not find the system node!\r\n");
	                  return;
	              }
              }
            }
            else if(strncasecmp((char *)debug_ascii_in_array+1, "menu", 4) == 0)
            {                
            	  if( !wxc2_debuginfo.mmi_switch )  
            	  {
            		  debug_ascii_out_proc("[debug] : The debug MMI is not configurated!\r\n");
                  cmd_valid = 3;
            	  }
            	  else
            	  {
	                menu_curr_node=menu_root_node;
	                mib_curr_node=menu_curr_node->menu_oid_node;
	                main_menu("");
	                debug_cfg = 2;
	                debug_cfg_end = 1 ;
                }
            }

            else
                cmd_valid = 2;//Err
        }
        else if(debug_cfg == 1 )
        {
            if (strncasecmp((char *)debug_ascii_in_array+1,"contents",8) == 0)
            {
                debug_mmi_contents((char *)debug_ascii_in_array+9);
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"parent",6) == 0)
            {
                debug_mmi_parent();
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"quit",4) == 0)
            {
                debug_send_char('q');
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"child ", 6) == 0)
            {
                debug_mmi_child((char *)debug_ascii_in_array+7);
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"create", 6) == 0)
            {
                debug_mmi_create((char *)debug_ascii_in_array+7);
            }
            else if (strncmp((char *)debug_ascii_in_array+1,"delete",6) == 0)
            {
                debug_mmi_del((char *)debug_ascii_in_array+7);
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"list", 4) == 0)
            {
                list_flag=1;
                strcpy((char *)list_msg, (char *)debug_ascii_in_array+5);
                debug_cfg_end=0;
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"save",4) == 0)
            {
                save_flag=2;
                debug_mmi_save(); 
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"option ", 7) == 0)
            {
                debug_mmi_option((char *)debug_ascii_in_array+8);
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"setins ", 7) == 0)
            {
                debug_mmi_setins((char *)debug_ascii_in_array+8);
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"set", 3) == 0)
            {
                if(debug_mmi_set((char *)debug_ascii_in_array+4) == 0)
                {
                    cmd_valid = 3;	//fail
                    debug_cfg_end = 1;
                }
                else
                {
                    debug_cfg_end = 0;
                    cmd_valid = 4;	//wait
                }
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"get ", 4) == 0)
            {
                if(debug_mmi_get((char *)debug_ascii_in_array+5) == 0)
                {
                    cmd_valid = 3;	//fail
                    debug_cfg_end = 1;
                }
                else
                {
                    debug_cfg_end = 0;
                    cmd_valid = 4;	//wait
                }
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"get", 3) == 0)
            {
                if(debug_ascii_in_array[4] != '\0')
                {
                    cmd_valid = 2;	//fail
                    debug_cfg_end = 1;
                }
                else if(debug_mmi_get_all() == 0)
                {
                    cmd_valid = 3;	//fail
                    debug_cfg_end = 1;
                }
                else
                {
                    debug_cfg_end = 0;
                    cmd_valid = 4;	//wait
                }
            }
            else if (strcmp((char *)debug_ascii_in_array+1,"pwd") == 0)
            {
                debug_mmi_pwd();
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"tree", 4) == 0)
            {
                /*BYTE node_layer;
                if(isdigit(debug_ascii_in_array+5))
                node_layer=(BYTE)(debug_ascii_in_array+5);
                	*/
                debug_mmi_tree(mib_curr_node, "{*}--", 3);
                treehost=1;
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"cd", 2) == 0)
            {
                debug_mmi_cd((char *)debug_ascii_in_array+3);
            }
            /*else if (strncasecmp(debug_ascii_in_array+1,"ip ", 3) == 0)
            {
            if( debug_mmi_ip(debug_ascii_in_array+4) == 0)
            cmd_valid = 3;	//Fail
            }*/
            else if (strncasecmp((char *)debug_ascii_in_array+1,"find host", 9) == 0)
            {
                debug_mmi_find_host(debug_ascii_in_array+10);
                debug_mmi_show_host();
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"show host", 9) == 0)
            {
                if( debug_mmi_show_host() ==0)
                    cmd_valid = 3;	//Fail
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"select", 6) == 0)
            {
                if( debug_mmi_select_host((char *)debug_ascii_in_array+7) ==0)
                    cmd_valid = 3;	//Fail
                else
                    debug_mmi_show_host();
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"help",4) == 0)
            {
                debug_mmi_cfg_help(debug_ascii_in_array+5);
            }
            else if (strcmp((char *)debug_ascii_in_array+1,"pwd") == 0)
            {
                debug_mmi_pwd();
            }
            else if(strcmp((char *)debug_ascii_in_array+1,"remark") == 0)
            {
                debug_remark_flag=1;
                debug_cfg_end=0;
            }
            else if(strcmp((char *)debug_ascii_in_array+1, "cmm")==0)
            {

                debug_cfg = 0;
                debug_cfg_end = 1;
                debug_mmi_cmm_help(" ");
            }
            else
            {
                cmd_valid = 2;  //Err 
            }
        }
        else if(debug_cfg==2)
        {
            if (strncasecmp((char *)debug_ascii_in_array+1,"contents",8) == 0)
            {
                debug_mmi_view();
                //debug_cfg_end=1;
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"cd",2) == 0)
            {
                debug_mmi_mcd((char *)debug_ascii_in_array+3);
                debug_cfg_end=1; 
            }
            else if(strcmp((char *)debug_ascii_in_array+1,"remark") == 0)
            {
                debug_remark_flag=1;
                debug_cfg_end=0; 
            }
             else if (strncasecmp((char *)debug_ascii_in_array+1,"child",5) == 0)
            {
                debug_mmi_mcd((char *)debug_ascii_in_array+6);
                debug_cfg_end=1;  
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"quit",4) == 0)
            {
                debug_send_char('q');
            }
            else  if (strncasecmp((char *)debug_ascii_in_array+1,"create", 6) == 0)
            {
                debug_mmi_create((char *)debug_ascii_in_array+7);
            }
            else if (strncmp((char *)debug_ascii_in_array+1,"delete",6) == 0)
            {
                debug_mmi_del((char *)debug_ascii_in_array+7);
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"list", 4) == 0)
            {
                list_flag=1;
                strcpy((char *)list_msg, (char *)debug_ascii_in_array+5);
                debug_cfg_end=0;
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"save",4) == 0)
            {
                save_flag=2;
                debug_mmi_save();
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"option ", 7) == 0)
            {
                debug_mmi_option((char *)debug_ascii_in_array+8);
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"setins ", 7) == 0)
            {
                debug_mmi_setins((char *)debug_ascii_in_array+8);
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"set", 3) == 0)
            {
                if(debug_mmi_set((char *)debug_ascii_in_array+4) == 0)
                {
                    cmd_valid = 3;	//fail
                    debug_cfg_end = 1;
                }
                else
                {
                    debug_cfg_end = 0;
                    cmd_valid = 4;	//wait
                }
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"get ", 4) == 0)
            {
                if(debug_mmi_get((char *)debug_ascii_in_array+5) == 0)
                {
                    cmd_valid = 3;	//fail
                    debug_cfg_end = 1;
                }
                else
                {
                    debug_cfg_end = 0;
                    cmd_valid = 4;	//wait
                }
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"get", 3) == 0)
            {
                if(debug_ascii_in_array[4] != '\0')
                {
                    cmd_valid = 2;	//fail
                    debug_cfg_end = 1;
                }
                else if(debug_mmi_get_all() == 0)
                {
                    cmd_valid = 3;	//fail
                    debug_cfg_end = 1;
                }
                else
                {
                    debug_cfg_end = 0;
                    cmd_valid = 4;	//wait
                }
            }  
            /*else if (strcmp(debug_ascii_in_array+1,"pwd") == 0)
            {
                debug_mmi_pwd();
            }
            else if (strncasecmp(debug_ascii_in_array+1,"tree", 4) == 0)
            {
                BYTE node_layer;
                if(isdigit(debug_ascii_in_array+5))
                node_layer=(BYTE)(debug_ascii_in_array+5);
                
                debug_mmi_tree(mib_curr_node, "{*}--", 3);
                treehost=1;
            }
            */
            else  if (strncasecmp((char *)debug_ascii_in_array+1,"find host", 9) == 0)
            {
                debug_mmi_find_host(debug_ascii_in_array+10);
                debug_mmi_show_host();
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"show host", 9) == 0)
            {
                if( debug_mmi_show_host() ==0)
                    cmd_valid = 3;	//Fail
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"select", 6) == 0)
            {
                if( debug_mmi_select_host((char *)debug_ascii_in_array+7) ==0)
                    cmd_valid = 3;	//Fail
                else
                    debug_mmi_show_host();
            }
            else if (strncasecmp((char *)debug_ascii_in_array+1,"help",4) == 0)
            {
                main_menu((char *)debug_ascii_in_array+5); 
            }
            /*
            else if(strcmp(debug_ascii_in_array+1,"remark") == 0)
            {
                debug_mmi_remark();
            }*/
            else if(strcmp((char *)debug_ascii_in_array+1, "cmm")==0)
            {

                debug_cfg = 0;
                debug_cfg_end = 1;
                debug_mmi_cmm_help(" ");
            }
            else
            {
                cmd_valid = 2;  //Err
            }
        }

        if (cmd_valid == 1)
        {
            // debug_color_out(BRIGHT,WHITE,BLACK,"OK!\r\n");
        }
        else if(cmd_valid == 3)//Fail
        {
            debug_color_out(BRIGHT,RED,BLACK,"Fail    : Command excute fail!\r\n");
        }
        else if(cmd_valid == 2)
        {
            sprintf(temp_str,  "Error   : Incorrect command: %s\r\n",debug_ascii_in_array);
            debug_color_out(BRIGHT,RED,BLACK,temp_str);
        }
        strcpy((char *)debug_ascii_in_array, "");
    }
}	//	end of debug_ascii_in_cmd_handle
///////////////////////////////End of Shell//////////////////////////////





//////////////////////////////////buffer process/////////////////////
/*
*
*       Fun:    debug_ascii_out_porc
*
*       Desc:   store the string into the out buffer,print in the telnet or pc serial port
*
*       Ret:    REVOID

*       File:   debug.c
*
*/

void debug_ascii_out_proc(const char *str_ptr)     
{
	int str_len,local_len; 
	str_len=strlen(str_ptr); 
	if (debug_ascii_out_ph < debug_ascii_out_pt)
     local_len = MAX_ASCII_LEN - 1 + debug_ascii_out_ph - debug_ascii_out_pt;///
  else
     local_len = debug_ascii_out_ph - debug_ascii_out_pt;
  if( str_len>0 && (local_len + str_len) < (MAX_ASCII_LEN))
  {
  	   while((*(debug_ascii_out_array+debug_ascii_out_ph)=*str_ptr++))
  	   				debug_ascii_out_ph=(debug_ascii_out_ph+1)%MAX_ASCII_LEN;       
  }
}	//	debug_ascii_out_proc 

/*
void debug_ascii_out_proc(char *str_ptr)
{
    if( (strlen(debug_ascii_out_array) + strlen(str_ptr)) < (MAX_ASCII_LEN))
        strcat(debug_ascii_out_array, str_ptr);
}	//	debug_ascii_out_proc
*/
/////////////////////////////////End of buffer process///////////////////////







////////////////////////////////page 0//////////////////////////////
/*
*
*       Fun:    debug_change_title1
*
*       Desc:   show module page form module_page.txt(page=0)
*
*       Ret:    REVOID
*       File:   debug.c
*
*/
void debug_read_main_title()
{
    FILE	*fp;
    int		i,j,k;
    int		row;
    char	*temp_ch;
    char	temp_str[256];
    char	top[9]="\0",tail[6]="\0",/*add_help[6]="\0",*/add_menu[6]="\0";
    char	title_row[MAX_ROW][MAX_COLUMN];

    top[0] = 0xc;
    top[1] = 0x1b;
    top[2] = '[';
    top[3] = 'H';
    top[4] = 0x1b;
    top[5] = '[';
    top[6] = '2';
    top[7] = 'J';
    top[8] = '\0';
    tail[0] = 0x1b;
    tail[1] = '[';
    tail[2] = '0';
    tail[3] = 'm';
    tail[4] = '%';
    tail[5] = '\0'; 
    
    //page 0
    row = 0;
    if ((fp = fopen("./conf/menu_page","r")) == NULL)
    {
        printf("[debug] Cannot open file [menu_page].\n");
        return;
    }
    for (i = 0;i < MAX_ROW; i++)
    {
        if (feof(fp))
            break;
        temp_str[0] = '\0';
        fgets(temp_str,256,fp);
        if (temp_str[0] == '$')
            break;
        temp_ch = strchr(temp_str,0xa);
        if (temp_ch != NULL)
            *temp_ch = '\0';
        strcpy(title_row[row++],temp_str);
    }
    fclose(fp);
    strcpy(title1,top);
    add_menu[0]	= 0x1b;
    add_menu[1] = '[';
    for (i = 0;i < row;i ++)
    {
        for (j = 0;j < PAGE_MODULE_COUNT;j ++)
        {
        	  int alph_num = j;
        	  if( j >=22 ) alph_num = j + 2;
        	  if( j > 15 && j < 22 ) alph_num = j + 1;
            sprintf(temp_str,"%c -- ",'a'+alph_num);
            temp_ch = strstr(title_row[i],temp_str);
            if (temp_ch != NULL)
            {
                temp_ch = temp_ch + strlen(temp_str);
                for (k = 0;k < DEBUG_NAME_LEN - 1;k ++)
                {
                    if (isalpha(temp_ch[k]) || isdigit(temp_ch[k]))
                        debug_shm_ptr->module_d[j].name[k] = temp_ch[k];
                    else
                        break;
                }
                debug_shm_ptr->module_d[j].name[k] = '\0';
            }
        }
        if (i < 10)
        {
            add_menu[2] = i+'0';
            add_menu[3] = 'H';
            add_menu[4] = '\0';
        }
        else
        {
            add_menu[2] = i/10+'0';
            add_menu[3] = i%10+'0';
            add_menu[4] = 'H';
            add_menu[5] = '\0';
        }
        strcat(title1,add_menu);
        strcat(title1,title_row[i]);
    }
    
    //page 1
    row = 0;
    memset(title_row,0,MAX_ROW*MAX_COLUMN);
    if ((fp = fopen("./conf/menu_page2","r")) == NULL)
    {
        printf("[debug] Cannot open file [menu_page].\n");
        return;
    }
    for (i = 0;i < MAX_ROW; i++)
    {
        if (feof(fp))
            break;
        temp_str[0] = '\0';
        fgets(temp_str,256,fp);
        if (temp_str[0] == '$')
            break;
        temp_ch = strchr(temp_str,0xa);
        if (temp_ch != NULL)
            *temp_ch = '\0';
        strcpy(title_row[row++],temp_str);
    }
    fclose(fp);
    strcpy(title1+TITLE_CHAR,top);
    add_menu[0]	= 0x1b;
    add_menu[1] = '[';
    for (i = 0;i < row;i ++)
    {
        for (j = 0;j < PAGE_MODULE_COUNT;j ++)
        {
        	  int alph_num = j;
        	  if( j >=22 ) alph_num = j + 2;
        	  if( j > 15 && j < 22 ) alph_num = j + 1;
            sprintf(temp_str,"%c -- ",'a'+alph_num);
            temp_ch = strstr(title_row[i],temp_str);
            if (temp_ch != NULL)
            {
                temp_ch = temp_ch + strlen(temp_str);
                for (k = 0;k < DEBUG_NAME_LEN - 1;k ++)
                {
                    if (isalpha(temp_ch[k]) || isdigit(temp_ch[k]))
                        debug_shm_ptr->module_d[j+PAGE_MODULE_COUNT].name[k] = temp_ch[k];
                    else
                        break;
                }
                debug_shm_ptr->module_d[j+PAGE_MODULE_COUNT].name[k] = '\0';
            }
        }
        if (i < 10)
        {
            add_menu[2] = i+'0';
            add_menu[3] = 'H';
            add_menu[4] = '\0';
        }
        else
        {
            add_menu[2] = i/10+'0';
            add_menu[3] = i%10+'0';
            add_menu[4] = 'H';
            add_menu[5] = '\0';
        }
        strcat(title1+TITLE_CHAR,add_menu);
        strcat(title1+TITLE_CHAR,title_row[i]);
    }    
}

/*
*
*       Fun:    debug_change_title1
*
*       Desc:   Add String to module page(page=0)
*
*       Ret:    REVOID
	
*       File:   debug.c
*
*/
void debug_change_title1()
{
    char	ch_tmp1[14]="\0";
    int		i;
    DWORD title_len;
    char	tail[6] = "\0";
    char	menu_line[TITLE_CHAR] = "\0";
    
    if(disp_main_page_num >=DEBUG_MAX_PAGE)
    {
    	return;
    }
    title_len=strlen(title1+TITLE_CHAR*disp_main_page_num);
    if (title_len < TITLE_CHAR)
        strcpy(title1_show+TITLE_CHAR*disp_main_page_num,title1+TITLE_CHAR*disp_main_page_num);

    sprintf(menu_line,"\x1b[20;20H***      %s     ***\x1b[16;4H\r\n",wxc2_debuginfo.wxc2_ver);
    //In current page, press 'a--p' or 'A--P' to enter selected sub module",wxc2_debuginfo.wxc2_ver);
    //sprintf(menu_line+strlen(menu_line),"\x1b[17;4HIn sub module, press '0--9' and 'a--f' to turn over pages");
    //sprintf(menu_line+strlen(menu_line),"\x1b[18;4HIn sub module, press 'x' or 'X' to return menu page");
    sprintf(menu_line+strlen(menu_line),"\x1b[24;25Hsystem start time:%s\x1b[25;1Hr -- refresh\x1b[22;60H%d/%d",time_buf,disp_main_page_num+1,DEBUG_MAX_PAGE);
    tail[0]	= 0x1b;
    tail[1]	= '[';
    tail[2]	= '0';
    tail[3]	= 'm';
    tail[4]	= '%';
    tail[5]	= '\0';
    for (i = 0;i < PAGE_MODULE_COUNT;i ++)
    {
        if (debug_shm_ptr->module_d[i+disp_main_page_num*PAGE_MODULE_COUNT].status[0] == 1)
        {
            ch_tmp1[0]	= 0x1b;
            ch_tmp1[1]	= '[';
            if (i < PAGE_MODULE_COUNT/2)
            {
                ch_tmp1[2] = (i+5)/10 + '0';	//x =??
                ch_tmp1[3] = (i+5)%10 + '0';
            }
            else
            {
                ch_tmp1[2] = (i+5-PAGE_MODULE_COUNT/2)/10 + '0';
                ch_tmp1[3] = (i+5-PAGE_MODULE_COUNT/2)%10 + '0';
            }
            ch_tmp1[4]	= ';';
            if(i < PAGE_MODULE_COUNT/2)
            {	
                ch_tmp1[5]	= '2';	//y =28
                ch_tmp1[6]	= '8';	
            }
            else
           	{
                ch_tmp1[5]	= '5';
                ch_tmp1[6]	= '2';
            }
            
            ch_tmp1[7]	= 'H';
            ch_tmp1[8]	= '(';
            ch_tmp1[9] = '\0';
            if ((title_len+strlen(ch_tmp1)) < TITLE_CHAR)
                 strpcat(title1_show+TITLE_CHAR*disp_main_page_num,&title_len,ch_tmp1); 
            if ((title_len+strlen(debug_shm_ptr->module_d[i+disp_main_page_num*PAGE_MODULE_COUNT].version)) < TITLE_CHAR)
                strpcat(title1_show+TITLE_CHAR*disp_main_page_num,&title_len,debug_shm_ptr->module_d[i+disp_main_page_num*PAGE_MODULE_COUNT].version);
            if ((title_len+1) < TITLE_CHAR)
                strpcat(title1_show+TITLE_CHAR*disp_main_page_num,&title_len,")");
        }
    }
    if (title_len+strlen(hostip_buf) < TITLE_CHAR)
        strpcat(title1_show+TITLE_CHAR*disp_main_page_num,&title_len,hostip_buf);
    if (title_len+strlen(menu_line) < TITLE_CHAR)
        strpcat(title1_show+TITLE_CHAR*disp_main_page_num,&title_len,menu_line);
    if (title_len+strlen(tail) < TITLE_CHAR)
        strpcat(title1_show+TITLE_CHAR*disp_main_page_num,&title_len,tail);
        
    title_pt = TITLE_CHAR*disp_main_page_num;     
    memset(current_timer,0,sizeof(current_timer));
}
////////////////////////////End of Page 0//////////////////////////////





////////////////////////////////Module Page/////////////////////////
int debug_set_response(BYTE oidlen,DWORD *oid,BYTE *data_ptr,WORD data_len)
{
    WORD	temp_len;
    BYTE	temp_id,temp_page;
    BYTE	ch_m = 0x80;
    WORD	title_len = 0;
    BYTE	cancel_control[] = {0x1b,'[','0','m','%','\0'};

    switch(oid[12])
    {
    case 0:
        break;
    case 1:
        break;
    case 2:		// configure
        if (oid[13] > MODULE_COUNT+1 || oid[13] < 2)
            return -1;
        temp_id = oid[13] - 2;
        if( debug_shm_ptr == NULL )
        {
        	printf("Wanning : Debug is not init! Here register your module to debug share memery would fail!\r\n");
        	printf("          Please make assure that you function is need to inital debug or not!\r\n");
        	return -1;
        }
        //printf("Module ID: %d regiseted! oid[14] :%ld\n", temp_id,  oid[14]);
        switch(oid[14])
        {
        case 1:		// status
            if (data_len != 1)
                return -1;
            debug_shm_ptr->module_d[temp_id].status[0] = *data_ptr;
            debug_counter += 1;
            if (temp_id <= 7)
                debug_heartbeat[5] |= (ch_m >> temp_id);
            else
                debug_heartbeat[6] |= (ch_m >> (temp_id - 8));
            ch_m = 0x80; 
            return 1;
            break;
        case 2:		// module version
            if (data_len > 20)
                return -1;
            memcpy(debug_shm_ptr->module_d[temp_id].version,data_ptr,data_len);
            debug_shm_ptr->module_d[temp_id].version[data_len] = '\0';
            return strlen(debug_shm_ptr->module_d[temp_id].version);
            break;
        case 3:		// ascii in buffer
            debug_shm_ptr->module_d[temp_id].ascii_in = data_ptr;
            debug_shm_ptr->module_d[temp_id].ascii_in_set_flag = 1;
            return data_len;
            break;
        case 4:		// ascii out buffer
            ascii_out_len = (data_len < strlen((char *)data_ptr))?data_len:strlen((char *)data_ptr);
            *(data_ptr+ascii_out_len) = '\0';
            debug_shm_ptr->module_d[temp_id].ascii_out = data_ptr;
            debug_shm_ptr->module_d[temp_id].ascii_out_set_flag = 1;
            return ascii_out_len;
            break;
        default:	// display page 0 -- 15
            if (oid[14] > 20)
                return -1;
            temp_page = oid[14] - 5;
            if (temp_page == 0)	// do not fill page 0
                return -1;
            switch(oid[15])
            {
            case 0:
                break;
            case 1:		// title
                title_len = (data_len < strlen((char *)data_ptr))?data_len:strlen((char *)data_ptr);
                if (title_len > TITLE_CHAR - 8)
                    break;
                data_ptr[title_len] = '\0';
                strcpy(debug_shm_ptr->module_d[temp_id].page_d[temp_page].title,title_top);
                strcat(debug_shm_ptr->module_d[temp_id].page_d[temp_page].title, (char *)data_ptr);
                temp_len = strlen(debug_shm_ptr->module_d[temp_id].page_d[temp_page].title);
                if (temp_len+strlen((char *)cancel_control) < TITLE_CHAR)
                    debug_shm_ptr->module_d[temp_id].page_d[temp_page].title[temp_len] = '\0';
                if (temp_len+strlen((char *)cancel_control) < TITLE_CHAR)
                    strcat(debug_shm_ptr->module_d[temp_id].page_d[temp_page].title, (char *)cancel_control);
                return title_len;
                break;
            default:	// line
                if (oid[15] > MAX_ROW || oid[15] < 2)
                    return -1;
                switch(oid[16])
                {
                case 0:
                    break;
                case 1:
                    debug_shm_ptr->module_d[temp_id].page_d[temp_page].line_d[oid[15]-2].pt_vartype = *data_ptr;
                    break;
                case 2:
                    if (data_len <= 20)
                        debug_shm_ptr->module_d[temp_id].page_d[temp_page].line_d[oid[15]-2].length = data_len;
                    else
                        debug_shm_ptr->module_d[temp_id].page_d[temp_page].line_d[oid[15]-2].length = 20;
                    debug_shm_ptr->module_d[temp_id].page_d[temp_page].line_d[oid[15]-2].pointer = data_ptr;
                    debug_shm_ptr->module_d[temp_id].page_d[temp_page].pointer_set_flag = 1;
                    if (debug_shm_ptr->module_d[temp_id].maxpage < temp_page)
                        debug_shm_ptr->module_d[temp_id].maxpage = temp_page;
                    return data_len;
                    break;
                default:
                    break;
                }
                break;
            }
            break;
        }
        break;
    case 3:
        break;
    default:
        break;
    }
    return -1;
}


/*
*
*       Fun:    debug_module_page
*
*       Desc:   set debug page(page>0)
*
*       Ret:    REVOID
*       File:   debug.c
*
*/
void debug_module_page(int count)
{
    if (command_flag == 1)
        return;
    switch (disp_main_flag)
    {
    case 0:
        switch(m_state)
        {
        case 0:
            m_state	= 1;
            debug_change_title1();
            if(count>=1)
               send_char_flag=1;
            break;
        case 1:
        	  if (title1_show[title_pt] != '%')
            {
                if (debug_send_char(title1_show[title_pt]))
                    title_pt++;
            }
            else
            {
                debug_set_currenttime();
                SendTelnetFlag = 1;
                send_char_flag=1;
            }     
            break; 
        }
        break;
    case 1:		// module flag
        switch(m_state_sub_module)
        {
        case 0:
            title_top_pt = 0;
            m_state_sub_module = 1;
            break;
        case 1:
            if (disp_page_flag == 0)
            {
            	  debug_send_string(title_top);
            	  SendTelnetFlag = 1;
                m_state_sub_module = 2;
                ascii_out_ph = ascii_out_pt = 0;
                send_char_flag=1;               
            }
            else
            {
                m_state_sub_module = 3;
                title_sub_module_pt	= 0;
            }
            break;
        case 2:		// send page 0                 
	            if (ascii_out_pt != ascii_out_ph)
	            {	            	
	                debug_send_char(ascii_out_array[ascii_out_pt]);
	                ascii_out_pt = (ascii_out_pt + 1) % MAX_ASCII_LEN;
	                if (ascii_out_pt == ascii_out_ph)	                
	                    SendTelnetFlag = 1;	             	              
	            }           
	            else
	               send_char_flag=1;    
            break;
        case 3:		// title
            if (disp_page_flag > debug_shm_ptr->module_d[disp_sub_module_flag].maxpage)
                return;
            if (debug_shm_ptr->module_d[disp_sub_module_flag].page_d[disp_page_flag].title[title_sub_module_pt] == '%')
            {
                SendTelnetFlag = 1;
                m_state_sub_module = 4;
                disp_index = 0;
                memset(comp_disp,0xeb,500); 
            }
            else if (debug_send_char(debug_shm_ptr->module_d[disp_sub_module_flag].page_d[disp_page_flag].title[title_sub_module_pt]))
                title_sub_module_pt = (title_sub_module_pt+1)%TITLE_CHAR;
            //if(count>=3)
            //   send_char_flag=1;
            break;
        case 4:
            char_ptr = 0;
            hex_state = 0;
            disp_length	= debug_shm_ptr->module_d[disp_sub_module_flag].page_d[disp_page_flag].line_d[disp_index].length;
            if (debug_shm_ptr->module_d[disp_sub_module_flag].page_d[disp_page_flag].pointer_set_flag == 1)
                m_state_sub_module = 5;
            disp_ptr = debug_shm_ptr->module_d[disp_sub_module_flag].page_d[disp_page_flag].line_d[disp_index].pointer;
            break;
        case 5:
            if (char_ptr == disp_length)
            {
                if (disp_index%3 == 0)
                    SendTelnetFlag = 1;
                disp_index = (disp_index + 1) % 23;
                m_state_sub_module = 4;
            }
            else
                debug_disp_hex();
            break;
        default:
            break;
        }
        break;
    default:
        break;
    }
}

/*
*
*       Fun:    debug_disp_hex
*
*       Desc:   Set debug page show data as hex(page>0)
*
*       Ret:    REVOID
*       File:   debug.c
*
*/
void debug_disp_hex()
{
    WORD	r_no;
    WORD	i;

    switch(hex_state)
    {
    case 0:
        r_no = disp_index*20 + char_ptr;
        if (comp_disp[r_no] != disp_ptr[char_ptr])
        {
            comp_disp[r_no]	= disp_ptr[char_ptr];
            row_ptr	= disp_index + 3;
            col_ptr	= char_ptr;
            col_ptr = (col_ptr*3) + 20;
            hex_state = 1;
        }
        else
            char_ptr = char_ptr + 1;
        break;
    case 1:
        cursor_p[2] = ascii[row_ptr/10];
        cursor_p[3]	= ascii[row_ptr%10];
        cursor_p[5] = ascii[col_ptr/10];
        cursor_p[6]	= ascii[col_ptr%10];
        space_counter = 0;
        hex_state = 2;
        break;
    case 2:
        if (space_counter == 8)
            hex_state = 3;
        else
        {
            if (sendserbuf_t != (sendserbuf_h + 1))
            {
                debug_send_char(cursor_p[space_counter]);
                space_counter = space_counter + 1;
            }
        }
        break;
    case 3:
        i = disp_ptr[char_ptr];
        i = ascii[(i>>4)&0xf];	/* high bit */
        if (sendserbuf_t != (sendserbuf_h + 1))
        {
            debug_send_char(i);
            hex_state = 4;
        }
        break;
    case 4:
        i = disp_ptr[char_ptr];
        i = ascii[i&0xf];
        if (sendserbuf_t != (sendserbuf_h + 1))
        {
            debug_send_char(i);
            //				SendTelnetFlag = 1;
            char_ptr = char_ptr + 1;
            hex_state = 0;
        }
        break;
    default:
        break;
    }
}


void debug_set_para()
{
    DWORD	object_id[20]={1,3,6,1,4,1,1373,1,3,2,3,1,2,15,1};
    BYTE	data_ptr[TITLE_CHAR];
    int    i,j;
    BYTE   agent_buf_arr[16];

    /*	object_id[0] = 1;
    object_id[1] = 3;
    object_id[2] = 6;
    object_id[3] = 1;
    object_id[4] = 4;
    object_id[5] = 1;
    object_id[6] = 1373;
    object_id[7] = 1;
    object_id[8] = 3;
    object_id[9] = 2;
    object_id[10] = 3; 
    object_id[11] = 1;	//debug
    object_id[12] = 2;	//configuration
    object_id[13] = 15;	//debug module
    object_id[14] = 1;
    */
    data_ptr[0]	= 1;
    debug_set_response(13,object_id,data_ptr,1);//debug status

    object_id[14] = 2;
    strcpy((char *)data_ptr,DEBUG_VERSION);
    debug_set_response(13,object_id,data_ptr,strlen((char *)data_ptr));//debug name

    object_id[14] = 3;
    debug_set_response(13,object_id,debug_ascii_in_array,1);

    object_id[14] = 4;
    debug_set_response(13,object_id,debug_ascii_out_array,1);

    object_id[14] = 6;
    object_id[15] = 1;
    strcpy((char *)data_ptr,"\tDebug\tPage 01\t\tmodule status\r\
           1  2\r\
           M0 status\r\
           M1 status\r\
           M2 status\r\
           M3 status\r\
           M4 status\r\
           M5 status\r\
           M6 status\r\
           M7 status\r\
           M8 status\r\
           M9 status\r\
           M10 status\r\
           M11 status\r\
           M12 status\r\
           M13 status\r\
           M14 status\r\
           M15 status");
    debug_set_response(14,object_id,data_ptr,strlen((char *)data_ptr));//debug title

    for (i = 0;i < MODULE_COUNT;i ++)
    {
        object_id[14] = 6;
        object_id[15] = 2 + i;
        object_id[16] = 2;
        debug_set_response( 15 ,object_id, (BYTE *)debug_shm_ptr->module_d[i].status,1);//debug page 0 line 1
    }

    object_id[14] = 7;
    object_id[15] = 1;
    strcpy((char *)data_ptr,"\tDebug\tPage 02\t\tascii_in_flag\r\
           1  2  3\r\
           current module\r\
           M0 ascii in flag\r\
           M1 ascii in flag\r\
           M2 ascii in flag\r\
           M3 ascii in flag\r\
           M4 ascii in flag\r\
           M5 ascii in flag\r\
           M6 ascii in flag\r\
           M7 ascii in flag\r\
           M8 ascii in flag\r\
           M9 ascii in flag\r\
           M10 ascii in flag\r\
           M11 ascii in flag\r\
           M12 ascii in flag\r\
           M13 ascii in flag\r\
           M14 ascii in flag\r\
           M15 ascii in flag");
    debug_set_response(14,object_id,data_ptr,strlen((char *)data_ptr));

    for (i = 0;i <= MODULE_COUNT;i ++)
    {
        object_id[14] = 7;
        object_id[15] = i + 2;
        object_id[16] = 2;
        if (i==0)
            debug_set_response(15,object_id,&disp_sub_module_flag,1);
        else
            debug_set_response(15,object_id,&debug_shm_ptr->module_d[i-1].ascii_in_set_flag,1);
    }
    object_id[14] = 8;
    object_id[15] = 1;
    strcpy((char *)data_ptr,"\tDebug\tPage 03\t\tTest Page\r\
           1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20\r\
           line1");
    debug_set_response(14,object_id,data_ptr,strlen((char *)data_ptr));//debug page 1 line 1 title

    object_id[14] = 8;
    object_id[15] = 2;
    object_id[16] = 2;
    debug_set_response(15,object_id,test_array,20);//debug page 2 line 1 title
    /*
    object_id[14] = 9;
    object_id[15] = 1;
    strcpy(data_ptr,"\tDebug\tPage 04\t\tascii_out_flag\r
           1  2  3\r
           M0 ascii out flag\r
           M1 ascii out flag\r
           M2 ascii out flag\r
           M3 ascii out flag\r
           M4 ascii out flag\r
           M5 ascii out flag\r
           M6 ascii out flag\r
           M7 ascii out flag\r
           M8 ascii out flag\r
           M9 ascii out flag\r
           M10 ascii out flag\r
           M11 ascii out flag\r
           M12 ascii out flag\r
           M13 ascii out flag\r
           M14 ascii out flag\r
           M15 ascii out flag");
    debug_set_response(14,object_id,data_ptr,strlen(data_ptr));

    for (i = 0;i < MODULE_COUNT;i ++)
    {
        object_id[14] = 9;
        object_id[15] = i + 2;
        object_id[16] = 2;
        debug_set_response(15,object_id,&debug_shm_ptr->module_d[i].ascii_out_set_flag,1);
    }
    */
    object_id[14] = 9;
    object_id[15] = 1;
    strcpy((char *)data_ptr,"\tDebug\tPage 05\t\tdebug_snmp_agent_buf\r\
           1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16\r\
           oid  10        \t\r\
           oid  11        \t\r\
           oid  12        \t\r\
           oid  13        \t\r\
           oid  14        \t\r\
           oid  15        \t\r\
           oid  16        \t\r\
           oid  17        \t\r\
           oid  18        \t\r\
           oid_len				 \t\r\
           cmm_state      \t\r\
           result_state   \t\r\
           totoal_instance\t\r\
           totoal_field   \t\r\
           total_ip       ");
    /*
     para0 in_type  \t\r      
    para0selectid  \t\r        
    total_select   \t\r
    paran in_type  \t\r      
    paranselectid  \t\r        
    total_select"
    */
    debug_set_response(14,object_id,data_ptr,strlen((char *)data_ptr));
    object_id[14]=9;
    object_id[15]=2;
    object_id[16]=2;
    for(j=9;j<18;j++)
    {
        for(i=0;i<16;i++)
        {
            agent_buf_arr[i]=(BYTE)debug_snmp_agent_buf[i].oid[j];
        }
        debug_set_response(15,object_id,agent_buf_arr,16);
        object_id[15]++;
    }
    for(i=0;i<16;i++)
    {
        agent_buf_arr[i]=debug_snmp_agent_buf[i].oid_len;
    }
    debug_set_response(15,object_id,agent_buf_arr,16);
    object_id[15]++;
    for(i=0;i<16;i++)
    {
        agent_buf_arr[i]=debug_snmp_agent_buf[i].cmm_state;
    }
    debug_set_response(15,object_id,agent_buf_arr,16);
    object_id[15]++;
    for(i=0;i<16;i++)
    {
        agent_buf_arr[i]=debug_snmp_agent_buf[i].result_state;
    }
    debug_set_response(15,object_id,agent_buf_arr,16);
    object_id[15]++;
    for(i=0;i<16;i++)
    {
        agent_buf_arr[i]=debug_snmp_agent_buf[i].total_instance;
    }
    debug_set_response(15,object_id,agent_buf_arr,16);
    object_id[15]++;
    for(i=0;i<16;i++)
    {
        agent_buf_arr[i]=debug_snmp_agent_buf[i].total_field; 
    }
    debug_set_response(15,object_id,agent_buf_arr,16);
    object_id[15]++;
    for(i=0;i<16;i++)
    {
        agent_buf_arr[i]=debug_snmp_agent_buf[i].total_ip;
    }
    debug_set_response(15,object_id,agent_buf_arr,16);
    object_id[15]++;
    /*
    debug_set_response(15,object_id,&debug_snmp_agent_buf[i].total_field,1);
    object_id[15]++;
      debug_set_response(15,object_id,&debug_snmp_agent_buf[i].param_field_arr[0].input_type,1);
      object_id[15]++; 
      debug_set_response(15,object_id,&debug_snmp_agent_buf[i].param_field_arr[0].curr_select_id,1);
      object_id[15]++; 
      debug_set_response(15,object_id,&debug_snmp_agent_buf[i].param_field_arr[0].total_select ,1);
      object_id[15]++; 
        debug_set_response(15,object_id,&debug_snmp_agent_buf[i].param_field_arr[1].input_type,1);
      object_id[15]++; 
      debug_set_response(15,object_id,&debug_snmp_agent_buf[i].param_field_arr[1].curr_select_id,1);
      object_id[15]++; 
      debug_set_response(15,object_id,&debug_snmp_agent_buf[i].param_field_arr[1].total_select ,1);
      object_id[15]++;  
      debug_set_response(15,object_id,&debug_snmp_agent_buf[i].param_field_arr[debug_snmp_agent_buf[i].total_field-1].input_type,1);
      object_id[15]++; 
      debug_set_response(15,object_id,&debug_snmp_agent_buf[i].param_field_arr[debug_snmp_agent_buf[i].total_field-1].curr_select_id,1);
      object_id[15]++; 
      debug_set_response(15,object_id,&debug_snmp_agent_buf[i].param_field_arr[debug_snmp_agent_buf[i].total_field-1].total_select ,1);
      object_id[15]++; 
      */

}

/////////////////////////////End of Module Page///////////////////////////






///////////////////////////////Create OID Tree//////////////////////
/*
*
*       Fun:    MIB_create_tree
*
*       Desc:   Create a new MIB tree in memory using the name of configure file which lists mib files 
*
*       Ret:    1: Successful;		0: Fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
int MIB_create_tree(char *conf_dir,	char *conf_file)
{
    FILE 				*fp_conf, *fp_mib;
    char				*temp_ch;
    char				file_name[256],	temp_str[256];
    int                 i;
    BYTE                line_count;
    mib_type_num = 0;
    mib_node_index = 0;
    mib_child_node_index = 0;
    mib_root_node = NULL;
    mib_curr_node = NULL;
    memset((BYTE *)mib_nodes, 0, sizeof(mib_node) * MIB_MAX_NODES);
    memset((BYTE *)mib_child_nodes, 0, sizeof(mib_child_node) * MIB_MAX_CHILD_NODES);
    
    strcpy(file_name, 	"");
    strcpy(file_name,	MIB_FILES_DIR);
    strcat(file_name,	MIB_WRITE_TREE); 
    remove(file_name);
    
    
    for(i=0;i<MIB_MAX_NODES;i++)
    {
        mib_nodes[i].module_index=0xFF;
        mib_nodes[i].enable_field_id=0xFF;
        mib_nodes[i].total_instance=0;
        mib_nodes[i].total_child_nodes=0;
    }
    printf("\r\n");
    printf("Debug mib directory:  %s",conf_dir);
    strcpy(temp_str, 	"");
    strcpy(file_name, 	"");

    strcpy(file_name,	conf_dir);
    strcat(file_name,	conf_file);

    if((fp_conf = fopen(file_name,	"r")) == NULL)
    {
        printf("[debug] Error: cannot open conf_file: [%s].\r\n",file_name);
        return 0;
    }
    line_count=mib_line_count=0;
    printf("\r\n");
    while(!feof(fp_conf))
    {
        strcpy(file_name,	"");
        strcpy(file_name,	conf_dir);
        strcpy(temp_str,	"");
        line_count++;
        if( read_line(fp_conf,	temp_str) == 0)
            continue;
        if(temp_str[0]=='/' && temp_str[1]=='/')
        {
        	continue;
        }
        strcat(file_name,	temp_str);
        if((fp_mib = fopen(file_name,	"r")) == NULL)
        {
            printf("[debug] Error: Cannot open mib-file: [%s].line:%d\r\n",file_name,line_count);
            fclose(fp_conf);
            return 0;
        }
        strcpy(file_name,temp_str);
        mib_line_count=0;
        while(!feof(fp_mib))
        {
        	mib_line_count++;
            if(read_line(fp_mib,temp_str) == 0)
                continue;
            if((temp_ch = strstr(temp_str,"DEFINITIONS")))	// there must be these words in order: DEFINITIONS , ::= , BEGIN
            {	if((temp_ch = strstr(temp_ch,"::=")))
                {
                    if((temp_ch = strstr(temp_ch,"BEGIN")))
                        break;
                    else
                    {
                        printf("[debug] Error: There is not word [%s] after the word [%s]\r\n in the line [%s]\r\n in the file [%s].line:%d.\r\n", "BEGIN", "::=", temp_str, file_name,mib_line_count);
                        return 0;
                    }
                }
                else
                {
                    printf("[debug] Error: There is not word [%s] after the word [%s]\r\n in the line [%s]\r\n in the file [%s].line:%d.\r\n", "::=", "DEFINITIONS", temp_str, file_name,mib_line_count);
                    return 0;
                }
            }
        }

        if(feof(fp_mib))
        {
            printf("[debug] Error: There is not [BEGIN] in mib-file [%s].line:%d.\r\n",file_name,mib_line_count);
            fclose(fp_mib);
            fclose(fp_conf);
            return 0;
        }
        else
        {
            //printf("begin_found.\r\n");
            if(BEGIN_found(fp_mib) == 1)
            {
                //printf("file close. mib_node_index:%d.\r\n",mib_node_index);
                fclose(fp_mib);
            }
            else
            {
                printf("[debug] Error: Read mib-file [%s] fail.line:%d.\r\n",file_name,mib_line_count);
                fclose(fp_mib);
                fclose(fp_conf);
                return 0;
            }
        }
    printf("Open mib file [%-20s] to Create mib tree:   [",file_name);
    sprintf(temp_str," OK ");
    TxtColor(BRIGHT,YELLOW,BLACK,temp_str);
    printf("%s", temp_str);
    printf("]\r\n");
    }
    fclose(fp_conf);
    return 1;
}	/*	end of MIB_create_tree	*/


/*
*
*       Fun:    BEGIN_found
*
*       Desc:   Analyze the whole MIB file and collect data after the string "BEGIN" was found
*
*       Ret:    1: Successful;		0: Fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
int BEGIN_found(FILE *fp)
{
    char 		temp_str[256];
    char 		*temp_ch;
    int 		return_val, state, next_line_flag, index;
    mib_node 	*father_node_ptr;
    DWORD 		last_node_oid[32];


    

    for(index=0;index<256;index++)
        temp_str[index]='\0';
    return_val = 0;
    state = 0;
    next_line_flag = 1;
    father_node_ptr = NULL;

    while(!feof(fp))
    {
    	
        if(next_line_flag)
        {
        	mib_line_count++;
            //printf("begin_found read_line begin:. mib_node_index: %d\r\n",mib_node_index);
            if( read_line(fp, temp_str) == 0 )
                continue;
            //printf("begin_found read_line end:%s\r\n", temp_str);
        }
        if(temp_str[0]=='-' && temp_str[1]=='-')
          continue;
        //printf("begin_found state = [%d].\r\nread_line: %s.\r\n",state,temp_str);
        switch(state)
        {
        case 0:
            next_line_flag =0;
            if( (temp_ch = strstr(temp_str,"OBJECT-TYPE")) != NULL )
                state  = 4;
            else
            {
                if( (temp_ch = strstr(temp_str,"OBJECT-IDENTITY")) != NULL )
                    state  = 3;
                else
                {
                    if( (temp_ch = strstr(temp_str,"MODULE-IDENTITY")) != NULL )
                        state  = 2;
                    else
                    {
                        if( (temp_ch = strstr(temp_str,"IMPORTS")) != NULL )
                            state  = 1;
                        else
                        {
                            if( (temp_ch = strstr(temp_str,"END")) != NULL )
                                return 1;
                            else
                            {
                                next_line_flag = 1;
                                state = 0;
                            }
                        }
                    }
                }

            }
            break;
        case 1:		// except "IMPORTS"
            //temp_ch = strstr(temp_str,"IMPORTS");
            //if(temp_ch != NULL)
            //{
            if( (father_node_ptr = IMPORTS_found(fp)) != NULL)
            {
                //printf("imports_data.from_file [%s].\r\nimports_data.father_name [%s].\r\n",imports_data.from_file,imports_data.father_name);
                memset(last_node_oid, 0 , sizeof(DWORD) * 32);
                for(index=0;index<father_node_ptr->oid_len;index++)
                {
                    last_node_oid[index] = father_node_ptr->oid[index];
                }
                next_line_flag = 1;
                state = 0;
                break;
            }
            else
            {
                printf("[debug] There is error in IMPORTS part.line:%d.\r\n",mib_line_count);
                return 0;
            }
            //}
            //next_line_flag =0;
            //state=0;
            //break;
        case 2:		// except "MODULE-IDENTITY"
            //temp_ch = strstr(temp_str,"MODULE-IDENTITY");
            //if(temp_ch != NULL)
            //	{
            //*temp_ch = '\0';
            pick_name(temp_str, temp_str,temp_ch);		// take the first word as a the name of this module-identity
            if(strlen(temp_str) == 0)
            {
                printf("[debug] Error: There is no module name in the line [%s].line:%d.\r\n",temp_str,mib_line_count);
                return 0;
            }
            //printf("module name [%s].\r\nmodule_found:\r\n",temp_str);
            if(MODULE_found(temp_str, fp, father_node_ptr, last_node_oid) == 1)
            {
                next_line_flag = 1;
                state = 0;
                break;
            }
            else
            {
                printf("[debug] Error: There is error in MODULE-IDENTITY part.line:%d.\r\n",mib_line_count);
                return 0;
            }
            //	}
            //next_line_flag = 0;
            //state = 2;
            //break;
        case 3:		// except "OBJECT-IDENTITY"
            //temp_ch = strstr(temp_str,"OBJECT-IDENTITY");
            //if(temp_ch != NULL)
            //{
            //*temp_ch = '\0';
            pick_name(temp_str, temp_str,temp_ch);		// take the first word as a the name of this object-identity
            if(strlen(temp_str) == 0)
            {
                printf("[debug] Error: There is no object name in the line [%s].line:%d.\r\n",temp_str,mib_line_count);
                return 0;
            }
            if(OBJECT_found(temp_str, fp, father_node_ptr, last_node_oid) == 1)
            {
                next_line_flag = 1;
                state = 0;
                break;
            }
            else
            {
                printf("[debug] Error: There is error in OBJECT-IDENTITY part.line:%d.\r\n",mib_line_count);
                return 0;
            }
            //}
            //next_line_flag = 0;
            //state = 3;
            //break;
        case 4: 	// except "OBJECT-TYPE"
            //temp_ch = strstr(temp_str,"OBJECT-TYPE");
            //if(temp_ch != NULL)
            //{
            //*temp_ch = '\0';
            pick_name(temp_str, temp_str,temp_ch);	// take the first word as a the name of this object-type
            if(strlen(temp_str) == 0)
            {
                printf("[debug] Error: There is no object-type name in the line [%s].line:%d.\r\n",temp_str,mib_line_count);
                return 0;
            }

            //printf("type_found(temp_str: %s\r\n",temp_str);
            if(TYPE_found(temp_str, fp, father_node_ptr, last_node_oid) == 1)
            {
                next_line_flag = 1;
                state = 0;
                break;
            }
            else
            {
                printf("[debug] Error: There is error in OBJECT-TYPE part.line:%d.\r\n",mib_line_count);
                return 0;
            }
            //}
            //next_line_flag=0;
            //state=4;
            //break;
        default:
            next_line_flag=0;
            state=0;
            break;
        }
    }
    printf("[debug] Error: There is not word [END].line:%d.\r\n",mib_line_count);
    return 0;
}	/*	end of BEGIN_found	*/


/*
*
*       Fun:    OBJECT_found
*
*       Desc:   Analyze the OBJECT part and collect data after the string "OBJECT-IDENTITY" was found
*
*       Ret:    1: Successful;		0: Fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
int OBJECT_found(char *name, FILE *fp, mib_node *father_node_ptr, DWORD *last_node_oid)
{
    char 		temp_str[256];
    char		temp_child_str[256];
    char 		*temp_ch;
    DWORD       size;
    DWORD       i;
    BYTE        find_module=0;
    FILE                 *fp_conf;
    char                 file_name[128];  
    int			state,	next_line, 	found,	index;
    mib_node    *node_ptr;
    next_line=1;
    state=1;
    
    
    strcpy(file_name, 	"");
    strcpy(file_name,	MIB_FILES_DIR);
    strcat(file_name,	MIB_WRITE_TREE ); 
    for(index=0;index<256;index++)
    {
        temp_str[index] = '\0';
        temp_child_str[index] = '\0';
    }
    temp_ch = NULL;

    strcpy(object_data.name, "");
    object_data.ST = 0;
    strcpy(object_data.DE, "");
    strcpy(object_data.RF, "");
    strcpy(object_data.father_name, "");
    strcpy(object_data.ID, "");

    if(strlen(name) ==0)
    {
        printf("[debug] Error: Object name can not be NULL.line:%d.\r\n",mib_line_count);
        return 0;
    }
    strcpy(object_data.name,name);
    found=0;

    while(!feof(fp))
    {
        if(next_line)
        {
            mib_line_count++;
            if( read_line(fp,temp_str) == 0 )
                continue;
        }
        temp_ch = strchr(temp_str,'"');	// take the string between '"' and '"' into temp_child_str[]
        if(temp_ch != NULL)
        {
            int index = 0;
            while(temp_ch[index + 1] != '"')
            {
                temp_child_str[index] = temp_ch[index + 1];
                index++;
            }
            temp_child_str[index] = '\0';
        }
        else
            strcpy(temp_child_str, "");

        switch(state)
        {
        case 1:	//	must be "STATUS"
            temp_ch = strstr(temp_str, "STATUS");
            if(temp_ch != NULL)
            {
                if(strstr(temp_str, "current"))		// fill the object_data.ST accroding to the word behind "STATUS"
                {
                    object_data.ST = 1;
                }
                else
                {
                    if(strstr(temp_str, "deprecated"))
                    {
                        object_data.ST = 2;
                    }
                    else
                    {
                        if(strstr(temp_str, "obsolete"))
                        {
                            object_data.ST = 3;
                        }
                        else
                        {
                            object_data.ST = 0;
                            printf("[debug] Error: Fail to collect the contents of STATUS.line:%d.\r\n",mib_line_count);
                            return 0;
                        }
                    }
                }
                found = 1;
                next_line=1;
                state=2;
                break;
            }
            next_line = 1;
            state = 1;
            break;
        case 2:	//	must be "DESCRIPTION"
            temp_ch = strstr(temp_str,"DESCRIPTION");
            if(temp_ch != NULL)
            {
            	take_str(temp_child_str, fp, temp_str);
            	if((temp_ch=strstr(temp_str,"[SubSystem_Module]"))) 
            	{
            	   strcpy(object_data.DE, temp_ch+18);          	          	
                   
                   	find_module=1;
                    found = 2;
                    next_line=1;
                    state=3;
                    break;
                                  
                 }else if(strlen(temp_str))
                 {
                 	strcpy(object_data.DE,temp_str);
                 	found = 2;
                    next_line=1;
                    state=3;
                    break;
                 }
                 else
                 {
                    printf("[debug] Error: Fail to collect the contents of DESCRIPTION field.line:%d.\r\n",mib_line_count);
                    return 0;
                  }
            }
            next_line = 1;
            state = 2;
            break;
        case 3:	//	may be "REFERENCE"
            temp_ch = strstr(temp_str,"REFERENCE");
            if(temp_ch != NULL)
            {
                if( take_str(temp_child_str, fp, object_data.RF) ) 	// take the string between '"' and '"' after "REFERENCE " into object_data.RF[]
                {
                    next_line = 1;
                    found = 3;
                    state=4;
                    break;
                }
                else
                {
                    printf("[debug] Error: Fail to collect the contents of the REFERENCE field.line:%d.\r\n",mib_line_count);
                    return 0;
                }
            }
            next_line=0;
            state = 4;
            break;
        case 4:	//	must be "::="
            temp_ch = strstr(temp_str,"::=");
            if(temp_ch != NULL)
            {
                //printf("object_data.name: %s.\r\n",object_data.name);
                if((node_ptr=create_node(temp_str, object_data.name,	0, father_node_ptr, last_node_oid)) ) 	// create the object nod
                {
                    //show_node(father_node_ptr,"{ }--", 1);
                    //printf("\r\nmib_node_index: %d. node_name: %s.\r\n",mib_node_index, object_data.name);
                    if(find_module)
                    {                   	
                    	strcpy(subsys_module[gobal_module_index].name,object_data.name);
                    	subsys_module[gobal_module_index].index=gobal_module_index;
                    	node_ptr->module_index=gobal_module_index;
                    	for(index=6;index<node_ptr->oid_len;index++)
                    	   subsys_module[gobal_module_index].oid[index-6]=node_ptr->oid[index];
                    	gobal_module_index++;
                    }
                    else
                      node_ptr->module_index=gobal_module_index-1;
                    if((fp_conf = fopen(file_name,"a+")) == NULL)
										{
										        printf("[debug] Error: cannot open conf_file: [%s].\r\n",file_name);
										        return 0;
										} 
                    if(!strlen(object_data.DE))
                    {
                 		  node_ptr->remark_ptr=NULL;                 		   	
                    }
                    else
                    {                    	  
										    for(i=0;i<strlen(object_data.DE);i++)
										        if(object_data.DE[i]!=' ' && object_data.DE[i]!='\t' && object_data.DE[i]!='\n')
										            break;
										    if(i!=strlen(object_data.DE))
										    {
										       char    temp_str2[2048];
										       strcpy(temp_str2,object_data.DE);
										       for(i=strlen(temp_str2)-1;i>0;i--)
										          if(temp_str2[i]==' ' || temp_str2[i]=='\t' || temp_str2[i]=='\n')
										              temp_str2[i]='\0';
										          else
										             break;
										      strcpy(object_data.DE,temp_str2);
										    }
										    
	                    	size=strlen(object_data.DE);                 
		                		memcpy(heap_remark_curr,object_data.DE,sizeof(char)*(size+1));
		                		node_ptr->remark_ptr=heap_remark_curr;
		                		heap_remark_index+=size+1; 
		                		heap_remark_curr=heap_remark+heap_remark_index;                		           		  
                    }
                    fprintf(fp_conf,"Remark: %s\r\n",node_ptr->remark_ptr);
	                  fprintf(fp_conf,"\r\n\r\n");
                    fclose(fp_conf);
                    return 1;
                }
                else
                {
                    printf("[debug] Error: Fail to create the object nod: [%s].line:%d.\r\n",object_data.name,mib_line_count);
                    return 0;
                }
            }
        default:
            next_line = 1;
            state = 1;
            break;
        }
    }

    switch(found)
    {
    case 0:
        printf("[debug] Error: Can not find STATUS field.line:%d.\r\n",mib_line_count);
        break;
    case 1:
        printf("[debug] Error: Can not find DESCRIPTION field.line:%d.\r\n",mib_line_count);
        break;
    default:
        printf("[debug] Error: Can not find ::= field.line:%d.\r\n",mib_line_count);
        break;
    }
    return 0;
}	/*	end of OBJECT_found	*/





/*
*
*       Fun:    TYPE_found
*
*       Desc:   Analyze the TYPE part and collect data after the string "TYPE-IDENTITY" was found
*
*       Ret:    1: Successful;		0: Fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
int TYPE_found(char *name, FILE *fp, mib_node *father_node_ptr, DWORD *last_node_oid)
{
    char 		temp_str[256];
    char		temp_child_str[256];
    char 		*temp_ch;
    int			state,	next_line, 	found,	index;
    FILE                 *fp_conf;
    char                 file_name[128];  
    char access_str[32];
    mib_node	*node_ptr;
    mib_node    *leaf_node;
    mib_child_node *mib_child_node_point;
    mib_node_param *mib_node_param_point;
    mib_param_select *mib_param_select_point;
    WORD        i;
    BYTE        j;
    next_line=1;
    state=1;
    i=j=0;
    for(index=0;index<256;index++)
    {
        temp_str[index] = '\0';
        temp_child_str[index] = '\0';
    }
    temp_ch = NULL;

    strcpy(type_data.name, "");
    type_data.ST = 0;
    strcpy(type_data.UP, "");
    type_data.MA = 0;
    type_data.SY = 0;
    strcpy(type_data.DE, "");
    type_data.total_instance = 0;
    strcpy(type_data.RF, "");
    strcpy(type_data.father_name, "");
    strcpy(type_data.ID, "");

    if(strlen(name) ==0)
    {
        printf("[debug] Error: Type name can not be NULL.line:%d.\r\n",mib_line_count);
        return 0;
    }
    strcpy(type_data.name,name);

    found=0;

    while(!feof(fp))
    {
        if(next_line)  
        {
        	mib_line_count++;
            if( read_line(fp,temp_str) == 0 )
                continue;
            
         }
 
        temp_ch = strchr(temp_str,'"');	// take the string between '"' and '"' into temp_child_str[]
        if(temp_ch != NULL)
        {
            int index = 0;
            while(temp_ch[index + 1] != '"')
            {
                temp_child_str[index] = temp_ch[index + 1];
                index++;
            }
            temp_child_str[index] = '\0';
        }
        else
            strcpy(temp_child_str, "");
        //printf("t_f read_line: %s. temp_child_str: %s. state= %d\r\n",temp_str, temp_child_str, state);
        //printf("state=%d\r\n",state);
        switch(state)
        {
        case 1:	//	must be "SYNTAX"
            temp_ch = strstr(temp_str,"SYNTAX");
            if(temp_ch != NULL)
            {
                // fill the type_data.SY accroding to the word behind "SYNTAX"
                //	1: Integer32, 2: INTEGER, 3: OCTET STRING,
                //	4: OBJECT IDENTIFIER, 5: BITS, 6: IpAddress, 7: Counter32,
                //  8: Gauge32, 9: TimeTicks, 10: Opaque, 11: Counter64,
                //	12: Unsigned32, 13:Conceptual Tables
                if(strstr(temp_str, "Integer32"))
                {
                    type_data.ST = 1;
                }
                else
                {
                    if(strstr(temp_str, "INTEGER"))
                    {
                        type_data.SY = 2;
                    }
                    else
                    {
                        if(strstr(temp_str, "OCTET") && strstr(temp_str, " STRING"))
                        {
                            type_data.SY = 3;
                        }
                        else
                        {
                            if(strstr(temp_str, "OBJECT") && strstr(temp_str, " IDENTIFIER"))
                            {
                                type_data.SY = 4;
                            }
                            else
                            {
                                if(strstr(temp_str, "BITS"))
                                {
                                    type_data.SY = 5;
                                }
                                else
                                {
                                    if(strstr(temp_str, "IpAddress"))
                                    {
                                        type_data.SY = 6;
                                    }
                                    else
                                    {
                                        if(strstr(temp_str, "Counter32"))
                                        {
                                            type_data.SY = 7;
                                        }
                                        else
                                        {
                                            if(strstr(temp_str, "Gauge32"))
                                            {
                                                type_data.SY = 8;
                                            }
                                            else
                                            {
                                                if(strstr(temp_str, "TimeTicks") && strstr(temp_str, " STRING"))
                                                {
                                                    type_data.SY = 9;
                                                }
                                                else
                                                {
                                                    if(strstr(temp_str, "Opaque"))
                                                    {
                                                        type_data.SY = 10;
                                                    }
                                                    else
                                                    {
                                                        if(strstr(temp_str, "Counter64"))
                                                        {
                                                            type_data.SY = 11;
                                                        }
                                                        else
                                                        {
                                                            if(strstr(temp_str, "Unsigned32"))
                                                            {
                                                                type_data.SY = 12;
                                                            }
                                                            else
                                                            {
                                                                if(strstr(temp_str, "Conceptual") && strstr(temp_str, " Tables"))
                                                                {
                                                                    type_data.SY = 13;
                                                                }
                                                                else
                                                                {
                                                                    type_data.SY = 0;
                                                                    printf("[debug] Error: Fail to collect the contents of SYNTAX field.line:%d.\r\n",mib_line_count);
                                                                    return 0;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                found = 1;
                next_line=1;
                state=2;
                break;
            }
            next_line = 1;
            state = 1;
            break;
        case 2:	//	may be "UnitsPart"
            temp_ch = strstr(temp_str,"UnitsPart");
            if(temp_ch != NULL)
            {
                if( take_str(temp_child_str, fp, type_data.UP) ) 	// take the string between '"' and '"' after "UnitsPart " into type_data.UP[]
                {
                    next_line=1;
                    state=3;
                    break;
                }
                else
                {
                    printf("[debug] Error: Fail to collect the contents of UnitsPart field.line:%d.\r\n",mib_line_count);
                    return 0;
                }
            }
            next_line = 0;
            state = 3;
            break;
        case 3:	//	must be "MAX-ACCESS"
            temp_ch = strstr(temp_str,"MAX-ACCESS");
            if(temp_ch != NULL)
            {
                // fill the type_data.MA accroding to the word behind "MAX-ACCESS"
                //	0: invalid, 1: not-accessible, 2: accessible-for-notify, 3: read-only, 4: read-write, 5: read-create
                if(strstr(temp_str, "not-accessible"))
                {
                    type_data.MA = 1;
                }
                else
                {
                    if(strstr(temp_str, "accessible-for-notify"))
                    {
                        type_data.MA = 2;
                    }
                    else
                    {
                        if(strstr(temp_str, "read-only"))
                        {
                            type_data.MA = 3;
                        }
                        else
                        {
                            if(strstr(temp_str, "read-write"))
                            {
                                type_data.MA = 4;
                            }
                            else
                            {
                                if(strstr(temp_str, "read-create"))
                                {
                                    type_data.MA = 5;
                                }
                                else
                                {
                                    if(strstr(temp_str, "write-only"))
                                    {
                                        type_data.MA = 6;
                                    }
                                    else
                                    {
                                        type_data.MA = 0;
                                        printf("[debug] Error: Fail to collect the contents of MAX-ACCESS field.line:%d.\r\n",mib_line_count);
                                        return 0;
                                    }
                                }
                            }
                        }
                    }
                }
                found = 2;
                next_line=1;
                state = 4;
                break;
            }
            next_line = 1;
            state = 3;
            break;
        case 4:	//	must be "STATUS"
            temp_ch = strstr(temp_str, "STATUS");
            if(temp_ch != NULL)
            {
                if(strstr(temp_str, "current"))		// fill the type_data.ST accroding to the word behind "STATUS"
                {
                    type_data.ST = 1;
                }
                else
                {
                    if(strstr(temp_str, "deprecated"))
                    {
                        type_data.ST = 2;
                    }
                    else
                    {
                        if(strstr(temp_str, "obsolete"))
                        {
                            type_data.ST = 3;
                        }
                        else
                        {
                            type_data.ST = 0;
                            printf("[debug] Error: Fail to collect the contents of STATUS.line:%d.\r\n",mib_line_count);
                            return 0;
                        }
                    }
                }
                found = 3;
                next_line=1;
                state=5;
                break;
            }
            next_line = 1;
            state = 4;
            break;
        case 5:	//	must be "DESCRIPTION"
            temp_ch = strstr(temp_str,"DESCRIPTION");
            if(temp_ch != NULL)
            {
                //printf("description: beginline:%d.\r\n",mib_line_count);
                //if( take_str(temp_child_str, fp, type_data.DE) ) 	// take the string between '"' and '"' after "DESCRIPTION " into type_data.DE[]
                //{
                    /*int i=0;
                    //	take the first word of Description into type_data.total_instance
                    while(type_data.DE[i] != 0xa) 
                    {
                    temp_str[i] = type_data.DE[i];
                    i++;	
                    }
                    temp_str[i]='\0';
                    	type_data.total_instance = atoi(temp_str);*/
                    // type_data.total_instance = 256;
                    //printf("description end: %s\r\n",type_data.DE);
                    if(!(strchr(temp_str,'"')))
                    {
                    	mib_line_count++;
                        read_line(fp,temp_str);
                        if(!(strchr(temp_str,'"')))                        
				            {
				                  printf("[debug] Error: There is no first quotes .line:%d.\r\n",mib_line_count);
				                   return 0;
				            }
                    } 
                    memset(&temp_leaf_node,0,sizeof(mib_node));   
                    leaf_compute_field_num=0;                   
                    if(!Analyze_Leaf_Description(fp, &temp_leaf_node))
                    {
                    	printf("[debug] Error: Fail to collect the contents of DESCRIPTION field.line:%d.\r\n",mib_line_count);
				         return 0;
                    }                    
                    mib_line_count++;
                    read_line(fp,temp_str);
                    if(!(strchr(temp_str,'"')))                        
				    {
				       printf("[debug] Error: There is no second quotes.line:%d.\r\n",mib_line_count);
				       return 0;
				    }
                    found = 4;
                    next_line=1;
                    state=6;        
                    break;
            }
            next_line = 1;
            state = 5;
            break;
        case 6:	//	may be "REFERENCE"
            temp_ch = strstr(temp_str,"REFERENCE");
            if(temp_ch != NULL)
            {
                if( take_str(temp_child_str, fp, type_data.RF) ) 	// take the string between '"' and '"' after "REFERENCE" into type_data.RF
                {
                    next_line = 1;
                    state=7;
                    break;
                }
                else
                {
                    printf("[debug] Error: Fail to collect the contents of the REFERENCE field %d.line:%d.\r\n",module_data.RE_index,mib_line_count);
                    return 0;
                }
            }
            next_line=0;
            state = 7;
            break;
        case 7:	//	may be "INDEX" or "AUGMENTS"
            temp_ch = strstr(temp_str,"INDEX");
            if(temp_ch != NULL)
            {
                //	handle the line contenting "INDEX"
                next_line = 1;
                state = 8;
                break;
            }
            else
            {
                temp_ch = strstr(temp_str,"AUGMENTS");
                if(temp_ch != NULL)
                {
                    //	handle the line contenting "AUGMENTS"
                    next_line = 1;
                    state = 8;
                    break;
                }
                else
                {
                    next_line = 0;
                    state = 8;
                    break;
                }
            }
        case 8:	//	must be "::="
            temp_ch = strstr(temp_str,"::=");
            if(temp_ch != NULL)
            {
                //printf("type.::= begin: tmep_str: %s....type_data.name: %s\r\n",temp_str,type_data.name);
                if( (node_ptr = create_node(temp_str, type_data.name,	0,	father_node_ptr, 	last_node_oid)) ) 	// create the module node
                {           	
                    mib_type_num++;
                    node_ptr->max_access = type_data.MA;
                    leaf_node=&temp_leaf_node;
                    node_ptr->module_index=gobal_module_index-1;
                    node_ptr->data_length=leaf_node->data_length;
                    node_ptr->total_instance=leaf_node->total_instance;
                    node_ptr->total_child_nodes=leaf_node->total_child_nodes;
										node_ptr->enable_field_id=leaf_node->enable_field_id; 
										strncpy(node_ptr->init_value,leaf_node->init_value,leaf_node->data_length);
										node_ptr->remark_ptr=leaf_node->remark_ptr;
                    for(index=0;index<MIB_MAX_CHILD_NODES_PER_NODE;index++)
                    {                   	
                    	 node_ptr->child_nodes[index]=leaf_node->child_nodes[index];
                    }
                    if(node_save_flag==1)
                    {                    
                    	module_save_flag[node_ptr->module_index].module_index=node_ptr->module_index;                   	
                    	module_save_flag[node_ptr->module_index].save_select_id=node_save_module.save_select_id;
											module_save_flag[node_ptr->module_index].save_field_id=node_save_module.save_field_id;  
				              for(i=0;i<32;i++)
				                   module_save_flag[node_ptr->module_index].oid[i]=node_ptr->oid[i];
				              module_save_flag[node_ptr->module_index].oid_len=node_ptr->oid_len;
                    	node_save_flag=0;
                    	if(node_ptr->max_access<4) 
					    {           
					               printf("[Debug] : The save command node can not right!Please check the MIB files!\r\n");
					               exit(0);
					    }
                    }
                    
                    if(leaf_compute_field_num!=0)
                       for(i=0;i<leaf_compute_field_num;i++)
                           compute_field_arr[compute_field_num-i-1].compute_node=node_ptr;                                      
               	    
               	    strcpy(file_name, 	"");
				            strcpy(file_name,	MIB_FILES_DIR); 
				            strcat(file_name,	MIB_WRITE_TREE ); 
     
				         if((fp_conf = fopen(file_name,"a+")) == NULL)
				    {
				        printf("[debug] Error: cannot open conf_file: [%s].\r\n",file_name);
				        return 0;
				    } 
				    
				    fprintf(fp_conf,"Register leaf node: %s.\r\n",node_ptr->name);
				    fprintf(fp_conf,"Instance: %d\r\n",node_ptr->total_instance);
				    fprintf(fp_conf,"Length  : %d\r\n",node_ptr->data_length);
				    fprintf(fp_conf,"Flag    : %d\r\n",node_ptr->enable_field_id); 
				    fprintf(fp_conf,"Default : %s\r\n",node_ptr->init_value);
				    fprintf(fp_conf,"Remark  : %s\r\n",node_ptr->remark_ptr);
				    fprintf(fp_conf,"Field   : total(%d)\r\n",node_ptr->total_child_nodes);
				    
				    switch(node_ptr->max_access)
				    {
				    	case 1:
				    		  strcpy(access_str,"not-accessible");
				    		  break;
				        case 2:
				    		  strcpy(access_str,"accessible-for-notify");
				    		  break;
				        case 3:
				    		  strcpy(access_str,"read-only");
				    		  break;
				        case 4:
				    		  strcpy(access_str,"read-write");
				    		  break;
				        case 5:
				    		  strcpy(access_str,"read-create");
				    		  break;	 
						case 6:
				    		  strcpy(access_str,"write-only");
				    		  break;
                        default:
                        	  strcpy(access_str,"access type error");
				    		  break;
				    }
				    fprintf(fp_conf,"Access  : %s",access_str);
				    for(i=0;i<node_ptr->total_child_nodes;i++)
				    {
				    	mib_child_node_point=node_ptr->child_nodes[i];
				    	if(mib_child_node_point)
				    	   if(mib_child_node_point->child_ptr)  
				    	   {
				    	       mib_node_param_point=(mib_node_param* )mib_child_node_point->child_ptr;
				    	       if(mib_node_param_point->select_total==0) 
				    	       {
				    	          if(mib_node_param_point->input_ptr!=NULL)
				    	           fprintf(fp_conf,"[%d]%s\r\n		  {\r\n		    [u]%d.%d-%d.%d\r\n		    [u]input\r\n		    [v]%s\r\n		  }\r\n",mib_node_param_point->id+1,mib_node_param_point->name,mib_node_param_point->byte_begin,mib_node_param_point->bit_begin,mib_node_param_point->byte_end,mib_node_param_point->bit_end,field_input_type[mib_node_param_point->input_ptr->input_param_type]);
				    	       }
				    	       else 
				    	       {
				    	       	 fprintf(fp_conf,"          [%d]%s\r\n		  {\r\n		    [u]%d.%d-%d.%d\r\n		    [u]select\r\n",mib_node_param_point->id+1,mib_node_param_point->name,mib_node_param_point->byte_begin,mib_node_param_point->bit_begin,mib_node_param_point->byte_end,mib_node_param_point->bit_end);
				    	         for(j=0;j<mib_node_param_point->select_total;j++)
				    	         {
				    	         	mib_param_select_point=mib_node_param_point->select_ptr[j];
				    	         	if(mib_param_select_point)
				    	         	   fprintf(fp_conf,"		    [v]%d[opt]%s\r\n", mib_param_select_point->number,mib_param_select_point->description);
				    	         }
				    	         fprintf(fp_conf,"		  }\r\n");
				    	       }
				    	   }
				    } 
				    fprintf(fp_conf,"\r\n\r\n");
				    fclose(fp_conf);
                    return 1;
                }
                else
                {
                    printf("[debug] Error: Fail to create the type nod: [%s].line:%d.\r\n",type_data.name,mib_line_count);
                    return 0;
                }
            }
            next_line = 1;
            state = 8;
            break;
        default:
            next_line = 1;
            state = 0;
            break;
        }
    }
    switch(found)
    {
    case 0:
        printf("[debug] Error: Can not find SYNTAX field.line:%d.\r\n",mib_line_count);
        break;
    case 1:
        printf("[debug] Error: Can not find MAX-ACCESS field.line:%d.\r\n",mib_line_count);
        break;
    case 2:
        printf("[debug] Error: Can not find STATUS field.line:%d.\r\n",mib_line_count);
        break;
    case 3:
        printf("[debug] Error: Can not find DESCRIPTION field.line:%d.\r\n",mib_line_count);
        break;
    default:
        printf("[debug] Error: Can not find ::= field.line:%d.\r\n",mib_line_count);
        break;
    }
    return 0;
}	/*	end of TYPE_found	*/


/*
*
*       Fun:    IMPORTS_found
*
*       Desc:   Analyze the IMPORTS part and collect data after the string "IMPORTS" was found
*
*       Ret:    return the pointer to the father node if successful. If fail, return null pointer
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
mib_node *IMPORTS_found(FILE *fp)
{
    char 		temp_str[256];
    char 		*temp_ch;
    int			index, found[3] = {0,0,0};
    mib_node 	*father_node_ptr;

    for(index=0;index<256;index++)
        temp_str[index] = '\0';
    temp_ch = NULL;

    strcpy(imports_data.father_name, "");
    strcpy(imports_data.from_file, "");

    while(!feof(fp))
    {
    	mib_line_count++;
        if( read_line(fp,temp_str) == 0)
            continue;
        //printf("read_line: %s.\r\n",temp_str);
        temp_ch = strchr(temp_str,',');	// skip the line contents ','
        if(temp_ch == NULL)
        {
            temp_ch = strstr(temp_str, "FROM");	// except a line contents "FROM" and ';'
            if(temp_ch != NULL)
            {
                found[1] = 1;
                if(strchr(temp_str, ';') != NULL)	// if the line contents ';', collect the word between "FROM " and ';'
                {
                    index=0;
                    temp_ch = &temp_ch[5];
                    pick_first_word(temp_ch, imports_data.from_file);
                    temp_ch = strchr(imports_data.from_file, ';');
                    if (temp_ch != NULL)
                        *temp_ch = '\0';
                    found[2] = 1;
                    //printf("from_file: %s.\r\n",imports_data.from_file);
                    if( strlen(imports_data.father_name) ==0 )
                    {
                        printf("[debug] Error: Can't not find name of Father_nod in IMPORTS part.line:%d.\r\n",mib_line_count);
                        return NULL;
                    }
                    if(mib_node_index == 0)
                    {
                        father_node_ptr = create_mib_node(imports_data.father_name, root_oid_len, root_oid, 0);
                        mib_curr_node = mib_root_node = father_node_ptr;
                        //printf("the first nod [%s]. return\r\n", imports_data.father_name);
                        return father_node_ptr;
                    }
                    else
                        father_node_ptr = find_node(mib_root_node, imports_data.father_name, 0, NULL);
                    return father_node_ptr;
                }
            }
            else	// take the name of the father nod into imports_data.father_name[]
            {
                pick_first_word(temp_str, imports_data.father_name);
                //printf("father name: %s.\r\n",imports_data.father_name);
                found[0] = 1;
            }
        }
    }
    if(found[0]==0)
        printf("[debug] Error: Can't not find name of Father_nod in IMPORTS part.line:%d.\r\n",mib_line_count);
    else if(found[1] == 0)
        printf("[debug] Error: Can't not find name of Import_from_file in IMPORTS part.line:%d.\r\n",mib_line_count);
    else if(found[2] == 0)
        printf("[debug] Error: Can't not find end sign ';' in IMPORTS part.line:%d.\r\n",mib_line_count);
    return NULL;
}	// end of IMPORTS_found


/*
*
*       Fun:    MODULE_found
*
*       Desc:   Analyze the MODULE part and collect data after the string "MODULE-IDENTITY" was found
*
*       Ret:    1: Successful;		0: Fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
int MODULE_found(char *name, FILE *fp, mib_node *father_node_ptr, DWORD *last_node_oid)
{
    char 		temp_str[256];
    char		temp_child_str[256];
    char 		*temp_ch;
    int			state,	next_line, 	found,	index;
    WORD        size;
    mib_node    *node_ptr;
    if(strlen(name) ==0)
    {
        printf("[debug] Error: Module name can not be NULL.line:%d.\r\n",mib_line_count);
        return 0;
    }
    strcpy(module_data.name,name);
    found=0;

    next_line=1;
    state=1;
    strcpy(temp_str, "");
    strcpy(temp_child_str, "");
    temp_ch = NULL;

    strcpy(module_data.LU, "");
    strcpy(module_data.OR, "");
    strcpy(module_data.CI, "");
    strcpy(module_data.DE, "");
    for(index = 0; index < MIB_MODULE_REVISION_MAX_NUM; index++)
    {
        strcpy(module_data.RE[index], "");
        strcpy(module_data.RE_DE[index], "");
    }
    module_data.RE_index = 0;
    strcpy(module_data.father_name, "");
    strcpy(module_data.ID, "");

    while(!feof(fp))
    {
        if(next_line) 
        {
        	mib_line_count++;
            if( read_line(fp,temp_str) == 0 )
                continue;
        }
        temp_ch = strchr(temp_str,'"');	// take the string between '"' and '"' into temp_child_str[]
        if(temp_ch != NULL)
        {
            int index = 0;
            while(temp_ch[index + 1] != '"')
            {
                temp_child_str[index] = temp_ch[index + 1];
                index++;
            }
            temp_child_str[index] = '\0';
        }
        else
            strcpy(temp_child_str, "");

        //printf("module state [%d].\r\n", state);
        switch(state)
        {
        case 1:	//	must be "LAST-UPDATED"
            temp_ch = strstr(temp_str,"LAST-UPDATED");
            if(temp_ch != NULL)
            {
                if( take_str(temp_child_str, fp, module_data.LU) )	// take the string between '"' and '"' after "LAST-UPDATED " into module_data.LA[]
                {
                    //printf("module_data.LAST-UPDATED [%s].\r\n", module_data.LU);
                    found = 1;
                    next_line=1;
                    state=2;
                    break;
                }
                else
                {
                    printf("[debug] Error: Fail to collect the contents of LAST-UPDATED field.line:%d.\r\n",mib_line_count);
                    return 0;
                }
            }
            next_line = 1;
            state = 1;
            break;
        case 2:	//	must be "ORGANIZATION"
            temp_ch = strstr(temp_str,"ORGANIZATION");
            if(temp_ch != NULL)
            {
                if( take_str(temp_child_str, fp, module_data.OR) ) 	// take the string between '"' and '"' after "ORGANIZATION " into module_data.OR[]
                {
                    //printf("module_data.ORGANIZATION [%s].\r\n", module_data.OR);
                    found= 2;
                    next_line=1;
                    state=3;
                    break;
                }
                else
                {
                    printf("[debug] Error: Fail to collect the contents of ORGANIZATION field.line:%d.\r\n",mib_line_count);
                    return 0;
                }
            }
            next_line = 1;
            state = 2;
            break;
        case 3:	//	must be "CONTACT-INFO"
            temp_ch = strstr(temp_str,"CONTACT-INFO");
            if(temp_ch != NULL)
            {
                if( take_str(temp_child_str, fp, module_data.CI) ) 	// take the string between '"' and '"' after "ORGANIZATION " into module_data.CI[]
                {
                    //printf("module_data.CONTACT-INFO [%s].\r\n", module_data.CI);
                    found = 3;
                    next_line=1;
                    state=4;
                    break;
                }
                else
                {
                    printf("[debug] Error: Fail to collect the contents of CONTACT-INFO field.line:%d.\r\n",mib_line_count);
                    return 0;
                }
            }
            next_line = 1;
            state = 3;
            break;
        case 4:	//	must be "DESCRIPTION"
            temp_ch = strstr(temp_str,"DESCRIPTION");
            if(temp_ch != NULL)
            {
                if( take_str(temp_child_str, fp, module_data.DE) ) 	// take the string between '"' and '"' after "DESCRIPTION " into module_data.DE[]
                {
                    //printf("module_data.DESCRIPTION [%s].\r\n", module_data.DE);
                    found = 4;
                    next_line=1;
                    state=5;
                    break;
                }
                else
                {
                    printf("[debug] Error: Fail to collect the contents of DESCRIPTION field.line:%d.\r\n",mib_line_count);
                    return 0;
                }
            }
            next_line = 1;
            state = 4;
            break;
        case 5:	//	may be "REVISION"
            temp_ch = strstr(temp_str,"REVISION");
            if(temp_ch != NULL)
            {
                if( take_str(temp_child_str, fp, module_data.RE[module_data.RE_index]) ) 	// take the string between '"' and '"' after "REVISION " into module_data.RE[RE_index]
                {
                    //printf("module_data.REVISION(index) [%s](%d).\r\n", module_data.RE[module_data.RE_index], module_data.RE_index);
                    next_line = 1;
                    found = 5;
                    state=6;
                    break;
                }
                else
                {
                    printf("[debug] Error: Fail to collect the contents of the REVISION field %d.line:%d.\r\n",module_data.RE_index,mib_line_count);
                    return 0;
                }
            }
            next_line=0;
            state = 7;
            break;
        case 6:	//	must be "DESCRIPTION" which belong to the nearest "REVISION"
            temp_ch = strstr(temp_str,"DESCRIPTION");
            if(temp_ch != NULL)
            {
                if( take_str(temp_child_str, fp, module_data.RE_DE[module_data.RE_index]) ) 	// take the string between '"' and '"' after "DESCRIPTION " into module_data.RE_DE[RE_index]
                {
                    //printf("module_data.REVISION.DESCRIPTION(index) [%s](%d).\r\n", module_data.RE_DE[module_data.RE_index], module_data.RE_index);
                    module_data.RE_index = (module_data.RE_index + 1) % MIB_MODULE_REVISION_MAX_NUM;
                    next_line = 1;
                    found = 6;
                    state=5;
                    break;
                }
                else
                {
                    printf("[debug] Error: Fail to collect the contents of DESCRIPTION field of the REVISION field: %d.line:%d.\r\n",module_data.RE_index,mib_line_count);
                    return 0;
                }
            }
            next_line = 1;
            state = 6;
            break;
        case 7:	//	must be "::="
            temp_ch = strstr(temp_str,"::=");
            if(temp_ch != NULL)
            {
                //printf("entering create_node().line:%d.\r\n",mib_line_count);
                if( (node_ptr=create_node(temp_str, module_data.name,	0, father_node_ptr, last_node_oid))) 	// create the module nod
                {
                	if(!strlen(module_data.DE))
                    {
                 		node_ptr->remark_ptr=NULL;
                 		return 1;   	
                    }
                    else
                    {                  
	                    	size=strlen(module_data.DE);                 	                			        				                  
				            memcpy(heap_remark_curr,module_data.DE,sizeof(char)*(size+1)); 
				            node_ptr->remark_ptr=heap_remark_curr;
	                		heap_remark_index+=size+1; 
	                		heap_remark_curr=heap_remark+heap_remark_index;
                    }
                    return 1;
                }
                else
                {
                    printf("[debug] Error: Fail to create the module nod: [%s].line:%d.\r\n",module_data.name,mib_line_count);
                    return 0;
                }
            }
        default:
            next_line = 1;
            state = 0;
            break;
        }
    }
    switch(found)
    {
    case 0:
        printf("[debug] Error: Can not find LAST-UPDATED field.line:%d.\r\n",mib_line_count);
        break;
    case 1:
        printf("[debug] Error: Can not find ORGANIZATION field.line:%d.\r\n",mib_line_count);
        break;
    case 2:
        printf("[debug] Error: Can not find CONTACT-INFO field.line:%d.\r\n",mib_line_count);
        break;
    case 3:
        printf("[debug] Error: Can not find DESCRIPTION field.line:%d.\r\n",mib_line_count);
        break;
    case 5:
        printf("[debug] Error: Can not find the DESCRIPTION of REVISION field.line:%d.\r\n",mib_line_count);
        break;
    default:
        printf("[debug] Error: Can not find ::= field.line:%d.\r\n",mib_line_count);
        break;
    }
    return 0;
}	/*	end of MODULE_found	*/


/*
*
*       Fun:    Analyze_Description
*
*       Desc:   Analyze leaf node Description script in the mib script
*
*       Ret:    0 err 1 successfull
*
*       File:   debug.c
*
*/
int Analyze_Leaf_Description(FILE *fp, mib_node *leaf_node)
{
     BYTE unit_start_flag,
          remark_start_flag,
          select_start_flag,
          field_start_flag,        
          new_line=0,
          leaf_state,
          field_state,
          i, 
          j;
     char instance_str[5],
          default_str[256],
          length_str[4],
          data_arr[512],
          temp_str[128],
          *temp_ch, 
          *p;
     WORD total_instance,
          pos_arr[4],
          length,
          size;
     mib_child_node *mib_child_node_ptr = NULL;
     mib_param_select *mib_param_select_ptr = NULL;
     mib_param_input *mib_param_input_ptr = NULL;
     mib_node_param *mib_node_param_ptr = NULL;
                  
     unit_start_flag=remark_start_flag=select_start_flag=field_start_flag=0; 
     leaf_node->enable_field_id=0xFF;
     leaf_state=0;
     field_state=0;
     while(!feof(fp))
     {
     	  if(leaf_state==10)
     	  {
             printf("[Debug] Error: Script error.The field segment error.Line: %d.\r\n", mib_line_count);
             return 0; 
          }
          if((leaf_state<=5  && field_start_flag==0) || ((remark_start_flag==1)&& leaf_state>=8)) 
          {
          	  mib_line_count++; 
          	  memset(data_arr,0,512*sizeof(char));
	          if( read_line(fp,data_arr) == 0 ) 
	              continue;          
	                  
          }
          new_line=0;
          switch (leaf_state)
          {
          case 0:
               temp_ch = strstr(data_arr, "[Begin]");
               if(temp_ch != NULL)
                    leaf_state = 1;
               else
                    leaf_state = 10; 
               break;
          case 1:
               temp_ch = strstr(data_arr, "Instance");
               if(temp_ch != NULL)
               {          
               	    temp_ch+=8;
               	    for(i=0;i<strlen(temp_ch);i++)
               	    {
               	    	if(temp_ch[i]!=' ' && temp_ch[i]!=':')
               	    	    break;
               	    }         
               	    if(i==strlen(temp_ch))
               	    {
               	    	leaf_state=10;
               	    	break;
               	    }       
               	    temp_ch+=i;
               	    del_space(temp_ch,2);
                    if((test_str(temp_ch,0)))
                    { 
                         strncpy(instance_str, temp_ch, 4);
                         instance_str[4]='\0';
                         total_instance = atoi(instance_str);
                         if(total_instance == 0 || total_instance > MAX_TOTAL_INSTANCE)                              
                         {
                              printf
                                   ("[Debug] Error: script error.The max instance number is %d.line:%d.\r\n",
                                   MAX_TOTAL_INSTANCE, mib_line_count);
                              return 0;
                         }
                         leaf_node->total_instance = total_instance;
                         leaf_state = 2;
                         break;
                    }                  
               }
               leaf_state = 10;
               break;
          case 2:
               temp_ch = strstr(data_arr, "Length");
               if(temp_ch != NULL)
               {           
               	    temp_ch+=6;
               	    for(i=0;i<strlen(temp_ch);i++)
               	    {
               	    	if(temp_ch[i]!=' ' && temp_ch[i]!=':')
               	    	    break;
               	    }         
               	    if(i==strlen(temp_ch))
               	    {
               	    	leaf_state=10;
               	    	break;
               	    }       
               	    temp_ch+=i;  
               	    del_space(temp_ch,2);  
                    if((test_str(temp_ch,0)))
                    {
                         strncpy(length_str, temp_ch, 4);
                         length = atoi(length_str);
                         if(length == 0 || length > MAX_NODE_PACKET_LENGTH) 
                         {
                              printf
                                   ("[Debug] Error: script error.The max data length is %d. line:%d.\r\n",
                                   MAX_NODE_PACKET_LENGTH,
                                   mib_line_count);
                              return 0;
                         }
                         leaf_node->data_length = length;
                         leaf_state = 3;
                         break;
                    }
               }
               leaf_state = 10;
               break;
          case 3:
               temp_ch = strstr(data_arr, "Default");
               if(temp_ch != NULL)
               {
               	    temp_ch+=7;
               	    for(i=0;i<strlen(temp_ch);i++)
               	    {
               	    	if(temp_ch[i]!=' ' && temp_ch[i]!=':')
               	    	    break;
               	    }         
               	    if(i==strlen(temp_ch))
               	    {
               	    	leaf_state=10;
               	    	break;
               	    }       
               	    temp_ch+=i;
               	    memset(default_str,0,sizeof(char)*256);
                    strcpy(default_str, temp_ch);  
                    del_space(default_str,2);                 
                    if(strlen(default_str) > 256 && (test_str(default_str,1)))
                    {
                         printf("[Debug] Error: script error.The max default value length is 256.Line: %d.\r\n", mib_line_count);
                         return 0;
                    }
                    memset(leaf_node->init_value,0,256*sizeof(char));
                    AsciiToRbcd ((BYTE *)leaf_node->init_value, default_str, strlen(default_str));
                    leaf_node->init_value[leaf_node->data_length]='\0';
                    leaf_state = 5;
                    break;
               }
               leaf_state=10;
               break;
          case 5:
          	   temp_ch = strstr(data_arr, "Flag");
               if(temp_ch != NULL)
               {
               	 temp_ch+=4;
               	 if((temp_ch=strstr(data_arr, "E")) ) 
               	 {
               	    leaf_state=6;
               	    break;
               	 }
               	 else if((temp_ch=strstr(data_arr, "S"))) 
               	 {
               	    leaf_state=7;
               	    break;
               	 }
               	 else leaf_state=10;
               } 
               field_start_flag=1;  
               leaf_state=4;
               break;
          case 6:
          	   temp_ch++;         
          	   if(*temp_ch=='\0')
          	   {
                  printf("[Debug] Error: script error.The enable Flag format is not right.Line: %d.\r\n", mib_line_count);
                  return 0;
               }
               strcpy(temp_str,temp_ch);
               leaf_node->enable_field_id=atoi(temp_ch)-1;
               leaf_state=4;
          	   break;
          case 7:
          	   temp_ch++;   
          	   i=0;      
          	   while(*(temp_ch+i)!='\0')
          	   {          	   	 
          	   	 if(*(temp_ch+i)=='.')
          	   	   break;
          	   	 if(!isdigit(*(temp_ch+i)))
          	   	 {
                  printf("[Debug] Error: script error.The save Flag format is not right.Line: %d.\r\n", mib_line_count);
                  return 0;
                 }
          	   	 i++;
          	   } 
          	   if(*(temp_ch+i)=='\0')
          	   {
                  printf("[Debug] Error: script error.The save Flag format is not right.Line: %d.\r\n", mib_line_count);
                  return 0;
               }
               node_save_flag=1;
               strncpy(temp_str,temp_ch,i);
               temp_str[i]='\0'; 
               temp_ch=temp_ch+i+1;                     
               node_save_module.save_field_id=atoi(temp_str);
               strcpy(temp_str,temp_ch);
               if(!(test_str(temp_str,0)) ) 
               {
                  printf("[Debug] Error: script error.The save Flag format is not right.Line: %d.\r\n", mib_line_count);
                  return 0;
               }  
               node_save_module.save_select_id=atoi(temp_str);            
          	   leaf_state=4;
          	   break;
          case 4:
          	   field_start_flag=0;         	   
               if(unit_start_flag==0)
               {
                    temp_ch = strstr(data_arr, "Field");
                    if(temp_ch != NULL)
                    {              	    
	                    temp_ch = strstr(data_arr, "[");
	                    if(temp_ch != NULL)
	                    {
	                         field_state = 1;  	                         
	                    }
	                    else
	                    {
	                         field_state=0;     
	                         temp_ch=data_arr;	             
	                    }         	                         
	                }
	                 else
                    {
	                    leaf_state = 10;
	                    break;
                    }	  
                    unit_start_flag=1;              
               }
               else if(field_state!=1 && (temp_ch=strstr(data_arr,"Remark"))!=NULL)
               {
               	 printf("[Debug] Error: The field is not over! line:%d.\r\n",mib_line_count);
               	 return 0;
               }
               else
                  temp_ch=data_arr;
               p=temp_ch;              
               while(*p != '\0')
               {
               	    if(field_state==13)
               	    {
                         printf("[Debug] Error: Script error.The field segment error.Line: %d.\r\n", mib_line_count);
                         return 0; 
                    }
                    if((strstr(p,"Remark")!=NULL) && (strstr(p,":"))!=NULL)
                    {
                        leaf_state=8;
                        break;
                    }
               	    if(new_line==1)
               	       break;               	                                           
                    switch (field_state)
                    {
                    case 0:    //idle                  	
                         field_state = 1; 
                         new_line=1;
                         break;
                    case 1:    //meet the "["  
                    	 if(select_start_flag==1)
                    	 {
                    	    temp_ch=strstr(p,"}");
                    	    if(temp_ch != NULL)
                    	    {
                    	       field_state=7;
                    	       break;
                    	    }
                    	 }
                         temp_ch = strstr(p, "[");
                         if(temp_ch != NULL)
                         {
                              p = temp_ch + 1;
                              if(isdigit(*p))
                              {
                                   field_state = 2;
                                   break;
                              }
                              else if(*p == 'i' || *p == 'I')
                              {
                                   field_state = 3;
                                   break;
                              }
                              else if(*p == 'u' || *p == 'U')
                              {
                                   field_state = 4;
                                   break;
                              }
                              else if(*p == 'v' || *p == 'V')
                              {
                                   field_state = 5;
                                   break;
                              }                                                            
                         }
                         field_state = 13;
                         break;
                    case 2:    //meet the "[num"
                    	// p--; 
                         i = strcspn(p, "]");
                         if(i != 0)
                         {
                              strncpy(temp_str, p, i);
                              temp_str[i]='\0';
                              del_space(temp_str,2);
                              if((test_str(temp_str,0)) && strlen(temp_str)<4)
                              { 
                              	  p=p+i+1; 
                                  if(*p=='\0')
						          {
						                printf("[debug] : Script Error. There is no field name.Line: %d.\r\n", mib_line_count);
						                return 0;
						          }
					             if (mib_node_param_index >= MIB_MAX_NODE_PARAMS)
					              {
					                printf("[debug] : Script Error. There is no field have been alloced in the memery.Line: %d.\r\n", mib_line_count);
					                return 0;
					              }
					              mib_node_param_ptr = &mib_node_param_array[mib_node_param_index++];
					              strcpy( mib_node_param_ptr->name, p);
					              if(atoi(temp_str)>0)
					                 mib_node_param_ptr->id =(BYTE)(atoi(temp_str)-1); 
					              else
					              {
					                printf("[Debug] : The field id start from 1!line:%d.\r\n",mib_line_count);
					                return 0;
					              } 
					              //leaf_node->total_child_nodes++;
					              mib_node_param_ptr->parent_ptr=leaf_node;
					               if (!(mib_child_node_ptr = create_mib_child_node(mib_node_param_ptr->name, leaf_node->total_child_nodes)))
						            {
						                printf("[debug] Error: Faile to Create child nod.id:%s,name:%s.line:%d\r\n", temp_str,p,mib_line_count);
						                return 0;
						            }
						            if (!register_child_node(leaf_node, mib_child_node_ptr))
						            {  
						                printf("[debug] Error: Fail to register the child nod id:%s,name:%s..line:%d.\r\n",temp_str,p,mib_line_count);
						                return 0;
						            }
            						mib_child_node_ptr->child_ptr = mib_node_param_ptr;

					              field_state=6;
					              new_line=1;
					              break;
                              }                             
                         }
                         field_state = 13;                        
                         break;
                    case 3:    //meet the "[I"
                         p++;
                         i = strcspn(p, "]");
                         if(i != 0)
                         { 
                              strncpy(temp_str, p, i);  
                              temp_str[i]='\0';  
                              p+=i+1;                                                       
                              i = 0;
                              while(temp_str[i] != '\0')
                              {
                                   if(!isdigit(temp_str[i]))
                                        break;
                                   i++;
                              }
                              if(i == strlen(temp_str) && atoi(temp_str)<256)
                              {
                                   j=atoi(temp_str)-1;    
                                   
                              }    
                              else
                              {
				                					printf("[debug] Error: The field id is not right.line:%d.\r\n",mib_line_count);
				                					return 0;
				            					}     
				            					if(*p!='\0')
				            					   strcpy(temp_str,p);
				            					else
                              {
				                					printf("[debug] Error: There field name is not right.line:%d.\r\n",mib_line_count);
				                					return 0;
				            					} 
				            					if(create_compute_field(fp,leaf_node,j,temp_str)==0)
				            					{  
				            						 field_state=13;
				            					   break;
				            					}
				            					field_state=1;
				            					leaf_compute_field_num++;   
				            					new_line=1; 
				            					break;				            					                  
                         }                         
                         field_state = 13;
                         break;
                    case 4:    //meet the "[u"
                    	 temp_ch=strstr(data_arr, "[u]select");
                         if(temp_ch!=NULL)
                         {
                              field_state = 8;
                              break;
                         }
                         temp_ch=strstr(data_arr, "[u]input");
                         if(temp_ch!=NULL)
                         {
                              field_state = 9;
                              break;
                         }
                         temp_ch=strstr(data_arr,"]"); 
                         if(temp_ch!=NULL)
                             if(isdigit(temp_ch[1]))
                             {
                             	p=temp_ch;
                                field_state=10;
                                break;
                             }
                         field_state=13;
                         break;
                    case 5:    //meet the "[v"
                    	  if(strncasecmp(mib_node_param_ptr->name,"Reserved",strlen("Reserved"))==0 && strcmp(p,"{"))
                    	  {
                    	  	 field_state=7;
                    	  	 break;
                    	  } 
                    	  temp_ch=strstr(p,"]");
                    	  if(temp_ch!=NULL)
                    	  {
                    	  	if(isdigit(*(temp_ch+1)))
                    	  	   field_state=12;
                    	  	else
                    	  	   field_state=11;
                    	  	break;
                    	  }
                    	  field_state=13;
                    	  break;
                    case 6:    //meet the "{"
                    	   temp_ch=strstr(p,"{");
                    	   field_state=1;
                           new_line=1;
                           break;
                    case 7:    //meet the "}"
                    	   temp_ch=strstr(p,"}");
                    	   field_state=1;
                           new_line=1;
                           break;
                    case 8:    //meet the "[u]select"
                    	   field_state=5;
                    	   new_line=1;
                    	   break;
                    case 9:    //meet the "[u]input"
                    	   field_state=5;
                    	   new_line=1;
                    	   break;
                    case 10:   //meet the "[u]00.0-10.0"
                           p++;        	   
                    	   for(i=0,j=0;i<4;i++)
                    	   {
	                    	   while(*p!='\0') 
	                    	   {
	                    	   	 if(*(p+j)=='.' || *(p+j)=='-')
	                    	   	   break;
	                    	   	 j++;
	                    	   }
	                    	   strncpy(temp_str,p,j);
	                    	   temp_str[j]='\0';
	                    	   pos_arr[i]=atoi(temp_str);
	                    	   p+=j+1;
	                    	   j=0;
                    	   }
                    	   
                    	   mib_node_param_ptr->byte_begin=pos_arr[0];
                    	   mib_node_param_ptr->bit_begin=pos_arr[1];
                    	   mib_node_param_ptr->byte_end=pos_arr[2];                    	   
                    	   mib_node_param_ptr->bit_end=pos_arr[3];
                    	   field_state=1;
                    	   new_line=1;
                    	   break;                    	   
                    case 11:   //meet the "[v]keepHex-low-E"
                    	    if (mib_param_input_index >= MIB_MAX_PARAM_INPUTS)
				            {
				                printf("[debug] Error: There are not enough mib_param_inputs in the array.line:%d.\r\n",mib_line_count);
				                return 0;
				            }
				            mib_param_input_ptr = &mib_param_input_array[mib_param_input_index++];
				            mib_node_param_ptr->input_ptr = mib_param_input_ptr;
                    	    strcpy(temp_str,data_arr);
                    	    if(strstr(temp_str, "toDec-high-0"))
				            {
				                mib_param_input_ptr->input_param_type = TODEC_HIGH_0;
				            }
				            else
				            {
				                if(strstr(temp_str, "keepHex-high-0"))
				                    mib_param_input_ptr->input_param_type = KEEPHEX_HIGH_0;
				                else
				                {
				                    if(strstr(temp_str, "keepHex-low-0"))
				                        mib_param_input_ptr->input_param_type = KEEPHEX_LOW_0;
				                    else
				                    {
				                        if(strstr(temp_str, "keepHex-low-E"))
				                            mib_param_input_ptr->input_param_type = KEEPHEX_LOW_E;
				                        else
				                        {
				                            if(strstr(temp_str, "keepHex-low-F"))
				                                mib_param_input_ptr->input_param_type = KEEPHEX_LOW_F;
				                            else
				                            {
				                                if(strstr(temp_str, "toAscii-low-0"))
				                                    mib_param_input_ptr->input_param_type = TOASCII_LOW_0;
				                                else
				                                {
				                                    if(strstr(temp_str, "keepAsc-low-F"))
				                                        mib_param_input_ptr->input_param_type = KEEPASC_LOW_F;
				                                    else
				                                    {
				                                        printf("[debug] Error: There is not input param like %s.line:%d.\r\n",temp_str,mib_line_count);
				                                        return 0;
				                                    }
				                                }
				                            }
				                        }
				                    }
				                }
				            }
				            field_state=7;
				            new_line=1;
				            break;
                    case 12:   //meet the "[v]1[opt]Spare"
                    	    mib_param_select_ptr = &mib_param_select_array[mib_param_select_index++];
            				mib_node_param_ptr->select_ptr[mib_node_param_ptr->select_total++] = mib_param_select_ptr;            				
                    	    temp_ch=strtok(data_arr,"[]");  //'V'     
                    	    temp_ch=strtok(NULL,"[]"); //'1'               	    
                    	    for(i=0;i<strlen(temp_ch);i++)
                    	    {
                    	    	if(!isdigit(temp_ch[i]))
                    	    	   break; 
                    	    }
                    	    if(i==strlen(temp_ch))
                    	    {
                    	       mib_param_select_ptr->number = (BYTE)atoi(temp_ch);
                    	    }
                    	    else
                    	    {
                    	       field_state=13;
                    	       break;
                    	    }
                    	    temp_ch=strtok(NULL,"[]"); //'opt' 
                    	    temp_ch=strtok(NULL,"[]"); //'Spare' 
                    	    if(temp_ch!=NULL)
                    	       strcpy(mib_param_select_ptr->description, temp_ch);
                    	    else
                    	       printf("[Debug] : There is no select option .line:%d.\r\n",mib_line_count);
                    	    select_start_flag=1;
                    	    field_state=1;
                    	    new_line=1;
                    	    break; 
                    }
                    //if(*p!='\0')
                    //  p++; 
               }
               break;
          case 8: 
          	   i=0;
          	   if(remark_start_flag==0)
          	   {
                  temp_ch = strstr(data_arr, "Remark");  
                                
                  if(temp_ch != NULL)
                  {
                  	 temp_ch+=6;  
               	     for(i=0;i<strlen(temp_ch);i++)
               	     {
               	     	if(temp_ch[i]!=' ' && temp_ch[i]!=':' && temp_ch[i]!='\t' && temp_ch[i]!='\n')
               	     	   break; 
               	     }
               	     if(i==strlen(temp_ch))
						         {
						             leaf_node->remark_ptr=NULL;
						             leaf_state=1;
						             break;
						         }
			               else
			               strcpy(temp_str,temp_ch+i); 
                 }
                  else
                      leaf_state=10;
               }
                else
               {
               	    if(strstr(data_arr,"[End]")==NULL)
                       strcpy(temp_str,data_arr);
                    else 
                      {
                      	remark_start_flag=0;
                        leaf_state=9;
                        break;
                      }
               }
               strcat(temp_str,"\n");
               /*
               if(strlen(temp_str)>0)
               {
	               for(i=strlen(temp_str)-1;i>0;i--)
	               {
	                   if(temp_str[i]==' ' ||  temp_str[i]=='\n')
	                       temp_str[i]='\0';
	                   else
	                       break;
	               }
               }
               */
				       size=strlen(temp_str);         
				       if(remark_start_flag==1)
				          strcat(leaf_node->remark_ptr,temp_str);
				       else				                  
				          memcpy(heap_remark_curr,temp_str,sizeof(char)*(size+1));
				       if(remark_start_flag==0)  
				           leaf_node->remark_ptr=heap_remark_curr;
				       heap_remark_index+=size+1; 
				       heap_remark_curr=heap_remark+heap_remark_index;
				       remark_start_flag=1;
		       			//strcpy(father_node_ptr->remark, temp_str);           
               break;
          case 9:
               temp_ch = strstr(data_arr, "[End]");
               if(temp_ch != NULL)
               {
               	   
                    return 1;
               }
               else
                    leaf_state = 10;
               break;
          }
     }
     return 1;
} 




/*
*
*       Fun:    analyze_setTemplate
*
*       Desc:   analyze the text of the setTemplate field, create and register field nods to the father nod which father_nod_ptr points to
*
*       Ret:    successful: 1,	fail: 0
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*

int analyze_setTemplate(FILE *fp, mib_node *father_node_ptr)
{
    char				temp_str[1024*6], 	*temp_ch,	stop_ch[32],	temp_ch_2;
    int 				state = 0,i;
    DWORD       size;
    mib_child_node		*mib_child_node_ptr = NULL;
    mib_param_select 	*mib_param_select_ptr = NULL;
    mib_param_input		*mib_param_input_ptr = NULL;
    mib_node_param 		*mib_node_param_ptr = NULL;
    fpos_t				pos_3;

    strcpy(stop_ch , "[");
    while(!feof(fp))
    {
        read_until(fp, temp_str, stop_ch);
        if(mib_node_param_ptr)
        {
            if(temp_str[strlen(temp_str)-3]=='#' && temp_str[strlen(temp_str)-2]=='*' && temp_str[strlen(temp_str)-1]=='#')
            {
                father_node_ptr->enable_field_id=mib_node_param_ptr->id-1;//remember the param id
                //remember the enable select id,no need ,default is 1 or  0
            }
            if(temp_str[strlen(temp_str)-3]=='#' && temp_str[strlen(temp_str)-2]=='#' && temp_str[strlen(temp_str)-1]=='*')
            {//remmember the param id and the save select id
                module_save_flag[father_node_ptr->module_index].module_index=father_node_ptr->module_index;
                module_save_flag[father_node_ptr->module_index].save_field_id=father_node_ptr->total_child_nodes;
                //father_node_ptr->max_access=4;
                module_save_flag[father_node_ptr->module_index].save_select_id=mib_node_param_ptr->select_total;
                for(i=0;i<32;i++)
                    module_save_flag[father_node_ptr->module_index].oid[i]=father_node_ptr->oid[i];
                module_save_flag[father_node_ptr->module_index].oid_len=father_node_ptr->oid_len;
            }
        }
        //printf("setTem state: %d , temp_str: %s\r\n",state,temp_str);
        switch(state)
        {
        case 0:		//	name of the father node, not save, not null
            strcpy(stop_ch , "]");
            state = 1;
            break;
        case 1:		// find unit or remark
            if( strstr(temp_str, "remark") != NULL )
            {
                strcpy(stop_ch,";");
                state = 18;
            }
            else if( strstr(temp_str, "unit") != NULL )
            {
                strcpy(stop_ch,"[");
                state = 2;
            }
            else
            {
                printf("[debug] Error: Fail to find [unit] or [remark] in the setTemplate.\r\n");
                return 0;
            }
            break;
        case 2:		// apply a mib_node_param and fill its name
            if(strlen(temp_str) == 0)
            {
                printf("[debug] Error: Fail to get the field name after the string [unit] in the setTemplate.\r\n");
                return 0;
            }
            if (mib_node_param_index >= MIB_MAX_NODE_PARAMS)
            {
                printf("[debug] Error: There are not enough mib_node_param in the array.\r\n");
                return 0;
            }
            mib_node_param_ptr = &mib_node_param_array[mib_node_param_index++];
            strcpy( mib_node_param_ptr->name, temp_str);
            mib_node_param_ptr->id = (BYTE)(father_node_ptr->total_child_nodes+1);
            mib_node_param_ptr->parent_ptr=father_node_ptr;
            if (!(mib_child_node_ptr = create_mib_child_node(temp_str, father_node_ptr->total_child_nodes+1)))
            {
                printf("[debug] Error: Faile to Create child nod: [%s].\r\n", temp_str);
                return 0;
            }
            if (!register_child_node(father_node_ptr, mib_child_node_ptr))
            {
                printf("[debug] Error: Fail to register the child nod which named [%s].\r\n",temp_str);
                return 0;
            }
            mib_child_node_ptr->child_ptr = mib_node_param_ptr;

            strcpy(stop_ch,"]");
            state = 3;
            break;
        case 3:
            if( strstr(temp_str, "u") == NULL )
            {
                printf("[debug] Error: Fail to find [%s] after [%s] in the setTemplate.\r\n", "u", "unit");
                return 0;
            }
            strcpy(stop_ch,".");
            state = 4;
            break;
        case 4:
            if(strlen(temp_str) == 0)
            {
                printf("[debug] Error: Fail to get the byte_begin in the setTemplate.\r\n");
                return 0;
            }
            mib_node_param_ptr->byte_begin = (BYTE)atoi(temp_str);
            strcpy(stop_ch,"-");
            state = 5;
            break;
        case 5:
            if(strlen(temp_str) == 0)
            {
                printf("[debug] Error: Fail to get the bit_begin in the setTemplate.\r\n");
                return 0;
            }
            mib_node_param_ptr->bit_begin = (BYTE)atoi(temp_str);
            strcpy(stop_ch,".");
            state = 6;
            break;
        case 6:
            if(strlen(temp_str) == 0)
            {
                printf("[debug] Error: Fail to get the byte_end in the setTemplate.\r\n");
                return 0;
            }
            mib_node_param_ptr->byte_end = (BYTE)atoi(temp_str);
            strcpy(stop_ch,"[");
            state = 7;
            break;
        case 7:
            if(strlen(temp_str) == 0)
            {
                printf("[debug] Error: Fail to get the bit_end in the setTemplate.\r\n");
                return 0;
            }
            mib_node_param_ptr->bit_end = (BYTE)atoi(temp_str);
            strcpy(stop_ch,"]");
            state = 8;
            break;
        case 8:
            if( strstr(temp_str, "u") == NULL )
            {
                printf("[debug] Error: Fail to find [%s] after [%s] in the setTemplate.\r\n", "u", "u");
                return 0;
            }
            strcpy(stop_ch,"[");
            state = 9;
            break;
        case 9: 	// meet "select" or "input"
            if( strstr(temp_str, "select") != NULL )
                state = 10;
            else
            {
                if( strstr(temp_str, "input") != NULL )
                {
                    state = 15;
                }
                else
                {
                    printf("[debug] Error: Fail to find [%s] or [%s] in the setTemplate.\r\n", "select", "input");
                    return 0;
                }
            }
            strcpy(stop_ch,"]");
            break;
        case 10:	// "v" or "remark" or "unit"
            if( strstr(temp_str, "v") != NULL )
            {
                strcpy(stop_ch,"[");
                state = 11;
            }
            else
            {
                if( strstr(temp_str, "unit") != NULL )
                {
                    strcpy(stop_ch,"[");
                    state = 2;
                }
                else
                {
                    if( strstr(temp_str, "remark") != NULL )
                    {
                        strcpy(stop_ch, ";");
                        // may meet "[uint]" or a ';', so must handle the whole remark field and then find "[unit]" or ';'
                        state = 18;
                    }
                    else
                    {
                        printf("[debug] Error: Fail to find [%s] or [%s] or [%s] in the setTemplate.\r\n", "v", "unit", "remark");
                        return 0;
                    }
                }
            }
            break;
        case 11:
            if(strlen(temp_str) == 0)
            {
                printf("[debug] Error: Fail to get the select number in the setTemplate.\r\n");
                return 0;
            }
            if(mib_param_select_index >= MIB_MAX_PARAM_SELECTS)
            {
                printf("[debug] Error: There are not enough mib_param_selects in the array.\r\n");
                return 0;
            }
            mib_param_select_ptr = &mib_param_select_array[mib_param_select_index++];
            mib_node_param_ptr->select_ptr[mib_node_param_ptr->select_total++] = mib_param_select_ptr;
            mib_param_select_ptr->number = (BYTE)atoi(temp_str);
            strcpy(stop_ch,"]");
            state = 12;
            break;
        case 12:
            if( strstr(temp_str, "opt") == NULL )
            {
                printf("[debug] Error: Fail to find [%s] after [%s] in the setTemplate.\r\n", "opt", "v");
                return 0;
            }
            strcpy(stop_ch,"[");

            fgetpos(fp, &pos_3);
            while(!feof(fp))
            {
                temp_ch_2 = fgetc(fp);
                if(temp_ch_2 == '[')
                {
                    strcpy(stop_ch,"[");
                    break;
                }
                else if(temp_ch_2 == ';')
                {
                    strcpy(stop_ch,";");
                    break;
                }
            }
            fsetpos(fp, &pos_3);

            state = 13;
            break;
        case 13:
            if(strlen(temp_str) == 0)
            {
                printf("[debug] Error: Fail to get the description of the select option in the setTemplate.\r\n");
                return 0;
            }
            if( (temp_ch=strchr(temp_str, 0xd)) != NULL )
                *temp_ch = '\0';
            if( (temp_ch=strchr(temp_str, 0xa)) != NULL )
                *temp_ch = '\0';
            strcpy(mib_param_select_ptr->description, temp_str);
            if(stop_ch =="[")
                strcpy(stop_ch,"]");
            else
            {
                strcpy(stop_ch,";");
                return 1;
            }
            state = 10;
            break;
        case 15:	 // has meet "input"
            if( strstr(temp_str, "v") == NULL )
            {
                printf("[debug] Error: Fail to find [%s] after [%s] in the setTemplate.\r\n", "v", "input");
                return 0;
            }
            state = 16;
            strcpy(stop_ch,"[");
            break;
        case 16:
            if (mib_param_input_index >= MIB_MAX_PARAM_INPUTS)
            {
                printf("[debug] Error: There are not enough mib_param_inputs in the array.\r\n");
                return 0;
            }
            mib_param_input_ptr = &mib_param_input_array[mib_param_input_index++];
            mib_node_param_ptr->input_ptr = mib_param_input_ptr;

            if(strstr(temp_str, "toDec-high-0"))
            {
                mib_param_input_ptr->input_param_type = TODEC_HIGH_0;
            }
            else
            {
                if(strstr(temp_str, "keepHex-high-0"))
                    mib_param_input_ptr->input_param_type = KEEPHEX_HIGH_0;
                else
                {
                    if(strstr(temp_str, "keepHex-low-0"))
                        mib_param_input_ptr->input_param_type = KEEPHEX_LOW_0;
                    else
                    {
                        if(strstr(temp_str, "keepHex-low-E"))
                            mib_param_input_ptr->input_param_type = KEEPHEX_LOW_E;
                        else
                        {
                            if(strstr(temp_str, "keepHex-low-F"))
                                mib_param_input_ptr->input_param_type = KEEPHEX_LOW_F;
                            else
                            {
                                if(strstr(temp_str, "toAscii-low-0"))
                                    mib_param_input_ptr->input_param_type = TOASCII_LOW_0;
                                else
                                {
                                    if(strstr(temp_str, "keepAsc-low-F"))
                                        mib_param_input_ptr->input_param_type = KEEPASC_LOW_F;
                                    else
                                    {
                                        printf("[debug] Error: There is not input param like %s.\r\n",temp_str);
                                        return 0;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            strcpy(stop_ch,"]");
            state = 17;
            break;
        case 17:
            if( strstr(temp_str, "unit") != NULL )
            {
                strcpy(stop_ch,"[");
                state = 2;
            }
            else
            {
                if( strstr(temp_str, "remark") != NULL )
                {
                    strcpy(stop_ch, ";");
                    // may meet "[uint]" or a ';', so must handle the whole remark field and then find "[unit]" or ';'
                    state = 18;
                }
                else
                {
                    printf("[debug] Error: Fail to find [%s] or [%s] in the setTemplate.\r\n", "unit", "remark");
                    return 0;
                }
            }
            break;
        case 18:
            if(strlen(temp_str) == 0)
            {
                father_node_ptr->remark_ptr=NULL;
                return 1;
            }
            else
            {
            	 while((temp_ch = strstr(temp_str, "\n")) != NULL)
                {
                    temp_ch[0] = ' ';
                }
                while((temp_ch = strstr(temp_str, "<br><br>")) != NULL)
                {
                    temp_ch[0] = '\n';
                    temp_ch[1] = ' ';
                    temp_ch[2] = ' '; 
                    temp_ch[3] = '\r'; 
                    temp_ch[4] = ' ';
                    temp_ch[5] = ' ';
                    temp_ch[6] = ' ';
                    temp_ch[7] = ' ';                    
                }
                while((temp_ch = strstr(temp_str, "<br>")) != NULL)
                {
                    temp_ch[0] = ' ';
                    temp_ch[1] = ' ';
                    temp_ch[2] = ' '; 
                    temp_ch[3] = ' ';                    
                }
                size=strlen(temp_str);                 
                memcpy(heap_remark_curr,temp_str,sizeof(char)*(size+1));
                father_node_ptr->remark_ptr=heap_remark_curr;
                heap_remark_index+=size+1; 
                heap_remark_curr=heap_remark+heap_remark_index;
                //strcpy(father_node_ptr->remark, temp_str);
                return 1;
            }
        }
    }
    printf("[debug] Error: There is an Error in the state %d in the setTemplate.\r\n", state);
    return 0;
}	//	end of analyze_setTemplate
  

*
*       Fun:    MIB_create_leaves
*
*       Desc:   Create the leaves of the MIB tree in memory using the name of configure file which lists mib files 
*
*       Ret:    1: Successful;		0: Fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*

int MIB_create_leaves(char *conf_dir,	char *sysConf_file,		char *param_list_file)
{
    FILE 				*fp_list,	 *fp_param;
    char				ch,	file_name[256],	temp_str[256],out_text[32],	temp_str_2[DEBUG_OMCBUF_LEN];
    DWORD				OID[32];
    int					obj_num, oid_len,	state, 	index,	Disable;
    fpos_t				pos_1, 		pos_2;
    mib_node			*grand_node_ptr, *father_node_ptr;
    mib_child_node      *mib_child_node_ptr;
    int          module_index,i;
    char        total_instance[4];

    mib_node_param_index = 0;
    mib_param_select_index = 0;
    mib_param_input_index = 0;

    memset((BYTE *)mib_param_select_array, 0, sizeof(mib_param_select) * MIB_MAX_PARAM_SELECTS);
    memset((BYTE *)mib_param_input_array, 0, sizeof(mib_param_input) * MIB_MAX_PARAM_INPUTS);
    memset((BYTE *)mib_node_param_array, 0, sizeof(mib_node_param) * MIB_MAX_NODE_PARAMS);
    grand_node_ptr = NULL;
    father_node_ptr = NULL;

    strcpy(temp_str, 	"");
    strcpy(temp_str_2, 	"");

    strcpy(file_name, 	"");
    strcpy(file_name,	conf_dir);
    strcat(file_name,	param_list_file);

    if((fp_list = fopen(file_name,	"r")) == NULL)
    {
        printf("[debug] Error: cannot open param_list_file: [%s].\n",file_name);
        return 0;
    }

    while(!feof(fp_list))
    {
        strcpy(file_name,	"");
        strcpy(file_name,	conf_dir);
        strcpy(temp_str,	"");
        if( read_line(fp_list,	temp_str) == 0)
            continue;

        if(!( (strstr(temp_str, "OBJ_")) && (strstr(temp_str, "_paramConf.csv"))) )
        {
            strcpy(temp_str, "OBJ_***_paramConf.csv");
            printf("[debug] Error: The name of a mib_param_file should like %s, the * presents digit. \nAnd we need the digit to find the OID prefix of this file.\n",temp_str);
            fclose(fp_list);
            return 0;
        }

        for(index = 4; (index < 7) && (temp_str[index] != '_'); index++)
        {
            temp_str_2[index - 4] = temp_str[index];
        }
        temp_str_2[index - 4] = '\0';
        i=0;
        while(i<MIB_SUBSYS_MODULE_MAX_NUM)
        {
            if(strncmp(temp_str_2,subsys_module[i].obj_type_no,strlen(subsys_module[i].obj_type_no))==0)
                break;
            i++;
        }
        if(i!=MIB_SUBSYS_MODULE_MAX_NUM)
            module_index=i;
        else
        {
            printf("[DEBUG]:error!Do not register the module attach to the obj_%s_para!\r\n",temp_str_2);
        }
        obj_num = atoi(temp_str_2);
        strcpy(temp_str_2, "");

        strcat(file_name,	temp_str);
        if((fp_param = fopen(file_name,	"r")) == NULL)
        {
            printf("[debug] Error: Cannot open mib_param_file: [%s].\n",file_name);
            fclose(fp_list);
            return 0;
        }

        printf("open mib_param_file [%s].\n",file_name);

        memset( OID, 0, sizeof(DWORD) * 32);
        strcpy(file_name,	conf_dir);
        strcat(file_name,	sysConf_file);

        oid_len = find_OID(obj_num,file_name, OID);
        if(oid_len == 0)
        {
            printf("[debug] Error: Cannot find the oid prefix which number is %d in mib_param_sysConf_file: [%s].\n",obj_num, sysConf_file);
            fclose(fp_list);
            return 0;
        }
        else
            grand_node_ptr = search_mib_node(mib_root_node, OID, oid_len);
        if(grand_node_ptr == NULL)
        {
            int length = oid_len + index;
            printf("[debug] Error: can not find the mib node whose oid is %d",(int)OID[0]);
            for(index = 1; (index < length) && (OID[index] != 0); index ++)
                printf(".%d",(int)OID[index]);
            printf("\n");
            fclose(fp_param);
            fclose(fp_list);
            return 0;
        }

        state = 0;
        while(!feof(fp_param))
        {
            //	printf("state: %d. ",state);
            switch(state) 
            {
            case 0:
                if(read_until(fp_param, temp_str, ";") == 0)
                    state = 0;
                else
                    state = 1;
                //printf("start number: %s\n",temp_str);
                
                break;
            case 6:
                Disable = 0;	//0: used,  1: not used
                strcpy(temp_str, "");
                if((read_until(fp_param, temp_str, ";") != 0) && (atoi(temp_str) == 0) )
                    Disable = 1;
                state++;
                break;
            case 7:
                if((read_until(fp_param, temp_str, ";") != 0))
                {
                    strcpy(total_instance,temp_str);
                }
                state++;
                break;
            case 10:       //intit value
                if((read_until(fp_param, temp_str, ";") != 0))
                {
                    strcpy(curr_instance_value,temp_str);
                    if(strlen(curr_instance_value)>256)
                    {
                        printf("the max init value long is 256!\r\n");
                        printf(father_node_ptr->name);
                        strcpy(temp_str_2,"");
                        for(i=0;i<32;i++)
                        {
                            printf("%ld.",father_node_ptr->oid[i]);
                            strcat(temp_str_2,out_text);
                        }
                        printf("oid:%s\n",temp_str_2);
                        strcpy(temp_str_2,"");
                        printf("length:%d\n",strlen(temp_str));
                        printf("OBJ_%d_param.cvs\r\nstring:",obj_num);
                        printf(temp_str);
                        return 0;
                    }
                }
                state++;
                break;
            case 14:
                fgetpos(fp_param, &pos_1);
                state++;
                break;
            case 19:
                //printf("state: %d. ",state);
                for(index=0;index<10;index++)
                {
                    read_until(fp_param, temp_str, ";");
                    OID[oid_len + index] = atoi(temp_str);
                    //printf("%ld. ",OID[oid_len + index]);
                    if(OID[oid_len + index] == 0)
                        break;
                }
                read_until(fp_param, temp_str, ";");
                read_until(fp_param, temp_str, "\n");//0x0a
                if(Disable==0) // used
                {
                    fgetpos(fp_param, &pos_2);

                    fsetpos(fp_param, &pos_1);
                    if(fgetc(fp_param) != ';')
                    {
                        fsetpos(fp_param, &pos_1);

                        father_node_ptr = search_mib_node(grand_node_ptr, OID, oid_len + index);
                        if( father_node_ptr == NULL )
                        {
                            int length = oid_len + index;
                            printf("[debug] Error: can not find the father node whose oid is %d",(int)OID[0]);
                            for(index = 1; index < length; index ++)
                                printf(".%d",(int)OID[index]);
                            printf("\ngrand_father->name: %s,	OID= %d",grand_node_ptr->name, (int)grand_node_ptr->oid[0]);
                            for(index = 1; index < grand_node_ptr->oid_len; index ++)
                                printf(".%d",(int)grand_node_ptr->oid[index]);
                            printf("\n");
                            for(index=0; index<grand_node_ptr->total_child_nodes; index++)
                            {
                                mib_child_node_ptr=grand_node_ptr->child_nodes[index];
                                if(mib_child_node_ptr)
                                		printf("oid: %d,  name: %s\n", (int)mib_child_node_ptr->oid,grand_node_ptr->child_nodes[index]->name);
                            }
                            fclose(fp_param);
                            fclose(fp_list);
                            return 0;
                        }
                        else
                        {
                            if(strlen(total_instance))
                                father_node_ptr->total_instance=atoi(total_instance);
                            else
                                father_node_ptr->total_instance=0;
                            strcpy(father_node_ptr->init_value,curr_instance_value);
                            father_node_ptr->module_index=module_index;
                            if(analyze_setTemplate(fp_param, father_node_ptr) != 1)
                            {
                                printf("analyze_setTemplate2 fail.\n");
                                fclose(fp_param);
                                fclose(fp_list);
                                return 0;
                            }
                        }
                    }
                    fsetpos(fp_param, &pos_2);
                }
                state = 0;
                break;
            default:
                ch = fgetc(fp_param);
                if( ch == ';' )
                    state++;
                break;
            }
        }
        if(state == 0)
            fclose(fp_param);
        else
        {
            printf("[debug] Error: There is an error in the mib_param_file %s.\n", file_name);
            fclose(fp_param);
            fclose(fp_list);
            return 0;
        } 
    }
    fclose(fp_list);
    return 1;
}// end of MIB_create_leaves
*/




/*
*
*       Fun:    create_node
*
*       Desc:   create and register node using the given parameters 
*
*       Ret:    successful: 1,	fail: 0
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
mib_node *create_node(char *line, char *nod_name, int instance, mib_node *father_ptr, DWORD *last_node_oid)
{
    mib_node			*mib_node_ptr,	 *father_node_ptr;
    mib_child_node		*mib_child_node_ptr;
    char 				*tmp_ch, 	father_name[64],	id[32];
    DWORD				ID_c=0,	curr_oid[32];
    int 				index , curr_oid_len;
    FILE                 *fp_conf;
    char                 file_name[128];
    char                 space[32]="-> ";;

    strcpy(file_name, 	"");

    strcpy(file_name,	MIB_FILES_DIR);
    strcat(file_name,	MIB_WRITE_TREE ); 
     
    if((fp_conf = fopen(file_name,"a+")) == NULL)
    {
        printf("[debug] Error: cannot open conf_file: [%s].\r\n",file_name);
        return 0;
    } 
     
    strcpy(father_name, "");
    strcpy(id, "");
    //fprintf(fp_conf,"line = [%s], nod_name = [%s].\r\n",line , nod_name);
    tmp_ch = strchr(line, '}');	// collect the father_name and nod's ID from the line
    if(tmp_ch != NULL)
    {
        tmp_ch = strchr(line, '{');
        if(tmp_ch != NULL)
        {
            tmp_ch = &tmp_ch[1];
            tmp_ch = pick_first_word(tmp_ch, father_name);
            if( strchr(father_name, '}') )
            {
                printf("[debug] Error: There is not ID between '{' and '}' in line [%s].\r\n",line);
                fclose(fp_conf);
                return 0;
            }
            pick_first_word(tmp_ch, id);
            tmp_ch = strchr(id, '}');
            if(tmp_ch != NULL)
                *tmp_ch = '\0';
            if(strlen(id) == 0)
            {
                printf("[debug] Error: There is not ID between '{' and '}' in line [%s].\r\n",line);
                fclose(fp_conf);
                return 0;
            }
            ID_c = atol(id);
           
        }
        else
        {
            printf("[debug] Error: Can not find a '{' as a beginning of a text.\r\n");
            fclose(fp_conf);
            return NULL;
        } 
    }
    else
    {
        printf("[debug] Error: Can not find a '}' as an ending of a text.\r\n");
        fclose(fp_conf);
        return NULL;
    }
    
    father_node_ptr = find_node(father_ptr, father_name, 1, last_node_oid);
    if(!father_node_ptr)
    {
        fprintf(fp_conf,"father_ptr->name: %s  , father_name: %s\r\n",father_ptr->name,father_name);
        printf("[debug] Error: Can not find the father nod which named [%s].\r\n",father_name);
        fclose(fp_conf);
        return NULL;
    }
    
    for(index=0; index<32; index++)
        curr_oid[index] = father_node_ptr->oid[index];
    curr_oid_len = father_node_ptr->oid_len;

    curr_oid[curr_oid_len++] = ID_c;

    if(!(mib_node_ptr = create_mib_node(nod_name, curr_oid_len, curr_oid, instance)))
    {
        printf("[debug] Error: Create nod: [%s] fail. mib_node_index = %d.\r\n", nod_name, mib_node_index);
        fclose(fp_conf);
        return 0;
    }
    
    //mib_node_ptr->module_index=gobal_module_index-1; 
    
    fprintf(fp_conf,"Create node: %s(OID: ",nod_name);
    for(index=0;index<curr_oid_len-1;index++)
    {
    	fprintf(fp_conf,"%u.",curr_oid[index]);
    }
    fprintf(fp_conf,"%u)\r\n",curr_oid[curr_oid_len-1]);
    fprintf(fp_conf,"%s father_name = [%s]\r\n",space,father_name);
    strcat(space,"-> ");
    // this nod is not the first nod, must register it to its father nod and itself.
    if (!(mib_child_node_ptr = create_mib_child_node(nod_name, ID_c)))
    {
        printf("[debug] Error: Create child nod: [%s] fail.\r\n", nod_name);
        fclose(fp_conf);
        return NULL;
    }

    if (!register_child_node(father_node_ptr, mib_child_node_ptr)) 
    {
        printf("[debug] Error: Can not register the child nod which named [%s].\r\n",nod_name);
        fclose(fp_conf);
        return NULL;
    }

    //fprintf(fp_conf,"register the child nod [%s].father nod name [%s] father nod total_child_nodes [%d].\r\n",nod_name,father_node_ptr->name, father_node_ptr->total_child_nodes);
    for(index = 0; index< father_node_ptr->total_child_nodes; index++)
    {
        mib_child_node_ptr=father_node_ptr->child_nodes[index];
        if(mib_child_node_ptr)
    	    fprintf(fp_conf,"%s    %s(%d)\r\n", space,father_node_ptr->child_nodes[index]->name,index);
    }

    if (!register_node(mib_node_ptr, curr_oid, curr_oid_len))
    {
        printf("[debug] Error: Can not register the nod which named [%s].\r\n",nod_name);
        fclose(fp_conf);
        return NULL;
    }
    memset(last_node_oid, 0, sizeof(DWORD) * 32);
    for(index =0; index < mib_node_ptr->oid_len; index++)
        last_node_oid[index] = mib_node_ptr->oid[index];
    fprintf(fp_conf,"\r\n\r\n");
    fclose(fp_conf);
    return mib_node_ptr;
}	//	end of create_node

/*
*
*       Fun:    create_mib_child_node
*
*       Desc:   create a mib_child_node
*
*       Ret:    return the mib_child_node pointer if successful, return null if fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c   
*
*/
mib_child_node	*create_mib_child_node(char *name, DWORD oid)
{
    mib_child_node *mib_child_node_ptr;

    if (mib_child_node_index >= MIB_MAX_CHILD_NODES)
        return NULL;

    mib_child_node_ptr = &mib_child_nodes[mib_child_node_index++];

    strcpy(mib_child_node_ptr->name, name);
    mib_child_node_ptr->oid = oid;
    mib_child_node_ptr->child_ptr = NULL;

    return mib_child_node_ptr;
}	// end of create_mib_child_node



/*
*
*       Fun:    create_mib_node
*
*       Desc:   create a mib_node
*
*       Ret:    return the mib_node pointer if successful, return null if fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
mib_node	*create_mib_node(char *name, BYTE oid_len, DWORD *oid, WORD total_instance)
{
    mib_node 	*mib_node_ptr;
    if (mib_node_index >= MIB_MAX_NODES)
        return NULL;

    mib_node_ptr = &mib_nodes[mib_node_index++];
    /*
    index=0xFF;
    if(oid_len>9)
    {
        memcpy(temp_oid, oid, oid_len * sizeof(DWORD));
        max_len=0;
        for(j=0;subsys_module[j].index!=0xFF;j++)
        {
            for(i=8;subsys_module[j].oid[i-6]!=0 && i<14;i++)
                if(temp_oid[i]!=subsys_module[j].oid[i-6])
                    break;
            if(subsys_module[j].oid[i-6]==0 && i>max_len)
            {
                max_len=i;
                index=j;
            }
        }
        if(index!=0xFF)
            mib_node_ptr->module_index=index;
    }
    */   
    strcpy(mib_node_ptr->name, name);
    mib_node_ptr->oid_len = oid_len;
    memcpy(mib_node_ptr->oid, oid, oid_len * sizeof(DWORD));
    mib_node_ptr->total_instance = total_instance;
    mib_node_ptr->curr_instance = 0;
    mib_node_ptr->parent_ptr = NULL;
    //mib_node_ptr->mib_proc = mib_proc;

    return mib_node_ptr;
}	//	end of create_mib_node




int register_node(mib_node *mib_node_ptr, DWORD *oid, BYTE oid_len)
{
    mib_node 		*node_ptr;
    mib_child_node	*child_node_ptr = NULL;
    BYTE			i, loop_times;
    WORD			child_index;

    if (oid_len <= mib_root_node->oid_len)
        return 0;
    if ((loop_times = oid_len - mib_root_node->oid_len) >= 20)
        return 0;

    node_ptr = mib_root_node;
    for (i = 0; i < loop_times; i++)
    {
        if ((child_index = find_child_index(node_ptr, oid[mib_root_node->oid_len + i], NULL, 0)) >= MIB_MAX_CHILD_NODES_PER_NODE)
            return 0;
        child_node_ptr = node_ptr->child_nodes[child_index];
        if(child_node_ptr)
	        if (i != (loop_times - 1))
	        {
	            if (child_node_ptr->child_ptr == NULL)
	                return 0;
	            node_ptr = (mib_node *) child_node_ptr->child_ptr;
	        }
    }

    child_node_ptr->child_ptr = mib_node_ptr;
    mib_node_ptr->parent_ptr = node_ptr;

    return 1;
}


/*
*
*       Fun:    register_child_node
*
*       Desc:   register a child node to its father mib node
*
*       Ret:    return 1 if successful, return 0 if fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/ 
int register_child_node(mib_node *mib_node_ptr, mib_child_node *mib_child_node_ptr)
{
	  char text_out[128];
	  int  index;
	  mib_child_node *tmp_child_node;
    if (mib_node_ptr->total_child_nodes >= MIB_MAX_CHILD_NODES_PER_NODE)
    {
    	printf("[Debug] Error: The total child is more then the max child number.%d,MAX:%d.\r\n",mib_node_ptr->total_child_nodes,MIB_MAX_CHILD_NODES_PER_NODE);
        return 0;
    }
    if(!mib_child_node_ptr)
    {
       sprintf(text_out,"[Debug] : The child node is NULL! Father:%s,ID: %d\r\n",mib_node_ptr->name,mib_node_ptr->total_child_nodes);     
       TxtColor(BRIGHT,RED,BLACK,text_out);
       printf("%s", text_out);  
       return 0;
    }
    for (index = 0; index < mib_node_ptr->total_child_nodes; index++)
		{
				tmp_child_node = mib_node_ptr->child_nodes[index];			      
				if ( mib_child_node_ptr->oid <= tmp_child_node->oid )
				{
				   printf("[debug] Error: The oid have register or it is not written by ascending! Can not be Reisterted again\r\nLine: %d.\r\n", mib_line_count);
				   exit(1);
				 }				       
		}
    mib_node_ptr->child_nodes[mib_node_ptr->total_child_nodes] = mib_child_node_ptr;
    mib_node_ptr->total_child_nodes++;
    return 1;
}// end of register_child_node

//////////////////////////////End of Create OID Tree//////////////////////



void debug_mmi_remark()
{
      
   //char    out_char[16];
   char    out_text[128];
   WORD    out_len=0;
   WORD    i;
   BYTE    j,k,m;
   BYTE    line_count=0;
   char    ch;
   switch(debug_remark_flag)
   {
   case 0:
   	      return;
   case 1:
			    if(mib_curr_node->remark_ptr==NULL) 
			    {
			        debug_ascii_out_proc("Fail    : Sorry,No Remark for the object!\r\n");
			        return;
			    }  
			    debug_ascii_out_proc("Remark  : ");			    
			    for(i=0;*(mib_curr_node->remark_ptr+i);i++)
			    {
			        ch=mib_curr_node->remark_ptr[i]; 
			        if(ch!='\n' && ch!='\t' && ch!=' ')
			           break;
			    }
			    if(!*(mib_curr_node->remark_ptr+i))
			    {
			          debug_remark_flag=0;
			          debug_cfg_end=1;
			          return;
			    }
			    debug_remark_ptr=i;
			    debug_remark_flag=2;
			    debug_cfg_end=0;
			    remark_meet_lf=0;
			    break;
	 case 2: 	     
	 	      if(debug_remark_ptr) 
	 	      {
	 	         strcpy(out_text,"          ");
	 	         out_len=10;
	 	      }
	 	      else
	 	      {
			       strcpy(out_text,"   ");
			       out_len=3;
			       j=3;  
			    }
			    if(remark_meet_lf)
			    {
			       j=3;
			       strcat(out_text,"   ");
			       out_len=13; 
			       remark_meet_lf=0;
			    }
			    else if(debug_remark_ptr)
			       j=0;
			    i=debug_remark_ptr;			    
			    for(;*(mib_curr_node->remark_ptr+i)!='\0';i++)
			    {
			        ch=mib_curr_node->remark_ptr[i];       
				      if(ch!='\n' && ch!='\t')
				      {
			             *(out_text+out_len++)=ch;
				           j++;
				      }
				      if((ch=='\n' && i) || j==70)
				      {				      	      
				      	      if(ch!='\n')
				      	      {
					      	      while((ch=mib_curr_node->remark_ptr[i+1]))
					      	      {
					      	      	if(ch==' ' || ch=='\t')
					      	      	{
					      	      	   i++;
					      	      	   continue;
					      	      	}else if(ch=='\n')
					      	        {
					      	        	i++;
					           	      j=0;
					           	      continue;
					      	        }
					      	        else 
					      	          break;
					      	      }
				      	      }
				      	      else 
				      	      {
				      	      	i++;
				           	  	while((ch=*(mib_curr_node->remark_ptr+i)))
				           	  	         if(ch!=' ' || ch!='\n' || ch!='\t')
				           	  	            break;
				           	  	         else
				           	  	            i++; 
				           	  	if(*(mib_curr_node->remark_ptr+i)=='\0')
				           	  	    line_count=3;
				           	  	i--;
				      	      }
				      	      
				      	      *(out_text+out_len++)='\r';
				           	  *(out_text+out_len++)='\n';	
				           	  *(out_text+out_len)='\0';       	  
				           	  debug_color_out(BRIGHT,YELLOW,BLACK,out_text);
				           	  line_count++; 
				           	  if(j==70)
				           	     m=10;
				           	  else
				           	  {
				           	     m=13;
				           	     remark_meet_lf=1;
				           	  }
				           	  if(line_count==3)
				           	  {   
				           	  	    debug_remark_ptr=(mib_curr_node->remark_ptr[i]=='\n')?(i+1):i;			           	  	    
				           	        if(*(mib_curr_node->remark_ptr+i)=='\0')
				           	        {
				           	             debug_remark_flag=0;
														     debug_remark_ptr=0;
														     remark_meet_lf=0; 
														     debug_cfg_end=1; 
														 }
                            return;
                       }
				           	  *out_text='\0';
				           	  out_len=0;				           	  
				           	  for(k=0;k<m;k++)    
				           	    *(out_text+out_len++)=' ';
				           	  j=0;
				           	  if(remark_meet_lf) 
				           	     j=3; 
				           	  remark_meet_lf=0;   	 			               
				      }
				  }    
				  *(out_text+out_len++)='\0';
				   if(*(mib_curr_node->remark_ptr+i)=='\0' && j!=0 && line_count<3)
				       debug_color_out(BRIGHT,YELLOW,BLACK,out_text); 
				   if(!(out_text[out_len-1]=='\n' && out_text[out_len-2]=='\r'))
			         debug_ascii_out_proc("\r\n");     
			     debug_remark_flag=0;
			     debug_remark_ptr=0;
			     remark_meet_lf=0;
			     debug_cfg_end=1; 
			     break;
		}			   
} 


void register_host()
{
    char      file_name[48],temp_str[128],ch2[32],*p,*q,*m,ip[4],stop_ch[32];
    char      temp_str2[32];
    FILE      *fp;
    BYTE      i,j,state,group_index,member_index,max_group;
    WORD      module_index;
    group_index=0;
    member_index=0;
    /*
    strcpy(file_name, 	"");
    strcpy(file_name,	MIB_FILES_DIR);
    strcat(file_name,	MIB_SYSTEM_CONFIG_FILE	);
    */
    /*
    if((fp = fopen(file_name,	"r")) == NULL)
    {
        sprintf(temp_str, "Fail    : cannot open system config file: [%s].\r\n",file_name);
        debug_color_out(BRIGHT,RED,BLACK,temp_str);
        return ;
    }
    strcpy(stop_ch," ");
    module_index=0;
    state=1;
    while(!feof(fp))
    {
        read_until(fp, temp_str,stop_ch);
        if(strlen(temp_str)>64)
        {
            debug_color_out(BRIGHT,RED,BLACK,"Error   : The config file is not right format!\r\n");
            return ;
        }
        switch(state)
        {
        case 1:  //get the module oid
            for(i=0;i<=12;i++)   //del the root_oid 6X2=1.3.6.1.4.1.
            {
                p=temp_str+i;
            }
            subsys_module[module_index].index=module_index;
            i=0;
            for(j=0;j<6;j++)
            {
                while(*(p+i)!='.' && *(p+i)!='\0')
                {
                    oid[i]=*(p+i);
                    i++;
                }
                oid[i]='\0';
                subsys_module[module_index].oid[j]=atoi(oid);
                if(*(p+i)=='\0')
                    break;
                q=p+i+1;
                p=q;
                i=0;
            }
            state=2;
            break;
        case 2: //get the module name
            strcpy(subsys_module[module_index].name,temp_str);
            state=3;
            break;
        case 3://get the module obj_type_num
            strcpy(subsys_module[module_index].obj_type_no,temp_str);
            while((ch=fgetc(fp))!=0xa && ch!=EOF)
            {}
            if(ch==EOF)
                break;
            state=1;
            module_index++;
            break;
        }

    }
    fclose(fp);
   */
    strcpy(file_name, 	"");
    strcpy(file_name,	MIB_FILES_DIR);
    strcat(file_name,	MIB_SYSTEM_IP_LIST_FILE);
    
    if((fp = fopen(file_name,	"r")) == NULL)
    {
        sprintf(temp_str, "Fail    : cannot open system ip list file: [%s].\r\n",file_name);
        debug_color_out(BRIGHT,RED,BLACK,temp_str);
        return;
    }
    state=1;
    module_index=0;
    strcpy(stop_ch," ");
    while(!feof(fp))
    {
        read_line(fp, temp_str);
        if(temp_str[0]=='/' && temp_str[1]=='/')
        {            
            continue;
        }
        if(strlen(temp_str)==0)
            continue;
        else if(strlen(temp_str)>64)
        {
            printf("Error   : The config file is not right format!\r\n");
            return ;
        }
        for(i=0;i<strlen(temp_str);i++)
        {
        	if(temp_str[i]==';')
        	    break;
        }
        if(temp_str[i]=='\0')
        {
        	printf("[Debug]: The IP List file 's format is not right!\r\n");
        	return ;
        }
        if(i>=1)
        {
	        for(j=i-1;j>0;j--)
	        {
	          if(temp_str[j]!=' ')
	             break;
	        }
        }
        strncpy(temp_str2,temp_str,j+1);
        temp_str2[j+1]='\0';
        m=temp_str+i+1;   
        module_host[module_index].index=module_index;
        strcpy(module_host[module_index].host_name,temp_str2);
        for(i=0;i<strlen(temp_str2);i++)
        {
           if(*(temp_str+i)=='_')
             break;
        }
        p=temp_str2+i+1;
        strcpy(ch2,"");
        ch2[0]=*p;
        ch2[1]='\0';
        group_index=atoi(ch2);
        module_host[module_index].group_index=group_index;
        p+=2;
        i=0;
        while(*(p+i)!='\0')
        { 
        	 if(*(p+i)==' ')
        	    break;
        	 i++;
        }
        if(*(p+i)=='\0')
        {
        	printf("[Debug]: The IP List file 's format is not right!\r\n");
        	return ;
        }
        strncpy(ch2,p,i);
        ch2[i]='\0';
        if(!test_str(ch2,0))
        {
        	printf("[Debug]: The IP List file 's format is not right!\r\n");
        	return ;
        } 
        member_index=atoi(ch2); 
        module_host[module_index].member_index=member_index; 
        
     
        p=m;
        while(*p==' ')
            p++;   
        if(strstr(p,"LocalHost") || strstr(p,"localhost") || strstr(p,"LOCALHOST")||strstr(p,"Localhost"))
        {
        	struct in_addr in;
          get_my_address(&in);
          p=inet_ntoa(in);
        }       
	     for(i=0;i<4;i++)
	      {
	          for(j=0;*(p+j)!='.'&& *(p+j)!=';'&&*(p+j)!='\0';j++)
	          {
	                ip[j]=*(p+j);
	          }
	          ip[j]='\0';
	          module_host[module_index].host_ip[i]=atoi(ip);
	          q=p+j+1;
	          p=q;
	      }       
        module_index++;
        
    }
    fclose(fp);
    max_group=0;
    for(i=0;subsys_module[i].index!=0xFF;i++)
    {
        for(j=0;module_host[j].index!=0xFF;j++)
        {
            if(strncmp(module_host[j].host_name,subsys_module[i].name,strlen(subsys_module[i].name))==0 && max_group<module_host[j].index)
                max_group=module_host[j].group_index;
        }
        subsys_module[i].max_group=max_group;
    }
    printf("Read the ip list file                                      [");
    sprintf(temp_str," OK ");
    TxtColor(BRIGHT,YELLOW,BLACK,temp_str);
    printf("%s", temp_str);
    printf("]\r\n\r\n");
}

 

void debug_mmi_save()
{
    mib_node  *find_node_ptr;
    char       temp_text[32],temp_text2[32];
    mib_child_node   *mib_child_node_ptr;

    if(save_flag>=1)
    {
        if(module_save_flag[curr_module].save_field_id==0)
        {
            debug_color_out(BRIGHT,RED,BLACK,"Error   : The module do not contain save flag!\r\n");
            save_flag=0;
            return;
        }
        if(mib_curr_node->module_index!=curr_module || save_flag==2)
        {
        	  if(debug_cfg==1)
               temp_mib_node=mib_curr_node;
            else if(debug_cfg==2)
            {
               tmp_menu_node=menu_curr_node;
               temp_mib_node=mib_curr_node;
            }
            find_node_ptr=search_mib_node(mib_root_node,module_save_flag[curr_module].oid,module_save_flag[curr_module].oid_len);
            if(find_node_ptr)
                mib_curr_node=find_node_ptr;
            mib_child_node_ptr=mib_curr_node->child_nodes[module_save_flag[curr_module].save_field_id-1];
            if(mib_child_node_ptr)
                param_node = (mib_node_param *)mib_child_node_ptr->child_ptr;
            else
               printf("[Debug] : The child node is NULL. Father_node:%s,ID:%d.\r\n",mib_curr_node->name,module_save_flag[curr_module].save_field_id-1);          
            strcpy(temp_text,"#0."); 
            gcvt((double)(module_save_flag[curr_module].save_field_id),2,temp_text2);
            strcat(temp_text,temp_text2);
            strcat(temp_text,"=");
            sprintf(temp_text2,"%X",param_node->select_ptr[module_save_flag[curr_module].save_select_id-1]->number);
            //gcvt((double)param_node->select_ptr[module_save_flag[curr_module].save_select_id]->number,4,temp_text2);
            strcat(temp_text,temp_text2);
            if(save_flag==1)
                save_flag=3;
            else
                save_flag=4;
            debug_mmi_set(temp_text);

        }
    }
    else
        debug_color_out(BRIGHT,RED,BLACK,"Fail    : No change the data ,so not need to save!\r\n");
    debug_ascii_out_proc("Status  : ");
    sprintf(temp_text2,"Saving the module of %s,NOW!\r\n",subsys_module[curr_module].name);
    debug_color_out(BRIGHT,CYAN,BLACK,temp_text2);

}


///////////////////////////////Create Menu Tree//////////////////////
int create_menu_tree(char *conf_dir,char *menu_page,mib_node  *sys_node)
{
    char 		     *p,*q,temp_str1[256],file_name[64],temp_str2[64],temp_str3[64],oid[16];
    BYTE 		     index1,index2,sys_group,group_type;//group_type:'/' is 0,'M' is 1,'m' afer '/' is 2
    WORD         index3, line_count=0;
    menu_node    *menu_node_ptr; 
    menu_node    *menu_father_node;
    mib_node     *mib_tmp_node;
    BYTE         father_id[16]; 
    FILE         *fp;
    DWORD        full_oid[32];

    mib_tmp_node=search_mib_node(mib_root_node,cur_oid,9);
    strcpy(file_name, 	"");
    strcpy(file_name,	conf_dir);
    strcat(file_name,	menu_page);
    if((fp = fopen(file_name,	"r")) == NULL)
    {
        printf("Error: cannot open menu tree file [%s].\n",file_name);
        return 0;
    }
    while(!feof(fp))
    {
        line_count++;
        if( read_line(fp,temp_str1) == 0)
            continue;
        if(temp_str1[0]=='#')
            continue;
        for(index1=0;index1<strlen(temp_str1);index1++)
        {
            if(temp_str1[index1]!=' ')
                break;
        }
        p=temp_str1+index1;
        while(*p!=';')
        {
            switch(*p)
            {
            case '/':
                group_type=0;
                p++;
                index3=0;
                while(*p=='/'|| *p==' ')
                    p++;
                if(*p!='.')
                {
                    printf("[debug] : Format is not right,the menu id string must start with '.',line:%d\n",line_count);
                    return 0;
                }
                else
                {
                    if(*(p+1)==' ')
                    {
                        menu_node_ptr=&menu_nodes[menu_node_index++];
                        strcpy(menu_node_ptr->name,"WXC2");
                        menu_node_ptr->father_ptr=NULL;
                        menu_node_ptr->menu_id[0]=0;
                        menu_node_ptr->id_len=0;
                        menu_node_ptr->sys_group=9;
                        for(index1=0;index1<10;index1++)
                            menu_node_ptr->oid[index1]=0;
                        menu_node_ptr->menu_oid_node=search_mib_node(mib_root_node,cur_oid,9);
                        menu_root_node=menu_father_node=menu_node_ptr;
                        father_id[0]=0;
                        break;
                    }
                    else
                    {
                        strcpy(temp_str2,p);
                        q=strtok(temp_str2,".");
                        index2=0;
                        for(index2=0;index2<16;index2++)
                        {
                            father_id[index2]=0;
                        }
                        index2=0;
                        father_id[index2++]=atoi(q);
                        while((q=strtok(NULL,".")))
                        {  
                        	if(q!=NULL && isdigit(*q)) 
                            father_id[index2++]=atoi(q);
                          else if(*q!='\0')
                              break;                     
                        }
                        menu_father_node=search_menu_node(menu_root_node,father_id,index2);//(BYTE)((father_id-1)/2));
                        if(!menu_father_node)
                        {
                            printf("[Debug] Error: Can not find menu father node,line:%d\n",line_count);
                            return 0;
                        }
                        p=q;
                    }
                }
                break;
            case '(' :
                p++;
                if(isdigit(*(p)))
                {
                    if(group_type==0)
                    {
                        *(p+1)='\0';
                        sys_group=atoi(p);
                        group_type=1;
                        *p=';';
                    }
                    else if(group_type==1)
                    {
                        temp_str2[0]=*p;
                        temp_str2[1]='\0';                      
                        menu_node_ptr->sys_group=atoi(temp_str2);
                        if(menu_node_ptr->sys_group==0)
                        {
                           for(index1=0;index1<strlen(menu_node_ptr->name);index1++) 
                              if(menu_node_ptr->name[index1]=='_')
                                  break;
                           if(index1==strlen(menu_node_ptr->name))
                           {
                              printf("[Debug] Error: The menu item sysgroup for '8kE1' format must be '8KE1_*',line:%d\n",line_count);
                              return 0;
                           }
                        }
                        p+=2;
                    }

                }
                break;
            case 'M':  
            case 'm':
                menu_node_ptr=&menu_nodes[menu_node_index++];
                if(menu_father_node)
                    menu_node_ptr->father_ptr=menu_father_node;
                else
                {
                    printf("[Debug] Error:fail to create the menu tree,id %s,line:%d\n",temp_str3,line_count);
                    return 0;
                }
                if(index3<MENU_MAX_NODES)
                {
                    menu_father_node->child_node[index3]=menu_node_ptr;
                    index3++;
                    menu_father_node->total_menu_child=index3;

                }
                else
                {
                    printf("The child node is more than the max number!,line:%d\n",line_count);
                    return 0;
                }
                if(*(p+1)==':')
                {
                    p+=2;
                    for(index1=0;*(p+index1)!='\0';index1++)
                    {
                        if(*(p+index1)==' ')
                        {
                            break;
                        }
                    }
                }
                if(index1==0)
                {
                    printf("Format is not right,eg:  'M:13 name  {1.2};',line:%d\n",line_count);
                    return 0;
                }
                strncpy(temp_str2,p,index1);
                temp_str2[index1]='\0';
                for(index2=0;father_id[index2]!=0;index2++)
                    menu_node_ptr->menu_id[index2]=father_id[index2];
                menu_node_ptr->menu_id[index2]=strtoul(temp_str2, NULL, 10);
                menu_node_ptr->id_len=index2+1;
                if(group_type==1)
                    menu_node_ptr->sys_group=sys_group;
                p+=index1-1;
                break;
            case '[':
                q=p+1;
                index1=0;
                while(*p!=']' && *p!='\0')
                {
                    p++;
                    index1++;
                }
                if(*p==']')
                    strncpy(menu_node_ptr->name,q,index1-1);
                p++;
                break;
            case '{':
                p++;
                q=p;
                while(*p!='}' && *p!='\0')
                {
                    p++;
                    index1++;
                }
                if(*p=='}')
                    strncpy(oid,q,index1);
                p=oid;
                index2=0;
                for(index1=0;index1<10;index1++)
                    menu_node_ptr->oid[index1]=0;
                index1=0;
                while(*p!=';')
                {
                    if(isdigit(*(p+index1)))
                    {
                        index1++;
                        continue;
                    }
                    strncpy(temp_str2,p,index1);
                    temp_str2[index1]='\0';
                    menu_node_ptr->oid[index2++]=strtoul(temp_str2, NULL, 10);
                    p+=index1+1;
                    index1=0;
                }
                for(index1=0;index1<9;index1++)
                    full_oid[index1]=cur_oid[index1];
                for(index1=9;index1<19 && menu_node_ptr->oid[index1-9]!=0;index1++)
                    full_oid[index1]=(DWORD)menu_node_ptr->oid[index1-9];
                menu_node_ptr->menu_oid_node=search_mib_node(mib_tmp_node,full_oid,index1);
                if(!menu_node_ptr->menu_oid_node)
                {
                    printf("[debug] : can not find  mib node ,please check oid.menu_tree.txt,line:%d\r\n",line_count);
                    printf("oid: ");
                    for(index2=0;index2<index1;index2++)
                       printf("%u ",full_oid[index2]); 
                    printf("\r\n");
                    return 0;
                }
                *p=';';
                break;
            default :
                p++;
                break;
            }
        }
    }
    return 1;
}

///////////////////////////////End of Create Menu Tree/////////////////////






///////////////////////////////CMM Command//////////////////////////

/*
*
*       Fun:    debug_mmi_cmm_help
*
*       Desc:   display the commands and their usage in the command state
*
*       Ret:    RVOID
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
void debug_mmi_cmm_help(char *str)
{
    time_t	tick_2;
    char time_buf_2[64]= "\0",  str_buf[128]="\0";
    tick_2 = time(NULL);
    debug_ascii_out_proc("\033[2J\033[1;1H");
    snprintf(time_buf_2, sizeof(time_buf_2), "%.24s", ctime(&tick_2));
    sprintf(str_buf, "       -----Start Time: %s-----       \r\n", time_buf_2);
    debug_color_out(BRIGHT,CYAN,WHITE,str_buf);
    debug_ascii_out_proc("\r\n");
    debug_ascii_out_proc("\r\n");
    debug_ascii_out_proc("                           ");
    debug_ascii_out_proc("*************************\r\n");//80-20=60/2=30
    debug_ascii_out_proc("                            ");
    debug_ascii_out_proc("*");
    debug_ascii_out_proc(" ");
    debug_color_out(BRIGHT,WHITE,BLACK,"Command  Help  Menu");//80-20=60/2=30
    debug_ascii_out_proc(" ");
    debug_ascii_out_proc("*\r\n");
    debug_ascii_out_proc("                           ");
    debug_ascii_out_proc("*************************\r\n");//80-20=60/2=30
    debug_ascii_out_proc("\r\n");
    debug_ascii_out_proc("\r\n");
    debug_ascii_out_proc("================================================================================\r\n");
    debug_ascii_out_proc("|help                  : display this page                                     |\r\n");
    debug_ascii_out_proc("|log all/none          : turn on/off logs of modules exclude iptrans and snmp  |\r\n");
    debug_ascii_out_proc("|log error on/off      : turn on/off error logs of all modules                 |\r\n");
    debug_ascii_out_proc("|log status            : display what log option are opened                    |\r\n");
    debug_ascii_out_proc("|module                : send commands to sub module:IPTR,MTP3,SCCP,TCAP       |\r\n");
    debug_ascii_out_proc("|                        XAPP,MSC,VLR,SSF,HLR,SMSC,PPS,SNMP,M2UA,DEBUG,AUC,EIR |\r\n");
    debug_ascii_out_proc("|module module_command : execute module command for the specific module        |\r\n");
    debug_ascii_out_proc("|cfg                   : change to the state of configuration oid tree mode    |\r\n");
    debug_ascii_out_proc("|menu                  : change to the state of configuration menu tree mode   |\r\n");
    debug_ascii_out_proc("|x                     : back to main menu                                     |\r\n");
    debug_ascii_out_proc("|q                     : exit                                                  |\r\n");
    debug_ascii_out_proc("================================================================================\r\n");
    debug_color_out(BRIGHT,WHITE,BLACK,"-->Press \"Esc\" then \"Enter\"<--\r\n");
}	// end of debug_mmi_cmm_help


/*
*
*       Fun:    debug_mmi_log_status
*
*       Desc:   display the log status
*
*       Ret:    REVOID
*
*       File:   debug.c
*
*/
void debug_mmi_log_status()
{
    char   temp_str[256]="";
    int    i;

    if(debug_log_all == 1)
        strcat(temp_str, "        log all\r\n");
    else
        strcat(temp_str, "        log none \r\n");
    if(debug_log_error_on == 1)
        strcat(temp_str, "        log error on\r\n");
    else
        strcat(temp_str, "        log error off\r\n");
    debug_ascii_out_proc(temp_str);

    strcpy(temp_str, "lot status");
    for (i=0; i < MODULE_COUNT; i++)
    {
        if( (i != 13) && (debug_shm_ptr->module_d[i].ascii_in_set_flag == 1))
        {
            debug_shm_ptr->module_d[i].ascii_in[0] = 1;
            strcpy((char *)debug_shm_ptr->module_d[i].ascii_in+1,temp_str);
        }
    }
}	//	end of debug_mmi_log_status

/*
*
*       Fun:    debug_mmi_log
*
*       Desc:   send the log command(log_str) to all the other modules
*
*       Ret:    1: succeful, 0: fail
*
*       File:   debug.c
*
*/
int debug_mmi_log(char *log_str)
{
    char temp_str[128]="";
    int 	i;

    if( strstr(log_str, "all") !=  NULL )
    {
        strcpy(temp_str, "log all");
        debug_log_all = 1;
    }
    else if( strstr(log_str, "none") !=  NULL )
    {
        strcpy(temp_str, "log none");
        debug_log_all = 0;
    }
    else if( strstr(log_str, "error on") != NULL )
    {
        strcpy(temp_str, "log error on");
        debug_log_error_on = 1;
    }
    else if( strstr(log_str, "error off") != NULL )
    {
        strcpy(temp_str, "log error off");
        debug_log_error_on = 0;
    }
    else
        return 0;

    for(i=0; i < MODULE_COUNT; i++) 
    {
        if( (i !='n'-'a') && (debug_shm_ptr->module_d[i].ascii_in_set_flag == 1))
        {
            debug_shm_ptr->module_d[i].ascii_in[0] = 1;
            strcpy((char *)debug_shm_ptr->module_d[i].ascii_in+1,temp_str);
        }
    }
    return 1;
}	// end of debug_mmi_log
//////////////////////////////End of CMM Command/////////////////////////




///////////////////////////////Cfg OID Tree Command/////////////////


/*
*
*       Fun:    debug_mmi_cfg_help
*
*       Desc:   display the commands and their usage in the configure state
*
*       Ret:    RVOID
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
void debug_mmi_cfg_help(char *str)
{
    char out_text[128],*ch;
    BYTE i;
    for(i=0;i<80 && *(str+i)!='\0';i++)
    {
        if(*(str+i)!=' ')
            break;
    }
    ch=str+i;
    strcpy(out_text,ch);
    if(*out_text!='\0')
    {
        if(strncasecmp(out_text,"help",4)==0)
        {
            debug_ascii_out_proc(" Usage 1: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"help  \r\n");
            debug_ascii_out_proc("          Show the cfg main page\r\n");
            debug_ascii_out_proc(" Usage 1: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"help command\r\n");
            debug_ascii_out_proc("          Show the usage of the command\r\n");
        }
        else if(strncasecmp(out_text,"cd",2)==0)
        {
            debug_ascii_out_proc(" Usage 1: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"cd  \r\n");
            debug_ascii_out_proc("          Enter the root node:enterprises oid=1.3.6.1.4.1\r\n");
            debug_ascii_out_proc(" Usage 2: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"cd //\r\n");
            debug_ascii_out_proc("          Enter our system node:wxc2 oid=1.3.6.1.4.1.1373.1.3\r\n");
            debug_ascii_out_proc(" Usage 3: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"cd ....\r\n");
            debug_ascii_out_proc("          Enter the above node untill up to the roor node,the layer to go back \r\n");
            debug_ascii_out_proc("          can control by the point number(layers=point number-1,the max point \r\n");
            debug_ascii_out_proc("          number is 8) behind the 'cd'. eg:'cd ..' enter the parent node \r\n");
            debug_ascii_out_proc(" Usage 4: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"cd ./XXX or ./name\r\n");
            debug_ascii_out_proc("          Enter the child node which id or name is xxx or name\r\n");
            debug_ascii_out_proc(" Usage 5: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"cd ./X.X.X\r\n");
            debug_ascii_out_proc("          Enter the descendant's node whose oid is x.x.x from the current oid\r\n");
            debug_ascii_out_proc(" Usage 6: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"cd ../X.X.X\r\n");
            debug_ascii_out_proc("          Enter the descendant's node whose oid is x.x.x from the parent oid\r\n");
            debug_ascii_out_proc(" Usage 7: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"cd module\r\n");
            debug_ascii_out_proc("          Enter the module's node \r\n");
            debug_ascii_out_proc(" Usage 8: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"cd -name\r\n");
            debug_ascii_out_proc("          Find the node as the name from the current node\r\n");
            debug_ascii_out_proc(" Usage 9: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"cd --name\r\n");
            debug_ascii_out_proc("          Find the node as the name from the root node \r\n");
        }
        else if(strncasecmp(out_text,"get",3)==0)
        {
            help_cmm(1,"get   or get *","Get and printf the current instance's value of the node\r\n");
            help_cmm(2,"get #XX","Get and printf the special instance's value of the node\r\n");
            help_cmm(3,"get *.X or get *.name","Get and printf the current instance's special field value of the node\r\n");
            help_cmm(4,"get #X.X or get #x.name","Get and printf the special instance's special field value of the node\r\n");
        }
        else if(strncasecmp(out_text,"set",3)==0)
        {
            help_cmm(1,"set or set *","Set the current instance's value to default value of the node and open the isntance ,then printf\r\n");
            help_cmm(2,"set #XX ","Set the special instance's value to default value of the node and open the isntance ,then printf\r\n");
            help_cmm(3,"set *.X =X or set *.name=X ","Set the current instance's field value to given value of the node and open the isntance ,then printf\r\n");
            help_cmm(4,"set #X.X =X or set #x.name=X ","Set the special instance's field value to given value of the node and open the isntance ,then printf\r\n");
            help_cmm(5,"set #X =XXX","Set the special instance's whole value to given value of the node and open the isntance ,then printf\r\n");
        }
        else if(strncasecmp(out_text,"create",3)==0)
        {
            help_cmm(1,"creat or create *","Create an instance with the min closed instance id and set it's value to default value of the node and open the isntance ,then printf\r\n");
            help_cmm(2,"create #XX","Create the special instance,and set it's value to default value of the node and open the isntance ,then printf\r\n");
            help_cmm(3,"create *.X =X or create *.name=X ","Create an instance's with the min closed instance id ,and set it's field value to given value of the node and open the isntance ,then printf\r\n");
            help_cmm(4,"create #X.X =X or create #x.name=X ","Create the special instance,and set it's field value to given value of the node and open the isntance ,then printf\r\n");
            help_cmm(5,"create #X =XXX","Create the special instance,and set is's whole value to given value of the node and open the isntance ,then printf\r\n");
        }
        else if(strncasecmp(out_text,"delete",3)==0)
        {
            help_cmm(1,"delete  ","Delete the current instance ,if it is opened,clost it.set the value to default value of the node\r\n");
            help_cmm(2,"delete #XX","Delete the special instance,if it is opened,clost it.set the value to default value of the node\r\n");
        }
        else if(strncasecmp(out_text,"contents",3)==0)
        {
            help_cmm(1,"contents ","Show the information about the object .if it is parent node ,will print the name,the all child node.if it is leaf node ,will print the module,the read-write property,the type,the current instance(the mix id of the opened instance),the total instance,the all instance which are opened,and show the current isntance and all it's field .if it is parent node\r\n");
        }
        else if(strncasecmp(out_text,"list",3)==0)
        {
            help_cmm(1,"list [-a]","the switch of show all the field and value of the instance include the 'NULL' and the closed instance,default:off\r\n");
            help_cmm(2,"list [-e]","the switch of show opened the instance,hide the field and it's value default:off\r\n");
            help_cmm(3,"list [-r]","the switch of show the field data range.default:off\r\n");
            help_cmm(4,"list [-t]","the switch of show input data type ,default:off\r\n");
            help_cmm(5,"list [-f[X XX X]]","the switch of show the special field list in the [ ] ,default:off\r\n");
            help_cmm(6,"list [X-XX]","the switch of show the instance range form x to xxx.default:off\r\n");
        }
        else if(strncasecmp(out_text,"child",3)==0)
        {
            help_cmm(1,"child X","Enter the child node whose  id is X\r\n");
            help_cmm(2,"child name","Enter the child node whose name is 'name'\r\n");
        }
        else if(strncasecmp(out_text,"parent",3)==0)
        {
            help_cmm(1,"parent","Enter the parent node\r\n");
        }
        else if(strncasecmp(out_text,"pwd",3)==0)
        {
            help_cmm(1,"pwd","Show the full path and the full oid\r\n");
        }
        else if(strncasecmp(out_text,"remark",3)==0)
        {
            help_cmm(1,"remark","Show the description of the node\r\n");
        }
        else if(strncasecmp(out_text,"option",3)==0)
        {
            help_cmm(1,"option X","Show the select option of the fied node whose id is x of leaf node \r\n");
            help_cmm(2,"option name","Show the select option of the fied node whose name is ''name' of leaf node \r\n");
        }
        else if(strncasecmp(out_text,"save",3)==0)
        {
            help_cmm(1,"save","save the debug\r\n");
        }
        else if(strncasecmp(out_text,"quit",3)==0)
        {
            help_cmm(1,"quit","Quit and save teh debug\r\n");
        }
        else if(strncasecmp(out_text,"find host",3)==0)
        {
            help_cmm(1,"find host","Find all the host who have the object about the current node\r\n");
        }
        else if(strncasecmp(out_text,"select",3)==0)
        {
            help_cmm(1,"select X","Select the host ip list,if been selected ,the set command would do effect on the host\r\n");
        }
        else if(strncasecmp(out_text,"setinst",3)==0)
        {
            help_cmm(1,"setinst X","Set the current instance as the special number\r\n");
        }
        else if(strncasecmp(out_text,"cmm",3)==0)
        {
            help_cmm(1,"cmm","Enter the debug cmm state\r\n");
        }
        else if(strncasecmp(out_text,"addhost",3)==0)
        {}
        else if(strncasecmp(out_text,"delhost",3)==0)
        {}
        else if(strncasecmp(out_text,"tree",4)==0)
        {
            help_cmm(1,"tree","Print the three layer of the tree from the current node\r\n");
        }
        else     
        {
           sprintf(out_text,"Error   : Sorry, No support this command %s!\r\n" ,ch);
        	 debug_color_out(BRIGHT,RED,BLACK,out_text); 
        }
    }
    else
    {
        time_t	tick_2;
        char time_buf_2[64]= "\0",  str_buf[128]="\0";
        tick_2 = time(NULL);
        debug_ascii_out_proc("\033[2J\033[1;1H");
        snprintf(time_buf_2, sizeof(time_buf_2), "%.24s", ctime(&tick_2));
        sprintf(str_buf, "       -----Start Time: %s-----       \r\n", time_buf_2);
        debug_color_out(BRIGHT,CYAN,WHITE,str_buf);
        debug_ascii_out_proc("\r\n");
        debug_ascii_out_proc("                           ");
        debug_ascii_out_proc("*************************\r\n");//80-20=60/2=30
        debug_ascii_out_proc("                            ");
        debug_ascii_out_proc("*");
        debug_ascii_out_proc(" ");
        debug_color_out(BRIGHT,WHITE,BLACK,"Configure Help Menu");//80-20=60/2=30
        debug_ascii_out_proc(" ");
        debug_ascii_out_proc("*\r\n");
        debug_ascii_out_proc("                           ");
        debug_ascii_out_proc("*************************\r\n");//80-20=60/2=30
        debug_ascii_out_proc("\r\n");
        debug_ascii_out_proc("================================================================================\r\n");
        debug_ascii_out_proc("|help           :  show the configure command help or main cfg menu            |\r\n");
        debug_ascii_out_proc("|contents       :  show the contents of the current object                     |\r\n");
        debug_ascii_out_proc("|parent         :  enter the parent object of the current object               |\r\n");
        debug_ascii_out_proc("|child          :  entr the child object of the current object                 |\r\n");
        debug_ascii_out_proc("|cd             :  go to the object use name or oid or other control char      |\r\n");
        debug_ascii_out_proc("|pwd            :  show the current object path from the root object           |\r\n");
        debug_ascii_out_proc("|tree           :  show the object tree from the current object node           |\r\n");
        //debug_ascii_out_proc("|setins       :  set the instance number of the current object               |\r\n");
        //debug_ascii_out_proc("|find host           :  find the hosts in which the current node exist.        |\r\n");
        //debug_ascii_out_proc("|show host         :  show the hosts in which the current node exist.        |\r\n");
        debug_ascii_out_proc("|select         :  select the host which get and set command use.              |\r\n");
        debug_ascii_out_proc("|get            :  get the value of a paticular parameter field                |\r\n");
        debug_ascii_out_proc("|set            :  set the value of a paticular parameter field                |\r\n");
        debug_ascii_out_proc("|create         :  create and set the intance value of a parameter field       |\r\n");
        debug_ascii_out_proc("|delete         :  delete an opened instance of a parameter field              |\r\n");
        debug_ascii_out_proc("|cmm            :  change to the state of command                              |\r\n");
        debug_ascii_out_proc("|quit           :  exit the debug telnet interview                             |\r\n");
        debug_ascii_out_proc("================================================================================\r\n");
    }
}	// end of debug_mmi_cfg_help




/*
*
*       Fun:    debug_mmi_cd
*
*       Desc:   change to a mib_node by path
*
*       Ret:    return 1 if successful, return 0 if fail
*
*       File:   debug.c
*
*/
int debug_mmi_cd(char *path)
{
    BYTE			str_len,	index,	index_2,	index_3;
    mib_node		*find_node_ptr;
    mib_node_param  *find_node_ptr2;
    DWORD			OID[32];
    char			temp_str[128],	path_name[128], *temp_ch,temp_str2[128];
    int      temp;
    int       i;
    strcpy(path_name, path);
    temp_mib_node=mib_curr_node;
    for(i=0;i!=strlen(path_name);i++)
    {
        if(*(path+i)!=' ')
            break;
    }
    if(*(path_name+i)=='\0')
    {
        index_3=6;
        temp_mib_node=mib_curr_node;
        mib_curr_node= mib_root_node;
        if(curr_module!=mib_curr_node->module_index && save_flag==1)
            debug_mmi_save();
        else
            debug_mmi_contents("");
        //find_node_ptr=find_node(mib_root_node,"MTP3",0,0);
        //if(find_node_ptr!=NULL)
        //  mib_curr_node=find_node_ptr;

        return 1;
    }
    temp_ch=path_name+i;
    strcpy(path_name,temp_ch);
    strcpy(temp_str,path_name);
    for(i=0;i<strlen(temp_str);i++)
    {
        if(*(temp_str+i)=='-')
            break;
    }
    if(i!=strlen(temp_str))
    {
        temp_ch=temp_str+i+1;
        if(*(temp_ch+1)!=' ' && *(temp_ch+1)!='\0')
        {
            debug_color_out(BRIGHT,RED,BLACK,"Error   : input error\r\n");
            return 0;
        }
        *temp_str2=*temp_ch;
        *(temp_str2+1)='\0';
        for(temp=0;temp<i;temp++)
        {
            if(*(path_name+temp)!=' ')
                break;
        }
        if(i!=temp)
            strncpy(path_name,temp_str,i-1);
        else
        {
            if(strlen(temp_ch)>3)
                strcpy(path_name,temp_ch+2);
        }
        if(!isdigit(*temp_ch))
        {
            debug_color_out(BRIGHT,RED,BLACK,"Error   : Please input system number!\r\n");
            return 0;
        }
        host_group=atoi(temp_str2);
        if(host_group>GROUP_NUM-1)
        {
            sprintf(temp_str2,"Error   : Please input valid system id,the max is %d!\r\n",GROUP_NUM-1);
            debug_color_out(BRIGHT,RED,BLACK,temp_str2);
            return 0;
        }
    }




    if (!(str_len = strlen(path_name)))
    {
        debug_color_out(BRIGHT,RED,BLACK,"Fail    : The path you put is not right for current object!\r\n");
        return 0;
    }
    index=0;
    while(path_name[index]==' ')
    {
        temp=index+1;
        while(path_name[temp]!='\0')
        {
            path_name[temp-1]=path_name[temp];
            temp++;
        }
        path_name[temp-1]=path_name[temp];
    }
    temp=0;
    if(strncmp(path_name,"--",2)==0)//locate the para or node by name
    {
        temp=1;//find from the root node
    }
    else if(path_name[0]=='-')
    {
        temp=2;//find from the current node
    }
    if(temp>0)
    {
        if(strlen(path_name)>80)
            debug_color_out(BRIGHT,RED,BLACK,"Error   : no node have so long name!\r\n");
        if(temp==1)
        {
            mib_curr_node=mib_root_node;
            strcpy(temp_str,path_name+2);
        }
        if(temp==2)
            strcpy(temp_str,path_name+1);
        find_node_ptr= find_node(mib_curr_node, temp_str, 0, NULL);
        if(find_node_ptr!=NULL)
        {
            mib_curr_node=find_node_ptr;
            if(mib_curr_node->total_instance)
            {
                list_once=0;
                curr_module=mib_curr_node->module_index;
                register_instance();
            }
            if(curr_module!=mib_curr_node->module_index && save_flag==1)
                debug_mmi_save();
            else
                debug_mmi_contents("");
            return 1;
        }
        else
        {
            debug_color_out(BRIGHT,RED,BLACK,"Error   : There is no the special node of the name!\r\n");
            return 0;
        }
    }
    index=0;
    if(path_name[0]=='.' && path_name[1]=='.' && path_name[2]=='\0')
    {
        if(curr_module!=mib_curr_node->module_index && save_flag==1)
            debug_mmi_save();
        else
            debug_mmi_parent();
        return 1 ;
    }
    while(path_name[index]=='.')
        index++;
    if(index>2 && path_name[index]=='\0' && index<8 )
    {
        for(index_2=1;index_2<index;index_2++)
        {
            index_3=mib_curr_node->oid_len-1;
            if(index_3<=6)
            {
                debug_color_out(BRIGHT,RED,BLACK,"Error   : Have reached the root!\r\n");
                debug_mmi_contents("");
                return 0;
            }
            mib_curr_node= search_mib_node(mib_root_node, mib_curr_node->oid, index_3);
            if(mib_curr_node==mib_root_node)
            {
                debug_color_out(BRIGHT,RED,BLACK,"Fail    : Have reached the root node!\r\n");
                return 0;
            }

        }

        if(mib_curr_node->total_instance)
        {
            list_once=0;
            curr_module=mib_curr_node->module_index;
            register_instance();
            //for(i=0;i<=255;i++)
            //    if(curr_instance_arr[i].eable==1)
            //        break;
            // mib_curr_node->curr_instance=i;
            //debug_mmi_get_all();
        }
        if(curr_module!=mib_curr_node->module_index && save_flag==1)
            debug_mmi_save();
        else
            debug_mmi_contents("");
        return 1;
    }
    else if(index>8)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error    : Input error,the max layer number you can go back is 8.\r\n");
        return 0;
    }

    if(path_name[0]=='/' && path_name[1]=='/')
    {
        index_3=9;
        mib_curr_node= search_mib_node(mib_root_node, cur_oid, index_3);
        if(curr_module!=mib_curr_node->module_index && save_flag==1)
            debug_mmi_save();
        else
            debug_mmi_contents("");
        return 1;
    }

    if( (path_name[0] != '.') && ( !isdigit(path_name[0]) ) ) // path is module_name
    {

        index_2 = strlen(path_name);
        for(index=0; index<index_2; index++)
            path_name[index] = toupper(path_name[index]);
        for(i=0;i<MIB_HOST_MAX_NUM;i++)
            if( strncasecmp(subsys_module[i].name, path_name, strlen(path_name)) == 0) //find
            {
                break;
            }

        if(i== MIB_HOST_MAX_NUM)
        {
            sprintf(temp_str, "Fail    : Can't not find this node's by module name:%s.\r\n", path_name);
            debug_color_out(BRIGHT,RED,BLACK,temp_str);
            return 0;
        }
        for(index=0;index<32;index++)
            OID[index]=0;
        for(index=0; index<6; index++)
            OID[index]=root_oid[index];
        for(;index<14;index++)
            if(subsys_module[i].oid[index-6]!=0)
                OID[index]=subsys_module[i].oid[index-6];
            else
                break;
        index_3=index;
    }
    else
    {
        strcpy(temp_str, "");
        path[str_len] = '\0';

        if( (path_name[0]== '.') && (path_name[1] == '.') && (path_name[2] == '/') ) // path is ../oid, \u5C06\u5728\u5F53\u524D\u8282\u70B9\u7684\u7236\u8282\u70B9\u4E0B\u67E5\u627Eoid\u6240\u5BF9\u5E94\u7684object
        {
            for(index=0; index<32; index++)
                OID[index] = mib_curr_node->oid[index];
            index_3 = mib_curr_node->oid_len - 1;
            OID[index_3] = 0;
            index = 3;
        }
        else if( (path_name[0]== '.') && (path_name[1] == '/') )	// path is ./oid, \u5C06\u5728\u5F53\u524D\u76EE\u5F55\u4E0B\u67E5\u627Eoid\u6240\u5BF9\u5E94\u7684object
        {
            for(index=0; index<32; index++)
                OID[index] = mib_curr_node->oid[index];
            index_3 = mib_curr_node->oid_len;
            index = 2;
        }
        else
        {
            OID[0]=1;
            OID[1]=3;
            OID[2]=6;
            OID[3]=1;
            OID[4]=4;
            OID[5]=1;
            OID[6]=1373;
            for(index=7; index<32; index++)
                OID[index]=0;
            index_3 = 7;
            index = 0;
        }
        str_len = strlen(path_name);
        for(index_2=0; index<str_len; index++)
        {
            if(path_name[index] != '.')
            {
                temp_str[index_2] = path_name[index];
                index_2++;
            }
            else
            {
                temp_str[index_2] = '\0';
                index_2 = 0;
                OID[index_3++] = atoi(temp_str);
            }
        }
        temp_str[index_2] = '\0';
        index_2 = 0;
        OID[index_3++] = atoi(temp_str);
    }
    find_node_ptr2 =(mib_node_param*)search_mib_node(mib_root_node, OID, index_3);

    if(find_node_ptr2== NULL)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Fail    : No such object\r\n");
        return 0;
    }
    else
    {
        find_node_ptr=find_node_ptr2->parent_ptr;
        if(find_node_ptr!=NULL)
        {
            if (find_node_ptr->total_instance)
            {
                if(strlen(path_name)>4)
                    mib_curr_node=find_node_ptr->parent_ptr;
                debug_color_out(BRIGHT,RED,BLACK,"Error   : Have reach the leaf node!\r\n");
                return 0;
            }
            else
                mib_curr_node = (mib_node*)find_node_ptr2;
        }
    }

    if(mib_curr_node->total_instance)
    {
        list_once=0;

        curr_module=mib_curr_node->module_index;
        register_instance();
        //for(i=0;i<=255;i++)
        //{
        //    if(curr_instance_arr[i].eable==1)
        //        break;
        //}
        //mib_curr_node->curr_instance=i;
        //debug_mmi_get_all();
    }
    //debug_ascii_out_proc("Current object changed to: ");
    //debug_color_out(BRIGHT,YELLOW,BLACK,mib_curr_node->name);
    //debug_ascii_out_proc("\r\n");
    if(curr_module!=mib_curr_node->module_index && save_flag==1)
        debug_mmi_save();
    else
        debug_mmi_contents("");
    return 1;
}	// end of debug_mmi_cd

/*
*
*       Fun:    debug_mmi_contents
*
*       Desc:   show the current mib_node's all information such as name, path, child nodes and so on
*
*       Ret:    1: succeful, 0: fail
*
*       File:   debug.c 
*
*/
int debug_mmi_contents(char *str)
{
    WORD 			i,j;
    DWORD     k;
    char			ascii_text[256*5];
    //char      temp_str[64];
    mib_child_node	*child_node_ptr;
    mib_node        *mib_tmp_node;
    char      ins_str[16],temp_str[128];
    BYTE      max_group,ismodule;
 
    if(strlen(str)!=0)
    {
        for(i=0;i<strlen(str);i++)
        {
            if(*(str+i)=='-')
                break;
        }
        if(i!=strlen(str))
            if(isdigit(*(str+i+1)) && !isdigit(*(str+i+2)))
            {
                ins_str[0]=*(str+i+1);
                ins_str[1]='\0';
                host_group=atoi(ins_str);
                list_once=0;
                register_instance();
            }
    }
    if (!mib_curr_node->total_child_nodes)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Fail    : No contents in this object,no child,Please go back!     \r\n");
        return 0;
    }
    debug_ascii_out_proc("Name    : ");
    sprintf(ascii_text,"%s",mib_curr_node->name);
    debug_color_out(BRIGHT,YELLOW,BLACK,ascii_text);
    debug_ascii_out_proc("\r\n");
    debug_ascii_out_proc("Type    : ");
    if(mib_curr_node->total_instance)
    {
        debug_mmi_find_host(""); 
    }
    if(mib_curr_node->total_instance)
    {
        debug_color_out(BRIGHT,YELLOW,BLACK,"Leaf node(");
        debug_color_out(BRIGHT,YELLOW,BLACK,max_access[mib_curr_node->max_access]);
        debug_color_out(BRIGHT,YELLOW,BLACK,")");
    }
    else
        debug_color_out(BRIGHT,YELLOW,BLACK,"Parent node");
    debug_ascii_out_proc("\r\n");

    if(debug_cfg==1)
    {
	    if(mib_curr_node->module_index!=0xFF)
	    {
	        max_group=subsys_module[mib_curr_node->module_index].max_group;
	        if(host_group<=max_group)
	        {
	            debug_ascii_out_proc("Module  : ");
	            sprintf(temp_str,"%s-%d\r\n",subsys_module[mib_curr_node->module_index].name,host_group);
	            debug_color_out(BRIGHT,YELLOW,BLACK,temp_str);
	        }
	        else
	        {
	            debug_ascii_out_proc("Module  : ");
	            sprintf(temp_str,"%s-0\r\n",subsys_module[mib_curr_node->module_index].name);
	            debug_color_out(BRIGHT,YELLOW,BLACK,temp_str);
	            sprintf(temp_str,"Error   : the max system id is %d.\r\n",max_group);
	            debug_color_out(BRIGHT,RED,BLACK,temp_str);
	        }                                              
	        }
	  }
	  else if(debug_cfg==2)
	  {
	  	    if(menu_curr_node->sys_group==9)
	  	    {
	  	    	debug_ascii_out_proc("Module  : ");
	  	    	for(i=0;i<ip_num;i++)
	  	    	{
	            sprintf(temp_str,"%s-%d ",subsys_module[mib_curr_node->module_index].name,i);
	            debug_color_out(BRIGHT,YELLOW,BLACK,temp_str);
	  	    	}
	  	    	debug_ascii_out_proc("\r\n");
	  	    }
	  	    else 
	  	    {
	  	    	   debug_ascii_out_proc("Module  : "); 
		           sprintf(temp_str,"%s-%d",subsys_module[mib_curr_node->module_index].name,menu_curr_node->sys_group);
		           debug_color_out(BRIGHT,YELLOW,BLACK,temp_str);
	  	    	   debug_ascii_out_proc("\r\n");	  	   
	  	    }
	  	   
	  	   
	  }

    if(!mib_curr_node->total_instance)
    {
        debug_ascii_out_proc("Child   : ");
        sprintf(temp_str, "[%d]\r\n",mib_curr_node->total_child_nodes);
        debug_color_out(BRIGHT,YELLOW,BLACK,temp_str);
        max_group=0;
        for (i = 0; i < mib_curr_node->total_child_nodes; i++)
        {
            child_node_ptr = mib_curr_node->child_nodes[i];
            if(!child_node_ptr)
                continue;
            mib_tmp_node=(mib_node*)child_node_ptr->child_ptr;
            if(mib_tmp_node!=NULL)
            {
                max_group=subsys_module[mib_tmp_node->module_index].max_group;
            }
            if(strncmp(subsys_module[mib_tmp_node->module_index].name,mib_tmp_node->name,strlen(mib_tmp_node->name))==0)
                ismodule=1;
            else
                ismodule=0;
            if(ismodule==0 || max_group==0)
            {
                sprintf(temp_str, "           |---(%u) %s\r\n", child_node_ptr->oid,child_node_ptr->name);
                debug_color_out(BRIGHT,YELLOW,BLACK,temp_str);
            }
            else
            {
                j=0;
                while(max_group>0)
                {
                    sprintf(temp_str, "           |---(%u) %s-%d\r\n", child_node_ptr->oid,child_node_ptr->name,j);
                    debug_color_out(BRIGHT,YELLOW,BLACK,temp_str);
                    j++;
                    if(j==max_group+1)
                        break;
                }
            }
        }
    }
    else
    {
        if(list_once==0)
            list_flag=1;
        debug_cfg_end=0;
    }
    if(list_once==1)
    {

        if (mib_curr_node->total_instance)
        {
            strcpy(ascii_text,"");
            k=0;
            for(i=0;i<=255;i++)
            {
                if(curr_instance_arr[i].eable==1) 
                {
                    sprintf(ins_str,"#%-3d ",i);                   
                    strpcat(ascii_text,&k,ins_str);
                }
            } 
            debug_ascii_out_proc("Instance: ");
            strcpy(ins_str,"");
            if(strlen(ascii_text))
                sprintf(ins_str, "#%-3d(%-3d): ", mib_curr_node->curr_instance,mib_curr_node->total_instance);
            else
                sprintf(ins_str, "None(%-3d): ", mib_curr_node->total_instance);
            debug_color_out(BRIGHT,YELLOW,BLACK,ins_str);
            //formatstr(10,ascii_text,1);//,(int)ceil(strlen(ascii_text)/70)+1);
            debug_color_out(BRIGHT,BLUE,BLACK,ascii_text);
            debug_ascii_out_proc("\r\n");
            if(strlen(ascii_text))
            {
                none_instance=0;
            }
            else
                none_instance=1;
            if(none_instance==0)
            {
                if(mib_curr_node->max_access>=2 && mib_curr_node->max_access<=5)
                {
                    IP_Selected[0]=1;  
                    for(i=1;i<16;i++)
                    {
                        IP_Selected[i]=0;
                    }
                    debug_cfg_end=0;
                    debug_mmi_get_all();
                    debug_cfg_end=0;
                }
            }
            else
            {

                strcpy(ascii_text,"");
                debug_ascii_out_proc("Child   : ");
                sprintf(ascii_text, "[%d]\r\n",mib_curr_node->total_child_nodes);
                debug_color_out(BRIGHT,YELLOW,BLACK,ascii_text);

                for (i = 0; i < mib_curr_node->total_child_nodes; i++)
                {
                    child_node_ptr = mib_curr_node->child_nodes[i];
                    if(!child_node_ptr)
                       continue;
                    sprintf(ascii_text, "           |---(%u) %s\r\n",child_node_ptr->oid+1, child_node_ptr->name);
                    debug_color_out(BRIGHT,YELLOW,BLACK,ascii_text);
                }
                debug_cfg_end=1;
            }
        }
    }
    
    /*
    if(!mib_curr_node->total_instance)
    {
       debug_ascii_out_proc("Command : Now,you can execute the commands include "); 
       debug_color_out(BRIGHT,YELLOW,BLACK,"\"cd ,child ,parent ,contnets           ,pwd ,tree ,remark ,");
    }
     else 
     {
     	 debug_ascii_out_proc("Command : Now,you can execute the commands include "); 
       debug_color_out(BRIGHT,YELLOW,BLACK,"\"cd ,parent ,contnets ,pwd ,            tree ,get ,set ,list ,create ,del ,remark ,");
     }
     if(save_flag==1)
        debug_color_out(BRIGHT,YELLOW,BLACK,"save ,cmm ,q ,help .\"\r\n");
     else
        debug_color_out(BRIGHT,YELLOW,BLACK,"cmm ,q ,help .\"\r\n");
    */
    return 1;
}
// end of debug_mmi_contents


/*
*
*       Fun:    debug_mmi_child 
*
*       Desc:   change to the current mib_node's child node by the child node's name or id if it exists
*
*       Ret:    1: succeful, 0: fail
*
*       File:   debug.c
*
*/
int debug_mmi_child(char *name)
{
    BYTE			str_len;
    WORD 			child_index;
    WORD			oid=0;
    mib_child_node	*child_node_ptr;

    if (mib_curr_node->total_instance)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Fail    : The current object is a leaf node, can not change to its child node.\r\n");
        return 0;
    } 

    if (!(str_len = strlen(name)))
    {
        debug_color_out(BRIGHT,RED,BLACK,"Fail    : No such child object for the current object\r\n");
        return 0;
    }

    if (!mib_curr_node->total_child_nodes)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Fail    : No child object for the current object\r\n");
        return 0;
    }

    name[str_len] = '\0';

    if (isdigit(name[0]))
    {
        oid = strtoul(name, NULL, 10);
        child_index = find_child_index(mib_curr_node, oid, NULL, 0);
    }
    else
        child_index = find_child_index(mib_curr_node, 0, name, 1);

    if (child_index  < MIB_MAX_CHILD_NODES_PER_NODE)
    {
        child_node_ptr = mib_curr_node->child_nodes[child_index];
        if(child_node_ptr)
        {
	        if (!child_node_ptr->child_ptr)
	        {
	            debug_color_out(BRIGHT,RED,BLACK,"Fail    : No such child object for the current object\r\n");
	            return 0;
	        }
	        mib_curr_node = (mib_node *)child_node_ptr->child_ptr;
	        //debug_ascii_out_proc("Current object changed to: ");
	        //debug_color_out(BRIGHT,YELLOW,BLACK,mib_curr_node->name);
	        //debug_ascii_out_proc("\r\n");
	        debug_mmi_contents("");
	        return 1;
       }       
    }

    debug_color_out(BRIGHT,RED,BLACK,"Error  : No such child object for the current object\r\n");
    return 0;
}	// end of debug_mmi_child
 
/*
*
*       Fun:    debug_mmi_parent
*
*       Desc:   changet to the current mib_node's parent node if it exists
*
*       Ret:    1: succeful, 0: fail
*
*       File:   debug.c
*
*/
int debug_mmi_parent()
{
    if (mib_curr_node->parent_ptr == NULL)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error   : No parent object for the current object\r\n");
        return 0;
    }

    mib_curr_node = (mib_node *) mib_curr_node->parent_ptr;
    //debug_ascii_out_proc("Current object changed to: ");
    //debug_color_out(BRIGHT,YELLOW,BLACK,mib_curr_node->name);
    //debug_ascii_out_proc("\r\n");
    debug_mmi_contents("");
    return 1;
}// end of debug_mmi_parent

/*
*
*       Fun:    debug_mmi_pwd
*
*       Desc:   show the current object path
*
*       Ret:    return 1 if successful, return 0 if fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
int debug_mmi_pwd()
{
    mib_node 		*node_ptr;
    mib_child_node	*child_node_ptr = NULL;
    BYTE			i, loop_times;
    WORD			child_index;
    char      tem_str[128], text_string[1024], text_oid[128];
    char			ascii_text[DEBUG_OMCBUF_LEN],oid[16];
    //DWORD     temp_oid[32]={1,3,6,1,4,1,1373,1,3,3,2,2,2,1,2,0};

     
    /*if(!ShowRegister)
    {
     	  for(i=0;i<16;i++)
     	  {
     	  	printf("id:%d,Module name: %s,ascii_in: 0X%ld,ascii_out:0X%ld\r\n",i,debug_shm_ptr->module_d[i].name,&(debug_shm_ptr->module_d[i].ascii_in),&(debug_shm_ptr->module_d[i].ascii_out));
     	  }
     	  ShowRegister=1;
     } 
     */ 
    if ((loop_times = mib_curr_node->oid_len - mib_root_node->oid_len) >= 15)
    {
        sprintf(tem_str, "Object path error for the current object\r\n");
        TxtColor(BRIGHT,RED,BLACK,tem_str);
        debug_ascii_out_proc(tem_str);
        return 0;
    }
    sprintf(text_string,"%s(%u)", mib_root_node->name,mib_root_node->oid[mib_root_node->oid_len-1]);
    node_ptr = mib_root_node;
    for (i = 0; i < loop_times; i++)
    {
        if ((child_index = find_child_index(node_ptr, mib_curr_node->oid[mib_root_node->oid_len + i], NULL, 0)) >= MIB_MAX_CHILD_NODES_PER_NODE)
        {
            sprintf(tem_str,"Object path error for the current object\r\n");
            TxtColor(BRIGHT,RED,BLACK,tem_str);
            debug_ascii_out_proc(tem_str);
            return 0;
        }
        child_node_ptr = node_ptr->child_nodes[child_index];
        if(child_node_ptr)
        {
	        sprintf(text_oid, ".%s(%u)", child_node_ptr->name, child_node_ptr->oid);
	        strcat(text_string, text_oid);
	
	        if (i != (loop_times - 1))
	        {
	            if (child_node_ptr->child_ptr == NULL)
	            {
	                debug_color_out(BRIGHT,RED,BLACK,"Object path error for the current object\r\n");
	                return 0;
	            }
	            node_ptr = (mib_node *) child_node_ptr->child_ptr;
	        }
	     }
    }
    debug_ascii_out_proc("Path    : ");
    formatstr(10,text_string,0);//,(int)ceil(strlen(text_string)/70)+1);
    debug_color_out(BRIGHT,YELLOW,BLACK,text_string);
    debug_ascii_out_proc("\r\n");
    strcpy(ascii_text, "\0");
    for (i = 0; i < (mib_curr_node->oid_len - 1); i++)
    {
        sprintf(oid, "%u.", mib_curr_node->oid[i]);
        strcat(ascii_text, oid);
    }
    sprintf(oid, "%u", mib_curr_node->oid[mib_curr_node->oid_len - 1]);
    strcat(ascii_text, oid);
    debug_ascii_out_proc("OID     : ");
    debug_color_out(BRIGHT,YELLOW,BLACK,ascii_text);
    debug_ascii_out_proc("\r\n");
    /*
    for(i=0;i<32;i++)
        snmp_agent_packet1.oid[i]=temp_oid[i];
    snmp_agent_packet1.oid_len=16;
    snmp_agent_packet1.total_ip=1;
    strcpy(snmp_agent_packet1.dst_ip[0],"172.18.144.1");
    snmp_agent_packet1.cmm_state=1;
    snmp_agent_packet1.total_instance=255;
    for(i=0;i<MAX_PACKET_NUM;i++)
        if(debug_snmp_agent_buf[i].cmm_state==0)
            break;
    if(i!=MAX_PACKET_NUM)
    {
        debug_agent_index=i;
        memcpy(&debug_snmp_agent_buf[debug_agent_index],&snmp_agent_packet1,sizeof(debug_snmp_agent));
    }
    else
        printf("Buffer is full!\r\n");
    */
    /*
    for(i=0;i<32;i++)
        snmp_agent_packet1.oid[i]=temp_oid[i];
    snmp_agent_packet1.oid_len=16;
    snmp_agent_packet1.total_ip=1;
    snmp_agent_packet1.total_field=15;
    strcpy(snmp_agent_packet1.dst_ip[0],"172.18.144.1");
    strcpy(snmp_agent_packet1.param_field_arr[0].curr_input_value,"");
    snmp_agent_packet1.param_field_arr[0].total_select=6;
    snmp_agent_packet1.param_field_arr[0].curr_select_id=3;
    strcpy(snmp_agent_packet1.param_field_arr[1].curr_input_value,"");
    snmp_agent_packet1.param_field_arr[1].total_select=6;
    snmp_agent_packet1.param_field_arr[1].curr_select_id=3;
    strcpy(snmp_agent_packet1.param_field_arr[2].curr_input_value,"");
    snmp_agent_packet1.param_field_arr[2].total_select=6;
    snmp_agent_packet1.param_field_arr[2].curr_select_id=3;
    strcpy(snmp_agent_packet1.param_field_arr[9].curr_input_value,"");
    snmp_agent_packet1.param_field_arr[9].total_select=2;
    snmp_agent_packet1.param_field_arr[9].curr_select_id=1;
    */
    /*
    snmp_agent_packet1.cmm_state=1;  
    for(i=0;i<MAX_PACKET_NUM;i++)
        if(debug_snmp_agent_buf[i].cmm_state==0)
          break;
    if(i!=MAX_PACKET_NUM) 
    {
       debug_agent_index=i;
       memcpy(&debug_snmp_agent_buf[debug_agent_index],&snmp_agent_packet1,sizeof(debug_snmp_agent)); 
    }
    else
       printf("Buffer is full!\r\n");
    */ 
    return 1;
}// end of debug_mmi_pwd

/*
*
*       Fun:    debug_mmi_tree
*
*       Desc:   show the simple mib_tree including the next node_levels nodes start from the node_ptr whit the separation as a class sign
*
*       Ret:    RETVOID
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
void debug_mmi_tree(mib_node *node_ptr,char *separation, BYTE node_levels)
{
    int			index,	length;
    int layer=node_levels;
    mib_node 	*child_node_ptr;
    char		spaces[256],	separation2[256],  	print_buf[256];
    mib_child_node *mib_child_node_ptr;
    if((layer-1) < 0)
    {
        return;
    }
    if(treehost==1)
    {
        /*
        if(node_levels>=1 && node_levels<=8)
        layer=node_levels;
        else
        {
        layer=3;
        debug_ascii_out_proc("the max layer number is eight!\r\n");
        }
        	*/
        debug_ascii_out_proc("Layers  : ");
        sprintf(print_buf,"%d-%d\r\n",mib_curr_node->oid_len,mib_curr_node->oid_len+layer);
        debug_color_out(BRIGHT,GREEN,BLACK,print_buf);
        debug_ascii_out_proc("Host OID: ");
        sprintf(print_buf,"%d",(int)mib_curr_node->oid[0]);
        debug_color_out(BRIGHT,GREEN,BLACK,print_buf);
        for(index=1; index<mib_curr_node->oid_len-1; index++) 																																				//                      ."id"."id"...
        {
            sprintf(print_buf, ".%d",(int)mib_curr_node->oid[index]);
            TxtColor(BRIGHT,GREEN,BLACK,print_buf);
            debug_ascii_out_proc(print_buf);
        }
        debug_ascii_out_proc("\r\n");
    }
    treehost=0;
    strcpy(separation2, separation);
    length = strlen(separation2);
    if(node_ptr == NULL)
    {
        debug_ascii_out_proc("\r\n");
        return;
    }
    for(index=0; index<length+4; index++)
    {
        spaces[index] = ' ';
    }
    spaces[index]='\0';
    spaces[index-1]='>';
    spaces[index-2]='<';
    spaces[index-3]='-';
    spaces[index-4]='|';
    spaces[1]= '|';
    sprintf(print_buf,"%s%s(%d)\r\n",separation2, node_ptr->name, (int)node_ptr->oid[node_ptr->oid_len-1]);//[][][]node name: "node name"
    debug_color_out(BRIGHT,YELLOW,BLACK,print_buf);
    if( node_ptr->total_child_nodes == 0 )
        return;

    if(node_ptr->total_instance != 0)
    {
        for(index = 0; index < (node_ptr->total_child_nodes); index++)
        {
            mib_node_param 	*mib_node_param_ptr;
            mib_child_node_ptr=node_ptr->child_nodes[index];
            if(!mib_child_node_ptr)
                 continue;
            mib_node_param_ptr = (mib_node_param *)mib_child_node_ptr->child_ptr;																		//        id:"id",name: "name",byte_begin.bit_begin-byte_end.bit_end
            sprintf(print_buf, "%s  id:%d; %s; [%d.%d-%d.%d]\r\n", spaces, (int)mib_node_param_ptr->id+1, mib_node_param_ptr->name, (int)mib_node_param_ptr->byte_begin, (int)mib_node_param_ptr->bit_begin, (int)mib_node_param_ptr->byte_end, (int)mib_node_param_ptr->bit_end);
            debug_color_out(BRIGHT,YELLOW,BLACK,print_buf);
        }
        return;
    }
    spaces[index-1]='-';
    spaces[index-2]='*';
    spaces[index-3]='-';
    spaces[index-4]='|';
    for(index = 0; index < node_ptr->total_child_nodes; index++)
    {
    	mib_child_node_ptr=node_ptr->child_nodes[index];
        if(!mib_child_node_ptr)
                 continue;
        sprintf(print_buf, "%s  %s(%d)\r\n",spaces,  mib_child_node_ptr->name,(int)mib_child_node_ptr->oid);
        TxtColor(BRIGHT,YELLOW,BLACK,print_buf);
        debug_ascii_out_proc(print_buf);
    }
    sprintf(print_buf,"{%d}--",(int)node_ptr->oid[node_ptr->oid_len-1]);
    strcat(separation2, print_buf);
    for(index = 0; index < node_ptr->total_child_nodes; index++)
    {
    	mib_child_node_ptr=node_ptr->child_nodes[index];
    	if(!mib_child_node_ptr)
    	   continue;
        child_node_ptr = mib_child_node_ptr->child_ptr;
        debug_mmi_tree(child_node_ptr,separation2, layer-1);
    }
    debug_ascii_out_proc("\r\n");
}	// end of debug_mmi_tree

void debug_mmi_create(char *str)
{
    //DWORD        oid[20];
    BYTE         i;
    BYTE         byte,bit;
    char         str_id[10];
    //char         str_value[128];
    char         temp_str[256];
    char         *p;
    int          id;
    //mib_node     *temp_node;
    save_flag=1;
    strcpy(str_id,"");
    //strcpy(str_value,"");
    for(i=0;i!=strlen(str);i++)
    {
        if(*(str+i)!=' ')
            break;
    }
    p=str+i;
    strcpy(str,p);
    if(str[0]=='\0')
    {
        if(mib_curr_node->enable_field_id==0xFF)
        {
            debug_color_out(BRIGHT,RED,BLACK,"Error   : Please input the instance value for the special input type!\r\n");
            return ;
        }
        for(id=0;id<255;id++)
            if(curr_instance_arr[id].eable==0)
            {
                curr_instance_arr[id].eable=1; 
                break;
            }
        byte=(id)/8;
        bit=(id)%8;
        mib_curr_node->enable_flag[byte]=mib_curr_node->enable_flag[byte] | (BYTE)my_pow(2,bit);
        mib_curr_node->curr_instance=id;
        debug_mmi_set("*");
        //debug_mmi_contents("");
        if(mib_curr_node->enable_flag[byte] & (BYTE)my_pow(2,bit))
        {
            debug_ascii_out_proc("State   : ");
            sprintf(temp_str,"The %s #%d have been created successfully!\r\n",mib_curr_node->name,id);
            debug_color_out(BRIGHT,CYAN,BLACK,temp_str);
        }
        return;
    }
    else if(str[0]=='#')
    {
        p=str+1;
        strcpy(str,p);
        for(i=0;i<strlen(str);i++)
        {
            if(!isdigit(*(str+i)))
            {
                break;
            }
            else
                str_id[i]=str[i];
        }
        str_id[i]='\0';
        if(*(str+i)=='\0' || (*(str+i)=='=' && *(str+i)=='\0'))
            if(mib_curr_node->enable_field_id==0xFF)
            {
                debug_color_out(BRIGHT,RED,BLACK,"Error   : Please input the value !\r\n");
                return ;
            }
        id=strtol(str_id,NULL,10);
        if(id>mib_curr_node->total_instance-1)
        {
            sprintf(temp_str,"Error   : The max instance id is %d\r\n",mib_curr_node->total_instance-1);
            debug_color_out(BRIGHT,RED,BLACK,temp_str);
            return;
        }
        /*
            for(i=0;i<strlen(str);i++)
            {
                if(!isdigit(*(str+i)) && *(str+i+1)!='=')
                {
                    debug_color_out(BRIGHT,RED,BLACK,"Error   : Input error. Fromat is:create XXX=XXX!");
                    return;
                }
                if(*(str+i)=='=')
                    if(i>0)
                    {
                        strncpy(str_id,str,i);
                        break;
                    }
            }
            
            if(curr_instance_arr[id].eable==1)
            {
                debug_color_out(BRIGHT,RED,BLACK,"Erorr   : the instance is not exist!please select another id!\r\n");
                return;
            }
            p=str+i;
            strcpy(str_value,p);
         
            for(i=0;i<strlen(str_value);i++)
            {
                if(isdigit(*(str_value+i))||( *(str_value+i)>96 && *(str_value+i)<103) || (*(str_value+i)>64 && *(str_value+i)<72))//other flag||0-f
                    ;
                else
                {
                    debug_color_out(BRIGHT,RED,BLACK,"Error   : Please in put Value form 0-f!\r\n");
                    return ;
                }
            }
            //for(i=0;i<mib_curr_node->oid_len;i++)
             //   oid[i]=mib_curr_node->oid[i];
            //oid[i+1]=id;
            //temp_node=search_mib_node(mib_root_node, oid, i+1);
            //if(!temp_node)
            //{
               // mib_curr_node=temp_node;
        */

        if(curr_instance_arr[id].eable==1)
        {
            debug_color_out(BRIGHT,RED,BLACK,"Error   : The instance have exist!\r\n");
            return;
        }
        else
        {
            if(mib_curr_node->enable_field_id!=0xFF)
            {
                byte=(id)/8;
                bit=(id)%8;
                mib_curr_node->enable_flag[byte]=mib_curr_node->enable_flag[byte] | ((BYTE)my_pow(2,bit));
            }
            curr_instance_arr[id].eable=1;

        }
        temp_instance=mib_curr_node->curr_instance;
        mib_curr_node->curr_instance=id;

        if(*(str+i)=='\0')
        {

            debug_mmi_set("*");
            //debug_mmi_contents("");
            mib_curr_node->curr_instance=temp_instance;
            if(curr_instance_arr[id].eable==1)
            {
                debug_ascii_out_proc("State   : ");
                sprintf(temp_str,"The %s #%d have been created successfully!\r\n",mib_curr_node->name,id);
                debug_color_out(BRIGHT,CYAN,BLACK,temp_str);
            }
            return;
        }
        else if(*(str+i)=='=')
        {
            p=str+i+1;
            strcpy(str,p);
            if(strlen(str)>254)
            {
                str[255]='\0';
            }
            strcpy(temp_str,"*=");
            strcat(temp_str,str);
            debug_mmi_set(temp_str);
            //debug_mmi_contents("");
            mib_curr_node->curr_instance=temp_instance;
            if(curr_instance_arr[id].eable==1)
            {
                debug_ascii_out_proc("State   : ");
                sprintf(temp_str,"The %s #%d have been created successfully!\r\n",mib_curr_node->name,id);
                debug_color_out(BRIGHT,CYAN,BLACK,temp_str);
            }
            return;
        }
        else if(*(str+i)=='.')
        {
            p=str+i+1;
            strcpy(str,p);
            if(strlen(str)>254)
            {
                str[255]='\0';
            }
            strcpy(temp_str,"*.");
            strcat(temp_str,str);
            debug_mmi_set(temp_str);
            //debug_mmi_contents("");
            mib_curr_node->curr_instance=temp_instance;
            if(curr_instance_arr[id].eable==1)
            {
                debug_ascii_out_proc("State   : ");
                sprintf(temp_str,"The %s #%d have been created successfully!\r\n",mib_curr_node->name,id);
                debug_color_out(BRIGHT,CYAN,BLACK,temp_str);
            }
            return;
        }
    }
    else
        debug_color_out(BRIGHT,RED,BLACK,"Error   : Input error!\r\n");
}

void debug_mmi_del(char *str)
{
    //DWORD        oid[20];
    int         i;
    BYTE        byte,bit;
    char         str_id[128];
    char         str_value[128];
    char         *p;
    int          id;
    //mib_node     *temp_node;
    save_flag=1;
    strcpy(str_id,"");
    strcpy(str_value,""); 
    for(i=0;i!=strlen(str);i++)
    {
        if(*(str+i)!=' ')
            break;
    }
    p=str+i;
    strcpy(str,p);
    if(str[0]!='\0' && str[0]!='#')
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error   : Input Error!\r\n");
        return;
    }

    if(strlen(str)==0)
    {
        id=mib_curr_node->curr_instance;
    }
    else if(str[0]=='#')
    {
        p=str+1;
        strcpy(str,p);
        for(i=0;i<strlen(str);i++)
            if(!isdigit(*(str+i)))
            {
                debug_color_out(BRIGHT,RED,BLACK,"Error   : Input error. \r\n");
                return;
            }
            else
                str_id[i]=str[i];
        id=strtol(str_id,NULL,10);
        if(id>mib_curr_node->total_instance-1)
        {
            sprintf(str_value,"Error   : The max instance id is %d\r\n",mib_curr_node->total_instance-1);
            debug_color_out(BRIGHT,RED,BLACK,str_value);
            return;
        }
    }
    /*
        for(i=0;i<strlen(str);i++)
        {
            if(!isdigit(*(str+i)) && *(str+i+1)!='=')
            {
                debug_color_out(BRIGHT,RED,BLACK,"Error   : Input error. Fromat is:create XXX=XXX!");
                return;
            }
            if(*(str+i)=='=')
                if(i>0)
                {
                    strncpy(str_id,str,i); 
                    break;
                }
        }
        
        if(curr_instance_arr[id].eable==1) 
        {
            debug_color_out(BRIGHT,RED,BLACK,"Erorr   : the instance is not exist!please select another id!\r\n");
            return;
        }
        p=str+i;
        strcpy(str_value,p);
     
        for(i=0;i<strlen(str_value);i++)
        {
            if(isdigit(*(str_value+i))||( *(str_value+i)>96 && *(str_value+i)<103) || (*(str_value+i)>64 && *(str_value+i)<72))//other flag||0-f
                ;
            else
            {
                debug_color_out(BRIGHT,RED,BLACK,"Error   : Please in put Value form 0-f!\r\n");
                return ;
            }
        }
        //for(i=0;i<mib_curr_node->oid_len;i++)
         //   oid[i]=mib_curr_node->oid[i];
        //oid[i+1]=id;
        //temp_node=search_mib_node(mib_root_node, oid, i+1);
        //if(!temp_node)
        //{
           // mib_curr_node=temp_node;
    */



    if(curr_instance_arr[id].eable!=0)
    {
        byte=(id)/8;
        bit=(id)%8;
        mib_curr_node->enable_flag[byte]=mib_curr_node->enable_flag[byte] & (~(BYTE)my_pow(2,bit));
        curr_instance_arr[id].eable=0;
    }
    else
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error   : The instance is not exist!\r\n");
        return;
    }
    temp_instance=mib_curr_node->curr_instance;
    mib_curr_node->curr_instance=id;
    del_flag=1;
    debug_mmi_set("*");
    //    debug_mmi_contents("");
    if(temp_instance==id)
    {
        for(i=0;i<256;i++)
            if(curr_instance_arr[i].eable==1)
            {
                temp_instance=i;
                break;
            }
    }
    mib_curr_node->curr_instance=temp_instance;
    debug_ascii_out_proc("State   : ");
    sprintf(str_value,"The %s #%d have been deleted successfully!\r\n",mib_curr_node->name,id);
    debug_color_out(BRIGHT,CYAN,BLACK,str_value);
    return ;
}



void debug_mmi_option(char *str)
{
    int select_id;
    int child_index;
    mib_child_node	*child_node_ptr;
    char			out_text[80];
    int i=0;
    if(!mib_curr_node->total_instance)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error   : This is no a leaf node!\r\n");
        return ;
    }
    del_space(str,0);
    if(isdigit(str[0]))
    {
        for(i=0;i<strlen(str);i++)
        {
            if(!isdigit(str[i]))
            {
                debug_color_out(BRIGHT,RED,BLACK,"Error   : Please input number id of para in instance !\r\n");
                return ;
            }
        }
        select_id=strtol(str,NULL,10);
        if(select_id>mib_curr_node->total_child_nodes+1 || select_id==0)
        {
            sprintf(out_text,"Error   : The max field id is %d\r\n",mib_curr_node->total_child_nodes);
            debug_color_out(BRIGHT,RED,BLACK,out_text);
            return;
        }
        select_id--;
        child_index = find_child_index(mib_curr_node, select_id, NULL, 0);
    }
    else
        child_index = find_child_index(mib_curr_node, 0, str, 1);


    if (child_index  < MIB_MAX_CHILD_NODES_PER_NODE)
    {
        child_node_ptr = mib_curr_node->child_nodes[child_index];
        if (!child_node_ptr->child_ptr)
        {
            debug_color_out(BRIGHT,RED,BLACK,"Fail    : No such child object for the current object\r\n");
            return ;
        }

        param_node = (mib_node_param *)child_node_ptr->child_ptr;
    }
    else
    {
        debug_color_out(BRIGHT,RED,BLACK,"Fail    : There is no such child.\n");
        return ;
    }
    debug_ascii_out_proc("Option  : ");
    debug_color_out(BRIGHT,YELLOW,BLACK,param_node->name);
    debug_ascii_out_proc("\r\n");

    if(param_node->select_total == 0)
    {
        debug_ascii_out_proc("Fail    : There is no description for the select option.\n");
        return ;
    }
    else
    {
        i=0;
        while(i<param_node->select_total)
        {
            debug_ascii_out_proc("          ");
            sprintf(out_text, "%-2d :%-18s ", param_node->select_ptr[i]->number, param_node->select_ptr[i]->description);
            debug_color_out(BRIGHT,BLUE,BLACK,out_text);
            i+=1;
            if(i<param_node->select_total)
            {

                sprintf(out_text, "%-2d :%-18s ", param_node->select_ptr[i]->number, param_node->select_ptr[i]->description);
                debug_color_out(BRIGHT,BLUE,BLACK,out_text);
                i+=1;
                if(i<param_node->select_total)
                {
                    sprintf(out_text, "%-2d :%-18s\r\n", param_node->select_ptr[i]->number, param_node->select_ptr[i]->description);
                    debug_color_out(BRIGHT,BLUE,BLACK,out_text);
                    i+=1;
                }
            }
        }
        if(param_node->select_total%3!=0) 
            debug_ascii_out_proc("\r\n");
    }
    return;
}

/*
*
*       Fun:    debug_mmi_ip
*
*       Desc:   set the hosts' ip which are used by get or set command 
*
*       Ret:    return 1 if successful, return 0 if fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
int debug_mmi_ip(char *ips)
{
    BYTE			str_len,	index,	index_2,	index_3,  index_4;
    char			temp_str[32],	ip[80];

    strcpy(ip, ips);
    printf("ips: %s\r\n", ip);
    if (!(str_len = strlen(ip)))
    {
        debug_ascii_out_proc("Null IP strings.\r\n");
        return 0;
    }
    strcpy(temp_str, "");
    Peer_IP[0] = 0;
    for(index_4=0, index_3=0, index_2=0, index=0; index <= str_len; index++)
    {
        if( (ip[index] != '.') && (ip[index] != ' '))
        {
            temp_str[index_2] = ip[index];
            index_2++;
            if(index_2 > 3)
                break;
        }
        else
        {
            temp_str[index_2] = '\0';
            Peer_IP[index_3] += atoi(temp_str) * my_pow(256, index_4++);
            index_2 = 0;
            if(index_4 >= 4)
            {
                index_3++;
                Peer_IP[index_3] = 0;
                index_4 = 0;
                if(index_3 >= 16)
                    break;
            }
        }
    }

    if(index < str_len)
    {
        debug_ascii_out_proc("Wrong IP.\r\n");
        return 0;
    }

    temp_str[index_2] = '\0';
    if(index_3<16)
        Peer_IP[index_3++] += atoi(temp_str) * my_pow(256, index_4++);
    if(index_3<16)
        Peer_IP[index_3] = 0;

    for(index = 0; index<16; index++)
        printf("peer_id[%d] =  %x		", index,Peer_IP[index]);
    return 1;
}	// end of debug_mmi_ip


void	debug_mmi_find_host(char *str)
{
    BYTE index,i,j,find_all=0,find;
    char temp_str1[64],temp_str2[64],temp_str3[64],*p;


    for(i=0;i<strlen(str);i++)
    {
        if(*(str+i)=='-')
            break;
    }
    if(*(str+i)=='-' && (*(str+i+1)=='a' ||  *(str+i+1)=='A' ))
        find_all=1;
    for(index=0;index<16;index++)
    {
        Peer_IP[index] = 0;
        for(ip_num=0;ip_num<32;ip_num++)
            Remote_Host_Name[index][ip_num]='\0';
        for(ip_num=0;ip_num<16;ip_num++)
            Remote_Host_IP[index][ip_num]='\0';
        IP_Selected[index]=1;
    }
    strcpy(temp_str1,subsys_module[mib_curr_node->module_index].name);
    if(debug_cfg==2 && menu_curr_node!=NULL)
    {
    	if(menu_curr_node->sys_group==9)
    	   find_all=1; 
	    else if(menu_curr_node->sys_group==0)
	    {
	       strcpy(temp_str2,menu_curr_node->name);
	       for(i=0;i<strlen(temp_str2);i++)
	          if(temp_str2[i]=='_')
	             break;
	       if(i!=strlen(temp_str2))
	          p=temp_str2+i;
	       else
	       {
	          debug_ascii_out_proc("Fail    : The menu tree file format is not right!\r\n");
	          return ;
	       }
	       strcat(temp_str1,"_");
         gcvt(host_group,1,temp_str3);
         strcat(temp_str1,temp_str3); 
	       strcat(temp_str1,p);
	       find_all=2;
	    }
	    else
	       host_group=menu_curr_node->sys_group-1;       
	   }
    if(find_all==0)
    {
        strcat(temp_str1,"_");
        gcvt(host_group,1,temp_str2);
        strcat(temp_str1,temp_str2);
    }
    i=0;
    ip_num=0;
    if(mib_curr_node->total_instance)
    {
        while(module_host[i].index!=0xFF)
        {
        	  find=!strncmp(module_host[i].host_name,temp_str1,strlen(temp_str1));
        	  if(find_all==2 && isdigit(module_host[i].host_name[strlen(temp_str1)]))        
        	     find=0; 
            if(find)
            { 
                strcpy(Remote_Host_Name[ip_num],module_host[i].host_name);
                IP_Selected[ip_num]=1;
                strcpy(temp_str2,"");
                strcpy(temp_str3,"");
                for(j=0;j<4;j++)
                {

                    Peer_IP[ip_num] += module_host[i].host_ip[j] * my_pow(256, j);
                    gcvt((double)(module_host[i].host_ip[j]),3,temp_str2);
                    strcat(temp_str3,temp_str2);
                    strcat(temp_str3,".");
                }
                temp_str3[strlen(temp_str3)-1]='\0';
                strcpy(Remote_Host_IP[ip_num],temp_str3);
                ip_num++;
                if(ip_num >= 16)
                    break;

            }
            i++;
        }
    }
    for(index=0;index<16;index++)
    {
        if(Peer_IP[index]!=0)
            break;
    }
    if(list_once==1)
        if(snmp_agent_flag==0)
            if(index==16)
            {
                debug_color_out(BRIGHT,RED,BLACK,"Fial    : Can not find the host!\r\n");
                return ;
            }

    find_all=0;
}

/*
*
*       Fun:    debug_mmi_show_host
*
*       Desc:   if the current node is a leaf node, show its hosts' name and ips
*
*       Ret:    1: succeful,   0: fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
int 	debug_mmi_show_host()
{
    int		index;
    char	out_text[256];
    if( (strlen(Remote_Host_Name[0])<=0) && (strlen(Remote_Host_IP[0])<=0))
    {
        debug_color_out(BRIGHT,RED,BLACK,"Host    : there is no host for the object!\r\n");
        return 0;
    }
    if (mib_curr_node->total_instance)
    {
        if(ip_num!=0)
            debug_ascii_out_proc("Host    : Count      Host Name                  Host IP         Selected\r\n");
        else
        {
            if(list_once==1)
                debug_color_out(BRIGHT,RED,BLACK,"Host    : can not show host because no host for the object!\r\n");
        }
        for(index=0; index<ip_num; index++)
        {
            if(IP_Selected[index] == 1)
                sprintf(out_text, "           %d     %21s       %-12s       Yes\r\n", (index+1), Remote_Host_Name[index], Remote_Host_IP[index]);
            else
                sprintf(out_text, "           %d     %21s       %-12s       No\r\n", (index+1), Remote_Host_Name[index], Remote_Host_IP[index]);
            debug_color_out(BRIGHT,YELLOW,BLACK,out_text);
        }
        return 1;
    }
    debug_color_out(BRIGHT,RED,BLACK,"Host    : can not show host because the current node is not a leaf node.\r\n");
    return 0;
} // end of debug_mmi_show_host

/*
*
*       Fun:    debug_mmi_select_host
*
*       Desc:   if the current node is a leaf node, select which hosts can be used by get or set command 
*
*       Ret:    1: succeful, 0: fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
int 	debug_mmi_select_host(char *host_str)
{
    char 	temp_str[256]="";
    int 	str_len,  index,	index_2,	num;

    for(index=0;index<16;index++)
        IP_Selected[index]=0;

    strcpy(temp_str, "");
    str_len = strlen(host_str);
    for(index_2=0, index=0; index<=str_len; index++)
    {
        if( (host_str[index] != ' ') && (host_str[index] != '\0'))
        {
            if( !isdigit(host_str[index]) )
            {
                debug_color_out(BRIGHT,RED,BLACK,"Error   : The select item must be number.\r\n");
                return 0;
            }
            temp_str[index_2++] = host_str[index];
            temp_str[index_2]='\0';
        }
        else
        {
            temp_str[index_2] = '\0';
            if(strlen(temp_str) != 0)
            {
                num = atoi(temp_str)-1;
                for(index_2=0; (index_2<16) && (Peer_IP[index_2] !=0); index_2++)
                    ;
                if(num >= index_2)
                {
                    if(index_2!=0)
                        sprintf(temp_str, "Error   : The select item must be less than %d.\r\n", index_2);
                    debug_color_out(BRIGHT,RED,BLACK,temp_str);
                    return 0;
                }
                IP_Selected[num]=1;
                index_2=0;
                temp_str[index_2] = '\0';
            }
        }
    }
    return 1;
}// end of debug_mmi_select_host


/*
*
*       Fun:    debug_mmi_setins
*
*       Desc:   if the current node is a leaf node, set the current node's current instance number
*
*       Ret:    1: succeful, 0: fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
int debug_mmi_setins(char *ins)
{
    BYTE			str_len;
    WORD 			instance;
    BYTE			ascii_text[128];

    if (!(str_len = strlen(ins)))
    {
        debug_color_out(BRIGHT,RED,BLACK,"Fail    : No such instance for the current object\r\n");
        return 0;
    }

    if (!mib_curr_node->total_instance)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Fail    : No instance for the current object\r\n");
        return 0;
    }

    ins[str_len] = '\0';

    instance = strtoul(ins, NULL, 10);

    if ( instance >= mib_curr_node->total_instance )
    {
        sprintf((char *)ascii_text, "Error   : The instance value must less than the total instance: %d\r\n",mib_curr_node->total_instance);
        debug_ascii_out_proc((char *)ascii_text);
        return 0;
    }

    mib_curr_node->curr_instance = instance;
    debug_ascii_out_proc("Current instance changed to:");
    sprintf((char *)ascii_text,"%d\r\n", instance);
    debug_color_out(BRIGHT,YELLOW,BLACK, (char *)ascii_text);

    return 1;
}	// end of debug_mmi_setins


////////////////////////////////End of Cfg OID Tree Command/////////



////////////////////////////////MIB OID Operation//////////////////
//find the the child index on just given node
WORD find_child_index(mib_node *node_ptr, DWORD oid, char *name, BYTE type)
{
    WORD 			i;
    mib_child_node	*child_node_ptr;

    //printf("oid[%d], name [%s], type [%d], total_child: %d.\r\n",(int)oid, name,(int)type,node_ptr->total_child_nodes);
    if (!node_ptr->total_child_nodes)
        return 0xFFFF;

    for (i = 0; i < node_ptr->total_child_nodes; i++)
    {
        child_node_ptr = node_ptr->child_nodes[i];
        //printf("child_nod_ptr id [%d], name [%s]. para_name: [%s]\r\n", (int)child_node_ptr->oid, child_node_ptr->name, name);
        if ( (!type) && (oid == child_node_ptr->oid) )
        {
            return i;
        }
        else if (type && (!strncasecmp(child_node_ptr->name, name,strlen(child_node_ptr->name))))
            return i;
    }
    //printf("can't find the nod oid=%d.\r\n",(int)oid);
    return 0xFFFF;
}


/*
*
*       Fun:    search_mib_node
*
*       Desc:   begin form the start_mib_node_ptr, search the node whose OID is equal to the oid
*
*       Ret:    the pionter which point to the node whose OID is equal to the oid , or the NULL pointer if fali
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
mib_node *search_mib_node(mib_node *start_mib_node_ptr, DWORD *oid, BYTE oid_len)
{
    mib_node 		*node_ptr;
    mib_child_node	*child_node_ptr = NULL;
    BYTE			i, loop_times;
    WORD			child_index;

    if(start_mib_node_ptr == NULL)
    {
        printf("start_mib_node_ptr is NULL.\r\n");
        return NULL;
    }
    if (oid_len <= start_mib_node_ptr->oid_len)
        return NULL;
    if ((loop_times = oid_len - start_mib_node_ptr->oid_len) >= 15)
        return NULL;
    node_ptr = start_mib_node_ptr;
    for (i = 0; i < loop_times; i++)
    {
        if ((child_index = find_child_index(node_ptr, oid[start_mib_node_ptr->oid_len + i], NULL, 0)) >= MIB_MAX_CHILD_NODES_PER_NODE)
            return NULL;
        child_node_ptr = node_ptr->child_nodes[child_index];
        if (i != (loop_times - 1))
        {
            if (child_node_ptr->child_ptr == NULL)
                return NULL;
            node_ptr = (mib_node *) child_node_ptr->child_ptr;
        }
    }
    return (mib_node *) child_node_ptr->child_ptr;
}	// end of search_mib_node



/*
*
*       Fun:    find_node
*
*       Desc:   find the node whose name is node_name from the start_node_ptr
*
*       Ret:    successful: the pointer to the node,	fail: NULL
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
mib_node *find_node(mib_node *start_node_ptr, char *node_name, int find_type, DWORD *last_node_oid)
{
    int			index,	index_2;
    mib_node 	*child_node_ptr;
    mib_child_node  *mib_child_node_ptr;
    //printf("find_node begin: %s", start_node_ptr->name);
    if(start_node_ptr == NULL)
        return NULL;
    //printf("nod_name: %s.\r\n",start_node_ptr->name);
    if(!strcmp(start_node_ptr->name, node_name))
    {
        if(find_type)
        {
            for(index_2=0; index_2 < start_node_ptr->oid_len-1; index_2++)
            {
                //printf("%ld.\r\n",start_node_ptr->oid[index_2]);
                if(start_node_ptr->oid[index_2] != last_node_oid[index_2])
                    break;
            }
            if(index_2 == (start_node_ptr->oid_len -1))
                return start_node_ptr;
        }
        else
            return start_node_ptr;
    }
    if(start_node_ptr->total_child_nodes == 0)
        return NULL;
    if(!start_node_ptr->total_instance)
    {
	    for(index = 0; index < start_node_ptr->total_child_nodes; index++)
	    {
	        //printf("child nod_name[%d]: %s.\r\n",index,start_node_ptr->child_nodes[index]->name);
	        mib_child_node_ptr=start_node_ptr->child_nodes[index];
	        if(mib_child_node_ptr)
		        if( !(strcmp(mib_child_node_ptr->name, node_name))	)
		        {
		            child_node_ptr = mib_child_node_ptr->child_ptr;
		            if(child_node_ptr!=NULL)
		            {
			            if(find_type)
			            {
			                for(index_2=0; index_2 < child_node_ptr->oid_len-1; index_2++)
			                {
			                    if(child_node_ptr->oid[index_2] != last_node_oid[index_2])
			                        break;
			                }
			                if(index_2 == (child_node_ptr->oid_len -1))
			                    return child_node_ptr;
			            }
			            else
			                return child_node_ptr;
		            }
		        }
	    }
    
	    for(index = 0; index < start_node_ptr->total_child_nodes; index++)
	    {
	    	mib_child_node_ptr=start_node_ptr->child_nodes[index];
	        if(mib_child_node_ptr)
	        {           
	           child_node_ptr = find_node(mib_child_node_ptr->child_ptr, node_name, find_type, last_node_oid);
		        if( child_node_ptr != NULL)
		        {
		            if(find_type)
		            {  
		                for(index_2=0; index_2<child_node_ptr->oid_len-1; index_2++)
		                {
		                    if(child_node_ptr->oid[index_2] != last_node_oid[index_2])
		                        break;
		                }
		                if(index_2 == (child_node_ptr->oid_len -1))
		                    return child_node_ptr;
		            }
		            else
		                return child_node_ptr;
		        }
		    }
	    }  
    }
    return NULL;
}	// end of find_node

/*
**
*       Fun:    show_node
*
*       Desc:   show the mib_tree start from the node_ptr whit the separation as a class sign
*
*       Ret:    RETVOID
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
void show_node(mib_node *node_ptr,char *separation)
{
    int			index,	length;
    mib_node 	*child_node_ptr;
    char		spaces[256],	separation2[256];
    strcpy(separation2, separation);
    length = strlen(separation2);
    if(node_ptr == NULL)
    {
        printf("\r\n");
        return;
    }
    for(index=0; index<length; index++)
    {
        spaces[index] = ' ';
    }
    spaces[index]='\0';
    spaces[0]= '|';
    if(node_ptr == mib_root_node)
    {
        printf("%snode name:    %s\r\n%soid:        %d",separation2, node_ptr->name, spaces, (int)node_ptr->oid[0]);

        for(index=1; index<node_ptr->oid_len; index++)
        {
            printf(".%d",(int)node_ptr->oid[index]);
        }
    }
    else
        printf("%snode name:    %s\r\n%soid:        %d",separation2, node_ptr->name, spaces, (int)node_ptr->oid[node_ptr->oid_len - 1]);
    printf("\n");
    if( node_ptr->total_child_nodes == 0 )
        return;
    else
    {
        printf("%schild nodes:\r\n",spaces);
    }
    if(node_ptr->total_instance != 0)
    {
        for(index = 0; index < (node_ptr->total_child_nodes); index++)
        {
            mib_node_param 	*mib_node_param_ptr;

            printf("%s   oid: %d,      name: %s\r\n",spaces, (int)node_ptr->child_nodes[index]->oid, node_ptr->child_nodes[index]->name);

            mib_node_param_ptr = (mib_node_param *)node_ptr->child_nodes[index]->child_ptr;//??

            printf("%s      id: %d,     name: %s,     %d . %d - %d . %d\r\n", spaces, (int)mib_node_param_ptr->id+1, mib_node_param_ptr->name, (int)mib_node_param_ptr->byte_begin, (int)mib_node_param_ptr->bit_begin, (int)mib_node_param_ptr->byte_end, (int)mib_node_param_ptr->bit_end );

            if(mib_node_param_ptr->select_total != 0)
            {
                int index_s;
                mib_param_select *mib_param_select_ptr;

                printf("%s      selects: \r\n", spaces);

                for(index_s=0; index_s< mib_node_param_ptr->select_total; index_s++)
                {
                    mib_param_select_ptr = mib_node_param_ptr->select_ptr[index_s];

                    printf("%s         number: %d,      opt: %s\r\n", spaces, (int)mib_param_select_ptr->number, mib_param_select_ptr->description);
                }
            }
            if(mib_node_param_ptr->input_ptr != NULL)
            {
                mib_param_input *mib_param_input_ptr;
                mib_param_input_ptr = mib_node_param_ptr->input_ptr;
                printf("%s      input:\r\n%s         input_param_type: %d,      input_string: %s\r\n", spaces, spaces, (int)mib_param_input_ptr->input_param_type, mib_param_input_ptr->input);
            }
        }
        return;
    }

    for(index = 0; index < node_ptr->total_child_nodes; index++)
        printf("%s      oid: %d,      name: %s\r\n",spaces, (int)node_ptr->child_nodes[index]->oid, node_ptr->child_nodes[index]->name);
    strcat(separation2, "{ }--");
    for(index = 0; index < node_ptr->total_child_nodes; index++)
    {
        child_node_ptr = node_ptr->child_nodes[index]->child_ptr;
        show_node(child_node_ptr,separation2);
    }
    printf("\r\n");
}	// end of show_node


/*
*
*       Fun:    find_OID
*
*       Desc:   search the OID which number is obj_num in the sysConf_file, and store them to the *OID
*
*       Ret:    the length of the OID;		0: Fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/

int find_OID(int obj_num, char *sysConf_file_name, DWORD *OID)
{
    char 	temp_str[256], stop_ch[32];
    int 	state = 0,	find;
    FILE	*fp_sysConf;

    if((fp_sysConf = fopen(sysConf_file_name,	"r")) == NULL)
    {
        printf("[debug] Error: cannot open param_sysConf_file: [%s].\r\n",sysConf_file_name);
        return 0;
    }

    strcpy(stop_ch,";");
    while( !feof(fp_sysConf) )
    {
        read_until(fp_sysConf, temp_str, stop_ch);

        switch(state)
        {
        case 0:
            if(strlen(temp_str) == 0)
            {
                printf("[debug] Error: Fail to get the object number in the sysConf_file.\r\n");
                fclose(fp_sysConf);
                return 0;
            }
            if( obj_num == atoi(temp_str) )
                find = 1;
            else
                find = 0;
            strcpy(stop_ch,";"); 
            state = 1;
            break;
        case 6:
            if(find && (strlen(temp_str) != 0))
            {
                int i, j, k, length=strlen(temp_str);
                char temp_str_2[256];
                strcpy(temp_str_2, "");
                for(k=0; k<32; k++)
                    OID[k] = 0;
                for(i=0,j=0,k=0 ; i<length; i++,j++)
                {
                    if(temp_str[i] != '.')
                        temp_str_2[j] = temp_str[i];
                    else
                    {
                        temp_str_2[j]= '\0';
                        OID[k++] = atoi(temp_str_2);
                        j = -1;
                        strcpy(temp_str_2, "");
                    }
                }
                temp_str_2[j]= '\0';
                OID[k++] = atoi(temp_str_2);
                fclose(fp_sysConf);
                return k;
            }
            else
            {
                strcpy(stop_ch,";");
                state++;
                break;
            }
        case 25:
            read_until(fp_sysConf, temp_str, "\n");//0x0a
            strcpy(stop_ch,";");
            state = 0;
            break;
        default:
            strcpy(stop_ch ,";");
            state++;
            break;
        }
    }
    printf("[debug] Error: Can not find the OID which number is %d in the sysConf_file.\r\n", obj_num);
    fclose(fp_sysConf);
    return 0;
}	// end of find_OID


////////////////////////////////MIB OID Operation//////////////////////




///////////////////////////////CMM Menu Tree Command///////////////////

void debug_mmi_view()
{
    char         ascii_text[128];
    menu_node    *menu_tmp_node;
    BYTE          i;

    if(menu_curr_node->menu_oid_node==mib_curr_node && menu_curr_node->total_menu_child)
    {
        debug_ascii_out_proc("Name    : ");
        sprintf(ascii_text,"%s",menu_curr_node->name);
        debug_color_out(BRIGHT,YELLOW,BLACK,ascii_text);
        debug_ascii_out_proc("\r\n");
        debug_ascii_out_proc("Contents: ");
        sprintf(ascii_text, "[%d]\r\n",menu_curr_node->total_menu_child);
        debug_color_out(BRIGHT,YELLOW,BLACK,ascii_text);
        for (i = 0; i < menu_curr_node->total_menu_child; i++)
        {
            menu_tmp_node = menu_curr_node->child_node[i];
            if(menu_tmp_node!=NULL)  
            {
                sprintf(ascii_text, "           |---(%d) %s\r\n",menu_tmp_node->menu_id[menu_tmp_node->id_len-1], menu_tmp_node->name);
                debug_color_out(BRIGHT,YELLOW,BLACK,ascii_text);
            }
        }
    }
    else
    {
        debug_mmi_contents("");
    }
}

void debug_mmi_mcd(char *str)
{
    char     		input_str[32];
    int      		index;//,index1;
    menu_node   	*menu_tmp_node;
    mib_child_node 	*child_node_ptr;
    mib_node        *find_node_ptr;
    DWORD           temp_index;  
    DWORD           OID[32];
    BYTE        index_start,index_end;
    
    
    if(mib_curr_node->total_instance && strstr(str,"..")==0)
    {
       debug_color_out(BRIGHT,RED,BLACK,"Error   : Have reached the leaf node!\r\n");
       return;
    }
   
    
   if(strlen(str)>32)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error   : No need so long command!\r\n");
        return ;
    }
   
    for(index_start=0;index_start<strlen(str);index_start++)
    {
    	if(*(str+index_start)!=' ')
    	   break;
    }
    for(index_end=strlen(str);index_start>0;index_start--)
    {
    	if(*(str+index_end)!=' ')
    	   break;
    }    
    strncpy(input_str,str+index_start,index_end-index_start);
    if(!strlen(input_str))
    {
        menu_curr_node=menu_root_node;
        if(menu_curr_node->menu_oid_node)
        		mib_curr_node=menu_curr_node->menu_oid_node;
        list_once=0;
        debug_mmi_view();
        return ;
    }
    if(input_str[0]=='.'&& input_str[1]=='.')
    {
    	 if(mib_curr_node!=menu_curr_node->menu_oid_node)
    	 {
    	 	  mib_curr_node=(mib_node*)mib_curr_node->parent_ptr; 
    	 }
    	 else if(mib_curr_node==menu_curr_node->menu_oid_node)
    	 {
	       menu_tmp_node=(menu_node *)menu_curr_node->father_ptr;
	       if(menu_tmp_node) 
	       {
	          menu_curr_node=menu_tmp_node;
	         if(menu_curr_node->menu_oid_node)
	         {
	        		mib_curr_node=menu_curr_node->menu_oid_node;
	        		list_once=0;
	         }  
	      }
	       else
	       {
	       	 debug_color_out(BRIGHT,RED,BLACK,"Error   : Have reach the 'WXC2' or no father node\r\n"); 
	       	 return ;
	       }
      }
    } 
    else if(!isdigit(input_str[0]) || (isdigit(input_str[0]) && !isdigit(input_str[1]) && input_str[1]!='\0'))
		{
		    	  if(mib_curr_node==menu_curr_node->menu_oid_node && menu_curr_node->total_menu_child)
		    	  {
				        for(index=0;index<menu_curr_node->total_menu_child;index++)
				        {
				            if((menu_tmp_node=(menu_node*)menu_curr_node->child_node[index]))
				                if(strncasecmp(input_str,menu_tmp_node->name,strlen(menu_tmp_node->name))==0)
				                {
				                    menu_curr_node=menu_tmp_node; 
				                    if(menu_curr_node->menu_oid_node)
		        									mib_curr_node=menu_curr_node->menu_oid_node;
				                    break;
				                }
				        }
				        if(index==menu_curr_node->total_menu_child)
				        {
				           	 debug_color_out(BRIGHT,RED,BLACK,"Error   : No such object as the name or input error!\r\n");
				           	 return ;
				        }
				    }
				    else
				    {
				    	index = find_child_index(mib_curr_node, 0, input_str, 1);

					    if (index  < MIB_MAX_CHILD_NODES_PER_NODE)
					    {
					        child_node_ptr = mib_curr_node->child_nodes[index];
					        if (!child_node_ptr->child_ptr)
					        {
					            debug_color_out(BRIGHT,RED,BLACK,"Fail    : No such child object for the current object\r\n");
					            return ;  
					        }
					        else
					           mib_curr_node=child_node_ptr->child_ptr; 
					    }
					     else
							  {
							  	debug_color_out(BRIGHT,RED,BLACK,"Error   : Input error!\r\n");
								  return ;
						    } 
				    }
		}
		else if(isdigit(input_str[0]))
		{
		        for(index=0;index<strlen(input_str);index++)
		        {
		            if(!isdigit(input_str[index]))
		            {
		                debug_color_out(BRIGHT,RED,BLACK,"Error   : Input error!\r\n");
		                return ;
		            }  
		        }
		        if(index==strlen(input_str))
		        {
		            if(index>4)
		            {
		                debug_color_out(BRIGHT,RED,BLACK,"Error   : Input error!\r\n");
		                return ;
		            }
		            temp_index=atoi(input_str);
		        }
		        if(mib_curr_node==menu_curr_node->menu_oid_node && menu_curr_node->total_menu_child)
		        {
		        	if(temp_index<=0 || temp_index>menu_curr_node->total_menu_child)
		        	{
		        		debug_color_out(BRIGHT,RED,BLACK,"Error   : Input error!\r\n");
		            return ; 
		        	}
			        if(!menu_curr_node->total_menu_child || temp_index>menu_curr_node->total_menu_child)
			        {
			        	 debug_color_out(BRIGHT,RED,BLACK,"Error   : No child menu item!\r\n");
			           return ; 
			        }	        
			        else if((menu_tmp_node=(menu_node*)menu_curr_node->child_node[temp_index-1]))
				      {
				            menu_curr_node=menu_tmp_node;
				            if(menu_curr_node->menu_oid_node)
	        							mib_curr_node=menu_curr_node->menu_oid_node;
				      }
			      }
			      else
			      {
			      	if(mib_curr_node->total_child_nodes)
			      	{
			           OID[mib_curr_node->oid_len]=temp_index; 
			           find_node_ptr =(mib_node*)search_mib_node(mib_curr_node, OID,mib_curr_node->oid_len+1);
			      	   //child_node_ptr=mib_curr_node->child_nodes[temp_index-1];
			      	   if(!find_node_ptr)
			      	   {
				           debug_color_out(BRIGHT,RED,BLACK,"Error   : No such child item!\r\n");
				           return ; 
			           }
			           else
			           {
			              mib_curr_node=find_node_ptr;
			              list_once=0;	
			            }
			      	   //if(child_node_ptr->child_ptr)
			      	    //   mib_curr_node=child_node_ptr->child_ptr;			  
			      	   //else
			      	   //{
			      	   // debug_color_out(BRIGHT,RED,BLACK,"Error   : No such child node!\r\n");
		             //  return ;
		             //}
			      	}
			      	else 
			      	{
			      		debug_color_out(BRIGHT,RED,BLACK,"Error   : No child for current node!\r\n");
		            return ;
			      	}
			      }
		}
	  else
	  {
	  	debug_color_out(BRIGHT,RED,BLACK,"Error   : Input error!\r\n");
		  return ;
    } 
    /*
    for(index=0;index<compute_field_num;index++)
    {
    	 compute_field_arr[index]. print_count=0;  
       if(mib_curr_node==compute_field_arr[index].compute_node)
       {
       	       memset(compute_field_list,0,6*sizeof(compute_list));       	       
        	 	   memcpy(&compute_curr_unit,compute_field_arr+index,sizeof(compute_field)); 
        	 	   field_compute_state=1; 
        	 	   print_line_index=0;
        	 	   read_syntax_nor_list=1;
        	 	   line_index=0;  
        	 	   print_once=1;     	 	   
        	     compute_state=1; 
        	     temp_compute_node=mib_curr_node;
        	     translate_compute_field();
       }
    }
    if(field_compute_state>3) 
    {
       field_compute_state=0;
       read_syntax_nor_list=0;
       line_index=0;
       compute_state=0;
       print_line_index=0;
    }
    */
    if(mib_curr_node==menu_curr_node->menu_oid_node && menu_curr_node->total_menu_child)
        debug_mmi_view();
    else
    {
    	if(menu_curr_node->menu_oid_node)   
    	{
    		curr_module=mib_curr_node->module_index; 
    	  if(mib_curr_node->total_instance)
    	  {
	        list_once=0;
	        curr_module=mib_curr_node->module_index;
	        register_instance();	        
	        debug_mmi_contents(" ");
	        debug_cfg_end=0; 
	      } 
	      else
	      {
	      	 debug_mmi_contents(" ");
	      } 
	    }
	    else 
	    {
	    	 debug_color_out(BRIGHT,RED,BLACK,"Error   : No oid node match along with the menu item!\r\n");
		     return ; 
	    } 
    }

}

void main_menu(char *str)
{
    char out_text[128],*ch;
    BYTE i;
    for(i=0;i<80 && *(str+i)!='\0';i++)
    {
        if(*(str+i)!=' ')
            break;
    }
    ch=str+i;
    strcpy(out_text,ch);
    if(*out_text!='\0')
    {
        if(strncasecmp(out_text,"help",4)==0)
        {      
	        debug_ascii_out_proc(" Usage 1: ");
	        debug_color_out(BRIGHT,YELLOW,BLACK,"help  \r\n");
	        debug_ascii_out_proc("          Show the WXC2 configer menu page\r\n");
	        debug_ascii_out_proc(" Usage 1: ");
	        debug_color_out(BRIGHT,YELLOW,BLACK,"help command\r\n");
	        debug_ascii_out_proc("          Show the usage of the command: contents,cd,child,parent,get,set,lsit,\r\n          create,delete,remark,option,cmm,help\r\n");
        }
        else if(strncasecmp(out_text,"cd",2)==0)
        {
            debug_ascii_out_proc(" Usage 1: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"cd  \r\n");
            debug_ascii_out_proc("          Enter the system main menu WXC2\r\n");
            debug_ascii_out_proc(" Usage 2: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"cd ..\r\n");
            debug_ascii_out_proc("          Enter the parent menu item \r\n");
            debug_ascii_out_proc(" Usage 3: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"cd name\r\n");
            debug_ascii_out_proc("          Enter the child menu item name as 'name'\r\n");
            debug_ascii_out_proc(" Usage 4: ");
            debug_color_out(BRIGHT,YELLOW,BLACK,"cd XX\r\n");
            debug_ascii_out_proc("          Enter the child menu item whose id is 'XX' \r\n");
        }
        else if(strncasecmp(out_text,"get",3)==0)
        {
            help_cmm(1,"get   or get *","Get and printf the current instance's value of the node\r\n");
            help_cmm(2,"get #XX","Get and printf the special instance's value of the node\r\n");
            help_cmm(3,"get *.X or get *.name","Get and printf the current instance's special field value of the node\r\n");
            help_cmm(4,"get #X.X or get #x.name","Get and printf the special instance's special field value of the node\r\n");
        }
        else if(strncasecmp(out_text,"set",3)==0)
        {
            help_cmm(1,"set or set *","Set the current instance's value to default value of the node and open the isntance ,then printf\r\n");
            help_cmm(2,"set #XX ","Set the special instance's value to default value of the node and open the isntance ,then printf\r\n");
            help_cmm(3,"set *.X =X or set *.name=X ","Set the current instance's field value to given value of the node and open the isntance ,then printf\r\n");
            help_cmm(4,"set #X.X =X or set #x.name=X ","Set the special instance's field value to given value of the node and open the isntance ,then printf\r\n");
            help_cmm(5,"set #X =XXX","Set the special instance's whole value to given value of the node and open the isntance ,then printf\r\n");
        }
        else if(strncasecmp(out_text,"create",3)==0)
        {
            help_cmm(1,"creat or create *","Create an instance with the min closed instance id and set it's value to default value of the node and open the isntance ,then printf\r\n");
            help_cmm(2,"create #XX","Create the special instance,and set it's value to default value of the node and open the isntance ,then printf\r\n");
            help_cmm(3,"create *.X =X or create *.name=X ","Create an instance's with the min closed instance id ,and set it's field value to given value of the node and open the isntance ,then printf\r\n");
            help_cmm(4,"create #X.X =X or create #x.name=X ","Create the special instance,and set it's field value to given value of the node and open the isntance ,then printf\r\n");
            help_cmm(5,"create #X =XXX","Create the special instance,and set is's whole value to given value of the node and open the isntance ,then printf\r\n");
        }
        else if(strncasecmp(out_text,"delete",3)==0)
        {
            help_cmm(1,"delete  ","Delete the current instance ,if it is opened,clost it.set the value to default value of the node\r\n");
            help_cmm(2,"delete #XX","Delete the special instance,if it is opened,clost it.set the value to default value of the node\r\n");
        }
        else if(strncasecmp(out_text,"contents",3)==0)
        {
            help_cmm(1,"contents ","Show the current menu item or the informat about the oid node\r\n");
        }
        else if(strncasecmp(out_text,"list",3)==0)
        {
            help_cmm(1,"list [-a]","the switch of show all the field and value of the instance include the 'NULL' and the closed instance,default:off\r\n");
            help_cmm(2,"list [-e]","the switch of show opened the instance,hide the field and it's value default:off\r\n");
            help_cmm(3,"list [-r]","the switch of show the field data range.default:off\r\n");
            help_cmm(4,"list [-t]","the switch of show input data type ,default:off\r\n");
            help_cmm(5,"list [-f[X XX X]]","the switch of show the special field list in the [ ] ,default:off\r\n");
            help_cmm(6,"list [X-XX]","the switch of show the instance range form x to xxx.default:off\r\n");
        }
        else if(strncasecmp(out_text,"child",3)==0)
        {
            help_cmm(1,"child X","Enter the child node whose  id is X\r\n");
            help_cmm(2,"child name","Enter the child node whose name is 'name'\r\n");
        }
        else if(strncasecmp(out_text,"parent",3)==0)
        {
            help_cmm(1,"parent","Enter the parent node\r\n");
        }
        /*
        else if(strncasecmp(out_text,"pwd",3)==0)
        {
            help_cmm(1,"pwd","Show the full path and the full oid\r\n");
        }
        */ 
        else if(strncasecmp(out_text,"remark",3)==0)
        {
            help_cmm(1,"remark","Show the description of the node\r\n");
        }
        else if(strncasecmp(out_text,"option",3)==0)
        {
            help_cmm(1,"option X","Show the select option of the fied node whose id is x of leaf node \r\n");
            help_cmm(2,"option name","Show the select option of the fied node whose name is ''name' of leaf node \r\n");
        }
        else if(strncasecmp(out_text,"save",3)==0)
        {
            help_cmm(1,"save","save the debug\r\n");
        }
        else if(strncasecmp(out_text,"find host",3)==0)
        {
            help_cmm(1,"find host","Find all the host who have the object about the current node\r\n");
        }
        else if(strncasecmp(out_text,"select",3)==0)
        {
            help_cmm(1,"select X","Select the host ip list,if been selected ,the set command would do effect on the host\r\n");
        }
        else if(strncasecmp(out_text,"setinst",3)==0)
        {
            help_cmm(1,"setinst X","Set the current instance as the special number\r\n");
        }
        else if(strncasecmp(out_text,"cmm",3)==0)
        {
            help_cmm(1,"cmm","Enter the debug cmm state\r\n");
        }
        /*
        else if(strncasecmp(out_text,"addhost",3)==0)
        {}
        else if(strncasecmp(out_text,"delhost",3)==0)
        {}
        else if(strncasecmp(out_text,"tree",4)==0)
        {
            help_cmm(1,"tree","Print the menu three\r\n");
        }
        */
        else
        {
        	debug_color_out(BRIGHT,RED,BLACK,"Error   : No such Command!\r\n");
          return ; 
        }
    }
    else 
    {
        time_t	tick_2;
        char time_buf_2[64]= "\0",  str_buf[128]="\0";
        BYTE    count=0;
        tick_2 = time(NULL);
        debug_ascii_out_proc("\033[2J\033[1;1H");
        snprintf(time_buf_2, sizeof(time_buf_2), "%.24s", ctime(&tick_2));
        sprintf(str_buf, "       -----Start Time: %s-----       \r\n", time_buf_2);
        debug_color_out(BRIGHT,CYAN,WHITE,str_buf);
        debug_ascii_out_proc("\r\n");
        debug_ascii_out_proc("                           ");
        debug_ascii_out_proc("*************************\r\n");//80-20=60/2=30
        debug_ascii_out_proc("                         ");
        debug_ascii_out_proc("*");
        debug_ascii_out_proc(" ");
        debug_color_out(BRIGHT,WHITE,BLACK,"WXC2 System Configer Menu");//80-20=60/2=30
        debug_ascii_out_proc(" ");
        debug_ascii_out_proc("*\r\n");
        debug_ascii_out_proc("                           ");
        debug_ascii_out_proc("*************************\r\n");//80-20=60/2=30
        debug_ascii_out_proc("\r\n");
        debug_ascii_out_proc("\r\n");
        debug_ascii_out_proc(" ============================================================================== \r\n");
        debug_ascii_out_proc("       |            |            |            |            |             |      \r\n");
        debug_ascii_out_proc("   ");          
        count=menu_root_node->total_menu_child;
        for(i=0;i<count;i++)
        {  	  
            sprintf(out_text,"   %s   ",((menu_node*)menu_root_node->child_node[i])->name);
            debug_color_out(BRIGHT,YELLOW,GREEN,out_text);
            debug_ascii_out_proc("    ");
        }
        debug_ascii_out_proc("\r\n\r\n\r\n\r\n");
        if(debug_cfg==2) 
        {
	        debug_ascii_out_proc(" Usage 1: ");
	        debug_color_out(BRIGHT,YELLOW,BLACK,"help  \r\n");
	        debug_ascii_out_proc("          Show the WXC2 configer menu page\r\n");
	        debug_ascii_out_proc(" Usage 1: ");
	        debug_color_out(BRIGHT,YELLOW,BLACK,"help command\r\n");
	        debug_ascii_out_proc("          Show the usage of the command: contents,cd,child,parent,get,set,list,\r\n          create,delete,remark,option,cmm,save,help\r\n");
       }
    }
}	// end of debug_mmi_cfg_help



menu_node *search_menu_node(menu_node *start_menu_node_ptr, BYTE *menu_id, BYTE id_len)
{
    menu_node 		*node_ptr,*menu_node_ptr;
    BYTE			    i, loop_times;


    if(start_menu_node_ptr == NULL)
    {
        printf("start_menu_node_ptr is NULL.\r\n");
        return NULL;
    }
    if (id_len <= start_menu_node_ptr->id_len)
        return NULL;
    if ((loop_times = id_len - start_menu_node_ptr->id_len) >= 15)
        return NULL;
    node_ptr = start_menu_node_ptr;
    for (i = 0; i < loop_times; i++)
    {
        menu_node_ptr= (menu_node*)node_ptr->child_node[menu_id[i]-1];
        if(menu_node_ptr)
            node_ptr=menu_node_ptr;
        else
        {
            printf("[debug]   :Fail to search menu node!father node: %s,search_id:%d\n",node_ptr->name,menu_id[loop_times]);
            return NULL;
        }
    }
    return (menu_node *) node_ptr;
}	// end of search_menu_node
//////////////////////////////End of CMM Menu Tree Command////////////






///////////////////////////////snmp senior agnet/////////////////

void debug_set_agent()
{
    DWORD 					oid[32];
    mib_child_node        *child_node_ptr;
    mib_node        *find_node_ptr;
    char            *p,temp_str1[16],temp_str2[4];
    WORD            ip[16][4];
    DWORD           Set_IP[16],sum,sub_sum;
    BYTE            m;
    param_field    *tmp_param_field; 
    WORD            i,j,k,child_index,index3,index,index2,index4,select_id;
    WORD            total_field;
    if(snmp_agent_flag==0 && (get_state!=0 || set_state!=0))
       return ;
   


    for(i=0;i<MAX_PACKET_NUM;i++)
    {
        if(debug_snmp_agent_buf[i].cmm_state==3)
            return ;
    }
    for(i=0;i<MAX_PACKET_NUM;i++)
    {
        if(debug_snmp_agent_buf[i].cmm_state==2)
            break ;
    }
    if(i==MAX_PACKET_NUM) 
        return ;
    debug_agent_index=i;
    
    
    
    
    
    memcpy(&snmp_agent_packet,debug_snmp_agent_buf+debug_agent_index,sizeof(debug_snmp_agent));

    for(i=0;i<snmp_agent_packet.oid_len-1;i++)
    {
        oid[i]=snmp_agent_packet.oid[i];
    }
    find_node_ptr=search_mib_node(mib_root_node,oid,snmp_agent_packet.oid_len-1);
    if(find_node_ptr==NULL)
    {
        debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
        debug_snmp_agent_buf[debug_agent_index].result_state=3;
        return ;
    }
    if(!find_node_ptr->total_instance)
    {
        debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
        debug_snmp_agent_buf[debug_agent_index].result_state=3;
        return ;
    }
    agent_tmp_node=mib_curr_node;     
    mib_curr_node=find_node_ptr;
    for(i=0;i<mib_curr_node->total_child_nodes;i++)
    {
    	child_node_ptr=mib_curr_node->child_nodes[i];
    	if(child_node_ptr) 
    	{
    	   param_node = (mib_node_param *)child_node_ptr->child_ptr;
         tmp_param_field=&debug_snmp_agent_buf[debug_agent_index].param_field_arr[i];
         if(param_node->select_total!=0)
         {
         	 tmp_param_field->total_select=param_node->select_total;
             for(index=0; index<param_node->select_total; index++)
             {
                  tmp_param_field->value_arr[index]=param_node->select_ptr[index]->number;
                  strcpy(tmp_param_field->value_arr_remark[index],param_node->select_ptr[index]->description);
             }
             tmp_param_field->input_type=0xFF;
         }
         else if(param_node->input_ptr!=NULL)
         {
             tmp_param_field->total_select=0; 
             tmp_param_field->input_type=param_node->input_ptr->input_param_type;
         }
         strcpy(tmp_param_field->name,param_node->name);
    	}
    	
    }
    
    if(mib_curr_node->max_access ==0 ||  mib_curr_node->max_access ==1 || mib_curr_node->max_access ==3)
    {
        debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
        debug_snmp_agent_buf[debug_agent_index].result_state=6;
        mib_curr_node=agent_tmp_node;
        return ; 
    }

    if(snmp_agent_packet.oid[snmp_agent_packet.oid_len-1] >mib_curr_node->total_instance)
    {
        debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
        debug_snmp_agent_buf[debug_agent_index].result_state=5;
        mib_curr_node=agent_tmp_node;
        return ;
    }

    mib_curr_node->curr_instance=snmp_agent_packet.oid[snmp_agent_packet.oid_len-1];


    
    
    //set the param
    if(snmp_agent_packet.total_field!=mib_curr_node->total_child_nodes)
    {
        debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
        debug_snmp_agent_buf[debug_agent_index].result_state=7;
        mib_curr_node=agent_tmp_node;
        return ;
    }
    j=mib_curr_node->data_length;
    for(i=0;i<j;i++)
    {
        *(recv_msgbuf.var[0].msg+i)=mib_curr_node->init_value[i];
    }

    total_field=snmp_agent_packet.total_field;
    //total_field=mib_curr_node->total_child_nodes;  
    for(child_index=0;child_index<total_field;child_index++)
    {
        child_node_ptr = mib_curr_node->child_nodes[child_index];
        if (!child_node_ptr->child_ptr)
        {
            strcpy(temp_str1,"1");
            sprintf(temp_str2,"%d",child_index);
            strcat(temp_str1,temp_str2);
            j=atoi(temp_str1);
            debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
            debug_snmp_agent_buf[debug_agent_index].result_state=j;
            mib_curr_node=agent_tmp_node;
            return ;
        }
        param_node = (mib_node_param *)child_node_ptr->child_ptr;

        if(param_node->select_total != 0)
        {
            select_id=atoi(snmp_agent_packet.param_field_arr[child_index].curr_input_value);
            if(select_id>param_node->select_total)
            {
                strcpy(temp_str1,"2");
                sprintf(temp_str2,"%d",snmp_agent_packet.param_field_arr[child_index].curr_select_id);
                strcat(temp_str1,temp_str2);
                j=atoi(temp_str1);
                debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                debug_snmp_agent_buf[debug_agent_index].result_state=j;
                mib_curr_node=agent_tmp_node;
                return ;

            }
            else
            {
                j=select_id;
                set_val=param_node->select_ptr[j]->number;
                index3 = sum = 0;
                for(index=param_node->byte_begin; index <= param_node->byte_end; index++)
                {
                    if(index == param_node->byte_begin)
                        index2 = param_node->bit_begin;
                    else
                        index2 = 0;
                    for(; index2<8; index2++)
                    {
                        if((set_val & (BYTE)my_pow(2, index3)) == 0)
                            recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] & (255 - my_pow(2, index2));
                        else
                            recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] | my_pow(2, index2);
                        if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                            break;
                        index3++;
                    }
                    if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                        break;
                }
            }
        }
        else if(param_node->input_ptr != NULL)
        {
            strcpy(set_val_str,snmp_agent_packet.param_field_arr[child_index].curr_input_value);
            switch(param_node->input_ptr->input_param_type)// ananlise
            {
            case 0:	//toDec_high_0		//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u662F\u5426\u6709\u6548
                j = strlen(set_val_str);
                for(i=0; i<j; i++)
                {
                    if( !isdigit(set_val_str[i]) )
                        break;
                }
                if(i<j)	// set_val_str is not a digital string
                {
                    strcpy(temp_str1,"3");
                    sprintf(temp_str2,"%d",child_index);
                    strcat(temp_str1,temp_str2);
                    j=atoi(temp_str1);
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=j;
                    mib_curr_node=agent_tmp_node;
                    return ;
                }
                else
                {

                    j =(param_node->byte_end * 8 + 1 + param_node->bit_end) - (param_node->byte_begin * 8  - param_node->bit_begin);
                    set_val = atol(set_val_str);
                    if(set_val > my_pow(2, j))
                    {
                        strcpy(temp_str1,"3");
                        sprintf(temp_str2,"%d",child_index);
                        strcat(temp_str1,temp_str2);
                        j=atoi(temp_str1);
                        debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                        debug_snmp_agent_buf[debug_agent_index].result_state=j;
                        mib_curr_node=agent_tmp_node;
                        return ;
                    }
                }
                break;
            case 1:	//keepHex_high_0		//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u5B57\u7B26\u4E32set_val_str\u662F\u5426\u6709\u6548, \u5982\u679C\u6709\u6548\u5219\u5C06\u9AD8\u4F4D\u8865\u6210\u5B57\u7B26'0'\uFF0C\u8865\u8DB3\u4F4D\u6570
                j = strlen(set_val_str);
                for(i=0; i<j; i++)
                    set_val_str[i] = toupper(set_val_str[i]);

                for(i=0; i<j; i++)
                {
                    if( isdigit(set_val_str[i]) )		//is digit
                        continue;
                    if( (set_val_str[i] >= 'A') && (set_val_str[i] <= 'F'))	//is A to F
                        continue;
                    break;
                }
                if(i<j) //\u542B\u4E0D\u662F\u6570\u5B57\u800C\u4E14\u4E5F\u4E0D\u662FA\u81F3F\u7684\u5B57\u7B26\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                {
                    strcpy(temp_str1,"3");
                    sprintf(temp_str2,"%d",child_index);
                    strcat(temp_str1,temp_str2);
                    j=atoi(temp_str1);
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=j;
                    mib_curr_node=agent_tmp_node;
                    return ;
                }

                i =( (param_node->byte_end * 8 + 1 + param_node->bit_end) - (param_node->byte_begin * 8  - param_node->bit_begin) )/4;
                if(j > i)	//\u5B57\u7B26\u4E32\u957F\u5EA6\u5927\u4E8E\u6240\u80FD\u5B58\u50A8\u7684\u4F4D\u6570\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                {
                    strcpy(temp_str1,"3");
                    sprintf(temp_str2,"%d",child_index);
                    strcat(temp_str1,temp_str2);
                    j=atoi(temp_str1);
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=j;
                    mib_curr_node=agent_tmp_node;
                    return ;
                }

                //\u8F93\u5165\u6709\u6548\uFF0C\u5219\u5C06\u9AD8\u4F4D\u8865\u6210'0'
                k = i-j;
                for(j=i-1; (j-k)>=0 && j!=0 ; j--)
                    set_val_str[j]=set_val_str[j-k];
                set_val_str[i] = '\0';
                for(j=0; j<k; j++)
                    set_val_str[j]='0';
                break;
            case 2:	//keepHex_low_0		//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u5B57\u7B26\u4E32set_val_str\u662F\u5426\u6709\u6548, \u5982\u679C\u6709\u6548\u5219\u5C06\u4F4E\u4F4D\u8865\u6210\u5B57\u7B26'0'\uFF0C\u8865\u8DB3\u4F4D\u6570
                j = strlen(set_val_str);
                for(i=0; i<j; i++)
                    set_val_str[i] = toupper(set_val_str[i]);

                for(i=0; i<j; i++)
                {
                    if( isdigit(set_val_str[i]) )		//is digit
                        continue;
                    if( (set_val_str[i] >= 'A') && (set_val_str[i] <= 'F'))	//is A to F
                        continue;
                    break;
                }
                if(i<j) //\u542B\u4E0D\u662F\u6570\u5B57\u800C\u4E14\u4E5F\u4E0D\u662FA\u81F3F\u7684\u5B57\u7B26\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                {
                    strcpy(temp_str1,"3"); 
                    sprintf(temp_str2,"%d",child_index);
                    strcat(temp_str1,temp_str2);
                    j=atoi(temp_str1);
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=j;
                    mib_curr_node=agent_tmp_node;
                    return ;
                }

                i =( (param_node->byte_end * 8 + 1 + param_node->bit_end) - (param_node->byte_begin * 8  - param_node->bit_begin) )/4;
                if(j > i)	//\u5B57\u7B26\u4E32\u957F\u5EA6\u5927\u4E8E\u6240\u80FD\u5B58\u50A8\u7684\u4F4D\u6570\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                {
                    strcpy(temp_str1,"3");
                    sprintf(temp_str2,"%d",child_index);
                    strcat(temp_str1,temp_str2);
                    j=atoi(temp_str1);
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=j;
                    mib_curr_node=agent_tmp_node;
                    return ;
                }

                //\u8F93\u5165\u6709\u6548\uFF0C\u5219\u5C06\u4F4E\u4F4D\u8865\u6210'0'
                for(; j<i; j++)
                    set_val_str[j]='0';
                set_val_str[j] = '\0';
                break;
            case 3:	//keepHex_low_E		//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u5B57\u7B26\u4E32set_val_str\u662F\u5426\u6709\u6548, \u5982\u679C\u6709\u6548\u5219\u5C06\u4F4E\u4F4D\u8865\u6210\u5B57\u7B26'E'\uFF0C\u8865\u8DB3\u4F4D\u6570
                j = strlen(set_val_str);
                for(i=0; i<j; i++)
                    set_val_str[i] = toupper(set_val_str[i]);

                for(i=0; i<j; i++)
                {
                    if( isdigit(set_val_str[i]) )		//is digit
                        continue;
                    if( (set_val_str[i] >= 'A') && (set_val_str[i] < 'E'))	//is A to D
                        continue;
                    break;
                }
                if(i<j) //\u6709\u4E0D\u662F\u6570\u5B57\u800C\u4E14\u4E5F\u4E0D\u662FA\u81F3D\u7684\u5B57\u7B26\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                {
                    strcpy(temp_str1,"3");
                    sprintf(temp_str2,"%d",child_index);
                    strcat(temp_str1,temp_str2);
                    j=atoi(temp_str1);
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=j;
                    mib_curr_node=agent_tmp_node;
                    return ;
                }

                i =( (param_node->byte_end * 8 + 1 + param_node->bit_end) - (param_node->byte_begin * 8  - param_node->bit_begin) )/4;
                if(j > i)	//\u5B57\u7B26\u4E32\u957F\u5EA6\u5927\u4E8E\u6240\u80FD\u5B58\u50A8\u7684\u4F4D\u6570\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                {
                    strcpy(temp_str1,"3");
                    sprintf(temp_str2,"%d",child_index);
                    strcat(temp_str1,temp_str2);
                    j=atoi(temp_str1);
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=j;
                    mib_curr_node=agent_tmp_node;
                    return ;
                }

                //\u8F93\u5165\u6709\u6548\uFF0C\u5219\u5C06\u4F4E\u4F4D\u8865\u6210E
                for(; j<i; j++)
                    set_val_str[j]='E';
                set_val_str[j] = '\0';
                break;
            case 4:	//keepHex_low_F		//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u5B57\u7B26\u4E32set_val_str\u662F\u5426\u6709\u6548, \u5982\u679C\u6709\u6548\u5219\u5C06\u4F4E\u4F4D\u8865\u6210\u5B57\u7B26'F'\uFF0C\u8865\u8DB3\u4F4D\u6570
                j = strlen(set_val_str);
                for(i=0; i<j; i++)
                    set_val_str[i] = toupper(set_val_str[i]);

                for(i=0; i<j; i++)
                {
                    if( isdigit(set_val_str[i]) )		//is digit
                        continue;
                    if( (set_val_str[i] >= 'A') && (set_val_str[i] < 'F'))	//is A to E
                        continue;
                    break;
                }
                if(i<j) //\u6709\u4E0D\u662F\u6570\u5B57\u800C\u4E14\u4E5F\u4E0D\u662FA\u81F3E\u7684\u5B57\u7B26\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                {
                    strcpy(temp_str1,"3");
                    sprintf(temp_str2,"%d",child_index);
                    strcat(temp_str1,temp_str2);
                    j=atoi(temp_str1);
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=j;
                    mib_curr_node=agent_tmp_node;
                    return ;
                }

                i =( (param_node->byte_end * 8 + 1 + param_node->bit_end) - (param_node->byte_begin * 8  - param_node->bit_begin) )/4;
                if(j > i)	//\u5B57\u7B26\u4E32\u957F\u5EA6\u5927\u4E8E\u6240\u80FD\u5B58\u50A8\u7684\u4F4D\u6570\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                {
                    strcpy(temp_str1,"3");
                    sprintf(temp_str2,"%d",child_index);
                    strcat(temp_str1,temp_str2);
                    j=atoi(temp_str1);
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=j;
                    mib_curr_node=agent_tmp_node;
                    return ;
                }

                //\u8F93\u5165\u6709\u6548\uFF0C\u5219\u5C06\u4F4E\u4F4D\u8865\u6210'F'
                for(; j<i; j++)
                    set_val_str[j]='F';
                set_val_str[j] = '\0';
                break;
            case 5:	//toAscii_low_0			//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u5B57\u7B26\u4E32set_val_str\u662F\u5426\u6709\u6548, \u5982\u679C\u6709\u6548\u5219\u5C06\u4F4E\u4F4D\u8865\u6210'0'\uFF0C\u8865\u8DB3\u4F4D\u6570
                j = strlen(set_val_str);

                i =( (param_node->byte_end * 8 + 1 + param_node->bit_end) - (param_node->byte_begin * 8  - param_node->bit_begin) )/8;
                if(j > i)	//\u5B57\u7B26\u4E32\u957F\u5EA6\u5927\u4E8E\u6240\u80FD\u5B58\u50A8\u7684\u4F4D\u6570\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                {
                    strcpy(temp_str1,"3");
                    sprintf(temp_str2,"%d",child_index);
                    strcat(temp_str1,temp_str2);
                    j=atoi(temp_str1);
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=j;
                    mib_curr_node=agent_tmp_node;
                    return ;
                }

                //\u8F93\u5165\u6709\u6548\uFF0C\u5219\u5C06\u4F4E\u4F4D\u8865\u6210'0'
                for(; j<i; j++)
                    set_val_str[j]='0';
                set_val_str[j] = '\0';
                break;
            case 6:	//keepAscii_low_F			//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u5B57\u7B26\u4E32set_val_str\u662F\u5426\u6709\u6548, \u5982\u679C\u6709\u6548\u5219\u5C06\u4F4E\u4F4D\u8865\u6210\u5B57\u7B26F\uFF0C\u8865\u8DB3\u4F4D\u6570
                j = strlen(set_val_str);

                i =( (param_node->byte_end * 8 + 1 + param_node->bit_end) - (param_node->byte_begin * 8  - param_node->bit_begin) )/8;
                if(j > i)	//\u5B57\u7B26\u4E32\u957F\u5EA6\u5927\u4E8E\u6240\u80FD\u5B58\u50A8\u7684\u4F4D\u6570\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                {
                    strcpy(temp_str1,"3");
                    sprintf(temp_str2,"%d",child_index);
                    strcat(temp_str1,temp_str2);
                    j=atoi(temp_str1);
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=j;
                    mib_curr_node=agent_tmp_node;
                    return ;
                }

                //\u8F93\u5165\u6709\u6548\uFF0C\u5219\u5C06\u4F4E\u4F4D\u8865\u6210'F'
                for(; j<i; j++)
                    set_val_str[j]='F';
                set_val_str[j] = '\0';
                break;
            }
            switch(param_node->input_ptr->input_param_type)//  set
            {
            case 0:	//toDec_high_0
                index3 = sum = 0;
                for(index=param_node->byte_end; index >=param_node->byte_begin; index--)
                {
                    if(index == param_node->byte_begin)
                        index2 = param_node->bit_begin;
                    else
                        index2 = 0;
                    for(; index2<8; index2++)
                    {
                        if((set_val & (BYTE)my_pow(2, index3)) == 0)
                            recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] & (255 - my_pow(2, index2));
                        else
                            recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] | my_pow(2, index2);
                        index3++;
                        if( (index == param_node->byte_begin) &&	(((param_node->byte_begin==param_node->byte_end)&& index2 == param_node->bit_end) || ((param_node->byte_begin!=param_node->byte_end)&& index2==7)))
                            break;
                        
                    }
                    if( (index == param_node->byte_begin) &&	(((param_node->byte_begin==param_node->byte_end)&& index2 == param_node->bit_end) || ((param_node->byte_begin!=param_node->byte_end)&& index2==7)))
                        break;
                }
                break;
            case 1:	//keepHex_high_0
            case 2:	//keepHex_low_0
            case 3:	//keepHex_low_E
            case 4:	//keepHex_low_F

                index3 = index4 = sum = sub_sum = 0;
                for(index=param_node->byte_begin; index <= param_node->byte_end; index++)
                {
                    if(index == param_node->byte_begin)
                        index2 = param_node->bit_begin;
                    else
                        index2 = 0;
                    if((index3 == 0) && ( (index4+1) <= strlen(set_val_str) )) //\u53D6\u4E24\u4F4D\u5B57\u7B26
                    {
                        set_val = 0;
                        if( isdigit(set_val_str[index4]) )	//\u6570\u5B57\u5B57\u7B26
                            set_val += (set_val_str[index4++] - '0') * 16;
                        else								// A \u81F3 E \u4E4B\u95F4
                            set_val += (set_val_str[index4++] - 'A' + 10) * 16;

                        if( isdigit(set_val_str[index4]) )
                            set_val += set_val_str[index4++] - '0';
                        else
                            set_val += set_val_str[index4++] - 'A' + 10;
                    }

                    for(; index2<8; index2++)
                    {
                        if((set_val & (BYTE)my_pow(2, index3)) == 0)
                            recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] & (255 - my_pow(2, index2));
                        else
                            recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] | my_pow(2, index2);
                        if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                            break;
                        index3++;
                        if(index3 == 8)
                            index3 = 0;
                    }
                    if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                        break;
                }
                break;
            case 5:	//toAscii_low_0
            case 6:	//keepAscii_low_F

                index3 = index4 = sum = sub_sum = 0;
                for(index=param_node->byte_begin; index <= param_node->byte_end; index++)
                {
                    if(index == param_node->byte_begin)
                        index2 = param_node->bit_begin;
                    else
                        index2 = 0;
                    if(index3 == 0) //\u53D6\u4E00\u4F4D\u5B57\u7B26
                        set_val = set_val_str[index4++];

                    for(; index2<8; index2++)
                    {
                        if((set_val & (BYTE)my_pow(2, index3)) == 0)
                            recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] & (255 - my_pow(2, index2));
                        else
                            recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] | my_pow(2, index2);
                        if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                            break;
                        index3++;
                        if(index3 == 8)
                            index3 = 0;
                    }
                    if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                        break;
                }
                break;
            }
        }


    }
    j=recv_msgbuf.var[0].msglen;
    
    for(i=0;i<j;i++)
    {
        set_val_str[i]=recv_msgbuf.var[0].msg[i];
    }
     
    //strcpy(set_val_str,recv_msgbuf.var[0].msg);
    //BcdToAscii (set_val_str, recv_msgbuf.var[0].msg, recv_msgbuf.var[0].msglen);
    //set the ip
    if(snmp_agent_packet.total_ip)
    {
        for(i=0;i<16;i++)
        {
            Set_IP[i]=0;
            IP_Selected[i]=0;
            Peer_IP[i]=0;
        }
        ip_num=0;
        for(m=0;m<snmp_agent_packet.total_ip;m++)
        {
            strcpy(temp_str1,snmp_agent_packet.dst_ip[m]);
            i=j=k=0;
            while(*(temp_str1+i)!='\0')
            {
                for(j=i;*(temp_str1+j)!='.' && *(temp_str1+j)!='\0';j++)
                {
                    if(!isdigit(*(temp_str1+j)))
                        break;
                }
                if((*(temp_str1+j)=='.' && k<3) || (*(temp_str1+j)=='\0' && k==3))
                {
                    k++;
                    p=temp_str1+i;
                    strncpy(temp_str2,p,j-i);
                    temp_str2[j-i]='\0';
                    ip[m][k-1]=atoi(temp_str2);
                }
                else if((*(temp_str1+j)=='\0' && k!=3) || !isdigit(*(temp_str1+j)))
                {
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=4;
                    mib_curr_node=agent_tmp_node;
                    return ;
                }
                if(*(temp_str1+j)=='\0')
                    i=j;
                else
                    i=j+1;
            }
            for(j=0;j<4;j++)
            {
                Set_IP[ip_num] += ip[m][j] * my_pow(256, j);
            }
            ip_num++;
            if(ip_num>15)
                break;
        }
        for(i=0;i<=ip_num;i++)
        {
            Peer_IP[i]=Set_IP[i];
            IP_Selected[i]=1;
        }
        for(ip_index=0;ip_index<16;ip_index++)
        {
            if(IP_Selected[ip_index] != 1)
                continue;
            send_addr.remote_ip = Peer_IP[ip_index];
            break;
        }
        if(ip_index>=16)
        {
            debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
            debug_snmp_agent_buf[debug_agent_index].result_state=4;
            mib_curr_node=agent_tmp_node;
            return ;
        }
    }
    else
        debug_mmi_find_host("");
    send_addr.local_ip = Local_IP;
    send_addr.remote_port = SNMP_PORT;
    send_addr.local_port = DEBUG_SNMP_PORT;

    send_addr.broadcast = 0;
    send_msgbuf.pdu_type = 0;
    send_msgbuf.request_id = 0;
    send_msgbuf.var_num = 1;
    for(i=0; i<32; i++)
        send_msgbuf.var[0].oid[i] = mib_curr_node->oid[i];
    send_msgbuf.var[0].oid[mib_curr_node->oid_len] = (DWORD)mib_curr_node->curr_instance;
    send_msgbuf.var[0].oidlen = mib_curr_node->oid_len + 1;
    recv_msgbuf.pdu_type = 2;
    recv_msgbuf.request_id = 0;
    recv_msgbuf.var_num = 1;
    for(i=0; i<32; i++)
        recv_msgbuf.var[0].oid[i] = mib_curr_node->oid[i];
    recv_msgbuf.var[mib_curr_node->oid_len].oid[i] = (DWORD)mib_curr_node->curr_instance;
    recv_msgbuf.var[0].oidlen = mib_curr_node->oid_len + 1;

    for(i=0; i<256; i++)
        recv_msgbuf.var[0].msg[i] = 0;

    recv_addr.remote_ip = send_addr.remote_ip;
    recv_addr.local_ip = Local_IP;
    recv_addr.remote_port = SNMP_PORT;
    recv_addr.local_port = DEBUG_SNMP_PORT;
    recv_addr.broadcast = 0;

    snmp_agent_flag=2;
    debug_snmp_agent_buf[debug_agent_index].cmm_state=3;
    ip_index = 0;
    set_state = 1;
    debug_cfg_end = 0;
    return  ;
}

void debug_get_agent()  
{
    DWORD 					oid[32];
    mib_node        *find_node_ptr;
    char            *p,temp_str1[16],temp_str2[4];
    WORD            ip[16][4];
    DWORD           Set_IP[16];
    BYTE            m;
    WORD            i,j,k;
    static  BYTE    t1s; 

    if(snmp_agent_flag==0 && (get_state!=0 || set_state!=0))
       return ;
       
    if(bind_num>0 && snmp_agent_flag>0)   
    {
        for(i=0;i<MAX_PACKET_NUM;i++)
        {
            if(debug_snmp_agent_buf[i].cmm_state==3)
                return ;
        }
        for(i=0;i<MAX_PACKET_NUM;i++)
        {
            if(debug_snmp_agent_buf[i].cmm_state==5)
                break;
        }
        if(i!=MAX_PACKET_NUM)     
        {
            if(t1s++>50)
            {
                    t1s=0;
                    bind_num=0;
                    get_state=0;
                    snmp_agent_flag=0;
                    for(i=0;i<MAX_PACKET_NUM;i++)
                        debug_snmp_agent_buf[i].cmm_state=0;
                    return ;
            }
        }
        for(i=0;i<MAX_PACKET_NUM;i++)
        {
            if(debug_snmp_agent_buf[i].cmm_state==0)
                break ;
        }
        if(i==MAX_PACKET_NUM)
            return;
        else
            t1s=0 ; 
        for(i=0;i<MAX_PACKET_NUM;i++)
        {
        	 if(debug_snmp_agent_buf[i].cmm_state==0)
        	    continue;
        }
        if(i!=MAX_PACKET_NUM)
           printf("All the buffer do not set 0\r\n"); 
   	    mib_curr_node->curr_instance+=bind_num-1;	 	
		   
		    if(mib_curr_node->curr_instance==mib_curr_node->total_instance-1)
		    {
		    	  bind_num=0;
            get_state=0;
            snmp_agent_flag=0;
            mib_curr_node->curr_instance=0;
            mib_curr_node=agent_tmp_node;
            return; 
		    }
        if(mib_curr_node->curr_instance<mib_curr_node->total_instance-MAX_PACKET_NUM)
        {
            //mib_curr_node->curr_instance+=bind_num; 
			      mib_curr_node->curr_instance++;			    
            snmp_agent_flag=1;
            for(i=0;i<MAX_PACKET_NUM;i++)
               debug_snmp_agent_buf[i].cmm_state=0;
            goto bind_set;
        }
        else if(mib_curr_node->curr_instance<mib_curr_node->total_instance-1)
        { 
        	  mib_curr_node->curr_instance++;
            bind_num=mib_curr_node->total_instance+1-mib_curr_node->curr_instance;          
            snmp_agent_flag=1;
            for(i=0;i<MAX_PACKET_NUM;i++)
               debug_snmp_agent_buf[i].cmm_state=0;
            goto bind_set;
        }
        else
        {
            bind_num=0;
            get_state=0;
            snmp_agent_flag=0;
            debug_get_all_value=0;
            mib_curr_node->curr_instance=0;
            mib_curr_node=agent_tmp_node; 
            return;
        }
    }


    for(i=0;i<MAX_PACKET_NUM;i++)
    {
        if(debug_snmp_agent_buf[i].cmm_state==3)
            return ;
    }
    for(i=0;i<MAX_PACKET_NUM;i++)
    {
        if(debug_snmp_agent_buf[i].cmm_state==1)
            break ;
    }
    if(i==MAX_PACKET_NUM)
        return;
    debug_agent_index=i;
    memcpy(&snmp_agent_packet,debug_snmp_agent_buf+debug_agent_index,sizeof(debug_snmp_agent));



    for(i=0;i<snmp_agent_packet.oid_len-1;i++)
    {
        oid[i]=snmp_agent_packet.oid[i];
    }
    find_node_ptr=search_mib_node(mib_root_node,oid,snmp_agent_packet.oid_len-1);
    if(find_node_ptr==NULL)
    {
        debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
        debug_snmp_agent_buf[debug_agent_index].result_state=3;        
        return ;
    }
    agent_tmp_node=mib_curr_node;    
    mib_curr_node=find_node_ptr;
    snmp_agent_packet.total_field=mib_curr_node->total_child_nodes;
    if(mib_curr_node->max_access ==0 ||  mib_curr_node->max_access ==1)
    {
        debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
        debug_snmp_agent_buf[debug_agent_index].result_state=6;
        mib_curr_node=agent_tmp_node;
        return ;
    }

    if(snmp_agent_packet.oid[snmp_agent_packet.oid_len-1]>mib_curr_node->total_instance)
    {
        debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
        debug_snmp_agent_buf[debug_agent_index].result_state=5;
        mib_curr_node=agent_tmp_node;
        return ;
    }
    if(mib_curr_node->total_instance==snmp_agent_packet.total_instance && mib_curr_node->total_instance>=MAX_PACKET_NUM )
    {
    	  if(debug_agent_index==0 &&  !debug_snmp_agent_buf[debug_agent_index].oid[ debug_snmp_agent_buf[debug_agent_index].oid_len-1])
    	  {
	        bind_num=MAX_PACKET_NUM;
	        snmp_agent_flag=1;
	        mib_curr_node->curr_instance=0; 
        }
        else
          printf("Error : The debug SNMP Agent buffer is not start from 0,while getting all instance\r\n");
    }
    else if(mib_curr_node->total_instance==snmp_agent_packet.total_instance && mib_curr_node->total_instance<MAX_PACKET_NUM )
    {
    	  if(debug_agent_index==0 && !debug_snmp_agent_buf[debug_agent_index].oid[ debug_snmp_agent_buf[debug_agent_index].oid_len-1])
    	  {
    	  	mib_curr_node->curr_instance=0;
          bind_num=mib_curr_node->total_instance;
          snmp_agent_flag=1;
        }
       else
          printf("Error : The debug SNMP Agent buffer is not start from 0,while getting all instance\r\n");
    }
    else
    {
        bind_num=1;
        snmp_agent_flag=1;
        mib_curr_node->curr_instance=snmp_agent_packet.oid[snmp_agent_packet.oid_len-1];
    }
    debug_get_all_value=mib_curr_node->total_child_nodes;

    //set the ip
    if(snmp_agent_packet.total_ip)
    {
        for(i=0;i<16;i++)
        {
            Set_IP[i]=0;
            IP_Selected[i]=0;
            Peer_IP[i]=0;
        }
        ip_num=0;
        for(m=0;m<snmp_agent_packet.total_ip;m++)
        {
            strcpy(temp_str1,snmp_agent_packet.dst_ip[m]);
            i=j=k=0;
            while(*(temp_str1+i)!='\0')
            {
                for(j=i;*(temp_str1+j)!='.' && *(temp_str1+j)!='\0';j++)
                {
                    if(!isdigit(*(temp_str1+j)))
                        break;
                }
                if((*(temp_str1+j)=='.' && k<3) || (*(temp_str1+j)=='\0' && k==3))
                {
                    k++;
                    p=temp_str1+i;
                    strncpy(temp_str2,p,j-i);
                    temp_str2[j-i]='\0';
                    ip[m][k-1]=atoi(temp_str2);
                }
                else if((*(temp_str1+j)=='\0' && k!=3) || !isdigit(*(temp_str1+j)))
                {
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=4;
                    mib_curr_node=agent_tmp_node;
                    return ;
                }
                if(*(temp_str1+j)=='\0')
                    i=j;
                else
                    i=j+1;
            }
            for(j=0;j<4;j++)
            {
                Set_IP[ip_num] += ip[m][j] * my_pow(256, j);
            }
            ip_num++;
            if(ip_num>15)
                break;
        }
        for(i=0;i<=ip_num;i++)
        {
            Peer_IP[i]=Set_IP[i];
            IP_Selected[i]=1;
        }
        for(ip_index=0;ip_index<16;ip_index++)
        {
            if(IP_Selected[ip_index] != 1)
                continue;
            send_addr.remote_ip = Peer_IP[ip_index];
            break;
        }
        if(ip_index>=16)
        {
            debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
            debug_snmp_agent_buf[debug_agent_index].result_state=4;
            mib_curr_node=agent_tmp_node;
            return ;
        }
    }
    else
        debug_mmi_find_host("");



bind_set:
    send_msgbuf.pdu_type = 0;
    send_msgbuf.request_id = 0;
    send_msgbuf.var_num = bind_num;
    for(j=0;j<bind_num;j++)
    {
        for(i=0; i<32; i++)
            send_msgbuf.var[j].oid[i] = mib_curr_node->oid[i];
        send_msgbuf.var[j].oid[mib_curr_node->oid_len] = (DWORD)mib_curr_node->curr_instance+j;
        send_msgbuf.var[j].oidlen = mib_curr_node->oid_len + 1;
    }

    send_addr.local_ip = Local_IP;
    send_addr.remote_port = SNMP_PORT;
    send_addr.local_port = DEBUG_SNMP_PORT;
    send_addr.broadcast = 0;

    recv_msgbuf.pdu_type = 2;
    recv_msgbuf.request_id = 0;
    recv_msgbuf.var_num = bind_num;
    for(j=0;j<bind_num;j++)
    {
        for(i=0; i<32; i++)
            recv_msgbuf.var[j].oid[i] = mib_curr_node->oid[i];
        recv_msgbuf.var[j].oid[mib_curr_node->oid_len] = (DWORD)mib_curr_node->curr_instance+j;
        recv_msgbuf.var[j].oidlen = mib_curr_node->oid_len + 1;
        for(i=0; i<256; i++)
            recv_msgbuf.var[j].msg[i] = 0;
    }

    recv_addr.remote_ip = send_addr.remote_ip;
    recv_addr.local_ip = Local_IP;
    recv_addr.remote_port = SNMP_PORT;
    recv_addr.local_port = DEBUG_SNMP_PORT;
    recv_addr.broadcast = 0;

    get_state = 1;
    debug_cfg_end = 0;
    if(bind_num!=MAX_PACKET_NUM)
        debug_snmp_agent_buf[debug_agent_index].cmm_state=3; 
    else
        for(i=0;i<bind_num;i++)
        {
            debug_snmp_agent_buf[i].cmm_state=3;
        }
    snmp_agent_flag=1;
    return ;
}

//////////////////////////////End of snmp senior agnet////////////







///////////////////////////////Operation get-set/////////////////

/*
*
*       Fun:    debug_mmi_get_all
*
*       Desc:   handle the get_all command, if valid, start the debug_snmp_get_rt
*
*       Ret:    return 1 if successful, return 0 if fail
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
int debug_mmi_get_all()
{
    int			 	i;
    char			temp_str[128];

    if (!(mib_curr_node->total_instance))
    {
        debug_ascii_out_proc("Fail    : There is no instance for the current object\r\n");
        debug_cfg_end=1;
        return 0;
    }

    if (mib_curr_node->max_access == 0)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error   : This leaf node's MAX-ACCESS property is 'INVALID'. Can not get its value.\r\n");
        debug_cfg_end=1;
        return 0;
    }
    if (mib_curr_node->max_access == 1)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error   : This leaf node's MAX-ACCESS property is 'NOT-ACCESSIBLE'. Can not get its value.\r\n");
        debug_cfg_end=1;
        return 0;
    }
    if (mib_curr_node->max_access == 6 && snmp_agent_flag==0)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error   : This leaf node's MAX-ACCESS property is 'WRITE-ONLY'. Can not get its value.\r\n");
        debug_cfg_end=1;
        return 0;
    }

    if(mib_curr_node->total_child_nodes == 0)
    {
        strcpy(paraname, mib_curr_node->name);
    }
    else
    {
        debug_get_all_value = mib_curr_node->total_child_nodes;
        if(mib_curr_node->total_child_nodes==1)
            param_node = mib_curr_node->child_nodes[0]->child_ptr;
    }

    if( (toupper(temp_str[0]) == 'R') && (toupper(temp_str[1]) == 'E') && (toupper(temp_str[2]) == 'S') && (toupper(temp_str[3]) == 'E') && (toupper(temp_str[4]) == 'R') && (toupper(temp_str[5]) == 'V') && (toupper(temp_str[6]) == 'E') && (toupper(temp_str[7]) == 'D') && (toupper(temp_str[8]) == '\0') )
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error   : This node is Reserved. Can not get its value.\r\n");
        return 0;
    }
    
    //if(print_once==0 && field_compute_state==5)
    //   print_once=2;

   
    send_msgbuf.pdu_type = 0;
    send_msgbuf.request_id = 0;
    send_msgbuf.var_num = 1;
    for(i=0; i<32; i++)
        send_msgbuf.var[0].oid[i] = mib_curr_node->oid[i];
    send_msgbuf.var[0].oid[mib_curr_node->oid_len] = (DWORD)mib_curr_node->curr_instance;
    send_msgbuf.var[0].oidlen = mib_curr_node->oid_len + 1;

    for(i=0;ip_index<ip_num;i++)
    {
        if(IP_Selected[i] != 1)
            continue;
        send_addr.remote_ip = Peer_IP[i];
        break;
    }
    if(i>=16)
    {
        debug_ascii_out_proc("Fail    : There is not host ip available.\r\n");
        return 0;
    }
    send_addr.local_ip = Local_IP;
    send_addr.remote_port = SNMP_PORT;
    send_addr.local_port = DEBUG_SNMP_PORT; 
    send_addr.broadcast = 0;

    recv_msgbuf.pdu_type = 2;
    recv_msgbuf.request_id = 0;
    recv_msgbuf.var_num = 1;
    for(i=0; i<32; i++)
        recv_msgbuf.var[0].oid[i] = mib_curr_node->oid[i];
    recv_msgbuf.var[0].oid[mib_curr_node->oid_len] = (DWORD)mib_curr_node->curr_instance;
    recv_msgbuf.var[0].oidlen = mib_curr_node->oid_len + 1;

    for(i=0; i<256; i++)
        recv_msgbuf.var[0].msg[i] = 0;

    recv_addr.remote_ip = send_addr.remote_ip;
    recv_addr.local_ip = Local_IP;
    recv_addr.remote_port = SNMP_PORT;
    recv_addr.local_port = DEBUG_SNMP_PORT;
    recv_addr.broadcast = 0;
    bind_num=1;

    ip_index = 0;
    get_state = 1;
    debug_cfg_end = 0;
    for(i=0;i<256;i++)
    {
        if(curr_instance_arr[i].eable==1)
            break;
    }
    if(i==256)
        none_instance=1;
    else
        none_instance=0;
    if(list_flag==0)
    {
        get_show_state=1;
        //show_snmp_state=1;
        show_all_value = 1;
    }

    /*
    if(show_snmp_state==1)
    {
        debug_ascii_out_proc("GetStatus:");
        debug_color_out(BRIGHT,CYAN,BLACK,"Waiting...");
    }
    //sprintf(out_text, "1    %s = ", temp_str);
    //debug_ascii_out_proc(out_text);
    */
    return 1;
}// end of debug_mmi_get_all


/*
*
*       Fun:    debug_mmi_get
*
*       Desc:   handle the get command, if valid, start the debug_snmp_get_rt
*
*       Ret:    return 1 if successful, return 0 if fail
*
*       File:   debug.c
*
*/
int debug_mmi_get(char *child_name)
{
    int 			i,ins;
    WORD 			child_index;
    mib_child_node	*child_node_ptr;
    char			out_text[256], temp_str[256];
    char      *p,str_ins_id[32];
    WORD			oid=0;

    if (!(mib_curr_node->total_instance))
    {
        debug_color_out(BRIGHT,RED,BLACK,"Fail    : There is no instance for the current object\r\n");
        debug_cfg_end=1;
        return 0;
    }
    if (!mib_curr_node->total_child_nodes)
    {
        debug_cfg_end=1;
        debug_color_out(BRIGHT,RED,BLACK,"Fail    : No child object for the current object\r\n");
        return 0;
    }

    if (mib_curr_node->max_access == 0)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error   : This leaf node's MAX-ACCESS is 'INVALID'. Can not get its value.\r\n");
        debug_cfg_end=1;
        return 0;
    }
    if (mib_curr_node->max_access == 1)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error   : This leaf node's MAX-ACCESS is 'NOT-ACCESSIBLE'. Can not get its value.\r\n");
        debug_cfg_end=1;
        return 0;
    }
    if (mib_curr_node->max_access == 6 && snmp_agent_flag==0)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error   : This leaf node's MAX-ACCESS is 'WRITE-ONLY'. Can not get its value.\r\n");
        debug_cfg_end=1;
        return 0;
    }
    for(i=0;i!=strlen(child_name);i++)
    {
        if(*(child_name+i)!=' ')
            break;
    }
    p=child_name+i;
    strcpy(child_name,p);
    if(child_name[0]=='#')//get #
    {
        p=child_name+1;
        strcpy(child_name,p);
        for(i=0;i<strlen(child_name);i++)
        {
            if(!isdigit(*(child_name+i)))
            {
                if(*(child_name+i)=='.')
                {
                    if(i==0)
                    {
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : Please input instance number!\r\n");
                        return 0;
                    }
                    str_ins_id[i]='\0';
                    break;
                }
                else
                {
                    debug_color_out(BRIGHT,RED,BLACK,"Error   : Input error. Fromat is:get #X.X!");
                    return 0;
                }
            }
            else
                str_ins_id[i]=child_name[i];
        }
        ins=strtol(str_ins_id,NULL,10);

        temp_instance=mib_curr_node->curr_instance;
        if(ins>mib_curr_node->total_instance-1)
        {
            sprintf(temp_str,"Error   : The max instance id is %d\r\n",mib_curr_node->total_instance-1);
            debug_color_out(BRIGHT,RED,BLACK,temp_str);
            return 0;
        }
        mib_curr_node->curr_instance=ins;
        if(i==strlen(child_name))       										 //#3
        {
            debug_get_all_value= mib_curr_node->total_child_nodes;
        }
        else
        {
            p=child_name+i+1;
            strcpy(child_name,p);
            if (isdigit(child_name[0]))                        //#3.2
            {
                oid = strtoul(child_name, NULL, 10);
                if(oid>0)
                {
                  oid--;
                  child_index = find_child_index(mib_curr_node, oid, NULL, 0);
                }
                else
                {
                  debug_color_out(BRIGHT,RED,BLACK,"Fail    : There is no such child.\n");
                  return 0;
                }
            }
            else                                              //#3.ENABLE
                child_index = find_child_index(mib_curr_node, 0, child_name, 1);

            if (child_index  < MIB_MAX_CHILD_NODES_PER_NODE)
            {
                child_node_ptr = mib_curr_node->child_nodes[child_index];
                if (!child_node_ptr->child_ptr)
                {
                    debug_color_out(BRIGHT,RED,BLACK,"Fail    : No such child object for the current object\r\n");
                    return 0;
                }
                debug_get_all_value= 301;
                param_node = (mib_node_param *)child_node_ptr->child_ptr;
            }
            else
            {
                debug_color_out(BRIGHT,RED,BLACK,"Fail    : There is no such child.\n");
                return 0;
            }
            strcpy(temp_str, param_node->name);
            if( (toupper(temp_str[0]) == 'R') && (toupper(temp_str[1]) == 'E') && (toupper(temp_str[2]) == 'S') && (toupper(temp_str[3]) == 'E') && (toupper(temp_str[4]) == 'R') && (toupper(temp_str[5]) == 'V') && (toupper(temp_str[6]) == 'E') && (toupper(temp_str[7]) == 'D') && (toupper(temp_str[8]) == '\0') )
            {
                sprintf(out_text, "Error   : %s is Reserved. Can not get its value.\r\n", param_node->name);
                debug_color_out(BRIGHT,RED,BLACK,out_text);
                return 0;
            }
        }
    }
    else if(child_name[0]=='*' && child_name[1]=='\0') //get *
    {
        debug_mmi_get_all();
        return 1;
    }
    else if(strncmp(child_name,"*.",2)==0)  //get *.
    {
        p=child_name+2;
        strcpy(child_name,p);

        if (isdigit(child_name[0]))
        {
            oid = strtoul(child_name, NULL, 10);
            if(oid>0) 
            {
            	oid--;
              child_index = find_child_index(mib_curr_node, oid, NULL, 0);
            }
            else
            {
                debug_color_out(BRIGHT,RED,BLACK,"Fail    : No such child object for the current object\r\n");
                return 0;
            }
        }
        else
            child_index = find_child_index(mib_curr_node, 0, child_name, 1);

        if (child_index  < MIB_MAX_CHILD_NODES_PER_NODE)
        {
            child_node_ptr = mib_curr_node->child_nodes[child_index];
            if (!child_node_ptr->child_ptr)
            {
                debug_color_out(BRIGHT,RED,BLACK,"Fail    : No such child object for the current object\r\n");
                return 0;
            }
            debug_get_all_value= 301;
            param_node = (mib_node_param *)child_node_ptr->child_ptr;
        }
        else
        {
            debug_color_out(BRIGHT,RED,BLACK,"Fail    : There is no such child.\n");
            return 0;
        }
        strcpy(temp_str, param_node->name);
        if( (toupper(temp_str[0]) == 'R') && (toupper(temp_str[1]) == 'E') && (toupper(temp_str[2]) == 'S') && (toupper(temp_str[3]) == 'E') && (toupper(temp_str[4]) == 'R') && (toupper(temp_str[5]) == 'V') && (toupper(temp_str[6]) == 'E') && (toupper(temp_str[7]) == 'D') && (toupper(temp_str[8]) == '\0') )
        {
            sprintf(out_text, "Error   : %s is Reserved. Can not get its value.\r\n", param_node->name);
            debug_color_out(BRIGHT,RED,BLACK,out_text);
            return 0;
        }
    }
    /*
    if(debug_get_all_value==mib_curr_node->total_child_nodes)
       if(print_once==0 && field_compute_state==5)
          print_once=2;  
    */
    
    send_msgbuf.pdu_type = 0;
    send_msgbuf.request_id = 0;
    send_msgbuf.var_num = 1;
    for(i=0; i<32; i++)
        send_msgbuf.var[0].oid[i] = mib_curr_node->oid[i];
    send_msgbuf.var[0].oid[mib_curr_node->oid_len] = (DWORD)mib_curr_node->curr_instance;
    send_msgbuf.var[0].oidlen = mib_curr_node->oid_len + 1;

    for(ip_index=0;ip_index<16;ip_index++)
    {
        if(IP_Selected[ip_index] != 1)
            continue;
        send_addr.remote_ip = Peer_IP[ip_index];
        break;
    }
    if(ip_index>=16)
    {
        debug_ascii_out_proc("Fail    : There is not host ip available.\r\n");
        return 0;
    }
    send_addr.local_ip = Local_IP;
    send_addr.remote_port = SNMP_PORT;
    send_addr.local_port = DEBUG_SNMP_PORT;
    send_addr.broadcast = 0;

    recv_msgbuf.pdu_type = 2;
    recv_msgbuf.request_id = 0;
    recv_msgbuf.var_num = 1;
    for(i=0; i<32; i++)
        recv_msgbuf.var[0].oid[i] = mib_curr_node->oid[i];
    recv_msgbuf.var[0].oid[mib_curr_node->oid_len] = (DWORD)mib_curr_node->curr_instance;
    recv_msgbuf.var[0].oidlen = mib_curr_node->oid_len + 1;

    for(i=0; i<256; i++)
        recv_msgbuf.var[0].msg[i] = 0;

    recv_addr.remote_ip = send_addr.remote_ip;
    recv_addr.local_ip = Local_IP;
    recv_addr.remote_port = SNMP_PORT;
    recv_addr.local_port = DEBUG_SNMP_PORT;
    recv_addr.broadcast = 0;
    bind_num=1;

    get_state = 1;
    debug_cfg_end = 0;
    for(i=0;i<256;i++)
    {
        if(curr_instance_arr[i].eable==1)
            break;
    }
    if(i==256)
        none_instance=1;
    else
        none_instance=0;
    if(list_flag==0)
    {
        get_show_state=1;
        //if(contents_state!=0)
        //   show_snmp_state=1;
        show_all_value = 1;
    }
    /*if(show_snmp_state==1)
    {
        debug_ascii_out_proc("GetStatus:");
        debug_color_out(BRIGHT,CYAN,BLACK,"Waiting...");
    }*/
    // strcpy(paraname,child_node_ptr->name);

    return 1;
}	// end of deubg_mmi_get

/*
*
*       Fun:    debug_mmi_set
*
*       Desc:   handle the set command, if valid, full fill the value and start the debug_snmp_set_rt
*
*       Ret:    return 1 if successful, return 0 if fail
*
*       File:   debug.c
*
*/
int debug_mmi_set(char *set_str)
{
    int 			i,	j,	k, ins;
    BYTE			str_len;
    WORD 			child_index;
    WORD			oid=0;
    mib_child_node	*child_node_ptr;
    char 			child_name[80],	 out_text[256], *temp_ch;
    char      str_ins_id[10];
    char      p,q;
    if(save_flag==0)
        save_flag=1;

    if(strlen(set_str)>80)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error    : the string is too long !\r\n");
        return 0;
    }

    strcpy(child_name, set_str);
    for(i=0;i!=strlen(child_name);i++)
    {
        if(*(child_name+i)!=' ')
            break;
    }
    if(*(child_name+i)=='\0') //set
    {
        strcpy(set_ins_value, curr_instance_value);
        temp_instance=mib_curr_node->curr_instance;
        strcpy(child_name,"");
        set_all=1;
    }
    temp_ch=child_name+i;
    strcpy(child_name,temp_ch);
    if(child_name[0]=='#')//set #
    {
        temp_ch=child_name+1;
        strcpy(child_name,temp_ch);
        for(i=0;i<strlen(child_name);i++)
        {
            if(!isdigit(*(child_name+i)))
            {
                if(*(child_name+i)=='.'  || *(child_name+i)=='=')
                {
                    if(i==0)
                    {
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : Please input instance number!\r\n");
                        return 0;
                    }
                    str_ins_id[i]='\0';
                    break;
                }
                else
                {
                    debug_color_out(BRIGHT,RED,BLACK,"Error   : Input error. Fromat is:get #X.X!");
                    return 0;
                }
            }
            else
                str_ins_id[i]=child_name[i];
        }
        ins=strtol(str_ins_id,NULL,10);
        if(ins>mib_curr_node->total_instance-1)
        {
            sprintf(out_text,"Error   : The max instance id is %d\r\n",mib_curr_node->total_instance-1);
            debug_color_out(BRIGHT,RED,BLACK,out_text);
            return 0;
        }
        temp_instance=mib_curr_node->curr_instance;
        mib_curr_node->curr_instance=ins;

        if(*(child_name+i)=='\0')      //set #3
        {
            //debug_get_all_value= mib_curr_node->total_child_nodes;
            /* if(mib_curr_node->enable_field_id==0xFF)
             {
                  debug_color_out(BRIGHT,RED,BLACK,"Error   : Please input the value !\r\n");
                  return 0;
               }
               */


            strcpy(set_ins_value, curr_instance_value);
            strcpy(child_name,"");
            set_all=1;
        }
        else if(*(child_name+i)=='=')//#3=
        {
            if(mib_curr_node->total_child_nodes==1)
            {
                temp_ch=child_name+i+1;
                strcpy(out_text,"1=");
                strcat(out_text,temp_ch);
                strcpy(child_name,out_text);
                strcpy(set_ins_value,"");
            }
            else
            {
                temp_ch=child_name+i+1;
                if(temp_ch!=NULL)
                {
                    strcpy(child_name,temp_ch);
                    for(i=0;i<strlen(child_name);i++)
                    {
                        set_ins_value[i]=child_name[i];
                    }
                    set_ins_value[i]='\0';
                }
                strcpy(child_name,"");
                set_all=1;
            }
        }
        else if(*(child_name+i)=='.')//#3.2= or #3.NAME=
        {
            temp_ch=child_name+i+1;
            strcpy(child_name,temp_ch);
            strcpy(set_ins_value,"");
        }
        else
        {


            debug_color_out(BRIGHT,RED,BLACK,"Error   : Format is not right,set # !\r\n");
            return 0;

        }
    }
    else if(child_name[0]=='*' )//set *
    {
        temp_instance=mib_curr_node->curr_instance;
        if(child_name[1]=='\0')     //*
        {
            /*
              if(mib_curr_node->enable_field_id==0xFF)
               {
                  debug_color_out(BRIGHT,RED,BLACK,"Error   : Please input the value !\r\n");
                  return 0 ;
               }   
            */
            strcpy(set_ins_value, curr_instance_value);
            strcpy(child_name,"");
            set_all=1;
        }
        else if(child_name[1]=='=') //*=
        {
            if(mib_curr_node->total_child_nodes==1 && save_flag<3)
            {
                temp_ch=child_name+2;
                strcpy(out_text,"1=");
                strcat(out_text,temp_ch);
                strcpy(child_name,out_text);
                strcpy(set_ins_value,"");
            }
            else
            {
                if(child_name[2]!='\0')
                    temp_ch=child_name+2;
                strcpy(child_name,temp_ch);
                for(i=0;i<strlen(child_name);i++)
                {
                    set_ins_value[i]=child_name[i];
                }
                set_ins_value[i]='\0';
                strcpy(child_name,"");
                set_all=1;
            }
        }
        else if(strncmp(child_name,"*.",2)==0)  //*.2= or *.ENABLE=
        {
            temp_ch=child_name+2;
            strcpy(child_name,temp_ch);
            strcpy(set_ins_value,"");
        }
    }
    else
    {
        if(set_str[0]!='\0')
        {
            debug_color_out(BRIGHT,RED,BLACK,"Error   : Format is not right!\r\n");
            return 0;
        }
    }

    set_str_len=strlen(set_ins_value);
    for(i=0;i<set_str_len;i++)
    {
        if(*(set_ins_value+i)==' ')
        {
            for(j=i;j<set_str_len-1;j++)
                *(set_ins_value+j)=*(set_ins_value+j+1);
            *(set_ins_value+j)='\0';
        }
    }
    set_str_len=strlen(set_ins_value);
    if(set_all==1)//&& strcmp(curr_instance_value,set_ins_value)!=0)  //translate the ascii value to hex
    {
        for(i=0;i<set_str_len;i++)
        {
            if((!(*(set_ins_value+i)<=57 && *(set_ins_value+i)>=48 ))&& (!(*(set_ins_value+i)<=70 && *(set_ins_value+i)>=65)) && (!(*(set_ins_value+i)>=97 && *(set_ins_value+i)<=102)))
            {
                debug_color_out(BRIGHT,RED,BLACK,"Error   : please input 0-9 or a-f or A-F\r\n");
                return 0;
            }
            else if(isdigit(*(set_ins_value+i)))
            {
                out_text[i]=*(set_ins_value+i)-48;
            }
            else if((*(set_ins_value+i)<=70 && *(set_ins_value+i)>=65))//A-F
            {
                out_text[i]=*(set_ins_value+i)-55;
            }
            else if((*(set_ins_value+i)>=97 && *(set_ins_value+i)<=102))//a-f
            {
                out_text[i]=*(set_ins_value+i)-87;
            }
        }
        //strncpy(set_ins_value,out_text,set_str_len);
        i=0;
        j=0;
        k=0;
        while(i<set_str_len)
        {
            if(set_str_len%2==1 && i==set_str_len-1)
            {
                p=*(out_text+i);
                p=p* my_pow(2,4);
                set_ins_value[k]=p;
                break;
            }
            q=*(out_text+i);
            i++;
            p=*(out_text+i);
            i++;
            p+=(q)* my_pow(2,4);
            set_ins_value[k]=p;
            k++;
        }
    }

    if(strlen(child_name))//#3.2= or  #3.ENABLE= or *.2= or *.ENABLE=
    {
        if(!(temp_ch = strchr(child_name, '=')))
        {
            debug_ascii_out_proc("Fail    : There is no equatation in the set command.\r\n");
            return 0;
        }
        else
        {
            //*temp_ch = '\0';
            strcpy(set_val_str, "");
            for(child_index=1; temp_ch[child_index]==' '; child_index++) 	//\u53BB\u9664 = \u53F7\u540E\u9762\u7684\u7A7A\u683C
                ;
            temp_ch = &temp_ch[child_index];

            for(child_index=0; temp_ch[child_index]!='\0'; child_index++)
                set_val_str[child_index] = temp_ch[child_index];
            set_val_str[child_index] = '\0';
            j = strlen(set_val_str);
            for(i=0; i<j; i++)
            {
                if( !isxdigit(set_val_str[i]))
                    break;
            }
            if(i<j)
                set_val = -1; 
            else
            {
               for(i=0; i<j; i++)
               {
                if( !isdigit(set_val_str[i]))
                    break;
               }
               if(i<j)
                set_val =strtoul(set_val_str,NULL,16);
               else
                set_val =strtoul(set_val_str,NULL,0);
           }
        }

        if (!(mib_curr_node->total_instance))
        {
            debug_ascii_out_proc("Fail    : There is no instance for the current object\r\n");
            return 0;
        }

        if (!(str_len = strlen(child_name)))
        {
            debug_ascii_out_proc("Fail    : No child name in the set command.\r\n");
            return 0;
        }

        if (!mib_curr_node->total_child_nodes)
        {
            debug_ascii_out_proc("Fail    : No child object for the current object\r\n");
            return 0;
        }

        if (mib_curr_node->max_access == 0)
        {
            debug_color_out(BRIGHT,RED,BLACK,"Error   : This leaf node's MAX-ACCESS property is 'INVALID'. Can not set its value.\r\n");
            return 0;
        }
        if (mib_curr_node->max_access == 1)
        {
            debug_color_out(BRIGHT,RED,BLACK,"Error   : This leaf node's MAX-ACCESS property is 'NOT-ACCESSIBLE'. Can not set its value.\r\n");
            return 0;
        }
        if (mib_curr_node->max_access == 3)
        {
            debug_color_out(BRIGHT,RED,BLACK,"Error   : This leaf node's MAX-ACCESS property is 'READ-ONLY'. Can not set its value.\r\n");
            return 0;
        }

        child_name[str_len] = '\0';

        if (isdigit(child_name[0]))
        {
            oid = strtoul(child_name, NULL, 10)-1; 
            child_index = find_child_index(mib_curr_node, oid, NULL, 0);
        }
        else
            child_index = find_child_index(mib_curr_node, 0, child_name, 1);

        if (child_index  < MIB_MAX_CHILD_NODES_PER_NODE)
        {
            child_node_ptr = mib_curr_node->child_nodes[child_index];
            if (!child_node_ptr->child_ptr)
            {
                debug_ascii_out_proc("Fail    : No such child object for the current object\r\n");
                return 0;
            }
            param_node = (mib_node_param *)child_node_ptr->child_ptr;
            if(param_node->select_total != 0)		//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u662F\u5426\u6709\u6548\uFF0C\u662F\u5426\u5B58\u5728\u8BE5\u6570\u503C\u4EE3\u8868\u7684select\u9009\u9879
            {
                if(set_val == -1)
                {
                    debug_color_out(BRIGHT,RED,BLACK,"Error   : The select option value must be digit or a-f.\r\n");
                    return 0;
                }

                for(i=0; i<param_node->select_total; i++)
                {
                    if(param_node->select_ptr[i]->number == set_val)
                        break;
                }
                if(i >= param_node->select_total)
                {
                    sprintf( out_text, "Fail    : The value %u is not exist in the select options:\r\n", set_val);
                    debug_color_out(BRIGHT,RED,BLACK,out_text);
                    sprintf(out_text,"%d",param_node->id+1);
                    debug_mmi_option(out_text);
                    return 0;
                }
            }
            if(param_node->input_ptr != NULL)
            {
                switch(param_node->input_ptr->input_param_type)
                {
                case 0:	//toDec_high_0		//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u662F\u5426\u6709\u6548
                    j = strlen(set_val_str);
                    for(i=0; i<j; i++)
                    {
                        if( !isdigit(set_val_str[i]) )
                            break;
                    }
                    if(i<j)	// set_val_str is not a digital string
                    {
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : The value must be digit.\r\n");
                        return 0;
                    }
                    else
                    {
                        /*
                        if(j>(param_node->byte_end-param_node->byte_begin+(param_node->bit_end-param_node->bit_begin+1)/8))
                        {
                            sprintf( out_text, "Error   : The value must be less than %d byte!\r\n",(param_node->byte_end-param_node->byte_begin+(param_node->bit_end-param_node->bit_begin+1)/8));
                             debug_ascii_out_proc(out_text);
                             return 0; 
                         }
                         */
                        j =(param_node->byte_end * 8 + 1 + param_node->bit_end) - (param_node->byte_begin * 8  - param_node->bit_begin);
                        set_val = atol(set_val_str);
                        if(set_val > my_pow(2, j))
                        {
                            sprintf( out_text, "Error   : The value must be less than %u.\r\n", (my_pow(2,j)-1) );
                            debug_ascii_out_proc(out_text);
                            return 0;
                        }
                    }
                    break;
                case 1:	//keepHex_high_0		//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u5B57\u7B26\u4E32set_val_str\u662F\u5426\u6709\u6548, \u5982\u679C\u6709\u6548\u5219\u5C06\u9AD8\u4F4D\u8865\u6210\u5B57\u7B26'0'\uFF0C\u8865\u8DB3\u4F4D\u6570
                    j = strlen(set_val_str);
                    for(i=0; i<j; i++)
                        set_val_str[i] = toupper(set_val_str[i]);

                    for(i=0; i<j; i++)
                    {
                        if( isdigit(set_val_str[i]) )		//is digit
                            continue;
                        if( (set_val_str[i] >= 'A') && (set_val_str[i] <= 'F'))	//is A to F
                            continue;
                        break;
                    }
                    if(i<j) //\u542B\u4E0D\u662F\u6570\u5B57\u800C\u4E14\u4E5F\u4E0D\u662FA\u81F3F\u7684\u5B57\u7B26\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                    {
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : The value must be Hex.\r\n");
                        return 0;
                    }

                    i =( (param_node->byte_end * 8 + 1 + param_node->bit_end) - (param_node->byte_begin * 8  - param_node->bit_begin) )/4;
                    if(j > i)	//\u5B57\u7B26\u4E32\u957F\u5EA6\u5927\u4E8E\u6240\u80FD\u5B58\u50A8\u7684\u4F4D\u6570\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                    {
                        sprintf(out_text, "Error   : The length of the value string must be less than %d.\r\n", i);
                        debug_ascii_out_proc(out_text);
                        return 0;
                    }

                    //\u8F93\u5165\u6709\u6548\uFF0C\u5219\u5C06\u9AD8\u4F4D\u8865\u6210'0'
                    k = i-j;
                    for(j=i-1; (j-k)>=0 && j!=0; j--)
                        set_val_str[j]=set_val_str[j-k];
                    set_val_str[i] = '\0';
                    for(j=0; j<k; j++)
                        set_val_str[j]='0';
                    break;
                case 2:	//keepHex_low_0		//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u5B57\u7B26\u4E32set_val_str\u662F\u5426\u6709\u6548, \u5982\u679C\u6709\u6548\u5219\u5C06\u4F4E\u4F4D\u8865\u6210\u5B57\u7B26'0'\uFF0C\u8865\u8DB3\u4F4D\u6570
                    j = strlen(set_val_str);
                    for(i=0; i<j; i++)
                        set_val_str[i] = toupper(set_val_str[i]);

                    for(i=0; i<j; i++)
                    {
                        if( isdigit(set_val_str[i]) )		//is digit
                            continue;
                        if( (set_val_str[i] >= 'A') && (set_val_str[i] <= 'F'))	//is A to F
                            continue;
                        break;
                    }
                    if(i<j) //\u542B\u4E0D\u662F\u6570\u5B57\u800C\u4E14\u4E5F\u4E0D\u662FA\u81F3F\u7684\u5B57\u7B26\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                    {
                        debug_ascii_out_proc("The value must be Hex.\r\nFAIL\r\n");
                        return 0;
                    }

                    i =( (param_node->byte_end * 8 + 1 + param_node->bit_end) - (param_node->byte_begin * 8  - param_node->bit_begin) )/4;
                    if(j > i)	//\u5B57\u7B26\u4E32\u957F\u5EA6\u5927\u4E8E\u6240\u80FD\u5B58\u50A8\u7684\u4F4D\u6570\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                    {
                        sprintf(out_text, "The length of the value string must be less than %d.\r\nFAIL\r\n", i);
                        debug_ascii_out_proc(out_text);
                        return 0;
                    }

                    //\u8F93\u5165\u6709\u6548\uFF0C\u5219\u5C06\u4F4E\u4F4D\u8865\u6210'0'
                    for(; j<i; j++)
                        set_val_str[j]='0';
                    set_val_str[j] = '\0';
                    break;
                case 3:	//keepHex_low_E		//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u5B57\u7B26\u4E32set_val_str\u662F\u5426\u6709\u6548, \u5982\u679C\u6709\u6548\u5219\u5C06\u4F4E\u4F4D\u8865\u6210\u5B57\u7B26'E'\uFF0C\u8865\u8DB3\u4F4D\u6570
                    j = strlen(set_val_str);
                    for(i=0; i<j; i++)
                        set_val_str[i] = toupper(set_val_str[i]);

                    for(i=0; i<j; i++)
                    {
                        if( isdigit(set_val_str[i]) )		//is digit
                            continue;
                        if( (set_val_str[i] >= 'A') && (set_val_str[i] < 'E'))	//is A to D
                            continue;
                        break;
                    }
                    if(i<j) //\u6709\u4E0D\u662F\u6570\u5B57\u800C\u4E14\u4E5F\u4E0D\u662FA\u81F3D\u7684\u5B57\u7B26\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                    {
                        debug_ascii_out_proc("The value must be Hex and there is not 'E' and 'F' character.\r\nFAIL\r\n");
                        return 0;
                    }

                    i =( (param_node->byte_end * 8 + 1 + param_node->bit_end) - (param_node->byte_begin * 8  - param_node->bit_begin) )/4;
                    if(j > i)	//\u5B57\u7B26\u4E32\u957F\u5EA6\u5927\u4E8E\u6240\u80FD\u5B58\u50A8\u7684\u4F4D\u6570\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                    {
                        sprintf(out_text, "The length of the value string must be less than %d.\r\nFail\r\n", i);
                        debug_ascii_out_proc(out_text);
                        return 0;
                    }

                    //\u8F93\u5165\u6709\u6548\uFF0C\u5219\u5C06\u4F4E\u4F4D\u8865\u6210E
                    for(; j<i; j++)
                        set_val_str[j]='E';
                    set_val_str[j] = '\0';
                    break;
                case 4:	//keepHex_low_F		//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u5B57\u7B26\u4E32set_val_str\u662F\u5426\u6709\u6548, \u5982\u679C\u6709\u6548\u5219\u5C06\u4F4E\u4F4D\u8865\u6210\u5B57\u7B26'F'\uFF0C\u8865\u8DB3\u4F4D\u6570
                    j = strlen(set_val_str);
                    for(i=0; i<j; i++)
                        set_val_str[i] = toupper(set_val_str[i]);

                    for(i=0; i<j; i++)
                    {
                        if( isdigit(set_val_str[i]) )		//is digit
                            continue;
                        if( (set_val_str[i] >= 'A') && (set_val_str[i] < 'F'))	//is A to E
                            continue;
                        break;
                    }
                    if(i<j) //\u6709\u4E0D\u662F\u6570\u5B57\u800C\u4E14\u4E5F\u4E0D\u662FA\u81F3E\u7684\u5B57\u7B26\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                    {
                        debug_ascii_out_proc("The value must be Hex and there is not 'F' character.\r\nFAIL\r\n");
                        return 0;
                    }

                    i =( (param_node->byte_end * 8 + 1 + param_node->bit_end) - (param_node->byte_begin * 8  - param_node->bit_begin) )/4;
                    if(j > i)	//\u5B57\u7B26\u4E32\u957F\u5EA6\u5927\u4E8E\u6240\u80FD\u5B58\u50A8\u7684\u4F4D\u6570\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                    {
                        sprintf(out_text, "The length of the value string must be less than %d.\r\nFAIL\r\n", i);
                        debug_ascii_out_proc(out_text);
                        return 0;
                    }

                    //\u8F93\u5165\u6709\u6548\uFF0C\u5219\u5C06\u4F4E\u4F4D\u8865\u6210'F'
                    for(; j<i; j++)
                        set_val_str[j]='F';
                    set_val_str[j] = '\0';
                    break;
                case 5:	//toAscii_low_0			//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u5B57\u7B26\u4E32set_val_str\u662F\u5426\u6709\u6548, \u5982\u679C\u6709\u6548\u5219\u5C06\u4F4E\u4F4D\u8865\u6210'0'\uFF0C\u8865\u8DB3\u4F4D\u6570
                    j = strlen(set_val_str);

                    i =( (param_node->byte_end * 8 + 1 + param_node->bit_end) - (param_node->byte_begin * 8  - param_node->bit_begin) )/8;
                    if(j > i)	//\u5B57\u7B26\u4E32\u957F\u5EA6\u5927\u4E8E\u6240\u80FD\u5B58\u50A8\u7684\u4F4D\u6570\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                    {
                        sprintf(out_text, "The length of the AscII string must be less than %d.\r\nFAIL\r\n", i);
                        debug_ascii_out_proc(out_text);
                        return 0;
                    }

                    //\u8F93\u5165\u6709\u6548\uFF0C\u5219\u5C06\u4F4E\u4F4D\u8865\u6210'0'
                    for(; j<i; j++)
                        set_val_str[j]='0';
                    set_val_str[j] = '\0';
                    break;
                case 6:	//keepAscii_low_F			//\u5224\u65AD\u8F93\u5165\u7684\u6570\u503C\u5B57\u7B26\u4E32set_val_str\u662F\u5426\u6709\u6548, \u5982\u679C\u6709\u6548\u5219\u5C06\u4F4E\u4F4D\u8865\u6210\u5B57\u7B26F\uFF0C\u8865\u8DB3\u4F4D\u6570
                    j = strlen(set_val_str);

                    i =( (param_node->byte_end * 8 + 1 + param_node->bit_end) - (param_node->byte_begin * 8  - param_node->bit_begin) )/8;
                    if(j > i)	//\u5B57\u7B26\u4E32\u957F\u5EA6\u5927\u4E8E\u6240\u80FD\u5B58\u50A8\u7684\u4F4D\u6570\uFF0C\u5219\u63D0\u793A\u5E76\u9000\u51FA
                    {
                        sprintf(out_text, "The length of the AscII string must be less than %d.\r\nFAIL\r\n", i);
                        debug_ascii_out_proc(out_text);
                        return 0;
                    }

                    //\u8F93\u5165\u6709\u6548\uFF0C\u5219\u5C06\u4F4E\u4F4D\u8865\u6210'F'
                    for(; j<i; j++)
                        set_val_str[j]='F';
                    set_val_str[j] = '\0';
                    break;
                }
            }
        }
        else
        {
            debug_ascii_out_proc("Fail    : There is no such child.\r\n");
            return 0;
        }
    }
    
    if(debug_cfg==2 && menu_curr_node->sys_group==9)
       for(i=0;i<ip_num;i++)
            IP_Selected[i] = 1;
       
    
    send_msgbuf.pdu_type = 0;
    send_msgbuf.request_id = 0;
    send_msgbuf.var_num = 1;
    for(i=0; i<32; i++)
        send_msgbuf.var[0].oid[i] = mib_curr_node->oid[i];
    send_msgbuf.var[0].oid[mib_curr_node->oid_len] = (DWORD)mib_curr_node->curr_instance;
    send_msgbuf.var[0].oidlen = mib_curr_node->oid_len + 1;

    for(i=0;i<16;i++)
    {
        if(IP_Selected[i] != 1)
            continue;
        send_addr.remote_ip = Peer_IP[i];
        break;
    }
    if(i>=16)
    {
        debug_ascii_out_proc("There is not host ip available.\r\n");
        return 0;
    }


    send_addr.local_ip = Local_IP;
    send_addr.remote_port = SNMP_PORT;
    send_addr.local_port = DEBUG_SNMP_PORT;
    send_addr.broadcast = 0;

    recv_msgbuf.pdu_type = 2;
    recv_msgbuf.request_id = 0;
    recv_msgbuf.var_num = 1;
    for(i=0; i<32; i++)
        recv_msgbuf.var[0].oid[i] = mib_curr_node->oid[i];
    recv_msgbuf.var[mib_curr_node->oid_len].oid[i] = (DWORD)mib_curr_node->curr_instance;
    recv_msgbuf.var[0].oidlen = mib_curr_node->oid_len + 1;

    for(i=0; i<256; i++)
        recv_msgbuf.var[0].msg[i] = 0;

    recv_addr.remote_ip = send_addr.remote_ip;
    recv_addr.local_ip = Local_IP;
    recv_addr.remote_port = SNMP_PORT;
    recv_addr.local_port = DEBUG_SNMP_PORT;
    recv_addr.broadcast = 0;

    ip_index = 0;
    set_state = 1;
    debug_cfg_end = 0;

    return 1;
}	// end of debug_mmi_set



/*
*
*       Fun:    debug_snmp_get_rt
*
*       Desc:   get the mib_node's value from the host by snmp, then analyze and show the value of its field's value 
*
*       Ret:    RVOID
*
*       Notes:  Wang Jian add
*
*       File   debug.c
*
*/
void 	debug_snmp_get_rt()
{
    int 	  request_id=0,	index,i;
    char   	out_text[256]="";
    BYTE	  value_valid;
    char    type_str[31];
    char    out_text1[128];
    char    temp_str[32];
    char    out_ins_text[256];  
    BYTE    ins_byte,field_id;
    BYTE    ins_bit;
    BYTE    byte,bit;
    WORD    curr_select_id;
    mib_child_node *child_node_ptr;
    param_field  *tmp_param_field;

   

  
    switch(get_state)
    {
    case 0:	//idle
        break;
    case 1: //send local get request to local host
        t5s_flag=0;

        if (mib_curr_node->max_access == 0)
        {
            //debug_color_out(BRIGHT,RED,BLACK,"Error   : This leaf node's MAX-ACCESS is 'INVALID'. Can not get its value.\r\n");
            get_state=0;
            debug_cfg_end=1;
            return ;
        }
        if (mib_curr_node->max_access == 1)
        {
            //debug_color_out(BRIGHT,RED,BLACK,"Error   : This leaf node's MAX-ACCESS is 'NOT-ACCESSIBLE'. Can not get its value.\r\n");
            get_state=0;
            debug_cfg_end=1;
            return ;
        }
        if (mib_curr_node->max_access == 6 && snmp_agent_flag==0)
        {
            //debug_color_out(BRIGHT,RED,BLACK,"Error   : This leaf node's MAX-ACCESS is 'WRITE-ONLY'. Can not get its value.\r\n");
            get_state=0;
            debug_cfg_end=1;
            return ;
        }
        for(ip_index=0;ip_index<ip_num;ip_index++)
        {
            if(IP_Selected[ip_index] != 1)
                continue;
            send_addr.remote_ip = Peer_IP[ip_index];
            break;
        }
        if(ip_index>=ip_num)
        {
            if(snmp_agent_flag==0)
                debug_color_out(BRIGHT,RED,BLACK,"Fail   : Can not get the value because there is not host ip available.\r\n");
            else
            {
                debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                debug_snmp_agent_buf[debug_agent_index].result_state=4;
                mib_curr_node=agent_tmp_node;
            }
            ip_index = 0;
            snmp_agent_flag=0;
            debug_cfg_end = 1;
            get_state = 0;
            snmp_agent_flag=0;
            debug_get_all_value = 0;
            break;
        }

        request_id = snmp_send(&send_msgbuf, &send_addr);
        if(request_id == 0)
        {
            if(snmp_agent_flag==0)
            {
                sprintf(out_text, "Error   : Fail to send snmp get request to the host: %s     %s\r\n", Remote_Host_Name[ip_index], Remote_Host_IP[ip_index]);
                debug_ascii_out_proc(out_text);
            }
            else
            {
                debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                debug_snmp_agent_buf[debug_agent_index].result_state=9;
                mib_curr_node=agent_tmp_node;
            }
            ip_index = 0;
            snmp_agent_flag=0;
            debug_cfg_end = 1;
            get_state = 0;
            snmp_agent_flag=0;
            debug_get_all_value = 0;
        }
        else
        {
            recv_msgbuf.pdu_type = 2;
            recv_msgbuf.request_id = request_id;
            recv_addr.remote_ip = send_addr.remote_ip;
            get_state = 2;
        }
        break;
    case 2:	//receive local get response
        request_id = snmp_receive(&recv_msgbuf, &recv_addr);
        if(request_id == 0)	// not received
        {
            if(t5s_flag > 250)
            {
                if(snmp_agent_flag==0)
                {
                    //if(list_once==1)
                    {
                    	  if(list_once==0)
                    	  {
                    	  	mib_node_param *node_param_ptr;
                    	  	debug_ascii_out_proc("Child   : ");
							sprintf(out_text, "[%d]\r\n",mib_curr_node->total_child_nodes);
							      debug_color_out(BRIGHT,YELLOW,BLACK,out_text);
                    	  	for (i = 0; i < mib_curr_node->total_child_nodes; i++)
							{
								                child_node_ptr = mib_curr_node->child_nodes[i];
								                node_param_ptr=(mib_node_param *)child_node_ptr->child_ptr;
								                sprintf(out_text, "           |---(%u) %s\r\n",child_node_ptr->oid+1, node_param_ptr->name);
								                debug_color_out(BRIGHT,YELLOW,BLACK,out_text);
							}
                    	  } 
                        sprintf(out_text, "Fail    : Time out! Check the connection to Host:%s(%s)\r\n",Remote_Host_Name[ip_index], Remote_Host_IP[ip_index]);
                        debug_ascii_out_proc(out_text);
                    }

                }
                else
                {
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=1;
                    mib_curr_node=agent_tmp_node;
                }
                ip_index=0;
                list_flag=0;
                snmp_agent_flag=0;
                list_start=list_end=0;
                get_show_state=0;
                debug_cfg_end = 1;
                get_state = 0;
                snmp_agent_flag=0;
                debug_get_all_value = 0;
                break;
            }
            else
                break;
        }
        debug_cfg_end = 0;
        get_state = 3;
        break;
    case 3:	// received
        //for look                    
        if(snmp_agent_flag==0)
        {
            if(recv_msgbuf.error_status != 0)
            {
                //sprintf(out_text, "Fail    : get value from the host: %s[%s]",Remote_Host_Name[ip_index], Remote_Host_IP[ip_index]);
                switch(recv_msgbuf.error_status)
                {
                case 1:
                    strcat(out_text,"Error   : TooBig\r\n");
                    break;
                case 5:
                    strcat(out_text,"Error   : GenErr\r\n");
                    break;
                case 6:
                    strcat(out_text,"Error   : NoAccessl\r\n");
                    break;
                case 7:
                    strcat(out_text,"Error   : WrongType\r\n");
                    break;
                case 8:
                    strcat(out_text,"Error   : WrongLength\r\n");
                    break;
                case 9:
                    strcat(out_text,"Error   : WrongEncoding\r\n");
                    break;
                case 10:
                    strcat(out_text,"Error   : WrongValue\r\n");
                    break;
                case 11:
                    strcat(out_text,"Error   : NoCreation\r\n");
                    break;
                case 12:
                    strcat(out_text,"Error   : inconsistentValue\r\n");
                    break;
                case 13:
                    strcat(out_text,"Error   : resourceUnavailable\r\n");
                    break;
                case 14:
                    strcat(out_text,"Error   : commitFailed\r\n");
                    break;
                case 15:
                    strcat(out_text,"Error   : UndoFailed\r\n");
                    break;
                case 16:
                    strcat(out_text,"Error   : AuthorizationError\r\n");
                    break;
                case 17:
                    strcat(out_text,"Error   : NoWritable\r\n");
                    break;
                case 18:
                    strcat(out_text,"Error   : inconsistentName\r\n");
                    break;
                default:
                    break;
                }
                if(snmp_agent_flag==0)
                    debug_color_out(BRIGHT,RED,BLACK,out_text);
                ip_index=0;
                snmp_agent_flag=0;
                debug_cfg_end = 1;
                get_state = 0;
                snmp_agent_flag=0;
                debug_get_all_value = 0;
                break;
            }
            strcpy(out_ins_text,"");
            if(get_show_state==1)
            {
                show_all_value=1;
                show_type=0;
                show_expend=1;
                show_range=0;
            }
        } 
        
        while(bind_index<bind_num)
        {
        	 
            if(snmp_agent_flag==0)
            {
            	 if(list_once==1)
            	 {
	                strcpy(out_ins_text,""); 
	                strcpy(out_text,"");
	                if(debug_get_all_value!=301)
	                    debug_get_all_value=list_field;
	                              
	                sprintf(out_text1,"%s  ",mib_curr_node->name);
	                TxtColor(BRIGHT,YELLOW,BLACK,out_text1);
	                strcat(out_ins_text,out_text1);
	                if(!(none_instance==1 || show_empty==1))
	                    sprintf(out_text1,"#%-3d  ",mib_curr_node->curr_instance+bind_index);
	                else
	                    strcpy(out_text1,"");
	
	                if(mib_curr_node->total_child_nodes>1 || show_expend==0)
	                    strcat(out_text1,"\r\n");
	                strcat(out_ins_text,out_text1);
	              
                  show_list=1;
               }
                
                if(show_all_value==0 )
                {
                    if(mib_curr_node->total_child_nodes==1)
                    {
                        param_node=mib_curr_node->child_nodes[0]->child_ptr;
                        if(param_node!=NULL)
                        {
                            if(param_node->input_ptr!=NULL)
                            {
                                if(param_node->input_ptr->input_param_type==0) 
                                    index=1;
                                else                              
                                    index=strncmp(mib_curr_node->init_value, (char *)recv_msgbuf.var[bind_index].msg,mib_curr_node->data_length);                               
                            }
                        }
                    }
                    else                    
                       index=strncmp(mib_curr_node->init_value, (char *)recv_msgbuf.var[bind_index].msg,mib_curr_node->data_length); 
                   
                    if(mib_curr_node->enable_field_id!=0xFF) 
                    {
                        param_node = mib_curr_node->child_nodes[mib_curr_node->enable_field_id]->child_ptr;
                        if(param_node->byte_begin==param_node->byte_end && param_node->bit_begin ==param_node->bit_end)
                        {
                            ins_byte=param_node->byte_begin;
                            ins_bit=param_node->bit_begin ;
                            if(recv_msgbuf.var[bind_index].msg[ins_byte] & (BYTE)my_pow(2,ins_bit))
                                index=1;
                            else
                                index=0;
                        }
                    }
                    if(index)
                    {
                        byte=(mib_curr_node->curr_instance+bind_index)/8;
                        bit=(mib_curr_node->curr_instance+bind_index)%8;
                        mib_curr_node->enable_flag[byte]=mib_curr_node->enable_flag[byte] | (BYTE)my_pow(2,bit);
                        curr_instance_arr[mib_curr_node->curr_instance+bind_index].ins=mib_curr_node->curr_instance+bind_index;
                        curr_instance_arr[mib_curr_node->curr_instance+bind_index].eable=1;
                    }
                    else
                    {
                        byte=(mib_curr_node->curr_instance+bind_index)/8;
                        bit=(mib_curr_node->curr_instance+bind_index)%8;
                        mib_curr_node->enable_flag[byte]=mib_curr_node->enable_flag[byte] & (~(BYTE)my_pow(2,bit));
                        curr_instance_arr[mib_curr_node->curr_instance+bind_index].ins=mib_curr_node->curr_instance+bind_index;
                        curr_instance_arr[mib_curr_node->curr_instance+bind_index].eable=0;
                    }
                }              
            }            
            if(((bind_num==1 && debug_snmp_agent_buf[debug_agent_index].cmm_state==3 )|| (debug_snmp_agent_buf[bind_index].cmm_state ==3 && bind_num!=1)) && snmp_agent_flag==1)
            {
                show_list=1;
                show_all_value=1; 
                show_expend=1;
                debug_get_all_value=mib_curr_node->total_child_nodes;
            }
            
            
            if(show_list==1)
            {
                if(index!=0 ||  show_all_value==1)
                {
                    if(snmp_agent_flag==0 && field_compute_state!=4) 
                        debug_ascii_out_proc(out_ins_text);   
                    if(show_expend==1)
                    {
                    	  
                        while(debug_get_all_value != 0 )
                        {
                            debug_cfg_end = 0;
                            get_state = 3;
                            if(debug_get_all_value!=301)
                            {
                                param_node = mib_curr_node->child_nodes[mib_curr_node->total_child_nodes - debug_get_all_value]->child_ptr;
                                if(snmp_agent_flag==0)
                                    if(show_field==1)
                                    {
                                        for(i=0;list_field_arr[i]!=0xFF && i<64;i++)
                                        {
                                            if(list_field_arr[i]==mib_curr_node->total_child_nodes - debug_get_all_value+1)
                                            {
                                                break;
                                            }
                                        }
                                        if( list_field_arr[i]==0xFF || i==64)
                                        {
                                            debug_get_all_value--;
                                            continue;
                                        }
                                        else if(list_field_arr[i]==mib_curr_node->total_child_nodes - debug_get_all_value+1)
                                        {
                                            ;
                                        }
                                    }
                            } 
                            if(param_node == NULL)
                            {// not error but mib_curr_node has no child
                                int 	index2,	index3;
                                DWORD	sum;
                                int 	sub_sum,  find_first,	byte_begin, byte_end, bit_begin, bit_end;
                                find_first = 0;
                                strcpy(out_text, "");
                                sub_sum=0;
                                index3 = 0;
                                sum = 0;
                                byte_begin = 0;
                                byte_end = recv_msgbuf.var[bind_index].msglen - 1;
                                bit_begin = 0;
                                bit_end = 7;
                                strcpy(out_text, "0x");
                                for(index=byte_begin; index <= byte_end; index++)
                                {
                                    if(index == byte_begin) //index--byte index
                                        index2 = bit_begin;
                                    else
                                        index2 = 0;
                                    for(; index2<8; index2++)//index2--bit index
                                    {
                                        if(index3 == 0)
                                            sub_sum = 0;
                                        if((recv_msgbuf.var[bind_index].msg[index] & (BYTE)my_pow(2, index2)) != 0)//get index bit
                                            sub_sum += my_pow(2, index3);
                                        index3++;
                                        if( (index == byte_end) &&	(index2 == bit_end))
                                        {
                                            switchchar(out_text,sub_sum & 0xF0);
                                            switchchar(out_text,sub_sum & 0x0F);
                                        }
                                        if(index3==8)
                                        {
                                            switchchar(out_text,sub_sum & 0xF0);
                                            switchchar(out_text,sub_sum & 0x0F);
                                            index3 = 0;
                                        }
                                    }
                                }
                                if(strlen(out_text) == 2)
                                    value_valid=0;
                                strcpy(type_str,"Hex");
                                ip_index = 0;
                                debug_cfg_end = 1;
                                get_state = 0;
                                debug_get_all_value = 0;
                                break;
                            }

                            else if((recv_msgbuf.var[bind_index].vartype == 2) || (recv_msgbuf.var[bind_index].vartype == 4))	//int or string
                            {//	not error but mib_curr_node has children

                                char	temp_str[16]="";
                                int 	index2,	index3;
                                DWORD	sum;
                                int 	sub_sum,  find_first,	index4;
                                find_first = 0;
                                strcpy(out_text, "");
                                
                                if(param_node->input_ptr != NULL)
                                {
                                    switch(param_node->input_ptr->input_param_type)
                                    {
                                    case 0:	//toDec_high_0
                                        index3 = sum = 0;
                                        for(index=param_node->byte_end; index >=param_node->byte_begin; index--)
                                        {
                                            if(index == param_node->byte_begin)
                                                index2 = param_node->bit_begin;
                                            else
                                                index2 = 0;
                                            for(; index2<8; index2++)
                                            {
                                                if((recv_msgbuf.var[bind_index].msg[index] & (BYTE)my_pow(2, index2)) != 0)
                                                    sum += my_pow(2, index3);
                                                index3++;
                                                if( (index == param_node->byte_begin) &&	(((param_node->byte_begin==param_node->byte_end)&& index2 == param_node->bit_end) || ((param_node->byte_begin!=param_node->byte_end)&& index2==7)))
                                                    break; 
                                                
                                            } 
                                            if( (index == param_node->byte_begin) &&	(((param_node->byte_begin==param_node->byte_end)&& index2 == param_node->bit_end) || ((param_node->byte_begin!=param_node->byte_end)&& index2==7)))
                                                break;
                                        }
                                        sprintf(out_text, "%u", sum);
                                        strcpy(type_str,"toDec-hight-0");
                                        break;
                                    case 1:	//keepHex_high_0  
                                       sub_sum=0;
                                        index3 = 0;
                                        sum = 0;
                                        strcpy(out_text, "");
                                        for(index=param_node->byte_begin; index <= param_node->byte_end; index++)
                                        {
                                            if(index == param_node->byte_begin)
                                                index2 = param_node->bit_begin;
                                            else
                                                index2 = 0;
                                            for(; index2<8; index2++)
                                            {
                                                if(index3 == 0)
                                                    sub_sum = 0;
                                                if((recv_msgbuf.var[bind_index].msg[index] & (BYTE)my_pow(2, index2)) != 0)
                                                    sub_sum += my_pow(2, index3);
                                                index3++;

                                                if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                                {
                                                    switchchar(out_text,sub_sum & 0xF0);
                                                    switchchar(out_text,sub_sum & 0x0F);
                                                    if(out_text[strlen(out_text)]=='0')
                                                        ;
                                                    value_valid=0;
                                                    break;
                                                }
                                                if(index3==8)
                                                {
                                                    switchchar(out_text,sub_sum & 0xF0);
                                                    switchchar(out_text,sub_sum & 0x0F);
                                                    index3 = 0;
                                                }
                                            }
                                        }
                                        sprintf(out_text1,"%s ",out_text);
                                        strcpy(out_text,out_text1);
                                        strcpy(type_str,"keepHex-high-0"); 
                                        break;
                                    case 2:	//keepHex_low_0
                                        sub_sum=0;
                                        index4=0;
                                        index3 = 0;
                                        sum = 0;
                                        strcpy(out_text, "");
                                        for(index=param_node->byte_begin; index <= param_node->byte_end ; index++)
                                        {
                                            if(index == param_node->byte_begin)
                                                index2 = param_node->bit_begin;
                                            else
                                                index2 = 0;
                                            for(; index2<8; index2++)
                                            {
                                                if(index3 == 0)
                                                    sub_sum = 0;
                                                if((recv_msgbuf.var[bind_index].msg[index] & (BYTE)my_pow(2, index2)) != 0)
                                                    sub_sum += my_pow(2, index3);
                                                index3++;
                                                if(index3==8)
                                                {
                                                    switchchar(out_text,sub_sum & 0xF0);
                                                    if((sub_sum & 0xF0)==0x00 )
                                                    {
                                                        out_text[strlen(out_text)-1]='\0';
                                                        find_first = 1;
                                                        break;
                                                    }
                                                    switchchar(out_text,sub_sum & 0x0F);
                                                    if((sub_sum & 0x0F)==0x00)
                                                    {
                                                        out_text[strlen(out_text)-1]='\0';
                                                        find_first = 1;
                                                        break;
                                                    }
                                                    index3 = 0;
                                                }
                                                if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                                {
                                                    switchchar(out_text,sub_sum & 0x00);
                                                    if((sub_sum & 0xF0)==0x00 )
                                                    {
                                                        if(strlen(out_text) == 1)
                                                            value_valid=0;
                                                        out_text[strlen(out_text)-1]='\0';
                                                        find_first = 1;
                                                        break;
                                                    }
                                                    switchchar(out_text,sub_sum & 0x0F);
                                                    if((sub_sum & 0x0F)==0x00)
                                                    {
                                                        out_text[strlen(out_text)-1]='\0';
                                                        find_first = 1;
                                                        break;
                                                    }
                                                    index3=0;
                                                }
                                            }
                                            if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                                break;
                                            if(find_first == 1)
                                                break;
                                        }
                                        strcpy(type_str,"keepHex-low-0");
                                        break;
                                    case 3:	//keepHex_low_E
                                        sub_sum=0;
                                        index4=0;
                                        index3 = 0;
                                        sum = 0;
                                        strcpy(out_text, "");
                                        for(index=param_node->byte_begin; index <= param_node->byte_end ; index++)
                                        {
                                            if(index == param_node->byte_begin)
                                                index2 = param_node->bit_begin;
                                            else
                                                index2 = 0;
                                            for(; index2<8; index2++)
                                            {
                                                if(index3 == 0)
                                                    sub_sum = 0;
                                                if((recv_msgbuf.var[bind_index].msg[index] & (BYTE)my_pow(2, index2)) != 0)
                                                    sub_sum += my_pow(2, index3);
                                                index3++;
                                                if(index3==8)
                                                {
                                                    switchchar(out_text,sub_sum & 0xF0);
                                                    if((sub_sum & 0xF0)==0xE0 )
                                                    {
                                                        if(strlen(out_text) == 1)
                                                            value_valid=0;
                                                        out_text[strlen(out_text)-1]='\0';

                                                        find_first = 1;
                                                        break;
                                                    }
                                                    switchchar(out_text,sub_sum & 0x0F);
                                                    if((sub_sum & 0x0F)==0x0E)
                                                    {
                                                        out_text[strlen(out_text)-1]='\0';
                                                        find_first = 1;
                                                        break;
                                                    }
                                                    index3 = 0;
                                                }
                                                if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end) && param_node->bit_end!=7)
                                                {
                                                    switchchar(out_text,sub_sum & 0xF0);
                                                    if((sub_sum & 0xF0)==0xE0 )
                                                    {
                                                        out_text[strlen(out_text)]='\0';
                                                        find_first = 1;
                                                        break;
                                                    }
                                                    switchchar(out_text,sub_sum & 0x0F);
                                                    if((sub_sum & 0x0F)==0x0E)
                                                    {
                                                        out_text[strlen(out_text)-1]='\0';
                                                        find_first = 1;
                                                        break;
                                                    }
                                                    index3=0;
                                                }
                                            }
                                            if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                                break;
                                            if(find_first == 1)
                                                break;
                                        }
                                        strcpy(type_str,"keepHex-low-E");
                                        //printf("The keephex_low_e:%s",out_text);
                                        break;
                                    case 4:	//keepHex_low_F
                                        sub_sum=0;
                                        index4=0;
                                        index3 = 0;
                                        sum = 0;
                                        strcpy(out_text, "");
                                        for(index=param_node->byte_begin; index <= param_node->byte_end; index++)
                                        {
                                            if(index == param_node->byte_begin)
                                                index2 = param_node->bit_begin;
                                            else
                                                index2 = 0;
                                            for(; index2<8; index2++)
                                            {
                                                if(index3 == 0)
                                                    sub_sum = 0;
                                                if((recv_msgbuf.var[bind_index].msg[index] & (BYTE)my_pow(2, index2)) != 0)
                                                    sub_sum += my_pow(2, index3);
                                                index3++;
                                                if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                                {
                                                    switchchar(out_text,sub_sum & 0xF0);
                                                    if((sub_sum & 0xF0)==0xF0 )
                                                    {
                                                        out_text[strlen(out_text)-1]='\0';
                                                        find_first = 1;
                                                        break;
                                                    }
                                                    switchchar(out_text,sub_sum & 0x0F);
                                                    if((sub_sum & 0x0F)==0x0F)
                                                    {
                                                        out_text[strlen(out_text)-1]='\0';
                                                        find_first = 1;
                                                        break;
                                                    }
                                                    index3=0;
                                                }
                                                if(index3==8)
                                                {
                                                    switchchar(out_text,sub_sum & 0xF0);
                                                    if((sub_sum & 0xF0)==0xF0 )
                                                    {
                                                        if(strlen(out_text) == 1)
                                                            value_valid=0;
                                                        out_text[strlen(out_text)-1]='\0';
                                                        find_first = 1;
                                                        break;
                                                    }
                                                    switchchar(out_text,sub_sum & 0x0F);
                                                    if((sub_sum & 0x0F)==0x0F)
                                                    {
                                                        out_text[strlen(out_text)-1]='\0';

                                                        find_first = 1;
                                                        break;
                                                    }
                                                    index3 = 0;
                                                }
                                            }
                                            if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                                break;
                                            if(find_first == 1)
                                                break;
                                        }
                                        strcpy(type_str,"keepHex-low-F");
                                        break;
                                    case 5:	//toAscii_low_0
                                        sub_sum=0;
                                        index3 = 0;
                                        sum = 0;
                                        strcpy(out_text, "");
                                        strcpy(temp_str, "");
                                        for(index=param_node->byte_begin; index <= param_node->byte_end; index++)
                                        {
                                            if(index == param_node->byte_begin)
                                                index2 = param_node->bit_begin;
                                            else
                                                index2 = 0;
                                            for(; index2<8; index2++)
                                            {
                                                if(index3 == 0)
                                                    sub_sum = 0;
                                                if((recv_msgbuf.var[bind_index].msg[index] & (BYTE)my_pow(2, index2)) != 0)
                                                    sub_sum += my_pow(2, index3);
                                                index3++;
                                                if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                                {
                                                    if(sub_sum == '0')  //\u4EE5 '0' \u7ED3\u5C3E
                                                    {
                                                        if(strlen(out_text) == 2)
                                                            value_valid=0;
                                                        find_first = 1;
                                                        break;
                                                    }
                                                    else
                                                    {
                                                        strcpy(temp_str, "");
                                                        sprintf(temp_str, "%c", sub_sum);
                                                        strcat(out_text, temp_str);
                                                    }
                                                    break;
                                                }
                                                if(index3==8)
                                                {
                                                    if(sub_sum == '0')  //\u4EE5 0 \u7ED3\u5C3E
                                                    {
                                                        if(strlen(out_text) == 0)
                                                            value_valid=0;
                                                        find_first = 1;
                                                        break;
                                                    }
                                                    else
                                                    {
                                                        strcpy(temp_str, "");
                                                        sprintf(temp_str, "%c", sub_sum);
                                                        strcat(out_text, temp_str);
                                                    }
                                                    index3 = 0;
                                                }
                                            }
                                            if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                                break;
                                            if(find_first == 1)
                                                break;
                                        }
                                        strcpy(type_str,"toAscii-low-0");
                                        break;
                                    case 6:	//keepAscii_low_F
                                        sub_sum=0;
                                        index3 = 0;
                                        sum = 0;
                                        strcpy(out_text, "");
                                        strcpy(temp_str, "");

                                        for(index=param_node->byte_begin; index <= param_node->byte_end; index++)
                                        {
                                            if(index == param_node->byte_begin)
                                                index2 = param_node->bit_begin;
                                            else
                                                index2 = 0;
                                            for(; index2<8; index2++)
                                            {
                                                if(index3 == 0)
                                                    sub_sum = 0;
                                                if((recv_msgbuf.var[bind_index].msg[index] & (BYTE)my_pow(2, index2)) != 0)
                                                    sub_sum += my_pow(2, index3);
                                                index3++;
                                                if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                                {
                                                    if(sub_sum == 'F')  //\u4EE5 'F' \u7ED3\u5C3E
                                                    {
                                                        if(strlen(out_text) == 2)
                                                            value_valid=0;
                                                        find_first = 1;
                                                        break;
                                                    }
                                                    else
                                                    {
                                                        strcpy(temp_str, "");
                                                        sprintf(temp_str, "%c", sub_sum);
                                                        strcat(out_text, temp_str);
                                                    }
                                                    break;
                                                }
                                                if(index3==8)
                                                {
                                                    if(sub_sum == 'F')  //\u4EE5 'F' \u7ED3\u5C3E
                                                    {
                                                        if(strlen(out_text) == 0)
                                                            value_valid=0;
                                                        find_first = 1;
                                                        break;
                                                    }
                                                    else
                                                    {
                                                        strcpy(temp_str, "");
                                                        sprintf(temp_str, "%c", sub_sum);
                                                        strcat(out_text, temp_str);
                                                    }
                                                    index3 = 0;
                                                }
                                            }
                                            if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                                break;
                                            if(find_first == 1)
                                                break;
                                        }
                                        strcpy(type_str,"toAscii-low-F");
                                        break;
                                    default:
                                        break;
                                    }
                                    param_node->input_ptr->input[0] = sum;
                                }
                                else if(param_node->select_total != 0)
                                {
                                    index3 = sum = 0;
                                    for(index=param_node->byte_begin; index <= param_node->byte_end; index++)
                                    {
                                        if(index == param_node->byte_begin)
                                            index2 = param_node->bit_begin;
                                        else
                                            index2 = 0;
                                        for(; index2<8; index2++)
                                        {
                                            if((recv_msgbuf.var[bind_index].msg[index] & (BYTE)my_pow(2, index2)) != 0)
                                                sum += my_pow(2, index3);
                                            if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                                break;
                                            index3++;
                                        }
                                        if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                            break;
                                    }
                                    strcpy(out_text, "");
                                    for(index=0; index<param_node->select_total; index++)
                                    {
                                        if(param_node->select_ptr[index]->number == sum)
                                        {
                                            sprintf(out_text, "%u (%s)", sum, param_node->select_ptr[index]->description);
                                            curr_select_id=index;
                                        }
                                    }
                                    if(strlen(out_text) == 0)
                                        sprintf(out_text, "%u (UNREGESTER VALUE)", sum);
                                }
                                else
                                    sprintf(out_text, "%u  ", sum);                                                         
                            } 
                            
                            
                            
                            del_space(out_text,1);   
                            if(strncasecmp(param_node->name, "reserved", 8) == 0 )
                            {                               
                                if(snmp_agent_flag==1)
                                {
                                    field_id=mib_curr_node->total_child_nodes-debug_get_all_value;
                                    tmp_param_field=&snmp_agent_packet.param_field_arr[field_id];
                                    strcpy(tmp_param_field->name,param_node->name);
                                }
                            }
                            else
                            {                            	 
                            	   
                                if(snmp_agent_flag==0 && field_compute_state!=4)
                                {
                                    if(mib_curr_node->total_child_nodes==1)
                                        sprintf(out_text1, " |---(%d) %s",param_node->id+1, param_node->name);
                                    else
                                    {
                                        if(debug_get_all_value==301)
                                            sprintf(out_text1, "          |---(%d) %s",param_node->id+1, param_node->name);
                                        else
                                            sprintf(out_text1, "          |---(%d) %s",1+mib_curr_node->total_child_nodes - debug_get_all_value, param_node->name);
                                    }

                                    debug_color_out(BRIGHT,YELLOW,BLACK,out_text1); 

                                    if(!(none_instance==1 && show_empty==1))
                                    {
                                        if(!strlen(out_text))
                                        {
                                            debug_ascii_out_proc(" = ");
                                            debug_color_out(BRIGHT,BLUE,BLACK,"NULL ");
                                        }
                                        else
                                        {
                                            debug_ascii_out_proc(" = ");
                                            debug_color_out(BRIGHT,BLUE,BLACK,out_text);
                                            debug_ascii_out_proc(" ");
                                        }
                                    }
                                    
                                    if(param_node->input_ptr!=NULL)
                                    {
                                        if(show_type==1 )
                                        {
                                            //debug_ascii_out_proc("Type    : ");
                                            debug_color_out(BRIGHT,YELLOW,BLACK,type_str);
                                        }
                                    }
                                    if(show_range==1)
                                    {
                                        //debug_ascii_out_proc("Range   : ");
                                        sprintf(out_text, " [%d.%d - %d.%d]\r\n", param_node->byte_begin, param_node->bit_begin, param_node->byte_end, param_node->bit_end);
                                        debug_color_out(BRIGHT,YELLOW,BLACK,out_text); //for look
                                    }
                                    else
                                    {
                                        if( strncasecmp(param_node->name, "reserved", 8) != 0 )
                                            debug_ascii_out_proc("\r\n");
                                    }
                                    
                                }
                                else if(snmp_agent_flag==1)     
                                {

                                    field_id=mib_curr_node->total_child_nodes-debug_get_all_value;
                                    tmp_param_field=&snmp_agent_packet.param_field_arr[field_id];
                                    if(param_node->select_total!=0)
                                    {

                                        tmp_param_field->curr_select_id=curr_select_id;
                                        for(index=0; index<param_node->select_total; index++)
                                        {
                                            tmp_param_field->value_arr[index]=param_node->select_ptr[index]->number;
                                            strcpy(tmp_param_field->value_arr_remark[index],param_node->select_ptr[index]->description);
                                        }
                                        tmp_param_field->total_select=param_node->select_total;
                                        tmp_param_field->curr_input_value[0]='\0';
                                        tmp_param_field->input_type=0xFF;
                                    }
                                    else if(param_node->input_ptr!=NULL)
                                    {
                                        tmp_param_field->curr_select_id=0xFF;
                                        for(index=0; index<MAX_SELECT_OPTION_NUM; index++)
                                        {
                                            tmp_param_field->value_arr[index]=0xFF;
                                            strcpy(tmp_param_field->value_arr_remark[index],"");
                                        }
                                        tmp_param_field->total_select=0;
                                        strcpy(tmp_param_field->curr_input_value,out_text);
                                        tmp_param_field->input_type=param_node->input_ptr->input_param_type;
                                    }
                                    strcpy(tmp_param_field->name,param_node->name);
                                }
                                else if(field_compute_state==4 )
                                {
                                	if(param_node->select_total!=0)
                                	{
                                		sprintf(temp_str,"%d",param_node->select_ptr[curr_select_id]->number); 
                                		strcpy(compute_field_list[compute_field_list_index].field_val[mib_curr_node->curr_instance+bind_index-1],temp_str);
                                	}
                                	else if(param_node->input_ptr!=NULL)
                                	{
                                		strcpy(compute_field_list[compute_field_list_index].field_val[mib_curr_node->curr_instance+bind_index-1],out_text);
                                  }
                                }                                
                                
                            }
                           
                            if(debug_get_all_value==301)
                                debug_get_all_value=1;
                            debug_get_all_value--;

                          
                        }      
                        
                    }
                }
            }             
            if(snmp_agent_flag==1)
            {

              if(bind_num==1)
               {
                  memcpy(&debug_snmp_agent_buf[debug_agent_index],&snmp_agent_packet,sizeof(debug_snmp_agent));
                  debug_snmp_agent_buf[debug_agent_index].cmm_state=5;
                  debug_snmp_agent_buf[debug_agent_index].result_state=0;
                }
              else 
               {
                 memcpy(&debug_snmp_agent_buf[bind_index],&snmp_agent_packet,sizeof(debug_snmp_agent));
                 debug_snmp_agent_buf[bind_index].cmm_state=5;
                 debug_snmp_agent_buf[bind_index].result_state=0;
                 debug_snmp_agent_buf[bind_index].oid[debug_snmp_agent_buf[bind_index].oid_len-1]=mib_curr_node->curr_instance+bind_index;
               }

            }
            bind_index++;
			//printf("send ins %d ",mib_curr_node->curr_instance+bind_index);
            if(bind_index<bind_num && show_list==1 && snmp_agent_flag==0 && mib_curr_node->total_child_nodes>4)
               return; 
        } 
        if(snmp_agent_flag==0 && list_once==1) 
        {
            if(recv_msgbuf.var[bind_index].vartype == 5)	//NULL
            {
                debug_ascii_out_proc("Error   : NULL\r\n");
            }
            else if(recv_msgbuf.var[bind_index].vartype == 6)	//Object identifier
            {
                debug_ascii_out_proc("Error   : Object identifier wrong\r\n");
            }
            else if(recv_msgbuf.var[bind_index].vartype == 0x80)	//noSuchObject
            {
                debug_ascii_out_proc("Error   : noSuchObject\r\n");
            }
            else if(recv_msgbuf.var[bind_index].vartype == 0x81)	//NoSuchInstance
            {
                debug_ascii_out_proc("Error   : NoSuchInstance\r\n");
            }
            else if(recv_msgbuf.var[bind_index].vartype == 0x82)	//endOfMibView
            {
                debug_ascii_out_proc("Error   : endOfMibView\r\n");
            }
        }
        //sprintf(out_text,"%d-%d   ",debug_snmp_agent_buf[debug_agent_index].cmm_state,debug_snmp_agent_buf[debug_agent_index].result_state);
        ip_index = 0;
        if(list_flag==0)         
           debug_cfg_end=1;
       if(((list_start>=list_end && list_flag) || !list_flag) && !snmp_agent_flag)
       {
           get_state=0;
           bind_num=0;
           bind_index=0;
       }
       if(snmp_agent_flag>0)
          get_state=0;   
	     if(snmp_agent_flag>0)
		       bind_index=0;	   
        get_show_state=0; 
        //if(snmp_agent_flag==0)
        //   bind_num=0;
        //snmp_agent_flag=0;             
        expend_view=0;
        debug_get_all_value = 0;
        t5s_flag=0;
        break;
    default:
        debug_ascii_out_proc("\r\nError   : no value for this node.\r\nFail\r\n");
        while(debug_get_all_value != 0)
        {
            param_node = mib_curr_node->child_nodes[mib_curr_node->total_child_nodes - debug_get_all_value]->child_ptr;
            if( strncmp(param_node->name, "Reserved", 8) == 0 )
            {
                sprintf(out_text, "%d    Reserved.\r\n",1+mib_curr_node->total_child_nodes - debug_get_all_value);
                debug_ascii_out_proc(out_text);
                debug_get_all_value--;
            }
            else
                break;
        }
        if(debug_get_all_value != 0)
        {
            param_node = mib_curr_node->child_nodes[mib_curr_node->total_child_nodes - debug_get_all_value]->child_ptr;
            sprintf(out_text, "%d    %s = ", 1+mib_curr_node->total_child_nodes - debug_get_all_value, param_node->name);
            debug_get_all_value--;
            debug_ascii_out_proc(out_text);
            debug_cfg_end = 0;
            get_state = 3;
        }
        else
        {
            ip_index =0;
            debug_cfg_end = 1;
            get_state = 0;
        }
        break;
    }
    
}//end of debug_snmp_get_rt 



/*
*
*       Fun:    debug_snmp_set_rt
*
*       Desc:   get the mib_node's value from the host by snmp, then analyze and replace the value of its field's value, then set back the mib_node's value to the host by snmp
*
*       Ret:    RVOID
*
*       Notes:  Wang Jian add
*
*       File:   debug.c
*
*/
void 	debug_snmp_set_rt()
{
    int request_id=0,	index;
    char  out_text[256]="";
    char  out_text2[256]="";
    int   ins_byte,ins_bit;
    mib_node_param *temp_param;
    int   i;

    switch(set_state)
    {
    case 0:	//idle
        break;
    case 1: //send the not null and selected ip in the Peer_IP[16] set request to local host
        t5s_flag=0;
        if(ip_index >= ip_num)
        {
            debug_cfg_end = 1;
            set_state = 0;
            ip_index = 0;
            set_ok_once=0;          
            save_flag=0;
            break;
        }
        if(Peer_IP[ip_index] == 0)
        {
            debug_cfg_end = 1;
            set_state = 0;
            if(snmp_agent_flag==0)
            {
                if(ip_index == 0 )
                    debug_color_out(BRIGHT,RED,BLACK,"Error   : There is not host ip available.\r\n");
                    
            }
            else
            {
                debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                debug_snmp_agent_buf[debug_agent_index].result_state=4;
                mib_curr_node=agent_tmp_node;
                return ;
            }
            ip_index = 0;
            break;
        }

        if(IP_Selected[ip_index] == 1)
            send_addr.remote_ip = Peer_IP[ip_index];
        else
        {
            for(; ip_index<ip_num; ip_index++)
            {
                if(IP_Selected[ip_index] == 1)
                {
                    send_addr.remote_ip = Peer_IP[ip_index];
                    break;
                }
            }
            break;
        }

        request_id = snmp_send(&send_msgbuf, &send_addr);

        if(request_id == 0)
        {
            if(snmp_agent_flag==0)
            {
                //sprintf(out_text, "Error   : Fail to send snmp get request to the host: %s     %s\r\n", Remote_Host_Name[ip_index], Remote_Host_IP[ip_index]);
            }
            else
            {
                debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                debug_snmp_agent_buf[debug_agent_index].result_state=9;
                mib_curr_node=agent_tmp_node;
                snmp_agent_flag=0;
            }
            ip_index++;
            debug_cfg_end = 0;
            set_ok_once=0;
            set_state = 1;
        }
        else
        {
            recv_msgbuf.pdu_type = 2;
            recv_msgbuf.request_id = request_id;
            recv_addr.remote_ip = send_addr.remote_ip;
            set_state = 2;
            debug_cfg_end = 0;
        }
        break;
    case 2:	//receive get response
        request_id = snmp_receive(&recv_msgbuf, &recv_addr);
        if(request_id == 0) // did not receive
        {
            if(t5s_flag > 250)
            {
                if(snmp_agent_flag==0)
                {
                    if(save_flag>=3)
                    {

                        //debug_ascii_out_proc("Status  : ");
                        //sprintf(out_text,"Fail to save the module of %s,time out,please check the host\r\n",subsys_module[curr_module].name);
                        //debug_color_out(BRIGHT,CYAN,BLACK,out_text);
                        mib_curr_node=temp_mib_node;
                        sprintf(out_text, "Host    : %s     %s  (State: Time out  !)\r\n", Remote_Host_Name[ip_index], Remote_Host_IP[ip_index]);
                        if(save_flag==3)
                            debug_mmi_contents("");
                        if(debug_cfg==1)
                            mib_curr_node=temp_mib_node;
                        else if(debug_cfg==2)
                        {
                            menu_curr_node=tmp_menu_node;
                            mib_curr_node=temp_mib_node;
                        }
                        save_flag=0;
                    }
                    else
                        sprintf(out_text, "Host    : %s     %s  (State: Time out  !)\r\n", Remote_Host_Name[ip_index], Remote_Host_IP[ip_index]);
                    debug_color_out(BRIGHT,RED,BLACK,out_text);
                }
                else
                {
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=1;
                    mib_curr_node=agent_tmp_node;
                    snmp_agent_flag=0;
                }
                ip_index++;
                debug_cfg_end = 0;
                set_ok_once=0;
                set_state = 1;
                break;
            }
            else
                break;
        }
        else	//received
        {
            char	 byte_val1[16];
            if(snmp_agent_flag==0)
            {
                if(save_flag<3 || save_flag==4)
                {
                    // debug_ascii_out_proc("OldValue: ");
                    for(index=0; index<recv_msgbuf.var[0].msglen; index++)
                    {
                        if((recv_msgbuf.var[0].msg[index] & 0xF0)==0)
                        {
                            strcat(out_text2,"0");
                            sprintf(byte_val1,"%X ",recv_msgbuf.var[0].msg[index]);
                            strcat(out_text2,byte_val1);
                        }
                        /*
                        else if((recv_msgbuf.var[0].msg[index] & 0x0F)==0)
                        {
                            sprintf(byte_val1, "%X", recv_msgbuf.var[0].msg[index]);
                            strcat(byte_val1,"0 ");
                            strcat(out_text2,byte_val1);
                        }
                        */
                        else
                        {
                            sprintf(byte_val1, "%X ", recv_msgbuf.var[0].msg[index]);
                            strcat(out_text2,byte_val1);
                        }
                    }
                    //debug_color_out(BRIGHT,YELLOW,BLACK,out_text2);//for look
                    //debug_ascii_out_proc("\r\n");
                }
                //debug_ascii_out_proc("SetStatus:");
                //debug_color_out(BRIGHT,CYAN,BLACK,"Waiting...");
                //sprintf(out_text1, "range: %d.%d - %d.%d\r\n", param_node->byte_begin, param_node->bit_begin, param_node->byte_end, param_node->bit_end);
                //debug_color_out(BRIGHT,YELLOW,BLACK,out_text1);//for look
            }
            if(recv_msgbuf.error_status != 0)
            {
                if(snmp_agent_flag==0)
                {
                    //sprintf(out_text, "Error   : get value from the host: %s     %s\r\n",Remote_Host_Name[ip_index], Remote_Host_IP[ip_index]);
                    //debug_color_out(BRIGHT,RED,BLACK,out_text);
                    switch(recv_msgbuf.error_status)
                    {
                    case 1:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : TooBig\r\n");
                        break;
                    case 5:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : GenErr\r\n");
                        break;
                    case 6:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : NoAccess\r\n");
                        break;
                    case 7:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : WrongType\r\n");
                        break;
                    case 8:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : WrongLength\r\n");
                        break;
                    case 9:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : WrongEncoding\r\n");
                        break;
                    case 10:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : WrongValue\r\n");
                        break;
                    case 11:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : NoCreation\r\n");
                        break;
                    case 12:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : inconsistentValue\r\n");
                        break;
                    case 13:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : resourceUnavailable\r\n");
                        break;
                    case 14:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : commitFailed\r\n");
                        break;
                    case 15:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : UndoFailed\r\n");
                        break;
                    case 16:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : AuthorizationError\r\n");
                        break;
                    case 17:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : NoWritable\r\n");
                        break;
                    case 18:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : inconsistentName\r\n");
                        break;
                    default:
                        break;
                    }
                    ip_index++;
                    debug_cfg_end = 0;
                    set_state = 1;
                }
                else
                {
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=9;
                    mib_curr_node=agent_tmp_node;
                    snmp_agent_flag=0;
                }
                break;
            }
            else if((recv_msgbuf.var[0].vartype == 2) || (recv_msgbuf.var[0].vartype == 4))	//int or string
            {
                char	out_text[128];
                int 	index2,	index3, index4;
                DWORD	sum;
                int 	sub_sum;
                //int child_index;
                strcpy(out_text, "");
                if(debug_snmp_agent_buf[debug_agent_index].cmm_state==3 && snmp_agent_flag==2)
                {
                    index2=mib_curr_node->data_length*2; 
                    for(i=0;i<index2;i++)
                    {
                        recv_msgbuf.var[0].msg[i]=set_val_str[i];
                    }
                }
                else
                {
                    if(set_str_len)
                    {
                        if(del_flag==0) //set or create
                        {
                            //if(strcmp(curr_instance_value,set_ins_value)==0)
                            //    strcpy(recv_msgbuf.var[0].msg,set_ins_value);
                            //else
                            index2=set_str_len/2;

                            for(i=0;i<index2;i++)
                            {
                                *(recv_msgbuf.var[0].msg+i)=set_ins_value[i];
                            }
                            if(set_str_len%2==1)
                            {
                                recv_msgbuf.var[0].msg[i]= (recv_msgbuf.var[0].msg[i] & 0x0F)|(set_ins_value[i] & 0xF0);
                            }
                            index2=0;

                        }
                        else            //delete
                        {
                            for(i=0;i<set_str_len;i++)
                            {
                                *(recv_msgbuf.var[0].msg+i)=set_ins_value[i];
                            }
                        }
                        if(mib_curr_node->enable_field_id!=0xFF)
                        {
                            param_node = (mib_node_param *)mib_curr_node->child_nodes[mib_curr_node->enable_field_id]->child_ptr;

                            if(param_node->byte_begin==param_node->byte_end && param_node->bit_begin ==param_node->bit_end)
                            {
                                ins_byte=param_node->byte_begin;
                                ins_bit=param_node->bit_begin ;
                                if(del_flag==0)
                                {
                                    recv_msgbuf.var[0].msg[ins_byte] =recv_msgbuf.var[0].msg[ins_byte] | (BYTE)my_pow(2,ins_bit);
                                }
                                else
                                {
                                    recv_msgbuf.var[0].msg[ins_byte] =recv_msgbuf.var[0].msg[ins_byte] & (~(BYTE)my_pow(2,ins_bit));
                                }
                            }
                        }
                        set_all=0;
                    }
                    else
                    {
                        temp_param=param_node;

                        if(mib_curr_node->enable_field_id!=0xFF)
                        {
                            param_node = (mib_node_param *)mib_curr_node->child_nodes[mib_curr_node->enable_field_id]->child_ptr;

                            if(param_node->byte_begin==param_node->byte_end && param_node->bit_begin ==param_node->bit_end)
                            {
                                ins_byte=param_node->byte_begin;
                                ins_bit=param_node->bit_begin ;
                                if(del_flag==0)
                                    recv_msgbuf.var[0].msg[ins_byte] =recv_msgbuf.var[0].msg[ins_byte] | (BYTE)my_pow(2,ins_bit);
                                else
                                    recv_msgbuf.var[0].msg[ins_byte] =recv_msgbuf.var[0].msg[ins_byte] & (~(BYTE)my_pow(2,ins_bit));
                            }
                        }
                        param_node=temp_param;
                        if(param_node->select_total != 0)
                        {
                            index3 = sum = 0;
                            for(index=param_node->byte_begin; index <= param_node->byte_end; index++)
                            {
                                if(index == param_node->byte_begin)
                                    index2 = param_node->bit_begin;
                                else
                                    index2 = 0;
                                for(; index2<8; index2++)
                                {
                                    if((set_val & (BYTE)my_pow(2, index3)) == 0)
                                        recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] & (255 - my_pow(2, index2));
                                    else
                                        recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] | my_pow(2, index2);
                                    if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                        break;
                                    index3++;
                                }
                                if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                    break;
                            }
                        }
                        else if(param_node->input_ptr != NULL)
                        {
                            switch(param_node->input_ptr->input_param_type)
                            {
                            case 0:	//toDec_high_0
                                strcpy(out_text, "");
                                index3 = sum = 0;
                                for(index=param_node->byte_end; index >=param_node->byte_begin; index--)
                                {
                                    if(index == param_node->byte_begin)
                                        index2 = param_node->bit_begin;
                                    else
                                        index2 = 0;
                                    for(; index2<8; index2++)
                                    {
                                        if((set_val & (BYTE)my_pow(2, index3)) == 0)
                                            recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] & (255 - my_pow(2, index2));
                                        else
                                            recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] | my_pow(2, index2);
                                        index3++;
                                        if( (index == param_node->byte_begin) &&	(((param_node->byte_begin==param_node->byte_end)&& index2 == param_node->bit_end) || ((param_node->byte_begin!=param_node->byte_end)&& index2==7)))
                                            break;
                                        
                                    }
                                    if( (index == param_node->byte_begin) &&	(((param_node->byte_begin==param_node->byte_end)&& index2 == param_node->bit_end) || ((param_node->byte_begin!=param_node->byte_end)&& index2==7)))
                                        break;
                                }
                                break;
                            case 1:	//keepHex_high_0
                            case 2:	//keepHex_low_0
                            case 3:	//keepHex_low_E
                            case 4:	//keepHex_low_F
                                strcpy(out_text, "");
                                index3 = index4 = sum = sub_sum = 0;
                                for(index=param_node->byte_begin; index <= param_node->byte_end; index++)
                                {
                                    if(index == param_node->byte_begin)
                                        index2 = param_node->bit_begin;
                                    else
                                        index2 = 0;
                                    if((index3 == 0) && ( (index4+1) <= strlen(set_val_str) )) //\u53D6\u4E24\u4F4D\u5B57\u7B26
                                    {
                                        set_val = 0;
                                        if( isdigit(set_val_str[index4]) )	//\u6570\u5B57\u5B57\u7B26
                                            set_val += (set_val_str[index4++] - '0') * 16;
                                        else								// A \u81F3 E \u4E4B\u95F4
                                            set_val += (set_val_str[index4++] - 'A' + 10) * 16;

                                        if( isdigit(set_val_str[index4]) )
                                            set_val += set_val_str[index4++] - '0';
                                        else
                                            set_val += set_val_str[index4++] - 'A' + 10;
                                    }

                                    for(; index2<8; index2++)
                                    {
                                        if((set_val & (BYTE)my_pow(2, index3)) == 0)
                                            recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] & (255 - my_pow(2, index2));
                                        else
                                            recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] | my_pow(2, index2);
                                        if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                            break;
                                        index3++;
                                        if(index3 == 8)
                                            index3 = 0;
                                    }
                                    if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                        break;
                                }
                                break;
                            case 5:	//toAscii_low_0
                            case 6:	//keepAscii_low_F
                                strcpy(out_text, "");
                                index3 = index4 = sum = sub_sum = 0;
                                for(index=param_node->byte_begin; index <= param_node->byte_end; index++)
                                {
                                    if(index == param_node->byte_begin)
                                        index2 = param_node->bit_begin;
                                    else
                                        index2 = 0;
                                    if(index3 == 0) //\u53D6\u4E00\u4F4D\u5B57\u7B26
                                        set_val = set_val_str[index4++];

                                    for(; index2<8; index2++)
                                    {
                                        if((set_val & (BYTE)my_pow(2, index3)) == 0)
                                            recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] & (255 - my_pow(2, index2));
                                        else
                                            recv_msgbuf.var[0].msg[index] = recv_msgbuf.var[0].msg[index] | my_pow(2, index2);
                                        if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                            break;
                                        index3++;
                                        if(index3 == 8)
                                            index3 = 0;
                                    }
                                    if( (index == param_node->byte_end) &&	(index2 == param_node->bit_end))
                                        break;
                                }
                                break;
                            default:
                                if(snmp_agent_flag==0)
                                {
                                    sprintf(out_text, "Get value (%s)\r\n", recv_msgbuf.var[0].msg);
                                    debug_ascii_out_proc(out_text);
                                    sprintf(out_text,"from the host: %s     %s.\r\nBut can not parse the value type (%d).\r\n", Remote_Host_Name[ip_index], Remote_Host_IP[ip_index], recv_msgbuf.var[0].vartype);
                                    debug_ascii_out_proc(out_text);
                                }
                                ip_index++;
                                debug_cfg_end = 0;
                                set_state = 1;
                                return;
                            }
                        }
                    }
                }
                //	send set request
                t5s_flag=0;
                recv_msgbuf.pdu_type = 3;
                recv_msgbuf.request_id = 0;
                recv_addr.remote_ip = Peer_IP[ip_index];
                request_id = snmp_send(&recv_msgbuf, &recv_addr);
                if(request_id == 0)
                {
                    if(snmp_agent_flag==0)
                    {
                        sprintf(out_text, "Error   : Fail to send snmp set request to the host: %s     %s\r\n",Remote_Host_Name[ip_index],Remote_Host_IP[ip_index]);
                        debug_color_out(BRIGHT,RED,BLACK,out_text);
                    }
                    ip_index++;
                    debug_cfg_end = 0;
                    set_state =1;
                }
                else
                {
                    recv_msgbuf.pdu_type = 2;
                    recv_msgbuf.request_id = request_id;
                    recv_addr.remote_ip = Peer_IP[ip_index];
                    set_state = 3;
                }
                break;
            }
            else
            {
                if(snmp_agent_flag==0)
                {
                    if(recv_msgbuf.var[0].vartype == 5)	//NULL
                    {
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : NULL\r\n");
                    }
                    else if(recv_msgbuf.var[0].vartype == 6)	//Object identifier
                    {
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : Object identifier wrong\r\n");
                    }
                    else if(recv_msgbuf.var[0].vartype == 0x80)	//noSuchObject
                    {
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : noSuchObject\r\n");
                    }
                    else if(recv_msgbuf.var[0].vartype == 0x81)	//NoSuchInstance
                    {
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : NoSuchInstance\r\n");
                    }
                    else if(recv_msgbuf.var[0].vartype == 0x82)	//endOfMibView
                    {
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : endOfMibView\r\n");
                    }
                    if(save_flag>3)
                    {
                    	if(debug_cfg==1)
                            mib_curr_node=temp_mib_node;
                        else if(debug_cfg==2)
                        {
                            menu_curr_node=tmp_menu_node;
                            mib_curr_node=temp_mib_node;
                        }                   	
                    	save_flag=0;  
                    } 
                }
            }

        }
        debug_cfg_end = 0;
        ip_index++;
        set_state = 1;
        break;
    case 3:	//receive set response
        request_id = snmp_receive(&recv_msgbuf, &recv_addr);

        if(request_id == 0)	// not received
        {
            if(t5s_flag > 250)
            {
                if(snmp_agent_flag==0)
                {
                    sprintf(out_text, "Error    : Time out to set value to the host: %s     %s\r\n",Remote_Host_Name[ip_index], Remote_Host_IP[ip_index]);
                    debug_color_out(BRIGHT,RED,BLACK,out_text);
                }
                else
                {
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=1;
                    mib_curr_node=agent_tmp_node;
                    snmp_agent_flag=0;
                }
                ip_index++;
                set_ok_once=0;
                debug_cfg_end = 0;
                set_state = 1;
                break;
            }
            else
                break;
        }
        else	//received
        {
            if(snmp_agent_flag==0)
            {
                char	out_text1[256], byte_val1[16];
                //debug_ascii_out_proc("\rStatus  : ");
                //debug_color_out(BRIGHT,CYAN,BLACK,"Successful!    \r\n");
                //debug_ascii_out_proc("SNMP MSG: ");
                //sprintf(out_text1, "receive snmp response: error_status: %d, vartype: %d, msg_len: %d\r\n",recv_msgbuf.error_status, recv_msgbuf.var[0].vartype, recv_msgbuf.var[0].msglen);
                //debug_color_out(BRIGHT,CYAN,BLACK,out_text1);
                strcpy(out_text1,"");
                for(index=0; index<recv_msgbuf.var[0].msglen; index++)
                {
                    if((recv_msgbuf.var[0].msg[index] & 0xF0)==0)
                    {
                        strcat(out_text1,"0");
                        sprintf(byte_val1,"%X ",recv_msgbuf.var[0].msg[index]);
                        strcat(out_text1,byte_val1);
                    }
                    /*
                    else if((recv_msgbuf.var[0].msg[index] & 0x0F)==0)
                    {
                        sprintf(byte_val1, "%X", recv_msgbuf.var[0].msg[index]);
                        strcat(byte_val1,"0 ");
                        strcat(out_text1,byte_val1);
                    }
                    */
                    else
                    {
                        sprintf(byte_val1, "%X ", recv_msgbuf.var[0].msg[index]);
                        strcat(out_text1,byte_val1);
                    }
                }

                //if(save_flag<3 ||save_flag==4)
                //{
                //debug_ascii_out_proc("NewValue: ");
                //if(strlen(out_text1)==0)
                //    debug_color_out(BRIGHT,YELLOW,BLACK,"NULL");
                //else
                //    debug_color_out(BRIGHT,YELLOW,BLACK,out_text1);
                //sprintf(out_text1, "range: %d.%d - %d.%d", param_node->byte_begin, param_node->bit_begin, param_node->byte_end, param_node->bit_end);
                //debug_color_out(BRIGHT,YELLOW,BLACK,out_text1);//for look
                //debug_ascii_out_proc("\r\n");
                //}
                //*/
            }
            if(recv_msgbuf.error_status != 0)
            {
                if(snmp_agent_flag==0)
                {
                    //sprintf(out_text, "Error   : set value to the host: %s     %s\r\n",Remote_Host_Name[ip_index], Remote_Host_IP[ip_index]);
                    //debug_color_out(BRIGHT,RED,BLACK,out_text);
                    switch(recv_msgbuf.error_status)
                    {
                    case 1:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : TooBig\r\n");
                        break;
                    case 5:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : GenErr\r\n");
                        break;
                    case 6:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : NoAccess\r\n");
                        break;
                    case 7:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : WrongType\r\n");
                        break;
                    case 8:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : WrongLength\r\n");
                        break;
                    case 9:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : WrongEncoding\r\n");
                        break;
                    case 10:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : WrongValue\r\n");
                        break;
                    case 11:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : NoCreation\r\n");
                        break;
                    case 12:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : inconsistentValue\r\n");
                        break;
                    case 13:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : resourceUnavailable\r\n");
                        break;
                    case 14:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : commitFailed\r\n");
                        break;
                    case 15:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : UndoFailed\r\n");
                        break;
                    case 16:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : AuthorizationError\r\n");
                        break;
                    case 17:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : NoWritable\r\n");
                        break;
                    case 18:
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : inconsistentName\r\n");
                        break;
                    default:
                        break;
                    }
                }
                else
                {
                    debug_snmp_agent_buf[debug_agent_index].cmm_state=4;
                    debug_snmp_agent_buf[debug_agent_index].result_state=9;
                    mib_curr_node=agent_tmp_node;
                    snmp_agent_flag=0;
                }
            }
            else
            {
                if( save_flag==4)
                {
                    debug_ascii_out_proc("Host    : ");
                    sprintf(out_text,  "%s     %s  (State: Successful!)\r\n",Remote_Host_Name[ip_index], Remote_Host_IP[ip_index]);
                    debug_color_out(BRIGHT,YELLOW,BLACK,out_text);
                    if(debug_cfg==1)
                       mib_curr_node=temp_mib_node;
                    else if(debug_cfg==2)
                      {
                            menu_curr_node=tmp_menu_node;
                            mib_curr_node=temp_mib_node;
                        }
                    
                }

            }
        }
        debug_cfg_end = 0;
        set_state = 1;
        if(snmp_agent_flag==0)
        {
            if(save_flag>=3)
            {
                if(debug_cfg==1)
                    mib_curr_node=temp_mib_node;
                else if(debug_cfg==2)
                 {
                            menu_curr_node=tmp_menu_node;
                            mib_curr_node=temp_mib_node;
                        } 
                if(save_flag==3 && ip_index==0)
                    debug_mmi_contents("");
                save_flag=5;
                ip_index++;
                break;
            }
        }
        if(recv_msgbuf.error_status == 0)
        {
            if(snmp_agent_flag==2)
            {
                set_after_get=1;
                debug_snmp_agent_buf[debug_agent_index].cmm_state=1;
                debug_snmp_agent_buf[debug_agent_index].result_state=0;
                snmp_agent_flag=1;
            }
            else
            {
                if(save_flag<3 && !set_ok_once)
                {
                    get_state=3;
                    set_ok_once=1;
                }    
            }
            bind_num=1;
            set_state=0;
            debug_get_all_value = mib_curr_node->total_child_nodes;
        }
        if(del_flag==1)
            debug_mmi_contents("*");
        del_flag=0;
        ip_index++;
        if(snmp_agent_flag==0)
           set_state=1;
        break;

    default:
        ip_index=0;
        debug_cfg_end = 1;
        set_state = 0;
        break;
    }
}//end of debug_snmp_set_rt

int debug_snmp_get(BYTE oidlen,DWORD *oid,BYTE *data_ptr,BYTE *vartype)
{//??
    DWORD	ii;
    BYTE	temp_id;
    struct	debug_module_struct		*module_ptr;

    switch(oid[12])
    {
    case 0:
        break;
    case 1:
        break;
    case 2:		// configure
        if (oid[13] > MODULE_COUNT+1 || oid[13] < 2)
            return -1;
        temp_id = oid[13] - 2;
        module_ptr = (struct debug_module_struct *) &debug_shm_ptr->module_d[temp_id];
        switch(oid[14])
        {
        case 0:
            break;
        case 1:
            data_ptr[0] = module_ptr->status[0];
            *vartype = 4;
            return 1;
            break;
        case 2:
            strcpy((char *)data_ptr,module_ptr->version);
            return strlen(module_ptr->version);
            break;
        case 3:		// ascii in buffer
            break;
        case 4:		// ascii out buffer
            switch (oid[15])
            {
            case 1:		// get tail seq id
                ii = u32tobcd(data_ptr,module_ptr->seqid_t);
                *vartype = 4;
                return ii;
                break;
            case 2:		// get head seq id
                ii = u32tobcd(data_ptr,module_ptr->seqid_h);
                *vartype = 4;
                return ii;
                break;
            case 3:		// get out buffer
                if (module_ptr->seqid_h < module_ptr->seqid_t)
                {
                    ii = module_ptr->seqid_h + DEBUG_MAX_SEQID;
                    if (oid[16] >= ii || oid[16] < module_ptr->seqid_t)
                        return -1;
                }
                else
                {
                    if (oid[16] >= module_ptr->seqid_h || oid[16] < module_ptr->seqid_t)
                        return -1;
                }
                ii = oid[16] % DEBUG_SEQID_LEN;
                strcpy((char *)data_ptr, (char *)module_ptr->omc_outbuf[ii]);
                *vartype = 4;
                return strlen((char *)module_ptr->omc_outbuf[ii]);
                break;
            default:
                break;
            }
            break;
        default:
            break;
        }
        break;
    default:
        break;
    }
    return -1;
}

int debug_snmp_set(BYTE oidlen,DWORD *oid,BYTE *data_ptr,WORD data_len)
{
    BYTE	temp_id;

    switch(oid[12])
    {
    case 0:
        break;
    case 1:
        break;
    case 2:		// configure
        if (oid[13] > MODULE_COUNT+1 || oid[13] < 2)
            return -1;
        temp_id = oid[13] - 2;
        if (oid[14] != 3)	// not ascii in buffer
            return -1;
        if (data_len < 80)
        {
            if (debug_shm_ptr->module_d[temp_id].ascii_in_set_flag == 1)
            {
                debug_shm_ptr->module_d[temp_id].ascii_in[0] = disp_page_flag + 1;
                memcpy(debug_shm_ptr->module_d[temp_id].ascii_in+1,data_ptr,data_len);
                debug_shm_ptr->module_d[temp_id].ascii_in[data_len+1] = '\0';
            }
            return data_len;
        }
        break;
    default:
        break;
    }
    return -1;
}

void debug_mmi_list() 
{
    int        cur_b,cur_end,list_give_num;
    char       cur_c[64],temp[2],*p,arr[256],temp_str[256*5],*temp_ch;
    char      ins_str[16];
    int       i,j;
    DWORD     k; 
    BYTE     index;
    mib_child_node	*child_node_ptr;
    if(strlen((char *)list_msg)>96)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error   : No need so long string!\r\n");
        list_flag=0;
        debug_cfg_end=1;
        return;
    }
    switch(list_flag)
    {
    case 0:           //idel
        return;
    case 1:
        strcpy(arr, (char *)list_msg);
        for(i=0;i!=strlen(arr);i++)
        {
            if(*(arr+i)!=' ')
                break;
        }
        p=arr+i;
        strcpy(arr,p);
        list_give_num=0;
        if(mib_curr_node->total_instance==0)
        {
            debug_color_out(BRIGHT,RED,BLACK,"Fail    : This is not leaf node,so can't list the para and value.\r\n");
            list_flag=0;
            debug_cfg_end=1;
            return;
        }
        strcpy(temp,"");
        p=temp;
        if(strlen(arr)==0)
        {
            list_give_num=0;
        }
        while(strlen(arr)!=0)
        {
            for(i=0;i!=strlen(arr);i++)
            {
                if(*(arr+i)!=' ')
                    break;
            }
            p=arr+i;
            strcpy(temp_str,"");
            strcpy(temp_str,p);
            strcpy(arr,temp_str);
            i=0;
            if(arr[0]!='-' && !isdigit(arr[0]))
            {
                debug_color_out(BRIGHT,RED,BLACK,"Fail    : Format is not right!list [-a] [-e] [xxx-xxx].\r\n");
                list_flag=0;
                debug_cfg_end=1;
                return;
            }

            else if(strncmp(arr,"-s",2)==0)         //show get state,default hide
            {
                show_snmp_state=1;
                p=arr+2;
                strcpy(arr,p);
            }
            else if(strncmp(arr,"-a",2)==0)      //show all include NULL,default hide
            {
                show_all_value=1;
                p=arr+2;
                strcpy(arr,p);
            }
            else if(strncmp(arr,"-f",2)==0)    //show the special field
            {
                for(i=2;i<strlen(arr);i++)
                {
                    if(*(arr+i)==' ')
                        continue;
                    else if(*(arr+i)=='[')
                        break;
                    else
                    {
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : Please input format error!eg: list -f[2 3 10]!\r\n");
                        list_flag=0;
                        debug_cfg_end=1;
                        return;
                    }

                }
                show_field=1;
                p=arr+i+1;
                for(i=0;*(p+i)!='\0';i++)
                {
                    if(*(p+i)==']')
                        break;
                }
                strcpy(temp_str,"");
                strcpy(cur_c,"");
                cur_b=0;
                index=0;
                strncpy(temp_str,p,i);
                strcpy(arr,p+i+1);
                temp_str[i]='\0';
                i=0;
                j=0;
                k=0;
                for(i=0;i<64;i++)
                {
                    list_field_arr[i]=0xFF;
                }
                while(*(temp_str+k)!='\0')
                {
                    temp_ch=temp_str+k;
                    for(i=0;*(temp_ch+i)!='\0';i++)
                    {
                        if(!isdigit(*(temp_ch+i)) && *(temp_ch+i)!=' ' )
                        {
                            debug_color_out(BRIGHT,RED,BLACK,"Error   : Please input number when use the -f[]  flag!\r\n");
                            list_flag=0;
                            debug_cfg_end=1;
                            return;
                        }
                        else if(*(temp_ch+i)==' ')
                        {
                            break;
                        }
                        j++;
                        if(j>=3)
                        {
                            sprintf(cur_c,"Error   : Please input number between 1-%d!\r\n",mib_curr_node->total_child_nodes);
                            debug_color_out(BRIGHT,RED,BLACK,cur_c);
                            list_flag=0;
                            debug_cfg_end=1;
                            return;
                        }
                    }
                    if(i!=0)
                    {
                        strncpy(cur_c,temp_ch,i);
                        list_field_arr[index]=atoi(cur_c);
                        if(list_field_arr[index]<1 || list_field_arr[index]>(mib_curr_node->total_child_nodes))
                        {
                            sprintf(cur_c,"Error   : Please input number between 1-%d!\r\n",mib_curr_node->total_child_nodes);
                            debug_color_out(BRIGHT,RED,BLACK,cur_c);
                            list_flag=0;
                            debug_cfg_end=1;
                            return;
                        }
                        else
                            index++;
                    }
                    if(*(temp_ch+i)!='\0')
                        k+=i+1;
                    else
                        k+=i;
                    j=0;
                }
            }

            else if(strncmp(arr,"-r",2)==0)   //show para arrange,default hide
            {
                show_range=1;
                p=arr+2;
                strcpy(arr,p);
            }


            else if(strncmp(arr,"-t",2)==0)  //show para type,defualt hide
            {
                show_type=1;
                p=arr+2;
                strcpy(arr,p);
            }

            else if(strncmp(arr,"-e",2)==0)  //show without expand ,defualt expand
            {
                show_expend=0;
                p=arr+2;
                strcpy(arr,p);
            }
            else if(isdigit(arr[0]) || (arr[0]=='-' && isdigit(arr[1])))
            {
                p=temp;
                for(i=0;i<=strlen(arr);i++)
                {
                    *p=arr[i];
                    if(!isdigit(*p) && *p!='-')
                    {
                        debug_color_out(BRIGHT,RED,BLACK,"Error   : Please input number!format list:2-10\r\n");
                        list_flag=0;
                        debug_cfg_end=1;
                        return;
                    }
                    if(*p=='-')
                    {
                        if(i>=1)
                        {
                            strncpy(cur_c,arr,i);
                            cur_b=strtoul(cur_c,NULL,10);
                            break;
                        }
                        else
                        {
                            cur_b=0;
                            break;
                        }
                    }
                }
                i++;
                if(i==(strlen(arr)+2))
                    cur_end=mib_curr_node->total_instance-1;
                else
                {
                    strcpy(cur_c,"");
                    for(;i<strlen(arr);i++)
                    {
                        *p=arr[i];
                        if(!isdigit(*p))
                        {
                            debug_color_out(BRIGHT,RED,BLACK,"Error   : Please input number!\r\n");
                            list_flag=0;
                            debug_cfg_end=1;
                            return;
                        }
                        *(p+1)='\0';
                        strcat(cur_c,p);
                    }
                    cur_end=strtoul(cur_c,NULL,10);
                }
                if(i==strlen(arr))
                {
                    list_give_num=1;
                    break;
                }
            }

        }
        if(!list_give_num)
        {
            cur_b=0;
            cur_end=mib_curr_node->total_instance-1;
        }
        list_start=cur_b;
        list_end=cur_end;
        if(list_start>list_end)
        {
            debug_color_out(BRIGHT,RED,BLACK,"Error    : Input error!Example:list -a -e 2-10!\r\n");
            list_flag=0;
            debug_cfg_end=1;
            return;
        }
        if(list_end>mib_curr_node->total_instance-1)
        {
            sprintf(cur_c,"Error   : The max instance num is %d!\r\n",mib_curr_node->total_instance-1);
            debug_color_out(BRIGHT,RED,BLACK,cur_c);
            list_flag=0;
            debug_cfg_end=1;
            return;
        }
        list_flag=2;
        debug_cfg_end=0;
        break;
    case 2:
        //temp_instance=mib_curr_node->curr_instance;
        if(list_start<=list_end)
        {
            mib_curr_node->curr_instance=(DWORD)list_start;
            if(list_start+15<=list_end)
                bind_num=16;
            else
                bind_num=list_end-list_start+1;
            send_msgbuf.pdu_type = 0;
            send_msgbuf.request_id = 0;
            send_msgbuf.var_num =bind_num;
            for(j=0;j<bind_num;j++)
            {
                for(i=0; i<32; i++)
                    send_msgbuf.var[j].oid[i] = mib_curr_node->oid[i];
                send_msgbuf.var[j].oid[mib_curr_node->oid_len] = (DWORD)mib_curr_node->curr_instance+j;
                send_msgbuf.var[j].oidlen = mib_curr_node->oid_len + 1;
            }
            for(ip_index=0;ip_index<16;ip_index++)
            {
                if(IP_Selected[ip_index] != 1)
                    continue; 
                send_addr.remote_ip = Peer_IP[ip_index];
                break;
            }
            if(ip_index>=16)
            {
                debug_ascii_out_proc("Fail    : There is not host ip available.\r\n");
                return ;
            }
            send_addr.local_ip = Local_IP;
            send_addr.remote_port = SNMP_PORT;
            send_addr.local_port = DEBUG_SNMP_PORT;
            send_addr.broadcast = 0;
            recv_msgbuf.pdu_type = 2;
            recv_msgbuf.request_id = 0;
            recv_msgbuf.var_num = bind_num;
            for(j=0;j<bind_num;j++)
            {
                for(i=0; i<32; i++)
                    recv_msgbuf.var[j].oid[i] = mib_curr_node->oid[i];
                recv_msgbuf.var[j].oid[mib_curr_node->oid_len] = (DWORD)mib_curr_node->curr_instance+j;
                recv_msgbuf.var[j].oidlen = mib_curr_node->oid_len + 1;
                for(i=0; i<256; i++)
                    recv_msgbuf.var[j].msg[i] = 0;
            }
            recv_addr.remote_ip = send_addr.remote_ip;
            recv_addr.local_ip = Local_IP;
            recv_addr.remote_port = SNMP_PORT;
            recv_addr.local_port = DEBUG_SNMP_PORT;
            recv_addr.broadcast = 0;
            bind_index=0; 
            get_state=1;
            debug_cfg_end = 0;
            list_field=mib_curr_node->total_child_nodes;
            list_start+=bind_num;
        } 
        list_flag=3;
        break;
    case 3:
    	  if(get_state==3 && bind_index<bind_num) 
    	     return; 
    	  //if(get_state==3)
        if(get_state==3 && bind_index>=bind_num && bind_num!=0 && list_start<list_end)
        {         
                list_flag=2;
                get_state=0;
                debug_cfg_end=0; 
                return;
        }
        if(get_state==0 && list_start>=list_end+1)
        {

            show_all_value=0; 
            show_type=0;
            show_range=0;
            show_field=0;
            bind_num=1;
            bind_index=0;
            show_snmp_state=0;
            show_expend=1;
            debug_cfg_end=0;
            /*
            if(read_syntax_nor_list==0 && list_once==1)
            {
            	list_start=0;
              list_end=0;
              list_flag=0;
              compute_field_list_index++; 
              if(compute_field_list_index == compute_list_num && compute_field_list_index>0)
	            {
	                read_syntax_nor_list = 1;
	                field_compute_state=5;
	                mib_curr_node=temp_compute_node;
	                list_field=mib_curr_node->total_child_nodes;
	                debug_mmi_get_all();  
                  debug_cfg_end=0;
	            }	            
              return;
            } 
            */    
            
            
            list_flag=4;
            break;                                                      
        }
        if(t5s_flag> 250 && list_once==0)
        {
            list_flag=0;
            list_start=0;
            list_end=0;
            debug_ascii_out_proc("Instance: ");
            debug_color_out(BRIGHT,RED,BLACK,"Unknow Because can not get it value in time\r\n");
            strcpy(temp_str,"");
            debug_ascii_out_proc("Child   : ");
            sprintf(temp_str, "[%d]\r\n",mib_curr_node->total_child_nodes);
            debug_color_out(BRIGHT,YELLOW,BLACK,temp_str);

            for (i = 0; i < mib_curr_node->total_child_nodes; i++)
            {
                child_node_ptr = mib_curr_node->child_nodes[i];
                if(child_node_ptr)
                   sprintf(temp_str, "           |---(%u) %s\r\n",child_node_ptr->oid+1,child_node_ptr->name);
                debug_color_out(BRIGHT,YELLOW,BLACK,temp_str);
            }
            debug_cfg_end=1;
        } 
        break;
    case 4: 
    	    if(list_once==0)
          {
                if(list_start>=list_end+1)
                {
                    for(i=0;i<256;i++)
                    {
                        if(curr_instance_arr[i].eable==1)
                            break;
                    }
                    if(i==256) 
                    {
                        //debug_color_out(BRIGHT,RED,BLACK,"Error   : no instance was used!\r\n");
                        none_instance=1;
                    }
                    else
                        none_instance=0;
                    mib_curr_node->curr_instance=i;
                    temp_instance=mib_curr_node->curr_instance;
                }
          }
          else
                mib_curr_node->curr_instance=temp_instance;
                                                
          if (mib_curr_node->total_instance)
          {
                strcpy(temp_str,"");
                strcpy(ins_str,"");
                k=0;
                for(i=0;i<=255;i++)
                {
                    if(curr_instance_arr[i].eable==1)
                    {
                        sprintf(ins_str,"#%-3d ",i);
                        strpcat(temp_str,&k,ins_str);
                    }
                }
                debug_ascii_out_proc("Instance: "); 
                strcpy(ins_str,"");
                if(k)
                    sprintf(ins_str, "#%-3d(%-3d): ", mib_curr_node->curr_instance,mib_curr_node->total_instance);
                else
                    sprintf(ins_str, "None(%-3d): ", mib_curr_node->total_instance);
                debug_color_out(BRIGHT,YELLOW,BLACK,ins_str);
                debug_color_out(BRIGHT,BLUE,BLACK,temp_str);
                debug_ascii_out_proc("\r\n");
                if(k)
                    none_instance=0;
                else
                    none_instance=1;
          }
          list_flag=0;
          get_state=0; 
          debug_cfg_end=1;
    
          IP_Selected[0]=1;
	        for(i=1;i<16;i++)
	              IP_Selected[i]=0;	                  
	        if(list_once==0)
	        {	            	  
	                if(none_instance==0)
	                { 
	                    if(mib_curr_node->max_access>=2 && mib_curr_node->max_access<=5)
	                    {
	                        
	                        debug_mmi_get_all();
	                        debug_cfg_end=0;
	                    }
	                }
	                else
	                {
	
	                    strcpy(temp_str,"");
	                    debug_ascii_out_proc("Child   : ");
	                    sprintf(temp_str, "[%d]\r\n",mib_curr_node->total_child_nodes);  
	                    debug_color_out(BRIGHT,YELLOW,BLACK,temp_str);
	
	                    for (i = 0; i < mib_curr_node->total_child_nodes; i++)
	                    {
	                        child_node_ptr = mib_curr_node->child_nodes[i];
	                        sprintf(temp_str, "           |---(%u) %s\r\n",child_node_ptr->oid+1, child_node_ptr->name);
	                        debug_color_out(BRIGHT,YELLOW,BLACK,temp_str);
	                    }
	                    debug_cfg_end=1;
	
	                }
	        }
          list_once=1;
          list_start=0;
          list_end=0;
          list_flag=0;
          break;
    	  
    }
    return ;
}



void register_instance()
{
    char ascii_text[32],byte_val1[8];
    int i,index;
    if(mib_curr_node->total_instance) 
    {
        memset(curr_instance_value,0,sizeof(char));
        //strncpy(curr_instance_value,mib_curr_node->init_value,mib_curr_node->data_length);
        *curr_instance_value='\0';
        for(index=0; index<mib_curr_node->data_length; index++)
        {
           if((mib_curr_node->init_value[index] & 0xF0)==0)
           {
                        strcat(curr_instance_value,"0");
                        sprintf(byte_val1,"%X",mib_curr_node->init_value[index]);
                        strcat(curr_instance_value,byte_val1);
           }                   
           else
           {
                        sprintf(byte_val1, "%X", mib_curr_node->init_value[index]);
                        strcat(curr_instance_value,byte_val1);
           }
        }
        gcvt((double)(mib_curr_node->total_instance-1),3,ascii_text);
        strcpy((char *)list_msg,"");
        strcat((char *)list_msg," -e ");
        strcat((char *)list_msg,"0");
        strcat((char *)list_msg,"-");
        strcat((char *)list_msg,ascii_text);


        //default_value(mib_curr_node->oid,mib_curr_node->oid_len,curr_instance_value);
        show_list=0;
        for(i=0;i<=255;i++)
        {
            curr_instance_arr[i].eable=0;
        }
        debug_mmi_list();
    }
}



//////////////////////////////End of Operation get-set///////////




//////////////////////////////Data Send and recieve//////////////

void debug_send_serial()
{
    //int		n;
    int		i;
    message_list	iptr_msg;

    for (i = 0;i < MAX_SEND_CHAR_NUM ;i ++)
    {
        if (sendserbuf_t != sendserbuf_h)
        {
            //n = write(com_port,&sendser_buf[sendserbuf_t],1);
            //			if (n > -1)
            //			{
            debug_shm_ptr->TelnetSendData.telsend_data[debug_shm_ptr->TelnetSendData.telsend_datah] = sendser_buf[sendserbuf_t];
            sendserbuf_t = (sendserbuf_t + 1) % SERIAL_BUF_LEN;
            debug_shm_ptr->TelnetSendData.telsend_datah = (debug_shm_ptr->TelnetSendData.telsend_datah+1)%TELNET_SEND_BUF_LEN;
            if (debug_shm_ptr->TelnetSendData.telsend_datah > 256)
                //			if (debug_shm_ptr->TelnetSendData.telsend_datah == 0)
                SendTelnetFlag = 1;
            //			}
        }
    }
    if (SendTelnetFlag == 1)
    {
        if (debug_shm_ptr->TelnetSendData.telsend_datah > 0)
        {
            iptr_msg.msgSrcPort = DEBUG_PORT;
            iptr_msg.msgLength = debug_shm_ptr->TelnetSendData.telsend_datah;
            memcpy(iptr_msg.msgContent,debug_shm_ptr->TelnetSendData.telsend_data,debug_shm_ptr->TelnetSendData.telsend_datah);
            iptrPutMessage(iptr_msg);
            memset(debug_shm_ptr->TelnetSendData.telsend_data,0,sizeof(BYTE)*TELNET_SEND_BUF_LEN);
            debug_shm_ptr->TelnetSendData.telsend_datah = 0;
        }
        SendTelnetFlag = 0;
    }
}

int debug_send_char(BYTE ch)
{
    sendser_buf[sendserbuf_h] = ch;
    sendserbuf_h = (sendserbuf_h + 1) % SERIAL_BUF_LEN;
    return 1;
}


int debug_send_string(const char *str_send)
{
	  int len,i;
	  i=0;
	  len=strlen(str_send);
	  if(sendserbuf_h+len>SERIAL_BUF_LEN && (sendserbuf_h+len)% SERIAL_BUF_LEN<sendserbuf_t)
	  {
	     while(sendserbuf_h<SERIAL_BUF_LEN) 
	         *(sendser_buf+sendserbuf_h++)=*str_send++;
	     while(i<sendserbuf_t && (*str_send)) 
	         *(sendser_buf+i++)=*str_send++; 
	     sendserbuf_h=i-1; 
	  }
	  else if(sendserbuf_h+len<SERIAL_BUF_LEN)
	  {
	     while((*(sendser_buf+sendserbuf_h++)=*str_send++));
	     sendserbuf_h--;
	  }	     
	  return 1;
}

//void debug_clear_serial()
//{
//	sendserbuf_h = sendserbuf_t = 0;
//}

void debug_read_serial()
{
    int 	i;
    message_list	iptr_msg;

   
    //telnet iptrans
    if (iptrGetMessage(&iptr_msg,DEBUG_PORT))
    {
        for (i = 0;i < iptr_msg.msgLength;i ++)
        {
            recser_buf[recserbuf_h] = iptr_msg.msgContent[i];           
            if(Init_Page==0 && recser_buf[recserbuf_h]=='x' )
            {
            	recser_buf[recserbuf_h]='n';
            	disp_main_page_num	=	0;
            	ascii_out_ph=ascii_out_pt=0;
            	Init_Page=1; 
            }     
            //if(Init_Page==1 && send_char_flag==1 && recser_buf[recserbuf_h]>='a' && recser_buf[recserbuf_h]<='a'+MODULE_COUNT)  
            //    send_char_flag=0;    
            recserbuf_h = (recserbuf_h + 1) % SERIAL_BUF_LEN;
        }
    }
}

int debug_read_char()
{
    char sig;

    if (recserbuf_t == recserbuf_h)
        return 0;
    if (recserbuf_t != recserbuf_h)
    {
        sig = recser_buf[recserbuf_t];
        recserbuf_t = (recserbuf_t + 1) % SERIAL_BUF_LEN;
    }
    return sig;
}


void debug_recv_cmd()
{
    int		i;
    char	pp;
    int		temp_page;
    char    pr_buf[30];//--lw 
    pp = debug_read_char();
    switch(pp)
    {
    case 0:
        return;
    case 113://"Q"
    case 81: //'q'  //--lw
        {
            first_page = 1;
            disp_main_flag = 0;
            if(debug_cfg)
               debug_mmi_save();
            Init_Page=0;
            mib_curr_node=mib_root_node;
            menu_curr_node=menu_root_node;
            debug_cfg=0;
            debug_cfg_end=0;
            debug_ascii_out_ph=debug_ascii_out_pt=0;
            ascii_out_pt=ascii_out_ph=0;
            /*
            if(debug_cfg>0)
            {
                debug_send_char('C');
                debug_send_char('M');
                debug_send_char('M');
                debug_cfg=0;
            }
            */
            break;
        }
    }
    switch (disp_main_flag)
    {
    case 0:		// enter in menu page
        if (isupper(pp))	// transfer the upper to lower
            pp = tolower(pp);
        if (pp == 'x')
        {
            m_state	= 0;
            disp_main_flag = 0;
        }
        //else if ((pp >= 'a') && (pp < 'a'+MODULE_COUNT))
        else if ((pp >= 'a') && ((pp <= 'a'+15 ) || (pp >= 17 + 'a' && pp <= 22 + 'a') || pp == 'y' || pp == 'z' ) ) //!=x !=q
        {
            disp_main_flag = 1;
            debug_module_num= pp - 'a';
            disp_sub_module_flag = pp - 'a';
        	  if( disp_sub_module_flag >= PAGE_MODULE_COUNT ) 
        	  	  disp_sub_module_flag -= 2;
        	  else if( disp_sub_module_flag > 16 && disp_sub_module_flag < 23 ) 
        	  	  disp_sub_module_flag--;
            debug_module_num += disp_main_page_num*PAGE_MODULE_COUNT;
            disp_sub_module_flag += disp_main_page_num*PAGE_MODULE_COUNT;        	  	  
            m_state_sub_module = 0;
            disp_page_flag = 0;
            command_flag = 0;
            // title_top_pt = 0;
            ascii_out_ph = ascii_out_pt = 0;
            // strcpy(ascii_out_array,"");
            // show the help info when enter in a new module
            if(debug_shm_ptr != NULL)  
            if (debug_shm_ptr->module_d[disp_sub_module_flag].ascii_in_set_flag == 1)
            {
                debug_shm_ptr->module_d[disp_sub_module_flag].ascii_in[0] = disp_page_flag + 1;
                strcpy((char *)debug_shm_ptr->module_d[disp_sub_module_flag].ascii_in+1,"help");
            }
            memset(current_timer,0,sizeof(current_timer));
        }
        else if ((pp >= '1') && (pp <= '9'))
        {
        	disp_main_page_num = pp - '1';
					if(disp_main_page_num >= DEBUG_MAX_PAGE)
						disp_main_page_num = 0;
          m_state	= 0;
          disp_main_flag = 0;
        }	
        break;
    case 1:		// enter in module page
        if (command_flag == 0)
        {
            if (isupper(pp))	// transfer the upper to lower
                pp = tolower(pp);
            if (pp == 'x')
            {
                disp_main_flag = 0;
                m_state	= 0;
                disp_page_flag = 0;
            }
            else if ((pp >= '0') && (pp <= '9'))
            {
                temp_page = pp - '0';
                if ( (debug_shm_ptr->module_d[disp_sub_module_flag].page_d[temp_page].pointer_set_flag == 0) && (temp_page != 0))
                    return;	// invalid page
                disp_page_flag = pp - '0';
                m_state_sub_module = 0;
                //					title_top_pt = 0;
            }
            else if ((pp >= 'a') && (pp <= 'f'))
            {
                temp_page = pp - 'a' + 0x0a;
                if (debug_shm_ptr->module_d[disp_sub_module_flag].page_d[temp_page].pointer_set_flag == 0)
                    return;	// invalid page
                disp_page_flag = pp - 'a' + 0xa;
                m_state_sub_module = 0;
                //					title_top_pt = 0;
            }
            else if (pp == 27)	// ESC
            {
                //debug_clear_serial();
                if (disp_page_flag != 0)	// not page 0
                {
                    for (i = 0;i < strlen(prompt_line25);i ++)
                        debug_send_char(prompt_line25[i]);
                }
                else
                {
                    //debug_send_char(0xa);//--lw
                    //debug_send_char(0xd);//--lw
                    if(disp_sub_module_flag == ('n'-'a'))
                    {
                        if(debug_cfg == 0) //cmm status
                        {
                            if(debug_module_num == ('n'-'a'))
                            {
                                sprintf(pr_buf,"CMM>");
                                Init_Page=2;
                                TxtColor(BRIGHT,WHITE,BLACK,pr_buf);
                                for(i=0;i<30;i++)
                                    if(pr_buf[i]!='\0')
                                        debug_send_char(pr_buf[i]);
                                    else
                                        break;
                                m_state_sub_module = 2;
                            }
                            else
                            {
                                sprintf(pr_buf,"%s>",debug_shm_ptr->module_d[debug_module_num].name);
                                TxtColor(BRIGHT,WHITE,BLACK,pr_buf);
                                for (i = 0;i < strlen(pr_buf);i ++)
                                    debug_send_char(pr_buf[i]); 
                            }
                        }
                        else if(debug_cfg ==1) //cfg status
                        {
                            sprintf(pr_buf,"CFG>");
                            TxtColor(BRIGHT,WHITE,BLACK,pr_buf);
                            debug_send_string(pr_buf);
                        }
                        else if(debug_cfg==2)
                        {
                        	  sprintf(pr_buf,"MENU>");
                            TxtColor(BRIGHT,WHITE,BLACK,pr_buf);
                            debug_send_string(pr_buf);
                        }
                    }
                    else
                    {
                        debug_send_string(debug_shm_ptr->module_d[disp_sub_module_flag].name);
                        debug_send_char('>');
                    }

                } 
                ascii_in_array[0] = disp_page_flag + 1;
                ascii_in_pt	= 1;
                command_flag = 1;
                SendTelnetFlag = 1;
            }
        }
        else	// in command mode
        {
            switch (pp)
            {
            case 27:	// ESC
                //debug_send_char(0xa);
                //debug_send_char(0xd);
                command_flag = 0;
                ascii_in_array[0] = disp_page_flag + 1;
                ascii_in_pt	= 1;
                break;
            case 8:		// backspace
                if (ascii_in_pt > 1)
                    ascii_in_pt -= 1;
                debug_send_char(pp);
                SendTelnetFlag = 1;
                break;
            case 10:	// line feed
                if (ascii_in_pt == 1)	// empty
                    return;
                break;
            case 13:	// return
                if (ascii_in_pt == 1)	// empty
                    return;
                //debug_send_char(pp);
                SendTelnetFlag = 1;
                //if (disp_page_flag == 0)	// display page 0
                //	debug_send_char(10);	// send line feed
                ascii_in_array[ascii_in_pt] = '\0';
                if (debug_shm_ptr->module_d[disp_sub_module_flag].ascii_in_set_flag == 1)
                    strncpy((char *)debug_shm_ptr->module_d[disp_sub_module_flag].ascii_in, (char *)ascii_in_array,80);
                command_flag = 0;
                ascii_in_pt = 0;
                ascii_in_array[0] = '\0';
                break;
            default:
                ascii_in_array[ascii_in_pt++] = pp;
                //debug_send_char(pp); 
                SendTelnetFlag = 1;
                break;
            } 
        }
        break;
    default:
        disp_main_flag = 0;
        break;
    }
}




//////////////////////////////end of Data Send and recieve//////////////

/////////////////////////////compute field////////////////////////////

/*
* 
* [I]TG to Prefix
* {
*   [v]OID="3.2.2.1.3"   
*   [l]OID.#.1           //for while 
*   [p]TG=(OID.*.1)      //print
* }
* 
* 
* [I]Prefix to TG
* {
*  [v]OID1="3.2.2.1.3"
*  [v]OID2="3.2.2.1.4"
*  [l]OID1.#.1
*  [l]OID2.#.1
*  [c](OID1.#.1)==(*) [p]prefix (#)=(OID2.#.1)   //if 
* }
                */
BYTE create_compute_field(FILE * fp, mib_node * node_ptr, BYTE field_id,char *name)
{
     char                data_arr[256];
     
   
     compute_field_arr[compute_field_num].field_id = field_id;
     strcpy(compute_field_arr[compute_field_num].name,name);
     while(!feof(fp))
     {
          mib_line_count++;
          if(read_line(fp, data_arr) == 0)
               continue;
          del_space(data_arr, 2);
          switch (data_arr[0])
          {
          case '{':            //"{"
               break;
          case '[':
               if(data_arr[1] == 'c' || data_arr[1] == 'v' || data_arr[1] == 'p' || data_arr[1] == 'l')
               {                   
                    if(strlen(data_arr) < 64)
                         strcpy(compute_field_arr[compute_field_num].syntax_str[compute_field_arr[compute_field_num].syntax_count], data_arr);
                    else
                    {
                         printf("[Debug] Error : Do not support so much line script for compute node.line:%d", mib_line_count);
                         return 0;
                    }
                    if(compute_field_arr[compute_field_num].syntax_count < 10)
                         compute_field_arr[compute_field_num].syntax_count++;
                    else
                    {
                         printf("[Debug] Error : Do not support so much line script for compute node.line:%d", mib_line_count);
                         return 0;
                    }
               }
               break;
          case '}':
          	   if(compute_field_num<16)
                  compute_field_num++; 
               else
                  return 0;
               
               return 1;
          default:
               printf("[Debug] Error : The mib compute field is not right.line:%d", mib_line_count);
               return 0;
          }
     }
     return 1;
}

void translate_compute_field() 
{
     char               *p, *temp_ch, temp_str[32],temp_str1[64], line[64]; 
     DWORD               oid[32];
     char                cmp_value[2][32];
     char                temp_value[32];
     BYTE                i,j, value_index,read_next_line=1,index;
     WORD                curr_instance_index;
     mib_node            *find_list_node;

     if(field_compute_state == 1)
     {
          memset(OID_val, 0, 5 * 16 * sizeof(DWORD));
          memset(OID_name_str, 0, 5 * 32 * sizeof(char));
          line_index = 0;
          OID_index=0;
          compute_field_list_index=0;
          compute_list_num=0;
          compute_state = 0;
     } 
     
     if(read_syntax_nor_list>0)
     {    	    
           while(line_index < compute_curr_unit.syntax_count && strlen(compute_curr_unit.syntax_str[line_index]) != 0)
           {
           	   if(!print_line_index)
          	       field_compute_state=2;
          	   else
          	       field_compute_state=6; 
               if(read_syntax_nor_list == 0)
               {      
               	 field_compute_state=3;
               	 print_line_index=line_index;        
                    return ;
               }
               if(read_next_line==1 && line_index<compute_curr_unit.syntax_count)
               {                    
                    strcpy(line, compute_curr_unit.syntax_str[line_index]); 
                    line_index++;
                    del_space(line, 2);                    
               }    
               read_next_line=1;           
               switch(compute_state) 
               {
               case 0:     //idel
                    compute_state = 1;
                    line_index--;
                    break;
               case 1:     //[v]
                    if((temp_ch = strstr(line, "[v]")) != NULL)
                    {
                         if(OID_index > 5)
                         {
                              printf("[Debug] Error: The max define oid number is 5.\r\n");
                              return ;
                         }
                         temp_ch += 3;
                         p = strtok(temp_ch, "=");  //variable name
                         strcpy(OID_name_str[OID_index], p);   
                         p=strtok(NULL,"=");        
                         strcpy(temp_str, p);                          
                         p = strtok(temp_str, ".");
                         for(i=0;i<9;i++)
                              OID_val[OID_index][i]=cur_oid[i];
                         OID_val[OID_index][i++] = atoi(p);
                         while((p = strtok(NULL, ".")))
                              OID_val[OID_index][i++] = atoi(p);
                         OID_index++;
                    }
                    else if((temp_ch = strstr(line, "[l]")) != NULL)
                    {
                         compute_state = 2;
                         line_index--;
                         break;
                    }
                    else
                    {
                         printf("[Debug] Error: The compute filed script is error.\r\n");
                         return ;
                    }
                    compute_state = 1;
                    break;
               case 2:      //[l]
                    if((temp_ch = strstr(line, "[l]")) != NULL)
                    {                                      	 
                       temp_ch +=3;     
                       /*                                                        
                       if(temp_ch[0] == '.' && temp_ch[1]=='#')
                       {                      	
                           compute_field_list[compute_list_num].list_node = mib_curr_node;   
                           strcpy(temp_str,temp_ch+3);                                                  
                           if(test_str(temp_str, 0) && atoi(temp_str) < MAX_SELECT_OPTION_NUM)
                           {
                                compute_field_list[compute_list_num].list_field_id = atoi(temp_str);
                                compute_list_num++;
                                break;
                           }                         
                           printf("[Debug] Error: The field id in [l] field is error.%s", temp_str);
                           return ;
                       }                    
                       else 
                       */
                       if((p=strtok(temp_ch,"."))!=NULL)
                       {
		                       for(i = 0; i < OID_index && strlen(OID_name_str[i]) != 0; i++)
		                       {
		                           if(strcmp(OID_name_str[i], p) == 0)
		                                  break;
		                       }
		                       if(i == OID_index || strlen(OID_name_str[i]) == 0)
		                       {
		                            printf("[Debug] Error: Can not define the string:%s.\r\n", p);
		                            return ;
		                       }                     
		                       for(j = 0; j < 32 && OID_val[i][j] != 0; j++)
		                       {
		                           oid[j] = OID_val[i][j];		                           
		                       }
		                       find_list_node=search_mib_node(mib_root_node,oid,j);
                           if(find_list_node!=NULL)
                               compute_field_list[compute_list_num].list_node = find_list_node;
                           else
                           {
                           	  printf("[Debug] Error: [l] field :no such mib node for the oid.\r\n");
		                         return ;                  	
                           }    
                           p=strtok(NULL,".");                           
		                       if(*p=='?')
		                       {
		                           if((p=strtok(NULL,"."))!=NULL)
		                           {
			                           if(test_str(p, 0) && atoi(p) < MAX_SELECT_OPTION_NUM)
			                           {
			                               compute_field_list[compute_list_num].list_field_id = atoi(p);
			                               compute_list_num++;
			                               break;
			                           }			                          
			                         }			                        
		                       }		                       
		                       printf("[Debug] Error: [l] field is error.%s", temp_str);
		                       return ;                           
                       }
                    }
                    else if((temp_ch = strstr(line, "[p]") )!= NULL)
                    {
                         read_syntax_nor_list = 0;
                         compute_state = 3;
                         line_index--; 
                         break;
                    }
                    compute_state = 2;
                    break; 
              case 3:      //[p]
                   if((temp_ch = strstr(line, "[p]")) != NULL && (strstr(line, "[c]")) == NULL)
                   { 
                       temp_ch += 3;  
                       for(i=0;temp_ch[i] != '\0';i++)                    
                       {
                           if(temp_ch[i] == '=') 
                              break;                        
                       }
                       if(temp_ch[i] == '\0')
                       {
                           printf("[Debug] Error: The field format is not right.\r\n"); 
                           return ;
                       } 
                       strncpy(temp_str, temp_ch, i);
                       temp_str[i] = '\0';
                       if(strstr(temp_str,"("))
                       {
                           for(i=0;temp_str[i] != '\0';i++)
                           {
                           if(temp_str[i] == '(') 
                               break;
                           }
                           if(i == strlen(temp_str) && strstr(temp_str, ")") == NULL)
                           {
                            printf("[Debug] Error: The field format is not right.\r\n"); 
                            return ;
                           }     
                           strncpy(compute_curr_unit.print_name_str[compute_curr_unit.print_count], temp_str, i); 
                           compute_curr_unit.print_name_str[compute_curr_unit.print_count][i] = '\0';
                           value_index = take_compute_value(temp_str + i,temp_value);
		                       if(value_index == 0)
		                       {
		                           printf("[Debug] Error: The field format is not right.\r\n"); 
		                           return ;
		                       }
		                       if(value_index == 1)
		                           strcat(compute_curr_unit.print_name_str[compute_curr_unit.print_count], temp_value);		                    
		                   }
		                   else
		                        strcpy(compute_curr_unit.print_name_str[compute_curr_unit.print_count], temp_str);
		                   temp_ch+=i+1;
                       for(i=0;temp_ch[i] != '\0';i++)
                       {
                           if(temp_ch[i] == '(')
                              break;
                       }
                       if(temp_ch[i] == '\0' && strstr(temp_ch, ")") == NULL)
                       {
                            printf("[Debug] Error: The field format is not right.\r\n"); 
                            return ;
                       }
                       temp_ch += i; 
                       value_index = take_compute_value(temp_ch,temp_value);
                       if(value_index == 0)
                       {
                           printf("[Debug] Error: The field format is not right.\r\n"); 
                           return ;
                       }
                       if(value_index == 1)
                            strcpy(compute_curr_unit.print_val_str[compute_curr_unit.print_count], temp_value);
                       compute_curr_unit.print_count++;
                   } 
                   else if((temp_ch = strstr(line, "[c]") )!= NULL)
                   {
                         line_index--;
                         compute_state = 4;
                         break;
                   }
                   compute_state = 3;
                   break;
              case 4:       //[c]
                   if((temp_ch = strstr(line, "[c]")) != NULL && strstr(line, "==") != NULL && (strstr(line, "[p]")) != NULL)
                   {
                   	                       	     
                   	     for(curr_instance_index=0;curr_instance_index<mib_curr_node->total_instance;curr_instance_index++)
                   	     {
                   	     	   strcpy(temp_str1,line); 
                   	     	   temp_ch = strstr(temp_str1, "[c]"); 
                   	     	   compute_curr_instance=curr_instance_index;
                             temp_ch += 3;
                             p = strtok(temp_ch, "=[");
                             if(p[0] == '(' && p[strlen(p) - 1] == ')')
                             {
                                 value_index = take_compute_value(p,temp_value);
                                 if(value_index == 0)
                                 {
                                     printf("[Debug] Error: The field format is not right.\r\n"); 
                                     return ;
                                 }
                                  if(value_index == 1)
                                      strcpy(cmp_value[0], temp_value);                               
                             }
                             p = strtok(NULL, "=[");;
                             if(p[0] == '(' && p[strlen(p) - 1] == ')')
                             {
                                  value_index = take_compute_value(p,temp_value);
                                  if(value_index == 0)
                                  {
                                      printf("[Debug] Error: The field format is not right.\r\n");
                                      return ;
                                  }
                                  if(value_index == 1)
                                       strcpy(cmp_value[1], temp_value);                          
                             }    
                             else
                             {
                                      printf("[Debug] Error: The field format is not right.\r\n");
                                      return ;
                             }
                             if(strcmp(cmp_value[0], cmp_value[1]) == 0)
                             {
                             	  strcpy(temp_str1,line);
                                if((temp_ch = strstr(temp_str1, "[p]")) != NULL)
                                {
                                	
                       							temp_ch += 3; 
                       							for(i=0;temp_ch[i] != '\0';i++)                     
                       							{
								                           if(temp_ch[i] == '=') 
								                              break;                        
                       							}
							                      if(temp_ch[i] == '\0')
							                      {
							                           printf("[Debug] Error: The field format is not right.\r\n"); 
							                           return ;
							                      } 
							                       strncpy(temp_str, temp_ch, i);
							                       temp_str[i] = '\0';
							                       if(strstr(temp_str,"("))
                       							 {
							                           for(i=0;temp_str[i] != '\0';i++)
							                           {
							                             if(temp_str[i] == '(') 
							                               break;
                           							 }
								                         if(i == strlen(temp_str) && strstr(temp_str, ")") == NULL)
								                         {
								                            printf("[Debug] Error: The field format is not right.\r\n"); 
								                            return ;
                           			         }     
							                           strncpy(compute_curr_unit.print_name_str[compute_curr_unit.print_count], temp_str, i); 
							                           compute_curr_unit.print_name_str[compute_curr_unit.print_count][i] = '\0';
							                           value_index = take_compute_value(temp_str + i,temp_value);
									                       if(value_index == 0)
									                       {
									                           printf("[Debug] Error: The field format is not right.\r\n"); 
									                           return ;
									                       }
									                       if(value_index == 1)
									                           strcat(compute_curr_unit.print_name_str[compute_curr_unit.print_count], temp_value);
		                     
		                   						   }
									                   else
									                        strcpy(compute_curr_unit.print_name_str[compute_curr_unit.print_count], temp_str);
								                     temp_ch+=i+1;
							                       for(i=0;temp_ch[i] != '\0';i++)
							                       {
							                           if(temp_ch[i] == '(')
							                              break;
							                       }
							                       if(temp_ch[i] == '\0' && strstr(temp_ch, ")") == NULL)
							                       {
							                            printf("[Debug] Error: The field format is not right.\r\n"); 
							                            return ;
							                       }
							                       temp_ch += i; 
							                       value_index = take_compute_value(temp_ch,temp_value);
							                       if(value_index == 0)
							                       {
							                           printf("[Debug] Error: The field format is not right.\r\n"); 
							                           return ;
							                       }
							                       if(value_index == 1)
							                            strcpy(compute_curr_unit.print_val_str[compute_curr_unit.print_count], temp_value);                      
                       								compute_curr_unit.print_count++;
                                   
                                }
                                else
                                {
                                     printf("[Debug] Error: No '[p]' in the [c] line.\r\n"); 
                                     return ;
                                }
                             }                                                                                                           
                         
                         } 
                         compute_curr_instance=0;        
                   }
                   else if((temp_ch = strstr(line, "[p]")) != NULL)
                   {
                       line_index--;
                       compute_state = 4; 
                       break;
                   }
                  break;
               }
           }
           line_index=print_line_index; 
           for(index=0;index<compute_field_num;index++)
    			 {
				       if(mib_curr_node==compute_field_arr[index].compute_node)
				       {				       	      
				        	 memcpy(compute_field_arr+index,&compute_curr_unit,sizeof(compute_field)); 			        	 	  
				       }
    			 }
    			 field_compute_state=7;
     }
} 

BYTE take_compute_value(char *str,char *temp_value) 
{
     char temp_str[32], temp_str1[32], *temp_ch;
     BYTE value_index, i,compute_list_index;
     
     temp_ch=str;
     for(i=0;temp_ch[i]!= '\0';i++) 
     {
           if(temp_ch[i] == '(')
             break;
     }
     if(temp_ch[i] == '\0')
     {
           printf("[Debug] Error: The field format is not right.\r\n");
           return 0;
     }
     temp_ch += i + 1; 
     i = 0;
     while(temp_ch[i] != '\0')
     {
           if(temp_ch[i] == ')') 
             break;
           i++;
     }
     if(temp_ch[i] == '\0')
     {
          printf("[Debug] Error: The field format is not right.\r\n");
          return 0;
     }
     strncpy(temp_str, temp_ch, i); 
     temp_str[i] = '\0';
     if(temp_str[0] == '*')
     {
       sprintf(temp_value, "%d", mib_curr_node->curr_instance+bind_index); 
       value_index = 1;
       return value_index;
     }
     else if(temp_str[0] == '#')
     {
     	  sprintf(temp_str,"%d",compute_curr_instance);
        strcpy(temp_value, temp_str);
        value_index =1;
        return value_index;
     }
     else
     /*
      if(temp_str[0] == '.')
     {
          if(temp_str[2]=='.')
          {
              strcpy(temp_str1,temp_str);
              if(*(temp_str1+1)=='\0' && atoi(temp_str1)==compute_field_list[0].list_field_id)
              {
				          if(temp_str[1] == '#')
				          {
				              for(i = 0; i < mib_curr_node->total_instance; i++)
				              {
				                   strcpy(temp_value[i], compute_field_list[0].field_val[i]);
				              }
				              value_index = mib_curr_node->total_instance;
				              return value_index;
				          }
				          else if(temp_str[1] == '*')
				          {
				              strcpy(temp_value[0], compute_field_list[0].field_val[mib_curr_node->curr_instance]);
				              value_index = 1;
				              return value_index;
				          }
		          }
		     }
     }
     else 
     */
     if((temp_ch = strstr(temp_str, "OID"))!=NULL)
     {
     	    if(isdigit(temp_ch[3]))
     	    {
          		strncpy(temp_str1, temp_ch, 4);
          		temp_ch += 4;
          }
          else
          {
              strcpy(temp_str1,"OID");
              temp_ch += 3;
           }
          for(i = 0; i < OID_index; i++)
          {
              if(strcmp(OID_name_str[i], temp_str1) == 0)
                 break;
          }
          if(i == OID_index)
          {
              printf("[Debug] Error: Can not find the oid define.\r\n"); 
              return 0;
          }
          compute_list_index= i; 
          
          if(temp_ch[0] == '.')
          {
          	  strcpy(temp_str1,temp_ch+3);
          	  if(*(temp_str1+1)=='\0' && atoi(temp_str1)==compute_field_list[compute_list_index].list_field_id)
          	  {
		              if(temp_ch[1] == '#')
		              {
		                  strcpy(temp_value, compute_field_list[compute_list_index].field_val[compute_curr_instance]);
		                  value_index = 1;
		                  return value_index;
		              }
		              else if(temp_ch[1] == '*')
		              {
		                  strcpy(temp_value, compute_field_list[compute_list_index].field_val[mib_curr_node->curr_instance+bind_index]); 
		                  value_index = 1;
		                  return value_index;
		              }
              }
           
          }
     }
     return 0;
}

void compute_field_WatchDog() 
{
	   char    temp_str[32];
	   if(list_once==1 && read_syntax_nor_list == 0)
	   {	    
	        if(list_flag == 0 && get_state==0)
	        {	        	   
	             if(compute_field_list_index < compute_list_num)
	             {        
	             	  field_compute_state=4;      
	                mib_curr_node = compute_field_list[compute_field_list_index].list_node; 	               	                
	                strcpy((char *)list_msg,"");
	        				//strcat(list_msg," -e ");
	        				strcat((char *)list_msg, "-f[");
	                sprintf(temp_str, "%d", compute_field_list[compute_field_list_index].list_field_id); 
	                strcat((char *)list_msg, temp_str); 
	                strcat((char *)list_msg, "] "); 
	        				strcat((char *)list_msg,"0");	        				
	        				strcat((char *)list_msg,"-");
	        				gcvt((double)(mib_curr_node->total_instance-1),3,temp_str);
	        				strcat((char *)list_msg,temp_str);	                
	                list_flag=1;
	                show_list=0;
	                debug_mmi_list();	                 
	             }
	        }    
	   }   
	   
}


void print_compute_field()
{
	  char out_text[256],temp_str[64];
	  BYTE    j,index,k,i=0;
	  if(list_once==1)
	  {
	   	     if(compute_field_list_index >= compute_list_num && compute_list_num>0)
	   	     {	   	     	   	   	  	   	     	       
	   	     	   	    for(index=0;index<compute_field_num;index++)
    								{
											    if(mib_curr_node==compute_field_arr[index].compute_node)
											    {
											    	   if(mib_curr_node->total_child_nodes==1)
											    	   {
											    	   	   strcpy(temp_str,"");
											    	   	   for(k=0;k<strlen(mib_curr_node->name)+9;k++)
											    	   	   {
											    	   	   	  strcat(temp_str," ");
											    	   	   }
											    	       sprintf(out_text, "%s|---(%d) %s %d",temp_str,compute_field_arr[index].field_id+1, compute_field_arr[index].name,mib_curr_node->curr_instance); 
											    	   }
											    	   else 											    	   
											    	        sprintf(out_text, "          |---(%d) %s %d",compute_field_arr[index].field_id+1, compute_field_arr[index].name,mib_curr_node->curr_instance); 
											    	   debug_color_out(BRIGHT,CYAN,BLACK,out_text);	 
											    	   
											    	   if(compute_field_arr[index].print_count==1)
											    	   {
											    	      sprintf(out_text, "     %s" ,compute_field_arr[index].print_name_str[0]);
								                  debug_color_out(BRIGHT,CYAN,BLACK,out_text); 
								                  debug_ascii_out_proc(" = ");
								                  if(strlen(compute_field_arr[index].print_val_str[0]))
								                  		sprintf(out_text, "%-8s",compute_field_arr[index].print_val_str[0]);
								                  else
								                      sprintf(out_text, "NULL    ");   
								                  debug_color_out(BRIGHT,BLUE,BLACK,out_text); 
								                  debug_ascii_out_proc("\r\n");
								               }
								               else
								               {
									               debug_ascii_out_proc("\r\n");
												    	   for(j=0;j<compute_field_arr[index].print_count;j++)
												    	   {
												    	   	  if(!i)
												    	   	      debug_ascii_out_proc("                   ");
												       	    sprintf(out_text, "%-9s" ,compute_field_arr[index].print_name_str[j]);
									                  debug_color_out(BRIGHT,CYAN,BLACK,out_text); 
									                  debug_ascii_out_proc("=");
									                  if(strlen(compute_field_arr[index].print_val_str[j]))
									                  		sprintf(out_text, "%-8s",compute_field_arr[index].print_val_str[j]);
									                  else
									                      sprintf(out_text, "NULL    ");  
									                  debug_color_out(BRIGHT,BLUE,BLACK,out_text); 
									                  debug_ascii_out_proc("  ");
									                  i++;
									                  if(i==3)
									                  {
									                    	debug_ascii_out_proc("\r\n");
									                    	i=0;
									                  }
									               } 
									               if(i!=0)
									                  debug_ascii_out_proc("\r\n");
									             }
								               if(print_line_index==line_index && line_index>0)
     	                                compute_curr_unit.print_count=0;
											    }  
    								}       							   	 
    								field_compute_state=5;   
    								print_once=0;	    	 	  								   	   	     	  
	   	     }
	  }
	  
}
/////////////////////////////////end of compute field/////////////////////////

////////////////////////////////Other///////////////////////////////
void debug_set_currenttime()
{
    u8		ii,jj;
    char	temp_timer[64]="\0";
    char	send_buf[64]="\0";
    time_t	tick;

    tick = time(NULL);

    sprintf(temp_timer,"\x1b[25;25Hcurrent time:%.24s",ctime(&tick));
    if (strlen(current_timer) == 0)	// current timer is empty
    {
        for (jj = 0;jj < strlen(temp_timer);jj ++)
            debug_send_char(temp_timer[jj]);
        strcpy(current_timer,temp_timer);
        return;
    }
    for (ii = 0;ii < strlen(temp_timer);ii ++)
    {
        if (temp_timer[ii] != current_timer[ii])
        {
            sprintf(send_buf,"\x1b[25;%dH%c",25+ii-8,temp_timer[ii]);
            for (jj = 0;jj < strlen(send_buf);jj ++)
                debug_send_char(send_buf[jj]);
        }
    }
    strcpy(current_timer,temp_timer);
}
/*
*
*       Fun:    del_space
*
*       Desc:    remove the spaces at the beginning of string
*
*       Ret:    string
*
*       File:   debug.c
*
*/
void del_space(char *str,BYTE type)
{
    int i,j,k;
    if(!strlen(str))
        return;
    if(type==0)       //del the ahead space
    {
	    for(i=0;i<strlen(str);i++)
	    {
	        if(str[i]!=' ')
	        {
	            for(j=0,k=i;str[k]!='\0';j++,k++)
	            {
	                str[j]=str[k];
	            }
	            str[j]='\0';
	            break;
	        }
	    }
   }
   else if(type==1)   //del the behind space
   {
   	  for(i=strlen(str)-1;i>0;i--)
	  {
	        if(str[i]!=' ')
	        {	          
	            break;
	        }
	  }
	  str[i+1]='\0';
   	
   }else if(type==2)  //del all the space
   {
   	for(i=0;i<strlen(str)-1;i++)
	  {
			   if(str[i]==' ')
			   {
			      for(j=i,k=i+1;str[k]!='\0';j++,k++)
			      {
			           str[j]=str[k];
			      }	   
			      str[j]='\0';
			   }
		}
		for(i=strlen(str)-1;i>0;i--)
	  {
	        if(str[i]!=' ')
	        {	          
	            break;
	        }
	  }
	  str[i+1]='\0';
    
   }


}


  
/*
*
*       Fun:    read_line
*
*       Desc:   Read a line from file fp, remove the spaces in the beginning and in the end of the line, store it in line_ptr 
*
*       Ret:    the length of line
*
*       File:   debug.c
*
*/
int read_line(FILE *fp, char *line_ptr)
{
    char 	*temp_ch_ptr;
    int 	index,	index2,	length;

    if(feof(fp))
        return 0;
    strcpy(line_ptr,	"");

    fgets(line_ptr,512,fp);
    temp_ch_ptr = strchr(line_ptr, 0xa);
    if (temp_ch_ptr != NULL )
        *temp_ch_ptr = '\0';


    length = strlen(line_ptr);
    for(index=0; index<length; index++)		//	remove all spaces in the beginning of the line
    {
        /*
        if(!isprint(line_ptr[index]))
            continue;
        else
        {
            if(line_ptr[index] == ' ' || line_ptr[index] == '\t')
                continue;
            else
                break;
        }
        */
         if(line_ptr[index] == ' ' || line_ptr[index] == '\t')
                continue;
            else
                break;
    }
    if(index!= 0)
    {
        for(index2=0; (index2 + index) <= length; index2++)
        {
            line_ptr[index2] = line_ptr[index2 + index];
        }
    }

    for( index=(length-1); index>=0; index--)	// cut off all spaces in the ending of the line
    {
    	/*
        if(!isprint(line_ptr[index]))
            line_ptr[index] = '\0'; 
        else 
        {
            if(line_ptr[index] == ' ' || line_ptr[index] == '\t')
                line_ptr[index] = '\0';
            else
                break;
        }
      */
       if(line_ptr[index] == ' ' || line_ptr[index] == '\t')
                line_ptr[index] = '\0';
            else
                break;
    }
    return strlen(line_ptr);
}	/*	end of read_line	*/


/*
*
*       Fun:    read_begin
*
*       Desc:   read the fp,  stop until meet the stop_ch
*
*       Ret:    the length of the stored string
*
*       File:   debug.c
* 
*/
int read_begin(FILE *fp, char *stop_ch)
{
    char 	ch;
    int 	index = 0,i=0;
   
	    while((ch=fgetc(fp)) != EOF)
	    {
	        if(stop_ch[i]!='\0')
	        {
	          if(ch==stop_ch[i])
	          {
	             i++;  
	             if(stop_ch[i]=='\0')
	             {
	             	index-=i-1; 	
	                break;
	             } 
	          }
	          else
	             i=0;
	        } 
	        else
	            break;
	        if(index == 3072)
	            break;
	    } 
   
    return index;
}	// end of read_begin



/*
*
*       Fun:    read_until
*
*       Desc:   read the fp, store the string to the data_ptr until meet the stop_ch
*
*       Ret:    the length of the stored string
*
*       File:   debug.c
*
*/
int read_until(FILE *fp, char *data_ptr, char *stop_ch)
{
    char 	ch;
    int 	index = 0,i=0;
    while((ch=fgetc(fp)) != EOF) 
    {
        if(stop_ch[i]!='\0')
        {
          if(ch==stop_ch[i])
          {
             i++;     
             if(stop_ch[i]=='\0')
             {
             	index-=i-1; 	
                break;
             } 
          }
          else
             i=0;
        } 
        else
            break;
        data_ptr[index++] = ch;
        if(index == 3072)
            break;
    }
    data_ptr[index]= '\0';
    return index;
}	// end of read_until



/*
*
*       Fun:    read_part
*
*       Desc:   read the fp, store the string to from the start_ch to syop_ch
*
*       Ret:    the length of the stored string
*
*       File:   debug.c
*
*/
int read_part(FILE *fp, char *data_ptr,char *start_ch, char *stop_ch)
{
    read_begin(fp,start_ch);
    read_until(fp,data_ptr,stop_ch);
    return strlen(data_ptr);
}	// end of read_until


/*
*
*       Fun:    pick_first_word(char *str_ptr, char *word_ptr)
*
*       Desc:   pick out the first word into word_ptr from the str_ptr
*
*       Ret:    the char pionter which point to the ending of the first word in the string
*
*       File:   debug.c
*
*/
char * pick_first_word(char *str_ptr, char *word_ptr)
{
    int 	index, 	index2, 	length;
    length = strlen(str_ptr);
    for(index = 0; index < length; index ++)
    {
        if(!isprint(str_ptr[index]))//find the first print char exclude spacebar
            continue;
        else
        {
            if(str_ptr[index] == ' ')
                continue;
            else
                break;
        }
    }
    for(index2=0; index <= length; index++, index2++)
    {
        if( (isprint(str_ptr[index])) && (str_ptr[index] !=  ' '))
            word_ptr[index2] = str_ptr[index];
        else
            break;
    }
    word_ptr[index2] = '\0';
    return &str_ptr[index];
}	// end of pick_first_word


/*
*
*       Fun:    take_str
*
*       Desc:   copy the data_str to the data_ptr if data_str is not NULL, or find two '"' in the file fp and take string between the two '"' into data_ptr
*
*       Ret:    successful: 1,	fail: 0
*
*       File:   debug.c
*
*/
int take_str(char *data_str, FILE *fp, char *data_ptr)
{
    char		tmp_ch= '"';
    int 		index;
    //printf("take_str begin:\r\n");
    if(strlen(data_str) != 0)
    {
        strcpy(data_ptr, data_str);
        return 1;
    }
    else
    {
        while(( !feof(fp) ) && (fgetc(fp) != '"'))
            ;
        if(feof(fp))
        {
            printf("[debug] Error: Can't find a [%c] as beginning of a text befor the End of file.\r\n",'"');
            return 0;
        }
        index = 0;
        while( !feof(fp) )
        {
            if( (tmp_ch = fgetc(fp)) != '"' )
                data_ptr[index++] = tmp_ch;
            else
            {
                data_ptr[index] = '\0';
                //printf("take_str end: %s.\r\n",data_ptr);
                return 1;
            }
        }
        printf("[debug] Error: Can't find a [%c] as ending of a text before the End of file.\r\n",'"');
        return 0;
    }
    //printf("take_str end: %s.\r\n",data_ptr);
}	// end of take_str;

void debug_color_out(int attr,int color,int bg,char *str)
{
    int len=strlen(str);
    char tmp_str[len+14];
    strcpy(tmp_str,str); 
    TxtColor(attr,color,bg,tmp_str);
    debug_ascii_out_proc(tmp_str);
}


void help_cmm(int index,char *str1,char *str2)
{
    char temp_str[3];
    char temp_str1[64],temp_str2[1024];
    strcpy(temp_str1,str1);
    strcpy(temp_str2,str2);
    debug_ascii_out_proc("Usage ");
    sprintf(temp_str,"%d : ",index);
    debug_ascii_out_proc(temp_str);
    debug_color_out(BRIGHT,YELLOW,BLACK,temp_str1);
    debug_ascii_out_proc("\r\n");
    debug_ascii_out_proc("          ");
    formatstr(10,temp_str2,0);
    debug_ascii_out_proc(temp_str2);
    debug_ascii_out_proc("\r\n");
}

void formatstr(int len,char *str,BYTE type)
{
    int       i, index_1=0, index_2=0;
    char      temp_str1[1024],temp_str2[1024];
    char      format_str[1024];
    char 	    space[80];
    char     *p;
    int       str_len;
    double    line_num;
    if(type)
    {
        for(i=0;i<strlen(str);i++)
        {
            if(str[i]!=' ' && str[i]!='\r' && str[i]!='\n')
                break;
        }
        p=str+i;
        strcpy(format_str,p);
        strcpy(str,format_str);
    }
    else
       p=str; 
    if(strlen(p)>1024)
    {
        debug_color_out(BRIGHT,RED,BLACK,"Error   : Remark is too long !\r\n");
        return ;
    }
    p=str;
    strcpy(format_str,"");
    while(*p!='\0')
    {
        i=0;
        while(i<strlen(str))    //copy a segment
        {
            if(*(p+i)!='\n')
                i++;
            else
                break;
        }
        if(i!=strlen(str))
        {
            strncpy(temp_str2,p,i);
            temp_str2[i]='\0';
            p=p+i+1; 
        }
        else
        {
            strcpy(temp_str2,str);
            *p='\0';
        }
        if(strlen(temp_str2)<70)
           str_len=1;
        else
        {
        	line_num=(double)strlen(temp_str2)/70.00;
        	if(line_num!=floor(line_num))
        	   str_len=floor(line_num)+1;
        	else
        	   str_len=line_num;
        }           
        for(i=0;i<=len-1;i++)
            space[i]=' ';
        space[i]='\0';
        strcpy(temp_str1,"");
        index_1=0;
        index_2=0;
        while(*(temp_str2+index_2)!='\0')
        {
            for(i=0;i<=(79-len);i++,index_1++,index_2++)
                if(*(temp_str2+index_2)!='\0')
                    *(temp_str1+index_1)=*(temp_str2+index_2);
                else
                    break;

            if(str_len!=0 && i==79-len+1)
            {
                temp_str1[index_1]='\0';
               if(*(temp_str2+index_2)!='\0') 
                {
                    strcat(temp_str1,space);
                    index_1+=(len);
                    str_len--;
                }
            }
        }
        temp_str1[index_1]='\0';
        strncpy(temp_str2,temp_str1,index_1);
        *(temp_str2+index_1)='\0';
        if(*(temp_str2+index_1-1)=='\n')
            *(temp_str2+index_1-1)='\0';
        if(*p!='\0'&& *(temp_str2+index_1-1)!='\n')
        {
            strcat(temp_str2,"\r\n");         
        }
        strcat(format_str,temp_str2);
    }
    strcpy(str,format_str); 
}

void switchchar(char *out_text,BYTE num)
{
    switch(num)
    {
    case 0x00:
        strcat(out_text, "0");
        break;
    case 0x10:
    case 0x01:
        strcat(out_text, "1");
        break;
    case 0x20:
    case 0x02:
        strcat(out_text, "2");
        break;
    case 0x30:
    case 0x03:
        strcat(out_text, "3");
        break;
    case 0x40:
    case 0x04:
        strcat(out_text, "4");
        break;
    case 0x50:
    case 0x05:
        strcat(out_text, "5");
        break;
    case 0x60:
    case 0x06:
        strcat(out_text, "6");
        break;
    case 0x70:
    case 0x07:
        strcat(out_text, "7");
        break;
    case 0x80:
    case 0x08:
        strcat(out_text, "8");
        break;
    case 0x90:
    case 0x09:
        strcat(out_text, "9");
        break;
    case 0xA0:
    case 0x0A:
        strcat(out_text, "A");
        break;
    case 0xB0:
    case 0x0B:
        strcat(out_text, "B");
        break;
    case 0xC0:
    case 0x0C:
        strcat(out_text, "C");
        break;
    case 0xD0:
    case 0x0D:
        strcat(out_text, "D");
        break;
    case 0xE0:
    case 0x0E:
        strcat(out_text, "E");
        break;
    case 0xF0:
    case 0x0F:
        strcat(out_text, "F");
        break;
    default:
        break;
    }

}


DWORD  my_pow(int x, int y)	// y \u4E2A x \u76F8\u4E58
{
    int 	index;
    DWORD	sum;
    for(index=0,sum=1;index<y;index++)
    {
        sum *= x;
    }
    return sum;
}

BYTE test_str(char *str,BYTE type)
{
	WORD i;
	if(type==0)  // is digit
	{
		for(i=0;i<strlen(str);i++)
		{
			if(!isdigit(str[i]))
			    break;			    
		}
		if(i==strlen(str))
		  return 1;
		else
		  return 0;
	}
	else if(type==1) //is hex
	{
		for(i=0;i<strlen(str);i++)
		{
			if(!isdigit(str[i]) && (str[i]<65 || (str[i]>70 && str[i]<97) || str[i]>102))
			    break;			    
		}
		if(i==strlen(str))
		  return 1;
		else
		  return 0;
    }
    else if(type==2)
    {
    	for(i=0;i<strlen(str);i++)
		{
			if(!isdigit(str[i]) &&  (str[i]<65 || (str[i]>90 && str[i]<97) || str[i]>122))
			    break;			    
		}
		if(i==strlen(str))
		  return 1;
		else
		  return 0;
    }
    else
     return 0;
}

void strpcat(char *dest_str,DWORD *dest_p,const char *src_str)
{
	while((*(dest_str+(*dest_p)++)=*src_str++));
	(*dest_p)--;
}

void pick_name(char *str_ptr, char *word_ptr,char *temp_ch)
{
	int start_index,end_index,i;
	char temp_str[512],*ch;
	strcpy(temp_str,str_ptr);
	start_index=end_index=i=0;
	if(temp_ch!=NULL)
	{
		for(start_index=0;start_index<strlen(temp_str);start_index++)
		      if(*(temp_str+start_index)!=' ' && *(temp_str+start_index)!='\t')
		            break;	
		strncpy(word_ptr,temp_str+start_index,i);
		ch=strstr(word_ptr,temp_ch);
		*ch='\0';
		for(end_index=strlen(word_ptr)-1;end_index>0;end_index--)
		      if(*(word_ptr+end_index)!=' ' && *(word_ptr+end_index)!='\t')
		            break;
		*(word_ptr+end_index+1)='\0';			     
	}
}


int get_my_address (struct in_addr *addr)
{
	struct ifreq req;
	int sock;
	
	sock = socket(AF_INET, SOCK_DGRAM, 0);
	strncpy (req.ifr_name, "eth0", IFNAMSIZ);
	
	if ( ioctl(sock, SIOCGIFADDR, &req) < 0 )
	{
	printf("failed to ioctl: %s\n", strerror (errno));
	return 0;
	}
	
	memcpy (addr, &((struct sockaddr_in *) &req.ifr_addr)->sin_addr, sizeof (struct in_addr));
	return 1;
}
/*@end@*/ 

////////////////////////////End of Other///////////////////////////
