home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol126 / roff46.c < prev    next >
Encoding:
C/C++ Source or Header  |  1984-04-29  |  7.0 KB  |  271 lines

  1. /********************************************************/
  2. /*                            */
  3. /*            ROFF4, Version 1.50            */
  4. /*                            */
  5. /* (C) 1983 by    Ernest E. Bergmann            */
  6. /*        Physics, Building #16            */
  7. /*        Lehigh Univerisity            */
  8. /*        Bethlehem, Pa. 18015            */
  9. /*                            */
  10. /* Permission is hereby granted for all commercial and    */
  11. /* non-commercial reproduction and distribution of this    */
  12. /* material provided this notice is included.        */
  13. /*                            */
  14. /********************************************************/
  15. /*June 27, 1983*/
  16. #include "roff4.h"
  17. /**************************************************/
  18. int value(base,string)    /*unsigned conversion*/
  19. int base;        /*radix for conversion*/
  20. char *string;        /*no leading blanks please!*/
  21.             /*trailing whitespace or '\0'*/
  22. {int val,d;
  23. char c;
  24.     val=0;
  25.     for(d=digit(*string);d>=0 && d<base ; d=digit(*string))
  26.         {val = val*base + d;
  27.         string++;
  28.         }
  29.     c=*string;
  30.     if(!c || c==' ' || c==TAB || c=='\n') return(val);
  31.     else return(-1);    /*error return is -1*/
  32. }
  33. /**************************************************/
  34. int digit(d)
  35. char d;
  36. {    d=toupper(d);
  37.     if(d<='9') return(d-'0');
  38.     if(d<'A') return(-1); /*error return is negative val*/
  39.     if(d<='Z') return(10-'A'+d);
  40.     return(-1);    /*error*/
  41. }
  42. /**************************************************/
  43. strln3(s,word,num)
  44.  /*returns printed string length; checks legality of
  45.         word function;keeps track of vertical
  46.         excursions; records them in globals*/
  47. char *s;
  48. int word;    /* boolean, if true, check is made for none
  49.         black characters in the string*/
  50. int num;    /* for expansion of NUMSIGN;set 1 to ignore*/
  51. {int i,i2,p1,p2,p3;
  52. int t,b,h;    /*vertical vars*/
  53. char c, *ss;
  54. ss=s;
  55. t=b=h=0;
  56. p3=p2=p1=-LSZ;
  57. for(c=*s,i2=i=0;c;c=*(++s))
  58.     {if(c==NUMSIGN)
  59.         {i++;
  60.         if(num>9) i++;
  61.         if(num>99) i++;
  62.         if(num>999) i++;
  63.         }
  64.     else if((c!=TCVAL)&&(c!=CFVAL))
  65.         {if((c<=' ')&&(word)) goto error;
  66.         else i++;
  67.         }
  68.     else if(c==CFVAL)
  69.         {c=*(++s);
  70.         if(c==TCVAL) goto error;/*both CFVAL,TCVAL*/
  71.         switch(c)
  72.         {case 'h':
  73.         case 'H':if(i>i2) i2=i;
  74.              if(i) i--;
  75.             else goto error;/*before start*/
  76.             break;
  77.         case '+': h--; if(h<t) t=h; break;
  78.         case '-': h++; if(h>b) b=h; break;
  79.         case 'B':
  80.         case 'b':
  81.         case 'D':
  82.         case 'd':
  83.         case 'u':
  84.         case 'U':
  85.         case 'X':
  86.         case 'x': break;
  87.         case '(': p1=i; break;
  88.         case '[': p2=i; break;
  89.         case '{': p3=i; break;
  90.         case ')': if(i>i2) i2=i; i=p1; break;
  91.         case ']': if(i>i2) i2=i; i=p2; break;
  92.         case '}': if(i>i2) i2=i; i=p3; break;
  93.         default: if(CPTR[c-' ']) break;
  94.         goto error;    /*undecipherable*/
  95.         }}
  96.     else/*c==TCVAL*/
  97.         {if(class(*(s+1))!=BLACK)
  98.         goto error;    /*illegal translation*/
  99.         }
  100.     }
  101. if(h) goto error;
  102. if(word){WTOP=t;
  103.     WBOT=b;
  104.     }
  105. else    {LTOP=t;
  106.     LBOT=b;
  107.     }
  108. if(i>=i2)return(i);
  109. /* else prints beyond last character: */
  110. error:
  111.     /*should be fprint -> STDERR*/
  112.     fprintf(STDERR,"STRLN3:<%s> is illegally formed\n",
  113.                 ss);
  114.     return(strlen(ss));
  115. }
  116. /* A properly formed token string has its first printable
  117. character indicating the lefthand edge and the last printable
  118. character at the right hand edge.  Only legal control pairs
  119. accepted.  It must consist of printable symbols. */
  120. /************************************
  121. set stack like set() sets a variable
  122. *************************************/
  123. setS(param,val,arg_typ,defval,minval,maxval)
  124. int param[STKSIZ+1],val,defval,minval,maxval;
  125. char arg_typ;
  126. {int i;
  127.     if(val==NO_VAL)
  128.         {for(i=0;i<STKSIZ;i++)    /*pop*/
  129.         param[i]=param[i+1];
  130.         param[STKSIZ]=defval;
  131.         }
  132.     else    {for(i=STKSIZ;i;i--)    /*push*/
  133.         param[i]=param[i-1];
  134.         if (arg_typ=='+') *param+=val;
  135.         else if (arg_typ=='-') *param-=val;
  136.         else *param=val;
  137.         }
  138.     *param=min(max(*param,minval),maxval);
  139. if DEBUG fprintf(STDERR,"\n  setS: *param = %d",*param);
  140. }
  141. /******************************************
  142. initialize stack type variable, st, with v
  143. *******************************************/
  144. initsk(st,v)
  145. int st[STKSIZ+1],v;
  146. {int i;
  147.     for(i=STKSIZ+1;i;st[--i]=v);
  148. }
  149. /**************************************************/
  150. gettr()    /*process .tr */
  151. {char chr,*pchr,getcode();
  152. char wrdbuf[MAXLINE];
  153.     getwrd(LINE,wrdbuf);    /*remove .tr*/
  154.     if(getwrd(LINE,wrdbuf)==WE_HAVE_A_WORD)chr=*wrdbuf;
  155.     else return;    /*error: missing args*/
  156.  
  157.     pchr = TREND;
  158.     if('.'==getcode())
  159.         TPTR[chr-' ']=pchr;    /*record pointer*/
  160.     else    {TREND=pchr;
  161.     fprintf(STDERR,"\nError for .TR; error in line:\n%s",
  162.         LINE);
  163.     }
  164. }
  165. /**************************************************/
  166. getpc()    /*process .pc, printer control */
  167. {char chr,*pchr,getcode();
  168. char wrdbuf[MAXLINE];
  169.     getwrd(LINE,wrdbuf);    /*remove .pc*/
  170.     if(getwrd(LINE,wrdbuf)==WE_HAVE_A_WORD)chr=*wrdbuf;
  171.     else return;    /*error: missing args*/
  172.  
  173.     pchr = TREND;
  174.     if('.'==getcode())
  175.         CPTR[chr-' ']=pchr;    /*record pointer*/
  176.     else    {TREND=pchr;
  177.     fprintf(STDERR,"\nError for .PC; error in line:\n%s",
  178.         LINE);
  179.     }
  180. }
  181. /**************************************************/
  182. char getcode()    /*LINE must contain the radix as the first
  183.         token and it and the following lines then
  184.         contain code values finally delimited by a
  185.         token that starts with a '.' ; comments can
  186.         be at the end of any of these lines, set off by
  187.         " ;" */
  188. {int base,code;    /*conversion radix, value*/
  189. char *pcode,ncode;
  190. char wrdbuf[MAXLINE];
  191.     if(TREND>(&TRTBL[TRSIZ-128]))
  192.         fprintf(STDERR,"\nTR table full");
  193.     if(getwrd(LINE,wrdbuf)==WE_HAVE_A_WORD)
  194.        {switch(toupper(*wrdbuf))
  195.         {case 'B': base=2;break;
  196.         case 'O':
  197.         case 'Q': base=8;break;
  198.         case 'D': base=10;break;
  199.         case 'H': base=16;break;
  200.         default: return(FALSE);    /*error*/
  201.         }
  202.     if DEBUG
  203.     fprintf(STDERR,"\nGETCODE:radix token=<%s>,base=<%d>",
  204.                     wrdbuf, base);
  205.        }
  206.     else return(FALSE);    /*error: missing arg*/
  207.     pcode =TREND++;
  208.     *pcode=ncode = 0;
  209.     while(ncode<127)
  210.     {while(getwrd(LINE,wrdbuf)!=WE_HAVE_A_WORD)
  211.         fgets2(LINE,IOBUF);
  212.     if DEBUG fprintf(STDERR,"\nGETTR: next token is <%s>",
  213.                 wrdbuf);
  214.      if(';'==*wrdbuf) fgets2(LINE,IOBUF);/*comment*/
  215.      else if('.'==*wrdbuf)
  216.         {*pcode = ncode;        /*save #*/
  217.         return(*wrdbuf);
  218.         }
  219.      else    {
  220.         if((code=value(base,wrdbuf)) > -1)
  221.             {*(TREND++) = code;
  222.              ncode++ ;
  223.             }
  224.         else return(*wrdbuf);    /*conversion error*/
  225.         }
  226.     }
  227.     fprintf(STDERR,"\nGETCODE: code sequence too long");
  228.     return(FALSE);
  229. }
  230. /**************************************************/
  231. ocode()    /*process .ou*/
  232. {char wrdbuf[MAXLINE],*pcode,*p;
  233.     getwrd(LINE,wrdbuf);    /*remove .ou*/
  234.     p=pcode=TREND;
  235.     if('.'==getcode()) outstr(p);
  236.     else fprintf(STDERR,"\nOCODE: error in:\n%s",LINE);
  237.     TREND=pcode;
  238. }
  239. /**************************************************/
  240. outstr(p)    /*print string whose bytecount is *p */
  241. char *p;
  242. {int i;
  243. for(i=*(p++); i; i--) putchar(*(p++));
  244. }
  245. /**************************************************/
  246. getfr()    /*process .FR ;cf. ocode() */
  247. {char *pchr,getcode(),wrdbuf[MAXLINE];
  248.     getwrd(LINE,wrdbuf);
  249.     if(getwrd(LINE,wrdbuf)==WE_HAVE_A_WORD)
  250.         FRVAL = atoi(wrdbuf);
  251.     else return;
  252.     FRVAL=max(1,FRVAL); FRVAL=min(FRVAL,4);
  253.     pchr=TREND;
  254.     if('.'==getcode()) FRSTRING=pchr;
  255.     else    {TREND = pchr;
  256.         fprintf(STDERR,"\nError for .FR in:\n%s\n",
  257.             LINE);
  258.         }
  259. }
  260. /**************************************************/
  261. getwh()    /*process .WH ;cf. gettr() */
  262. {char *pcode, getcode(), wrdbuf[MAXLINE];
  263.     getwrd(LINE,wrdbuf);
  264.     pcode = TREND;
  265.     if('.'==getcode()) WHSTRING=pcode;
  266.     else    {TREND = pcode;
  267.         fprintf(STDERR,"\nError for .WH in:\n%s\n",
  268.             LINE);
  269.         }
  270. }
  271.