home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 145_01 / roff45.c < prev    next >
Text File  |  1979-12-31  |  8KB  |  264 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. puttl3(s1,s2,s3,pageno)
  19. char *s1,*s2,*s3;
  20. int pageno;        /*put out three part title, none
  21.             containing '\n', with
  22.             optional page numbering; aligning
  23.             with page margins,0 & OWVAL;*/
  24. {int size1,gap1,size2,gap2,size3,gaps,remain;
  25.     size1 =strln3(s1,FALSE,pageno);
  26.     OUTTOP=LTOP;OUTBOT=LBOT;
  27.     size2 =strln3(s2,FALSE,pageno);
  28.     if(LTOP<OUTTOP) OUTTOP=LTOP;
  29.     size3 =strln3(s3,FALSE,pageno);
  30.     if(LTOP<OUTTOP) OUTTOP=LTOP;
  31.     gaps = max(0,OWVAL-size1-size2-size3);
  32.     gap1 = gaps/2;
  33.     gap2 = gaps-gap1;
  34.     remain=OWVAL;
  35.     if(size1<=remain){puttl(s1,pageno);remain-=size1;}
  36.     if(gap1<remain){blanks(gap1);remain-=gap1;}
  37.     if(size2<=remain){puttl(s2,pageno);remain-=size2;}
  38.     if(gap2<remain){blanks(gap2);remain-=gap2;}
  39.     if(size3<=remain){puttl(s3,pageno);remain-=size3;}
  40.     printout();
  41.     putchar('\r');
  42. }
  43. /****************************************/
  44. blanks(i)
  45. int i;    /*sends i blanks to OUTBUF2*/
  46. {    if(i<0) return;
  47.     for( ; i ; i--) putout( BLANK );
  48. }
  49. /****************************************/
  50. gettl3(sl,ttl1,ttl2,ttl3)
  51. char *sl,ttl1[MAXLINE],**ttl2,**ttl3;
  52.     /* Gets from source line three part title that it
  53.     transfers to buffer delineated by ttl1 which has
  54.     capacity for all three strings; none of the strings
  55.     will contain '\n' */
  56. {char c, cc, *dp;
  57. if DEBUG fprintf(STDERR,"\n\nGETTL3 sl =<%s>",sl);
  58.     /*pass over command*/
  59.     for(c=*sl;c!=' '&&c!='\n'&&c!='\t';sl++)c=*sl;
  60.     /*advance to first non-blank or '\n' */
  61.     for(;c==' '||c=='\t';sl++)c=*sl;
  62.     /*advance beyond delim. if present:*/
  63.     if(c!='\n'&&!c) sl++;
  64.     dp=ttl1;
  65.     transfer(&sl,&dp,c);
  66.     *ttl2=dp;
  67.     transfer(&sl,&dp,c);
  68.     *ttl3=dp;
  69.     transfer(&sl,&dp,c);
  70. if DEBUG
  71.    fprintf(STDERR,"\ndelim=<%c>\nT1=<%s>\nT2=<%s>\nT3=<%s>;\n",
  72.                   c,ttl1,*ttl2,*ttl3);
  73. }
  74. /****************************************/
  75. transfer(s,d,c)
  76. char c;        /*terminal character*/
  77. char **s;    /*source string*/
  78. char **d;    /*destination string*/
  79. /* Copy string from source to destination.  Original delim.
  80. can be \0, \n, or char.  The pointer to the source is updated
  81. to point at the \0, \n, or past char.  In destination, delim.
  82. is always replaced by \0.  The destination pointer always
  83. points past this \0. */
  84. {char a;
  85.     a=**s;
  86.     while(a!=c && a!='\n' && a)
  87.         {**d = a;  (*d)++;
  88.         (*s)++;  a=**s;
  89.         }
  90.     **d='\0';
  91.     (*d)++;
  92.     if(a!='\n' && a!='\0') (*s)++;
  93. }
  94. /**********************************************************
  95.         centers a line of text
  96. **********************************************************/
  97. center (line)
  98. char *line;
  99. {TIVAL = max(( RMVAL+TIVAL-strln3(line,FALSE,1))/2, 0 );
  100. OUTTOP=LTOP;OUTBOT=LBOT;
  101. return;
  102. }
  103. /************************************************************
  104. Revised April 24,83.Transfers next word from in to out.  Scans
  105. off leading white space from in.  If there is no word, returns
  106. FALSE.  Otherwise, input is truncated on left of word which is
  107. transfered to out without leading or trailling blanks.
  108. WE_HAVE_A_WORD will be returned.  If the transfered word
  109. terminates a sentence then SENTENCE is set to 1, otherwise it
  110. is reset to FALSE.
  111. **************************************************************/
  112. int getwrd (in,  out )
  113. char *in, *out;
  114. {char *pin,*pout,c,cm,cp;
  115.     skip_blanks(in);
  116.     replace_char(in,TAB,BLANK);
  117.     pin=in;
  118.     pout=out;
  119.     c=*pin;
  120.     SENTENCE=FALSE;
  121.     if(c==NEWLINE || c=='\0')
  122.         {*out='\0';
  123.         if DEBUG fprintf(STDERR,"\ngetwrd=<%s>",out);
  124.         return(FALSE);
  125.         }
  126.     while(c!=BLANK && c!=NEWLINE && c)
  127.         {*(pout++)=c;
  128.         *pin=BLANK;
  129.         c=*(++pin);
  130.         }
  131.     *pout='\0';    /*terminate out string*/
  132.     cm=*(pout-1);
  133.     cp=*(pin+1);
  134.     switch (cm)
  135.         {case ':' :
  136.         case ';' :
  137.         case '?' :
  138.         case '!' :
  139.             SENTENCE=1;
  140.             break;
  141.         case '.' :
  142.             if(cp==BLANK||cp==NEWLINE||c==NEWLINE)
  143.                 SENTENCE=1;
  144.         }
  145.     if DEBUG fprintf(STDERR, "\ngetwrd=<%s>",out);
  146.     return(WE_HAVE_A_WORD);
  147. }
  148. /*******************************************************
  149. Truncates white-space characters at the end of a string.
  150. ********************************************************/
  151. trunc_bl (string)
  152. char *string;
  153. {char *ptr;
  154. int k;
  155. k = strlen (string);
  156. ptr = &string[ k-1 ];    /* char before terminating nul */
  157. while (*ptr==BLANK || *ptr==TAB || *ptr==NEWLINE)    
  158.     *ptr--  = '\0';
  159. }
  160. /*********************************************
  161.     distribute words evenly across a line
  162. **********************************************/
  163. spread ( line, nextra, no_words)
  164. char *line;
  165. int nextra;    /* no. extra places left in line */
  166. int no_words;   /* no. words in the line         */
  167. {int i, j, nblanks, nholes;
  168. if DEBUG fprintf(STDERR,"spread:line=<%s>,\nnextra=%d, no_words=%d\n",
  169.                           line,      nextra,    no_words   );
  170. if (nextra <= 0 || no_words <= 1)    return;
  171. DIR = !(DIR);
  172. nholes = no_words - 1;
  173. trunc_bl (line);
  174. i = strlen(line) - 1 ; /* last character of string */
  175. j = min(MAXLINE-2,i+nextra);    /* last  position in output */
  176. line[j+1] = '\0';
  177. for ( ; i<j ; i--, j-- )
  178.       { line[j] = line[i];
  179.     if ( line[i] == BLANK)
  180.           { if (DIR == 0) nblanks=(nextra-1)/nholes+1;
  181.         else        nblanks = nextra/nholes;
  182.         nextra = nextra - nblanks;
  183.         nholes = nholes - 1;
  184.         for ( ; nblanks > 0;  nblanks-- )
  185.             line[--j] = BLANK;
  186.           }
  187.       }
  188. }
  189. /************************************************
  190. place portion of title line with optional page no. in OUTBUF2
  191. *************************************************/
  192. puttl ( str, num )
  193. char *str;
  194. int num;
  195. {int i;
  196. char c;
  197. for (i=0;c= *str; str++)
  198.     {if ( c != NUMSIGN ) putout(c);
  199.     else putnum(num);
  200.     }
  201. }
  202. /*******************************************
  203.     put out num to OUTBUF2 (conversion)
  204. ********************************************/
  205. putnum ( num, w )
  206. int num;
  207. {int i, nd;
  208. char chars[10];
  209. nd = itoc ( num, chars, 10 );
  210. for ( i=0;i<nd; i++) putout(chars[i]);
  211. }
  212. /************************************************
  213.     convert int num to char string in numstr
  214. *************************************************/
  215. itoc ( num, numstr, size )
  216. int num;
  217. char *numstr;
  218. int size;    /* largest size of numstr */
  219. {int absnum, i, j, k, d;
  220. absnum = abs (num);
  221. numstr[0] = '\0';
  222. i = 0;
  223. do      { i++;
  224.     d = absnum % 10;
  225.     numstr[i] = d + '0';
  226.     absnum = absnum/10;
  227.     }
  228. while ( absnum != 0 && i<size );
  229. if ( num < 0 && i<size )
  230.       { i++;
  231.     numstr[i] = '-';
  232.       }
  233. for( j=0; j<i; j++ )
  234.       { k = numstr[i];
  235.     numstr[i] = numstr[j];
  236.     numstr[j] = k;
  237.     i--;
  238.       }
  239. return ( strlen(numstr) );
  240. }
  241. /****************************************/
  242. putout(c)    /*places c in OUTBUF2[]*/
  243. char c;
  244. {if(c==SCVAL) c= BLANK;
  245. if(c==NEWLINE)c='\0';
  246. OUTBUF2[BPOS++]=c;
  247. OUTBUF2[BPOS]='\0';/*safty net*/
  248. }
  249. /************************************
  250.     replace c1 in string with c2
  251. *************************************/
  252. replace_char (string, c1, c2)
  253. char *string, c1, c2;
  254. {int i;
  255. for (i=0; string[i]; i++)
  256.     if (string[i] == c1)    string[i] = c2;
  257. }
  258.  
  259. #if C86
  260. abs(n)
  261. {
  262.     return (n < 0 ? -n : n);
  263. }
  264.