summaryrefslogtreecommitdiffstats
path: root/apps/plugins/searchengine/parser.c
blob: 76beab49cc69916be3d4bf1ecaf1e30e1902f2f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 by Michiel van der Kolk 
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "searchengine.h"
#include "token.h"
#include "dbinterface.h"
#include "parser.h"

struct token *currentToken, curtoken;
unsigned char *filter[20],*nofilter=0;
int currentlevel=0;
int syntaxerror;
int parse_fd;
char errormsg[250];

unsigned char *parse(int fd) {
    unsigned char *ret=0;
    int i;
    syntaxerror=0;
    parse_fd=fd;
    currentlevel=0;
    if(nofilter==0) {
        nofilter=my_malloc(sizeof(unsigned char)*rb->tagdbheader->filecount);
        rb->memset(nofilter,1,rb->tagdbheader->filecount);
    }
    for(i=0;i<20;i++)
        filter[i]=nofilter;
    database_init();
    parser_acceptIt();
    currentToken=&curtoken;
    PUTS("parse");
    ret=parseMExpr();
    if(syntaxerror) {
        PUTS("Syntaxerror");
        rb->splash(HZ*3,true,errormsg);
    }
    parser_accept(TOKEN_EOF);
    return ret;
}

void parser_acceptIt(void) {
    if(syntaxerror) return;
    rb->read(parse_fd,&curtoken,sizeof(struct token));
}

int parser_accept(unsigned char kind) {
    if(currentToken->kind!=kind) {
        syntaxerror=1;
        rb->snprintf(errormsg,250,"'%d' found where '%d' expected\n",currentToken->kind,kind);
        return 0;
    }
    else {
        parser_acceptIt();
        return 1;
    }
}

unsigned char *parseCompareNum() {
    struct token number1,number2;
    unsigned char *ret;
    int i,n1=-1,n2=-1;
    int op;
    if(syntaxerror) return 0;
    PUTS("parseCompareNum");
    if(currentToken->kind==TOKEN_NUM ||
        currentToken->kind==TOKEN_NUMIDENTIFIER) {
        rb->memcpy(&number1,currentToken,sizeof(struct token));
        parser_acceptIt();
    }
    else {
        syntaxerror=1;
        rb->snprintf(errormsg,250,"'%d' found where NUM/NUMID expected\n",currentToken->kind);
        return 0;
    }
    if(currentToken->kind>=TOKEN_GT && currentToken->kind <= TOKEN_NE) {
        op=currentToken->kind;
        parser_acceptIt();
    }
    else {
        syntaxerror=1;
        rb->snprintf(errormsg,250,"'%d' found where NUMOP expected\n",currentToken->kind);
        return 0;
    }
    if(currentToken->kind==TOKEN_NUM ||
         currentToken->kind==TOKEN_NUMIDENTIFIER) {
        rb->memcpy(&number2,currentToken,sizeof(struct token));
        parser_acceptIt();
    }
    else {
        syntaxerror=1;
        rb->snprintf(errormsg,250,"'%d' found where NUM/NUMID expected\n",currentToken->kind);
        return 0;
    }
    ret=my_malloc(sizeof(unsigned char)*rb->tagdbheader->filecount);
    if(number1.kind==TOKEN_NUM)
        n1=getvalue(&number1);
    if(number2.kind==TOKEN_NUM)
        n2=getvalue(&number2);
    for(i=0;i<rb->tagdbheader->filecount;i++) 
        if(filter[currentlevel][i]) {
            loadentry(i);
            if(number1.kind==TOKEN_NUMIDENTIFIER)
                n1=getvalue(&number1);
            if(number2.kind==TOKEN_NUMIDENTIFIER)
                n2=getvalue(&number2);
            switch(op) {
                case TOKEN_GT:
                    ret[i]=n1 > n2;
                    break;
                case TOKEN_GTE:
                    ret[i]=n1 >= n2;
                    break;
                case TOKEN_LT:
                    ret[i]=n1 < n2;
                    break;
                case TOKEN_LTE:
                    ret[i]=n1 <= n2;
                    break;
                case TOKEN_EQ:
                    ret[i]=n1 == n2;
                    break;
                case TOKEN_NE:
                    ret[i]=n1 != n2;
                    break;
            }
        }
    return ret;
}

