cmdparser.c File Reference

#include "argvize.c"
#include "cmdparser.h"
#include <myconfig.h>

Include dependency graph for cmdparser.c:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define LINESZ   200

Functions

char * _do_cmd (char *p_in, int need_end)
void add_cmd (struct mycmd *mycmd)
int cmd_expr (int argc, char **argv)
static int cmd_setconfig (int argc, char **argv)
static char * config_complete (int argc, char **argv, char *text, int start, int end)
char * config_generator (char *text, int state) const
void do_cmd (const char *p)
char * dupstr (int)
static char * expand (char *)
static char * expand1 (char *)
static char * expr (char *cmd)
static int help (int argc, char **argv)
static void init_cmd (void)
static void init_config (void)
static void init_symbol (void)
 mycmd_init (expr, cmd_expr,"expr","expr")
 mycmd_init (h, help,"h","show this help")
 mycmd_init1 (setconfig, cmd_setconfig,"setconfig [configname] [val]","setconfig for command", config_complete)
 mysymbol_export (add_cmd)

Variables

struct mycmd __mycmd_begin
struct mycmd __mycmd_end
struct myconfig __myconfig_begin
struct myconfig __myconfig_end
struct mysymbol __mysymbol_begin
struct mysymbol __mysymbol_end
struct mycmd * mycmd_head
struct mycmd * mycmd_tail
struct myconfig * myconfig_head
struct myconfig * myconfig_tail
struct mysymbol * mysymbol_head
struct mysymbol * mysymbol_tail
static int showcmd = 0


Define Documentation

#define LINESZ   200

Definition at line 2 of file cmdparser.c.

Referenced by expand(), and expand1().


Function Documentation

char * _do_cmd ( char *  p_in,
int  need_end 
)

Definition at line 240 of file cmdparser.c.

References argvize(), expand(), expand1(), mycmd.func, MAX_AC, mycmd.name, mycmd.next, mycmd.note, showcmd, and mycmd.usage.

Referenced by cmd_source(), do_cmd(), and main().

00241 {
00242         char    *av[MAX_AC];    /* argument holder */
00243         int32_t ac;             /* # of arguments */
00244         int      nc, j, c;
00245         struct mycmd *pcmd;
00246         char *p,*p_cmd;
00247 
00248 cmd_loop:
00249         p_cmd=p_in;
00250         for (; *p_in;) {
00251                 c = *p_in;
00252                 if(c == '\'' || c == '"') {
00253                         p_in++;
00254                         while (*p_in && *p_in != c) {
00255                                 ++p_in;
00256                         }
00257                         if(*p_in) {
00258                                 p_in++;
00259                         }
00260                         else {
00261                                 printf("unbalanced %c\n", c);
00262                                 return p;
00263                         }
00264                 }
00265                 else if((c == ';')||(c == '\n')) {
00266                         *p_in++ = 0;
00267                         goto run_a_cmd;
00268                 }
00269                 else {
00270                         p_in++;
00271                 }
00272         }
00273         if(need_end)return p_cmd;
00274 run_a_cmd:
00275 
00276         /* Expand command substituting $vars. Breakup into separate cmds */
00277 
00278         if(!(p = expand (p_cmd))) {
00279                 return p_cmd;
00280         }
00281 
00282         if(!(p = expand1 (p))) {
00283                 return p_cmd;
00284         }
00285 
00286         nc = 0;
00287 
00288         /*
00289          *  Lookup command in command list and dispatch.
00290          */
00291         do{
00292                 int stat = -1;
00293                 if(showcmd)printf("#%s\n",p);
00294                 ac = argvize (av, p);
00295                 if(ac == 0|| av[0][0]=='#') {
00296                         break;
00297                 }
00298 
00299 
00300    for(pcmd=mycmd_head;pcmd;pcmd=pcmd->next)
00301    if(!strcmp(av[0],pcmd->name))break;
00302    if(pcmd)
00303         stat = pcmd->func(ac, av);
00304    else 
00305    {printf("syntax error %d\n",stat);stat=0;}
00306 
00307                 if(stat < 0) {
00308                 printf("usage:%-10s:%-30s:%s\n",pcmd->name,pcmd->usage,pcmd->note);
00309                 }
00310 
00311                 if(stat != 0) {
00312                         return p_in;    /* skip commands after ';' */
00313                 }
00314         }while(0);
00315 
00316 if(*p_in)goto cmd_loop;
00317 return p_in;
00318 }

