#include "cmd.h"
#include "cmd_parse.h"
cmd_tok_t* cmd_tok_create( char *s, char *e, int sz, int type )
{
cmd_tok_t *ret = NULL;
ret = malloc( sizeof(cmd_tok_t) );
if ( ret == NULL )
return ret;
memset( ret, 0, sizeof(cmd_tok_t) );
ret->s = s;
ret->e = e;
ret->sz = sz;
ret->type = type;
ret->next = NULL; //no need, but sec programming says we need it
return ret;
}
int cmd_tok_add( cmd_tok_t *tok, cmd_tok_t *next )
{
int ret = 0;
if ( tok == NULL )
{
return -1;
}
if ( next == NULL )
{
return -1;
}
if ( tok->next != NULL )
{
printf("next token allready set\n");
return -1;
}
tok->next = next;
return ret;
}
int cmd_tok_print( cmd_tok_t *tok )
{
int ret = 0;
printf("TOK %p\n",tok);
if (tok)
{
printf("S:%p E:%p SZ:%d \n", tok->s, tok->e, tok->sz);
printf("TYPE: ");
switch (tok->type)
{
case CMDT_NONE:
printf("NONE");
break;
case CMDT_INT:
printf("INT");
break;
case CMDT_HEX:
printf("HEX");
break;
case CMDT_BIN:
printf("BIN");
break;
case CMDT_STR:
printf("STR");
break;
case CMDT_WORD:
printf("WORD");
break;
case CMDT_SP:
printf("SP");
break;
default:
printf("UNKNOWN");
}
printf("\n");
printf("NEXT: %p\n", tok->next );
}
return ret;
}
void cmd_tok_destroy( cmd_tok_t *tok )
{
cmd_tok_t *t=NULL, *n=NULL;
if (tok == NULL)
{
return;
}
if ( tok->next == NULL )
{
free( tok );
return;
}
t = tok;
n = t;
while ( n != NULL)
{
n = t->next;
t->next = NULL;
free(t);
t = n;
}
}
int cmd_tok_count( cmd_tok_t *tok )
{
int ret = 0;
cmd_tok_t *iter = NULL;
if (tok == NULL)
return 0;
iter = tok;
while( iter != NULL )
{
ret += 1;
iter = iter->next;
}
return ret;
}
cmd_arg_t* cmd_arg_create( cmd_tok_t *tok )
{
cmd_arg_t *ret = NULL;
int argc = -1;
cmd_tok_t *iter = NULL;
int i = 0;
ret = malloc( sizeof(cmd_arg_t) );
if ( ret == NULL )
{
return NULL;
}
memset( ret, 0, sizeof( cmd_arg_t ));
//get number of arguments in command line
argc = cmd_tok_count( tok );
ret->argc = argc;
//alloc mem for argument string values
ret->argv = malloc( sizeof(int*)*argc );
if ( ret->argv == NULL )
{
//printf("ERR:err_malloc_argv\n");
goto err_malloc_argv;
}
memset( ret->argv, 0, sizeof(int*)*argc );
//alloc mem for argument type values
ret->type = malloc( sizeof(int)*argc );
if ( ret->type == NULL )
{
//printf("ERR:err_malloc_type\n");
goto err_malloc_type;
}
memset( ret->type, 0, sizeof(int)*argc );
//create for each cmd token string and set type
iter = tok;
for (i=0; i<argc; i++,iter=iter->next)
{
ret->argv[i] = malloc( iter->sz+