📄 clilib.c
字号:
BOOL STTST_AssignFloat(const char *const token_p, double value, BOOL constant)
{
symtab_t *symbol_p;
BOOL IsBad = FALSE;
if (strlen(token_p) != 0)
{
symbol_p = look_for(token_p, CLI_FLT_SYMBOL);
if ((symbol_p == (symtab_t*) NULL) && (token_p[0] != '\0'))
{
symbol_p = insert_symbol(token_p, CLI_FLT_SYMBOL);
if (symbol_p == (symtab_t*) NULL)
{
IsBad = TRUE;
}
else
{
symbol_p->fixed = constant;
if (symbol_p->fixed)
{
symbol_p->info_p = "floating point constant";
}
else
{
symbol_p->info_p = "floating point variable";
}
symbol_p->value.flt_val = value;
}
}
else
{
if (symbol_p->fixed)
{
IsBad = TRUE;
}
else
{
symbol_p->value.flt_val = value;
}
}
}
return(IsBad);
}
/* ========================================================================
creates a floating point symbol table entry
=========================================================================== */
static BOOL create_float(const char * const token_p, double value, BOOL constant)
{
symtab_t *symbol_p;
BOOL IsBad = FALSE;
if (strlen(token_p) != 0)
{
symbol_p = insert_symbol(token_p, CLI_FLT_SYMBOL);
if (symbol_p == (symtab_t*) NULL)
{
IsBad = TRUE;
}
else
{
symbol_p->fixed = constant;
if (symbol_p->fixed)
{
symbol_p->info_p = "floating point constant";
}
else
{
symbol_p->info_p = "floating point variable";
}
symbol_p->value.flt_val = value;
}
}
return(IsBad);
}
/* ========================================================================
creates or updates a string symbol table entry
=========================================================================== */
/*BOOL assign_string(char *token_p, char *value, BOOL constant)*/
BOOL STTST_AssignString(const char *const token_p, const char *const value_p, BOOL constant)
{
symtab_t *symbol_p;
BOOL IsBad = FALSE;
if (strlen(token_p) != 0)
{
symbol_p = look_for(token_p, CLI_STR_SYMBOL);
if (symbol_p == (symtab_t*) NULL)
{
symbol_p = insert_symbol(token_p, CLI_STR_SYMBOL);
if (symbol_p == (symtab_t*) NULL)
{
IsBad = TRUE;
}
else
{
symbol_p->fixed = constant;
if (symbol_p->fixed)
{
symbol_p->info_p = "string constant";
}
else
{
symbol_p->info_p = "string variable";
}
symbol_p->value.str_val = memory_allocate(
(partition_t *)sttst_InitParams.CPUPartition_p, strlen(value_p)+1);
if (symbol_p->value.str_val == NULL)
{
IsBad = TRUE;
}
}
}
else
{
if (symbol_p->fixed)
{
IsBad = TRUE;
}
else
{
if (strlen(value_p) > strlen(symbol_p->value.str_val))
{
memory_deallocate(sttst_InitParams.CPUPartition_p, symbol_p->value.str_val);
symbol_p->value.str_val = memory_allocate(
(partition_t *)sttst_InitParams.CPUPartition_p, strlen(value_p)+1);
if (symbol_p->value.str_val == NULL)
{
IsBad = TRUE;
}
}
}
}
if (!IsBad)
{
strcpy(symbol_p->value.str_val, value_p);
}
}
return(IsBad);
}
/* ========================================================================
creates a string symbol table entry
=========================================================================== */
static BOOL create_string(const char * const token_p, char *value, BOOL constant)
{
symtab_t *symbol_p;
BOOL IsBad = FALSE;
if (strlen(token_p) != 0)
{
symbol_p = insert_symbol(token_p, CLI_STR_SYMBOL);
if (symbol_p == (symtab_t*) NULL)
{
IsBad = TRUE;
}
else
{
symbol_p->fixed = constant;
if (symbol_p->fixed)
{
symbol_p->info_p = "string constant";
}
else
{
symbol_p->info_p = "string variable";
}
symbol_p->value.str_val = memory_allocate(
(partition_t *)sttst_InitParams.CPUPartition_p, strlen(value)+1);
if (symbol_p->value.str_val == NULL)
{
IsBad = TRUE;
}
else
{
strcpy(symbol_p->value.str_val, value);
}
}
}
return(IsBad);
}
/* ========================================================================
delete an existing symbol (macro, variable, but not constant and command)
=========================================================================== */
BOOL STTST_DeleteSymbol(const char *const token_p)
{
BOOL IsBad = FALSE;
symtab_t *symbol_p;
symbol_p = look_for(token_p, CLI_ANY_SYMBOL);
if (symbol_p == (symtab_t*)NULL)
{
IsBad = TRUE; /* unknown symbol */
}
else if (symbol_p->fixed || (symbol_p->type == CLI_COM_SYMBOL))
{
IsBad = TRUE; /* fixed symbol or command */
}
else
{
IsBad = delete_symbol(token_p);
/* if IsBad : symbol out of current scope */
}
return(IsBad);
}
/* ========================================================================
establishes the existence of a command procedure
=========================================================================== */
BOOL STTST_RegisterCommand(const char *const token_p,
BOOL (*action)(STTST_Parse_t*, char*),
const char *const help_p)
{
symtab_t *symbol_p;
BOOL IsBad = FALSE;
symbol_p = look_for(token_p, (CLI_COM_SYMBOL | CLI_MAC_SYMBOL));
if (symbol_p != (symtab_t*) NULL)
{
IsBad = TRUE;
sttst_Print("Name clash when registering command \"%s\"\n",token_p);
}
else
{
symbol_p = insert_symbol(token_p, CLI_COM_SYMBOL);
if (symbol_p == (symtab_t*) NULL)
{
IsBad = TRUE;
}
else
{
symbol_p->fixed = TRUE;
symbol_p->info_p = help_p;
symbol_p->value.com_val = action;
}
}
return(IsBad);
}
/* ========================================================================
attempts a series of operations to extract an integer value from a token
=========================================================================== */
BOOL STTST_EvaluateInteger(const char *const token_p, S32 *value_p, S16 default_base)
{
STTST_Parse_t *Parse_p;
symtab_t *symbol_p;
BOOL IsValid, IsBad = FALSE;
if (token_p[0] == '\0')
{
IsBad = TRUE;
}
else if (token_p[0] == '-')
{ /* unary negative */
IsBad = STTST_EvaluateInteger(token_p+1, value_p, default_base);
if(!IsBad)
{
*value_p = -(*value_p);
}
}
else if (token_p[0] == '~')
{ /* unary bitflip */
IsBad = STTST_EvaluateInteger(token_p+1, value_p, default_base);
if (!IsBad)
{
*value_p = ~(*value_p);
}
}
else if (token_p[0] == '!')
{ /* unary NOT */
IsBad = STTST_EvaluateInteger(token_p+1, value_p, default_base);
if (!IsBad)
{
*value_p = !(*value_p);
}
}
else
{
/* First look for a simple number, then try a symbol reference. If this
all fails, the value may be due to an expression, so try and evaluate it*/
*value_p = conv_int(token_p, default_base, &IsValid);
if (!IsValid)
{
symbol_p = look_for(token_p, CLI_INT_SYMBOL);
if (symbol_p == (symtab_t*) NULL)
{
Parse_p = NewParseTableSlot(strlen(token_p)+1);
init_pars(Parse_p, token_p);
IsBad = evaluate_integer_expr(Parse_p, value_p, default_base);
FreeParseTableSlot();
}
else
{
*value_p = symbol_p->value.int_val;
}
}
}
return(IsBad);
}
/* ========================================================================
generalised recursive evaluate of a possibly bracketed integer expression.
No leading spaces are allowed in the passed string, and the parsing
structure is assumed to be initialised but not yet used
=========================================================================== */
static BOOL evaluate_integer_expr(STTST_Parse_t *Parse_p, S32 *value_p,
S16 default_base)
{
BOOL IsBad = FALSE;
S16 brackets = 0;
S32 value1, value2;
S16 index;
char *sub_expr;
char operation;
/* pair up a leading bracket */
if (Parse_p->line_p[0] == '(')
{
brackets = 1;
index = 1;
while((Parse_p->line_p[index] != '\0') && (brackets > 0))
{
if (Parse_p->line_p[index] == ')') brackets--;
if (Parse_p->line_p[index] == '(') brackets++;
index++;
}
if (brackets != 0)
{
IsBad = TRUE;
}
else
{
/* copy substring without enclosing brackets and evaluate it.
if this is ok, then update parsing start position */
sub_expr = NewTokenTableSlot(index);
strncpy(sub_expr,&(Parse_p->line_p[1]), index-2);
sub_expr[index-2] = '\0';
IsBad = STTST_EvaluateInteger(sub_expr, &value1, default_base);
FreeTokenTableSlot(); /* sub_expr */
if (!IsBad)
{
Parse_p->par_pos = index-1;
}
}
}
if (!IsBad)
{
/* look for a token and check for significance */
get_tok(Parse_p, ArithmeticOpsInt);
if (Parse_p->tok_del == '\0')
{ /* no operator seen*/
if (Parse_p->par_sta > 0) /* bracket removal code used */
{
*value_p = value1;
}
else
{
IsBad = TRUE; /* not a valid expression! */
}
}
else
{
/* have found an operator. If we stripped brackets in the first
half of this code, then the interesting part of the line is
now after the operator. If we did not, then evaluate both
sides of operator */
operation = Parse_p->tok_del;
if (Parse_p->par_sta == 0)
{
IsBad = STTST_EvaluateI
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -