xtag.c 25.6 KB
Newer Older
Gildas Bazin's avatar
Gildas Bazin committed
1 2 3 4 5
/*****************************************************************************
 * xtag.c : a trivial parser for XML-like tags
 *****************************************************************************
 * Copyright (C) 2003-2004 Commonwealth Scientific and Industrial Research
 *                         Organisation (CSIRO) Australia
6
 * Copyright (C) 2000-2004 the VideoLAN team
Gildas Bazin's avatar
Gildas Bazin committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * $Id$
 *
 * Authors: Conrad Parker <Conrad.Parker@csiro.au>
 *          Andre Pang <Andre.Pang@csiro.au>
 *          Gildas Bazin <gbazin@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
26
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Gildas Bazin's avatar
Gildas Bazin committed
27 28
 *****************************************************************************/

29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
Gildas Bazin's avatar
Gildas Bazin committed
35 36

#include "vlc_xml.h"
37 38
#include "vlc_block.h"
#include "vlc_stream.h"
Gildas Bazin's avatar
Gildas Bazin committed
39 40 41 42

#include <ctype.h>
#include <stdarg.h>

43 44
#include <assert.h>

Gildas Bazin's avatar
Gildas Bazin committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#undef XTAG_DEBUG

typedef struct _XList
{
    struct _XList *prev;
    struct _XList *next;
    void *data;
} XList;

/*
 * struct XTag is kind of a union ... it normally represents a whole
 * tag (and its children), but it could alternatively represent some
 * PCDATA. Basically, if tag->pcdata is non-NULL, interpret only it and
 * ignore the name, attributes and inner_tags.
 */
typedef struct _XTag
{
    char *name;
    char *pcdata;
    struct _XTag *parent;
    XList *attributes;
    XList *children;
    XList *current_child;
} XTag;

typedef struct _XAttribute
{
    char *name;
    char *value;
} XAttribute;

typedef struct _XTagParser
{
    int valid; /* boolean */
    XTag *current_tag;
    char *start;
    char *end;
} XTagParser;

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

90 91 92 93 94
vlc_module_begin ()
    set_description( N_("Simple XML Parser") )
    set_capability( "xml", 5 )
    set_callbacks( Open, Close )
vlc_module_end ()
Gildas Bazin's avatar
Gildas Bazin committed
95 96 97 98 99 100

struct xml_reader_sys_t
{
    XTag *p_root; /* Root tag */
    XTag *p_curtag; /* Current tag */
    XList *p_curattr; /* Current attribute */
101
    bool b_endtag;
Gildas Bazin's avatar
Gildas Bazin committed
102 103
};

104
static xml_reader_t *ReaderCreate( xml_t *, stream_t * );
Gildas Bazin's avatar
Gildas Bazin committed
105 106 107 108 109 110 111
static void ReaderDelete( xml_reader_t * );
static int ReaderRead( xml_reader_t * );
static int ReaderNodeType( xml_reader_t * );
static char *ReaderName( xml_reader_t * );
static char *ReaderValue( xml_reader_t * );
static int ReaderNextAttr( xml_reader_t * );

112
static int ReaderUseDTD ( xml_reader_t *, bool );
113

Gildas Bazin's avatar
Gildas Bazin committed
114 115 116 117 118
static void CatalogLoad( xml_t *, const char * );
static void CatalogAdd( xml_t *, const char *, const char *, const char * );

static XTag *xtag_new_parse( const char *, int );
static char *xtag_get_name( XTag * );
Rafaël Carré's avatar
Rafaël Carré committed
119
#if 0
Gildas Bazin's avatar
Gildas Bazin committed
120 121
static char *xtag_get_pcdata( XTag * );
static char *xtag_get_attribute( XTag *, char * );
Rafaël Carré's avatar
Rafaël Carré committed
122
#endif
Gildas Bazin's avatar
Gildas Bazin committed
123 124
static XTag *xtag_first_child( XTag *, char * );
static XTag *xtag_next_child( XTag *, char * );
125
static void  xtag_free( XTag * );
Gildas Bazin's avatar
Gildas Bazin committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
static int xtag_snprint( char *, int, XTag * );

/*****************************************************************************
 * Module initialization
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    xml_t *p_xml = (xml_t *)p_this;

    p_xml->pf_reader_create = ReaderCreate;
    p_xml->pf_reader_delete = ReaderDelete;

    p_xml->pf_catalog_load = CatalogLoad;
    p_xml->pf_catalog_add  = CatalogAdd;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Module deinitialization
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
Rafaël Carré's avatar
Rafaël Carré committed
149
    VLC_UNUSED(p_this);
Gildas Bazin's avatar
Gildas Bazin committed
150 151 152 153 154 155 156 157
    return;
}

/*****************************************************************************
 * Catalogue functions
 *****************************************************************************/
