home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / textools / part02 / trmatch.c < prev   
Encoding:
C/C++ Source or Header  |  1986-11-30  |  7.2 KB  |  294 lines

  1. /*
  2.  * trmatch: checks matching parantheses, braces, brackets, and dollar signs.
  3.  *        for troff documents
  4.  * non-getpar() version.
  5.  * to compile:  cc trmatch.c -o trmatch
  6.  */
  7.  
  8. char *documentation[] = {
  9. " SYNTAX",
  10. "        trmatch file1 file2 ....",
  11. "",
  12. " See the manula page for more details",
  13. "",
  14. };
  15.  
  16. /* Author:    Kamal Al-Yahya        7/20/1986 */
  17.  
  18. int    doclength = { sizeof documentation/sizeof documentation[0] };
  19.  
  20. #include        <stdio.h>
  21. #include        <sys/ioctl.h>
  22. #include        <sgtty.h>
  23.  
  24. struct sgttyb ttystat;
  25. static char *name="trmatch";
  26. extern char *strcpy(), *mktemp();
  27.  
  28. int xargc;
  29. char **xargv;
  30.  
  31. main(argc,argv)
  32. int argc; 
  33. char *argv[];
  34. {
  35.     FILE *temp;
  36.     register char *cptr;
  37.     int piped_in;
  38.     int i;
  39.  
  40.     /* If no arguments, and not in a pipeline, self document */
  41.     piped_in = ioctl ((fileno (stdin)), TIOCGETP, &ttystat);
  42.     if (argc == 1 && !piped_in)
  43.         {
  44.         for( i=0; i<doclength; i++)
  45.             printf("%s\n",documentation[i]);
  46.         exit (0);
  47.         }
  48.  
  49.      /* first process pipe input */
  50.     if(piped_in)
  51.         {
  52.         troff_match(stdin);
  53.         fprintf(stderr,"\n");
  54.         }
  55.  
  56. /* next process input line arguments and assume they are also input files */
  57.  
  58.     xargc = argc;
  59.     xargv = argv;
  60.     for (xargc--,xargv++; xargc; xargc--,xargv++)
  61.         {
  62.         cptr = *xargv; 
  63.         if((temp=fopen(cptr,"r")) != NULL)
  64.             {
  65.             fprintf(stderr,"%s:\n",cptr);
  66.             troff_match(temp);
  67.             fprintf(stderr,"\n");
  68.             fclose(temp);
  69.             }
  70.         else
  71.             fprintf(stderr,"%s: Cannot open %s\n",name,cptr);
  72.         }
  73.  
  74. }
  75.  
  76. troff_match(fp)            /* check matching */
  77. FILE *fp;
  78. {
  79.  
  80. int l=1;            /* line counter */
  81. int ld=0;            /* single left dollar signs */
  82. int rd=0;            /* single right dollar signs */
  83. int eq=0;            /* eq=1 : equation (delimeted by .EQ and .EN) */
  84. int lp=0;            /* left parantheses */
  85. int rp=0;            /* right parantheses */
  86. int lb=0;            /* left brackets */
  87. int rb=0;            /* right brackets */
  88. int lbr=0;            /* left braces */
  89. int rbr=0;            /* right braces */
  90. int c=' ';            /* current character */
  91. int c1=' ';            /* previous character */
  92. int lbrl=1;            /* line number of left braces */
  93. int lbl=1;            /* line number of left bracket */
  94. int lpl=1;            /* line number of left parentheses */
  95. int ldl=1;            /* line number of left single dollar sign */
  96. int eql=1;            /* line number at which equation is started */
  97. int war=0;            /* warning status */
  98. int esc=0;            /* escape status */
  99.  
  100. while ((c =getc(fp)) != EOF)
  101.     {
  102. top:
  103.     switch(c)
  104.         {
  105.         case '\n':
  106.             l++;        /* increment line counter */
  107. /* check to see if a single dollar sign is not closed at the same line */
  108.             if (ld == 1 && war == 0 && c1 != '\\')
  109.                 {
  110.                 fprintf(stderr,"line %d: WARNING: single dollar sign is not closed on the same line\n",l-1);
  111.                 war=1;        /* warning has been given */
  112.                 }
  113.             esc = 0;        /* escape status */
  114.             break;
  115.         case '{':
  116.             if (esc == 0)
  117.                 {
  118.                 lbr++;
  119.                 if (lbrl == 0) lbrl=l;
  120.                 }
  121.             esc = 0;        /* escape status */
  122.             break;
  123.         case '}':
  124.             if (esc == 0)    rbr++;
  125.             if (rbr > lbr)
  126.                 {
  127.                 fprintf(stderr,"line %d: unmatched braces\n",l);
  128.                 rbr--;        /* reset the count */
  129.                 }
  130.             if (lbr == rbr)    lbrl=0;
  131.             esc = 0;        /* escape status */
  132.             break;
  133.         case '[':
  134.             if (esc == 0)
  135.                 {
  136.                 lb++;
  137.                 if (lbl == 0) lbl=l;
  138.                 }
  139.             esc = 0;        /* escape status */
  140.             break;
  141.         case ']':
  142.             if (esc == 0)    rb++;
  143.             else        esc = 0;    /* escape status */
  144.             if (rb > lb)
  145.                 {
  146.                  fprintf(stderr,"line %d: unmatched brackets\n",l);
  147.                 rb--;        /* reset the count */
  148.                 }
  149.             if (lb == rb)    lbl=0;
  150.             esc = 0;        /* escape status */
  151.             break;
  152.         case '(':
  153.             if (esc == 0)
  154.                 {
  155.                 lp++;
  156.                 if (lpl == 0) lpl=l;
  157.                 }
  158.             esc = 0;        /* escape status */
  159.             break;
  160.         case ')':
  161.             if (esc == 0)    rp++;
  162.             if (rp > lp)
  163.                 {
  164.                fprintf(stderr,"line %d: unmatched parentheses\n",l);
  165.                 rp--;        /* reset the count */
  166.                 }
  167.             if (lp == rp)    lpl=0;
  168.             esc = 0;        /* escape status */
  169.             break;
  170.         case '$':
  171.             if (esc == 1)        /* escaped dollar sign */
  172.                 {
  173.                 c=' ';        /* reset the dollar sign */
  174.                 esc = 0;    /* escape status */
  175.                 break;
  176.                 }
  177.             if (ld == 1)    rd=1;    /* right dollar sign */
  178.             else
  179.                 {
  180.                 ld=1;     /* left dollar sign */
  181.                 ldl=l;    /* line number */
  182.                 war=0;    /* no warning hs been given */
  183.                 }
  184.             esc = 0;        /* escape status */
  185.             break;
  186.         case '.':
  187.             if (c1 == '\n' || l == 1)    /* troff command */
  188.                 {
  189.                 /* see if it is .EQ */
  190.                 c1=getc(fp);
  191.                 if (c1 == '\n')
  192.                     {
  193.                     esc=0;
  194.                     l++;
  195.                     break;
  196.                     }
  197.                 c=getc(fp);
  198.                 if (c == '\n')
  199.                     {
  200.                     esc=0;
  201.                     l++;
  202.                     break;
  203.                     }
  204.                 if (c1 == 'E' && c == 'Q')
  205.                     {
  206.                     if (eq == 1)
  207.     fprintf(stderr,"line %d: equation started while equation at line %d is still open\n",l,eql);
  208.                     eq=1;    /* beginning of equation */
  209.                     eql=l;    /* line number */
  210. /* Give warning about unclosed openings */
  211.                     if ((lbr-rbr) > 0)
  212.     fprintf(stderr,"line %d: %d unclosed braces before equation, first brace opened at line %d\n",eql,lbr-rbr,lbrl);
  213.                     if ((lb-rb) > 0)
  214.     fprintf(stderr,"line %d: %d unclosed brackets before equation, first bracket opened at line %d\n",eql,lb-rb,lbl);
  215.                     if ((lp-rp) > 0)
  216.     fprintf(stderr,"line %d: %d unclosed parentheses before equation, first parenthesis opened at line %d\n",eql,lp-rp,lpl);
  217. /* clear registers */
  218.                     lp=0; lb=0; lbr=0;
  219.                     rp=0; rb=0; rbr=0;
  220.                     lpl=0; lbrl=0; lbl=0;
  221.                     }
  222.                 else if (c1 == 'E' && c == 'N')
  223.                     {
  224.                     if (eq == 0)
  225.     fprintf(stderr,"line %d: equation ends but no equation beginning\n",l);
  226. /* Give warning about unclosed openings */
  227.                     if ((lbr-rbr) > 0)
  228.     fprintf(stderr,"line %d: %d unclosed braces in equation\n",eql,lbr-rbr);
  229.                     if ((lb-rb) > 0)
  230.     fprintf(stderr,"line %d: %d unclosed brackets in equation\n",eql,lb-rb);
  231.                     if ((lp-rp) > 0)
  232.     fprintf(stderr,"line %d: %d unclosed parentheses in equation\n",eql,lp-rp);
  233. /* clear registers */
  234.                     lp=0; lb=0; lbr=0;
  235.                     rp=0; rb=0; rbr=0;
  236.                     lpl=0; lbrl=0; lbl=0;
  237.                     eq=0;    /* end of equation */
  238.                     }
  239.                 else
  240.                     while ((c = getc(fp)) != EOF)
  241.                         if (c == '\n')
  242.                             {
  243.                             l++;
  244.                             break;
  245.                             }
  246.                 }
  247.             esc = 0;        /* escape status */
  248.             break;
  249.         case 'd':
  250. /* check for possible define; ignore define lines */
  251.             esc = 0;
  252.             if (eq == 1 && c1 == '\n')
  253.                 {
  254.                 if ((c = getc(fp)) == 'e')
  255.                 if ((c = getc(fp)) == 'f')
  256.                 if ((c = getc(fp)) == 'i')
  257.                 if ((c = getc(fp)) == 'n')
  258.                 if ((c = getc(fp)) == 'e')
  259.                     while ((c = getc(fp)) != EOF)
  260.                         if (c == '\n')    break;
  261.                 if (c == '\n')    l++;
  262.                 }
  263. /* if we grapped a character not in the word "define", go to switch
  264.    to parse the rest of the line */
  265.             if (!(c=='d'||c=='e'||c=='f'||c=='i'||c=='n'||c=='\n'))
  266.                         goto top;
  267.             c1 = ' ';
  268.             esc = 0;        /* escape status */
  269.             break;
  270.         case '\\':
  271. /* check escape status */
  272.             if (c1 == '\\' && esc == 1)    esc = 0;
  273.             else        esc = 1;
  274.             break;
  275.         default:
  276.             esc = 0;        /* escape status */
  277.             break;
  278.         }
  279.     c1=c;                    /* update previous character */
  280.     if (ld == 1 && rd == 1)
  281.         {ld=0.;        rd=0.;}        /* matched dollar signs */
  282.     }
  283. if ((lbr-rbr) > 0)
  284.     fprintf(stderr,"file ends: %d unclosed left braces, first opened at line %d \n",lbr-rbr,lbrl);
  285. if ((lb-rb) > 0)
  286.     fprintf(stderr,"file ends: %d unclosed left brackets, first opened at line %d \n",lb-rb,lbl);
  287. if ((lp-rp) > 0)
  288.     fprintf(stderr,"file ends: %d unclosed left parentheses, first opened at line %d \n",lp-rp,lpl);
  289. if (ld == 1)
  290.     fprintf(stderr,"file ends: single dollar sign opened at line %d unclosed\n",ldl);
  291. if (eq == 1)
  292.     fprintf(stderr,"file ends: equation started at line %d not closed\n",eql);
  293. }
  294.