home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 128_01 / roff42.c < prev    next >
Text File  |  1985-03-10  |  7KB  |  317 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. /*feb 19, 1984*/
  16. /*Jan 14, 1984*/
  17. #include "roff4.h"
  18.  
  19. insert()    /*takes a command line in LINE and adds its
  20.         entry to the table; no space check is made of
  21.         TRTBL*/
  22. {int *pw;
  23. char *pc,*s2,*s3,*fnd;
  24. pw=TREND;
  25. *pw=SLINK;
  26. pc=pw+1;
  27. gettl3(LINE,pc,&s2,&s3);
  28. if(fnd=find2(pc,SLINK))
  29.     {fprintf(STDERR,"%cWarning: <%s> was defined to be <%s>\n",BELL,pc,fnd);
  30.     fprintf(STDERR,"...now it is defined to be <%s>\n",s2);
  31.     }
  32. SLINK=TREND;
  33. TREND=s3;
  34. }
  35. /****************************************/
  36. showit()    /* displays the list of entries in the string
  37.         substitution table pointed to by HEAD. */
  38. {int *pw;
  39. char *pc;
  40. fprintf(STDERR,"ENTRIES:\n");
  41. pw=SLINK;
  42. while(pw)
  43.     {fprintf(STDERR,"%u: ",pw);
  44.     pc=pw+1;
  45.     fprintf(STDERR,"<%s>",pc);
  46.     pc += strlen(pc); pc++;
  47.     if(*pc) fprintf(STDERR," <%s>\n",pc);
  48.     else fprintf(STDERR," <>\n"); /*'C' bug*/
  49.     pw=*pw;
  50.     }
  51. dashes();
  52. }
  53. /****************************************/
  54. putback(c)    /*cf K & P, p256*/
  55. char c;
  56. {if(++BINP >= BACKSIZE)
  57.     {printf(STDERR,"Too many characters pushed back\n");
  58.     exit();
  59.     }
  60. BACKBUF[BINP]=c;
  61. }
  62. /****************************************/
  63. int ngetc(iobuf)    /*cf K & P p256*/
  64. struct _buf *iobuf;    /*filters \r followed by \n,msb*/
  65. {char c,kgetc();
  66. if(BINP) c=BACKBUF[BINP];
  67. else    {if(KEYBD) c=kgetc();
  68.     else c=0x7f&getc(iobuf);
  69.     BACKBUF[BINP=1]=c;
  70.     }
  71. if((c!=CPMEOF)&&c!=EOF) BINP--;
  72. if(c=='\r')
  73.     {c=ngetc(iobuf);
  74.     if(c!='\n')
  75.         {putback(c);
  76.         return('\r');
  77.         }
  78.     }
  79. return(c);
  80. }
  81. /****************************************/
  82. char kgetc()    /*like getc(),from keyboard, line-buffered*/
  83. {int i;
  84. if(!*KPTR)
  85.     {fprintf(STDERR,"%c",KEYBD);
  86.     gets(KLINE);
  87.     i=strlen(KLINE);
  88.     KLINE[i++]='\n';
  89.     KLINE[i]='\0';
  90.     KPTR=KLINE;
  91.     }
  92. return(*(KPTR++));
  93. }
  94. /****************************************/
  95. pbstr(s)    /*put back string on input;cf K&P,p257*/
  96. char s[LSZ];
  97. {int i;
  98. for(i=strlen(s);i>0;) putback(s[--i]);
  99. }
  100. /****************************************/
  101. minsert()    /*takes a .DM and following lines and places
  102.         the information in the table;  no macro
  103.         definition nesting permitted*/
  104. {char c, *pc,*pc2;
  105. int *pw1;
  106. /*pass over command and following white space*/
  107. for(pc=LINE,c=*pc; (c!=' ')&&(c!='\n')&&(c!='\t'); pc++)
  108.     c=*pc;
  109. for(; (c==' ')||(c=='\t'); pc++) c=*pc;
  110. start();
  111. if(c=='\n') {fprintf(STDERR,".DM is UNnamed\n"); }
  112. else    {pw1=TREND;
  113.     *(pw1++)=MLINK;
  114.     MLINK=TREND;
  115.     TREND=pw1;
  116.     while(class(c)==BLACK)
  117.         {*(TREND++)=c;
  118.         c=*(pc++);
  119.         }
  120.     }
  121. *(TREND++)='\0';
  122. while(fgets2(LINE,IOBUF))    /*until EOF or CPMEOF*/
  123.     {pw1=LINE;
  124.     if((*LINE==COMMAND)&&(comtyp(LINE)==EM)) break;
  125.     else    {transfer(&pw1,&TREND,0);
  126.         *(TREND-1)='\n';
  127.         }
  128.     }
  129. *(TREND++)='\0';
  130. complete();
  131. }
  132. /****************************************/
  133. showm()    /*lists macro definitions*/
  134. {int *pw;
  135. char *pc;
  136. fprintf(STDERR,"MACROS DEFINED:\n");
  137. pw=MLINK;
  138. while(pw)
  139.     {pc = pw+1;
  140.     fprintf(STDERR,"%u  .%s\n",pw,pc);
  141.     pc +=strlen(pc); pc++;
  142.     fprintf(STDERR,"%s\n",pc);
  143.     pw=*pw;
  144.     }
  145. dashes();
  146. }
  147. /****************************************/
  148. char *macq(line)    /*looks up name to see if it is a macro
  149.             definition.  If it is, returns the
  150.             corresponding string, else returns
  151.             FALSE.
  152.             */
  153. char *line;
  154. {char c,*pc,wb[LSZ],*find2();
  155. pc=wb;
  156. while(class(c=*(++line))==BLACK) *(pc++)=c;
  157. *pc='\0';
  158. return(find2(wb,MLINK));
  159. }
  160. /****************************************/
  161. char *find2(s,link)    /*finds or doesn't find s in table
  162.             of substitutions pointed to by link*/
  163. char *s;
  164. int *link;
  165. {char *pc;
  166. while(link)
  167.     {if(!strcmp(s,link+1))
  168.         {pc=link+1;
  169.         pc += strlen(pc);
  170.         pc++;
  171.         return(pc);
  172.         }
  173.     link=*link;
  174.     }
  175. return(FALSE);
  176. }
  177. /****************************************/
  178. /*from ndio.c*/
  179. #define CON_INPUT 1            /* BDOS call to read console       */
  180. #define CON_OUTPUT 2            /* BDOS call to write to console   */
  181. #define CON_STATUS 11            /* BDOS call to interrogate status */
  182.  
  183. #define CONTROL_C 3            /* Quit character           */
  184. #define INPIPE 2            /* bit setting to indicate directed
  185.                        input from a temp. pipe fil     */
  186. #define VERBOSE 2            /* bit setting to indicate output is to
  187.                        go to console AND directed output */
  188. #define DIRECTED_OUT 1
  189. #define CONS_TOO 2
  190. #define CPM_LIST_OUT 4
  191. #define PRINTER_OUT 8
  192. #define ROBOTYPE_OUT 16
  193. #define CONSOLE_ONLY 0
  194.  
  195. /* 
  196.     The "dioinit" function must be called at the beginning of the
  197.     "main" function:
  198. */
  199.  
  200. dioflush()
  201. {
  202.     if (_doflag)
  203.     {
  204.         putc(CPMEOF,_dobuf);
  205.         fflush(_dobuf);
  206.         fclose(_dobuf);
  207.         if (OK != strcmp ("TEMPOUT.$$$", sav_out_file))
  208.               { unlink (sav_out_file);
  209.             rename ("TEMPOUT.$$$", sav_out_file);
  210.               }
  211.         rename("tempout.$$$","tempin.$$$");
  212.     }
  213. }
  214.  
  215. /*
  216.     This version of "putchar" replaces the regular version when using
  217.     directed I/O:
  218. */
  219.  
  220. putchar(c)
  221. char c;
  222. {    if (SUPPRESS) return;
  223.     if (_doflag & DIRECTED_OUT)
  224.     {
  225.         if (c == '\n') putc('\r',_dobuf);
  226.         if(putc(c,_dobuf) == ERROR)
  227.         {
  228.             fprintf(STDERR,"File output error; disk full?\n");
  229.             exit();
  230.         }
  231.     }
  232.  
  233.     if (_doflag==0 || _doflag & CONS_TOO)
  234.     {
  235.     if (bdos(CON_STATUS) && bdos(CON_INPUT) == CONTROL_C) exit();
  236.     if (c == '\n') bdos(CON_OUTPUT,'\r');
  237.     bdos(CON_OUTPUT,c);
  238.     }
  239.  
  240.     if (_doflag & CPM_LIST_OUT)
  241.     {
  242.         bdos(5,c);
  243.         if (c=='\n') bdos(5,'\r');
  244.     }
  245.     if (_doflag & PRINTER_OUT)
  246.     {
  247.         bdos(5,c);
  248.     }
  249.     if (_doflag & ROBOTYPE_OUT)
  250.     {
  251.         fprintf(STDERR,"sending ROBO <%c>    ",c);
  252.     }
  253. }
  254. /****************************************/
  255. #define argc *argcp
  256. dioinit(argcp,argv)
  257. int *argcp;
  258. char **argv;
  259. {
  260.     int i,j, argcount;
  261.     int n;    /* this keeps track of location in argument */
  262.  
  263.     _doflag = FALSE;  /* No directed /O by default   */
  264.     _nullpos = &argv[argc];
  265.     argcount = 1;
  266.  
  267.     for (i = 1; i < argc; i++)    /* Scan the command line for > and )*/
  268.     {
  269.         n=0;    /* start with first character */
  270. getmore:    switch(argv[i][n++]) {
  271.  
  272.            case '+': 
  273.             _doflag |= VERBOSE;
  274.             goto getmore;
  275.            case ')':
  276.             _doflag |= CPM_LIST_OUT;
  277.             goto getmore;
  278.            case '}':
  279.             _doflag |= PRINTER_OUT;
  280.             goto getmore;
  281.            case ']':
  282.             _doflag |= ROBOTYPE_OUT;
  283.             goto getmore;
  284.             
  285.          foo:   case '>':/*Check for directed output*/
  286.             if (!argv[i][n]) 
  287.             {
  288.             barf:   fprintf(STDERR,"Bad redirection/pipe specifier");
  289.                 exit();
  290.             }
  291.             strcpy (sav_out_file, &argv[i][n] );
  292.             if (fcreat("TEMPOUT.$$$", _dobuf) == ERROR)
  293.             {
  294.                 fprintf(STDERR,"\nCan't create <%s>\n",
  295.                                                      "TEMPOUT.$$$");
  296.                    exit();
  297.             }
  298.             _doflag++;
  299.  
  300.          movargv:    argc = argcount;
  301.             argv[argc] = 0;
  302.             break;
  303.  
  304.             default:    /* handle normal arguments: */
  305.                 if (n!=1) goto movargv;
  306.             argcount++;
  307.         }
  308.     }
  309. }
  310.  
  311.  
  312. #undef argc
  313. rgcount = 1;
  314.  
  315.     for (i = 1; i < argc; i++)    /* Scan the command line for > and )*/
  316.     {
  317.         n=0;    /* start with fi