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