Here is the call graph for this function:

void add_cmd ( struct mycmd *  mycmd  ) 

Definition at line 49 of file cmdparser.c.

References mycmd.next.

Referenced by initcmd().

00050 {
00051  printf("add cmd %x\n",mycmd);
00052  mycmd_tail->next = mycmd;
00053  mycmd_tail = mycmd;
00054 }

int cmd_expr ( int  argc,
char **  argv 
)

Definition at line 536 of file cmdparser.c.

References expr().

00537 {
00538         if(argc!=2)return -1;
00539         printf("%s\n",expr(argv[1]));
00540         return 0;
00541 }

Here is the call graph for this function:

static int cmd_setconfig ( int  argc,
char **  argv 
) [static]

Definition at line 84 of file cmdparser.c.

References myconfig.config, CONFIG_I, myconfig.name, myconfig.next, myconfig.note, and myconfig.type.

00085 {
00086 struct myconfig *pconfig;
00087 char str[20];
00088 int i;
00089 if(argc==1)
00090 {
00091    for(pconfig=myconfig_head,i=0;pconfig;pconfig=pconfig->next,i++)
00092         {
00093         if(pconfig->type == CONFIG_I)
00094         printf("%-30s: 0x%08x:%-30s\n",pconfig->name,*(unsigned int *)pconfig->config,pconfig->note);
00095         else
00096         printf("%-30s: %30s:%-30s\n",pconfig->name,*(char **)pconfig->config,pconfig->note);
00097         if(i%16==15){printf("press enter to continue...\n");read(0,str,20);}
00098         }
00099 }
00100 else if(argc==2)
00101 {
00102  for(pconfig=myconfig_head;pconfig;pconfig=pconfig->next)
00103         if(!strcmp(pconfig->name,argv[1]))
00104     {
00105         if(pconfig->type == CONFIG_I)
00106         printf("%-30s: %08x:%-30s\n",pconfig->name,*(unsigned int *)pconfig->config,pconfig->note);
00107         else
00108         printf("%-30s:%30s:%s\n",pconfig->name,*(char **)pconfig->config,pconfig->note);
00109         break;
00110         }
00111  if(!pconfig)printf("can not find config %s\n",argv[1]);
00112 }
00113 else if(argc == 3)
00114         {
00115  for(pconfig=myconfig_head;pconfig;pconfig=pconfig->next)
00116         if(!strcmp(pconfig->name,argv[1]))
00117     {
00118         if(pconfig->type == CONFIG_I)
00119          *(unsigned int *)pconfig->config = strtoul(argv[2],0,0);
00120         else
00121         {
00122          *(char **)pconfig->config = realloc(*(char **)pconfig->config,strlen(argv[2])+1);
00123          strcpy(*(char **)pconfig->config,argv[2]);
00124         }
00125         break;
00126         }
00127         }
00128         
00129 else return -1;
00130 
00131 return 0;
00132 }

static char* config_complete ( int  argc,
char **  argv,
char *  text,
int  start,
int  end 
) [static]

Definition at line 175 of file cmdparser.c.

References myconfig.complete, config_generator(), myconfig.name, and myconfig.next.

00176 {
00177 if(argc == 1)
00178 return rl_completion_matches (text, config_generator);
00179 if(argc == 2)
00180  {
00181         struct myconfig *pconfig;
00182    for(pconfig=myconfig_head;pconfig && strcmp(argv[1],pconfig->name);pconfig=pconfig->next);
00183    if(pconfig && pconfig->complete)
00184     return rl_completion_matches(text,pconfig->complete);
00185    else return NULL;
00186  }
00187 else return NULL;
00188 }

Here is the call graph for this function:

char* config_generator ( char *  text,
int  state 
) const

Definition at line 140 of file cmdparser.c.

References dupstr(), myconfig.name, and myconfig.next.

Referenced by config_complete().

