Configure needs AS to be set for the Makefiles.
[mplayer/glamo.git] / stream / realrtsp / asmrp.c
blobd4f7ce8267150f66528fd796ea3e03b334679bda
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.
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>
43 #include "mp_msg.h"
44 #include "asmrp.h"
47 #define LOG
50 #define ASMRP_SYM_NONE 0
51 #define ASMRP_SYM_EOF 1
53 #define ASMRP_SYM_NUM 2
54 #define ASMRP_SYM_ID 3
55 #define ASMRP_SYM_STRING 4
57 #define ASMRP_SYM_HASH 10
58 #define ASMRP_SYM_SEMICOLON 11
59 #define ASMRP_SYM_COMMA 12
60 #define ASMRP_SYM_EQUALS 13
61 #define ASMRP_SYM_AND 14
62 #define ASMRP_SYM_OR 15
63 #define ASMRP_SYM_LESS 16
64 #define ASMRP_SYM_LEQ 17
65 #define ASMRP_SYM_GEQ 18
66 #define ASMRP_SYM_GREATER 19
67 #define ASMRP_SYM_DOLLAR 20
68 #define ASMRP_SYM_LPAREN 21
69 #define ASMRP_SYM_RPAREN 22
71 #define ASMRP_MAX_ID 1024
73 #define ASMRP_MAX_SYMTAB 10
75 typedef struct {
76 char *id;
77 int v;
78 } asmrp_sym_t;
80 typedef struct {
82 /* public part */
84 int sym;
85 int num;
87 char str[ASMRP_MAX_ID];
89 /* private part */
91 char *buf;
92 int pos;
93 char ch;
95 asmrp_sym_t sym_tab[ASMRP_MAX_SYMTAB];
96 int sym_tab_num;
98 } asmrp_t;
100 static asmrp_t *asmrp_new (void) {
102 asmrp_t *p;
104 p = malloc (sizeof (asmrp_t));
106 p->sym_tab_num = 0;
107 p->sym = ASMRP_SYM_NONE;
109 return p;
112 static void asmrp_dispose (asmrp_t *p) {
114 int i;
116 for (i=0; i<p->sym_tab_num; i++)
117 free (p->sym_tab[i].id);
119 free (p);
122 static void asmrp_getch (asmrp_t *p) {
123 p->ch = p->buf[p->pos];
124 p->pos++;
126 #ifdef LOG
127 printf ("%c\n", p->ch);
128 #endif
132 static void asmrp_init (asmrp_t *p, const char *str) {
134 p->buf = strdup (str);
135 p->pos = 0;
137 asmrp_getch (p);
140 static void asmrp_number (asmrp_t *p) {
142 int num;
144 num = 0;
145 while ( (p->ch>='0') && (p->ch<='9') ) {
147 num = num*10 + (p->ch - '0');
149 asmrp_getch (p);
152 p->sym = ASMRP_SYM_NUM;
153 p->num = num;
156 static void asmrp_string (asmrp_t *p) {
158 int l;
160 l = 0;
162 while ( (p->ch!='"') && (p->ch>=32) ) {
164 if(l < ASMRP_MAX_ID - 1)
165 p->str[l++] = p->ch;
166 else
167 mp_msg(MSGT_STREAM, MSGL_ERR, "error: string too long, ignoring char %c.\n", p->ch);
169 asmrp_getch (p);
171 p->str[l]=0;
173 if (p->ch=='"')
174 asmrp_getch (p);
176 p->sym = ASMRP_SYM_STRING;
179 static void asmrp_identifier (asmrp_t *p) {
181 int l;
183 l = 0;
185 while ( ((p->ch>='A') && (p->ch<='z'))
186 || ((p->ch>='0') && (p->ch<='9'))) {
188 if(l < ASMRP_MAX_ID - 1)
189 p->str[l++] = p->ch;
190 else
191 mp_msg(MSGT_STREAM, MSGL_ERR, "error: identifier too long, ignoring char %c.\n", p->ch);
193 asmrp_getch (p);
195 p->str[l]=0;
197 p->sym = ASMRP_SYM_ID;
200 #ifdef LOG
201 static void asmrp_print_sym (asmrp_t *p) {
203 printf ("symbol: ");
205 switch (p->sym) {
207 case ASMRP_SYM_NONE:
208 printf ("NONE\n");
209 break;
211 case ASMRP_SYM_EOF:
212 printf ("EOF\n");
213 break;
215 case ASMRP_SYM_NUM:
216 printf ("NUM %d\n", p->num);
217 break;
219 case ASMRP_SYM_ID:
220 printf ("ID '%s'\n", p->str);
221 break;
223 case ASMRP_SYM_STRING:
224 printf ("STRING \"%s\"\n", p->str);
225 break;
227 case ASMRP_SYM_HASH:
228 printf ("#\n");
229 break;
231 case ASMRP_SYM_SEMICOLON:
232 printf (";\n");
233 break;
234 case ASMRP_SYM_COMMA:
235 printf (",\n");
236 break;
237 case ASMRP_SYM_EQUALS:
238 printf ("==\n");
239 break;
240 case ASMRP_SYM_AND:
241 printf ("&&\n");
242 break;
243 case ASMRP_SYM_OR:
244 printf ("||\n");
245 break;
246 case ASMRP_SYM_LESS:
247 printf ("<\n");
248 break;
249 case ASMRP_SYM_LEQ:
250 printf ("<=\n");
251 break;
252 case ASMRP_SYM_GEQ:
253 printf (">=\n");
254 break;
255 case ASMRP_SYM_GREATER:
256 printf (">\n");
257 break;
258 case ASMRP_SYM_DOLLAR:
259 printf ("$\n");
260 break;
261 case ASMRP_SYM_LPAREN:
262 printf ("(\n");
263 break;
264 case ASMRP_SYM_RPAREN:
265 printf (")\n");
266 break;
268 default:
269 printf ("unknown symbol %d\n", p->sym);
272 #endif
274 static void asmrp_get_sym (asmrp_t *p) {
276 while (p->ch <= 32) {
277 if (p->ch == 0) {
278 p->sym = ASMRP_SYM_EOF;
279 return;
282 asmrp_getch (p);
285 if (p->ch == '\\')
286 asmrp_getch (p);
288 switch (p->ch) {
290 case '#':
291 p->sym = ASMRP_SYM_HASH;
292 asmrp_getch (p);
293 break;
294 case ';':
295 p->sym = ASMRP_SYM_SEMICOLON;
296 asmrp_getch (p);
297 break;
298 case ',':
299 p->sym = ASMRP_SYM_COMMA;
300 asmrp_getch (p);
301 break;
302 case '=':
303 p->sym = ASMRP_SYM_EQUALS;
304 asmrp_getch (p);
305 if (p->ch=='=')
306 asmrp_getch (p);
307 break;
308 case '&':
309 p->sym = ASMRP_SYM_AND;
310 asmrp_getch (p);
311 if (p->ch=='&')
312 asmrp_getch (p);
313 break;
314 case '|':
315 p->sym = ASMRP_SYM_OR;
316 asmrp_getch (p);
317 if (p->ch=='|')
318 asmrp_getch (p);
319 break;
320 case '<':
321 p->sym = ASMRP_SYM_LESS;
322 asmrp_getch (p);
323 if (p->ch=='=') {
324 p->sym = ASMRP_SYM_LEQ;
325 asmrp_getch (p);
327 break;
328 case '>':
329 p->sym = ASMRP_SYM_GREATER;
330 asmrp_getch (p);
331 if (p->ch=='=') {
332 p->sym = ASMRP_SYM_GEQ;
333 asmrp_getch (p);
335 break;
336 case '$':
337 p->sym = ASMRP_SYM_DOLLAR;
338 asmrp_getch (p);
339 break;
340 case '(':
341 p->sym = ASMRP_SYM_LPAREN;
342 asmrp_getch (p);
343 break;
344 case ')':
345 p->sym = ASMRP_SYM_RPAREN;
346 asmrp_getch (p);
347 break;
349 case '"':
350 asmrp_getch (p);
351 asmrp_string (p);
352 break;
354 case '0': case '1': case '2': case '3': case '4':
355 case '5': case '6': case '7': case '8': case '9':
356 asmrp_number (p);
357 break;
359 default:
360 asmrp_identifier (p);
363 #ifdef LOG
364 asmrp_print_sym (p);
365 #endif
369 static int asmrp_find_id (asmrp_t *p, char *s) {
371 int i;
373 for (i=0; i<p->sym_tab_num; i++) {
374 if (!strcmp (s, p->sym_tab[i].id))
375 return i;
378 return -1;
381 static int asmrp_set_id (asmrp_t *p, char *s, int v) {
383 int i;
385 i = asmrp_find_id (p, s);
387 if (i<0) {
388 if (p->sym_tab_num == ASMRP_MAX_SYMTAB - 1) {
389 mp_msg(MSGT_STREAM, MSGL_ERR, "sym_tab overflow, ignoring identifier %s\n", s);
390 return 0;
392 i = p->sym_tab_num;
393 p->sym_tab_num++;
394 p->sym_tab[i].id = strdup (s);
396 #ifdef LOG
397 printf ("new symbol '%s'\n", s);
398 #endif
402 p->sym_tab[i].v = v;
404 #ifdef LOG
405 printf ("symbol '%s' assigned %d\n", s, v);
406 #endif
408 return i;
411 static int asmrp_condition (asmrp_t *p) ;
413 static int asmrp_operand (asmrp_t *p) {
415 int i, ret;
417 #ifdef LOG
418 printf ("operand\n");
419 #endif
421 ret = 0;
423 switch (p->sym) {
425 case ASMRP_SYM_DOLLAR:
427 asmrp_get_sym (p);
429 if (p->sym != ASMRP_SYM_ID) {
430 mp_msg(MSGT_STREAM, MSGL_ERR, "error: identifier expected.\n");
431 break;
434 i = asmrp_find_id (p, p->str);
435 if (i<0) {
436 mp_msg(MSGT_STREAM, MSGL_ERR, "error: unknown identifier %s\n", p->str);
437 } else
438 ret = p->sym_tab[i].v;
440 asmrp_get_sym (p);
441 break;
443 case ASMRP_SYM_NUM:
444 ret = p->num;
446 asmrp_get_sym (p);
447 break;
449 case ASMRP_SYM_LPAREN:
450 asmrp_get_sym (p);
452 ret = asmrp_condition (p);
454 if (p->sym != ASMRP_SYM_RPAREN) {
455 mp_msg(MSGT_STREAM, MSGL_ERR, "error: ) expected.\n");
456 break;
459 asmrp_get_sym (p);
460 break;
462 default:
463 mp_msg(MSGT_STREAM, MSGL_ERR, "syntax error, $ number or ( expected\n");
466 #ifdef LOG
467 printf ("operand done, =%d\n", ret);
468 #endif
470 return ret;
474 static int asmrp_comp_expression (asmrp_t *p) {
476 int a;
478 #ifdef LOG
479 printf ("comp_expression\n");
480 #endif
482 a = asmrp_operand (p);
484 while ( (p->sym == ASMRP_SYM_LESS)
485 || (p->sym == ASMRP_SYM_LEQ)
486 || (p->sym == ASMRP_SYM_EQUALS)
487 || (p->sym == ASMRP_SYM_GEQ)
488 || (p->sym == ASMRP_SYM_GREATER) ) {
489 int op = p->sym;
490 int b;
492 asmrp_get_sym (p);
494 b = asmrp_operand (p);
496 switch (op) {
497 case ASMRP_SYM_LESS:
498 a = a<b;
499 break;
500 case ASMRP_SYM_LEQ:
501 a = a<=b;
502 break;
503 case ASMRP_SYM_EQUALS:
504 a = a==b;
505 break;
506 case ASMRP_SYM_GEQ:
507 a = a>=b;
508 break;
509 case ASMRP_SYM_GREATER:
510 a = a>b;
511 break;
516 #ifdef LOG
517 printf ("comp_expression done = %d\n", a);
518 #endif
519 return a;
522 static int asmrp_condition (asmrp_t *p) {
524 int a;
526 #ifdef LOG
527 printf ("condition\n");
528 #endif
530 a = asmrp_comp_expression (p);
532 while ( (p->sym == ASMRP_SYM_AND) || (p->sym == ASMRP_SYM_OR) ) {
533 int op, b;
535 op = p->sym;
537 asmrp_get_sym (p);
539 b = asmrp_comp_expression (p);
541 switch (op) {
542 case ASMRP_SYM_AND:
543 a = a & b;
544 break;
545 case ASMRP_SYM_OR:
546 a = a | b;
547 break;
551 #ifdef LOG
552 printf ("condition done = %d\n", a);
553 #endif
554 return a;
557 static void asmrp_assignment (asmrp_t *p) {
559 #ifdef LOG
560 printf ("assignment\n");
561 #endif
563 if (p->sym == ASMRP_SYM_COMMA || p->sym == ASMRP_SYM_SEMICOLON) {
564 #ifdef LOG
565 printf ("empty assignment\n");
566 #endif
567 return;
570 if (p->sym != ASMRP_SYM_ID) {
571 mp_msg(MSGT_STREAM, MSGL_ERR, "error: identifier expected\n");
572 return;
574 asmrp_get_sym (p);
576 if (p->sym != ASMRP_SYM_EQUALS) {
577 mp_msg(MSGT_STREAM, MSGL_ERR, "error: = expected\n");
578 return;
580 asmrp_get_sym (p);
582 if ( (p->sym != ASMRP_SYM_NUM) && (p->sym != ASMRP_SYM_STRING)
583 && (p->sym != ASMRP_SYM_ID)) {
584 mp_msg(MSGT_STREAM, MSGL_ERR, "error: number or string expected\n");
585 return;
587 asmrp_get_sym (p);
589 #ifdef LOG
590 printf ("assignment done\n");
591 #endif
594 static int asmrp_rule (asmrp_t *p) {
596 int ret;
598 #ifdef LOG
599 printf ("rule\n");
600 #endif
602 ret = 1;
604 if (p->sym == ASMRP_SYM_HASH) {
606 asmrp_get_sym (p);
607 ret = asmrp_condition (p);
609 while (p->sym == ASMRP_SYM_COMMA) {
611 asmrp_get_sym (p);
613 asmrp_assignment (p);
616 } else if (p->sym != ASMRP_SYM_SEMICOLON) {
618 asmrp_assignment (p);
620 while (p->sym == ASMRP_SYM_COMMA) {
622 asmrp_get_sym (p);
623 asmrp_assignment (p);
627 #ifdef LOG
628 printf ("rule done = %d\n", ret);
629 #endif
631 if (p->sym != ASMRP_SYM_SEMICOLON) {
632 mp_msg(MSGT_STREAM, MSGL_ERR, "semicolon expected.\n");
633 return ret;
636 asmrp_get_sym (p);
638 return ret;
641 static int asmrp_eval (asmrp_t *p, int *matches) {
643 int rule_num, num_matches;
645 #ifdef LOG
646 printf ("eval\n");
647 #endif
649 asmrp_get_sym (p);
651 rule_num = 0; num_matches = 0;
652 while (p->sym != ASMRP_SYM_EOF) {
654 if (asmrp_rule (p)) {
655 #ifdef LOG
656 printf ("rule #%d is true\n", rule_num);
657 #endif
658 if(num_matches < MAX_RULEMATCHES - 1)
659 matches[num_matches++] = rule_num;
660 else
661 mp_msg(MSGT_STREAM, MSGL_ERR,
662 "Ignoring matched asm rule %d, too many matched rules.\n", rule_num);
665 rule_num++;
668 matches[num_matches] = -1;
669 return num_matches;
672 int asmrp_match (const char *rules, int bandwidth, int *matches) {
674 asmrp_t *p;
675 int num_matches;
677 p = asmrp_new ();
679 asmrp_init (p, rules);
681 asmrp_set_id (p, "Bandwidth", bandwidth);
682 asmrp_set_id (p, "OldPNMPlayer", 0);
684 num_matches = asmrp_eval (p, matches);
686 asmrp_dispose (p);
688 return num_matches;