static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
{
Rafaël Carré's avatar
Rafaël Carré committed
158
    VLC_UNUSED(psz_filename);
Gildas Bazin's avatar
Gildas Bazin committed
159 160 161 162 163 164
    msg_Dbg( p_xml, "catalog support not implemented" );
}

static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
                          const char *psz_arg2, const char *psz_filename )
{
Rafaël Carré's avatar
Rafaël Carré committed
165 166
    VLC_UNUSED(p_xml); VLC_UNUSED(psz_arg1); VLC_UNUSED(psz_arg2);
    VLC_UNUSED(psz_filename);
Gildas Bazin's avatar
Gildas Bazin committed
167 168 169 170 171
}

/*****************************************************************************
 * Reader functions
 *****************************************************************************/
172
static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *s )
Gildas Bazin's avatar
Gildas Bazin committed
173 174
{
    xml_reader_t *p_reader;
175
    char *p_buffer, *p_new;
176
    int i_size, i_pos = 0, i_buffer = 2048;
Gildas Bazin's avatar
Gildas Bazin committed
177 178 179
    XTag *p_root;

    /* Open and read file */
180
    p_buffer = malloc( i_buffer );
181
    if( p_buffer == NULL )
182
        return NULL;
183

184
    while( ( i_size = stream_Read( s, &p_buffer[i_pos], 2048 ) ) == 2048 )
185
    {
186
        i_pos += i_size;
187
        i_buffer += i_size;
188
        p_new = realloc( p_buffer, i_buffer );
189 190
        if( !p_new )
        {
191 192 193 194
            free( p_buffer );
            return NULL;
        }
        p_buffer = p_new;
195
    }
196
    if( i_pos + i_size == 0 )
Gildas Bazin's avatar
Gildas Bazin committed
197
    {
198
        msg_Dbg( p_xml, "empty XML" );
Gildas Bazin's avatar
Gildas Bazin committed
199
        free( p_buffer );
Rémi Duraffort's avatar
Rémi Duraffort committed
200
        return NULL;
Gildas Bazin's avatar
Gildas Bazin committed
201
    }
Rémi Duraffort's avatar
Rémi Duraffort committed
202
    p_buffer[ i_pos + i_size ] = '\0'; /* 0 terminated string */
Gildas Bazin's avatar
Gildas Bazin committed
203 204 205 206

    p_root = xtag_new_parse( p_buffer, i_buffer );
    if( !p_root )
    {
207
        msg_Warn( p_xml, "couldn't parse XML" );
Gildas Bazin's avatar
Gildas Bazin committed
208
        free( p_buffer );
Rémi Duraffort's avatar
Rémi Duraffort committed
209
        return NULL;
Gildas Bazin's avatar
Gildas Bazin committed
210 211
    }

212
    free( p_buffer );
Gildas Bazin's avatar
Gildas Bazin committed
213
    p_reader = malloc( sizeof(xml_reader_t) );
Rémi Duraffort's avatar
Rémi Duraffort committed
214 215
    if( !p_reader )
        return NULL;
216
    p_reader->p_sys = malloc( sizeof(xml_reader_sys_t) );
Rémi Duraffort's avatar
Rémi Duraffort committed
217 218 219 220 221
    if( !p_reader->p_sys )
    {
        free( p_reader );
        return NULL;
    }
Gildas Bazin's avatar
Gildas Bazin committed
222 223 224
    p_reader->p_sys->p_root = p_root;
    p_reader->p_sys->p_curtag = NULL;
    p_reader->p_sys->p_curattr = NULL;
225
    p_reader->p_sys->b_endtag = false;
Gildas Bazin's avatar
Gildas Bazin committed
226 227 228 229 230 231 232
    p_reader->p_xml = p_xml;

    p_reader->pf_read = ReaderRead;
    p_reader->pf_node_type = ReaderNodeType;
    p_reader->pf_name = ReaderName;
    p_reader->pf_value = ReaderValue;
    p_reader->pf_next_attr = ReaderNextAttr;
233
    p_reader->pf_use_dtd = ReaderUseDTD;
Gildas Bazin's avatar
Gildas Bazin committed
234 235 236 237 238 239 240 241 242 243 244

    return p_reader;
}

static void ReaderDelete( xml_reader_t *p_reader )
{
    xtag_free( p_reader->p_sys->p_root );
    free( p_reader->p_sys );
    free( p_reader );
}

245
static int ReaderUseDTD ( xml_reader_t *p_reader, bool b_use )
246
{
Rafaël Carré's avatar
Rafaël Carré committed
247
    VLC_UNUSED(p_reader); VLC_UNUSED(b_use);
248 249 250
    return VLC_EGENERIC;
}

