home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / textools / part01 / detex2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  6.0 KB  |  309 lines

  1. /* detex: strips TeX's and LaTeX's commands */
  2. /* to compile:   cc detex.c -o detex */
  3.  
  4.  
  5. char *documentation[] = {
  6. " SYNTAX",
  7. "        detex [-iw] [parameters] [inputfiles]",
  8. "",
  9. "        flags:",
  10. "              -i   ignores TeX's and LaTeX's \input and \include commands",
  11. "              -w   does not check matching",
  12. "",
  13. "        parameters:",
  14. "              in=filename       filename is the input file",
  15. "                                (Default: in=stdin)",
  16. "",
  17. "              out=filename      filename is the output file",
  18. "                                (Default: out=stdout)",
  19. ""
  20. };
  21.  
  22. /* Author: Kamal Al-Yahya, Stanford University,        11/1/83 */
  23. /* Modified:                        6/30/86 */
  24.  
  25. int    doclength = { sizeof documentation/sizeof documentation[0] };
  26.  
  27. #include        <stdio.h>
  28. #include        <sys/ioctl.h>
  29. #include        <sgtty.h>
  30. #define    MAXLEN    100000
  31.  
  32. char string[100],filename[100];
  33. FILE *out_file;
  34. struct sgttyb ttystat;
  35. extern char *strcpy(), *mktemp();
  36. char scratch_file[100];
  37.  
  38. int wflag;
  39. int xargc;
  40. char **xargv;
  41.  
  42. main(argc,argv)
  43. int argc; 
  44. char *argv[];
  45. {
  46. char big[MAXLEN];
  47. FILE *temp,*scr;
  48. register char *cptr;
  49. int piped_in;
  50. int iflag,i;
  51.  
  52. /* If no arguments, and not in a pipeline, self document */
  53. piped_in = ioctl ((fileno (stdin)), TIOCGETP, &ttystat);
  54. if (argc == 1 && !piped_in)
  55.     {
  56.     for( i=0; i<doclength; i++)
  57.         printf("%s\n",documentation[i]);
  58.     exit (0);
  59.     }
  60.  
  61. out_file = stdout;        /* default output */
  62.  
  63. /* process option flags */
  64. xargc = argc;
  65. xargv = argv;
  66. for (xargc--,xargv++; xargc; xargc--,xargv++)
  67.     {
  68.     cptr = *xargv; 
  69.     if( *cptr=='-' )
  70.         {
  71.         while( *(++cptr))
  72.             {
  73.             switch( *cptr )
  74.                 {
  75.                 case 'i':
  76.                     iflag=1;
  77.                     break;
  78.                 case 'w':
  79.                     wflag=1;
  80.                     break;
  81.                 default:
  82.                          fprintf(stderr,
  83.                     "unknown flag -%c\n",*cptr);
  84.                     break;
  85.                 }
  86.             }
  87.         }
  88.     }
  89.  
  90. /* process getpar parameters */
  91. xargc = argc;
  92. xargv = argv;
  93.  
  94. if(getpar_("out","s",string))
  95.     {
  96.     sscanf(string,"%s",filename);
  97.     if((temp=fopen(filename,"w")) == NULL)
  98.         fprintf(stderr,"detex: Cannot open output file %s\n",filename);
  99.     else
  100.         out_file = temp;
  101.     }
  102.  
  103. /* first process pipe input */
  104. if(piped_in)
  105.     {
  106. /* need to buffer; can't seek in pipes */
  107. /* make a temporary and volatile file in /tmp */
  108.     strcpy(scratch_file,"/tmp/texXXXXXX");
  109.     mktemp(scratch_file);
  110.     scr=fopen(scratch_file,"w");
  111.     scrbuf(stdin,scr);
  112.     fclose(scr);
  113.     scr=fopen(scratch_file,"r");
  114.     unlink(scratch_file);
  115.     if (wflag != 1)
  116.         {
  117.         fprintf(stderr,"Checking matching...\n");
  118.         TeXMatch(scr);
  119.         fseek(scr,0,0);
  120.         }
  121. /* either expand or buffer */
  122.     if (iflag != 1)
  123.         { TeXExpand(scr,big,MAXLEN);    fclose(scr); }
  124.     else
  125.         { tmpbuf(scr,big);        fclose(scr); }
  126.     if (wflag != 1)
  127.         fprintf(stderr,"Checking matching done\n\n");
  128.     DeTeX(big,out_file);
  129.     fclose(scr);
  130.     }
  131.  
  132. /* next process in=inputfiles */
  133. if(getpar_("in","s",string))
  134.     {
  135.     sscanf(string,"%s",filename);
  136.     if((temp=fopen(filename,"r")) != NULL)
  137.         {
  138.         if (wflag != 1)
  139.             {
  140.             fprintf(stderr,"Checking matching...\n");
  141.             fprintf(stderr,"%s:\n",cptr);
  142.             TeXMatch(temp);
  143.             fprintf(stderr,"\n");
  144.             fseek(temp,0,0);
  145.             }
  146. /* either expand or buffer */
  147.         if (iflag != 1)
  148.             { TeXExpand(temp,big,MAXLEN);    fclose(temp); }
  149.         else
  150.             { tmpbuf(temp,big);        fclose(temp); }
  151.         if (wflag != 1)
  152.             fprintf(stderr,"Checking matching done\n\n");
  153.         DeTeX(big,out_file);
  154.         fclose(temp);
  155.         }
  156.     else
  157.         fprintf(stderr,"detex: Cannot open %s\n",filename);
  158.     }
  159.  
  160. /* then process input line for arguments and assume they are input files */
  161. xargc = argc;
  162. xargv = argv;
  163.  
  164. for (xargc--,xargv++; xargc; xargc--,xargv++)
  165.     {
  166.     cptr = *xargv; 
  167.     if( *cptr=='-' ) continue; /* this is a flag */
  168.     while (*cptr)
  169.         {
  170.         if (*cptr == '=')  break; /* this is for getpar */
  171.         cptr++;
  172.         }       
  173.     if (*cptr)  continue;
  174.     cptr = *xargv;
  175.     if((temp=fopen(cptr,"r")) != NULL)
  176.         {
  177.         if (wflag != 1)
  178.             {
  179.             fprintf(stderr,"Checking matching...\n");
  180.             fprintf(stderr,"%s:\n",cptr);
  181.             TeXMatch(temp);
  182.             fprintf(stderr,"\n");
  183.             fseek(temp,0,0);
  184.             }
  185. /* either expand or buffer */
  186.         if (iflag != 1)
  187.             { TeXExpand(temp,big,MAXLEN);    fclose(temp); }
  188.         else
  189.             { tmpbuf(temp,big);        fclose(temp); }
  190.         if (wflag != 1)
  191.             fprintf(stderr,"Checking matching done\n\n");
  192.         DeTeX(big,out_file);
  193.         fclose(temp);
  194.         }
  195.     else
  196.         fprintf(stderr,"detex: Cannot open %s\n",cptr);
  197.     }
  198.  
  199. }
  200.  
  201. DeTeX(big,out_file)            /* stripping TEX commands */
  202.  
  203. char *big;
  204. FILE *out_file;
  205. {
  206. int c,c1,be;
  207.  
  208. while ((c = *big++) != '\0')
  209.     {
  210.     switch (c)
  211.     {
  212. /* detect TeX commands (backslash) */
  213.     case '\\':
  214.         c1=c ;        /* c1 is needed to see if there is \$ */
  215.         c=' ' ;        /* "erase" the backslash */
  216.         putc(c,out_file);
  217.         if ((c = *big++) =='b')
  218.         if (begin_end_buf(big,&be) == 0)
  219.             {
  220.             if (be == 1)
  221.                 {
  222.                 big += 14;
  223.                 while ( *big++ != '\0')
  224.                     {
  225.                     if(begin_end_buf(big,&be) == 0)
  226.                         if (be == 2)
  227.                             {
  228.                             big += 13;
  229.                             break;
  230.                             }
  231.                     }
  232.                 }
  233.             }
  234.         while ((c = *big++) != '\0')
  235.             {
  236.             if (c == '$')            goto dollar;
  237.             if (c == '%')            goto comm;
  238.             if (c == '{' || c == '#')    break;
  239.             if (c == ' ' || c == '\n' || c == '(' || c == ')')
  240.                 {
  241.                 putc(c,out_file);
  242.                 break;
  243.                 }
  244.             c1=c;                /* reinitialize c1 */
  245.             }
  246.         break;
  247. /* come here if the character is dollar sign */
  248. dollar:
  249.     case '$':
  250.         c=' ' ;        /* "erase" the dollar sign */
  251.         putc (c,out_file);
  252.         if (c1 == '\\')         break;        /* means \$ */
  253.         c = *big++;
  254.         if(c == '$')        goto dollar2;
  255.         else            goto dollar1;
  256.         break;
  257.  
  258. /* see if it is an in-line equation (delimeter is one dollar sign) */
  259. dollar1:
  260.         while((c = *big++) != '\0')
  261.             if(c == '$')    break;
  262.         break;
  263.  
  264. /* see if it is a displayed equation (delimeter is two dollar signs) */
  265. dollar2:
  266.         while((c = *big++) != '\0')
  267.             {
  268.             if(c == '$')
  269.                 {
  270.                 c = *big++;
  271.                 if (c != '$')    putc(c,out_file);
  272.                 break;
  273.                 }
  274.             }
  275.         break;
  276.  
  277. /* ignore commented text */
  278. comm:
  279.     case '%':
  280.         if (c1 == '\\')         break;        /* means \% */
  281.         while((c = *big++) != '\0')
  282.             if (c == '\n')
  283.                 {
  284.                 putc(c,out_file);
  285.                 break;
  286.                 }
  287.         break;
  288. /* erase these character */
  289.     case '{':
  290.         c=' ';
  291.     case '}':
  292.         c=' ';
  293.     case '_':
  294.         c=' ';
  295.     case '^':
  296.         c=' ';
  297.     case '&':
  298.         c=' ';
  299.     case '#':
  300.         c=' ';
  301. /* default is doing nothing: pass the character to the output */
  302.     default:
  303.         putc(c,out_file);
  304.         break;
  305.     }
  306.     c1=c;            /* reinitialize c1 */
  307.     }
  308. }
  309.