summaryrefslogtreecommitdiff
path: root/apps/plugins/searchengine/parser.c
blob: 501450c56218b0c149fdb736843840baa0f84e08 (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
/***************************************************************************
 *             __________               __   ___.
 *   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 *tokenbuffer,*currentToken;
int currentindex;
int syntaxerror;
char errormsg[250];

unsigned char *parse(struct token *tokenbuf) {
	unsigned char *ret=0;
	currentindex=0;
	syntaxerror=0;
	tokenbuffer=tokenbuf;
	database_init();
	currentToken=&tokenbuffer[currentindex];
	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;
	currentToken=&tokenbuffer[++currentindex];
}

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) {
	   number1=currentToken;	
	   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) {
           number2=currentToken;
           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++) {
		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,contains;
        if(syntaxerror) return 0;
        PUTS("parseCompareString");
        if(currentToken->kind==TOKEN_STRING ||
                currentToken->kind==TOKEN_STRINGIDENTIFIER) {
           string1=currentToken;
           parser_acceptIt();
        }
        else {
	   syntaxerror=1;
	   rb->snprintf(errormsg,250,"'%d' found where STRING/STRINGID expected\n",currentToken->kind);
	   return 0;
        }
	
	contains=currentToken->kind==TOKEN_CONTAINS;
        if(currentToken->kind==TOKEN_CONTAINS ||
               currentToken->kind==TOKEN_EQUALS)
	  parser_acceptIt();
	else {
           syntaxerror=1;
	   rb->snprintf(errormsg,250,"'%d' found where CONTAINS/EQUALS expected\n",currentToken->kind);
           return 0;
	}
	
        if(currentToken->kind==TOKEN_STRING ||
                currentToken->kind==TOKEN_STRINGIDENTIFIER) {
           string2=currentToken;
           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++) {
                loadentry(i);
		if(string1->kind==TOKEN_STRINGIDENTIFIER)
                  s1=getstring(string1);
		if(string2->kind==TOKEN_STRINGIDENTIFIER)
                  s2=getstring(string2);
		if(contains)
	          ret[i]=rb->strcasestr(s1,s2)!=0;
		else
		  ret[i]=rb->strcasecmp(s1,s2)==0;
        }
        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++)
		          ret[i]=!ret[i];
			break;
	    case TOKEN_LPAREN:
			parser_accept(TOKEN_LPAREN);
			ret = parseMExpr();
			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=parseExpr();
	while(currentToken->kind==TOKEN_AND||currentToken->kind==TOKEN_OR) {
		switch(currentToken->kind) {
			case 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];
				break;
			case TOKEN_OR:
				parser_accept(TOKEN_OR);
				PUTS("parseOr");
				ret2 = parseExpr();
				if(ret2==NULL) return 0;
				for(i=0;i<rb->tagdbheader->filecount;i++)
				    ret[i]=ret[i] || ret2[i];
				break;
		}
	}
	return ret;
}