Gildas Bazin's avatar
Gildas Bazin committed
251 252 253 254 255 256 257 258 259 260
static int ReaderRead( xml_reader_t *p_reader )
{
    XTag *p_child;

    if( !p_reader->p_sys->p_curtag )
    {
        p_reader->p_sys->p_curtag = p_reader->p_sys->p_root;
        return 1;
    }

Rémi Duraffort's avatar
Rémi Duraffort committed
261
    while( true )
Gildas Bazin's avatar
Gildas Bazin committed
262 263 264 265
    {
        if( (p_child = xtag_next_child( p_reader->p_sys->p_curtag, 0 )) )
        {
            p_reader->p_sys->p_curtag = p_child;
Rémi Duraffort's avatar
Rémi Duraffort committed
266
            p_reader->p_sys->p_curattr = NULL;
267
            p_reader->p_sys->b_endtag = false;
Gildas Bazin's avatar
Gildas Bazin committed
268 269 270
            return 1;
        }

271 272
        if( p_reader->p_sys->p_curtag->name && /* no end tag for pcdata */
            !p_reader->p_sys->b_endtag )
Gildas Bazin's avatar
Gildas Bazin committed
273
        {
274
            p_reader->p_sys->b_endtag = true;
Gildas Bazin's avatar
Gildas Bazin committed
275 276
            return 1;
        }
277

278
        p_reader->p_sys->b_endtag = false;
Gildas Bazin's avatar
Gildas Bazin committed
279 280 281 282 283 284 285 286 287
        if( !p_reader->p_sys->p_curtag->parent ) return 0;
        p_reader->p_sys->p_curtag = p_reader->p_sys->p_curtag->parent;
    }

    return 0;
}

static int ReaderNodeType( xml_reader_t *p_reader )
{
Rémi Duraffort's avatar
Rémi Duraffort committed
288 289 290 291 292 293
    if( p_reader->p_sys->p_curtag->name && p_reader->p_sys->b_endtag )
        return XML_READER_ENDELEM;
    if( p_reader->p_sys->p_curtag->name )
        return XML_READER_STARTELEM;
    if( p_reader->p_sys->p_curtag->pcdata )
        return XML_READER_TEXT;
Gildas Bazin's avatar
Gildas Bazin committed
294 295 296 297 298 299 300 301
    return XML_READER_NONE;
}

static char *ReaderName( xml_reader_t *p_reader )
{
    const char *psz_name;

    if( !p_reader->p_sys->p_curattr )
302
    {
Gildas Bazin's avatar
Gildas Bazin committed
303
        psz_name = xtag_get_name( p_reader->p_sys->p_curtag );
304
#ifdef XTAG_DEBUG
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
305
        fprintf( stderr, "TAG: %s\n", psz_name );
306 307
#endif
    }
Gildas Bazin's avatar
Gildas Bazin committed
308 309 310
    else
        psz_name = ((XAttribute *)p_reader->p_sys->p_curattr->data)->name;

Rémi Duraffort's avatar
Rémi Duraffort committed
311
    return psz_name ? strdup( psz_name ) : NULL;
Gildas Bazin's avatar
Gildas Bazin committed
312 313 314 315 316
}

static char *ReaderValue( xml_reader_t *p_reader )
{
    const char *psz_name;
317 318 319
    if( p_reader->p_sys->p_curtag->pcdata )
    {
#ifdef XTAG_DEBUG
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
320
        fprintf( stderr, "%s\n", p_reader->p_sys->p_curtag->pcdata );
321 322 323
#endif
        return strdup( p_reader->p_sys->p_curtag->pcdata );
    }
Gildas Bazin's avatar
Gildas Bazin committed
324

Rémi Duraffort's avatar
Rémi Duraffort committed
325
    if( !p_reader->p_sys->p_curattr ) return NULL;
Gildas Bazin's avatar
Gildas Bazin committed
326 327

#ifdef XTAG_DEBUG
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
328
    fprintf( stderr, "%s=%s\n", ((XAttribute *)p_reader->p_sys->p_curattr->data)->name,
Gildas Bazin's avatar
Gildas Bazin committed
329 330 331 332 333
            ((XAttribute *)p_reader->p_sys->p_curattr->data)->value );
#endif

    psz_name = ((XAttribute *)p_reader->p_sys->p_curattr->data)->value;

Rémi Duraffort's avatar
Rémi Duraffort committed
334
    return psz_name ? strdup( psz_name ) : NULL;
Gildas Bazin's avatar
Gildas Bazin committed
335 336 337 338 339 340 341 342
}

