Subversion Repositories wimsdev

Rev

Rev 3840 | 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
        /* Check the zone of click in an image */
19
 
20
        /* Return value: -1 if no condition is met.
21
         * n>0 for the line of condition meeting the click.
22
         * empty if condition has error. */
23
 
24
/***************** Nothing should need change hereafter *****************/
25
 
26
#include "../Lib/libwims.h"
27
#include "../Flydraw/gd.h"
28
 
29
#define VLEN 1024
30
 
31
int clickx, clicky;
32
int prec;
33
double v[VLEN+2];
34
char namebuf[2048];
35
char oldfile[1024]="";
36
char filebase[10240];
37
gdImagePtr image=NULL;
38
int testcolor;
39
 
40
void fbase(void)
41
{
42
    char *p, *m, *p1, *p2;
43
    p=getenv("w_insdraw_filebase");
44
    m=getenv("module_dir"); if(m==NULL) m="";
45
    if(strncmp(m,"modules/",strlen("modules/"))!=0) {
46
        snprintf(filebase,sizeof(filebase),"gifs"); return;
47
    }
48
    if(p==NULL || *p==0) {
49
        if(strncmp(m,"modules/adm/",strlen("modules/adm"))==0)
50
          snprintf(filebase,sizeof(filebase),"%s/insdraw gifs",m);
51
        else
52
          snprintf(filebase,sizeof(filebase),"%s gifs",m);
53
        return;
54
    }
55
    for(p1=find_word_start(p); *p1; p1=find_word_start(p2)) {
56
        p2=find_word_end(p1); if(*p2) *p2++=0;
57
        if(strstr(p1,"..")!=NULL) continue;
58
        snprintf(filebase+strlen(filebase),
59
                 sizeof(filebase)-strlen(filebase),
60
                 " %s/%s",m,p1);
61
    }
62
    snprintf(filebase+strlen(filebase),
63
             sizeof(filebase)-strlen(filebase),
64
             " gifs");
65
}
66
 
67
        /* File opening: with security */
68
FILE *open4read(char *n)
69
{
70
    char *p, *p1, *p2;
71
    int t;
72
    FILE *f;
73
    n=find_word_start(n);
74
    if(*n==0) return NULL;
75
    p=filebase;
76
    p1=n+strlen(n)-4;if(p1<n || strcasecmp(p1,".gif")!=0) t=1; else t=0;
77
    if(p!=NULL && *p!=0) {
78
        char pbuf[MAX_LINELEN+1];
79
        snprintf(pbuf,sizeof(pbuf),"%s",p);
80
        p=find_word_start(pbuf); if(strstr(p,"..")!=NULL) return NULL;
81
        if(*n=='/' || strstr(n,"..")!=NULL) return NULL;
82
                /* prohibit unusual file/dir names */
83
        for(p1=p;*p1;p1++)
84
          if(!isalnum(*p1) && !isspace(*p1) && strchr("~_-/.",*p1)==NULL)
85
            return NULL;
86
        for(p1=n;*p1;p1++)
87
          if(!isalnum(*p1) && !isspace(*p1) && strchr("~_-/.",*p1)==NULL)
88
            return NULL;
89
        f=NULL;
90
        for(p1=p; *p1; p1=find_word_start(p2)) {
91
            if(*p1=='/') return NULL;
92
            p2=find_word_end(p1);
93
            if(*p2) *p2++=0;
94
            snprintf(namebuf,sizeof(namebuf),"%s/%s",p1,n);
95
            f=fopen(namebuf,"r"); if(f!=NULL) goto imgtype;
96
        }
97
        p1=getenv("w_wims_session");
98
        if(p1!=NULL && strstr(p1,"..")==NULL && strncmp(n,"insert",6)==0) {
99
            snprintf(namebuf,sizeof(namebuf),"../s2/%s/%s",p1,n);
100
            f=fopen(namebuf,"r");
101
        }
102
    }
103
    else {
104
        snprintf(namebuf,sizeof(namebuf),"%s",n);
105
        f=fopen(namebuf,"r");
106
    }
107
    imgtype:
108
    if(t && f!=NULL) {
109
        char tbuf[1024],sbuf[4096];
110
        fclose(f); f=NULL;
111
        p1=getenv("TMPDIR"); if(p1==NULL || *p1==0) p1=".";
112
        snprintf(tbuf,sizeof(tbuf),"%s/drawfile_.gif",p1);
113
        snprintf(sbuf,sizeof(sbuf),"convert %s %s",namebuf,tbuf);
114
        system(sbuf); f=fopen(tbuf,"r");       
115
    }
116
    if(f!=NULL) snprintf(oldfile,sizeof(oldfile),"%s",n);
117
    return f;
118
}
119
 
