下面是我用于解析令牌的一段代码。
底部附近有一条>>>>
不再需要的行,但是如果我将其注释掉,则该cmd_parse_value_lookup()
功能将失败。如果我把它留在里面,代码运行正常。谁能告诉我为什么,并解释发生了什么?
void cmd_parse(void)
{
cmd_parse_value=0;
int cmd_parse_counter = 1;
char *cmd_parse_pointer;
cmd_parse_pointer = strtok(cmd_buffer_in, " ");
if (cmd_parse_pointer!=NULL)
{
cmd_parse_value_lookup(cmd_parse_pointer);
}
while (cmd_parse_pointer != NULL)
{
cmd_parse_counter++;
cmd_parse_pointer = strtok(NULL, " ");
if (cmd_parse_pointer!=NULL)
{
>>>>cmd_buffer_sprintf_return = sprintf(cmd_buffer_sprintf,"%i: %s\r\n", cmd_parse_counter, cmd_parse_pointer); //WHY DO I NEED THIS LINE
cmd_parse_value_lookup(cmd_parse_pointer);
}
}
}
void cmd_parse_value_lookup(char *cmd_command)
{
if (strcmp(cmd_command,"show")==0)
{
cmd_parse_value |= 1;
}
else if (strcmp(cmd_command,"get")==0)
{
cmd_parse_value |= 1;
}
else if (strcmp(cmd_command,"set")==0)
{
cmd_parse_value |= 2;
}
else if (strcmp(cmd_command,"system")==0)
{
cmd_parse_value |= 4;
}
}
编辑:这是完整的代码:
/** C O M M A N D ************************************************************/
#include "generic.h"
#include <stdio.h>
#include <string.h>
void btm_out_character (char character);
void btm_out_string(char *string);
void cmd_parse(void);
char cmd_buffer_in[81]="\0 ";
int cmd_buffer_in_position=0;
unsigned long long cmd_parse_value=0;
char cmd_buffer_sprintf[81];
int cmd_buffer_sprintf_return;
void cmd_parse_value_lookup(char *cmd_command);
void cmd_buffer_in_add(int character);
void cmd_init(void)
{
}
void cmd_cls(void)
{
if (dbg_mode==1){btm_out_string("\033[2J\033[1;33;40m\033[H\r\n");}
if (dbg_mode==1){btm_out_string("================================================================================");}
if (dbg_mode==1){btm_out_string("\033[24;0H================================================================================");}
if (dbg_mode==1){btm_out_string("\033[1;36;40m\033[8;66H DEBUG MODE ");}
if (dbg_mode==1){btm_out_string("\033[24;7H ***** ******** ********** Engineering Limited, All Rights Reserved ");}
if (dbg_mode==1){btm_out_string("\033[10;0H");}
}
void cmd_prompt(void)
{
cmd_buffer_in_position = 0; // Clear buffer position
cmd_buffer_in[0]=0; // Clear buffer
if (dbg_mode==1){btm_out_string("\r\n\033[1;37;40m***:> ");}
}
void cmd_buffer_in_add(int character)
{
switch (character)
{
case 8:
if (cmd_buffer_in_position>0)
{
if (dbg_mode==1){btm_out_string("\b \b");}
cmd_buffer_in[(int)cmd_buffer_in_position-1] = (char)character;
cmd_buffer_in[(int)cmd_buffer_in_position] = 0;
cmd_buffer_in_position--;
}
else
{
if (dbg_mode==1){btm_out_character(7);} //BELL alert (too long a string)
cmd_buffer_in[0] = 0;
cmd_buffer_in_position = 0;
}
break;
case 13:
cmd_parse();
cmd_buffer_sprintf_return = sprintf(cmd_buffer_sprintf,"\r\nparse value=%llu\r\n", cmd_parse_value);
if (dbg_mode==1){btm_out_string(cmd_buffer_sprintf);}
cmd_prompt();
break;
default:
if (cmd_buffer_in_position<73)
{
if (dbg_mode==1){btm_out_character((char)character);} //Echo character to host
cmd_buffer_in[(int)cmd_buffer_in_position] = (char)character;
cmd_buffer_in[(int)cmd_buffer_in_position+1] = 0;
cmd_buffer_in_position++;
}
else
{
if (dbg_mode==1){btm_out_character(7);} //BELL alert (too long a string)
}
}//switch
}//cmd_buffer_in_add
void cmd_parse(void)
{
cmd_parse_value=0;
/* this was code for testing */
//cmd_buffer_sprintf_return = sprintf(cmd_buffer_sprintf,"\r\nClone %s",cmd_buffer_in);
//cmd_buffer_sprintf_return = sprintf(cmd_buffer_sprintf,"Returnval='%i''%i''%i'",cmd_buffer_in[(int)0],cmd_buffer_in[(int)1],cmd_buffer_in[(int)2]);
//if (dbg_mode==1){btm_out_string(cmd_buffer_sprintf);}
int cmd_parse_counter = 1;
char *cmd_parse_pointer;
cmd_parse_pointer = strtok(cmd_buffer_in, " ");
if (cmd_parse_pointer!=NULL)
{
cmd_parse_value_lookup(cmd_parse_pointer);
}
while (cmd_parse_pointer != NULL)
{
cmd_parse_counter++;
cmd_parse_pointer = strtok(NULL, " ");
if (cmd_parse_pointer!=NULL)
{
cmd_buffer_sprintf_return = sprintf(cmd_buffer_sprintf,"%i: %s\r\n", cmd_parse_counter, cmd_parse_pointer); //WHY DO I NEED THIS LINE
//if (dbg_mode==1){btm_out_string(cmd_buffer_sprintf);}
cmd_parse_value_lookup(cmd_parse_pointer);
}
}
}
void cmd_parse_value_lookup(char *cmd_command)
{
if (strcmp(cmd_command,"show")==0)
{
cmd_parse_value |= 1;
}
else if (strcmp(cmd_command,"get")==0)
{
cmd_parse_value |= 1;
}
else if (strcmp(cmd_command,"set")==0)
{
cmd_parse_value |= 2;
}
else if (strcmp(cmd_command,"system")==0)
{
cmd_parse_value |= 4;
}
else if (strcmp(cmd_command,"sys")==0)
{
cmd_parse_value |= 4;
}
else if (strcmp(cmd_command,"adc")==0)
{
cmd_parse_value |= 8;
}
else if (strcmp(cmd_command,"a2d")==0)
{
cmd_parse_value |= 8;
}
else if (strcmp(cmd_command,"channel1")==0)//
{
cmd_parse_value |= 16;
}
else if (strcmp(cmd_command,"ch1")==0)
{
cmd_parse_value |= 16;
}
}