Subversion Repositories wimsdev

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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