Fix compilation by adding forgotten comma.
[mplayer/glamo.git] / asxparser.c
blob4a1f922d1483b106104528b3950c450c6d0c4390
1 #include "config.h"
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <stdarg.h>
6 #include <string.h>
7 #include <unistd.h>
9 #include "playtree.h"
10 #include "playtreeparser.h"
11 #include "stream/stream.h"
12 #include "libmpdemux/demuxer.h"
13 #include "asxparser.h"
14 #include "mp_msg.h"
15 #include "m_config.h"
17 extern m_config_t* mconfig;
19 ////// List utils
21 void
22 asx_list_add(void* list_ptr,void* entry){
23 void** list = *(void***)list_ptr;
24 int c = 0;
26 if(list != NULL)
27 for( ; list[c] != NULL; c++) ;
29 list = (void*)realloc(list,sizeof(void*)*(c+2));
31 list[c] = entry;
32 list[c+1] = NULL;
34 *(void***)list_ptr = list;
38 void
39 asx_list_remove(void* list_ptr,void* entry,ASX_FreeFunc free_func) {
40 void** list = *(void***)list_ptr;
41 int c,e = -1;
43 if(list == NULL) return;
45 for(c = 0 ; list[c] != NULL; c++){
46 if(list[c] == entry) e = c;
49 if(e == -1) return; // Not found
51 if(free_func != NULL) free_func(list[e]);
53 if(c == 1) { // Only one entry, we drop all
54 free(list);
55 *(void**)list_ptr = NULL;
56 return;
59 if(c > e) // If c==e the memmove is not needed
60 memmove(list+e,list+e+1,(c-e)*sizeof(void*));
62 list = (void*)realloc(list,(c-1)*sizeof(void*));
63 list[c-1] = NULL;
65 *(void***)list_ptr = list;
68 void
69 asx_list_free(void* list_ptr,ASX_FreeFunc free_func) {
70 void** ptr = *(void***)list_ptr;
71 if(ptr == NULL) return;
72 if(free_func != NULL) {
73 for( ; *ptr != NULL ; ptr++)
74 free_func(*ptr);
76 free(*(void**)list_ptr);
77 *(void**)list_ptr = NULL;
80 /////// Attribs utils
82 char*
83 asx_get_attrib(const char* attrib,char** attribs) {
84 char** ptr;
86 if(attrib == NULL || attribs == NULL) return NULL;
87 for(ptr = attribs; ptr[0] != NULL; ptr += 2){
88 if(strcasecmp(ptr[0],attrib) == 0)
89 return strdup(ptr[1]);
91 return NULL;
94 int
95 asx_attrib_to_enum(const char* val,char** valid_vals) {
96 char** ptr;
97 int r = 0;
99 if(valid_vals == NULL || val == NULL) return -2;
100 for(ptr = valid_vals ; ptr[0] != NULL ; ptr++) {
101 if(strcasecmp(val,ptr[0]) == 0) return r;
102 r++;
105 return -1;
108 #define asx_warning_attrib_required(p,e,a) mp_msg(MSGT_PLAYTREE,MSGL_WARN,"At line %d : element %s don't have the required attribute %s",p->line,e,a)
109 #define asx_warning_body_parse_error(p,e) mp_msg(MSGT_PLAYTREE,MSGL_WARN,"At line %d : error while parsing %s body",p->line,e)
111 ASX_Parser_t*
112 asx_parser_new(void) {
113 ASX_Parser_t* parser = calloc(1,sizeof(ASX_Parser_t));
114 return parser;
117 void
118 asx_parser_free(ASX_Parser_t* parser) {
119 if(!parser) return;
120 if(parser->ret_stack) free(parser->ret_stack);
121 free(parser);
125 #define LETTER "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
126 #define SPACE " \n\t\r"
129 asx_parse_attribs(ASX_Parser_t* parser,char* buffer,char*** _attribs) {
130 char *ptr1, *ptr2, *ptr3;
131 int n_attrib = 0;
132 char **attribs = NULL;
133 char *attrib, *val;
135 ptr1 = buffer;
136 while(1) {
137 for( ; strchr(SPACE,*ptr1) != NULL; ptr1++) { // Skip space
138 if(*ptr1 == '\0') break;
140 ptr3 = strchr(ptr1,'=');
141 if(ptr3 == NULL) break;
142 for(ptr2 = ptr3-1; strchr(SPACE,*ptr2) != NULL; ptr2--) {
143 if (ptr2 == ptr1) {
144 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : this should never append, back to attribute begin while skipping end space",parser->line);
145 break;
148 attrib = malloc(ptr2-ptr1+2);
149 strncpy(attrib,ptr1,ptr2-ptr1+1);
150 attrib[ptr2-ptr1+1] = '\0';
152 ptr1 = strchr(ptr3,'"');
153 if(ptr1 == NULL || ptr1[1] == '\0') ptr1 = strchr(ptr3,'\'');
154 if(ptr1 == NULL || ptr1[1] == '\0') {
155 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"At line %d : can't find attribute %s value",parser->line,attrib);
156 free(attrib);
157 break;
159 ptr2 = strchr(ptr1+1,ptr1[0]);
160 if (ptr2 == NULL) {
161 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"At line %d : value of attribute %s isn't finished",parser->line,attrib);
162 free(attrib);
163 break;
165 ptr1++;
166 val = malloc(ptr2-ptr1+1);
167 strncpy(val,ptr1,ptr2-ptr1);
168 val[ptr2-ptr1] = '\0';
169 n_attrib++;
171 attribs = (char**)realloc(attribs,(2*n_attrib+1)*sizeof(char*));
172 attribs[n_attrib*2-2] = attrib;
173 attribs[n_attrib*2-1] = val;
175 ptr1 = ptr2+1;
178 if(n_attrib > 0)
179 attribs[n_attrib*2] = NULL;
181 *_attribs = attribs;
183 return n_attrib;
187 * Return -1 on error, 0 when nothing is found, 1 on sucess
190 asx_get_element(ASX_Parser_t* parser,char** _buffer,
191 char** _element,char** _body,char*** _attribs) {
192 char *ptr1,*ptr2, *ptr3, *ptr4;
193 char *attribs = NULL;
194 char *element = NULL, *body = NULL, *ret = NULL, *buffer;
195 int n_attrib = 0;
196 int body_line = 0,attrib_line,ret_line,in = 0;
197 int quotes = 0;
199 if(_buffer == NULL || _element == NULL || _body == NULL || _attribs == NULL) {
200 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : asx_get_element called with invalid value",parser->line);
201 return -1;
204 *_body = *_element = NULL;
205 *_attribs = NULL;
206 buffer = *_buffer;
208 if(buffer == NULL) return 0;
210 if(parser->ret_stack && /*parser->last_body && */buffer != parser->last_body) {
211 ASX_LineSave_t* ls = parser->ret_stack;
212 int i;
213 for(i = 0 ; i < parser->ret_stack_size ; i++) {
214 if(buffer == ls[i].buffer) {
215 parser->line = ls[i].line;
216 break;
220 if( i < parser->ret_stack_size) {
221 i++;
222 if( i < parser->ret_stack_size)
223 memmove(parser->ret_stack,parser->ret_stack+i, (parser->ret_stack_size - i)*sizeof(ASX_LineSave_t));
224 parser->ret_stack_size -= i;
225 if(parser->ret_stack_size > 0)
226 parser->ret_stack = (ASX_LineSave_t*)realloc(parser->ret_stack,parser->ret_stack_size*sizeof(ASX_LineSave_t));
227 else {
228 free(parser->ret_stack);
229 parser->ret_stack = NULL;
234 ptr1 = buffer;
235 while(1) {
236 for( ; ptr1[0] != '<' ; ptr1++) {
237 if(ptr1[0] == '\0') {
238 ptr1 = NULL;
239 break;
241 if(ptr1[0] == '\n') parser->line++;
243 //ptr1 = strchr(ptr1,'<');
244 if(!ptr1 || ptr1[1] == '\0') return 0; // Nothing found
246 if(strncmp(ptr1,"<!--",4) == 0) { // Comments
247 for( ; strncmp(ptr1,"-->",3) != 0 ; ptr1++) {
248 if(ptr1[0] == '\0') {
249 ptr1 = NULL;
250 break;
252 if(ptr1[0] == '\n') parser->line++;
254 //ptr1 = strstr(ptr1,"-->");
255 if(!ptr1) {
256 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : unfinished comment",parser->line);
257 return -1;
259 } else {
260 break;
264 // Is this space skip very useful ??
265 for(ptr1++; strchr(SPACE,ptr1[0]) != NULL; ptr1++) { // Skip space
266 if(ptr1[0] == '\0') {
267 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing element start",parser->line);
268 return -1;
270 if(ptr1[0] == '\n') parser->line++;
273 for(ptr2 = ptr1; strchr(LETTER,*ptr2) != NULL;ptr2++) { // Go to end of name
274 if(*ptr2 == '\0'){
275 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing element start",parser->line);
276 return -1;
278 if(ptr2[0] == '\n') parser->line++;
281 element = malloc(ptr2-ptr1+1);
282 strncpy(element,ptr1,ptr2-ptr1);
283 element[ptr2-ptr1] = '\0';
285 for( ; strchr(SPACE,*ptr2) != NULL; ptr2++) { // Skip space
286 if(ptr2[0] == '\0') {
287 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing element start",parser->line);
288 free(element);
289 return -1;
291 if(ptr2[0] == '\n') parser->line++;
293 attrib_line = parser->line;
297 for(ptr3 = ptr2; ptr3[0] != '\0'; ptr3++) { // Go to element end
298 if(ptr3[0] == '"') quotes ^= 1;
299 if(!quotes && (ptr3[0] == '>' || strncmp(ptr3,"/>",2) == 0))
300 break;
301 if(ptr3[0] == '\n') parser->line++;
303 if(ptr3[0] == '\0' || ptr3[1] == '\0') { // End of file
304 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing element start",parser->line);
305 free(element);
306 return -1;
309 // Save attribs string
310 if(ptr3-ptr2 > 0) {
311 attribs = malloc(ptr3-ptr2+1);
312 strncpy(attribs,ptr2,ptr3-ptr2);
313 attribs[ptr3-ptr2] = '\0';
315 //bs_line = parser->line;
316 if(ptr3[0] != '/') { // Not Self closed element
317 ptr3++;
318 for( ; strchr(SPACE,*ptr3) != NULL; ptr3++) { // Skip space on body begin
319 if(*ptr3 == '\0') {
320 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing %s element body",parser->line,element);
321 free(element);
322 if(attribs) free(attribs);
323 return -1;
325 if(ptr3[0] == '\n') parser->line++;
327 ptr4 = ptr3;
328 body_line = parser->line;
329 while(1) { // Find closing element
330 for( ; ptr4[0] != '<' ; ptr4++) {
331 if(ptr4[0] == '\0') {
332 ptr4 = NULL;
333 break;
335 if(ptr4[0] == '\n') parser->line++;
337 if(ptr4 && strncmp(ptr4,"<!--",4) == 0) { // Comments
338 for( ; strncmp(ptr4,"-->",3) != 0 ; ptr4++) {
339 if(ptr4[0] == '\0') {
340 ptr4 = NULL;
341 break;
343 if(ptr1[0] == '\n') parser->line++;
345 continue;
347 if(ptr4 == NULL || ptr4[1] == '\0') {
348 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing %s element body",parser->line,element);
349 free(element);
350 if(attribs) free(attribs);
351 return -1;
353 if(ptr4[1] != '/' && strncasecmp(element,ptr4+1,strlen(element)) == 0) {
354 in++;
355 ptr4+=2;
356 continue;
357 } else if(strncasecmp(element,ptr4+2,strlen(element)) == 0) { // Extract body
358 if(in > 0) {
359 in--;
360 ptr4 += 2+strlen(element);
361 continue;
363 ret = ptr4+strlen(element)+3;
364 if(ptr4 != ptr3) {
365 ptr4--;
366 for( ; ptr4 != ptr3 && strchr(SPACE,*ptr4) != NULL; ptr4--) ;// Skip space on body end
367 // if(ptr4[0] == '\0') parser->line--;
369 ptr4++;
370 body = malloc(ptr4-ptr3+1);
371 strncpy(body,ptr3,ptr4-ptr3);
372 body[ptr4-ptr3] = '\0';
374 break;
375 } else {
376 ptr4 += 2;
379 } else {
380 ret = ptr3 + 2; // 2 is for />
383 for( ; ret[0] != '\0' && strchr(SPACE,ret[0]) != NULL; ret++) { // Skip space
384 if(ret[0] == '\n') parser->line++;
387 ret_line = parser->line;
389 if(attribs) {
390 parser->line = attrib_line;
391 n_attrib = asx_parse_attribs(parser,attribs,_attribs);
392 free(attribs);
393 if(n_attrib < 0) {
394 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"At line %d : error while parsing element %s attributes",parser->line,element);
395 free(element);
396 free(body);
397 return -1;
399 } else
400 *_attribs = NULL;
402 *_element = element;
403 *_body = body;
405 parser->last_body = body;
406 parser->ret_stack_size++;
407 parser->ret_stack = (ASX_LineSave_t*)realloc(parser->ret_stack,parser->ret_stack_size*sizeof(ASX_LineSave_t));
408 if(parser->ret_stack_size > 1)
409 memmove(parser->ret_stack+1,parser->ret_stack,(parser->ret_stack_size-1)*sizeof(ASX_LineSave_t));
410 parser->ret_stack[0].buffer = ret;
411 parser->ret_stack[0].line = ret_line;
412 parser->line = body ? body_line : ret_line;
414 *_buffer = ret;
415 return 1;
419 static void
420 asx_parse_param(ASX_Parser_t* parser, char** attribs, play_tree_t* pt) {
421 char *name,*val;
423 name = asx_get_attrib("NAME",attribs);
424 if(!name) {
425 asx_warning_attrib_required(parser,"PARAM" ,"NAME" );
426 return;
428 val = asx_get_attrib("VALUE",attribs);
429 if(m_config_get_option(mconfig,name) == NULL) {
430 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Found unknown param in asx: %s",name);
431 if(val)
432 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"=%s\n",val);
433 else
434 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"\n");
435 return;
437 play_tree_set_param(pt,name,val);
438 free(name);
439 if(val) free(val);
442 static void
443 asx_parse_ref(ASX_Parser_t* parser, char** attribs, play_tree_t* pt) {
444 char *href;
446 href = asx_get_attrib("HREF",attribs);
447 if(href == NULL) {
448 asx_warning_attrib_required(parser,"REF" ,"HREF" );
449 return;
452 play_tree_add_file(pt,href);
454 mp_msg(MSGT_PLAYTREE,MSGL_V,"Adding file %s to element entry\n",href);
456 free(href);
460 static play_tree_t*
461 asx_parse_entryref(ASX_Parser_t* parser,char* buffer,char** _attribs) {
462 play_tree_t* pt;
463 char *href;
464 stream_t* stream;
465 play_tree_parser_t* ptp;
466 int f=DEMUXER_TYPE_UNKNOWN;
468 if(parser->deep > 0)
469 return NULL;
471 href = asx_get_attrib("HREF",_attribs);
472 if(href == NULL) {
473 asx_warning_attrib_required(parser,"ENTRYREF" ,"HREF" );
474 return NULL;
476 stream=open_stream(href,0,&f);
477 if(!stream) {
478 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Can't open playlist %s\n",href);
479 free(href);
480 return NULL;
483 mp_msg(MSGT_PLAYTREE,MSGL_V,"Adding playlist %s to element entryref\n",href);
485 ptp = play_tree_parser_new(stream,parser->deep+1);
487 pt = play_tree_parser_get_play_tree(ptp, 1);
489 play_tree_parser_free(ptp);
490 free_stream(stream);
491 free(href);
492 //mp_msg(MSGT_PLAYTREE,MSGL_INFO,"Need to implement entryref\n");
494 return pt;
497 static play_tree_t*
498 asx_parse_entry(ASX_Parser_t* parser,char* buffer,char** _attribs) {
499 char *element,*body,**attribs;
500 int r,nref=0;
501 play_tree_t *ref;
503 ref = play_tree_new();
505 while(buffer && buffer[0] != '\0') {
506 r = asx_get_element(parser,&buffer,&element,&body,&attribs);
507 if(r < 0) {
508 asx_warning_body_parse_error(parser,"ENTRY");
509 return NULL;
510 } else if (r == 0) { // No more element
511 break;
513 if(strcasecmp(element,"REF") == 0) {
514 asx_parse_ref(parser,attribs,ref);
515 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to entry\n",element);
516 nref++;
517 } else
518 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Ignoring element %s\n",element);
519 if(body) free(body);
520 asx_free_attribs(attribs);
523 if(nref <= 0) {
524 play_tree_free(ref,1);
525 return NULL;
527 return ref;
532 static play_tree_t*
533 asx_parse_repeat(ASX_Parser_t* parser,char* buffer,char** _attribs) {
534 char *element,*body,**attribs;
535 play_tree_t *repeat, *list=NULL, *entry;
536 char* count;
537 int r;
539 repeat = play_tree_new();
541 count = asx_get_attrib("COUNT",_attribs);
542 if(count == NULL) {
543 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Setting element repeat loop to infinit\n");
544 repeat->loop = -1; // Infinit
545 } else {
546 repeat->loop = atoi(count);
547 free(count);
548 if(repeat->loop == 0) repeat->loop = 1;
549 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Setting element repeat loop to %d\n",repeat->loop);
552 while(buffer && buffer[0] != '\0') {
553 r = asx_get_element(parser,&buffer,&element,&body,&attribs);
554 if(r < 0) {
555 asx_warning_body_parse_error(parser,"REPEAT");
556 return NULL;
557 } else if (r == 0) { // No more element
558 break;
560 if(strcasecmp(element,"ENTRY") == 0) {
561 entry = asx_parse_entry(parser,body,attribs);
562 if(entry) {
563 if(!list) list = entry;
564 else play_tree_append_entry(list,entry);
565 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to repeat\n",element);
567 } else if(strcasecmp(element,"ENTRYREF") == 0) {
568 entry = asx_parse_entryref(parser,body,attribs);
569 if(entry) {
570 if(!list) list = entry;
571 else play_tree_append_entry(list,entry);
572 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to repeat\n",element);
574 } else if(strcasecmp(element,"REPEAT") == 0) {
575 entry = asx_parse_repeat(parser,body,attribs);
576 if(entry) {
577 if(!list) list = entry;
578 else play_tree_append_entry(list,entry);
579 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to repeat\n",element);
581 } else if(strcasecmp(element,"PARAM") == 0) {
582 asx_parse_param(parser,attribs,repeat);
583 } else
584 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Ignoring element %s\n",element);
585 if(body) free(body);
586 asx_free_attribs(attribs);
589 if(!list) {
590 play_tree_free(repeat,1);
591 return NULL;
593 play_tree_set_child(repeat,list);
595 return repeat;
601 play_tree_t*
602 asx_parser_build_tree(char* buffer,int deep) {
603 char *element,*asx_body,**asx_attribs,*body = NULL, **attribs;
604 int r;
605 play_tree_t *asx,*entry,*list = NULL;
606 ASX_Parser_t* parser = asx_parser_new();
608 parser->line = 1;
609 parser->deep = deep;
611 r = asx_get_element(parser,&buffer,&element,&asx_body,&asx_attribs);
612 if(r < 0) {
613 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : Syntax error ???",parser->line);
614 asx_parser_free(parser);
615 return NULL;
616 } else if(r == 0) { // No contents
617 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"empty asx element");
618 asx_parser_free(parser);
619 return NULL;
622 if(strcasecmp(element,"ASX") != 0) {
623 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"first element isn't ASX, it's %s\n",element);
624 asx_free_attribs(asx_attribs);
625 asx_parser_free(parser);
626 return NULL;
629 if(!asx_body) {
630 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"ASX element is empty");
631 asx_free_attribs(asx_attribs);
632 asx_parser_free(parser);
633 return NULL;
636 asx = play_tree_new();
637 buffer = asx_body;
638 while(buffer && buffer[0] != '\0') {
639 r = asx_get_element(parser,&buffer,&element,&body,&attribs);
640 if(r < 0) {
641 asx_warning_body_parse_error(parser,"ASX");
642 asx_parser_free(parser);
643 return NULL;
644 } else if (r == 0) { // No more element
645 break;
647 if(strcasecmp(element,"ENTRY") == 0) {
648 entry = asx_parse_entry(parser,body,attribs);
649 if(entry) {
650 if(!list) list = entry;
651 else play_tree_append_entry(list,entry);
652 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to asx\n",element);
654 } else if(strcasecmp(element,"ENTRYREF") == 0) {
655 entry = asx_parse_entryref(parser,body,attribs);
656 if(entry) {
657 if(!list) list = entry;
658 else play_tree_append_entry(list,entry);
659 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to asx\n",element);
661 } else if(strcasecmp(element,"REPEAT") == 0) {
662 entry = asx_parse_repeat(parser,body,attribs);
663 if(entry) {
664 if(!list) list = entry;
665 else play_tree_append_entry(list,entry);
666 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to asx\n",element);
668 } else
669 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Ignoring element %s\n",element);
670 if(body) free(body);
671 asx_free_attribs(attribs);
674 free(asx_body);
675 asx_free_attribs(asx_attribs);
676 asx_parser_free(parser);
679 if(!list) {
680 play_tree_free(asx,1);
682 return NULL;
685 play_tree_set_child(asx,list);
687 return asx;