static int ReaderNextAttr( xml_reader_t *p_reader )
{
    if( !p_reader->p_sys->p_curattr )
        p_reader->p_sys->p_curattr = p_reader->p_sys->p_curtag->attributes;
    else if( p_reader->p_sys->p_curattr )
        p_reader->p_sys->p_curattr = p_reader->p_sys->p_curattr->next;
Rémi Duraffort's avatar
Rémi Duraffort committed
343 344

    return p_reader->p_sys->p_curattr ? VLC_SUCCESS : VLC_EGENERIC;
Gildas Bazin's avatar
Gildas Bazin committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358
}

/*****************************************************************************
 * XTAG parser functions
 *****************************************************************************/

static XList *xlist_append( XList *list, void *data )
{
    XList *l, *last;

    l = (XList *)malloc( sizeof(XList) );
    l->prev = l->next = NULL;
    l->data = data;

Rémi Duraffort's avatar
Rémi Duraffort committed
359 360
    if( !list )
        return l;
Gildas Bazin's avatar
Gildas Bazin committed
361 362

    for( last = list; last; last = last->next )
Rémi Duraffort's avatar
Rémi Duraffort committed
363 364 365 366
    {
        if( !last->next )
            break;
    }
Gildas Bazin's avatar
Gildas Bazin committed
367 368

    if( last ) last->next = l;
369
    l->prev = last;
Gildas Bazin's avatar
Gildas Bazin committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
    return list;
}

static void xlist_free( XList *list )
{
    XList *l, *ln;

    for( l = list; l; l = ln )
    {
        ln = l->next;
        free( l );
    }
}

/* Character classes */
#define X_NONE           0
#define X_WHITESPACE  1<<0
#define X_OPENTAG     1<<1
#define X_CLOSETAG    1<<2
#define X_DQUOTE      1<<3
#define X_SQUOTE      1<<4
#define X_EQUAL       1<<5
#define X_SLASH       1<<6
#define X_QMARK       1<<7
#define X_DASH        1<<8
395
#define X_EMARK       1<<9
Gildas Bazin's avatar
Gildas Bazin committed
396 397 398

static int xtag_cin( char c, int char_class )
{
399 400 401 402 403 404 405 406 407 408 409 410
    if( char_class & X_WHITESPACE ) if( isspace(c) ) return true;
    if( char_class & X_OPENTAG )    if( c == '<' ) return true;
    if( char_class & X_CLOSETAG )   if( c == '>' ) return true;
    if( char_class & X_DQUOTE )     if( c == '"' ) return true;
    if( char_class & X_SQUOTE )     if( c == '\'' ) return true;
    if( char_class & X_EQUAL )      if( c == '=' ) return true;
    if( char_class & X_SLASH )      if( c == '/' ) return true;
    if( char_class & X_QMARK )      if( c == '?' ) return true;
    if( char_class & X_DASH  )      if( c == '-' ) return true;
    if( char_class & X_EMARK )      if( c == '!' ) return true;

    return false;
Gildas Bazin's avatar
Gildas Bazin committed
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
}

static int xtag_index( XTagParser *parser, int char_class )
{
    char *s = parser->start;
    int i;

    for( i = 0; s[i] && s != parser->end; i++ )
    {
        if( xtag_cin( s[i], char_class ) ) return i;
    }

    return -1;
}

static void xtag_skip_over( XTagParser *parser, int char_class )
{
    char *s = parser->start;
    int i;

    if( !parser->valid ) return;

    for( i = 0; s[i] && s != parser->end; i++ )
    {
        if( !xtag_cin( s[i], char_class ) )
        {
            parser->start = &s[i];
            return;
        }
    }

    return;
}

static void xtag_skip_whitespace( XTagParser * parser )
{
    xtag_skip_over( parser, X_WHITESPACE );
}

static char *xtag_slurp_to( XTagParser *parser, int good_end, int bad_end )
{
    char *ret, *s = parser->start;
    int xi;

    if( !parser->valid ) return NULL;

    xi = xtag_index( parser, good_end | bad_end );

    if( xi > 0 && xtag_cin (s[xi], good_end) )
    {
461
        ret = malloc( xi+1 );
Gildas Bazin's avatar
Gildas Bazin committed
462 463 464 465 466 467 468 469 470 471 472 473 474
        strncpy( ret, s, xi );
        ret[xi] = '\0';
        parser->start = &s[xi];
        return ret;
    }

    return NULL;
}

static int xtag_assert_and_pass( XTagParser *parser, int char_class )
{
    char *s = parser->start;

475
    if( !parser->valid ) return false;
Gildas Bazin's avatar
Gildas Bazin committed
476 477 478

    if( !xtag_cin( s[0], char_class ) )
    {
479 480
        parser->valid = false;
        return false;
Gildas Bazin's avatar
Gildas Bazin committed
481 482 483 484
    }

    parser->start = &s[1];

485
    return true;
Gildas Bazin's avatar
Gildas Bazin committed
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
}

