home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume15 / gbench / part01 / utils.c < prev   
Encoding:
C/C++ Source or Header  |  1988-06-12  |  8.1 KB  |  376 lines

  1. #include <gbench.h>
  2.  
  3. int Abs(n)
  4.     int n;
  5. {
  6.     return n>=0?n:-n;
  7. }
  8.  
  9. void CopyParams(dp,sp)
  10.     Params* dp;
  11.     Params* sp;
  12.  
  13. {
  14.     bcopy(sp,dp,sizeof(Params));
  15. }
  16.  
  17. void Fatal(p,e)
  18.     char*   p;
  19.     char*   e;
  20. {
  21.     fprintf(stderr,"%s:  ",p);
  22.     fprintf(stderr,e,p);
  23.     putc('\n',stderr);
  24.     exit(1);
  25. }
  26.  
  27. int FontIndex(sp,sizep)
  28.     State*  sp;
  29.     int*    sizep;
  30. {
  31.     int i;
  32.     int    mindist = _DisplayHeight;
  33.     int minindex = -1;
  34.     int minsize = *sizep;
  35.     int newsize;
  36.  
  37.     for (i=0;(i<MaxFonts)&&sp->fontinfo[i];i++) {
  38. #    ifdef X10
  39.         newsize = sp->fontinfo[i]->height;
  40. #    else
  41.         newsize = sp->fontinfo[i]->ascent;
  42. #    endif
  43.     if (Abs(*sizep-newsize)<mindist) {
  44.         mindist = Abs(*sizep-newsize);
  45.         minindex = i;
  46.         minsize = newsize;
  47.     }
  48.     }
  49.     *sizep = minsize;
  50.     return minindex;
  51. }
  52.  
  53. Load GetLoad()
  54. {
  55.     int            kmem;
  56.     struct nlist    nl[2];
  57. #   ifdef FIXLOAD
  58.     long        load;
  59. #   else
  60.     double        load;
  61. #   endif
  62.  
  63.     nl[0].n_name = "_avenrun";
  64.     nl[1].n_name = nil;
  65.     nlist("/vmunix",nl);
  66.     if (!nl[0].n_type) return 0.;
  67.     if ((kmem=open("/dev/kmem",0))<0) return 0.;
  68.     lseek(kmem,nl[0].n_value,0);
  69.     read(kmem,&load,sizeof(load));
  70. #   ifdef FIXLOAD
  71.     return load/256.;
  72. #   else
  73.     return load;
  74. #   endif
  75. }
  76.  
  77. OpIndex GetOpIndex(opname)
  78.     char* opname;
  79. {
  80.     int i;
  81.     
  82.     for (i=0;i<NumOps;i++) {
  83.     if (!strncmp(opname,OpNames[i][0],strlen(opname))) return (OpIndex)i;
  84.     }
  85.     return NilIndex;
  86. }
  87.  
  88. Rtime GetTime(sp)
  89.     State*  sp;
  90. {
  91.     struct timeval t;
  92.  
  93.     XSync(DARGC true);
  94.     gettimeofday(&t,0);
  95.     return t.tv_sec+t.tv_usec/1000000.;
  96. }
  97.  
  98. void Help(f,s)
  99.     FILE*   f;
  100.     char*   s;
  101. {
  102.     putc('#',f);
  103.     fputs(s,f);
  104.     putc('\n',f);
  105. }
  106.  
  107. int InputMask(cp)
  108.     Cmd* cp;
  109. {
  110.     int im = 0;
  111.     
  112.     if (cp->o.drag) im |= (ButtonPressMask|ButtonReleaseMask);
  113.     if (!cp->o.poll) im |= PointerMotionMask;
  114.     if (cp->o.overlap) im |= (ExposeRegionMask|ExposureMask);
  115.     return im;
  116. }
  117.  
  118. int Limit(n,min,max)
  119.     int    n;
  120.     int    min;
  121.     int    max;
  122. {
  123.     if (n<min) n = min;
  124.     else if (n>max) n = max;
  125.     return n;
  126. }
  127.  
  128. void MapWin(sp,w)
  129.     State*  sp;
  130.     Window  w;
  131. {
  132. #   ifdef X11
  133.         XEvent e;
  134.         XSelectInput(sp->d,w,StructureNotifyMask);
  135. #   endif X11
  136.     XMapWindow(DARGC w);
  137. #   ifdef X11
  138.         do
  139.             XNextEvent(sp->d,&e);
  140.         while (e.type!=MapNotify);
  141. #   endif X11
  142. }
  143.  
  144. void MoveClip(sp,cp,x,y)
  145.     State*  sp;
  146.     Cmd*    cp;
  147.     int        x;
  148.     int        y;
  149. {
  150.     if (cp->o.overlap) {
  151.     x += cp->p.maxshift/2-OverlapSize/2;
  152.     y += cp->p.maxshift/2-OverlapSize/2;
  153.     XMoveWindow(DARGC sp->cw1,x,y);
  154.     XMoveWindow(DARGC sp->cw2,x,y);
  155.     }
  156. }
  157.  
  158. void MoveVert(sp,cp,vcount)
  159.     State*  sp;
  160.     Cmd*    cp;
  161.     int        vcount;
  162. {
  163.     int i;
  164.     
  165.     if (cp->dx||cp->dy) for (i=0;i<=vcount;i++) {
  166.     sp->v[i].x += cp->dx;
  167.     sp->v[i].y += cp->dy;
  168.     }
  169. }
  170.  
  171. FILE* Out(sp,cp)
  172.     State*  sp;
  173.     Cmd*    cp;
  174. {
  175.     if (cp->o.silent) return stderr;
  176.     else return sp->outfd;
  177. }
  178.  
  179. void PrintConfig(f,sp)
  180.     FILE*   f;
  181.     State*  sp;
  182. {
  183.     int t = time(0);
  184.  
  185.     fprintf(f,"# host=%s\n",sp->host);
  186.     fprintf(f,"# display=%s\n",getenv("DISPLAY"));
  187.     fprintf(f,"# graphics=%s\n",sp->gxname);
  188.     fprintf(f,"# time=%s",ctime(&t));
  189.     fprintf(f,"# version=%.1f\n",Version);
  190.     fprintf(f,"# tag=%s\n",sp->defaults.tag);
  191. }
  192.  
  193. void PrintParam(f,pp,pre,name,post)
  194.     FILE*   f;
  195.     Params* pp;
  196.     char*   pre;
  197.     char*   name;
  198.     char*   post;
  199. {
  200.     fprintf(f,"%s",pre);
  201.     if (!strcmp(name,"angle")) fprintf(f,"%s=%d",name,pp->angle);
  202.     else if (!strcmp(name,"aspect")) fprintf(f,"%s=%.1f",name,pp->aspect);
  203.     else if (!strcmp(name,"count")) fprintf(f,"%s=%d",name,pp->count);
  204.     else if (!strcmp(name,"fonts")) fprintf(f,"%s=%s",name,pp->fonts);
  205.     else if (!strcmp(name,"ptsize")) fprintf(f,"%s=%d",name,pp->ptsize);
  206.     else if (!strcmp(name,"lwidth")) fprintf(f,"%s=%d",name,pp->lwidth);
  207.     else if (!strcmp(name,"maxshift")) fprintf(f,"%s=%d",name,pp->maxshift);
  208.     else if (!strcmp(name,"offset")) fprintf(f,"%s=%.1f",name,pp->offset);
  209.     else if (!strcmp(name,"nchar")) fprintf(f,"%s=%d",name,pp->nchar);
  210.     else if (!strcmp(name,"nwin")) fprintf(f,"%s=%d",name,pp->nwin);
  211.     else if (!strcmp(name,"nvert")) fprintf(f,"%s=%d",name,pp->nvert);
  212.     else if (!strcmp(name,"opts")) fprintf(f,"%s=%s",name,pp->opts);
  213.     else if (!strcmp(name,"outfile")) fprintf(f,"%s=%s",name,pp->outfile);
  214.     else if (!strcmp(name,"size")) fprintf(f,"%s=%d",name,pp->size);
  215.     else if (!strcmp(name,"tag")) fprintf(f,"%s=%s",name,pp->tag);
  216.     else if (!strcmp(name,"timegoal")) fprintf(f,"%s=%d",name,pp->timegoal);
  217.     else if (!strcmp(name,"winsize")) fprintf(f,"%s=%d",name,pp->winsize);
  218.     fprintf(f,"%s",post);
  219. }
  220.  
  221. void PrintParams(f,sp,pp)
  222.     FILE*   f;
  223.     State*  sp;
  224.     Params* pp;
  225. {
  226.     int i;
  227.  
  228.     PrintParam(f,pp,"# ","angle","\n");
  229.     PrintParam(f,pp,"# ","aspect","\n");
  230.     PrintParam(f,pp,"# ","count","\n");
  231.     PrintParam(f,pp,"# ","fonts","\n");
  232.     PrintParam(f,pp,"# ","ptsize","\n");
  233.     PrintParam(f,pp,"# ","lwidth","\n");
  234.     PrintParam(f,pp,"# ","maxshift","\n");
  235.     PrintParam(f,pp,"# ","offset","\n");
  236.     PrintParam(f,pp,"# ","nchar","\n");
  237.     PrintParam(f,pp,"# ","nwin","\n");
  238.     PrintParam(f,pp,"# ","nvert","\n");
  239.     PrintParam(f,pp,"# ","opts","\n");
  240.     PrintParam(f,pp,"# ","outfile","\n");
  241.     PrintParam(f,pp,"# ","size","\n");
  242.     PrintParam(f,pp,"# ","tag","\n");
  243.     PrintParam(f,pp,"# ","timegoal","\n");
  244.     PrintParam(f,pp,"# ","winsize","\n");
  245. }
  246.  
  247. void PrintHelp(f)
  248.     FILE* f;
  249. {
  250.     int        i;
  251.     int        j;
  252.  
  253.     Help(f,"Commands:");
  254.     for (i=0;i<NumOps;i++) {
  255.     fprintf(f,"#  %-8s",OpNames[i][0]);
  256.     for (j=0;(j<MaxPositional)&&OpNames[i][1+j];j++) {
  257.         fprintf(f," [%-6s]",OpNames[i][1+j]);
  258.     }
  259.     fprintf(f," [n=v]*\n");
  260.     }
  261.     Help(f,"  config");
  262.     Help(f,"  defaults [n=v]*");
  263.     Help(f,"  help");
  264.     Help(f,"  init");
  265.     Help(f,"  script   [filename]");
  266.     Help(f,"  quit");
  267.     Help(f,"  !");
  268.     Help(f,"  #");
  269.     Help(f,"Options:");
  270.     Help(f,"  ac    Alternate colors");
  271.     Help(f,"  af    Alternate fonts");
  272.     Help(f,"  ag    Alternate graphics contexts");
  273.     Help(f,"  aw    Alternate windows");
  274.     Help(f,"  d     Drag");
  275.     Help(f,"  f     Fill");
  276.     Help(f,"  i     Invert");
  277.     Help(f,"  m     Monitor for profiling");
  278.     Help(f,"  n     No options");
  279.     Help(f,"  o     Overlap");
  280.     Help(f,"  os    Offscreen source");
  281.     Help(f,"  od    Offscreen destination");
  282.     Help(f,"  p     Poll");
  283.     Help(f,"  ps    Polygon self-intersecting");
  284.     Help(f,"  pw    Polygon winding number fill");
  285.     Help(f,"  r     Reset defaults");
  286.     Help(f,"  s     Stipple");
  287.     Help(f,"  t     Tile");
  288.     Help(f,"  u     Unbatched");
  289. }
  290.  
  291. void ProfControl(i)
  292.     int i;
  293. {
  294. # ifdef GPROF
  295.     moncontrol(i);
  296. # endif GPROF
  297. }
  298.  
  299. char* StrToken(s1,s2,tsp)
  300.     char*    s1;
  301.     char*    s2;
  302.     TokenState*    tsp;
  303. {
  304.     char* bp;
  305.  
  306.     if (s1) {
  307.     tsp->cur = s1;
  308.     tsp->end = s1+strlen(s1);
  309.     }
  310.     else {
  311.     tsp->cur += strlen(tsp->cur)+1;
  312.     if (tsp->cur>=tsp->end) return nil;
  313.     }
  314.     tsp->cur += strspn(tsp->cur,s2);
  315.     if (bp=strpbrk(tsp->cur,s2)) *bp = '\0';
  316.     if (tsp->cur>=tsp->end) return nil;
  317.     else return tsp->cur;
  318. }
  319.  
  320. void UnmapWin(sp,w)
  321.     State*  sp;
  322.     Window  w;
  323. {
  324. #   ifdef X11
  325.         XEvent e;
  326.         XSelectInput(sp->d,w,StructureNotifyMask);
  327. #   endif X11
  328.     XUnmapWindow(DARGC w);
  329. #   ifdef X11
  330.         do
  331.             XNextEvent(sp->d,&e);
  332.         while (e.type!=UnmapNotify);
  333. #   endif X11
  334. }
  335.  
  336. #ifdef X10
  337. void X10Draw(sp,cp,vcount)
  338.     State*  sp;
  339.     Cmd*    cp;
  340.     int        vcount;
  341. {
  342.     if (cp->o.fill) {
  343.     if (cp->o.tile) {
  344.         XDrawTiled(cp->w,sp->v,vcount,sp->tile,cp->func,AllPlanes);
  345.     }
  346.     else XDrawFilled(cp->w,sp->v,vcount,cp->colorfg,cp->func,AllPlanes);
  347.     }
  348.     else {
  349.     if (cp->o.stipple) {
  350.         XDrawDashed(
  351.         cp->w,sp->v,vcount,cp->p.lwidth,cp->p.lwidth,cp->colorfg,
  352.         sp->pattern,cp->func,AllPlanes
  353.         );
  354.     }
  355.     else if (cp->o.tile) {
  356.         XDrawPatterned(
  357.         cp->w,sp->v,vcount,cp->p.lwidth,cp->p.lwidth,cp->colorfg,
  358.         cp->colorbg,sp->pattern,cp->func,AllPlanes
  359.         );
  360.     }
  361.     else if (vcount==2) {
  362.         XLine(
  363.         cp->w,sp->v[0].x,sp->v[0].y,sp->v[1].x,sp->v[1].y,cp->p.lwidth,
  364.         cp->p.lwidth,cp->colorfg,cp->func,AllPlanes
  365.         );
  366.     }
  367.     else {
  368.         XDraw(
  369.         cp->w,sp->v,vcount,cp->p.lwidth,cp->p.lwidth,cp->colorfg,
  370.         cp->func,AllPlanes
  371.         );
  372.     }
  373.     }
  374. }
  375. #endif X10
  376.