home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 304_01 / roff5.c < prev    next >
Text File  |  1990-02-14  |  10KB  |  319 lines

  1. /********************************************************/
  2. /*         ROFF5, Version 2.00            */
  3. /*    (c) 1983,4,8,9 by Ernest E. Bergmann        */
  4. /*    730 Seneca Street, Bethlehem, Pa. 18015        */
  5. /*                            */
  6. /* Permission is hereby granted for all commercial and    */
  7. /* non-commercial reproduction and distribution of this    */
  8. /* material provided this notice is included.        */
  9. /********************************************************/
  10. #include <roff5.h>
  11. #include <mem.h>
  12. #include <ctype.h>
  13. #if (VERSION!=2) || (subVERSION!=00)
  14. #error ***************This is version 2.00******************
  15. #endif
  16. ENTRY    SLINK, /* string substitution table */ /*insert(), showit()*/
  17.     MLINK, /* macro and diversion substitution table */
  18.         /*minsert(),showm(),macq1()*/
  19.         /*SLINK,MLINK table entries are following ident string
  20.             with second string used as replacement*/
  21.     RLINK; /* register substitution table;
  22.            ident string followed by int*/ /*showr(),regist()*/
  23. /* typical addition of new entries consists of:
  24.     entry *pe;
  25.     char  *pc;
  26.     char buff[?];
  27.     unsigned siz;
  28.     ENTRY *where;
  29.     pe = (ENTRY *)buff;
  30.     pe->link = ?LINK.link;
  31.     pc = &pe.ident;
  32.     pc++ = stpcpy(pc, NAME);
  33.         Now pc points to where info should be placed;
  34.     Compute actual siz;
  35.     if (NULL==(where=(ENTRY *)malloc(siz)))
  36.         exitm("ERROR MESSAGE cannot malloc\n");
  37.     ?LINK.link = (ENTRY *)memcpy(where,buff,siz);
  38. */
  39. int INPUTlns, INTRAP;
  40. char ITstring[LSZ];
  41. char EMstring[LSZ];
  42. int PAGESTOP=FALSE;
  43. int JUSTIFY=JU_INI;
  44. int DOTJ = 1; /*both margins*/
  45. int FFEED=FF_INI;
  46. int FIRSTPAGE=1,LASTPAGE=30000;/*for selectively printing output*/
  47. int SUPPRESS;/*if true,no output is passed by putchar()*/
  48.  
  49. int FILL=YES;
  50. int LSVAL[STKSIZ+1];    /* line spacing value -> default will be 1 */
  51. int TIVAL=TI_DEF;    /* temporary indent -> default  0 */
  52. int INVAL=0;    /* left indent -> default  0 */
  53. int RMVAL=65;    /* right margin -> default  PAGEWIDTH */
  54. int CEVAL=0;    /* set equal to number of lines to be centered    */
  55. int TFVAL[STKSIZ+1]; /*translation flag char*/
  56.         /* default is 0                                   */
  57. int SPVAL;    /* blank lines to be spaced down */
  58. int CURPAG=0;    /* current output page number */
  59. int NEWPAG=1;    /* next output page number */
  60. FORMAT PAGFMT = {' '/*typ*/, ' '/*fill*/, 1/*minwidth*/};
  61. int VLINENO=-1;    /* virtual (intended) line advances on page,
  62.             see vadv()*/
  63. int FVLINENO=0;    /* + line fraction */
  64. int PLINENO=0;    /* printer's actual line advances on page,
  65.             see padv()*/
  66. int FPLINENO=0;    /* + line fraction */
  67. int PLVAL[STKSIZ+1]; /* page length in lines */
  68. int M1VAL[STKSIZ+1]; /* margin before& including header in lines*/
  69. int M2VAL[STKSIZ+1]; /* margin after header in lines*/
  70. int M3VAL[STKSIZ+1]; /* margin after last text line in lines*/
  71. int M4VAL[STKSIZ+1]; /* bottom margin, including footer in lines*/
  72. int TRAPLOC;    /* location of next page trap in lines */
  73. int FOOTLOC;    /* location of page footer */
  74. int SCVAL[STKSIZ+1]; /* space character*/
  75. int SENTENCE;    /* Nov 20*/
  76. int CFVAL[STKSIZ+1]; /* control flag character value*/
  77. int ICVAL[STKSIZ+1]; /*insert character*/
  78. int EVVAL[STKSIZ+1]; /*environments*/
  79.  
  80. char LINE[ LSZ ];    /*input line buffer */
  81. char *LIN = &LINE[0];    /*variable start of LINE*/
  82. char EHEAD[ LSZ ],*EH2=EHEAD,*EH3=EHEAD;    /* even header title */
  83. char OHEAD[ LSZ ],*OH2=OHEAD,*OH3=OHEAD;    /* odd header title */
  84. char EFOOT[ LSZ ],*EF2=EFOOT,*EF3=EFOOT;    /* even footer title */
  85. char OFOOT[ LSZ ],*OF2=OFOOT,*OF3=OFOOT;    /* even footer title */
  86. int CZSUB=CZSUB_DEF;                /* ^Z substitute char */
  87. char RULER[ LSZ ];                /* cf WS to store tabs*/
  88. int LCVAL='.';
  89. int TCVAL=BLANK;
  90. FILE *ifp, *ofp;
  91. /*following added for buffered and formatted output:  */
  92. char OUTBUF2[LSZ];    /*for line to be output in fancy fmt*/
  93. int BPOS=0,CP,PP;        /*buffer,column,printer positions*/
  94. char XBUF[LSZ];        /*strikout buffer*/
  95. int XF=FALSE,XCOL;        /* " flag and column */
  96. char UBUF[LSZ];        /*underline buffer*/
  97. int UF=FALSE,UCOL;        /* " flag and column */
  98. int FIRST;            /*flag for first pass*/
  99. char DBUF[LSZ];        /*double strike buffer*/
  100. int dotO= 0;        /* .o page offset */
  101. int DPOS=-1;
  102. int OCNT;
  103. int MCNT=1;
  104. int BLKCNT;
  105. int DIR=0;            /* for "spreading" of lines    */
  106. int OUTWRDS=0;            /* no. words in OUTBUF */
  107. char OUTBUF[ LSZ ];        /*lines to be filled collected here */
  108. int OUTW=0;            /*current display width of OUTBUF*/
  109. int OUTPOS=0;            /* =strlen(OUTBUF) */
  110. int WTOP,LTOP,OUTTOP=0;        /*zero|negative;levels of subscripts*/
  111. int WBOT,LBOT,OUTBOT=0;        /*zero|positive;levels of subscripts*/
  112. int OLDLN=0;                /*position of previous main line*/
  113. int OLDBOT=0;                /*OUTBOT for previous line;reset
  114.                             each page*/
  115. int FRQ=0,FRVAL=1;        /* fractional line?, what fraction size*/
  116. char *FRSTRING = 0;         /* ^ to code for fractional spacing*/
  117. char *WHSTRING = 0;            /* ^ to code for whole line spacing*/
  118. char *CPTR[128-' '];    /*pointer table for print control*/
  119. char *TPTR[128-' '];    /*pointer table for translation strings
  120.                               for char; initialize to null */
  121. char BACKBUF0[BBSIZ0];
  122. int BBSIZ=BBSIZ0;
  123. char *BACKBUF=BACKBUF0;
  124. int BINP;        /*position in above;init to 0*/
  125. char KEYBD=FALSE;    /*boolean & prompt for keyboard input*/
  126. char KLINE[MAXLINE];    /*keyboard line input buffer*/
  127. char *KPTR=KLINE;    /*pointer for above*/
  128.  
  129. int    FPTR=0;
  130. FILEF FSTACK[FMAX];
  131. int XF2,UF2,MCNT2;
  132. int DLINES=0;
  133. int DIVERTING=FALSE;
  134. int NUMBERING=FALSE;
  135. int NNVAL=0;
  136. int LN=0;
  137. int LNMOD=1;
  138. int LNI=0;
  139. int LNST=1;
  140.  
  141. /******************/
  142. /*  MAIN - ROFF5  */
  143. /******************/
  144. void main(int argc,char **argv)
  145. {char option,*pc;
  146. char filename[FNAMSIZ];
  147. int i;
  148.  
  149. fprintf(stderr,"ROFF5 v2.00; MS-DOS, TurboC %s\n",__DATE__);
  150. fprintf(stderr,"         (c) 1983,4,8,9 by\n");
  151. fprintf(stderr,"E. E. Bergmann, 730 Seneca Street\n");
  152. fprintf(stderr,"      Bethlehem, PA 18015\n\n");
  153. ofp=stdout;
  154. SUPPRESS=FALSE;
  155. if (argc == 1) exitm("USAGE:  ROFF5 file1\n  more than one file OK\n");
  156. for(i=1;i<argc;i++){
  157.     strcpy (filename,argv[i]);
  158.     if(filename[0]==')') strcpy(filename,"}prn");
  159.      if(filename[0]=='}'){
  160.          fprintf(stderr,"Opening output file <%s>\n",&filename[1]);
  161.         if(( ofp = fopen(&filename[1],"wb")) == ERROR){
  162.           fprintf(stderr,"Error on opening <%s>\n",&filename[1]);
  163.                   exit();
  164.               }
  165.     break;
  166.      }
  167. }
  168.  
  169. argv++;
  170. init_defaults();
  171. INTRAP=0;
  172. EMstring[0]=ITstring[0]='\0';
  173. while ( --argc > 0 )
  174.       { strcpy (filename, *argv++);
  175.     if(*filename=='>')goto SKIPIT;
  176.     if(*filename=='}')goto SKIPIT;
  177.     if(*filename==')')goto SKIPIT;
  178.     fprintf(stderr,"Now processing <%s>\n",filename );
  179.     if(filename[0]=='-')
  180.         {option=toupper(filename[1]);
  181.         if(option=='M') showm(0);
  182.         else if(option=='F') putc(FORMF,ofp);
  183.         else if(option=='G') gloss();
  184.         else if(option=='I') showit();
  185.         else if(option=='R') showr();
  186.         else if(option=='S') PAGESTOP=TRUE;
  187.         else if(option=='O') range(&filename[2]);
  188.         else if(option=='N')
  189.         {Brk();
  190.          NEWPAG=atoi(&filename[2]);
  191.          if (NEWPAG<1) NEWPAG=1;
  192.          if (NEWPAG>9999) NEWPAG=9999;
  193.          CURPAG=NEWPAG-1;
  194.         }
  195.         else    {KEYBD=option;
  196.             dolns();
  197.             fprintf(stderr,"End of keyboard input <%c>\n",
  198.                 KEYBD);
  199.             KEYBD=FALSE;
  200.             }
  201. SKIPIT:         continue;
  202.         }
  203.     strcpy(FSTACK[0].FNAME,filename);
  204.     if (ERROR == (ifp=fopen(filename,"r")))
  205.         {fprintf(stderr,"can't open <%s>\n",filename );
  206.         continue;
  207.         }
  208.     else dolns();
  209.     if(VLINENO>0||OUTBUF[0])
  210.         { if (FILL && JUSTIFY && (DOTJ>1))
  211.             {int nextra;
  212.             nextra=min(RMVAL-TIVAL,MAXLINE-1)-OUTW+1;
  213.             if (OUTBUF[strlen(OUTBUF)-2]==BLANK) nextra++;
  214.             spread(OUTBUF,nextra,1);
  215.             }
  216.         Brk();
  217.         }
  218.     fprintf(stderr,"Done processing <%s>\n", FSTACK[0].FNAME );
  219.     close(ifp);
  220. }
  221. if(EMstring[0])
  222.     {pc=macq1(EMstring);
  223.      if(pc) pbstr(pc);
  224.      dolns();
  225.     }
  226. while(BINP)
  227.     dolns();
  228. while ((BINP)||(VLINENO<FOOTLOC))
  229.     {Brk();
  230.      dolns();
  231.      space(HUGE);
  232.     }
  233. close(ofp);
  234. }/* end main()           */
  235.  
  236. /**************************************************************
  237. initializes the global variables governing the execution of the
  238.  format commands.
  239. **************************************************************/
  240. void init_defaults()
  241. {int i;
  242. initsk(LSVAL,LS_DEF);    /* line spacing = 1 */
  243. initsk(PLVAL,PL_DEF);
  244. initsk(M1VAL,M1_DEF);
  245. initsk(M2VAL,M2_DEF);
  246. initsk(M3VAL,M3_DEF);
  247. initsk(M4VAL,M4_DEF);
  248. initsk(SCVAL,SC_INI);
  249. initsk(TFVAL,TF_DEF);
  250. initsk(CFVAL,CF_DEF);
  251. initsk(ICVAL,IC_DEF);
  252. initsk(EVVAL,EV_INI);
  253. initxu();
  254. redoRR();
  255. for(i=0;i<NUMENV;i++) chgenv(i,-1);    /* all environments */
  256. setTRAP();
  257. OUTBUF [0] = '\0';
  258. *EHEAD = *OHEAD = '\0' ;
  259. *EFOOT = *OFOOT = '\0' ;
  260. setmem(CPTR,2*(128-' '),0);
  261. setmem(TPTR,2*(128-' '),0);
  262. OUTBUF2[0]=0;
  263. setmem(&DBUF,LSZ,FALSE);
  264. *KLINE=0;
  265. SLINK.link=NULL;
  266. MLINK.link=NULL;
  267. RLINK.link=NULL;
  268. DIVERTING=FALSE;
  269. DLINES=0;
  270. }
  271. /*****************************/
  272. void range(char *s)
  273. {int num;
  274. num=0;
  275. while(isdigit(*s)) num=num*10+(*(s++)-'0');
  276. if(num) FIRSTPAGE=num;
  277. if(*s=='-')
  278.     {s++; num=0;
  279.     while(isdigit(*s)) num=num*10+(*(s++)-'0');
  280.     if(num) LASTPAGE=num;
  281.     }
  282. else    LASTPAGE=FIRSTPAGE;
  283. }
  284. /************************************/
  285. void redoRR()
  286. {int i=0,rm=65,ts=5;
  287.  RULER[i++] = 'L';
  288.  for(;i<rm;i++) if (i % ts) RULER[i]='-'; else RULER[i]='l';
  289.  RULER[i++] = 'R';
  290.  for ( ; i<LSZ; RULER[i++]='\0');
  291.  verifyRR(RULER);
  292. }
  293. /************************************/
  294. int verifyRR(char *rr)
  295. {int s,i,l,t;
  296.  char c;
  297.  s=strlen(rr)-1;
  298.  if (rr[s]!='R') return(FALSE);
  299.  for(l=0;rr[l]==' ';l++);
  300.  if (rr[l]!='L') return(FALSE);
  301.  t=l;
  302.  for(i=l+1; i<s; i++)
  303.     {c=rr[i];
  304.      if (c=='-') continue;
  305.      if (strchr("lcr.,",c))
  306.         {if (t==l) t=i;
  307.          continue;
  308.         }
  309.      else break;
  310.     }
  311.  if (i!=s) return(FALSE);
  312. /* o.k. so update  */
  313.  INVAL=l;
  314.  RMVAL=s+1;
  315.  strcpy(RULER, rr);
  316.  for (i=RMVAL; i<LSZ; RULER[i++]='\0');
  317.  return(TRUE);
  318. }
  319.