1 /* -------------------------------------------------------------------- */
2 /* SMS Client, send messages to mobile phones and pagers		*/
3 /*									*/
4 /* gs_parser.c								*/
5 /*									*/
6 /*  Copyright (C) 1997,1998 Angelo Masci				*/
7 /*									*/
8 /*  This library is free software; you can redistribute it and/or	*/
9 /*  modify it under the terms of the GNU Library General Public		*/
10 /*  License as published by the Free Software Foundation; either	*/
11 /*  version 2 of the License, or (at your option) any later version.	*/
12 /*									*/
13 /*  This library is distributed in the hope that it will be useful,	*/
14 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of	*/
15 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU	*/
16 /*  Library General Public License for more details.			*/
17 /*									*/
18 /*  You should have received a copy of the GNU Library General Public	*/
19 /*  License along with this library; if not, write to the Free		*/
20 /*  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.	*/
21 /*									*/
22 /*  You can contact the author at this e-mail address:			*/
23 /*									*/
24 /*  angelo@styx.demon.co.uk						*/
25 /*									*/
26 /* -------------------------------------------------------------------- */
27 /* $Id$
28    -------------------------------------------------------------------- */
29 
30 #include <stdio.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <stdlib.h>
34 
35 #include "common/common.h"
36 #include "logfile/logfile.h"
37 #include "gs_token.h"
38 #include "gs_parser.h"
39 
40 #include "gs_private.h"
41 
42 
43 /* -------------------------------------------------------------------- */
44 
45 static int parse_list(int fd, TOKEN_HEAP *heap);
46 static int parse_list_items(int fd, TOKEN_HEAP *heap, int depth);
47 static int parse_dictionary_items(int fd, TOKEN_HEAP *heap);
48 
49 /* -------------------------------------------------------------------- */
50 /* -------------------------------------------------------------------- */
parse_dictionary(int fd,TOKEN_HEAP * heap)51 int parse_dictionary(int fd, TOKEN_HEAP *heap)
52 {
53 	TOKEN_ID
54 		token_id;
55 
56 
57 	lprintf(LOG_VERYVERBOSE, "Entering parse_dictionary()\n");
58 
59 	token_id = get_next_token(fd, heap);
60 	if (token_id == lcurly_tok)
61 	{
62 		if (parse_dictionary_items(fd, heap))
63 		{
64 			token_id = get_next_token(fd, heap);
65 			if (token_id == rcurly_tok)
66 			{	return TRUE;
67 			}
68 			else
69 			{	lprintf(LOG_ERROR, "Line %d Expecting '}'\n", gs_get_linenumber());
70 				return FALSE;
71 			}
72 		}
73 		else
74 		{	lprintf(LOG_ERROR, "Line %d Expecting Dictionary items\n", gs_get_linenumber());
75 			return FALSE;
76 		}
77 	}
78 	else
79 	{
80 		lprintf(LOG_ERROR, "Line %d Expecting '{'\n", gs_get_linenumber());
81 		return FALSE;
82 	}
83 
84 	return TRUE;
85 }
86 
87 
88 /* -------------------------------------------------------------------- */
89 /* -------------------------------------------------------------------- */
parse_list(int fd,TOKEN_HEAP * heap)90 static int parse_list(int fd, TOKEN_HEAP *heap)
91 {
92 	TOKEN_ID
93 		token_id;
94 
95 
96 	lprintf(LOG_VERYVERBOSE, "Entering parse_list()\n");
97 
98 
99 	token_id = get_next_token(fd, heap);
100 	if (token_id == lparen_tok)
101 	{
102 		if (parse_list_items(fd, heap, 0))
103 		{
104 			token_id = get_next_token(fd, heap);
105 			if (token_id == rparen_tok)
106 			{	return TRUE;
107 			}
108 			else
109 			{	lprintf(LOG_ERROR, "Line %d Expecting ')'\n", gs_get_linenumber());
110 				return FALSE;
111 			}
112 		}
113 		else
114 		{	lprintf(LOG_ERROR, "Line %d Expecting List items\n", gs_get_linenumber());
115 			return FALSE;
116 		}
117 	}
118 	else
119 	{
120 		lprintf(LOG_ERROR, "Line %d Expecting '('\n", gs_get_linenumber());
121 		return FALSE;
122 	}
123 
124 	return TRUE;
125 }
126 
127 
128 /* -------------------------------------------------------------------- */
129 /* -------------------------------------------------------------------- */
parse_list_items(int fd,TOKEN_HEAP * heap,int depth)130 static int parse_list_items(int fd, TOKEN_HEAP *heap, int depth)
131 {
132 	TOKEN_ID
133 		token_id,
134 		identifier;
135 
136 
137 	TOKEN_HEAP
138 		*dictionary,
139 		*list;
140 
141 	long	value;
142 
143 	char	buf[64],
144 		*string;
145 
146 
147 
148 	lprintf(LOG_VERYVERBOSE, "Entering parse_list_items()\n");
149 
150 	sms_snprintf(buf, 64, "[%d]", depth);
151 	identifier = add_token(heap, buf, T_IDENTIFIER, TP_NULL, NULL);
152 	if (identifier == NULL)
153 	{	return FALSE;
154 	}
155 
156 
157 	depth++;
158 
159 	token_id = get_next_token(fd, heap);
160 	if (token_id == lcurly_tok)
161 	{
162 		dictionary = generate_new_heap();
163 		if (dictionary == NULL)
164 		{	lprintf(LOG_ERROR, "Allocating memory\n");
165 			return FALSE;
166 		}
167 
168 		token_assign_dictionary(heap, identifier, dictionary);
169 
170 		push_back_token(token_id);
171 		if (parse_dictionary(fd, dictionary))
172 		{
173 			token_id = get_next_token(fd, heap);
174 			if (token_id == comma_tok)
175 			{
176 				return parse_list_items(fd, heap, depth);
177 			}
178 			else
179 			{	push_back_token(token_id);
180 				return TRUE;
181 			}
182 		}
183 		else
184 		{	lprintf(LOG_ERROR, "Failed parsing Dictionary\n");
185 			return FALSE;
186 		}
187 	}
188 	else
189 	if (token_id == lparen_tok)
190 	{
191 		list = generate_new_heap();
192 		if (list == NULL)
193 		{	lprintf(LOG_ERROR, "Allocating memory\n");
194 			return FALSE;
195 		}
196 
197 		token_assign_list(heap, identifier, list);
198 
199 		push_back_token(token_id);
200 		if(parse_list(fd, list))
201 		{
202 			token_id = get_next_token(fd, heap);
203 			if (token_id == comma_tok)
204 			{
205 				return parse_list_items(fd, heap, depth);
206 			}
207 			else
208 			{	push_back_token(token_id);
209 				return TRUE;
210 			}
211 		}
212 		else
213 		{	lprintf(LOG_ERROR, "Failed parsing List\n");
214 			return FALSE;
215 		}
216 	}
217 	else
218 	if (get_token_type(heap, token_id) == T_STRING)
219 	{
220 		string = get_token_strvalue(heap, token_id);
221 		token_assign_strvalue(heap, identifier, string);
222 
223 		token_id = get_next_token(fd, heap);
224 		if (token_id == comma_tok)
225 		{
226 			return parse_list_items(fd, heap, depth);
227 		}
228 		else
229 		{	push_back_token(token_id);
230 			return TRUE;
231 		}
232 	}
233 	else
234 	if (get_token_type(heap, token_id) == T_NUMERIC)
235 	{
236 		string = get_token_strvalue(heap, token_id);
237 		value  = get_token_numvalue(heap, token_id);
238 
239 		token_assign_strvalue(heap, identifier, string);
240 		token_assign_numvalue(heap, identifier, value);
241 
242 		token_id = get_next_token(fd, heap);
243 		if (token_id == comma_tok)
244 		{
245 			return parse_list_items(fd, heap, depth);
246 		}
247 		else
248 		{	push_back_token(token_id);
249 			return TRUE;
250 		}
251 	}
252 	else
253 	{
254 		lprintf(LOG_ERROR, "Line %d Expecting Identifier, Dictionary or List\n", gs_get_linenumber());
255 		return FALSE;
256 	}
257 
258 	return TRUE;
259 }
260 
261 
262 /* -------------------------------------------------------------------- */
263 /* -------------------------------------------------------------------- */
parse_dictionary_items(int fd,TOKEN_HEAP * heap)264 static int parse_dictionary_items(int fd, TOKEN_HEAP *heap)
265 {
266 	TOKEN_ID
267 		token_id,
268 		identifier;
269 
270 	TOKEN_HEAP
271 		*dictionary,
272 		*list;
273 
274 	char	*string;
275 	long	value;
276 
277 
278 	lprintf(LOG_VERYVERBOSE, "Entering parse_dictionary_items()\n");
279 
280 	token_id = get_next_token(fd, heap);
281 	if (get_token_type(heap, token_id) == T_IDENTIFIER)
282 	{
283 		identifier = token_id;
284 
285 		token_id = get_next_token(fd, heap);
286 		if (token_id == assignment_tok)
287 		{
288 			token_id = get_next_token(fd, heap);
289 			if (token_id == lcurly_tok)
290 			{
291 				dictionary = generate_new_heap();
292 				if (dictionary == NULL)
293 				{	lprintf(LOG_ERROR, "Allocating memory\n");
294 					return FALSE;
295 				}
296 
297 				token_assign_dictionary(heap, identifier, dictionary);
298 
299 				push_back_token(token_id);
300 				if (parse_dictionary(fd, dictionary))
301 				{
302 					token_id = get_next_token(fd, heap);
303 					if (token_id != rcurly_tok)
304 					{
305 						push_back_token(token_id);
306 						return parse_dictionary_items(fd, heap);
307 					}
308 					else
309 					{	push_back_token(token_id);
310 						return TRUE;
311 					}
312 				}
313 				else
314 				{	lprintf(LOG_ERROR, "Failed parsing Dictionary\n");
315 					return FALSE;
316 				}
317 
318 			}
319 			else
320 			if (token_id == lparen_tok)
321 			{
322 				list = generate_new_heap();
323 				if (list == NULL)
324 				{	lprintf(LOG_ERROR, "Allocating memory\n");
325 					return FALSE;
326 				}
327 
328 				token_assign_list(heap, identifier, list);
329 
330 				push_back_token(token_id);
331 				if(parse_list(fd, list))
332 				{
333 					token_id = get_next_token(fd, heap);
334 					if (token_id != rcurly_tok)
335 					{
336 						push_back_token(token_id);
337 						return parse_dictionary_items(fd, heap);
338 					}
339 					else
340 					{	push_back_token(token_id);
341 						return TRUE;
342 					}
343 				}
344 				else
345 				{	lprintf(LOG_ERROR, "Failed parsing List\n");
346 					return FALSE;
347 				}
348 			}
349 			else
350 			if (get_token_type(heap, token_id) == T_STRING)
351 			{
352 				string = get_token_strvalue(heap, token_id);
353 				token_assign_strvalue(heap, identifier, string);
354 
355 				token_id = get_next_token(fd, heap);
356 				if (token_id != rcurly_tok)
357 				{
358 					push_back_token(token_id);
359 					return parse_dictionary_items(fd, heap);
360 				}
361 				else
362 				{	push_back_token(token_id);
363 					return TRUE;
364 				}
365 			}
366 			else
367 			if (get_token_type(heap, token_id) == T_NUMERIC)
368 			{
369 				string = get_token_strvalue(heap, token_id);
370 				value  = get_token_numvalue(heap, token_id);
371 
372 				token_assign_strvalue(heap, identifier, string);
373 				token_assign_numvalue(heap, identifier, value);
374 
375 				token_id = get_next_token(fd, heap);
376 				if (token_id != rcurly_tok)
377 				{
378 					push_back_token(token_id);
379 					return parse_dictionary_items(fd, heap);
380 				}
381 				else
382 				{	push_back_token(token_id);
383 					return TRUE;
384 				}
385 			}
386 			else
387 			{
388 				lprintf(LOG_ERROR, "Line %d Expecting Identifier, Dictionary or List\n", gs_get_linenumber());
389 				return FALSE;
390 			}
391 		}
392 		else
393 		{
394 			lprintf(LOG_ERROR, "Line %d Expecting '='\n", gs_get_linenumber());
395 			return FALSE;
396 		}
397 	}
398 	else
399 	{
400 		lprintf(LOG_ERROR, "Line %d Expecting Identifier\n", gs_get_linenumber());
401 		return FALSE;
402 	}
403 
404 	return TRUE;
405 }
406