home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume15 / gbench / part01 / cmds.c next >
Encoding:
C/C++ Source or Header  |  1988-06-12  |  7.2 KB  |  290 lines

  1. #include <gbench.h>
  2.  
  3. Result ParseOpts(os,sfp,ofp)
  4.     char*    os;
  5.     OptFlags*    sfp;
  6.     OptFlags*    ofp;
  7. {
  8.     int        i;
  9.     char    ps[MaxStr];
  10.     bool*    ofa = (bool*)ofp;
  11.     char*    osp = os;
  12.     Result    result = Succeeded;
  13.     bool*    sfa = (bool*)sfp;
  14.     char*    t;
  15.     TokenState    ts;
  16.  
  17.     for (i=0;i<(sizeof(OptFlags)/sizeof(bool));i++) ofa[i] = false;
  18.     strcpy(ps,osp);
  19.     *osp = '\0';
  20.     if (t=StrToken(ps,",",&ts)) do {
  21.     i = -1;
  22.     if (!strcmp(t,"ac")) i = &ofp->altcolor-ofa;
  23.     else if (!strcmp(t,"af")) i = &ofp->altfont-ofa;
  24.     else if (!strcmp(t,"ag")) i = &ofp->altgc-ofa;
  25.     else if (!strcmp(t,"aw")) i = &ofp->altwin-ofa;
  26.     else if (!strcmp(t,"d")) i = &ofp->drag-ofa;
  27.     else if (!strcmp(t,"f")) i = &ofp->fill-ofa;
  28.     else if (!strcmp(t,"i")) i = &ofp->invert-ofa;
  29.     else if (!strcmp(t,"m")) i = &ofp->profile-ofa;
  30.     else if (!strcmp(t,"o")) i = &ofp->overlap-ofa;
  31.     else if (!strcmp(t,"os")) i = &ofp->offsrc-ofa;
  32.     else if (!strcmp(t,"od")) i = &ofp->offdest-ofa;
  33.     else if (!strcmp(t,"p")) i = &ofp->poll-ofa;
  34.     else if (!strcmp(t,"ps")) i = &ofp->polyself-ofa;
  35.     else if (!strcmp(t,"pw")) i = &ofp->polywind-ofa;
  36.     else if (!strcmp(t,"r")) i = &ofp->setdefaults-ofa;
  37.     else if (!strcmp(t,"s")) i = &ofp->stipple-ofa;
  38.     else if (!strcmp(t,"t")) i = &ofp->tile-ofa;
  39.     else if (!strcmp(t,"u")) i = &ofp->unbatched-ofa;
  40.     if ((i>=0)&&!ofa[i]) {
  41.         if (!sfa[i]) result = OptNotSupported;
  42.         ofa[i] = true;
  43.         sprintf(osp,"%s,",t);
  44.         osp += strlen(osp);
  45.     }
  46.     } while (t=StrToken(nil,",",&ts));
  47.     if (osp>os) *--osp = '\0';
  48.     return result;
  49. }
  50.  
  51. char* GetToken(sp,first)
  52.     State*  sp;
  53.     bool    first;
  54. {
  55.     if (!sp->strinput) {
  56.     if (first) {
  57.         while((sp->argindex<sp->argc)&&(*sp->argv[sp->argindex]!='-')) {
  58.         sp->argindex++;
  59.         }
  60.         if (sp->argindex>=sp->argc) return nil;
  61.         else return sp->argv[sp->argindex++];
  62.     }
  63.     else {
  64.         if (*sp->argv[sp->argindex]=='-') return nil;
  65.         else return sp->argv[sp->argindex++];
  66.     }
  67.     }
  68.     else return StrToken(first?sp->instr:nil," ",&sp->ts);
  69. }
  70.  
  71. int ScanInt(s,min,max)
  72.     char*   s;
  73.     int        min;
  74.     int        max;
  75. {
  76.     int i;
  77.  
  78.     sscanf(s,"%d",&i);
  79.     return i = Limit(i,min,max);
  80. }
  81.  
  82. void SetParam(sp,pp,n,v,concat)
  83.     State*  sp;
  84.     Params* pp;
  85.     char*   n;
  86.     char*   v;
  87.     bool    concat;
  88. {
  89.     if (!strcmp(n,"angle")) pp->angle = ScanInt(v,0,360);
  90.     else if (!strcmp(n,"aspect")) {
  91.     sscanf(v,"%f",&pp->aspect);
  92.     if (pp->aspect<MinAspect) pp->aspect = MinAspect;
  93.     }
  94.     else if (!strcmp(n,"count")) pp->count = ScanInt(v,0,MaxNum);
  95.     else if (!strcmp(n,"fonts")) strcpy(pp->fonts,v);
  96.     else if (!strcmp(n,"lwidth")) pp->lwidth = ScanInt(v,0,_DisplayWidth);
  97.     else if (!strcmp(n,"maxshift")) pp->maxshift = ScanInt(v,0,_DisplayWidth);
  98.     else if (!strcmp(n,"nchar")) pp->nchar = ScanInt(v,0,MaxStr);
  99.     else if (!strcmp(n,"nvert")) pp->nvert = ScanInt(v,3,MaxVert);
  100.     else if (!strcmp(n,"nwin")) pp->nwin = ScanInt(v,0,MaxNum);
  101.     else if (!strcmp(n,"offset")) sscanf(v,"%f",&pp->offset);
  102.     else if (!strcmp(n,"opts")) {
  103.     if (concat) {
  104.         if (strlen(pp->opts)&&strlen(v)) strcat(pp->opts,",");
  105.         strcat(pp->opts,v);
  106.     }
  107.     else strcpy(pp->opts,v);
  108.     }
  109.     else if (!strcmp(n,"outfile")) strcpy(pp->outfile,v);
  110.     else if (!strcmp(n,"ptsize")) pp->ptsize = ScanInt(v,0,_DisplayHeight);
  111.     else if (!strcmp(n,"size")) pp->size = ScanInt(v,0,_DisplayWidth);
  112.     else if (!strcmp(n,"tag")) strcpy(pp->tag,v);
  113.     else if (!strcmp(n,"timegoal")) pp->timegoal = ScanInt(v,0,MaxNum);
  114.     else if (!strcmp(n,"winsize")) pp->winsize = ScanInt(v,1,_DisplayHeight);
  115. }
  116.  
  117. void ParseParams(sp,cp,pp,print)
  118.     State*    sp;
  119.     Cmd*    cp;
  120.     Params*    pp;
  121.     bool    print;
  122. {
  123.     int        i;
  124.     char*   t = nil;
  125.     char*   v;
  126.  
  127.     if (cp->index!=NilIndex) {
  128.     for (
  129.         i=0;
  130.         (i<MaxPositional)&&OpNames[(int)cp->index][1+i]&&
  131.         (t=GetToken(sp,false))&&!strchr(t,'=');
  132.         i++
  133.     ) {
  134.         SetParam(sp,pp,OpNames[(int)cp->index][1+i],t,true);
  135.     }
  136.     }
  137.     do if (t&&(v=strchr(t,'='))) {
  138.     *v++ = '\0';
  139.     SetParam(sp,pp,t,v,false);
  140.     if (print) PrintParam(Out(sp,cp),pp,"  ",t,"");
  141.     *--v = '=';
  142.     } while (t=GetToken(sp,false));
  143.     if (print) {
  144.     putc('\n',Out(sp,cp));
  145.     fflush(Out(sp,cp));
  146.     }
  147.     if (strcmp(sp->fonts,pp->fonts)) SetFonts(sp,pp);
  148.     if (strcmp(sp->outfile,pp->outfile)) SetOutfile(sp,pp);
  149.     if (sp->winsize!=pp->winsize) SetWinsize(sp,pp);
  150.     if (ParseOpts(pp->opts,&sp->supopts,&cp->o)!=Succeeded) {
  151.     cp->result = OptNotSupported;
  152.     }
  153. }
  154.  
  155. void ParseCmd(sp,cp)
  156.     State*  sp;
  157.     Cmd*    cp;
  158. {
  159.     cp->result = sp->supcmds[(int)cp->index]?Succeeded:CmdNotSupported;
  160.     ParseParams(sp,cp,&cp->p,false);
  161.     cp->func = cp->o.invert?GXinvert:GXcopy;
  162.     sp->previndex = cp->index;
  163.     CopyParams(&sp->prevparams,&cp->p);
  164.     if (cp->o.setdefaults) CopyParams(&sp->defaults,&cp->p);
  165. }
  166.  
  167. void PushInput(sp,fn)
  168.     State*  sp;
  169.     char*   fn;
  170. {
  171.     if ((sp->indepth<MaxIndepth)&&!(sp->infds[++sp->indepth]=fopen(fn,"r"))) {
  172.     sp->indepth--;
  173.     }
  174. }
  175.  
  176. bool PopInput(sp)
  177.     State*  sp;
  178. {
  179.     bool nonempty = (bool)(sp->indepth>0);
  180.  
  181.     if (nonempty) sp->indepth--;
  182.     return nonempty;
  183. }
  184.  
  185. void PrintComment(sp,cp,t)
  186.     State*  sp; 
  187.     Cmd*    cp;
  188.     char*   t;
  189. {
  190.     char* te;
  191.  
  192.     if (sp->strinput) {
  193.     te = t+strlen(t);
  194.      if (te<sp->ts.end) *te = ' ';
  195.     fprintf(Out(sp,cp),"%s\n",t);
  196.     }
  197.     else {
  198.     do fprintf(Out(sp,cp),"%s ",t); while (t=GetToken(sp,false));
  199.     putc('\n',Out(sp,cp));
  200.     }
  201. }
  202.  
  203. bool GetLine(sp)
  204.     State*  sp;
  205. {
  206.     char* cp;
  207.  
  208.     if (sp->scriptindex>=0) {
  209.     strcpy(sp->instr,Script[sp->scriptindex]);
  210.     if (!Script[++sp->scriptindex]) sp->scriptindex = -1;
  211.     sp->strinput = true;
  212.     }
  213.     else if (sp->argindex>=sp->argc) {
  214.     while (!fgets(sp->instr,MaxStr,sp->infds[sp->indepth])) {
  215.         if (!PopInput(sp)) return false;
  216.     }
  217.     if (cp=strchr(sp->instr,'\n')) *cp='\0';
  218.     sp->strinput = true;
  219.     }
  220.     else sp->strinput = false;
  221.     return true;
  222. }
  223.  
  224. bool GetCmd(sp,cp)
  225.     State*  sp;
  226.     Cmd*    cp;
  227. {
  228.     char*   t;
  229.     int     tlen;
  230.  
  231.     while (true) {
  232.     cp->index = NilIndex;
  233.     if (!GetLine(sp)) return false;
  234.     if (t=GetToken(sp,true)) {
  235.         if (*t=='-') t++;
  236.         if (*t=='@') {
  237.         t++;
  238.         cp->o.silent = true;
  239.         }
  240.         else cp->o.silent = false;
  241.         tlen = strlen(t);
  242.         if (!strncmp(t,"config",tlen)) {
  243.         fprintf(Out(sp,cp),"config\n");
  244.         PrintConfig(Out(sp,cp),sp);
  245.         putc('\n',Out(sp,cp));
  246.         }
  247.         else if (!strncmp(t,"defaults",tlen)) {
  248.         fprintf(Out(sp,cp),"defaults");
  249.         ParseParams(sp,cp,&sp->defaults,true);
  250.         PrintParams(stderr,sp,&sp->defaults);
  251.         putc('\n',stderr);
  252.         }
  253.         else if (!strncmp(t,"help",tlen)) {
  254.         PrintHelp(Out(sp,cp));
  255.         putc('\n',stderr);
  256.         }
  257.         else if (!strncmp(t,"init",tlen)) {
  258.         fprintf(Out(sp,cp),"init\n");
  259.         InitParams(sp,&sp->defaults);
  260.         PrintParams(stderr,sp,&sp->defaults);
  261.         putc('\n',stderr);
  262.         }
  263.         else if (!strncmp(t,"quit",tlen)) {
  264.         return false;
  265.         }
  266.         else if (!strncmp(t,"script",tlen)) {
  267.         fprintf(Out(sp,cp),"script");
  268.         if (t=GetToken(sp,false)) fprintf(Out(sp,cp)," %s",t);
  269.         putc('\n',Out(sp,cp));
  270.         if (t) PushInput(sp,t);
  271.         else sp->scriptindex = 0;
  272.         }
  273.         else if (*t=='#') {
  274.         PrintComment(sp,cp,t);
  275.         }
  276.         else if ((*t=='!')&&((cp->index=sp->previndex)!=NilIndex)) {
  277.         CopyParams(&cp->p,&sp->prevparams);
  278.         ParseCmd(sp,cp);
  279.         return true;
  280.         }
  281.         else if ((cp->index=GetOpIndex(t))!=NilIndex) {
  282.         CopyParams(&cp->p,&sp->defaults);
  283.         ParseCmd(sp,cp);
  284.         return true;
  285.         }
  286.     }
  287.     else putchar('\n');
  288.     }
  289. }
  290.