home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 128_01 / roff4161.c < prev    next >
Text File  |  1985-03-10  |  13KB  |  499 lines

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