00143 {
00144   static int list_index, len;
00145   char *name;
00146  static struct myconfig *pconfig;
00147 
00148   /* If this is a new word to complete, initialize now.  This
00149      includes saving the length of TEXT for efficiency, and
00150      initializing the index variable to 0. */
00151   if (!state)
00152     {
00153           pconfig = myconfig_head;
00154       len = strlen (text);
00155     }
00156 
00157   /* Return the next name which partially matches from the
00158      command list. */
00159 
00160 
00161 
00162   while (pconfig && (name = pconfig->name))
00163     {
00164           pconfig = pconfig->next;
00165 
00166       if (strncmp (name, text, len) == 0)
00167         return (dupstr(name));
00168     }
00169 
00170   /* If no names matched, then return NULL. */
00171   return ((char *)NULL);
00172 
00173 }

Here is the call graph for this function:

void do_cmd ( const char *  p  ) 

Definition at line 232 of file cmdparser.c.

References _do_cmd().

Referenced by cmd_putelf(), fGDBRDP_CommandInterpreter(), fGDBRDP_Handler(), fJTAGSOCK_GetConnection(), loop(), and main().

00233 {
00234 char *cmd=malloc(strlen(p)+1);
00235 strcpy(cmd,p);
00236 _do_cmd(cmd,0);
00237 free(cmd);
00238 }

Here is the call graph for this function:

char* dupstr ( int   ) 

Definition at line 3 of file readline.c.

Referenced by command_generator(), config_generator(), and flash_complete().

00005 {
00006   char *r;
00007 
00008   r = xmalloc (strlen (s) + 1);
00009   strcpy (r, s);
00010   return (r);
00011 }

static char * expand ( char *  cmdline  )  [static]

Definition at line 328 of file cmdparser.c.

References LINESZ.

Referenced by _do_cmd().

00330 {
00331         char *ip, *op, *v;
00332         char var[256];
00333         static char expline[LINESZ + 8];
00334 
00335         if(!strchr (cmdline, '$')) {
00336                 return cmdline;
00337         }
00338 
00339         ip = cmdline;
00340         op = expline;
00341         while (*ip) {
00342                 if(op >= &expline[sizeof(expline) - 1]) {
00343                         printf ("Line too long after expansion\n");
00344                         return (0);
00345                 }
00346 
00347                 if(*ip != '$') {
00348                         *op++ = *ip++;
00349                         continue;
00350                 }
00351 
00352                 ip++;
00353                 if(*ip == '$') {
00354                         *op++ = '$';
00355                         ip++;
00356                         continue;
00357                 }
00358 
00359                 /* get variable name */
00360                 v = var;
00361                 if(*ip == '{') {
00362                         /* allow ${xxx} */
00363                         ip++;
00364                         while (*ip && *ip != '}') {
00365                                 *v++ = *ip++;
00366                         }
00367                         if(*ip && *ip != '}') {
00368                                 printf ("Variable syntax\n");
00369                                 return (0);
00370                         }
00371                         ip++;
00372                 } else {
00373                         /* look for $[A-Za-z0-9]* */
00374                         while (isalpha(*ip) || isdigit(*ip)) {
00375                                 *v++ = *ip++;
00376                         }
00377                 }
00378 
00379                 *v = 0;
00380                 if(!(v = getenv (var))) {
00381                         printf ("'%s': undefined\n", var);
00382                         return (0);
00383                 }
00384 
00385                 if(op + strlen(v) >= &expline[sizeof(expline) - 1]) {
00386                         printf ("Line expansion ovf.\n");
00387                         return (0);
00388                 }
00389 
00390                 while (*v) {
00391                         *op++ = *v++;
00392                 }
00393         }
00394         *op = '\0';
00395         return (expline);
00396 }

static char * expand1 ( char *  cmdline  )  [static]

Definition at line 476 of file cmdparser.c.

References expr(), and LINESZ.

Referenced by _do_cmd().

