Back to index

wims  3.65+svn20090927
basic.c
Go to the documentation of this file.
00001 /*    Copyright (C) 2002-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018 void error(char *msg)
00019 {
00020     fprintf(stderr,"%s\n",msg);
00021     printf("ERROR\n");
00022     exit(1);
00023 }
00024 
00025 void strcompress(char *src, char *dest)
00026 {
00027     char *p1, *p2;
00028     char lastnospc, lastspc;
00029     p1=find_word_start(src); p2=dest;
00030     lastnospc=lastspc=0;
00031     for(;*p1 && p2-dest<MAX_LINELEN;p1++) {
00032        if(isspace(*p1)) {lastspc=' '; continue;}
00033        if(lastspc!=0) {
00034            if((isalnum(lastnospc) || lastnospc=='_') &&
00035               (isalnum(*p1) || *p1=='_')) *p2++=' ';
00036        }
00037        lastspc=0; *p2++=*p1; lastnospc=*p1;
00038     }
00039     *p2=0;
00040 }
00041 
00042        /* the type of an expression, with cutpoints */
00043 int _type(char *p, int commas[], int *commacnt)
00044 {
00045     int i,l,lvl;
00046     char *p1, *p2, *p3, *p4;
00047     char buf[MAX_LINELEN+1];
00048 
00049     lvl=-1; *commacnt=0;
00050     for(p1=find_word_start(p), p2=p1; *p2; p2=find_word_start(p3)) {
00051        if(*p2=='.' || isdigit(*p2)) {
00052            if(lvl<exp_number) lvl=exp_number;
00053            strtod(p2,&p3); continue;
00054        }
00055        if(*p2=='(') {
00056            p3=find_matching(p2+1,')');
00057            if(lvl<exp_paren) lvl=exp_paren;
00058            paren: 
00059            if(p3==NULL) error("Unmatched parentheses");
00060            p3++; continue;
00061        }
00062        if(*p2=='[') {
00063            if(lvl<exp_matrix) lvl=exp_matrix;
00064            p3=find_matching(p2+1,']'); goto paren;
00065        }
00066        if(*p2=='{') {
00067            if(lvl<exp_set) lvl=exp_set;
00068            p3=find_matching(p2+1,'}'); goto paren;
00069        }
00070        if(isalpha(*p2)) {
00071            for(p3=p2; *p3=='_' || isalnum(*p3); p3++);
00072            if(p3-p2>=16) goto notdefined;
00073            memmove(buf,p2,p3-p2); buf[p3-p2]=0;
00074            for(i=0;i<opalphano && strcmp(buf,opalpha[i].name)!=0; i++);
00075            if(i<opalphano) {
00076               l=opalpha[i].lvl; 
00077               if(l>lvl) {*commacnt=0; lvl=l;}
00078               if(l==lvl && *commacnt<MAX_COMMAS-2) {
00079                   commas[(*commacnt)++]=p2-p;
00080                   commas[(*commacnt)++]=p3-p;
00081               }
00082               continue;
00083            }
00084            notdefined: p4=find_word_start(p3);
00085            if(*p4=='(') {
00086               if(lvl<exp_fn) lvl=exp_fn;
00087               p3=find_matching(p4+1,')');
00088               if(p3==NULL) error("Unmatched parentheses.");
00089               p4++; memmove(buf,p4,p3-p2); buf[p3-p4]=0;
00090               p3++;
00091            }
00092            else {
00093               if(lvl<exp_variable) lvl=exp_variable;
00094            }
00095            continue;
00096        }
00097        for(i=0;i<oppunctno && strncmp(p2,oppunct[i].name,strlen(oppunct[i].name))!=0; i++);
00098        if(i>=oppunctno) error("Unknown operator.");
00099        p3=p2+strlen(oppunct[i].name); l=oppunct[i].lvl;
00100        if(l>lvl) {*commacnt=0; lvl=l;}
00101        if(l==lvl && *commacnt<MAX_COMMAS-2) {
00102            commas[(*commacnt)++]=p2-p;
00103            commas[(*commacnt)++]=p3-p;
00104        }
00105     }
00106     return lvl;
00107 }
00108 
00109 void getregex(char *p)
00110 {
00111     char *p1, *p2, *p3;
00112 
00113     p1=find_word_start(p);
00114     for(regexcnt=0; regexcnt<MAX_REGEX && *p1!=0; p1=find_word_start(p2)) {
00115        p2=find_word_end(p1); if(*p2) *p2++=0;
00116        regexchk[regexcnt].srcreg=p1;
00117        for(p3=p1; *p3 && (isalnum(*p3) || *p3=='.'); p3++);
00118        if(*p3==0) {
00119            regexchk[regexcnt].isvar=1; regexcnt++;
00120        }
00121        else {
00122            regexchk[regexcnt].isvar=0;
00123            if(regcomp(&(regexchk[regexcnt].cmpreg),p1,REG_EXTENDED|REG_ICASE)==0)
00124              regexcnt++;
00125        }
00126     }
00127 }
00128 
00129        /* returns 1 if yes, 0 if no. */
00130 int checkregex(char *p)
00131 {
00132     int i;
00133     char buf[MAX_LINELEN+1];
00134     regmatch_t matchbuf[100];
00135     
00136     if(regexcnt<1) return 1; /* nothing to check; always true. */
00137     strcompress(p,buf);
00138     for(i=0;i<regexcnt;i++) {      /* all regex words are ANDed. */
00139        if(regexchk[i].isvar) {
00140            if(varchr(buf,regexchk[i].srcreg)==NULL) return 0;
00141        }
00142        else if(regexec(&(regexchk[i].cmpreg),buf,80,matchbuf,0)!=0) return 0;
00143     }
00144     return 1;
00145 }
00146