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

  1. /* texeqn: TeX equation stripping
  2.  * to compile:   cc texeqn.c -o texeqn */
  3.  
  4. char *documentation[] = {
  5. " SYNTAX",
  6. "        texeqn [-iw] file1 [file2 .....]",
  7. "     or texeqn [-iw]  < file1 [file2 ....]",
  8. "",
  9. "        Flags:",
  10. "              -i     ignores TeX's and LaTeX's \input files",
  11. "              -w     matching is not checked",
  12. "",
  13. "See the manual page for more details.",
  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.     TeXEqn(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.         TeXEqn(big);
  135.         fclose(temp);
  136.         }
  137.     else
  138.         fprintf(stderr,"texeqn: Cannot open %s\n",cptr);
  139.     }
  140.  
  141. }
  142.  
  143. TeXEqn(buffer)            /* srips TEX equations */
  144. char *buffer;
  145. {
  146. int c,d;
  147. int i,j,be;
  148. while ((c = *buffer++) != '\0')
  149.     {
  150.     if(c == '%')
  151.         {
  152.         while ((c = *buffer++) != '\0')
  153.             if (c == '\n') break;
  154.         }
  155.     if(c == '$')
  156.         {
  157.         if ((d = *buffer++) == '$')
  158.             {
  159.             putc(c,stdout);    putc(d,stdout);
  160.             while ((c = *buffer++) != '\0')
  161.                 {
  162.                 if(c != '$')   putc(c,stdout);
  163.                 else
  164.                     {
  165.                     buffer++;
  166.                     fprintf(stdout,"$$ \n");
  167.                     break;
  168.                     }
  169.                 }
  170.             }
  171.         }
  172.     if(c == '\\')
  173.         {            /* check for LaTeX \begin{equation} */
  174.         if ((c = *buffer++) =='b')
  175.         if ((i=begin_end_buf(buffer,&be)) == 0)
  176.             {
  177.             if (be == 1)
  178.                 {
  179.                 fprintf(stdout,"\\begin{equation}");
  180.                 buffer += 14;
  181.                 while ((c = *buffer++) != '\0')
  182.                     {
  183.                     if (c != '\\')
  184.                         putc(c,stdout);
  185.                     else    putc('\\',stdout);
  186.                     if((i=begin_end_buf(buffer,&be)) > 0)
  187.                         {
  188.                         for (j=0; j < i; j++)
  189.                             {
  190.                             c = *buffer++;
  191.                             putc(c,stdout);
  192.                             }
  193.                         }
  194.                     else if (be == 2)
  195.                         {
  196.                         fprintf(stdout,"end{equation}\n");
  197.                         break;
  198.                         }
  199.                     }
  200.                 }
  201.             }
  202.         }
  203.     }
  204. }
  205.