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

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