Subversion Repositories wimsdev

Rev

Rev 3840 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 2002-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. void error(char *msg)
  19. {
  20.     fprintf(stderr,"%s\n",msg);
  21.     printf("ERROR\n");
  22.     exit(1);
  23. }
  24.  
  25. void strcompress(char *src, char *dest)
  26. {
  27.     char *p1, *p2;
  28.     char lastnospc, lastspc;
  29.     p1=find_word_start(src); p2=dest;
  30.     lastnospc=lastspc=0;
  31.     for(;*p1 && p2-dest<MAX_LINELEN;p1++) {
  32.         if(isspace(*p1)) {lastspc=' '; continue;}
  33.         if(lastspc!=0) {
  34.             if((isalnum(lastnospc) || lastnospc=='_') &&
  35.                (isalnum(*p1) || *p1=='_')) *p2++=' ';
  36.         }
  37.         lastspc=0; *p2++=*p1; lastnospc=*p1;
  38.     }
  39.     *p2=0;
  40. }
  41.  
  42.         /* the type of an expression, with cutpoints */
  43. int _type(char *p, int commas[], int *commacnt)
  44. {
  45.     int i,l,lvl;
  46.     char *p1, *p2, *p3, *p4;
  47.     char buf[MAX_LINELEN+1];
  48.  
  49.     lvl=-1; *commacnt=0;
  50.     for(p1=find_word_start(p), p2=p1; *p2; p2=find_word_start(p3)) {
  51.         if(*p2=='.' || isdigit(*p2)) {
  52.             if(lvl<exp_number) lvl=exp_number;
  53.             strtod(p2,&p3); continue;
  54.         }
  55.         if(*p2=='(') {
  56.             p3=find_matching(p2+1,')');
  57.             if(lvl<exp_paren) lvl=exp_paren;
  58.             paren:
  59.             if(p3==NULL) error("Unmatched parentheses");
  60.             p3++; continue;
  61.         }
  62.         if(*p2=='[') {
  63.             if(lvl<exp_matrix) lvl=exp_matrix;
  64.             p3=find_matching(p2+1,']'); goto paren;
  65.         }
  66.         if(*p2=='{') {
  67.             if(lvl<exp_set) lvl=exp_set;
  68.             p3=find_matching(p2+1,'}'); goto paren;
  69.         }
  70.         if(isalpha(*p2)) {
  71.             for(p3=p2; *p3=='_' || isalnum(*p3); p3++);
  72.             if(p3-p2>=16) goto notdefined;
  73.             memmove(buf,p2,p3-p2); buf[p3-p2]=0;
  74.             for(i=0;i<opalphano && strcmp(buf,opalpha[i].name)!=0; i++);
  75.             if(i<opalphano) {
  76.                 l=opalpha[i].lvl;
  77.                 if(l>lvl) {*commacnt=0; lvl=l;}
  78.                 if(l==lvl && *commacnt<MAX_COMMAS-2) {
  79.                     commas[(*commacnt)++]=p2-p;
  80.                     commas[(*commacnt)++]=p3-p;
  81.                 }
  82.                 continue;
  83.             }
  84.             notdefined: p4=find_word_start(p3);
  85.             if(*p4=='(') {
  86.                 if(lvl<exp_fn) lvl=exp_fn;
  87.                 p3=find_matching(p4+1,')');
  88.                 if(p3==NULL) error("Unmatched parentheses.");
  89.                 p4++; memmove(buf,p4,p3-p2); buf[p3-p4]=0;
  90.                 p3++;
  91.             }
  92.             else {
  93.                 if(lvl<exp_variable) lvl=exp_variable;
  94.             }
  95.             continue;
  96.         }
  97.         for(i=0;i<oppunctno && strncmp(p2,oppunct[i].name,strlen(oppunct[i].name))!=0; i++);
  98.         if(i>=oppunctno) error("Unknown operator.");
  99.         p3=p2+strlen(oppunct[i].name); l=oppunct[i].lvl;
  100.         if(l>lvl) {*commacnt=0; lvl=l;}
  101.         if(l==lvl && *commacnt<MAX_COMMAS-2) {
  102.             commas[(*commacnt)++]=p2-p;
  103.             commas[(*commacnt)++]=p3-p;
  104.         }
  105.     }
  106.     return lvl;
  107. }
  108.  
  109. void getregex(char *p)
  110. {
  111.     char *p1, *p2, *p3;
  112.  
  113.     p1=find_word_start(p);
  114.     for(regexcnt=0; regexcnt<MAX_REGEX && *p1!=0; p1=find_word_start(p2)) {
  115.         p2=find_word_end(p1); if(*p2) *p2++=0;
  116.         regexchk[regexcnt].srcreg=p1;
  117.         for(p3=p1; *p3 && (isalnum(*p3) || *p3=='.'); p3++);
  118.         if(*p3==0) {
  119.             regexchk[regexcnt].isvar=1; regexcnt++;
  120.         }
  121.         else {
  122.             regexchk[regexcnt].isvar=0;
  123.             if(regcomp(&(regexchk[regexcnt].cmpreg),p1,REG_EXTENDED|REG_ICASE)==0)
  124.               regexcnt++;
  125.         }
  126.     }
  127. }
  128.  
  129.         /* returns 1 if yes, 0 if no. */
  130. int checkregex(char *p)
  131. {
  132.     int i;
  133.     char buf[MAX_LINELEN+1];
  134.     regmatch_t matchbuf[100];
  135.    
  136.     if(regexcnt<1) return 1; /* nothing to check; always true. */
  137.     strcompress(p,buf);
  138.     for(i=0;i<regexcnt;i++) {   /* all regex words are ANDed. */
  139.         if(regexchk[i].isvar) {
  140.             if(varchr(buf,regexchk[i].srcreg)==NULL) return 0;
  141.         }
  142.         else if(regexec(&(regexchk[i].cmpreg),buf,80,matchbuf,0)!=0) return 0;
  143.     }
  144.     return 1;
  145. }
  146.  
  147.