120
int getvalue(char *p, int n)
121
{
122
    int t;
123
    char *p1;
124
    if(n<0) n=VLEN;
125
    for(t=0; t<n; t++,p=p1) {
126
        p1=find_item_end(p); if(*p1) *p1++=0;
127
        p=find_word_start(p);
128
        if(*p==0) break;
129
        v[t]=strevalue(p);
130
        if(!finite(v[t])) exit(-1);
131
    }
132
    return t;
133
}
134
 
135
        /* test one condition. Returns 1 if met. */
136
int test(char *p)
137
{
138
    char *p1;
139
    int i,t;
140
    double d;
141
    p=find_word_start(p); p1=find_item_end(p);
142
    if(*p1) *p1++=0; else exit(-1);
143
    prec=0;
144
    *p=tolower(*p);
145
    if(strncasecmp(p,"polygon",3)==0 || *p=='g') {
146
        int cross;
147
        double x0,y0,a1,b1,c1,a2,b2,c2,s1,s2,s3,s4;
148
        t=getvalue(p1,-1); if(t<6) exit(-1);
149
        cross=0; x0=-19; y0=-19;
150
        a1=clicky-y0;b1=x0-clickx;c1=clickx*y0-x0*clicky;
151
        v[t]=v[0];v[t+1]=v[1];
152
        for(i=2;i<t+2;i+=2) {
153
            a2=v[i+1]-v[i-1];b2=v[i-2]-v[i];
154
            c2=v[i]*v[i-1]-v[i-2]*v[i+1];
155
            s1=a1*v[i-2]+b1*v[i-1]+c1;
156
            s2=a1*v[i]+b1*v[i+1]+c1;
157
            s3=a2*x0+b2*y0+c2;
158
            s4=a2*clickx+b2*clicky+c2;
159
            if(s1==0) continue;
160
            if(s1*s2<=0 && s3*s4<=0) cross++;
161
        }
162
        return cross&1;
163
    }
164
    if(*p=='p' || strncasecmp(p,"point",3)==0) {
165
        t=getvalue(p1,-1); if(t<2) exit(-1);
166
        for(i=0;i<t;i+=2) {
167
            d=sqrt(pow(v[i]-clickx,2)+pow(v[i+1]-clicky,2));
168
            if(d<=4) return 1;
169
            if(d<=7) {prec=1; return 1;}
170
        }
171
        return 0;
172
    }
173
    if(strncasecmp(p,"rectangle",1)==0) {
174
        double x1,x2,y1,y2;
175
        if(getvalue(p1,4)!=4) exit(-1);
176
        x1=min(v[0],v[2]); x2=max(v[0],v[2]);
177
        y1=min(v[1],v[3]); y2=max(v[1],v[3]);
178
        if(clickx<x1 || clickx>x2 || clicky<y1 || clicky>y2) return 0;
179
        else return 1;
180
    }
181
    if(strncasecmp(p,"circle",1)==0) {
182
        double dist;
183
        if(getvalue(p1,3)!=3 || v[2]<0) exit(-1);
184
        dist=sqrt(pow(v[0]-clickx,2)+pow(v[1]-clicky,2));
185
        if(dist>v[2]/2) return 0; else return 1;
186
    }
187
    if(strncasecmp(p,"ellipse",1)==0) {
188
        double dist;
189
        if(getvalue(p1,4)!=4) exit(-1);
190
        if(v[2]<=0 || v[3]<=0) exit(-1);
191
        dist=sqrt(pow(2*(v[0]-clickx)/v[2],2)+pow(2*(v[1]-clicky)/v[3],2));
192
        if(dist>1) return 0; else return 1;
193
        return 0;
194
    }
195
    if(strncasecmp(p,"bound",1)==0) {
196
        char *p2;
197
        FILE *f;
198
        int c, T;
199
        p2=find_item_end(p1); if(*p2) *p2++=0;
200
        T=getvalue(p2,2); if(T!=2 && T!=0) exit(-1);
201
        p1=find_word_start(p1); *find_word_end(p1)=0;
202
        if(*p1==0) exit(-1);
203
        if(strcmp(p1,oldfile)!=0) {
204
            if(image) gdImageDestroy(image);
205
            f=open4read(p1); if(f==NULL) exit(-1);
206
            image=gdImageCreateFromGif(f); fclose(f);
207
            if(T==0) {
208
                testcolor=gdImageGetPixel(image,1,1);
209
            }
210
            else {
211
                testcolor=gdImageColorAllocate(image,2,3,5);
212
                gdImageFill(image,clickx,clicky,testcolor);
213
            }
214
        }
215
        if(T==0) {
216
            c=gdImageGetPixel(image,clickx,clicky);
217
            if(c!=testcolor) return 1;
218
        }
219
        else {
220
            c=gdImageGetPixel(image,v[0],v[1]);
221
            if(c==testcolor) return 1;
222
        }
223
        return 0;
224
    }
225
    exit(-1);
226
}
227
 
