00001 #include "argvize.c"
00002 #define LINESZ 200
00003
00004 static char *expand (char *);
00005 static char *expand1 (char *);
00006 static int help(int argc,char **argv);
00007
00008 static int showcmd=0;
00009
00010 #include "cmdparser.h"
00011 #include <myconfig.h>
00012
00013
00014 extern struct mycmd __mycmd_begin;
00015 extern struct mycmd __mycmd_end;
00016 struct mycmd *mycmd_head;
00017 struct mycmd *mycmd_tail;
00018
00019 static void init_cmd(void) __attribute__ ((constructor));
00020 static void init_cmd()
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 }
00031
00032 extern struct mysymbol __mysymbol_begin;
00033 extern struct mysymbol __mysymbol_end;
00034 struct mysymbol *mysymbol_head;
00035 struct mysymbol *mysymbol_tail;
00036
00037 static void init_symbol(void) __attribute__ ((constructor));
00038 static void init_symbol()
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 }
00048
00049 void add_cmd(struct mycmd *mycmd)
00050 {
00051 printf("add cmd %x\n",mycmd);
00052 mycmd_tail->next = mycmd;
00053 mycmd_tail = mycmd;
00054 }
00055
00056 mysymbol_export(add_cmd);
00057
00058 extern struct myconfig __myconfig_begin;
00059 extern struct myconfig __myconfig_end;
00060 struct myconfig *myconfig_head;
00061 struct myconfig *myconfig_tail;
00062
00063 static void init_config(void) __attribute__ ((constructor));
00064 static void init_config()
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 }
00083
00084 static int cmd_setconfig(int argc,char **argv)
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 }
00133
00134 char *dupstr (int);
00135
00136
00137
00138
00139 char *
00140 config_generator (text, state)
00141 const char *text;
00142 int state;
00143 {
00144 static int list_index, len;
00145 char *name;
00146 static struct myconfig *pconfig;
00147
00148
00149
00150
00151 if (!state)
00152 {
00153 pconfig = myconfig_head;
00154 len = strlen (text);
00155 }
00156
00157
00158
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
00171 return ((char *)NULL);
00172
00173 }
00174
00175 static char *config_complete(int argc,char **argv,char *text,int start,int end)
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 }
00189
00190 mycmd_init1(setconfig,cmd_setconfig,"setconfig [configname] [val]","setconfig for command",config_complete);
00191
00192 static int help(int argc,char **argv)
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 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 char *_do_cmd(char *p_in,int need_end);
00231
00232 void do_cmd(const char *p)
00233 {
00234 char *cmd=malloc(strlen(p)+1);
00235 strcpy(cmd,p);
00236 _do_cmd(cmd,0);
00237 free(cmd);
00238 }
00239
00240 char *_do_cmd(char *p_in,int need_end)
00241 {
00242 char *av[MAX_AC];
00243 int32_t ac;
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
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
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;
00313 }
00314 }while(0);
00315
00316 if(*p_in)goto cmd_loop;
00317 return p_in;
00318 }
00319
00320
00321
00322
00323
00324
00325
00326
00327 static char *
00328 expand(cmdline)
00329 char *cmdline;
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
00360 v = var;
00361 if(*ip == '{') {
00362
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
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 }
00397
00398 static char *expr(char *cmd)
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 }
00475
00476 static char *expand1(char *cmdline)
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
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 }
00535
00536 int cmd_expr(int argc,char **argv)
00537 {
00538 if(argc!=2)return -1;
00539 printf("%s\n",expr(argv[1]));
00540 return 0;
00541 }
00542
00543 mycmd_init(h,help,"h","show this help");
00544 mycmd_init(expr,cmd_expr,"expr","expr");