ao_pulse: convert to new AO API
[mplayer.git] / asxparser.c
blob5d0d811ab4f0b765a460be220947c7159900e10b
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "playtree.h"
28 #include "playtreeparser.h"
29 #include "stream/stream.h"
30 #include "libmpdemux/demuxer.h"
31 #include "asxparser.h"
32 #include "mp_msg.h"
33 #include "m_config.h"
35 ////// List utils
37 void
38 asx_list_free(void* list_ptr,ASX_FreeFunc free_func) {
39 void** ptr = *(void***)list_ptr;
40 if(ptr == NULL) return;
41 if(free_func != NULL) {
42 for( ; *ptr != NULL ; ptr++)
43 free_func(*ptr);
45 free(*(void**)list_ptr);
46 *(void**)list_ptr = NULL;
49 /////// Attribs utils
51 char*
52 asx_get_attrib(const char* attrib,char** attribs) {
53 char** ptr;
55 if(attrib == NULL || attribs == NULL) return NULL;
56 for(ptr = attribs; ptr[0] != NULL; ptr += 2){
57 if(strcasecmp(ptr[0],attrib) == 0)
58 return strdup(ptr[1]);
60 return NULL;
63 int
64 asx_attrib_to_enum(const char* val,char** valid_vals) {
65 char** ptr;
66 int r = 0;
68 if(valid_vals == NULL || val == NULL) return -2;
69 for(ptr = valid_vals ; ptr[0] != NULL ; ptr++) {
70 if(strcasecmp(val,ptr[0]) == 0) return r;
71 r++;
74 return -1;
77 #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)
78 #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)
80 ASX_Parser_t*
81 asx_parser_new(struct m_config *mconfig)
83 ASX_Parser_t* parser = calloc(1,sizeof(ASX_Parser_t));
84 parser->mconfig = mconfig;
85 return parser;
88 void
89 asx_parser_free(ASX_Parser_t* parser) {
90 if(!parser) return;
91 free(parser->ret_stack);
92 free(parser);
96 #define LETTER "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
97 #define SPACE " \n\t\r"
99 int
100 asx_parse_attribs(ASX_Parser_t* parser,char* buffer,char*** _attribs) {
101 char *ptr1, *ptr2, *ptr3;
102 int n_attrib = 0;
103 char **attribs = NULL;
104 char *attrib, *val;
106 ptr1 = buffer;
107 while(1) {
108 for( ; strchr(SPACE,*ptr1) != NULL; ptr1++) { // Skip space
109 if(*ptr1 == '\0') break;
111 ptr3 = strchr(ptr1,'=');
112 if(ptr3 == NULL) break;
113 for(ptr2 = ptr3-1; strchr(SPACE,*ptr2) != NULL; ptr2--) {
114 if (ptr2 == ptr1) {
115 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : this should never append, back to attribute begin while skipping end space",parser->line);
116 break;
119 attrib = malloc(ptr2-ptr1+2);
120 strncpy(attrib,ptr1,ptr2-ptr1+1);
121 attrib[ptr2-ptr1+1] = '\0';
123 ptr1 = strchr(ptr3,'"');
124 if(ptr1 == NULL || ptr1[1] == '\0') ptr1 = strchr(ptr3,'\'');
125 if(ptr1 == NULL || ptr1[1] == '\0') {
126 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"At line %d : can't find attribute %s value",parser->line,attrib);
127 free(attrib);
128 break;
130 ptr2 = strchr(ptr1+1,ptr1[0]);
131 if (ptr2 == NULL) {
132 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"At line %d : value of attribute %s isn't finished",parser->line,attrib);
133 free(attrib);
134 break;
136 ptr1++;
137 val = malloc(ptr2-ptr1+1);
138 strncpy(val,ptr1,ptr2-ptr1);
139 val[ptr2-ptr1] = '\0';
140 n_attrib++;
142 attribs = realloc(attribs, (2 * n_attrib + 1) * sizeof(char*));
143 attribs[n_attrib*2-2] = attrib;
144 attribs[n_attrib*2-1] = val;
146 ptr1 = ptr2+1;
149 if(n_attrib > 0)
150 attribs[n_attrib*2] = NULL;
152 *_attribs = attribs;
154 return n_attrib;
158 * Return -1 on error, 0 when nothing is found, 1 on sucess
161 asx_get_element(ASX_Parser_t* parser,char** _buffer,
162 char** _element,char** _body,char*** _attribs) {
163 char *ptr1,*ptr2, *ptr3, *ptr4;
164 char *attribs = NULL;
165 char *element = NULL, *body = NULL, *ret = NULL, *buffer;
166 int n_attrib = 0;
167 int body_line = 0,attrib_line,ret_line,in = 0;
168 int quotes = 0;
170 if(_buffer == NULL || _element == NULL || _body == NULL || _attribs == NULL) {
171 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : asx_get_element called with invalid value",parser->line);
172 return -1;
175 *_body = *_element = NULL;
176 *_attribs = NULL;
177 buffer = *_buffer;
179 if(buffer == NULL) return 0;
181 if(parser->ret_stack && /*parser->last_body && */buffer != parser->last_body) {
182 ASX_LineSave_t* ls = parser->ret_stack;
183 int i;
184 for(i = 0 ; i < parser->ret_stack_size ; i++) {
185 if(buffer == ls[i].buffer) {
186 parser->line = ls[i].line;
187 break;
191 if( i < parser->ret_stack_size) {
192 i++;
193 if( i < parser->ret_stack_size)
194 memmove(parser->ret_stack,parser->ret_stack+i, (parser->ret_stack_size - i)*sizeof(ASX_LineSave_t));
195 parser->ret_stack_size -= i;
196 if(parser->ret_stack_size > 0)
197 parser->ret_stack = realloc(parser->ret_stack,parser->ret_stack_size*sizeof(ASX_LineSave_t));
198 else {
199 free(parser->ret_stack);
200 parser->ret_stack = NULL;
205 ptr1 = buffer;
206 while(1) {
207 for( ; ptr1[0] != '<' ; ptr1++) {
208 if(ptr1[0] == '\0') {
209 ptr1 = NULL;
210 break;
212 if(ptr1[0] == '\n') parser->line++;
214 //ptr1 = strchr(ptr1,'<');
215 if(!ptr1 || ptr1[1] == '\0') return 0; // Nothing found
217 if(strncmp(ptr1,"<!--",4) == 0) { // Comments
218 for( ; strncmp(ptr1,"-->",3) != 0 ; ptr1++) {
219 if(ptr1[0] == '\0') {
220 ptr1 = NULL;
221 break;
223 if(ptr1[0] == '\n') parser->line++;
225 //ptr1 = strstr(ptr1,"-->");
226 if(!ptr1) {
227 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : unfinished comment",parser->line);
228 return -1;
230 } else {
231 break;
235 // Is this space skip very useful ??
236 for(ptr1++; strchr(SPACE,ptr1[0]) != NULL; ptr1++) { // Skip space
237 if(ptr1[0] == '\0') {
238 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing element start",parser->line);
239 return -1;
241 if(ptr1[0] == '\n') parser->line++;
244 for(ptr2 = ptr1; strchr(LETTER,*ptr2) != NULL;ptr2++) { // Go to end of name
245 if(*ptr2 == '\0'){
246 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing element start",parser->line);
247 return -1;
249 if(ptr2[0] == '\n') parser->line++;
252 element = malloc(ptr2-ptr1+1);
253 strncpy(element,ptr1,ptr2-ptr1);
254 element[ptr2-ptr1] = '\0';
256 for( ; strchr(SPACE,*ptr2) != NULL; ptr2++) { // Skip space
257 if(ptr2[0] == '\0') {
258 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing element start",parser->line);
259 free(element);
260 return -1;
262 if(ptr2[0] == '\n') parser->line++;
264 attrib_line = parser->line;
268 for(ptr3 = ptr2; ptr3[0] != '\0'; ptr3++) { // Go to element end
269 if(ptr3[0] == '"') quotes ^= 1;
270 if(!quotes && (ptr3[0] == '>' || strncmp(ptr3,"/>",2) == 0))
271 break;
272 if(ptr3[0] == '\n') parser->line++;
274 if(ptr3[0] == '\0' || ptr3[1] == '\0') { // End of file
275 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing element start",parser->line);
276 free(element);
277 return -1;
280 // Save attribs string
281 if(ptr3-ptr2 > 0) {
282 attribs = malloc(ptr3-ptr2+1);
283 strncpy(attribs,ptr2,ptr3-ptr2);
284 attribs[ptr3-ptr2] = '\0';
286 //bs_line = parser->line;
287 if(ptr3[0] != '/') { // Not Self closed element
288 ptr3++;
289 for( ; strchr(SPACE,*ptr3) != NULL; ptr3++) { // Skip space on body begin
290 if(*ptr3 == '\0') {
291 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing %s element body",parser->line,element);
292 free(element);
293 free(attribs);
294 return -1;
296 if(ptr3[0] == '\n') parser->line++;
298 ptr4 = ptr3;
299 body_line = parser->line;
300 while(1) { // Find closing element
301 for( ; ptr4[0] != '<' ; ptr4++) {
302 if(ptr4[0] == '\0') {
303 ptr4 = NULL;
304 break;
306 if(ptr4[0] == '\n') parser->line++;
308 if(ptr4 && strncmp(ptr4,"<!--",4) == 0) { // Comments
309 for( ; strncmp(ptr4,"-->",3) != 0 ; ptr4++) {
310 if(ptr4[0] == '\0') {
311 ptr4 = NULL;
312 break;
314 if(ptr1[0] == '\n') parser->line++;
316 continue;
318 if(ptr4 == NULL || ptr4[1] == '\0') {
319 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : EOB reached while parsing %s element body",parser->line,element);
320 free(element);
321 free(attribs);
322 return -1;
324 if(ptr4[1] != '/' && strncasecmp(element,ptr4+1,strlen(element)) == 0) {
325 in++;
326 ptr4+=2;
327 continue;
328 } else if(strncasecmp(element,ptr4+2,strlen(element)) == 0) { // Extract body
329 if(in > 0) {
330 in--;
331 ptr4 += 2+strlen(element);
332 continue;
334 ret = ptr4+strlen(element)+3;
335 if(ptr4 != ptr3) {
336 ptr4--;
337 for( ; ptr4 != ptr3 && strchr(SPACE,*ptr4) != NULL; ptr4--) ;// Skip space on body end
338 // if(ptr4[0] == '\0') parser->line--;
340 ptr4++;
341 body = malloc(ptr4-ptr3+1);
342 strncpy(body,ptr3,ptr4-ptr3);
343 body[ptr4-ptr3] = '\0';
345 break;
346 } else {
347 ptr4 += 2;
350 } else {
351 ret = ptr3 + 2; // 2 is for />
354 for( ; ret[0] != '\0' && strchr(SPACE,ret[0]) != NULL; ret++) { // Skip space
355 if(ret[0] == '\n') parser->line++;
358 ret_line = parser->line;
360 if(attribs) {
361 parser->line = attrib_line;
362 n_attrib = asx_parse_attribs(parser,attribs,_attribs);
363 free(attribs);
364 if(n_attrib < 0) {
365 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"At line %d : error while parsing element %s attributes",parser->line,element);
366 free(element);
367 free(body);
368 return -1;
370 } else
371 *_attribs = NULL;
373 *_element = element;
374 *_body = body;
376 parser->last_body = body;
377 parser->ret_stack_size++;
378 parser->ret_stack = realloc(parser->ret_stack,parser->ret_stack_size*sizeof(ASX_LineSave_t));
379 if(parser->ret_stack_size > 1)
380 memmove(parser->ret_stack+1,parser->ret_stack,(parser->ret_stack_size-1)*sizeof(ASX_LineSave_t));
381 parser->ret_stack[0].buffer = ret;
382 parser->ret_stack[0].line = ret_line;
383 parser->line = body ? body_line : ret_line;
385 *_buffer = ret;
386 return 1;
390 static void
391 asx_parse_ref(ASX_Parser_t* parser, char** attribs, play_tree_t* pt) {
392 char *href;
394 href = asx_get_attrib("HREF",attribs);
395 if(href == NULL) {
396 asx_warning_attrib_required(parser,"REF" ,"HREF" );
397 return;
399 #if 0
400 // replace http my mmshttp to avoid infinite loops
401 // disabled since some playlists for e.g. WinAMP use asx as well
402 // "-user-agent NSPlayer/4.1.0.3856" is a possible workaround
403 if (strncmp(href, "http://", 7) == 0) {
404 char *newref = malloc(3 + strlen(href) + 1);
405 strcpy(newref, "mms");
406 strcpy(newref + 3, href);
407 free(href);
408 href = newref;
410 #endif
412 play_tree_add_file(pt,href);
414 mp_msg(MSGT_PLAYTREE,MSGL_V,"Adding file %s to element entry\n",href);
416 free(href);
420 static play_tree_t*
421 asx_parse_entryref(ASX_Parser_t* parser,char* buffer,char** _attribs) {
422 play_tree_t* pt;
423 char *href;
424 stream_t* stream;
425 play_tree_parser_t* ptp;
426 int f=DEMUXER_TYPE_UNKNOWN;
428 if(parser->deep > 0)
429 return NULL;
431 href = asx_get_attrib("HREF",_attribs);
432 if(href == NULL) {
433 asx_warning_attrib_required(parser,"ENTRYREF" ,"HREF" );
434 return NULL;
436 stream=open_stream(href,0,&f);
437 if(!stream) {
438 mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Can't open playlist %s\n",href);
439 free(href);
440 return NULL;
443 mp_msg(MSGT_PLAYTREE,MSGL_V,"Adding playlist %s to element entryref\n",href);
445 ptp = play_tree_parser_new(stream, parser->mconfig, parser->deep+1);
447 pt = play_tree_parser_get_play_tree(ptp, 1);
449 play_tree_parser_free(ptp);
450 free_stream(stream);
451 free(href);
452 //mp_msg(MSGT_PLAYTREE,MSGL_INFO,"Need to implement entryref\n");
454 return pt;
457 static play_tree_t*
458 asx_parse_entry(ASX_Parser_t* parser,char* buffer,char** _attribs) {
459 char *element,*body,**attribs;
460 int r,nref=0;
461 play_tree_t *ref;
463 ref = play_tree_new();
465 while(buffer && buffer[0] != '\0') {
466 r = asx_get_element(parser,&buffer,&element,&body,&attribs);
467 if(r < 0) {
468 asx_warning_body_parse_error(parser,"ENTRY");
469 return NULL;
470 } else if (r == 0) { // No more element
471 break;
473 if(strcasecmp(element,"REF") == 0) {
474 asx_parse_ref(parser,attribs,ref);
475 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to entry\n",element);
476 nref++;
477 } else
478 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Ignoring element %s\n",element);
479 free(body);
480 asx_free_attribs(attribs);
483 if(nref <= 0) {
484 play_tree_free(ref,1);
485 return NULL;
487 return ref;
492 static play_tree_t*
493 asx_parse_repeat(ASX_Parser_t* parser,char* buffer,char** _attribs) {
494 char *element,*body,**attribs;
495 play_tree_t *repeat, *list=NULL, *entry;
496 char* count;
497 int r;
499 repeat = play_tree_new();
501 count = asx_get_attrib("COUNT",_attribs);
502 if(count == NULL) {
503 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Setting element repeat loop to infinit\n");
504 repeat->loop = -1; // Infinit
505 } else {
506 repeat->loop = atoi(count);
507 free(count);
508 if(repeat->loop == 0) repeat->loop = 1;
509 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Setting element repeat loop to %d\n",repeat->loop);
512 while(buffer && buffer[0] != '\0') {
513 r = asx_get_element(parser,&buffer,&element,&body,&attribs);
514 if(r < 0) {
515 asx_warning_body_parse_error(parser,"REPEAT");
516 return NULL;
517 } else if (r == 0) { // No more element
518 break;
520 if(strcasecmp(element,"ENTRY") == 0) {
521 entry = asx_parse_entry(parser,body,attribs);
522 if(entry) {
523 if(!list) list = entry;
524 else play_tree_append_entry(list,entry);
525 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to repeat\n",element);
527 } else if(strcasecmp(element,"ENTRYREF") == 0) {
528 entry = asx_parse_entryref(parser,body,attribs);
529 if(entry) {
530 if(!list) list = entry;
531 else play_tree_append_entry(list,entry);
532 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to repeat\n",element);
534 } else if(strcasecmp(element,"REPEAT") == 0) {
535 entry = asx_parse_repeat(parser,body,attribs);
536 if(entry) {
537 if(!list) list = entry;
538 else play_tree_append_entry(list,entry);
539 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to repeat\n",element);
541 } else
542 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Ignoring element %s\n",element);
543 free(body);
544 asx_free_attribs(attribs);
547 if(!list) {
548 play_tree_free(repeat,1);
549 return NULL;
551 play_tree_set_child(repeat,list);
553 return repeat;
559 play_tree_t*
560 asx_parser_build_tree(struct m_config *mconfig, char* buffer,int deep) {
561 char *element,*asx_body,**asx_attribs,*body = NULL, **attribs;
562 int r;
563 play_tree_t *asx,*entry,*list = NULL;
564 ASX_Parser_t* parser = asx_parser_new(mconfig);
566 parser->line = 1;
567 parser->deep = deep;
569 r = asx_get_element(parser,&buffer,&element,&asx_body,&asx_attribs);
570 if(r < 0) {
571 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"At line %d : Syntax error ???",parser->line);
572 asx_parser_free(parser);
573 return NULL;
574 } else if(r == 0) { // No contents
575 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"empty asx element");
576 asx_parser_free(parser);
577 return NULL;
580 if(strcasecmp(element,"ASX") != 0) {
581 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"first element isn't ASX, it's %s\n",element);
582 asx_free_attribs(asx_attribs);
583 asx_parser_free(parser);
584 return NULL;
587 if(!asx_body) {
588 mp_msg(MSGT_PLAYTREE,MSGL_ERR,"ASX element is empty");
589 asx_free_attribs(asx_attribs);
590 asx_parser_free(parser);
591 return NULL;
594 asx = play_tree_new();
595 buffer = asx_body;
596 while(buffer && buffer[0] != '\0') {
597 r = asx_get_element(parser,&buffer,&element,&body,&attribs);
598 if(r < 0) {
599 asx_warning_body_parse_error(parser,"ASX");
600 asx_parser_free(parser);
601 return NULL;
602 } else if (r == 0) { // No more element
603 break;
605 if(strcasecmp(element,"ENTRY") == 0) {
606 entry = asx_parse_entry(parser,body,attribs);
607 if(entry) {
608 if(!list) list = entry;
609 else play_tree_append_entry(list,entry);
610 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to asx\n",element);
612 } else if(strcasecmp(element,"ENTRYREF") == 0) {
613 entry = asx_parse_entryref(parser,body,attribs);
614 if(entry) {
615 if(!list) list = entry;
616 else play_tree_append_entry(list,entry);
617 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to asx\n",element);
619 } else if(strcasecmp(element,"REPEAT") == 0) {
620 entry = asx_parse_repeat(parser,body,attribs);
621 if(entry) {
622 if(!list) list = entry;
623 else play_tree_append_entry(list,entry);
624 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding element %s to asx\n",element);
626 } else
627 mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Ignoring element %s\n",element);
628 free(body);
629 asx_free_attribs(attribs);
632 free(asx_body);
633 asx_free_attribs(asx_attribs);
634 asx_parser_free(parser);
637 if(!list) {
638 play_tree_free(asx,1);
640 return NULL;
643 play_tree_set_child(asx,list);
645 return asx;