static char *xtag_slurp_quoted( XTagParser *parser )
{
    char * ret, *s;
    int quote = X_DQUOTE; /* quote char to match on */
    int xi;

    if( !parser->valid ) return NULL;

    xtag_skip_whitespace( parser );

    s = parser->start;

    if( xtag_cin( s[0], X_SQUOTE ) ) quote = X_SQUOTE;

    if( !xtag_assert_and_pass( parser, quote ) ) return NULL;

    s = parser->start;

    for( xi = 0; s[xi]; xi++ )
    {
        if( xtag_cin( s[xi], quote ) )
        {
            if( !(xi > 1 && s[xi-1] == '\\') ) break;
        }
    }

514
    ret = malloc( xi+1 );
Gildas Bazin's avatar
Gildas Bazin committed
515 516 517 518
    strncpy( ret, s, xi );
    ret[xi] = '\0';
    parser->start = &s[xi];

Rémi Duraffort's avatar
Rémi Duraffort committed
519 520 521 522 523
    if( !xtag_assert_and_pass( parser, quote ) )
    {
        free( ret );
        return NULL;
    }
Gildas Bazin's avatar
Gildas Bazin committed
524 525 526 527 528 529 530 531 532 533

    return ret;
}

static XAttribute *xtag_parse_attribute( XTagParser *parser )
{
    XAttribute *attr;
    char *name, *value;
    char *s;

Rémi Duraffort's avatar
Rémi Duraffort committed
534 535
    if( !parser->valid )
        return NULL;
Gildas Bazin's avatar
Gildas Bazin committed
536 537 538 539

    xtag_skip_whitespace( parser );
 
    name = xtag_slurp_to( parser, X_WHITESPACE|X_EQUAL, X_SLASH|X_CLOSETAG );
Rémi Duraffort's avatar
Rémi Duraffort committed
540 541
    if( !name )
        return NULL;
Gildas Bazin's avatar
Gildas Bazin committed
542 543 544 545 546 547 548

    xtag_skip_whitespace( parser );
    s = parser->start;

    if( !xtag_assert_and_pass( parser, X_EQUAL ) )
    {
#ifdef XTAG_DEBUG
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
549
        fprintf( stderr, "xtag: attr failed EQUAL on <%s>\n", name );
Gildas Bazin's avatar
Gildas Bazin committed
550 551 552 553 554 555 556 557 558 559 560
#endif
        goto err_free_name;
    }

    xtag_skip_whitespace( parser );

    value = xtag_slurp_quoted( parser );

    if( value == NULL )
    {
#ifdef XTAG_DEBUG
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
561
        fprintf (stderr, "Got NULL quoted attribute value\n");
Gildas Bazin's avatar
Gildas Bazin committed
562 563 564 565 566 567 568 569 570 571 572
#endif
        goto err_free_name;
    }

    attr = malloc( sizeof (*attr) );
    attr->name = name;
    attr->value = value;
    return attr;

 err_free_name:
    free (name);
573
    parser->valid = false;
Gildas Bazin's avatar
Gildas Bazin committed
574 575 576 577 578 579 580 581 582 583
    return NULL;
}