228
int oneline(char *p)
229
{
230
    char *p1, *p2;
231
    int t, rev;
232
 
233
    if(strparchr(p,'|')!=NULL) {
234
        t=0; for(p1=p;p1;p1=p2) {
235
            p2=strparchr(p1,'|'); if(p2!=NULL) *p2++=0;
236
            t|=oneline(p1);
237
        }
238
        return t;
239
    }
240
    if(strparchr(p,'&')!=NULL) {
241
        t=1; for(p1=p;p1;p1=p2) {
242
            p2=strparchr(p1,'&'); if(p2!=NULL) *p2++=0;
243
            t&=oneline(p1);
244
        }
245
        return t;
246
    }
247
    p1=find_word_start(p); rev=0;
248
    if(*p1=='^') {
249
        rev=1; p1=find_word_start(++p1);
250
    }
251
    if(*p1==0) return rev^1;
252
    if(*p1=='(') {
253
        p1++; p2=find_matching(p1,')');
254
        if(p2==NULL) exit(-1);
255
        *p2=0; return oneline(p1)^rev;
256
    }
257
    return test(p1)^rev;
258
}
259
 
260
        /* Returns the number of line matching the click coordinates */
261
int main()
262
{
263
    char *p, *p2, *p3;
264
    int i,j;
265
 
266
    fbase();
267
    p=getenv("wims_exec_parm"); if(p==NULL || *p==0) return -1;
268
    p=find_word_start(p); p2=strchr(p,'\n');
269
    if(p2==NULL) p2=p+strlen(p); else *p2++=0;
270
    p3=strchr(p,','); if(p3==NULL) return -1; else *p3++=0;
271
    clickx=atoi(p); clicky=atoi(p3);
272
    for(i=1,p=find_word_start(p2);*p;i++, p=find_word_start(p2)) {
273
        fprintf(stderr,"Line %d.\n",i);
274
        p2=strchr(p,'\n'); if(p2==NULL) p2=p+strlen(p); else *p2++=0;
275
        if(*p==0) continue;
276
        j=oneline(p);
277
        if(j) {printf("%d %d",i,prec); return 0;}
278
    }
279
    printf("-1"); return 0;
280
}
281