00477 {
00478         char *ip, *op, *v;
00479         char var[256];
00480         static char expline[LINESZ + 8];
00481 
00482         if(!strchr (cmdline, '`')) {
00483                 return cmdline;
00484         }
00485 
00486         ip = cmdline;
00487         op = expline;
00488         while (*ip) {
00489                 if(op >= &expline[sizeof(expline) - 1]) {
00490                         printf ("Line too long after expansion\n");
00491                         return (0);
00492                 }
00493 
00494                 if(*ip != '`') {
00495                         *op++ = *ip++;
00496                         continue;
00497                 }
00498 
00499                 ip++;
00500                 if(*ip == '`') {
00501                         *op++ = '`';
00502                         ip++;
00503                         continue;
00504                 }
00505 
00506                 /* get variable name */
00507                 v = var;
00508                         while (*ip && *ip != '`') {
00509                                 *v++ = *ip++;
00510                         }
00511                         if(*ip && *ip != '`') {
00512                                 printf ("Variable syntax\n");
00513                                 return (0);
00514                         }
00515                         ip++;
00516 
00517                 *v = 0;
00518                 if(!(v = expr (var))) {
00519                         printf ("'%s': undefined\n", var);
00520                         return (0);
00521                 }
00522 
00523                 if(op + strlen(v) >= &expline[sizeof(expline) - 1]) {
00524                         printf ("Line expansion ovf.\n");
00525                         return (0);
00526                 }
00527 
00528                 while (*v) {
00529                         *op++ = *v++;
00530                 }
00531         }
00532         *op = '\0';
00533         return (expline);
00534 }

Here is the call graph for this function:

static char* expr ( char *  cmd  )  [static]

Definition at line 398 of file cmdparser.c.

Referenced by cmd_expr(), and expand1().

00399 {
00400 char *token;
00401 unsigned int op='+';
00402 unsigned long result=0;
00403 static char cmd1[0x100];
00404 char *inp=cmd;
00405 char *outp=cmd1;
00406 unsigned long data;
00407 char c=0x20;
00408 int deep;
00409 const char *fmt="+-*/\\%|&^hpod";
00410 
00411 while(*inp)
00412 {
00413         if(strchr(fmt,*inp)){
00414         if(c!=0x20){*outp++=0x20;}
00415         *outp++=*inp++;*outp++=0x20;c=0x20;
00416         continue;
00417         }
00418         if(*inp==0x20||*inp=='\t'){inp++;continue;}
00419         *outp++=c=*inp++;
00420 }
00421 
00422 *outp=0;
00423 inp=cmd1;
00424 
00425    while(*inp)
00426    {
00427          while(*inp==' '||*inp=='\t')inp++;
00428 
00429          if(*inp=='('){
00430                  token=++inp;
00431                  deep=1;
00432                  while(1)
00433                  {
00434                          if(deep<1 || !*inp){printf("error synax");return 0;}
00435                          if(*inp==')' && deep==1){*inp=0;inp++;break;}
00436                          if(*inp=='('){inp++;deep++;continue;}
00437                          if(*inp==')'){inp++;deep--;continue;}
00438                          ++inp;
00439                  }
00440                  token=expr(token);
00441          }
00442          else {
00443          token=inp;
00444          while(*inp!=' ' && *inp!='\t' && *inp!=0)inp++;
00445          if(*inp){*inp=0;inp++;}
00446          }
00447 
00448         if(!token||!*token)break;
00449         if(token[1]==0 && strchr(fmt,*token)){op=*token;continue;}
00450         data=strtoul(token,0,0);
00451         switch(op)
00452         {
00453                 case '+':result=result+data;break;
00454                 case '-':result=result-data;break;
00455                 case '*':result=result*data;break;
00456                 case '/':result=result/data;break;
00457                 case '\\':result=data/result;break;
00458                 case '%':result=result%data;break;
00459                 case '|':result=result|data;break;
00460                 case '&':result=result&data;break;
00461                 case '^':result=result^data;break;
00462                 case '~':result=~result;break;
00463         }
00464 
00465    }
00466    switch(op)
00467    {
00468            case 'd':sprintf(cmd1,"%ld",result);break;
00469            case 'p':sprintf(cmd1,"%p",(void *)result);break;
00470            case 'o':sprintf(cmd1,"0%lo",result);break;
00471            default:sprintf(cmd1,"0x%lx",result);break;
00472    }
00473    return cmd1;
00474 }

static int help ( int  argc,
char **  argv 
) [static]

Definition at line 192 of file cmdparser.c.

References mycmd.name, mycmd.next, mycmd.note, and mycmd.usage.

