home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
-
- TeXMatch(fp) /* check matching */
- FILE *fp;
- {
-
- int l=1; /* line counter */
- int ld=0; /* single left dollar signs */
- int rd=0; /* single right dollar signs */
- int ldd=0; /* left double dollar signs */
- int rdd=0; /* right double dollar signs */
- int lateq=0; /* LaTeX \begin{equation} \end{equation} */
- int lp=0; /* left paranthesis */
- int rp=0; /* right paranthesis */
- int lb=0; /* left brackets */
- int rb=0; /* right brackets */
- int lbr=0; /* left braces */
- int rbr=0; /* right braces */
- int c=' '; /* current character */
- int c1=' '; /* previous character */
- int lbrl=1; /* line number of left braces */
- int lbl=1; /* line number of left bracket */
- int lpl=1; /* line number of left parenthesis */
- int ldl=1; /* line number of left single dollar sign */
- int lddl=1; /* line number of left double dollar sign */
- int lbeg=1; /* line of \begin{equation} */
- int war=0; /* warning status */
- int esc=0; /* escape status */
- int i=0;
-
- while ((c =getc(fp)) != EOF)
- {
- if (ldd == 1 && ld == 1 && c != '$')
- {
- fprintf(stderr,"line %d: a double dollar sign is closed by a single dollar sign\n",l);
- ld=0.; ldd=0.; /* reset dollar signs */
- /* Give warning about unclosed openings */
- if ((lbr-rbr) > 0)
- fprintf(stderr,"line %d: %d unclosed braces in equation\n",lddl,lbr-rbr);
- if ((lb-rb) > 0)
- fprintf(stderr,"line %d: %d unclosed brackets in equation\n",lddl,lb-rb);
- if ((lp-rp) > 0)
- fprintf(stderr,"line %d: %d unclosed parantheses in equation\n",lddl,lp-rp);
- /* clear registers */
- lp=0; lb=0; lbr=0;
- rp=0; rb=0; rbr=0;
- lpl=0; lbrl=0; lbl=0;
- }
- top:
- switch(c)
- {
- case '\n':
- l++; /* increment line counter */
- /* check to see if a single dollar sign is not closed at the same line */
- if (ld == 1 && war == 0)
- {
- fprintf(stderr,"line %d: single dollar sign is not closed on the same line\n",l-1);
- war=1; /* warning has been given */
- }
- esc = 0; /* escape status */
- break;
- case '%': /* ignore commented text */
- if (esc == 1) /* escaped % */
- { esc=0; break; }
- while ((c =getc(fp)) != EOF)
- if (c == '\n') {l++; break;}
- esc=0;
- break;
- case '{':
- if (esc == 0)
- {
- lbr++;
- if (lbrl == 0) lbrl=l;
- }
- esc = 0; /* escape status */
- break;
- case '}':
- if (esc == 0) rbr++;
- esc = 0; /* escape status */
- if (rbr > lbr)
- {
- fprintf(stderr,"line %d: unmatched braces\n",l);
- rbr--; /* reset the count */
- }
- if (lbr == rbr) lbrl=0;
- break;
- case '[':
- if (esc == 0)
- {
- lb++;
- if (lbl == 0) lbl=l;
- }
- esc = 0; /* escape status */
- break;
- case ']':
- if (esc == 0) rb++;
- esc = 0; /* escape status */
- if (rb > lb)
- {
- fprintf(stderr,"line %d: unmatched brackets\n",l);
- rb--; /* reset the count */
- }
- if (lb == rb) lbl=0;
- break;
- case '(':
- if (esc == 0)
- {
- lp++;
- if (lpl == 0) lpl=l;
- }
- esc = 0; /* escape status */
- break;
- case ')':
- if (esc == 0) rp++;
- esc = 0; /* escape status */
- if (rp > lp)
- {
- fprintf(stderr,"line %d: unmatched parentheses\n",l);
- rp--; /* reset the count */
- }
- if (lp == rp) lpl=0;
- break;
- case '$':
- if (esc == 1) /* escaped dollar sign */
- {
- c=' '; /* reset the dollar sign */
- esc = 0; /* escape status */
- break;
- }
- if (c1 == '$') /* double dollar sign */
- {
- if (ld == 0)
- {
- fprintf(stderr,"line %d: single dollar sign is closed by a duble dollar sign\n",l);
- c=' '; /* reset the dollar sign */
- break;
- }
- if (ldd == 1)
- {
- rdd=1; /* right double dollar sign */
- /* Give warning about unclosed openings */
- if ((lbr-rbr) > 0)
- fprintf(stderr,"line %d: %d unclosed braces in equation\n",lddl,lbr-rbr);
- if ((lb-rb) > 0)
- fprintf(stderr,"line %d: %d unclosed brackets in equation\n",lddl,lb-rb);
- if ((lp-rp) > 0)
- fprintf(stderr,"line %d: %d unclosed parantheses in equation\n",lddl,lp-rp);
- /* clear registers */
- lp=0; lb=0; lbr=0;
- rp=0; rb=0; rbr=0;
- lpl=0; lbrl=0; lbl=0;
- }
- else
- {
- ldd=1; /* left double dollar sign */
- lddl=l; /* line number */
- /* Give warning about unclosed openings */
- if ((lbr-rbr) > 0)
- fprintf(stderr,"line %d: %d unclosed braces before equation, first opened at line %d\n",lddl,lbr-rbr,lbrl);
- if ((lb-rb) > 0)
- fprintf(stderr,"line %d: %d unclosed brackets before equation, first opened at line %d\n",lddl,lb-rb,lbl);
- if ((lp-rp) > 0)
- fprintf(stderr,"line %d: unclosed parentheses before equation, first opened at line %d\n",lddl,lp-rp,lpl);
- /* clear registers */
- lp=0; lb=0; lbr=0;
- rp=0; rb=0; rbr=0;
- lpl=0; lbrl=0; lbl=0;
- }
- }
- if (ld == 1) rd=1; /* right dollar sign */
- else
- {
- ld=1; /* left dollar sign */
- ldl=l; /* line number */
- war=0; /* no warning has been given */
- }
- esc = 0; /* escape status */
- break;
- case '\\':
- /* check if \begin{equation} or \end{equation} */
- i=begin_end_file(fp,&c);
- /* check escape status */
- if (c == '\\')
- {
- if (i == 0) esc = 0;
- else esc = 1;
- }
- if (i < 0) /* not equation */
- {
- if (c != '\\') esc = 0;
- c1 = ' '; /* doesn't matter */
- goto top;
- }
- if (i == 0)
- {
- if (c == '\n') l++;
- esc = 0;
- c1 = ' '; /* doesn't matter */
- }
- else if (i == 1)
- {
- if (lateq == 1)
- fprintf(stderr,"line %d: new equation starts while equation on line %d is not closed\n",l,lbeg);
- lateq=1; /* \begin{equation} */
- lbeg=l; /* line number */
- /* Give warning about unclosed openings */
- if ((lbr-rbr) > 0)
- fprintf(stderr,"line %d: %d unclosed braces before equation, first opened at line %d\n",lbeg,lbr-rbr,lbrl);
- if ((lb-rb) > 0)
- fprintf(stderr,"line %d: %d unclosed brackets before equation, first opened at line %d\n",lbeg,lb-rb,lbl);
- if ((lp-rp) > 0)
- fprintf(stderr,"line %d: %d unclosed parentheses before equation, first opened at line %d\n",lbeg,lp-rp,lpl);
- /* clear registers */
- lp=0; lb=0; lbr=0;
- rp=0; rb=0; rbr=0;
- lpl=0; lbrl=0; lbl=0;
- }
- else
- {
- if (lateq == 0)
- fprintf(stderr,"line %d: equation ends but no beginning\n",l);
- /* Give warning about unclosed openings */
- if ((lbr-rbr) > 0)
- fprintf(stderr,"line %d: %d unclosed braces in equation\n",l,lbr-rbr);
- if ((lb-rb) > 0)
- fprintf(stderr,"line %d: %d unclosed brackets in equation\n",l,lb-rb);
- if ((lp-rp) > 0)
- fprintf(stderr,"line %d: %d unclosed parantheses in equation\n",l,lp-rp);
- /* clear registers */
- lp=0; lb=0; lbr=0;
- rp=0; rb=0; rbr=0;
- lpl=0; lbrl=0; lbl=0;
- lateq=0;
- }
- break;
- default:
- esc=0; /* escape status */
- break;
- }
- c1=c; /* update previous character */
- if (ld == 1 && rd == 1)
- {ld=0.; rd=0.;} /* matched dollar signs */
- if (ldd == 1 && rdd == 1)
- {ldd=0.; rdd=0.;} /* matched double dollar signs */
- }
- if ((lbr-rbr) > 0)
- fprintf(stderr,"file ends: %d unclosed left braces, first opened at line %d \n",lbr-rbr,lbrl);
- if ((lb-rb) > 0)
- fprintf(stderr,"file ends: %d unclosed left brackets, first opened at line %d \n",lb-rb,lbl);
- if ((lp-rp) > 0)
- fprintf(stderr,"file ends: %d unclosed left parentheses, first opened at line %d \n",lp-rp,lpl);
- if (ld == 1)
- fprintf(stderr,"file ends: single dollar sign opened at line %d unclosed\n",ldl);
- if (ldd == 1)
- fprintf(stderr,"file ends: double dollar sign opened at line %d unclosed\n",lddl);
- if (lateq == 1)
- fprintf(stderr,"file ends: equation started at line %d unclosed\n",lbeg);
- }
-
- tmpbuf(in,buffer) /* copy input to buffer, assume buffer is big enough */
- FILE *in;
- char *buffer;
- {
- while (( *buffer++ = getc(in)) != EOF) ;
- }
-
- scrbuf(in,out) /* copy input to output */
- FILE *in,*out;
- {
- int c;
- while ((c =getc(in)) != EOF) putc(c,out);
- }
-
- begin_end_buf(buffer,be)
- /*
- reads from a buffer and returns 1 if \begin{equation} is found,
- 2 if \end{equation} is found and -1 if neither is found
- */
- char *buffer;
- int *be;
- {
- int i;
- char w[11];
-
- *be = -1;
- for (i=0; (w[i] = *buffer++) != '\0' && i < 5; i++)
- if (w[i] < 'a' || w[i] > 'z') break;
- buffer--;
- w[i]='\0';
- if (strcmp(w,"egin") == 0) *be=1; /* \begin */
- else if (strcmp(w,"end") == 0) *be=2; /* \end */
- if ( *be > 0)
- {
- for (i=0; (w[i] = *buffer++) != '\0' && i < 10; i++) ;
- w[i]='\0';
- if (strcmp(w,"{equation}") == 0)
- return(0);
- }
- return(i);
- }
-
-
- #define MAXWORD 20
- begin_end_file(fp,c)
- /*
- reads from a file and checks for equations; returns 1 if \begin{equation}
- is found and 2 if \end{equation} is found; returns -1 if neither is found;
- return 0 if no letter is consumed.
- */
- FILE *fp;
- int *c;
- {
- int i,be;
- char w[MAXWORD];
-
- for (i=0; (w[i] = getc(fp)) != EOF && i < MAXWORD; i++)
- if (w[i] < 'a' || w[i] > 'z') break;
- *c=w[i];
- w[i]='\0';
- be = -1;
- if (strcmp(w,"begin") == 0) be=1;
- else if (strcmp(w,"end") == 0) be=2;
- if (i == 0) return(0);
- if (be > 0)
- {
- w[0] = *c;
- for (i=1; (w[i] = getc(fp)) != EOF && i < MAXWORD; i++)
- if (w[i] < 'a' || w[i] > 'z')
- if (w[i] != '{') break;
- *c=w[i];
- w[i]='\0';
- if (strcmp(w,"{equation") == 0)
- return(be);
- *c=' '; /* so that the brace does not count */
- }
- return(-1);
- }
-