home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / trmatch / trmatch.c < prev   
Encoding:
C/C++ Source or Header  |  1987-02-12  |  8.0 KB  |  332 lines

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