Subversion Repositories wimsdev

Rev

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

  1. /*    Copyright (C) 1998-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. #include "../Lib/libwims.h"
  19.  
  20. char *bufprep(char *p)
  21. {
  22. /*  singlespace(p); strip_trailing_spaces(p); return find_word_start(p); */
  23.     nospace(p); return p;
  24. }
  25.  
  26. char *parend(char *p)
  27. {
  28.     char *pp; int t;
  29.     t=0; for(pp=p;*pp;pp++) {
  30.       if(*pp=='(') t++;
  31.       if(*pp==')') {t--; if(t==0) return pp; if(t<0) return NULL;}
  32.     }
  33.     return NULL;
  34. }
  35.  
  36. char *relation_type[]={
  37.     "sametext","samecase",
  38.     "in",  "wordof","itemof","lineof","varof","variableof"
  39. };
  40. #define total_relations (sizeof(relation_type)/sizeof(relation_type[0]))
  41.  
  42. /* Here we only check syntax correctness. Returns 1 if OK. */
  43. int _check_compare(char *p, int lvl)
  44. {
  45.     char *p1, *p2, *pp, *pt;
  46.     char *r1, *r2;
  47.     int i, l, k, gotl;
  48.  
  49. /* Check global pairs of parentheses */
  50.     p2=strip_trailing_spaces(p);
  51.     p1=find_word_start(p);
  52.     while(*p1=='(' && *p2==')' && p2==parend(p1)) {
  53.       lvl=0; p1=find_word_start(++p1);
  54.       do p2--; while(p2>=p1 && myisspace(*p2));
  55.       p2[1]=0;
  56.     }
  57.     gotl=100; r1=r2=p1;
  58.     for(pp=p1; *pp; pp++) {
  59.       if(*pp==')') return 0;
  60.       if(*pp=='(') {
  61.           pp=parend(pp); if(pp==NULL) return 0;
  62.           continue;
  63.       }
  64.       if(gotl>3) {
  65.           switch(*pp) {
  66.             case '<': {
  67.                 gotl=3; r1=pp; r2=r1+1;
  68.                 if(*r2=='=') {r2++;break;}
  69.                 if(*r2=='>') r2++;
  70.                 break;
  71.             }
  72.             case '>': {
  73.                 gotl=3; r1=pp; r2=r1+1;
  74.                 if(*r2=='=') r2++;
  75.                 break;
  76.             }
  77.             case '=': {
  78.                 gotl=3; r1=pp; r2=r1+1; if(*r2=='=') r2++;
  79.                 break;
  80.             }
  81.             case '!': {
  82.                 if(pp[1]=='=') {
  83.                   gotl=3; r1=pp; r2=pp+2;
  84.                 }
  85.                 break;
  86.             }
  87.           }
  88.           if(r2>p1) {
  89.             if(lvl==2) break;
  90.             pp=r2-1; continue;
  91.           }
  92.       }
  93.       if(!myisspace(*pp)) continue;
  94.       pp=find_word_start(pp);
  95.       if(gotl>3) {
  96.           if(pp[0]=='i' && pp[1]=='s') {k=2; goto isnot;}
  97.           if(pp[0]=='n' && pp[1]=='o' && pp[2]=='t') {k=3; goto isnot;}
  98.           goto rel;
  99.           isnot:
  100.           if(strchr("siwlv",pp[k])==NULL) goto rel;
  101.           pt=pp; pp+=k; l=0;
  102.           for(i=0;i<total_relations;i++) {
  103.             l=strlen(relation_type[i]);
  104.             if(strncmp(pp, relation_type[i], l)==0 &&
  105.                ((!myisalnum(pp[l]) && pp[l]!='_') || pp[l]==0)) break;
  106.           }
  107.           if(i>=total_relations) {pp--; continue;}
  108.           gotl=3; pp+=l; r1=pt; r2=pp;
  109.           if(lvl==2) break; else {pp--; continue;}
  110.       }
  111.       rel:
  112.       if(*pp!='|' && *pp!='&' && *pp!='a' && *pp!='o')
  113.           {pp--; continue;}
  114.       if(gotl>2 &&
  115.          ((myisspace(pp[3]) && strncmp(pp,"and",3)==0) ||
  116.           (myisspace(pp[2]) && strncmp(pp,"&&",2)==0))) {
  117.           gotl=2; r1=pp; pp=r2=find_word_end(r1);
  118.           if(lvl==1) break; else {pp--;continue;}
  119.       }
  120.       if(gotl>1 && myisspace(pp[2]) &&
  121.          (strncmp(pp,"or",2)==0 || strncmp(pp,"||",2)==0)) {
  122.           gotl=1; r1=pp; r2=pp=r1+2; break;
  123.       }
  124.     }
  125.     switch(gotl) {
  126.       case 1: { /* or */
  127.           *r1=0;
  128.           if(!_check_compare(p1,1)) return 0;
  129.           if(!_check_compare(r2,0)) return 0;
  130.           return 1;
  131.       }
  132.       case 2: { /* and */
  133.           *r1=0;
  134.           if(!_check_compare(p1,2)) return 0;
  135.           if(!_check_compare(r2,1)) return 0;
  136.       }
  137.       case 3: {
  138.           return 1; /* this is considered as OK. */
  139.       }
  140.     }
  141.     return 0;
  142. }
  143.  
  144. int check_compare(char *p)
  145. {
  146.     char buf[MAX_LINELEN+1];
  147.     char *pp;
  148.     snprintf(buf,sizeof(buf),"%s",p);
  149.     pp=strparchr(buf,'?'); if(pp) *pp=0;
  150.     return _check_compare(buf,0);
  151. }
  152.  
  153.  
  154.