unsigned char *parseCompareString() {
    struct token string1,string2;
    unsigned char *ret;
    char *s1=NULL,*s2=NULL;
    int i,i2;
    int op;
    if(syntaxerror) return 0;
    PUTS("parseCompareString");
    if(currentToken->kind==TOKEN_STRING ||
          currentToken->kind==TOKEN_STRINGIDENTIFIER) {
        rb->memcpy(&string1,currentToken,sizeof(struct token));
        parser_acceptIt();
    }
    else {
        syntaxerror=1;
        rb->snprintf(errormsg,250,"'%d' found where STRING/STRINGID expected\n",currentToken->kind);
        return 0;
    }
    op=currentToken->kind;
    if(op>=TOKEN_CONTAINS&&op<=TOKEN_ENDSWITH) {
        parser_acceptIt();
    } else {
        syntaxerror=1;
        rb->snprintf(errormsg,250,"'%d' found where STROP expected\n",op);
        return 0;
    }
    if(currentToken->kind==TOKEN_STRING ||
          currentToken->kind==TOKEN_STRINGIDENTIFIER) {
        rb->memcpy(&string2,currentToken,sizeof(struct token));
        parser_acceptIt();
    }
    else {
        syntaxerror=1;
        rb->snprintf(errormsg,250,"'%d' found where STRING/STRINGID expected\n",currentToken->kind);
        return 0;
    }
    ret=my_malloc(sizeof(unsigned char)*rb->tagdbheader->filecount);
    if(string1.kind==TOKEN_STRING)
        s1=getstring(&string1);
    if(string2.kind==TOKEN_STRING)
        s2=getstring(&string2);
    for(i=0;i<rb->tagdbheader->filecount;i++) 
        if(filter[currentlevel][i]) {
            loadentry(i);
            if(string1.kind==TOKEN_STRINGIDENTIFIER)
                s1=getstring(&string1);
            if(string2.kind==TOKEN_STRINGIDENTIFIER)
                s2=getstring(&string2);
            switch(op) {
                case TOKEN_CONTAINS:
                    ret[i]=rb->strcasestr(s1,s2)!=0;
                    break;
                case TOKEN_EQUALS:
                    ret[i]=rb->strcasecmp(s1,s2)==0;
                    break;
                case TOKEN_STARTSWITH:
                    ret[i]=rb->strncasecmp(s1,s2,rb->strlen(s2))==0;
                    break;
                case TOKEN_ENDSWITH:
                    i2=rb->strlen(s2);
                    ret[i]=rb->strncasecmp(s1+rb->strlen(s1)-i2,s2,i2)==0;
                    break;
            }
        }
    return ret;
}

unsigned char *parseExpr() {
    unsigned char *ret;
    int i;
    if(syntaxerror) return 0;
    PUTS("parseExpr");       
    switch(currentToken->kind) {
        case TOKEN_NOT:
            parser_accept(TOKEN_NOT);
            PUTS("parseNot");
            ret = parseExpr();
            if(ret==NULL) return 0;
            for(i=0;i<rb->tagdbheader->filecount;i++)
                if(filter[currentlevel][i])
                    ret[i]=!ret[i];
            break;
        case TOKEN_LPAREN:
            parser_accept(TOKEN_LPAREN);
            currentlevel++;
            ret = parseMExpr();
            currentlevel--;
            if(ret==NULL) return 0;
            parser_accept(TOKEN_RPAREN);
            break;
        case TOKEN_NUM:
        case TOKEN_NUMIDENTIFIER:
            ret = parseCompareNum();
            if(ret==NULL) return 0;
            break;
        case TOKEN_STRING:
        case TOKEN_STRINGIDENTIFIER:
            ret = parseCompareString();
            if(ret==NULL) return 0;
            break;
        default:
            // error, unexpected symbol
            syntaxerror=1;
            rb->snprintf(errormsg,250,"unexpected '%d' found at parseExpr\n",currentToken->kind);
            ret=0;
            break;
    }
    return ret;
}

unsigned char *parseMExpr() {
    unsigned char *ret,*ret2;
    int i;
    if(syntaxerror) return 0;
    PUTS("parseMExpr");       
    ret=parseLExpr();
    while(currentToken->kind==TOKEN_OR) {
        parser_accept(TOKEN_OR);
        PUTS("parseOr");
        ret2 = parseLExpr();
        if(ret2==NULL) return 0;
        for(i=0;i<rb->tagdbheader->filecount;i++)
            if(filter[currentlevel][i]) // this should always be true
                ret[i]=ret[i] || ret2[i];
            else
                rb->splash(HZ*2,true,"An or is having a filter, bad.");
    }
    return ret;
}

unsigned char *parseLExpr() {
    unsigned char *ret,*ret2;
    int i;
    if(syntaxerror) return 0;
    PUTS("parseLExpr");
    filter[currentlevel]=nofilter;
    ret=parseExpr();
    filter[currentlevel]=ret;
    while(currentToken->kind==TOKEN_AND) {
        parser_accept(TOKEN_AND);
        PUTS("parseAnd");
        ret2 = parseExpr();
        if(ret2==NULL) return 0;
        for(i=0;i<rb->tagdbheader->filecount;i++)
            ret[i]=ret[i] && ret2[i];
    }
    filter[currentlevel]=nofilter;
    return ret;
}