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

  1. #include <stdio.h>
  2.  
  3. TeXMatch(fp)            /* check matching */
  4. FILE *fp;
  5. {
  6.  
  7. int l=1;            /* line counter */
  8. int ld=0;            /* single left dollar signs */
  9. int rd=0;            /* single right dollar signs */
  10. int ldd=0;            /* left double dollar signs */
  11. int rdd=0;            /* right double dollar signs */
  12. int lateq=0;            /* LaTeX \begin{equation} \end{equation} */
  13. int lp=0;            /* left paranthesis */
  14. int rp=0;            /* right paranthesis */
  15. int lb=0;            /* left brackets */
  16. int rb=0;            /* right brackets */
  17. int lbr=0;            /* left braces */
  18. int rbr=0;            /* right braces */
  19. int c=' ';            /* current character */
  20. int c1=' ';            /* previous character */
  21. int lbrl=1;            /* line number of left braces */
  22. int lbl=1;            /* line number of left bracket */
  23. int lpl=1;            /* line number of left parenthesis */
  24. int ldl=1;            /* line number of left single dollar sign */
  25. int lddl=1;            /* line number of left double dollar sign */
  26. int lbeg=1;            /* line of \begin{equation} */
  27. int war=0;            /* warning status */
  28. int esc=0;            /* escape status */
  29. int i=0;
  30.  
  31. while ((c =getc(fp)) != EOF)
  32.     {
  33.     if (ldd == 1 && ld == 1 && c != '$')
  34.         {
  35.         fprintf(stderr,"line %d: a double dollar sign is closed by a single dollar sign\n",l);
  36.         ld=0.;    ldd=0.;            /* reset dollar signs */
  37. /* Give warning about unclosed openings */
  38.         if ((lbr-rbr) > 0)
  39.     fprintf(stderr,"line %d: %d unclosed braces in equation\n",lddl,lbr-rbr);
  40.         if ((lb-rb) > 0)
  41.     fprintf(stderr,"line %d: %d unclosed brackets in equation\n",lddl,lb-rb);
  42.         if ((lp-rp) > 0)
  43.     fprintf(stderr,"line %d: %d unclosed parantheses in equation\n",lddl,lp-rp);
  44. /* clear registers */
  45.         lp=0; lb=0; lbr=0;
  46.         rp=0; rb=0; rbr=0;
  47.         lpl=0; lbrl=0; lbl=0;
  48.         }
  49. top:
  50.     switch(c)
  51.         {
  52.         case '\n':
  53.             l++;        /* increment line counter */
  54. /* check to see if a single dollar sign is not closed at the same line */
  55.             if (ld == 1 && war == 0)
  56.                 {
  57.                 fprintf(stderr,"line %d: single dollar sign is not closed on the same line\n",l-1);
  58.                 war=1;        /* warning has been given */
  59.                 }
  60.             esc = 0;    /* escape status */
  61.             break;
  62.         case '%':        /* ignore commented text */
  63.             if (esc == 1)        /* escaped % */
  64.                 { esc=0;    break; }
  65.             while ((c =getc(fp)) != EOF)
  66.                 if (c == '\n')    {l++;    break;}
  67.             esc=0;
  68.             break;
  69.         case '{':
  70.             if (esc == 0)
  71.                 {
  72.                 lbr++;
  73.                 if (lbrl == 0)    lbrl=l;
  74.                 }
  75.             esc = 0;    /* escape status */
  76.             break;
  77.         case '}':
  78.             if (esc == 0)    rbr++;
  79.             esc = 0;    /* escape status */
  80.             if (rbr > lbr)
  81.                 {
  82.                 fprintf(stderr,"line %d: unmatched braces\n",l);
  83.                 rbr--;        /* reset the count */
  84.                 }
  85.             if (lbr == rbr)    lbrl=0;
  86.             break;
  87.         case '[':
  88.             if (esc == 0)
  89.                 {
  90.                 lb++;
  91.                 if (lbl == 0)    lbl=l;
  92.                 }
  93.             esc = 0;    /* escape status */
  94.             break;
  95.         case ']':
  96.             if (esc == 0)    rb++;
  97.             esc = 0;    /* escape status */
  98.             if (rb > lb)
  99.                 {
  100.                  fprintf(stderr,"line %d: unmatched brackets\n",l);
  101.                 rb--;        /* reset the count */
  102.                 }
  103.             if (lb == rb)    lbl=0;
  104.             break;
  105.         case '(':
  106.             if (esc == 0)
  107.                 {
  108.                 lp++;
  109.                 if (lpl == 0)    lpl=l;
  110.                 }
  111.             esc = 0;    /* escape status */
  112.             break;
  113.         case ')':
  114.             if (esc == 0)    rp++;
  115.             esc = 0;    /* escape status */
  116.             if (rp > lp)
  117.                 {
  118.                fprintf(stderr,"line %d: unmatched parentheses\n",l);
  119.                 rp--;        /* reset the count */
  120.                 }
  121.             if (lp == rp)    lpl=0;
  122.             break;
  123.         case '$':
  124.             if (esc == 1)        /* escaped dollar sign */
  125.                 {
  126.                 c=' ';        /* reset the dollar sign */
  127.                 esc = 0;    /* escape status */
  128.                 break;
  129.                 }
  130.             if (c1 == '$')        /* double dollar sign */
  131.                 {
  132.                 if (ld == 0)
  133.                     {
  134.                     fprintf(stderr,"line %d: single dollar sign is closed by a duble dollar sign\n",l);
  135.                     c=' ';        /* reset the dollar sign */
  136.                     break;
  137.                     }
  138.                 if (ldd == 1)
  139.                     {
  140.                     rdd=1; /* right double dollar sign */
  141. /* Give warning about unclosed openings */
  142.                     if ((lbr-rbr) > 0)
  143.     fprintf(stderr,"line %d: %d unclosed braces in equation\n",lddl,lbr-rbr);
  144.                     if ((lb-rb) > 0)
  145.     fprintf(stderr,"line %d: %d unclosed brackets in equation\n",lddl,lb-rb);
  146.                     if ((lp-rp) > 0)
  147.     fprintf(stderr,"line %d: %d unclosed parantheses in equation\n",lddl,lp-rp);
  148. /* clear registers */
  149.                     lp=0; lb=0; lbr=0;
  150.                     rp=0; rb=0; rbr=0;
  151.                     lpl=0; lbrl=0; lbl=0;
  152.                     }
  153.                 else
  154.                     {
  155.                     ldd=1;    /* left double dollar sign */
  156.                     lddl=l;    /* line number */
  157. /* Give warning about unclosed openings */
  158.                     if ((lbr-rbr) > 0)
  159.     fprintf(stderr,"line %d: %d unclosed braces before equation, first opened at line %d\n",lddl,lbr-rbr,lbrl);
  160.                     if ((lb-rb) > 0)
  161.     fprintf(stderr,"line %d: %d unclosed brackets before equation, first opened at line %d\n",lddl,lb-rb,lbl);
  162.                     if ((lp-rp) > 0)
  163.     fprintf(stderr,"line %d: unclosed parentheses before equation, first opened at line %d\n",lddl,lp-rp,lpl);
  164. /* clear registers */
  165.                     lp=0; lb=0; lbr=0;
  166.                     rp=0; rb=0; rbr=0;
  167.                     lpl=0; lbrl=0; lbl=0;
  168.                     }
  169.                 }
  170.             if (ld == 1)    rd=1;    /* right dollar sign */
  171.             else
  172.                 {
  173.                 ld=1;     /* left dollar sign */
  174.                 ldl=l;    /* line number */
  175.                 war=0;    /* no warning has been given */
  176.                 }
  177.             esc = 0;        /* escape status */
  178.             break;
  179.         case '\\':
  180. /* check if \begin{equation} or \end{equation} */
  181.             i=begin_end_file(fp,&c);
  182. /* check escape status */
  183.             if (c == '\\')
  184.                 {
  185.                 if (i == 0)    esc = 0;
  186.                 else        esc = 1;
  187.                 }
  188.             if (i < 0)         /* not equation */
  189.                 {
  190.                 if (c != '\\')    esc = 0;
  191.                 c1 = ' ';    /* doesn't matter */
  192.                 goto top;  
  193.                 }
  194.             if (i == 0)
  195.                 {
  196.                 if (c == '\n')    l++;
  197.                 esc = 0;
  198.                 c1 = ' ';    /* doesn't matter */
  199.                 }
  200.             else if (i == 1)
  201.                 {
  202.                 if (lateq == 1)
  203.     fprintf(stderr,"line %d: new equation starts while equation on line %d is not closed\n",l,lbeg);
  204.                 lateq=1;    /* \begin{equation} */
  205.                 lbeg=l;        /* line number */
  206. /* Give warning about unclosed openings */
  207.                 if ((lbr-rbr) > 0)
  208.     fprintf(stderr,"line %d: %d unclosed braces before equation, first opened at line %d\n",lbeg,lbr-rbr,lbrl);
  209.                 if ((lb-rb) > 0)
  210.     fprintf(stderr,"line %d: %d unclosed brackets before equation, first opened at line %d\n",lbeg,lb-rb,lbl);
  211.                 if ((lp-rp) > 0)
  212.     fprintf(stderr,"line %d: %d unclosed parentheses before equation, first opened at line %d\n",lbeg,lp-rp,lpl);
  213. /* clear registers */
  214.                 lp=0; lb=0; lbr=0;
  215.                 rp=0; rb=0; rbr=0;
  216.                 lpl=0; lbrl=0; lbl=0;
  217.                 }
  218.             else
  219.                 {
  220.                 if (lateq == 0)
  221.     fprintf(stderr,"line %d: equation ends but no beginning\n",l);
  222. /* Give warning about unclosed openings */
  223.                 if ((lbr-rbr) > 0)
  224.     fprintf(stderr,"line %d: %d unclosed braces in equation\n",l,lbr-rbr);
  225.                 if ((lb-rb) > 0)
  226.     fprintf(stderr,"line %d: %d unclosed brackets in equation\n",l,lb-rb);
  227.                 if ((lp-rp) > 0)
  228.     fprintf(stderr,"line %d: %d unclosed parantheses in equation\n",l,lp-rp);
  229. /* clear registers */
  230.                 lp=0; lb=0; lbr=0;
  231.                 rp=0; rb=0; rbr=0;
  232.                 lpl=0; lbrl=0; lbl=0;
  233.                 lateq=0;
  234.                 }
  235.             break;
  236.         default:
  237.             esc=0;            /* escape status */
  238.             break;
  239.         }
  240.     c1=c;                    /* update previous character */
  241.     if (ld == 1 && rd == 1)
  242.         {ld=0.;        rd=0.;}        /* matched dollar signs */
  243.     if (ldd == 1 && rdd == 1)
  244.         {ldd=0.;    rdd=0.;}    /* matched double dollar signs */
  245.     }
  246. if ((lbr-rbr) > 0)
  247.     fprintf(stderr,"file ends: %d unclosed left braces, first opened at line %d \n",lbr-rbr,lbrl);
  248. if ((lb-rb) > 0)
  249.     fprintf(stderr,"file ends: %d unclosed left brackets, first opened at line %d \n",lb-rb,lbl);
  250. if ((lp-rp) > 0)
  251.     fprintf(stderr,"file ends: %d unclosed left parentheses, first opened at line %d \n",lp-rp,lpl);
  252. if (ld == 1)
  253.     fprintf(stderr,"file ends: single dollar sign opened at line %d unclosed\n",ldl);
  254. if (ldd == 1)
  255.     fprintf(stderr,"file ends: double dollar sign opened at line %d unclosed\n",lddl);
  256. if (lateq == 1)
  257.     fprintf(stderr,"file ends: equation started at line %d unclosed\n",lbeg);
  258. }
  259.  
  260. tmpbuf(in,buffer)    /* copy input to buffer, assume buffer is big enough */
  261. FILE *in;
  262. char *buffer;
  263. {
  264. while (( *buffer++ = getc(in)) != EOF) ;
  265. }
  266.  
  267. scrbuf(in,out)            /* copy input to output */
  268. FILE *in,*out;
  269. {
  270. int c;
  271. while ((c =getc(in)) != EOF)    putc(c,out);
  272. }
  273.  
  274. begin_end_buf(buffer,be)
  275. /*
  276. reads from a buffer and returns 1 if \begin{equation} is found,
  277. 2 if \end{equation} is found and -1 if neither is found
  278. */
  279. char *buffer;
  280. int *be;
  281. {
  282.     int i;
  283.     char w[11];
  284.  
  285.     *be = -1;
  286.     for (i=0; (w[i] = *buffer++) != '\0' && i < 5; i++)
  287.         if (w[i] < 'a' || w[i] > 'z')        break;
  288.     buffer--;
  289.     w[i]='\0';
  290.     if (strcmp(w,"egin") == 0)    *be=1;        /* \begin */
  291.     else if (strcmp(w,"end") == 0)    *be=2;        /* \end */
  292.     if ( *be > 0)
  293.         {
  294.         for (i=0; (w[i] = *buffer++) != '\0' && i < 10; i++) ;
  295.         w[i]='\0';
  296.         if (strcmp(w,"{equation}") == 0)
  297.             return(0);
  298.         }
  299.     return(i);
  300. }
  301.  
  302.  
  303. #define MAXWORD 20
  304. begin_end_file(fp,c)
  305. /*
  306. reads from a file and checks for equations; returns 1 if \begin{equation}
  307. is found and 2 if \end{equation} is found; returns -1 if neither is found;
  308. return 0 if no letter is consumed.
  309. */
  310. FILE *fp;
  311. int *c;
  312. {
  313.     int i,be;
  314.     char w[MAXWORD];
  315.  
  316.     for (i=0; (w[i] = getc(fp)) != EOF && i < MAXWORD; i++)
  317.         if (w[i] < 'a' || w[i] > 'z')        break;
  318.     *c=w[i];
  319.     w[i]='\0';
  320.     be = -1;
  321.     if (strcmp(w,"begin") == 0)    be=1;
  322.     else if (strcmp(w,"end") == 0)    be=2;
  323.     if (i == 0)    return(0);
  324.     if (be > 0)
  325.         {
  326.         w[0] = *c;
  327.         for (i=1; (w[i] = getc(fp)) != EOF  && i < MAXWORD; i++)
  328.             if (w[i] < 'a' || w[i] > 'z')
  329.                 if (w[i] != '{')    break;
  330.         *c=w[i];
  331.         w[i]='\0';
  332.         if (strcmp(w,"{equation") == 0)
  333.             return(be);
  334.         *c=' ';            /* so that the brace does not count */
  335.         }
  336.     return(-1);
  337. }
  338.