static XTag *xtag_parse_tag( XTagParser *parser )
{
    XTag *tag, *inner;
    XAttribute *attr;
    char *name;
    char *pcdata;
    char *s;
584
     int xi;
Gildas Bazin's avatar
Gildas Bazin committed
585 586 587 588 589 590

    if( !parser->valid ) return NULL;

    s = parser->start;

    /* if this starts a comment tag, skip until end */
591
    if( (parser->end - parser->start) > 7 &&
592
          xtag_cin( s[0], X_OPENTAG ) && xtag_cin( s[1], X_EMARK ) &&
Gildas Bazin's avatar
Gildas Bazin committed
593 594 595 596 597
        xtag_cin( s[2], X_DASH ) && xtag_cin( s[3], X_DASH ) )
    {
        parser->start = s = &s[4];
        while( (xi = xtag_index( parser, X_DASH )) >= 0 )
        {
598 599
            parser->start = s = &s[xi+1];
            if( xtag_cin( s[0], X_DASH ) && xtag_cin( s[1], X_CLOSETAG ) )
Gildas Bazin's avatar
Gildas Bazin committed
600
            {
601 602
                parser->start = &s[2];
                xtag_skip_whitespace( parser );
Gildas Bazin's avatar
Gildas Bazin committed
603 604 605
                return xtag_parse_tag( parser );
            }
        }
606 607
        return NULL;
    }
Gildas Bazin's avatar
Gildas Bazin committed
608

609 610
    /* ignore processing instructions '<?' ... '?>' */
    if( (parser->end - parser->start) > 4 &&
611
          xtag_cin( s[0], X_OPENTAG ) && xtag_cin( s[1], X_QMARK ) )
612 613 614 615 616 617 618 619 620
    {
        parser->start = s = &s[2];
        while ((xi = xtag_index( parser, X_QMARK )) >= 0) {
            if (xtag_cin( s[xi+1], X_CLOSETAG )) {
                parser->start = &s[xi+2];
                xtag_skip_whitespace( parser );
                return xtag_parse_tag( parser );
            }
        }
Gildas Bazin's avatar
Gildas Bazin committed
621 622 623
        return NULL;
    }

624 625
    /* ignore doctype  '<!DOCTYPE' ... '>' */
    if ( (parser->end - parser->start) > 8 &&
626
            !strncmp( s, "<!DOCTYPE", 9 ) ) {
627 628 629 630 631 632 633 634 635 636 637 638
        xi = xtag_index( parser, X_CLOSETAG );
        if ( xi > 0 ) {
            parser->start = s = &s[xi+1];
            xtag_skip_whitespace( parser );
            return xtag_parse_tag( parser );
        }
        else {
            return NULL;
        }
    }

    if( (pcdata = xtag_slurp_to( parser, X_OPENTAG, X_NONE )) != NULL )
Gildas Bazin's avatar
Gildas Bazin committed
639
    {
640 641 642 643 644 645 646
        tag = malloc( sizeof(*tag) );
        tag->name = NULL;
        tag->pcdata = pcdata;
        tag->parent = parser->current_tag;
        tag->attributes = NULL;
        tag->children = NULL;
        tag->current_child = NULL;
Gildas Bazin's avatar
Gildas Bazin committed
647

648 649 650 651 652 653 654 655
        return tag;
    }

    /* if this starts a close tag, return NULL and let the parent take it */
    if( xtag_cin( s[0], X_OPENTAG ) && xtag_cin( s[1], X_SLASH ) )
        return NULL;

    /* parse CDATA content */
656 657
    if ( (parser->end - parser->start) > 8 &&
            !strncmp( s, "<![CDATA[", 9 ) ) {
658 659 660 661
        parser->start = s = &s[9];
        while (parser->end - s > 2) {
            if (strncmp( s, "]]>", 3 ) == 0) {
                if ( !(tag = malloc( sizeof(*tag))) ) return NULL;
662
                if ( !(pcdata = malloc( s - parser->start + 1)) )
663 664 665 666
                {
                    free( tag );
                    return NULL;
                }
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
                strncpy( pcdata, parser->start, s - parser->start );
                pcdata[s - parser->start]='\0';
                parser->start = s = &s[3];
                tag->name = NULL;
                tag->pcdata = pcdata;
                tag->parent = parser->current_tag;
                tag->attributes = NULL;
                tag->children = NULL;
                tag->current_child = NULL;
                return tag;
            }
            else {
                s++;
            }
        }
        return NULL;
Gildas Bazin's avatar
Gildas Bazin committed
683 684 685 686 687 688 689 690
    }

    if( !xtag_assert_and_pass( parser, X_OPENTAG ) ) return NULL;

    name = xtag_slurp_to( parser, X_WHITESPACE|X_SLASH|X_CLOSETAG, X_NONE );
    if( name == NULL ) return NULL;

#ifdef XTAG_DEBUG
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
691
    fprintf (stderr, "<%s ...\n", name);
Gildas Bazin's avatar
Gildas Bazin committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
#endif

    tag = malloc( sizeof(*tag) );
    tag->name = name;
    tag->pcdata = NULL;
    tag->parent = parser->current_tag;
    tag->attributes = NULL;
    tag->children = NULL;
    tag->current_child = NULL;

    s = parser->start;

    if( xtag_cin( s[0], X_WHITESPACE ) )
    {
        while( (attr = xtag_parse_attribute( parser )) != NULL )
        {
            tag->attributes = xlist_append( tag->attributes, attr );
        }
    }

712
    xtag_skip_whitespace( parser );
Gildas Bazin's avatar
Gildas Bazin committed
713 714 715 716 717 718 719

    s = parser->start;

    if( xtag_cin( s[0], X_CLOSETAG ) )
    {
        parser->current_tag = tag;

720
        xtag_assert_and_pass( parser, X_CLOSETAG );
Gildas Bazin's avatar
Gildas Bazin committed
721 722 723 724 725 726

        while( (inner = xtag_parse_tag( parser ) ) != NULL )
        {
            tag->children = xlist_append( tag->children, inner );
        }

727 728
        parser->current_tag = tag->parent;
        xtag_skip_whitespace( parser );
Gildas Bazin's avatar
Gildas Bazin committed
729

730 731 732
        xtag_assert_and_pass( parser, X_OPENTAG );
        xtag_assert_and_pass( parser, X_SLASH );
        name = xtag_slurp_to( parser, X_WHITESPACE | X_CLOSETAG, X_NONE );
Gildas Bazin's avatar
Gildas Bazin committed
733 734 735 736 737
        if( name )
        {
            if( strcmp( name, tag->name ) )
            {
#ifdef XTAG_DEBUG
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
738
                fprintf (stderr, "got %s expected %s\n", name, tag->name);
Gildas Bazin's avatar
Gildas Bazin committed
739
#endif
740
                parser->valid = false;
Gildas Bazin's avatar
Gildas Bazin committed
741 742 743 744 745 746
            }
            free( name );
        }

        xtag_skip_whitespace( parser );
        xtag_assert_and_pass( parser, X_CLOSETAG );
747
        xtag_skip_whitespace( parser );
Gildas Bazin's avatar
Gildas Bazin committed
748 749 750 751 752
    }
    else
    {
        xtag_assert_and_pass( parser, X_SLASH );
        xtag_assert_and_pass( parser, X_CLOSETAG );
753
        xtag_skip_whitespace( parser );
Gildas Bazin's avatar
Gildas Bazin committed
754 755 756 757 758
    }

    return tag;
}

