home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 145_01 / roff4.c < prev    next >
Text File  |  1979-12-31  |  14KB  |  552 lines

  1. /********************************************************/
  2. /*                            */
  3. /*            ROFF4, Version 1.60            */
  4. /*                            */
  5. /*(C) 1983,4 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. #include "roff4.h"
  16.  
  17.  
  18. /*****************MAIN************MAIN*********/
  19. main(argc, argv)
  20. int argc;
  21. char **argv;
  22. {char option,*macq(),*pc;
  23. char filename[20];
  24. struct divfd *pd;
  25.  
  26. #if C86        /* For debugging    */
  27. extern char    _exittbc;
  28. _exittbc = 0xff;
  29. #endif
  30.  
  31. #if BDS
  32. dioinit (&argc, argv);    /* directed I/O */
  33. #endif
  34. fprintf(stderr,"ROFF4, version 1.60, Mar 10, 1984\n");
  35. fprintf(stderr,"        (c) 1983,4 by\n");
  36. fprintf(stderr,"E. E. Bergmann, Physics,Bldg. #16\n");
  37. fprintf(stderr,"Lehigh University, Bethlehem PA 18015\n");
  38. debug = FALSE;
  39. if(FALSE) putchar('\n');/*for linkage purposes only!*/
  40. if (argc == 1)
  41.       { fprintf(stderr,"USAGE:  ROFF4 file1\n  more than one file OK\n");
  42.     exit();
  43.       }
  44. argv++;
  45. init_defaults();
  46. while ( --argc > 0 )
  47.       { strcpy (filename, *argv++);
  48.     fprintf(stderr,"Now processing <%s>\n",filename );
  49.     if(filename[0]=='-')
  50. #if BDS
  51.         {option=filename[1];
  52. #else
  53.         {option=toupper(filename[1]);
  54. #endif
  55.         if(option=='M') showm();
  56.         else if(option=='B') debug=TRUE;
  57.         else if(option=='D') showd();
  58.         else if(option=='F') putchar(FORMF);
  59.         else if(option=='G') gloss();
  60.         else if(option=='I') showit();
  61.         else if(option=='R') showr();
  62.         else if(option=='S') PAGESTOP=TRUE;
  63.         else if(option=='O') range(&filename[2]);
  64.         else    {KEYBD=option;
  65.             dolns();
  66.             fprintf(stderr,"End of keyboard input <%c>\n",
  67.                 KEYBD);
  68.             KEYBD=FALSE;
  69.             }
  70.         continue;
  71.         }
  72.     if(pd=find2(filename,DLINK)) dclose(pd);
  73. #if BDS
  74.     if (ERROR == fopen( filename,IOBUF)))
  75. #endif
  76. #if C86
  77.     if (ERROR == (iobuf = fopen( filename,"r")))
  78. #endif
  79. #if DeSmet
  80.     if (FERR == (iobuf = fopen( filename,"r")))
  81. #endif
  82.         {fprintf(stderr,"can't open <%s>\n",filename );
  83.         continue;
  84.         }
  85.     else dolns();
  86.     if(VLINENO>0||OUTBUF[0]) space(HUGE);
  87.     fprintf(stderr,"Done processing <%s>\n", filename );
  88.     fclose(iobuf);
  89.       }     /* end while (--argc > 0 ) */
  90. dsclose();
  91. #if BDS
  92. dioflush();
  93. #endif
  94. #if C86
  95. fflush(stdout);
  96. #endif
  97. }        /* end main()           */
  98. /****************************************/
  99. dolns()    /*do processing of lines*/
  100. {
  101. char *pc;
  102. int typ;
  103. int e;
  104.   BINP=0;
  105.     while((e=fgets2(LINE,iobuf))||FPTR) {    /*until EOF or CPMEOF */
  106.         if(e) {
  107.             if(LINE[0]==COMMAND) {
  108.                 if(pc=macq(LINE)) pbstr(pc);
  109.                 else comand(LINE);
  110.             }
  111.             else text(LINE);
  112.         }
  113.         else endso();
  114.     }
  115. }
  116.  
  117. /**************************************************************
  118. initializes the global variables governing the execution of the
  119.  format commands.
  120. **************************************************************/
  121. init_defaults()
  122. {initsk(&FILL,FI_DEF);    /* yes we want filled lines */
  123. initsk(&LSVAL,LS_DEF);    /* line spacing = 1 */
  124. initsk(&INVAL,IN_DEF);    /* left margin indent  0 */
  125. initsk(&RMVAL,RM_DEF);    /* right margin = page width  */
  126. TIVAL = TI_DEF;    /* left margin temporary indent    0 */
  127. CEVAL = 0;    /* next n lines to be centered -  0  */
  128. PAGESTOP = FALSE;
  129. FFEED = FF_INI;
  130. FIRSTPAGE=1;
  131. LASTPAGE=30000;/*infinite*/
  132. JUSTIFY=JU_INI;
  133. initsk(&PLVAL,PL_DEF);
  134. initsk(&M1VAL,M1_DEF);
  135. initsk(&M2VAL,M2_DEF);
  136. initsk(&M3VAL,M3_DEF);
  137. initsk(&M4VAL,M4_DEF);
  138. initsk(&SCVAL,SC_INI);
  139. initsk(&OWVAL,OW_INI);
  140. initsk(&TABSIZ,TS_DEF);
  141. initsk(&TCVAL,TC_DEF);
  142. initsk(&CFVAL,CF_DEF);
  143. initsk(&ICVAL,IC_DEF);
  144. CURPAG = 0;
  145. NEWPAG = 1;
  146. FRQ=FRSTRING=WHSTRING=0;
  147. FRVAL=1;
  148. FVLINENO=FPLINENO = PLINENO = 0;VLINENO = -1;
  149. BOTTOM = PLVAL - M3VAL - M4VAL;
  150. OUTW=OUTPOS=OUTTOP=OUTBOT=OLDLN=OLDBOT=OUTWRDS = 0;
  151. OUTBUF [0] = '\0';
  152. DIR = 0;
  153. EH2 = EH3 = EHEAD ;
  154. OH2 = OH3 = OHEAD ;
  155. *EHEAD = *OHEAD = '\0' ;
  156. EF2 = EF3 = EFOOT ;
  157. OF2 = OF3 = OFOOT ;
  158. *EFOOT = *OFOOT = '\0' ;
  159. setmem(CPTR,2*(128-' '),0);
  160. setmem(TPTR,2*(128-' '),0);
  161. TREND = TRTBL;
  162. OUTBUF2[0]=BPOS=0;
  163. initxu();
  164. MCNT=1;
  165. UF=XF=FALSE;
  166. #if BDS
  167. setmem(&DBUF,LSZ,FALSE);
  168. #else
  169. setmem(DBUF,LSZ,FALSE);
  170. #endif
  171. DPOS=-1;
  172. FPTR=DLINK=RLINK=SLINK=MLINK=0;
  173. KPTR=KLINE;
  174. *KLINE=0;
  175. KEYBD=FALSE;
  176. #if BDS
  177. rsvstk(2000);
  178. _allocp = NULL; /*needed for alloc() and free()*/
  179. iobuf = alloc(BUFSIZ);
  180. #endif
  181. complete();
  182. }
  183. /****************************************/
  184.  
  185. /*    July 1: \#\ replaced by current page number.
  186.     June 8: concatenate lines with trailing '\'
  187.     June 1, 1983 modified for macro string processing.
  188.     fgets2:    (May 2, 1983 by EEB fix to reset parity)
  189.     This next function is like "gets", except that
  190.     a) the line is taken from a buffered input file instead
  191.     of from the console, and b) the newline is INCLUDED in
  192.     the string and followed by a null byte. 
  193.     
  194.     This one is a little tricky due to the CP/M convention
  195.     of having a carriage-return AND a linefeed character
  196.     at the end of every text line. In order to make text
  197.     easier to deal with from C programs, ngetc()
  198.     automatically strips off the CR from any CR-LF
  199.     combinations that come in from the file. Any CR
  200.     characters not immediately followed by LF are left
  201.     intact. The LF is included as part of the string, and
  202.     is followed by a null byte. (Note that LF equals
  203.     "newline".) There is no limit to how long a line
  204.     can be here; care should be taken to make sure the
  205.     string pointer passed to fgets points to an area
  206.     large enough to accept any possible line length
  207.     (a line must be terminated by a newline (LF, or '\n')
  208.     character before it is considered complete.)  The
  209.     ngetc() also resets the parity bit.
  210.  
  211.     The value NULL (defined to be 0 here) is returned
  212.     on EOF, whether it be a physical EOF (attempting to
  213.     read past last sector of the file) OR a logical EOF
  214.     (encountered a control-Z.) The 1.3 version didn't
  215.     recognize logical EOFs, because I did't realize how
  216.     SIMPLE it was to implement a buffered I/O "ungetc"
  217.     function.
  218. */
  219.  
  220. char *fgets2(s,iobuf)
  221. char *s;
  222. #if BDS
  223. struct _buf *iobuf;
  224. #endif
  225. #if C86
  226. FILE *iobuf;
  227. #endif
  228. #if DeSmet
  229. FILE iobuf;
  230. #endif
  231. {
  232.     int count,c,i,*pw,pnum;
  233.     char *cptr,*pc;
  234.     char wbuf[LSZ],*fnd;
  235.     count = MAXLINE;
  236.     cptr = s;
  237. #if BDS
  238.     if ( (c=ngetc(iobuf))==CPMEOF || c==EOF)
  239. #else
  240.     if ( (c=ngetc(iobuf))==EOF)
  241. #endif
  242.         return NULL;
  243.  
  244.     do {    if(c==ICVAL)/*need macro substitution*/
  245.           {for(i=0;ICVAL!=(wbuf[i]=ngetc(iobuf));i++)
  246.             {if(wbuf[i]=='\n')
  247.                 {if(i) putback('\n');
  248.                 break;
  249.                 }
  250.             }
  251.           if(i) 
  252.            {wbuf[i]='\0';
  253.             if((*wbuf==NUMSIGN)&&(i==1))
  254.             {if((VLINENO>=PLVAL)
  255.                 ||(VLINENO<0)) pnum=NEWPAG;
  256.             else pnum=CURPAG;
  257.             itoc(pnum,wbuf,10);
  258.             pbstr(wbuf);
  259.             }
  260.             else if(pw=find2(wbuf,RLINK))
  261.             {itoc(*pw,wbuf,10);
  262.             pbstr(wbuf);
  263.             }
  264.             else if(fnd=find2(wbuf,SLINK)) pbstr(fnd);
  265.             else{start();
  266.                 pc=wbuf;
  267.             pw=TREND;
  268.             *(pw++)=SLINK;
  269.             transfer(&pc,&pw,'\0');
  270.             fprintf(stderr,"%cPlease define <%s>:",
  271.                         BELL,wbuf);
  272.             gets(wbuf);
  273.             pc=wbuf;
  274.             transfer(&pc,&pw,'\0');
  275.             SLINK=TREND;
  276.             TREND=pw;
  277.             complete();
  278.             pbstr(wbuf);
  279.             }
  280.             continue;
  281.             }
  282.            else if(*wbuf!='\n') putback(ICVAL); 
  283.            c=ngetc(iobuf);
  284.           }
  285.         if ((*cptr++ = c) == '\n') break;
  286.  
  287. #if BDS
  288.      } while (count--&&(c=ngetc(iobuf))!=EOF&&c!=CPMEOF);
  289.  
  290.     if(c==CPMEOF)putback(c);    /*push back control-Z*/
  291. #else
  292.      } while (count--&&(c=ngetc(iobuf))!=EOF);
  293. #endif
  294. if (debug && c == EOF) fprintf(stderr, "fgets2: EOF\n");
  295.     *cptr = '\0';
  296.     return s;
  297. }
  298. /**************************************************************
  299. performs the formatting command returned by comtyp -sets global
  300.   variables ( indenting, underlining, etc. )
  301. **************************************************************/
  302. comand ( line )
  303. char *line;
  304. {int c_type;    /* command type  */
  305. int arg_val;    /* argument value, if any */
  306. char arg_typ;    /* relative (+ or -) or absolute */
  307. char wbuf[20];
  308. int i;
  309. c_type = comtyp (line);
  310. if DEBUG fprintf(stderr,"COMAND %d,",c_type);
  311. if (c_type == UNKNOWN)
  312.     {fprintf(stderr, "UNKNOWN COMMAND: <%s>\n", line);
  313.     return;
  314.     }
  315. arg_val = get_val ( line, &arg_typ );
  316. if DEBUG
  317. fprintf(stderr,"get_val returned arg_val=%d,arg_typ=%c\n",
  318.                    arg_val,    arg_typ   );
  319. switch (c_type)
  320.     {case IG : break;/* ignore remark */
  321.  
  322.     case FI :    /* filled lines  */
  323.         brk();
  324.         FILL = YES;
  325.         break;
  326.  
  327.     case NF :    /* non-filled lines */
  328.         brk();
  329.         FILL = NO;
  330.         break;
  331.  
  332.     case NJ :    /* non-justified lines */
  333.         JUSTIFY = NO;
  334.         break;
  335.  
  336.     case JU :    /* justified lines  */
  337.         JUSTIFY = YES;
  338.         break;
  339.  
  340.     case BR :    /* just cause a break */
  341.         brk();
  342.         break;
  343.  
  344.     case LS :    /* set line spacing value */
  345.         setS(&LSVAL, arg_val, arg_typ, LS_DEF, 1, HUGE );
  346.         break;
  347.  
  348.     case TI :    /* set temporary left indent */
  349.         brk();
  350.         set ( &TIVAL, arg_val, arg_typ, TI_DEF, 0, RMVAL );
  351.         break;
  352.  
  353.     case IN :    /* set left indent */
  354.         setS( &INVAL, arg_val, arg_typ, IN_DEF, 0, RMVAL-1 );
  355.         TIVAL = INVAL;
  356.         break;
  357.  
  358.     case RM:    /* set right margin */
  359.         setS( &RMVAL, arg_val, arg_typ, RM_DEF, TIVAL+1, OWVAL );
  360.         break;
  361.     case M1:    /* set topmost margin */
  362.         setS( &M1VAL, arg_val, arg_typ, M1_DEF,0,HUGE);
  363.         break;
  364.  
  365.     case M2:    /* set second top margin */
  366.         setS( &M2VAL, arg_val, arg_typ, M2_DEF,0,HUGE);
  367.         break;
  368.  
  369.     case M3:    /* set first bottom margin */
  370.         setS( &M3VAL, arg_val, arg_typ, M3_DEF,0,HUGE);
  371.         BOTTOM = PLVAL - M3VAL - M4VAL;
  372.         break;
  373.  
  374.     case M4:    /* set bottom-most margin */
  375.         setS(&M4VAL, arg_val, arg_typ, M4_DEF,0,HUGE);
  376.         BOTTOM = PLVAL - M3VAL - M4VAL;
  377.         break;
  378.  
  379.     case CE :    /* center next arg_val lines */
  380.         brk();
  381.         set ( &CEVAL, arg_val, arg_typ,CE_DEF,0, HUGE);
  382.         break;
  383.  
  384.     case HE :    /* get header title for pages */
  385.         gettl3 ( line, EHEAD, &EH2, &EH3 );
  386.         gettl3 ( line, OHEAD, &OH2, &OH3 );
  387.         break;
  388.  
  389.     case OH :    /*get odd header title*/
  390.         gettl3 ( line, OHEAD, &OH2, &OH3 );
  391.         break;
  392.  
  393.     case EH :    /*get even header title*/
  394.         gettl3 ( line, EHEAD, &EH2, &EH3 );
  395.         break;
  396.  
  397.     case FO :    /* get footer title for pages */
  398.         gettl3 ( line, EFOOT, &EF2, &EF3 );
  399.         gettl3 ( line, OFOOT, &OF2, &OF3 );
  400.         break;
  401.  
  402.     case OF :    /* get odd page footer title*/
  403.         gettl3 ( line, OFOOT, &OF2, &OF3 );
  404.         break;
  405.  
  406.     case EF :    /* get even page footer title*/
  407.         gettl3 ( line, EFOOT, &EF2, &EF3 );
  408.         break;
  409.  
  410.     case SP :    /* space down arg_val blank lines */
  411.         set (&SPVAL, arg_val, arg_typ, 1, 0, HUGE);
  412.         space ( SPVAL );
  413.         break;
  414.  
  415.     case ST :    /* stop(pause) at each page?*/
  416.         set(&PAGESTOP,arg_val,(char)'0',YES,NO,YES);
  417.         break;
  418.     case BP :    /* set pageno arg_val - begin page */
  419.         brk();
  420.         if(((VLINENO<=0)||(VLINENO>=BOTTOM))&&
  421.             (arg_val==NO_VAL)) break;
  422.         if ( VLINENO > 0 )    space (HUGE);
  423.         set(&CURPAG,arg_val,arg_typ,CURPAG+1,0,9999);
  424.         NEWPAG = CURPAG;
  425.         break;
  426.     case NE :    /*"need"*/
  427.         if (arg_val==NO_VAL) arg_val=2;/*default*/
  428.         need(arg_val);
  429.         break;
  430.  
  431.     case PL :    /* set page length */
  432.         setS(&PLVAL, arg_val, arg_typ, PL_DEF,
  433.           M1VAL+M2VAL+M3VAL+M4VAL+1,HUGE);
  434.         BOTTOM = PLVAL - M3VAL - M4VAL;
  435.         break;
  436.  
  437.     case FF :    /*formfeed*/
  438.         set(&FFEED,arg_val,(char)'0',FF_DEF,NO,YES);
  439.         break;
  440.  
  441.     case SC :    /*space character*/
  442.         if(arg_typ) arg_val=arg_typ;
  443.         setS(&SCVAL,arg_val,(char)'0',SC_INI,BLANK,127);
  444.         break;
  445.  
  446.     case OW :    /*output device width*/
  447.         setS(&OWVAL,arg_val,(char)'0',OW_INI,RMVAL,HUGE);
  448.         break;
  449.     case TS :    /*tabsize*/
  450.         setS(&TABSIZ,arg_val,(char)'0',TS_DEF,1,HUGE);
  451.         break;
  452.  
  453.     case AB :    /*abort*/
  454.         fprintf(stderr,"\n***USER ABORT***\n");
  455.         exit();
  456.  
  457.     case DB :    /*debug*/
  458.         setS(&debug,arg_val,(char)'0',NO,NO,YES);
  459.         if DEBUG fprintf(stderr,"\nDEBUG ON...\n");
  460.         else fprintf(stderr,"\n...END OF DEBUG\n");
  461.         break;
  462.  
  463.     case TC :    /*translate character flag*/
  464.         if(arg_typ) arg_val=arg_typ;
  465.         setS(&TCVAL,arg_val,(char)'0',TC_DEF,BLANK+1,127);
  466.         break;
  467.  
  468.     case CF :    /*translate character flag*/
  469.         if(arg_typ) arg_val=arg_typ;
  470.         setS(&CFVAL,arg_val,(char)'0',CF_DEF,BLANK+1,127);
  471.         break;
  472.  
  473.     case IC :    /*insert character for macro replace*/
  474.         if(arg_typ) arg_val=arg_typ;
  475.         setS(&ICVAL,arg_val,(char)'0',IC_DEF,BLANK+1,127);
  476.         break;
  477.  
  478.     case TR :    /*translation string defined here*/
  479.         gettr(); break;
  480.  
  481.     case OU :    /*output code string*/
  482.         ocode(); break;
  483.  
  484.     case FR :    /*define fractional spacing code*/
  485.         getfr(); break;
  486.  
  487.     case WH :    /*define whole line spacing code*/
  488.         getwh(); break;
  489.  
  490.     case DS :    /*define string*/
  491.         insert(); break;
  492.  
  493.     case DM :    /*define macro*/
  494.         minsert(); break;
  495.  
  496.     case RG :    /*register variable*/
  497.         dovar(); break;
  498.  
  499.     case DI :    /*diversion to file*/
  500.         dodiv(); break;
  501.  
  502.     case SO :    /*source from file*/
  503.         source(); break;
  504.  
  505.     case SA :    /*"say" to console*/
  506.         getwrd(LINE,wbuf);    /*skip command*/
  507.         skip_blanks(LINE);
  508.         trunc_bl(LINE);
  509.         fprintf(stderr,"<%s>\n",LINE);
  510.         break;
  511.  
  512.     case BJ :    /*break with right justification*/
  513.         if(FILL)/*not applicable otherwise*/
  514.         {spread(OUTBUF,
  515.             min(RMVAL-TIVAL,MAXLINE-1)-OUTW+1,
  516.             OUTWRDS);
  517.         brk();
  518.         }
  519.         break;
  520.  
  521.     case PC :    /*printer control*/
  522.         getpc(); break;
  523.       }
  524. }
  525.  
  526. start()/*to insure only one entry into TRTBL at a time*/
  527. {if(TFLAG)
  528.     {fprintf(stderr,"ILLEGAL double access of TRTBL\n");
  529.     fprintf(stderr,"Simultaneous creation of two definitions not possible");
  530.     exit();
  531.     }
  532. else    TFLAG=TRUE;
  533. }
  534.  
  535. complete()/*to insure only one entry into TRTBL at a time*/
  536. {TFLAG=FALSE;
  537. }
  538.  
  539. range(s)
  540. char *s;
  541. {int num;
  542. num=0;
  543. while(isdigit(*s)) num=num*10+(*(s++)-'0');
  544. if(num) FIRSTPAGE=num;
  545. if(*s=='-')
  546.     {s++; num=0;
  547.     while(isdigit(*s)) num=num*10+(*(s++)-'0');
  548.     if(num) LASTPAGE=num;
  549.     }
  550. else    LASTPAGE=FIRSTPAGE;
  551. }
  552.