Patch by Stefan Huehner / stefan % huehner ! org \
[mplayer/glamo.git] / libmpdemux / realrtsp / asmrp.c
blob05953daeb85b7b8b4000c43cd14cf7a1aef62b16
1 /*
2 * This file was ported to MPlayer from xine CVS asmrp.c,v 1.2 2002/12/17 16:49:48
3 */
5 /*
6 * Copyright (C) 2002 the xine project
8 * This file is part of xine, a free video player.
9 *
10 * xine is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * xine is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
25 * a parser for real's asm rules
27 * grammar for these rules:
30 rule_book = { rule }
31 rule = ( '#' condition { ',' assignment } | [ assignment {',' assignment} ]) ';'
32 assignment = id '=' const
33 const = ( number | string )
34 condition = comp_expr { ( '&&' | '||' ) comp_expr }
35 comp_expr = operand { ( '<' | '<=' | '==' | '>=' | '>' ) operand }
36 operand = ( '$' id | num | '(' condition ')' )
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
45 #define LOG
48 #define ASMRP_SYM_NONE 0
49 #define ASMRP_SYM_EOF 1
51 #define ASMRP_SYM_NUM 2
52 #define ASMRP_SYM_ID 3
53 #define ASMRP_SYM_STRING 4
55 #define ASMRP_SYM_HASH 10
56 #define ASMRP_SYM_SEMICOLON 11
57 #define ASMRP_SYM_COMMA 12
58 #define ASMRP_SYM_EQUALS 13
59 #define ASMRP_SYM_AND 14
60 #define ASMRP_SYM_OR 15
61 #define ASMRP_SYM_LESS 16
62 #define ASMRP_SYM_LEQ 17
63 #define ASMRP_SYM_GEQ 18
64 #define ASMRP_SYM_GREATER 19
65 #define ASMRP_SYM_DOLLAR 20
66 #define ASMRP_SYM_LPAREN 21
67 #define ASMRP_SYM_RPAREN 22
69 #define ASMRP_MAX_ID 1024
71 #define ASMRP_MAX_SYMTAB 10
73 typedef struct {
74 char *id;
75 int v;
76 } asmrp_sym_t;
78 typedef struct {
80 /* public part */
82 int sym;
83 int num;
85 char str[ASMRP_MAX_ID];
87 /* private part */
89 char *buf;
90 int pos;
91 char ch;
93 asmrp_sym_t sym_tab[ASMRP_MAX_SYMTAB];
94 int sym_tab_num;
96 } asmrp_t;
98 static asmrp_t *asmrp_new (void) {
100 asmrp_t *p;
102 p = malloc (sizeof (asmrp_t));
104 p->sym_tab_num = 0;
105 p->sym = ASMRP_SYM_NONE;
107 return p;
110 static void asmrp_dispose (asmrp_t *p) {
112 int i;
114 for (i=0; i<p->sym_tab_num; i++)
115 free (p->sym_tab[i].id);
117 free (p);
120 static void asmrp_getch (asmrp_t *p) {
121 p->ch = p->buf[p->pos];
122 p->pos++;
124 #ifdef LOG
125 printf ("%c\n", p->ch);
126 #endif
130 static void asmrp_init (asmrp_t *p, const char *str) {
132 p->buf = strdup (str);
133 p->pos = 0;
135 asmrp_getch (p);
138 static void asmrp_number (asmrp_t *p) {
140 int num;
142 num = 0;
143 while ( (p->ch>='0') && (p->ch<='9') ) {
145 num = num*10 + (p->ch - '0');
147 asmrp_getch (p);
150 p->sym = ASMRP_SYM_NUM;
151 p->num = num;
154 static void asmrp_string (asmrp_t *p) {
156 int l;
158 l = 0;
160 while ( (p->ch!='"') && (p->ch>=32) ) {
162 p->str[l] = p->ch;
164 l++;
165 asmrp_getch (p);
167 p->str[l]=0;
169 if (p->ch=='"')
170 asmrp_getch (p);
172 p->sym = ASMRP_SYM_STRING;
175 static void asmrp_identifier (asmrp_t *p) {
177 int l;
179 l = 0;
181 while ( ((p->ch>='A') && (p->ch<='z'))
182 || ((p->ch>='0') && (p->ch<='9'))) {
184 p->str[l] = p->ch;
186 l++;
187 asmrp_getch (p);
189 p->str[l]=0;
191 p->sym = ASMRP_SYM_ID;
194 #ifdef LOG
195 static void asmrp_print_sym (asmrp_t *p) {
197 printf ("symbol: ");
199 switch (p->sym) {
201 case ASMRP_SYM_NONE:
202 printf ("NONE\n");
203 break;
205 case ASMRP_SYM_EOF:
206 printf ("EOF\n");
207 break;
209 case ASMRP_SYM_NUM:
210 printf ("NUM %d\n", p->num);
211 break;
213 case ASMRP_SYM_ID:
214 printf ("ID '%s'\n", p->str);
215 break;
217 case ASMRP_SYM_STRING:
218 printf ("STRING \"%s\"\n", p->str);
219 break;
221 case ASMRP_SYM_HASH:
222 printf ("#\n");
223 break;
225 case ASMRP_SYM_SEMICOLON:
226 printf (";\n");
227 break;
228 case ASMRP_SYM_COMMA:
229 printf (",\n");
230 break;
231 case ASMRP_SYM_EQUALS:
232 printf ("==\n");
233 break;
234 case ASMRP_SYM_AND:
235 printf ("&&\n");
236 break;
237 case ASMRP_SYM_OR:
238 printf ("||\n");
239 break;
240 case ASMRP_SYM_LESS:
241 printf ("<\n");
242 break;
243 case ASMRP_SYM_LEQ:
244 printf ("<=\n");
245 break;
246 case ASMRP_SYM_GEQ:
247 printf (">=\n");
248 break;
249 case ASMRP_SYM_GREATER:
250 printf (">\n");
251 break;
252 case ASMRP_SYM_DOLLAR:
253 printf ("$\n");
254 break;
255 case ASMRP_SYM_LPAREN:
256 printf ("(\n");
257 break;
258 case ASMRP_SYM_RPAREN:
259 printf (")\n");
260 break;
262 default:
263 printf ("unknown symbol %d\n", p->sym);
266 #endif
268 static void asmrp_get_sym (asmrp_t *p) {
270 while (p->ch <= 32) {
271 if (p->ch == 0) {
272 p->sym = ASMRP_SYM_EOF;
273 return;
276 asmrp_getch (p);
279 if (p->ch == '\\')
280 asmrp_getch (p);
282 switch (p->ch) {
284 case '#':
285 p->sym = ASMRP_SYM_HASH;
286 asmrp_getch (p);
287 break;
288 case ';':
289 p->sym = ASMRP_SYM_SEMICOLON;
290 asmrp_getch (p);
291 break;
292 case ',':
293 p->sym = ASMRP_SYM_COMMA;
294 asmrp_getch (p);
295 break;
296 case '=':
297 p->sym = ASMRP_SYM_EQUALS;
298 asmrp_getch (p);
299 if (p->ch=='=')
300 asmrp_getch (p);
301 break;
302 case '&':
303 p->sym = ASMRP_SYM_AND;
304 asmrp_getch (p);
305 if (p->ch=='&')
306 asmrp_getch (p);
307 break;
308 case '|':
309 p->sym = ASMRP_SYM_OR;
310 asmrp_getch (p);
311 if (p->ch=='|')
312 asmrp_getch (p);
313 break;
314 case '<':
315 p->sym = ASMRP_SYM_LESS;
316 asmrp_getch (p);
317 if (p->ch=='=') {
318 p->sym = ASMRP_SYM_LEQ;
319 asmrp_getch (p);
321 break;
322 case '>':
323 p->sym = ASMRP_SYM_GREATER;
324 asmrp_getch (p);
325 if (p->ch=='=') {
326 p->sym = ASMRP_SYM_GEQ;
327 asmrp_getch (p);
329 break;
330 case '$':
331 p->sym = ASMRP_SYM_DOLLAR;
332 asmrp_getch (p);
333 break;
334 case '(':
335 p->sym = ASMRP_SYM_LPAREN;
336 asmrp_getch (p);
337 break;
338 case ')':
339 p->sym = ASMRP_SYM_RPAREN;
340 asmrp_getch (p);
341 break;
343 case '"':
344 asmrp_getch (p);
345 asmrp_string (p);
346 break;
348 case '0': case '1': case '2': case '3': case '4':
349 case '5': case '6': case '7': case '8': case '9':
350 asmrp_number (p);
351 break;
353 default:
354 asmrp_identifier (p);
357 #ifdef LOG
358 asmrp_print_sym (p);
359 #endif
363 static int asmrp_find_id (asmrp_t *p, char *s) {
365 int i;
367 for (i=0; i<p->sym_tab_num; i++) {
368 if (!strcmp (s, p->sym_tab[i].id))
369 return i;
372 return -1;
375 static int asmrp_set_id (asmrp_t *p, char *s, int v) {
377 int i;
379 i = asmrp_find_id (p, s);
381 if (i<0) {
382 i = p->sym_tab_num;
383 p->sym_tab_num++;
384 p->sym_tab[i].id = strdup (s);
386 #ifdef LOG
387 printf ("new symbol '%s'\n", s);
388 #endif
392 p->sym_tab[i].v = v;
394 #ifdef LOG
395 printf ("symbol '%s' assigned %d\n", s, v);
396 #endif
398 return i;
401 static int asmrp_condition (asmrp_t *p) ;
403 static int asmrp_operand (asmrp_t *p) {
405 int i, ret;
407 #ifdef LOG
408 printf ("operand\n");
409 #endif
411 ret = 0;
413 switch (p->sym) {
415 case ASMRP_SYM_DOLLAR:
417 asmrp_get_sym (p);
419 if (p->sym != ASMRP_SYM_ID) {
420 printf ("error: identifier expected.\n");
421 abort();
424 i = asmrp_find_id (p, p->str);
425 if (i<0) {
426 printf ("error: unknown identifier %s\n", p->str);
428 ret = p->sym_tab[i].v;
430 asmrp_get_sym (p);
431 break;
433 case ASMRP_SYM_NUM:
434 ret = p->num;
436 asmrp_get_sym (p);
437 break;
439 case ASMRP_SYM_LPAREN:
440 asmrp_get_sym (p);
442 ret = asmrp_condition (p);
444 if (p->sym != ASMRP_SYM_RPAREN) {
445 printf ("error: ) expected.\n");
446 abort();
449 asmrp_get_sym (p);
450 break;
452 default:
453 printf ("syntax error, $ number or ( expected\n");
454 abort();
457 #ifdef LOG
458 printf ("operand done, =%d\n", ret);
459 #endif
461 return ret;
464 static int asmrp_comp_expression (asmrp_t *p) {
466 int a;
468 #ifdef LOG
469 printf ("comp_expression\n");
470 #endif
472 a = asmrp_operand (p);
474 while ( (p->sym == ASMRP_SYM_LESS)
475 || (p->sym == ASMRP_SYM_LEQ)
476 || (p->sym == ASMRP_SYM_EQUALS)
477 || (p->sym == ASMRP_SYM_GEQ)
478 || (p->sym == ASMRP_SYM_GREATER) ) {
479 int op = p->sym;
480 int b;
482 asmrp_get_sym (p);
484 b = asmrp_operand (p);
486 switch (op) {
487 case ASMRP_SYM_LESS:
488 a = a<b;
489 break;
490 case ASMRP_SYM_LEQ:
491 a = a<=b;
492 break;
493 case ASMRP_SYM_EQUALS:
494 a = a==b;
495 break;
496 case ASMRP_SYM_GEQ:
497 a = a>=b;
498 break;
499 case ASMRP_SYM_GREATER:
500 a = a>b;
501 break;
506 #ifdef LOG
507 printf ("comp_expression done = %d\n", a);
508 #endif
509 return a;
512 static int asmrp_condition (asmrp_t *p) {
514 int a;
516 #ifdef LOG
517 printf ("condition\n");
518 #endif
520 a = asmrp_comp_expression (p);
522 while ( (p->sym == ASMRP_SYM_AND) || (p->sym == ASMRP_SYM_OR) ) {
523 int op, b;
525 op = p->sym;
527 asmrp_get_sym (p);
529 b = asmrp_comp_expression (p);
531 switch (op) {
532 case ASMRP_SYM_AND:
533 a = a & b;
534 break;
535 case ASMRP_SYM_OR:
536 a = a | b;
537 break;
541 #ifdef LOG
542 printf ("condition done = %d\n", a);
543 #endif
544 return a;
547 static void asmrp_assignment (asmrp_t *p) {
549 #ifdef LOG
550 printf ("assignment\n");
551 #endif
553 if (p->sym == ASMRP_SYM_COMMA || p->sym == ASMRP_SYM_SEMICOLON) {
554 #ifdef LOG
555 printf ("empty assignment\n");
556 #endif
557 return;
560 if (p->sym != ASMRP_SYM_ID) {
561 printf ("error: identifier expected\n");
562 abort ();
564 asmrp_get_sym (p);
566 if (p->sym != ASMRP_SYM_EQUALS) {
567 printf ("error: = expected\n");
568 abort ();
570 asmrp_get_sym (p);
572 if ( (p->sym != ASMRP_SYM_NUM) && (p->sym != ASMRP_SYM_STRING)
573 && (p->sym != ASMRP_SYM_ID)) {
574 printf ("error: number or string expected\n");
575 abort ();
577 asmrp_get_sym (p);
579 #ifdef LOG
580 printf ("assignment done\n");
581 #endif
584 static int asmrp_rule (asmrp_t *p) {
586 int ret;
588 #ifdef LOG
589 printf ("rule\n");
590 #endif
592 ret = 1;
594 if (p->sym == ASMRP_SYM_HASH) {
596 asmrp_get_sym (p);
597 ret = asmrp_condition (p);
599 while (p->sym == ASMRP_SYM_COMMA) {
601 asmrp_get_sym (p);
603 asmrp_assignment (p);
606 } else if (p->sym != ASMRP_SYM_SEMICOLON) {
608 asmrp_assignment (p);
610 while (p->sym == ASMRP_SYM_COMMA) {
612 asmrp_get_sym (p);
613 asmrp_assignment (p);
617 #ifdef LOG
618 printf ("rule done = %d\n", ret);
619 #endif
621 if (p->sym != ASMRP_SYM_SEMICOLON) {
622 printf ("semicolon expected.\n");
623 abort ();
626 asmrp_get_sym (p);
628 return ret;
631 static int asmrp_eval (asmrp_t *p, int *matches) {
633 int rule_num, num_matches;
635 #ifdef LOG
636 printf ("eval\n");
637 #endif
639 asmrp_get_sym (p);
641 rule_num = 0; num_matches = 0;
642 while (p->sym != ASMRP_SYM_EOF) {
644 if (asmrp_rule (p)) {
645 #ifdef LOG
646 printf ("rule #%d is true\n", rule_num);
647 #endif
648 matches[num_matches] = rule_num;
649 num_matches++;
652 rule_num++;
655 matches[num_matches] = -1;
656 return num_matches;
659 int asmrp_match (const char *rules, int bandwidth, int *matches) {
661 asmrp_t *p;
662 int num_matches;
664 p = asmrp_new ();
666 asmrp_init (p, rules);
668 asmrp_set_id (p, "Bandwidth", bandwidth);
669 asmrp_set_id (p, "OldPNMPlayer", 0);
671 num_matches = asmrp_eval (p, matches);
673 asmrp_dispose (p);
675 return num_matches;