759
static void xtag_free( XTag *xtag )
Gildas Bazin's avatar
Gildas Bazin committed
760 761 762 763 764
{
    XList *l;
    XAttribute *attr;
    XTag *child;

Rémi Duraffort's avatar
Rémi Duraffort committed
765
    if( !xtag )
766
        return;
Gildas Bazin's avatar
Gildas Bazin committed
767

768 769
    free( xtag->name );
    free( xtag->pcdata );
Gildas Bazin's avatar
Gildas Bazin committed
770 771 772 773 774

    for( l = xtag->attributes; l; l = l->next )
    {
        if( (attr = (XAttribute *)l->data) != NULL )
        {
775 776
            free( attr->name );
            free( attr->value );
Gildas Bazin's avatar
Gildas Bazin committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
            free( attr );
        }
    }
    xlist_free( xtag->attributes );

    for( l = xtag->children; l; l = l->next )
    {
        child = (XTag *)l->data;
        xtag_free( child );
    }
    xlist_free( xtag->children );

    free( xtag );
}

static XTag *xtag_new_parse( const char *s, int n )
{
    XTagParser parser;
    XTag *tag, *ttag, *wrapper;

797
    parser.valid = true;
Gildas Bazin's avatar
Gildas Bazin committed
798 799 800 801
    parser.current_tag = NULL;
    parser.start = (char *)s;

    if( n == -1 ) parser.end = NULL;
802 803 804
    else if( n == 0 )
    {
#ifdef XTAG_DEBUG
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
805
        fprintf (stderr, "empty buffer\n");
806
#endif
807 808
        return NULL;
    }
Gildas Bazin's avatar
Gildas Bazin committed
809 810
    else parser.end = (char *)&s[n];

811 812 813
    /* can't have whitespace pcdata outside rootnode */
    xtag_skip_whitespace( &parser );

Gildas Bazin's avatar
Gildas Bazin committed
814 815 816 817
    tag = xtag_parse_tag( &parser );

    if( !parser.valid )
    {
818
#ifdef XTAG_DEBUG
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
819
        fprintf (stderr, "invalid file\n");
820
#endif
Gildas Bazin's avatar
Gildas Bazin committed
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
        xtag_free( tag );
        return NULL;
    }

    if( (ttag = xtag_parse_tag( &parser )) != NULL )
    {
        if( !parser.valid )
        {
            xtag_free( ttag );
            return tag;
        }

        wrapper = malloc( sizeof(XTag) );
        wrapper->name = NULL;
        wrapper->pcdata = NULL;
        wrapper->parent = NULL;
        wrapper->attributes = NULL;
        wrapper->children = NULL;
        wrapper->current_child = NULL;

        wrapper->children = xlist_append( wrapper->children, tag );
        wrapper->children = xlist_append( wrapper->children, ttag );

        while( (ttag = xtag_parse_tag( &parser )) != NULL )
        {
            if( !parser.valid )
            {
                xtag_free( ttag );
                return wrapper;
            }

            wrapper->children = xlist_append( wrapper->children, ttag );
        }
        return wrapper;
    }

    return tag;
}

static char *xtag_get_name( XTag *xtag )
{
    return xtag ? xtag->name : NULL;
}

