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

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