home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / textools2 / part01 / Match.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-02-12  |  9.8 KB  |  314 lines

  1. /* COPYRIGHT (C) 1987 Kamal Al-Yahya */
  2. #define IN_TM
  3. #include   "setups.h"
  4.  
  5. Match(fp)            /* check matching */
  6. FILE *fp;
  7. {
  8.  
  9. int line=1;            /* line counter */
  10. int ld=0;            /* single left dollar signs */
  11. int rd=0;            /* single right dollar signs */
  12. int ldd=0;            /* left double dollar signs */
  13. int rdd=0;            /* right double dollar signs */
  14. int disp=0;            /* \[  \] */
  15. int disp_line=1;        /* line number of \[ */
  16. int form=0;            /* \(  \) */
  17. int lform=1;            /* line number of \( */
  18. int lp=0;            /* left parenthesis */
  19. int rp=0;            /* right parenthesis */
  20. int lb=0;            /* left brackets */
  21. int rb=0;            /* right brackets */
  22. int lbr=0;            /* left braces */
  23. int rbr=0;            /* right braces */
  24. int c=' ';            /* current character */
  25. int c1=' ';            /* previous character */
  26. int lbrl=0;            /* line number of left braces */
  27. int lbl=0;            /* line number of left bracket */
  28. int lpl=0;            /* line number of left parenthesis */
  29. int ldl=1;            /* line number of left single dollar sign */
  30. int lddl=1;            /* line number of left double dollar sign */
  31. int warn=0;            /* warning status */
  32. int env_count = 0;        /* environment counter */
  33. int i=0, j=0;
  34. char w[MAXWORD];
  35. char *p;
  36. int cc;
  37. extern char *malloc();
  38.  
  39. while ((c =getc(fp)) != EOF)
  40.     {
  41.     if (ldd == 1 && ld == 1 && c != '$')
  42.         {
  43.         fprintf(stderr,"line %d: a double dollar sign is closed by a single dollar sign\n",line);
  44.         ld=0.;    ldd=0.;            /* reset dollar signs */
  45. /* Give warning about unclosed openings */
  46.         if ((lbr-rbr) > 0)
  47.     fprintf(stderr,"line %d: %d unclosed braces in equation\n",lddl,lbr-rbr);
  48.         if ((lb-rb) > 0)
  49.     fprintf(stderr,"line %d: %d unclosed brackets in equation\n",lddl,lb-rb);
  50.         if ((lp-rp) > 0)
  51.     fprintf(stderr,"line %d: %d unclosed parentheses in equation\n",lddl,lp-rp);
  52. /* clear registers */
  53.         lp=0; lb=0; lbr=0;
  54.         rp=0; rb=0; rbr=0;
  55.         lpl=0; lbrl=0; lbl=0;
  56.         }
  57.     switch(c)
  58.         {
  59.         case '\n':
  60.             line++;        /* increment line counter */
  61. /* check to see if a single dollar sign is not closed at the same line */
  62.             if (ld == 1 && warn == 0)
  63.                 {
  64.                 fprintf(stderr,"line %d: single dollar sign is not closed on the same line\n",line-1);
  65.                 warn=1;        /* warning has been given */
  66.                 }
  67.             break;
  68.         case '%':        /* ignore commented text */
  69.             while ((c =getc(fp)) != EOF)
  70.                 if (c == '\n')    {line++;    break;}
  71.             break;
  72.         case '{':
  73.             if (lbrl == 0)    lbrl=line;
  74.             lbr++;
  75.             break;
  76.         case '}':
  77.             rbr++;
  78.             if (rbr > lbr)
  79.                 {
  80.                 fprintf(stderr,"line %d: unmatched brace\n",line);
  81.                 rbr--;        /* reset counter */
  82.                 }
  83.             if (lbr == rbr)    lbrl=0;
  84.             break;
  85.         case '[':
  86.             if (lbl == 0)    lbl=line;
  87.             lb++;
  88.             break;
  89.         case ']':
  90.             rb++;
  91.             if (rb > lb)
  92.                 {
  93.                  fprintf(stderr,"line %d: unmatched bracket\n",line);
  94.                 rb--;        /* reset counter */
  95.                 }
  96.             if (lb == rb)    lbl=0;
  97.             break;
  98.         case '(':
  99.             if (lpl == 0)    lpl=line;
  100.             lp++;
  101.             break;
  102.         case ')':
  103.             rp++;
  104.             if (rp > lp)
  105.                 {
  106.                fprintf(stderr,"line %d: unmatched parenthesis\n",line);
  107.                 rp--;        /* reset counter */
  108.                 }
  109.             if (lp == rp)    lpl=0;
  110.             break;
  111.         case '$':
  112.             if (c1 == '$')        /* double dollar sign */
  113.                 {
  114.                 if (ld == 0)
  115.                     {
  116.                     fprintf(stderr,"line %d: single dollar sign is closed by a duble dollar sign\n",line);
  117.                     c=' ';        /* reset the dollar sign */
  118.                     break;
  119.                     }
  120.                 if (ldd == 1)
  121.                     {
  122.                     rdd=1; /* right double dollar sign */
  123. /* Give warning about unclosed openings */
  124.                     if ((lbr-rbr) > 0)
  125.     fprintf(stderr,"line %d: %d unclosed braces in equation\n",lddl,lbr-rbr);
  126.                     if ((lb-rb) > 0)
  127.     fprintf(stderr,"line %d: %d unclosed brackets in equation\n",lddl,lb-rb);
  128.                     if ((lp-rp) > 0)
  129.     fprintf(stderr,"line %d: %d unclosed parentheses in equation\n",lddl,lp-rp);
  130. /* clear registers */
  131.                     lp=0; lb=0; lbr=0;
  132.                     rp=0; rb=0; rbr=0;
  133.                     lpl=0; lbrl=0; lbl=0;
  134.                     }
  135.                 else
  136.                     {
  137.                     ldd=1;    /* left double dollar sign */
  138.                     lddl=line;    /* line number */
  139. /* Give warning about unclosed openings */
  140.                     if ((lbr-rbr) > 0)
  141.     fprintf(stderr,"line %d: %d unclosed braces before equation, first opened at line %d\n",lddl,lbr-rbr,lbrl);
  142.                     if ((lb-rb) > 0)
  143.     fprintf(stderr,"line %d: %d unclosed brackets before equation, first opened at line %d\n",lddl,lb-rb,lbl);
  144.                     if ((lp-rp) > 0)
  145.     fprintf(stderr,"line %d: %d unclosed parentheses before equation, first opened at line %d\n",lddl,lp-rp,lpl);
  146. /* clear registers */
  147.                     lp=0; lb=0; lbr=0;
  148.                     rp=0; rb=0; rbr=0;
  149.                     lpl=0; lbrl=0; lbl=0;
  150.                     }
  151.                 }
  152.             if (ld == 1)    rd=1;    /* right dollar sign */
  153.             else
  154.                 {
  155.                 ld=1;     /* left dollar sign */
  156.                 ldl=line;    /* line number */
  157.                 warn=0;    /* no warning has been given */
  158.                 }
  159.             break;
  160.         case '\\':
  161. /* check for \begin{...} and \end{...} */
  162.             i = get_file_word(fp,w,&line,&cc);
  163.             if (i == 0 && cc == '[')
  164.                 {
  165.                 if (disp == 1)
  166.     fprintf(stderr,"line %d: displayed equation starts, previous one at line %d not closed\n",line,disp_line);
  167.                 disp=1;     /* left   \] */
  168.                 disp_line=line;
  169. /* Give warning about unclosed openings */
  170.                 if ((lbr-rbr) > 0)
  171.     fprintf(stderr,"line %d: %d unclosed braces before equation\n",line,lbr-rbr);
  172.                 if ((lb-rb) > 0)
  173.     fprintf(stderr,"line %d: %d unclosed brackets before equation\n",line,lb-rb);
  174.                 if ((lp-rp) > 0)
  175.     fprintf(stderr,"line %d: %d unclosed parentheses before equation\n",line,lp-rp);
  176. /* clear registers */
  177.                 lp=0; lb=0; lbr=0;
  178.                 rp=0; rb=0; rbr=0;
  179.                 lpl=0; lbrl=0; lbl=0;
  180.                 }
  181.             else if (i == 0 && cc == ']')
  182.                 {
  183.                 if (disp == 0)
  184.     fprintf(stderr,"line %d: displayed equation ends but no beginning\n",line);
  185.                 disp=0;        /* right \] */
  186. /* Give warning about unclosed openings */
  187.                 if ((lbr-rbr) > 0)
  188.     fprintf(stderr,"line %d: %d unclosed braces in equation\n",line,lbr-rbr);
  189.                 if ((lb-rb) > 0)
  190.     fprintf(stderr,"line %d: %d unclosed brackets in equation\n",line,lb-rb);
  191.                 if ((lp-rp) > 0)
  192.     fprintf(stderr,"line %d: %d unclosed parentheses in equation\n",line,lp-rp);
  193. /* clear registers */
  194.                 lp=0; lb=0; lbr=0;
  195.                 rp=0; rb=0; rbr=0;
  196.                 lpl=0; lbrl=0; lbl=0;
  197.                 }
  198.             else if (i == 0 && cc == '(')
  199.                 {
  200.                 if (form == 1)
  201.     fprintf(stderr,"line %d: formula starts but previous one not closed\n",line);
  202.                 form=1;            /* left \( */
  203.                 lform=line;        /* line of \( */
  204.                 }
  205.             else if (i == 0 && cc == ')')
  206.                 {
  207.                 if (form == 0)
  208.     fprintf(stderr,"line %d: formula ends but no beginning\n",line);
  209.                 form=0;            /* right \) */
  210.                 }
  211.             else if (strcmp(w,"begin") == 0)
  212.                 {
  213.                 (void) get_file_word(fp,w,&line,&cc);
  214.                 if ((j=is_new_env(w,env_count)) < 0)
  215.                     {
  216.                     j = env_count;
  217.                     env[j].env_beg = 0;
  218.                     env[j].env_end = 0;
  219.                     p = (char *) malloc((unsigned)(i*sizeof(char)));
  220.                     strcpy(p,w);
  221.                     env[env_count++].env_name = p;
  222.                     }
  223.                 env[j].beg_line = line;
  224.                 env[j].env_beg++;
  225. /* Give warning about unclosed openings before these environments */
  226.                 if (strcmp(env[j].env_name,"equation") == 0 ||
  227.                     strcmp(env[j].env_name,"eqnarray") == 0 ||
  228.                     strcmp(env[j].env_name,"eqnarray*") == 0 ||
  229.                     strcmp(env[j].env_name,"displaymath") == 0)
  230.                     {
  231.                     if ((lbr-rbr) > 0)
  232.     fprintf(stderr,"line %d: %d unclosed braces before environment ``%s'', first opened at line %d\n",line,lbr-rbr,env[j].env_name,lbrl);
  233.                     if ((lb-rb) > 0)
  234.     fprintf(stderr,"line %d: %d unclosed brackets before environment ``%s'', first opened at line %d\n",line,lb-rb,env[j].env_name,lbl);
  235.                     if ((lp-rp) > 0)
  236.     fprintf(stderr,"line %d: %d unclosed parentheses before environment ``%s'', first opened at line %d\n",line,lp-rp,env[j].env_name,lpl);
  237. /* clear registers */
  238.                     lp=0; lb=0; lbr=0;
  239.                     rp=0; rb=0; rbr=0;
  240.                     lpl=0; lbrl=0; lbl=0;
  241.                     }
  242.                 }
  243.             else if (strcmp(w,"end") == 0)
  244.                 {
  245.                 (void) get_file_word(fp,w,&line,&cc);
  246.                 if ((j=is_new_env(w,env_count)) < 0)
  247.     fprintf(stderr,"line %d: unmatched end for environment ``%s''\n",line,w);
  248.                 else
  249.                 {
  250.                 env[j].env_end++;
  251.                 if (env[j].env_end > env[j].env_beg)
  252.                     {
  253.     fprintf(stderr,"line %d: unmatched end for environment ``%s''\n",line,
  254.         env[j].env_name);
  255.                     env[j].env_end--;    /* reset */
  256.                     }
  257. /* Give warning about unclosed openings in these environments */
  258.                 if (strcmp(env[j].env_name,"equation") == 0 ||
  259.                     strcmp(env[j].env_name,"eqnarray") == 0 ||
  260.                     strcmp(env[j].env_name,"eqnarray*") == 0 ||
  261.                     strcmp(env[j].env_name,"displaymath") == 0)
  262.                     {
  263.                     if ((lbr-rbr) > 0)
  264.     fprintf(stderr,"line %d: %d unclosed braces in environment ``%s''\n",
  265. line,lbr-rbr,env[j].env_name);
  266.                     if ((lb-rb) > 0)
  267.     fprintf(stderr,"line %d: %d unclosed brackets in environment ``%s''\n",
  268. line,lb-rb,env[j].env_name);
  269.                     if ((lp-rp) > 0)
  270.     fprintf(stderr,"line %d: %d unclosed parentheses in environment ``%s''\n",
  271. line,lp-rp,env[j].env_name);
  272. /* clear registers */
  273.                     lp=0; lb=0; lbr=0;
  274.                     rp=0; rb=0; rbr=0;
  275.                     lpl=0; lbrl=0; lbl=0;
  276.                     }
  277.                 }
  278.                 }
  279.             else if (strcmp(w,"def") == 0)
  280.                 (void) def_file(fp,&line,1);
  281.             else if (strcmp(w,"newcommand") == 0)
  282.                 (void) comm_file(fp,&line,1);
  283.             else if (strcmp(w,"newenvironment") == 0)
  284.                 (void) getenv_file(fp,&line,1);
  285.             else if (i > 0)        fseek(fp,-1,1);
  286.             break;
  287.         default:
  288.             break;
  289.         }
  290.     c1=c;                    /* update previous character */
  291.     if (ld == 1 && rd == 1)
  292.         {ld=0.;        rd=0.;}        /* matched dollar signs */
  293.     if (ldd == 1 && rdd == 1)
  294.         {ldd=0.;    rdd=0.;}    /* matched double dollar signs */
  295.     }
  296. if ((lbr-rbr) > 0)
  297.     fprintf(stderr,"file ends: %d unclosed left braces, first opened at line %d \n",lbr-rbr,lbrl);
  298. if ((lb-rb) > 0)
  299.     fprintf(stderr,"file ends: %d unclosed left brackets, first opened at line %d\n",lb-rb,lbl);
  300. if ((lp-rp) > 0)
  301.     fprintf(stderr,"file ends: %d unclosed left parentheses, first opened at line %d\n",lp-rp,lpl);
  302. if (ld == 1)
  303.     fprintf(stderr,"file ends: single dollar sign opened at line %d unclosed\n",ldl);
  304. if (ldd == 1)
  305.     fprintf(stderr,"file ends: double dollar sign opened at line %d unclosed\n",lddl);
  306. if (disp == 1)
  307.     fprintf(stderr,"file ends: displayed equation opened at line %d unclosed\n",disp_line);
  308. if (form == 1)
  309.     fprintf(stderr,"file ends: formula opened at line %d unclosed\n",lform);
  310. for (i=0; i<env_count; i++)
  311.     if (env[i].env_beg > env[i].env_end)
  312.     fprintf(stderr,"file ends: enviornment ``%s'' begun at line %d not ended\n",env[i].env_name,env[i].beg_line);
  313. }
  314.