Subversion Repositories wimsdev

Rev

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

Rev Author Line No. Line
10 reyssat 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
 */
8136 bpr 17
#include "../../Lib/libwims.h"
18
#include "mathexp.h"
10 reyssat 19
void error(char *msg)
20
{
21
    fprintf(stderr,"%s\n",msg);
22
    printf("ERROR\n");
23
    exit(1);
24
}
25
 
26
void strcompress(char *src, char *dest)
27
{
28
    char *p1, *p2;
29
    char lastnospc, lastspc;
30
    p1=find_word_start(src); p2=dest;
31
    lastnospc=lastspc=0;
32
    for(;*p1 && p2-dest<MAX_LINELEN;p1++) {
8136 bpr 33
     if(isspace(*p1)) {lastspc=' '; continue;}
34
     if(lastspc!=0) {
35
         if((isalnum(lastnospc) || lastnospc=='_') &&
36
            (isalnum(*p1) || *p1=='_')) *p2++=' ';
37
     }
38
     lastspc=0; *p2++=*p1; lastnospc=*p1;
10 reyssat 39
    }
40
    *p2=0;
41
}
42
 
8136 bpr 43
/* the type of an expression, with cutpoints */
10 reyssat 44
int _type(char *p, int commas[], int *commacnt)
45
{
46
    int i,l,lvl;
47
    char *p1, *p2, *p3, *p4;
48
    char buf[MAX_LINELEN+1];
49
 
50
    lvl=-1; *commacnt=0;
51
    for(p1=find_word_start(p), p2=p1; *p2; p2=find_word_start(p3)) {
8136 bpr 52
     if(*p2=='.' || isdigit(*p2)) {
53
         if(lvl<exp_number) lvl=exp_number;
54
         (void)strtod(p2,&p3); continue;
55
     }
56
     if(*p2=='(') {
57
         p3=find_matching(p2+1,')');
58
         if(lvl<exp_paren) lvl=exp_paren;
59
         paren:
60
         if(p3==NULL) error("Unmatched parentheses");
61
         p3++; continue;
62
     }
63
     if(*p2=='[') {
64
         if(lvl<exp_matrix) lvl=exp_matrix;
65
         p3=find_matching(p2+1,']'); goto paren;
66
     }
67
     if(*p2=='{') {
68
         if(lvl<exp_set) lvl=exp_set;
69
         p3=find_matching(p2+1,'}'); goto paren;
70
     }
71
     if(isalpha(*p2)) {
72
         for(p3=p2; *p3=='_' || isalnum(*p3); p3++);
73
         if(p3-p2>=16) goto notdefined;
74
         memmove(buf,p2,p3-p2); buf[p3-p2]=0;
75
         for(i=0;i<opalphano && strcmp(buf,opalpha[i].name)!=0; i++);
76
         if(i<opalphano) {
77
          l=opalpha[i].lvl;
78
          if(l>lvl) {*commacnt=0; lvl=l;}
79
          if(l==lvl && *commacnt<MAX_COMMAS-2) {
80
              commas[(*commacnt)++]=p2-p;
81
              commas[(*commacnt)++]=p3-p;
82
          }
83
          continue;
84
         }
85
         notdefined: p4=find_word_start(p3);
86
         if(*p4=='(') {
87
          if(lvl<exp_fn) lvl=exp_fn;
88
          p3=find_matching(p4+1,')');
89
          if(p3==NULL) error("Unmatched parentheses.");
90
          p4++; memmove(buf,p4,p3-p2); buf[p3-p4]=0;
91
          p3++;
92
         }
93
         else {
94
          if(lvl<exp_variable) lvl=exp_variable;
95
         }
96
         continue;
97
     }
98
     for(i=0;i<oppunctno && strncmp(p2,oppunct[i].name,strlen(oppunct[i].name))!=0; i++);
99
     if(i>=oppunctno) error("Unknown operator.");
100
     p3=p2+strlen(oppunct[i].name); l=oppunct[i].lvl;
101
     if(l>lvl) {*commacnt=0; lvl=l;}
102
     if(l==lvl && *commacnt<MAX_COMMAS-2) {
103
         commas[(*commacnt)++]=p2-p;
104
         commas[(*commacnt)++]=p3-p;
105
     }
10 reyssat 106
    }
107
    return lvl;
108
}
109
 
110
void getregex(char *p)
111
{
112
    char *p1, *p2, *p3;
113
 
114
    p1=find_word_start(p);
115
    for(regexcnt=0; regexcnt<MAX_REGEX && *p1!=0; p1=find_word_start(p2)) {
8136 bpr 116
     p2=find_word_end(p1); if(*p2) *p2++=0;
117
     regexchk[regexcnt].srcreg=p1;
118
     for(p3=p1; *p3 && (isalnum(*p3) || *p3=='.'); p3++);
119
     if(*p3==0) {
120
         regexchk[regexcnt].isvar=1; regexcnt++;
121
     }
122
     else {
123
         regexchk[regexcnt].isvar=0;
124
         if(regcomp(&(regexchk[regexcnt].cmpreg),p1,REG_EXTENDED|REG_ICASE)==0)
125
           regexcnt++;
126
     }
10 reyssat 127
    }
128
}
129
 
8136 bpr 130
/* returns 1 if yes, 0 if no. */
10 reyssat 131
int checkregex(char *p)
132
{
133
    int i;
134
    char buf[MAX_LINELEN+1];
135
    regmatch_t matchbuf[100];
8136 bpr 136
 
10 reyssat 137
    if(regexcnt<1) return 1; /* nothing to check; always true. */
138
    strcompress(p,buf);
8136 bpr 139
    for(i=0;i<regexcnt;i++) {     /* all regex words are ANDed. */
140
     if(regexchk[i].isvar) {
141
         if(varchr(buf,regexchk[i].srcreg)==NULL) return 0;
142
     }
143
     else if(regexec(&(regexchk[i].cmpreg),buf,80,matchbuf,0)!=0) return 0;
10 reyssat 144
    }
145
    return 1;
146
}