|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T t
Length: 7341 (0x1cad) Types: TextFile Names: »trmatch.c«
└─⟦a0efdde77⟧ Bits:30001252 EUUGD11 Tape, 1987 Spring Conference Helsinki └─⟦this⟧ »EUUGD11/euug-87hel/sec1/textools/trmatch.c«
/* * trmatch: checks matching parantheses, braces, brackets, and dollar signs. * for troff documents * non-getpar() version. * to compile: cc trmatch.c -o trmatch */ char *documentation[] = { " SYNTAX", " trmatch file1 file2 ....", "", " See the manula page for more details", "", }; /* Author: Kamal Al-Yahya 7/20/1986 */ int doclength = { sizeof documentation/sizeof documentation[0] }; #include <stdio.h> #include <sys/ioctl.h> #include <sgtty.h> struct sgttyb ttystat; static char *name="trmatch"; extern char *strcpy(), *mktemp(); int xargc; char **xargv; main(argc,argv) int argc; char *argv[]; { FILE *temp; register char *cptr; int piped_in; int i; /* If no arguments, and not in a pipeline, self document */ piped_in = ioctl ((fileno (stdin)), TIOCGETP, &ttystat); if (argc == 1 && !piped_in) { for( i=0; i<doclength; i++) printf("%s\n",documentation[i]); exit (0); } /* first process pipe input */ if(piped_in) { troff_match(stdin); fprintf(stderr,"\n"); } /* next process input line arguments and assume they are also input files */ xargc = argc; xargv = argv; for (xargc--,xargv++; xargc; xargc--,xargv++) { cptr = *xargv; if((temp=fopen(cptr,"r")) != NULL) { fprintf(stderr,"%s:\n",cptr); troff_match(temp); fprintf(stderr,"\n"); fclose(temp); } else fprintf(stderr,"%s: Cannot open %s\n",name,cptr); } } troff_match(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 eq=0; /* eq=1 : equation (delimeted by .EQ and .EN) */ int lp=0; /* left parantheses */ int rp=0; /* right parantheses */ 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 parentheses */ int ldl=1; /* line number of left single dollar sign */ int eql=1; /* line number at which equation is started */ int war=0; /* warning status */ int esc=0; /* escape status */ while ((c =getc(fp)) != EOF) { 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 && c1 != '\\') { fprintf(stderr,"line %d: WARNING: 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 '{': if (esc == 0) { lbr++; if (lbrl == 0) lbrl=l; } esc = 0; /* escape status */ break; case '}': if (esc == 0) rbr++; if (rbr > lbr) { fprintf(stderr,"line %d: unmatched braces\n",l); rbr--; /* reset the count */ } if (lbr == rbr) lbrl=0; esc = 0; /* escape status */ break; case '[': if (esc == 0) { lb++; if (lbl == 0) lbl=l; } esc = 0; /* escape status */ break; case ']': if (esc == 0) rb++; else esc = 0; /* escape status */ if (rb > lb) { fprintf(stderr,"line %d: unmatched brackets\n",l); rb--; /* reset the count */ } if (lb == rb) lbl=0; esc = 0; /* escape status */ break; case '(': if (esc == 0) { lp++; if (lpl == 0) lpl=l; } esc = 0; /* escape status */ break; case ')': if (esc == 0) rp++; if (rp > lp) { fprintf(stderr,"line %d: unmatched parentheses\n",l); rp--; /* reset the count */ } if (lp == rp) lpl=0; esc = 0; /* escape status */ break; case '$': if (esc == 1) /* escaped dollar sign */ { c=' '; /* reset the dollar sign */ esc = 0; /* escape status */ break; } if (ld == 1) rd=1; /* right dollar sign */ else { ld=1; /* left dollar sign */ ldl=l; /* line number */ war=0; /* no warning hs been given */ } esc = 0; /* escape status */ break; case '.': if (c1 == '\n' || l == 1) /* troff command */ { /* see if it is .EQ */ c1=getc(fp); if (c1 == '\n') { esc=0; l++; break; } c=getc(fp); if (c == '\n') { esc=0; l++; break; } if (c1 == 'E' && c == 'Q') { if (eq == 1) fprintf(stderr,"line %d: equation started while equation at line %d is still open\n",l,eql); eq=1; /* beginning of equation */ eql=l; /* line number */ /* Give warning about unclosed openings */ if ((lbr-rbr) > 0) fprintf(stderr,"line %d: %d unclosed braces before equation, first brace opened at line %d\n",eql,lbr-rbr,lbrl); if ((lb-rb) > 0) fprintf(stderr,"line %d: %d unclosed brackets before equation, first bracket opened at line %d\n",eql,lb-rb,lbl); if ((lp-rp) > 0) fprintf(stderr,"line %d: %d unclosed parentheses before equation, first parenthesis opened at line %d\n",eql,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 (c1 == 'E' && c == 'N') { if (eq == 0) fprintf(stderr,"line %d: equation ends but no equation beginning\n",l); /* Give warning about unclosed openings */ if ((lbr-rbr) > 0) fprintf(stderr,"line %d: %d unclosed braces in equation\n",eql,lbr-rbr); if ((lb-rb) > 0) fprintf(stderr,"line %d: %d unclosed brackets in equation\n",eql,lb-rb); if ((lp-rp) > 0) fprintf(stderr,"line %d: %d unclosed parentheses in equation\n",eql,lp-rp); /* clear registers */ lp=0; lb=0; lbr=0; rp=0; rb=0; rbr=0; lpl=0; lbrl=0; lbl=0; eq=0; /* end of equation */ } else while ((c = getc(fp)) != EOF) if (c == '\n') { l++; break; } } esc = 0; /* escape status */ break; case 'd': /* check for possible define; ignore define lines */ esc = 0; if (eq == 1 && c1 == '\n') { if ((c = getc(fp)) == 'e') if ((c = getc(fp)) == 'f') if ((c = getc(fp)) == 'i') if ((c = getc(fp)) == 'n') if ((c = getc(fp)) == 'e') while ((c = getc(fp)) != EOF) if (c == '\n') break; if (c == '\n') l++; } /* if we grapped a character not in the word "define", go to switch to parse the rest of the line */ if (!(c=='d'||c=='e'||c=='f'||c=='i'||c=='n'||c=='\n')) goto top; c1 = ' '; esc = 0; /* escape status */ break; case '\\': /* check escape status */ if (c1 == '\\' && esc == 1) esc = 0; else esc = 1; 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 ((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 (eq == 1) fprintf(stderr,"file ends: equation started at line %d not closed\n",eql); }