Rafaël Carré's avatar
Rafaël Carré committed
865
#if 0
Gildas Bazin's avatar
Gildas Bazin committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
static char *xtag_get_pcdata( XTag *xtag )
{
    XList *l;
    XTag *child;

    if( xtag == NULL ) return NULL;

    for( l = xtag->children; l; l = l->next )
    {
        child = (XTag *)l->data;
        if( child->pcdata != NULL )
        {
            return child->pcdata;
        }
    }

    return NULL;
}

static char *xtag_get_attribute( XTag *xtag, char *attribute )
{
    XList *l;
    XAttribute *attr;

    if( xtag == NULL ) return NULL;

    for( l = xtag->attributes; l; l = l->next )
    {
        if( (attr = (XAttribute *)l->data) != NULL )
        {
            if( !strcmp( attr->name, attribute ) ) return attr->value;
        }
    }

    return NULL;
}
Rafaël Carré's avatar
Rafaël Carré committed
902
#endif
Gildas Bazin's avatar
Gildas Bazin committed
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980

static XTag *xtag_first_child( XTag *xtag, char *name )
{
    XList *l;
    XTag *child;

    if( xtag == NULL ) return NULL;
    if( (l = xtag->children) == NULL ) return NULL;

    if( name == NULL )
    {
        xtag->current_child = l;
        return (XTag *)l->data;
    }

    for( ; l; l = l->next )
    {
        child = (XTag *)l->data;

        if( !strcmp( child->name, name ) )
        {
            xtag->current_child = l;
            return child;
        }
    }

    xtag->current_child = NULL;

    return NULL;
}

static XTag *xtag_next_child( XTag *xtag, char *name )
{
    XList *l;
    XTag *child;

    if( xtag == NULL ) return NULL;

    if( (l = xtag->current_child) == NULL )
        return xtag_first_child( xtag, name );

    if( (l = l->next) == NULL ) return NULL;

    if( name == NULL )
    {
        xtag->current_child = l;
        return (XTag *)l->data;
    }

    for( ; l; l = l->next )
    {
        child = (XTag *)l->data;

        if( !strcmp( child->name, name ) )
        {
            xtag->current_child = l;
            return child;
        }
    }

    xtag->current_child = NULL;

    return NULL;
}

/*
 * This snprints function takes a variable list of char *, the last of
 * which must be NULL, and prints each in turn to buf.
 * Returns C99-style total length that would have been written, even if
 * this is larger than n.
 */
static int xtag_snprints( char *buf, int n, ... )
{
    va_list ap;
    char *s;
    int len, to_copy, total = 0;

    va_start( ap, n );
981
 
Gildas Bazin's avatar
Gildas Bazin committed
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
    for( s = va_arg( ap, char * ); s; s = va_arg( ap, char *) )
    {
        len = strlen (s);

        if( (to_copy = __MIN(n, len) ) > 0 )
        {
            memcpy( buf, s, to_copy );
            buf += to_copy;
            n -= to_copy;
        }

        total += len;
    }

    va_end( ap );

    return total;
}

static int xtag_snprint( char *buf, int n, XTag *xtag )
{
    int nn, written = 0;
    XList *l;
    XAttribute *attr;
    XTag *child;

#define FORWARD(N) \
    buf += __MIN(n, N); \
    n = __MAX(n-N, 0);  \
    written += N;

    if( xtag == NULL )
    {
        if( n > 0 ) buf[0] = '\0';
        return 0;
    }

    if( xtag->pcdata )
    {
        nn = xtag_snprints( buf, n, xtag->pcdata, NULL );
        FORWARD( nn );

        return written;
    }

    if( xtag->name )
    {
        nn = xtag_snprints( buf, n, "<", xtag->name, NULL );
        FORWARD( nn );

        for( l = xtag->attributes; l; l = l->next )
        {
            attr = (XAttribute *)l->data;
1035
 
Gildas Bazin's avatar
Gildas Bazin committed
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
            nn = xtag_snprints( buf, n, " ", attr->name, "=\"", attr->value,
                                "\"", NULL);
            FORWARD( nn );
        }

        if( xtag->children == NULL )
        {
            nn = xtag_snprints ( buf, n, "/>", NULL );
            FORWARD( nn );

            return written;
        }

        nn = xtag_snprints( buf, n, ">", NULL );
        FORWARD( nn );
    }

    for( l = xtag->children; l; l = l->next )
    {
        child = (XTag *)l->data;

        nn = xtag_snprint( buf, n, child );
        FORWARD( nn );
    }

    if( xtag->name )
    {
        nn = xtag_snprints( buf, n, "</", xtag->name, ">", NULL );
        FORWARD( nn );
    }

    return written;
}
Rémi Duraffort's avatar
Rémi Duraffort committed
1069