Subversion Repositories wimsdev

Rev

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