home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compiler / small_c / cb / sources / fmt3.c < prev    next >
Encoding:
Text File  |  1985-08-11  |  13.5 KB  |  594 lines

  1. /*
  2. ** fmt3.c -- text formatter part 3
  3. */
  4.  
  5. /*
  6. ** put -- put out line with proper spacing and indenting
  7. */
  8. put(buf) char buf[]; {
  9.   int i;
  10.   if((buf[0]==NULL)|(buf[0]==' ')) bline=YES;
  11.   else {
  12.     if(((bottom-lineno+1) < (mpval*lsval)) &&
  13.        (bline==YES) &&
  14.        (mpcnt >= mpval)) {
  15.       mpcnt=0;
  16.       skip(bottom-lineno+1);
  17.       lineno=bottom+1;   /* force footing */
  18.       }
  19.     bline=NO;
  20.     }
  21.   ++mpcnt;
  22.   if(lineno>bottom) pfoot();
  23.   if(lineno==0) phead();
  24.   i=poval;  /** minus on odd pages **/
  25.   while(++i <= (lmval+tival+inval+sqval))
  26.     print(" ", NO);
  27.   tival=0;
  28.   print(buf, NO);
  29.   if((lsval-1) < (bottom-lineno)) skip(lsval);
  30.   else skip(bottom-lineno+1);
  31.   if(plval > 0) lineno=lineno+lsval;
  32.   if(lineno > bottom) pfoot();
  33.   }
  34.  
  35. /*
  36. ** print -- print output to stdout if not skipping pages
  37. */
  38. print(str, title) char *str; int title; {
  39.   while(*str) {
  40.     if(*str == 1) {
  41.       if(!title) onoff(*(str-1));
  42.       }
  43.     else {
  44.       /* skip output of control sequence terminator */
  45.       if((plval==0)|((curpag >= begin)&(pagekill==NO))) {
  46.         begin=1;  /** future copies begin on page 1 **/
  47.         if(*str != blank) cout(*str, stdout);
  48.         else cout(' ', stdout);
  49.         }
  50.       else if(*str == 27) {
  51.         while(*str != 1) cout(*str++, stdout);
  52.         continue;
  53.         }
  54.       }
  55.     ++str;
  56.     }
  57.   }
  58.  
  59. /*
  60. ** onoff -- set and clear xxon and xxoff pointers
  61. */
  62. onoff(ch) int ch; {
  63.        if(ch == ulpref[2]) {ulon=ulpref; uloff=ulsuff;}
  64.   else if(ch == ulsuff[2]) ulon=uloff=0;
  65.   else if(ch == bfpref[1]) {bfon=bfpref; bfoff=bfsuff;}
  66.   else if(ch == bfsuff[1]) bfon=bfoff=0;
  67.   else if(ch == dspref[1]) {dson=dspref; dsoff=dssuff;}
  68.   else if(ch == dssuff[1]) dson=dsoff=0;
  69.   else if(ch == itpref[1]) {iton=itpref; itoff=itsuff;}
  70.   else if(ch == itsuff[1]) iton=itoff=0;
  71.   else if(ch == dwpref[1]) {dwon=dwpref; dwoff=dwsuff;}
  72.   else if(ch == dwsuff[1]) dwon=dwoff=0;
  73.   }
  74.  
  75. /*
  76. ** skip -- output n blank lines
  77. */
  78. skip(n) int n; {
  79.   while((n--) > 0) print("\n", NO);
  80.   }
  81.  
  82. /*
  83. ** phead -- put out header
  84. */
  85. phead() {
  86.   pagekill=NO;
  87.   if((curpag=newpag++) >= begin) {
  88.     if(page(curpag, pause, stdout)==KILL) {
  89.       pagekill=YES;
  90.       fputc('\n', stderr);
  91.       }
  92.     }
  93.   if(curpag%2) poval = -pospec;
  94.   else poval = pospec;
  95.   if(m1val > 0) {
  96.     skip(m1val-1);
  97.     if(curpag%2) puttl(oheader, curpag);
  98.     else puttl(eheader, curpag);
  99.     }
  100.   skip(m2val);
  101.   lineno=m1val+m2val+1;
  102.   }
  103.  
  104. /*
  105. ** pfoot -- put out footer
  106. */
  107. pfoot() {
  108.   skip(m3val);
  109.   if(m4val > 0) {
  110.     if(curpag%2) puttl(ofooter, curpag);
  111.     else puttl(efooter, curpag);
  112.     }
  113.   if((curpag >= end)&((ecopy==HUGE)|(ecopy==pass))) {
  114.     fclose(stdout);
  115.     exit(0);
  116.     }
  117.   if(pause==NO) skip(m4val-1);
  118.   lineno=0;
  119.   }
  120.  
  121. /*
  122. ** puttl -- put out title line with optional page number
  123. */
  124. puttl(buf, pageno) char buf[]; int pageno; {
  125.   char str[2];
  126.   int i, j, k ,parts;
  127.   str[1]=NULL;
  128.   i=poval;  /** minus on odd pages **/
  129.   while(++i <= lmval) print(" ", YES);
  130.   parts=1;
  131.   i=k=0;
  132.   while(buf[i]!=NULL) {
  133.     if(buf[i]==PAGENUM) {
  134.       itod(pageno, nbrstr, - NWIDTH);
  135.       j=0;
  136.       while(nbrstr[j]==' ') ++j;
  137.       while(j < NWIDTH) {
  138.         if(k < (MAXOUT-1)) ttlbuf[k++]=nbrstr[j];
  139.         ++j;
  140.         }
  141.       }
  142.     else {
  143.       if(buf[i]==TTLDELIM) ++parts;
  144.       if(k < (MAXOUT-1)) ttlbuf[k++]=buf[i];
  145.       }
  146.     ++i;
  147.     }
  148.   ttlbuf[k]=NULL;
  149.   spread(ttlbuf, k+1, MAXOUT, rmval-lmval-k, parts, TTLDELIM);
  150.   /* disable running ul, it, dw, ds, or bf */
  151.   if(uloff) print(uloff, YES);
  152.   if(itoff) print(itoff, YES);
  153.   if(dsoff) print(dsoff, YES);
  154.   if(bfoff) print(bfoff, YES);
  155.   if(dwoff) print(dwoff, YES);
  156.   print(ttlbuf, YES);
  157.   /* reenable running ul, it, dw, ds, or bf */
  158.   if(ulon) print(ulon, YES);
  159.   if(iton) print(iton, YES);
  160.   if(dson) print(dson, YES);
  161.   if(bfon) print(bfon, YES);
  162.   if(dwon) print(dwon, YES);
  163.   print("\n", YES);
  164.   }
  165.  
  166. /*
  167. ** gettl -- copy title from buf to ttl
  168. */
  169. gettl(buf, ttl) char *buf, *ttl; {
  170.   int i;
  171.   i=0;
  172.   while((buf[i]!=' ')&(buf[i]!='\t')&(buf[i]!=NULL)) ++i;
  173.   skipbl(buf, &i);
  174.   if((buf[i]=='\'')|(buf[i]=='"')) ++i; /** strip leading quote **/
  175.   buf=buf+i;
  176.   while(*ttl++ = *buf++);
  177.   }
  178.  
  179. /*
  180. ** space -- space n lines or to bottom
  181. */
  182. space(n) int n; {
  183.   brk();
  184.   if(lineno > bottom) return;
  185.   if(lineno==0) {
  186.     phead();
  187.     if(n==HUGE) return;
  188.     }
  189.   if(n < (bottom+1-lineno)) skip(n);
  190.   else skip(bottom+1-lineno);
  191.   if(plval > 0) lineno=lineno+n;
  192.   if(lineno > bottom) pfoot();
  193.   }
  194.  
  195. /*
  196. ** leadbl -- delete leading blanks, set tival
  197. */
  198. leadbl(buf) char buf[]; {
  199.   int i, j;
  200.   brk();
  201.   i=0;
  202.   while(buf[i]==' ') ++i;
  203.   if(buf[i]!=NULL) tival = tival + i;
  204.   j=0;
  205.   while(buf[j++]=buf[i++]);
  206.   }
  207.  
  208. /*
  209. ** width -- compute width of character string
  210. */
  211. width(buf) char *buf; {
  212.   int wide;
  213.   wide=0;
  214.   while(*buf) {
  215.     if(*buf==27) {
  216.       while(*buf++ != 1) ;
  217.       continue;
  218.       }
  219.     if(*buf == '\b') --wide;
  220.     else if(*buf != '\n') {
  221.       ++wide;
  222.       if(dwact && !isspace(*buf)) ++wide;
  223.       }
  224.     ++buf;
  225.     }
  226.   return (wide);
  227.   }
  228.  
  229. /*
  230. ** brk -- end current filled line
  231. */
  232. brk() {
  233.   if(outp > -1) {
  234.     outbuf[outp]=NULL;
  235.     outp=-1;
  236.     put(outbuf);
  237.     }
  238.   outw=outwds=0;
  239.   }
  240.  
  241. /*
  242. ** putwrd -- put a word in outbuf (does margin justification)
  243. */
  244. putwrd(wrdbuf) char wrdbuf[]; {
  245.   int last, llval, nextra, w, i;
  246.   w=width(wrdbuf);
  247.   last=strlen(wrdbuf)+outp+1;
  248.   llval=rmval-lmval-tival-inval-(sqval<<1);
  249.   if((outp > -1)&((outw+w > llval)|(last >= MAXOUT))) {
  250.     last=last-outp-1;
  251.     if(just!=YES) nextra=0;
  252.     else nextra=llval-outw+1;
  253.     spread(outbuf, outp, MAXOUT, nextra, outwds, ' ');
  254.     if((nextra > 0)&(outwds > 1)) outp=outp+nextra;
  255.     brk();
  256.     }
  257.   if(outp > 0 && cuact > 1 && !itsub && ttymode)
  258.     outbuf[outp]='_';
  259.   else {
  260.     ++outwds;
  261.     if(cuact==1) cuact=2;
  262.     }
  263.   if((outp < 0)&(wrdbuf[0]==' ')) i=1; else i=0;
  264.   scopy(wrdbuf+i, 0, outbuf, outp+1);
  265.   outp=last-i;
  266.   outbuf[outp]=' ';
  267.   outw=outw+w+1-i;
  268.   }
  269.  
  270. /*
  271. ** spread -- spread words to justify right margin
  272. */
  273. spread(buf, outp, max, nextra, outwds, gapid)
  274.   char buf[], gapid; int outp, max, nextra, outwds; {
  275.   int i, j, nb, ne, nholes;
  276.   if((nextra <= 0)|(outwds <= 1)) return;
  277.   dir=1-dir; /* reverse prev direction */
  278.   ne=nextra;
  279.   nholes=outwds-1;
  280.   i=outp-1;
  281.   if((max-2) < (i+ne)) j=max-2;
  282.   else j=i+ne;
  283.   while(i < j) {
  284.     if(buf[i]==gapid) {
  285.       buf[j]=' ';
  286.       if(dir==0) nb=(ne-1)/nholes + 1;
  287.       else nb=ne/nholes;
  288.       ne=ne-nb;
  289.       nholes=nholes-1;
  290.       while((nb--) > 0)
  291.         buf[--j]=' ';
  292.       }
  293.     else buf[j]=buf[i];
  294.     --i;
  295.     --j;
  296.     }
  297.   }
  298.  
  299. /*
  300. ** center -- center a line by setting tival
  301. */
  302. center(buf) char buf[]; {
  303.   int i, j;
  304.   j = -lmval - inval - sqval;
  305.   if((i=((rmval-lmval-width(buf))/2)-inval-sqval) < j)
  306.        tival = j;
  307.   else tival = i;
  308.   }
  309.  
  310. /*
  311. ** underl -- underline a line
  312. */
  313. underl(buf, tbuf, size) char buf[], tbuf[]; int size; {
  314.   char c, *ptr;
  315.   int prefixed, i, j;
  316.   if(*buf == 0) return;
  317.   if(!ttymode) prefixed=NO;
  318.   else prefixed=9;  /* neither YES nor NO */
  319.   i=j=0;
  320.   while((buf[i]!=NULL)&(j<(size-2))) {
  321.     c=buf[i++];
  322.     if(index(ulskips, c) < 0) {
  323.       if(prefixed==NO) {
  324.         prefixed=YES;
  325.         ptr=ulpref;
  326.         while((tbuf[j++]=*ptr++) != 1) ;
  327.         }
  328.       else if(ttymode) {
  329.         tbuf[j++]='_';
  330.         tbuf[j++]='\b';
  331.         }
  332.       }
  333.     else {
  334.       if(prefixed==YES) {
  335.         prefixed=NO;
  336.         ptr=ulsuff;
  337.         while((tbuf[j++]=*ptr++) != 1) ;
  338.         }
  339.       if(cuact>0 && fill==NO && c==' ' && ttymode) c='_';
  340.       }
  341.     tbuf[j++]=c;
  342.     }
  343.   if(prefixed==YES) {
  344.     ptr=ulsuff;
  345.     while((tbuf[j++]=*ptr++) != 1) ;
  346.     }
  347.   tbuf[j]=NULL;
  348.   scopy(tbuf, 0, buf, 0);
  349.   }
  350.  
  351. /*
  352. ** bold -- boldface a line
  353. */
  354. bold(buf, tbuf, size) char buf[], tbuf[]; int size; {
  355.   char *ptr;
  356.   int c, i, j;
  357.   if(*buf == 0) return;
  358.   i=j=0;
  359.   if(!ttymode) {
  360.     ptr=bfpref;
  361.     while((tbuf[j++]=*ptr++) != 1) ;
  362.     ptr=dspref;
  363.     while((tbuf[j++]=*ptr++) != 1) ;
  364.     }
  365.   while((buf[i]!=NULL)&(j<(size-2))) {
  366.     c=tbuf[j++]=buf[i++]&255;
  367.     if(ttymode && (c > ' ') && (c < 127)) {
  368.       int i;
  369.       i = bfstrikes;
  370.       while (--i) {
  371.         tbuf[j++]='\b';
  372.         tbuf[j++]=c;
  373.         }
  374.       }
  375.     }
  376.   if(!ttymode) {
  377.     ptr=bfsuff;
  378.     while((tbuf[j++]=*ptr++) != 1) ;
  379.     ptr=dssuff;
  380.     while((tbuf[j++]=*ptr++) != 1) ;
  381.     }
  382.   tbuf[j]=NULL;
  383.   scopy(tbuf, 0, buf, 0);
  384.   }
  385.  
  386. /*
  387. ** italic -- italicize a line
  388. */
  389. italic(buf, tbuf, size) char buf[], tbuf[]; int size; {
  390.   char *ptr;
  391.   int i, j;
  392.   if(*buf == 0) return;
  393.   if(ttymode) return;
  394.   i=j=0;
  395.   ptr=itpref;
  396.   while((tbuf[j++]=*ptr++) != 1) ;
  397.   ptr=bfpref;
  398.   while((tbuf[j++]=*ptr++) != 1) ;
  399.   while((buf[i]!=NULL)&(j<(size-2)))  tbuf[j++]=buf[i++];
  400.   ptr=itsuff;
  401.   while((tbuf[j++]=*ptr++) != 1) ;
  402.   ptr=bfsuff;
  403.   while((tbuf[j++]=*ptr++) != 1) ;
  404.   tbuf[j]=NULL;
  405.   scopy(tbuf, 0, buf, 0);
  406.   }
  407.  
  408. /*
  409. ** double -- double-wide a line (nontty mode only)
  410. */
  411. double(buf, tbuf, size) char buf[], tbuf[]; int size; {
  412.   char *ptr;
  413.   int space, i, j;
  414.   if(!*buf || ttymode) return;
  415.   space = YES;
  416.   i = j = 0;
  417.   while(buf[i] && j < (size-3)) {
  418.     if(isspace(buf[i])) {
  419.       if(!space) {
  420.         ptr=dwsuff;
  421.         while((tbuf[j++]=*ptr++) != 1) ;
  422.         }
  423.       space = YES;
  424.       }
  425.     else {
  426.       if(space) {
  427.         ptr=dwpref;
  428.         while((tbuf[j++]=*ptr++) != 1) ;
  429.         }
  430.       space = NO;
  431.       }
  432.     tbuf[j++]=buf[i++];
  433.     }
  434.   if(!space) {
  435.     ptr=dwsuff;
  436.     while((tbuf[j++]=*ptr++) != 1) ;
  437.     }
  438.   tbuf[j]=NULL;
  439.   scopy(tbuf, 0, buf, 0);
  440.   }
  441.  
  442. /*
  443. ** supersub -- process super- & sub-scripts
  444. */
  445. supersub(buf, tbuf, size) char buf[], tbuf[]; int size; {
  446.   char *pref, *suff, *ptr;
  447.   int i, j;
  448.   if(!*buf) return;
  449.   i = j = suff = 0;
  450.   while(buf[i] && j < (size-3)) {
  451.     if(isspace(buf[i]) && suff) {
  452.       while((tbuf[j++] = *suff++) != 1) ;
  453.       suff = 0;
  454.       }
  455.     if(buf[i] == SUBCHAR && buf[i+1] == SUBCHAR) {
  456.       if(!ttymode) {
  457.         if(suff) {ptr = suff;   suff = 0;}
  458.         else     {ptr = sbpref; suff = sbsuff;}
  459.         while((tbuf[j++] = *ptr++) != 1) ;
  460.         }
  461.       i += 2;
  462.       }
  463.     else if(buf[i] == SUPCHAR && buf[i+1] == SUPCHAR) {
  464.       if(!ttymode) {
  465.         if(suff) {ptr = suff;   suff = 0;}
  466.         else     {ptr = sppref; suff = spsuff;}
  467.         while((tbuf[j++] = *ptr++) != 1) ;
  468.         }
  469.       i += 2;
  470.       }
  471.     else tbuf[j++] = buf[i++];
  472.     }
  473.   if(suff) while((tbuf[j++] = *suff++) != 1) ;
  474.   tbuf[j] = NULL;
  475.   scopy(tbuf, 0, buf, 0);
  476.   }
  477.  
  478. /*
  479. ** doargs -- process command line arguments
  480. */
  481. doargs(argc, argv) int argc, *argv; {
  482.   char arg[MAXFN];
  483.   int i, err;
  484.   show=err=ttymode=itsub=ulsub=NO;
  485.   pause=ready=YES;
  486.   bfstrikes=3;
  487.   pospec=mrgfd=0;
  488.   mrgbuf[0]=NULL;
  489.   begin=bcopy=1;
  490.   end=ecopy=HUGE;
  491.   i=0;
  492.   while(getarg(++i, arg, MAXFN, argc, argv)!=EOF) {
  493.     if(arg[0]=='-') {
  494.       if(same(arg[1], 'n') & same(arg[2], 'p')) {
  495.         pause=NO;
  496.         continue;
  497.         }
  498.       if(same(arg[1], 'n') & same(arg[2], 'r')) {
  499.         ready=NO;
  500.         continue;
  501.         }
  502.       if(same(arg[1], 's') & arg[2] == NULL) {
  503.         show = YES;
  504.         continue;
  505.         }
  506.       if(same(arg[1], 't') & arg[2]==NULL) { /** tty mode **/
  507.         ttymode=YES;
  508.         continue;
  509.         }
  510.       if(same(arg[1], 'i') & arg[2] == NULL) {
  511.         /* italics replace underlines */
  512.         itsub=YES;
  513.         continue;
  514.         }
  515.       if(same(arg[1], 'u') & arg[2] == NULL) {
  516.         /* underlines replace italics */
  517.         ulsub=YES;
  518.         continue;
  519.         }
  520.       if(same(arg[1], 'b')) {
  521.         if(same(arg[2], 'p')) {  /** begin page # **/
  522.           if(utoi(arg+3, &begin) > 0) continue;
  523.           }
  524.         else if(same(arg[2], 'c')) {  /** copy # **/
  525.           if(utoi(arg+3, &bcopy) > 0) continue;
  526.           }
  527.         else if(same(arg[2], 's')) {  /** boldface strikes **/
  528.           if(utoi(arg+3, &bfstrikes) > 0) continue;
  529.           }
  530.         }
  531.       if(same(arg[1], 'e')) {   /** end  **/
  532.         if(same(arg[2], 'p')) {  /** page # **/
  533.           if(utoi(arg+3, &end) > 0) continue;
  534.           }
  535.         else if(same(arg[2], 'c')) {  /** copy # **/
  536.           if(utoi(arg+3, &ecopy) > 0) continue;
  537.           }
  538.         }
  539.       if(same(arg[1], 'p') & same(arg[2], 'o')) {  /** page offset **/
  540.         if(utoi(arg+3, &pospec) > 0) continue;
  541.         }
  542.       err=YES;
  543.       }
  544.     else  {
  545.       if((mrgfd=fopen(arg, "r"))==NULL) cant(arg);
  546.       continue;
  547.       }
  548.     err=YES;
  549.     }
  550.   if(err==YES) {
  551.     fputs("usage: FMT [mergefile] [-BC#] [-EC#]\n",stderr);
  552.     fputs("    [-BP#] [-EP#] [-PO#] [-NP] [-NR]\n",stderr);
  553.     fputs("    [-T] [-I] [-U] [-S] [-BS#]\n",stderr);
  554.     abort(7);
  555.     }
  556.   }
  557.  
  558. /*
  559. ** merge -- merge fields from mrgbuf into inbuf
  560. */
  561. merge() {
  562.   int i, j, k, l, m, merged;
  563.   if(mrgbuf[0]==NULL) return YES;
  564.   scopy(inbuf, 0, ttlbuf, 0);
  565.   merged=NO;
  566.   i=j=0;
  567.   while(inbuf[i]=ttlbuf[j++]) {
  568.     if(inbuf[i++]==mrg) {
  569.       if((l=utoi(ttlbuf+j, &k)) < 1) continue;
  570.       if(ttlbuf[j+l]!=mrg) continue;
  571.       --i;
  572.       j=j+l+1;
  573.       m=0;
  574.       while(--k) {
  575.         while(mrgbuf[m]) {
  576.           if(mrgbuf[m++]==mrg) break;
  577.           }
  578.         }
  579.       while((mrgbuf[m]!=NULL)&(mrgbuf[m]!=mrg)) {
  580.         inbuf[i++]=mrgbuf[m++];
  581.         if(i >= (INSIZE-1)) break;
  582.         }
  583.       merged=YES;
  584.       }
  585.     }
  586.   if(merged==NO) return YES;
  587.   i = -1;
  588.   while(inbuf[++i]) {
  589.     if((inbuf[i]!=' ')&(inbuf[i]!='\t')) return YES;
  590.     }
  591.   return NO;
  592.   }
  593. 
  594.