00193 {
00194 struct mycmd *pcmd;
00195 char str[20];
00196 int i;
00197 if(argc==1)
00198 {
00199    for(pcmd=mycmd_head,i=0;pcmd;pcmd=pcmd->next,i++)
00200         {
00201         printf("%-10s:%-30s:%s\n",pcmd->name,pcmd->usage,pcmd->note);
00202         if(i%16==15){printf("press enter to continue...\n");read(0,str,20);}
00203         }
00204 }
00205 else if(argc==2)
00206 {
00207  for(pcmd=mycmd_head;pcmd;pcmd=pcmd->next)
00208         if(!strcmp(pcmd->name,argv[1]))
00209     {
00210         printf("%-10s:%-30s:%s\n",pcmd->name,pcmd->usage,pcmd->note);
00211         break;
00212         }
00213  if(!pcmd)printf("can not find cmd %s\n",argv[1]);
00214 }
00215 else return -1;
00216 
00217 return 0;
00218 }

static void init_cmd ( void   )  [static]

Definition at line 19 of file cmdparser.c.

References __mycmd_begin, __mycmd_end, and mycmd.next.

00021 {
00022 struct mycmd *pcmd;
00023  for(pcmd=&__mycmd_begin+1;pcmd!=&__mycmd_end;pcmd++)
00024  {
00025    pcmd->next = pcmd+1;
00026  }
00027  mycmd_head = &__mycmd_begin+1;
00028  mycmd_tail = &__mycmd_end-1;
00029  mycmd_tail->next = 0;
00030 }

static void init_config ( void   )  [static]

Definition at line 63 of file cmdparser.c.

References __myconfig_begin, __myconfig_end, myconfig.config, CONFIG_S, myconfig.next, and myconfig.type.

00065 {
00066 struct myconfig *pconfig;
00067  for(pconfig=&__myconfig_begin+1;pconfig!=&__myconfig_end;pconfig++)
00068  {
00069          if(pconfig->type == CONFIG_S)
00070          {
00071                  char *p;
00072                  p = malloc(strlen(*(char **)pconfig->config)+1);
00073                  strcpy(p,*(char **)pconfig->config);
00074                  *(char **)pconfig->config = p;
00075          }
00076          pconfig->next = pconfig+1;
00077  }
00078  pconfig--;
00079  pconfig->next = 0;
00080  myconfig_head = &__myconfig_begin+1;
00081  myconfig_tail = pconfig;
00082 }

static void init_symbol ( void   )  [static]

Definition at line 37 of file cmdparser.c.

References __mysymbol_begin, __mysymbol_end, and mysymbol.next.

00039 {
00040 struct mysymbol *psymbol;
00041  for(psymbol=&__mysymbol_begin+1;psymbol!=&__mysymbol_end;psymbol++)
00042  {
00043    psymbol->next = psymbol+1;
00044  }
00045  mysymbol_head = &__mysymbol_begin+1;
00046  mysymbol_tail = &__mysymbol_end;
00047 }

mycmd_init ( expr  ,
cmd_expr  ,
"expr"  ,
"expr"   
)

mycmd_init ( h  ,
help  ,
"h"  ,
"show this help"   
)

mycmd_init1 ( setconfig  ,
cmd_setconfig  ,
"setconfig "  [configname][val],
"setconfig for command"  ,
config_complete   
)

mysymbol_export ( add_cmd   ) 


Variable Documentation

Referenced by init_cmd().

Referenced by init_cmd().

Referenced by init_config().

Referenced by init_config().

Referenced by init_symbol().

Referenced by init_symbol().

struct mycmd* mycmd_head

Definition at line 16 of file cmdparser.c.

Referenced by command_generator(), and fileman_completion().

struct mycmd* mycmd_tail

Definition at line 17 of file cmdparser.c.

Definition at line 60 of file cmdparser.c.

Definition at line 61 of file cmdparser.c.

Definition at line 34 of file cmdparser.c.

Referenced by cmd_import().

Definition at line 35 of file cmdparser.c.

int showcmd = 0 [static]

Definition at line 8 of file cmdparser.c.

Referenced by _do_cmd(), and echo().


Generated on Fri Nov 5 07:24:48 2010 for ejtag debug tools for loongson 232 cpu by  doxygen 1.5.6