summaryrefslogtreecommitdiff
path: root/support/texlab/crates/bibutils_sys/src/xml.c
blob: 922a24075f9d63ea445784572bb55cffffa83f7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * xml.c
 *
 * Copyright (c) Chris Putnam 2004-2019
 *
 * Source code released under the GPL version 2
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "is_ws.h"
#include "strsearch.h"
#include "xml.h"
#include "msvc_fix.h"

char *xml_pns = NULL;

void
xml_init( xml *node )
{
	str_init( &(node->tag) );
	str_init( &(node->value) );
	slist_init( &(node->attributes) );
	slist_init( &(node->attribute_values) );
	node->down = NULL;
	node->next = NULL;
}

static xml *
xml_new( void )
{
	xml *node = ( xml * ) malloc( sizeof( xml ) );
	if ( node ) xml_init( node );
	return node;
}

static void
xml_delete( xml *node )
{
	xml_free( node );
	free( node );
}

void
xml_free( xml *node )
{
	str_free( &(node->tag) );
	str_free( &(node->value) );
	slist_free( &(node->attributes) );
	slist_free( &(node->attribute_values) );
	if ( node->down ) xml_delete( node->down );
	if ( node->next ) xml_delete( node->next );
}

enum {
	XML_DESCRIPTOR,
	XML_COMMENT,
	XML_OPEN,
	XML_CLOSE,
	XML_OPENCLOSE
};

static int
xml_is_terminator( const char *p, int *type )
{
	if ( *p=='>' ) {
		return 1;
	} else if ( *p=='/' && *(p+1)=='>' ) {
		if ( *type==XML_OPENCLOSE ) return 1;
		else if ( *type==XML_OPEN ) {
			*type = XML_OPENCLOSE;
			return 1;
		}
	} else if ( *p=='?' && *(p+1)=='>' && *type==XML_DESCRIPTOR ) {
		return 1;
	} else if ( *p=='!' && *(p+1)=='>' && *type==XML_COMMENT ) {
		return 1;
	}
	return 0;
}

static int
xml_add_attribute( xml *node, char *attribute, char *attribute_value  )
{
	int status;

	if ( attribute )
		status = slist_addc( &(node->attributes), attribute );
	else
		status = slist_addc( &(node->attributes), "" );
	if ( status!=SLIST_OK ) return 0;

	if ( attribute_value )
		status = slist_addc( &(node->attribute_values), attribute_value );
	else
		status = slist_addc( &(node->attribute_values), "" );
	if ( status!=SLIST_OK ) {
		(void) slist_remove( &(node->attributes), node->attributes.n-1 );
		return 0;
	}
	return 1;
}

static const char *
xml_processattrib( const char *p, xml *node, int *type )
{
	char quote_character = '\"';
	int inquotes = 0;
	str aname, aval;

	str_init( &aname );
	str_init( &aval );

	while ( *p && !xml_is_terminator( p, type ) ) {

		/* get attribute name */
		while ( *p==' ' || *p=='\t' ) p++;
		while ( *p && !strchr( "= \t", *p ) && !xml_is_terminator( p, type ) ){
			str_addchar( &aname, *p );
			p++;
		}

		/* equals sign */
		while ( *p==' ' || *p=='\t' ) p++;
		if ( *p=='=' ) p++;
		while ( *p==' ' || *p=='\t' ) p++;

		/* get attribute value */
		if ( *p=='\"' || *p=='\'' ) {
			if ( *p=='\'' ) quote_character = *p;
			inquotes=1;
			p++;
		}
		while ( *p && ((!xml_is_terminator(p,type) && !strchr("= \t", *p ))||inquotes)){
			if ( *p==quote_character ) inquotes=0;
			else str_addchar( &aval, *p );
			p++;
		}
		if ( str_has_value( &aname ) ) {
			xml_add_attribute( node, str_cstr( &aname ), str_cstr( &aval ) );
		}

		str_empty( &aname );
		str_empty( &aval );
	}

	str_free( &aname );
	str_free( &aval );

	return p;
}

/*
 * xml_processtag
 *
 *                        start right after '<'
 *                        *
 *      XML_COMMENT      <!-- ....  -->
 * 	XML_DESCRIPTOR   <?.....>
 * 	XML_OPEN         <A>
 * 	XML_CLOSE        </A>
 * 	XML_OPENCLOSE    <A/>
 */
static const char *
xml_processtag( const char *p, xml *node, int *type )
{
	str tag;

	str_init( &tag );

	if ( *p=='!' ) {
		*type = XML_COMMENT;
		while ( *p && *p!='>' ) p++;
	}
	else if ( *p=='?' ) {
		*type = XML_DESCRIPTOR;
		p++; /* skip '?' */
		while ( *p && !strchr( " \t", *p ) && !xml_is_terminator(p,type) )
			str_addchar( &tag, *p++ );
		if ( *p==' ' || *p=='\t' )
			p = xml_processattrib( p, node, type );
	}
	else if ( *p=='/' ) {
		*type = XML_CLOSE;
		while ( *p && !strchr( " \t", *p ) && !xml_is_terminator(p,type) )
			str_addchar( &tag, *p++ );
		if ( *p==' ' || *p=='\t' ) 
			p = xml_processattrib( p, node, type );
	}
	else {
		*type = XML_OPEN;
		while ( *p && !strchr( " \t", *p ) && !xml_is_terminator(p,type) )
			str_addchar( &tag, *p++ );
		if ( *p==' ' || *p=='\t' ) 
			p = xml_processattrib( p, node, type );
	}
	while ( *p && *p!='>' ) p++;
	if ( *p=='>' ) p++;

	str_strcpy( &(node->tag), &tag );

	str_free( &tag );

	return p;
}

static void
xml_appendnode( xml *onode, xml *nnode )
{
	if ( !onode->down ) onode->down = nnode;
	else {
		xml *p = onode->down;
		while ( p->next ) p = p->next;
		p->next = nnode;
	}
}

const char *
xml_parse( const char *p, xml *onode )
{
	int type, is_style = 0;
	xml *nnode;

	while ( *p ) {

		/* retain white space for <style> tags in endnote xml */
		if ( str_cstr( &(onode->tag) ) &&
			!strcasecmp( str_cstr( &(onode->tag) ),"style") ) is_style=1;

		while ( *p && *p!='<' ) {
			if ( onode->value.len>0 || is_style || !is_ws( *p ) )
				str_addchar( &(onode->value), *p );
			p++;
		}

		if ( *p=='<' ) {
			nnode = xml_new();
			p = xml_processtag( p+1, nnode, &type );
			if ( type==XML_OPEN || type==XML_OPENCLOSE || type==XML_DESCRIPTOR ) {
				xml_appendnode( onode, nnode );
				if ( type==XML_OPEN )
					p = xml_parse( p, nnode );
			} else if ( type==XML_CLOSE ) {
				/*check to see if it's closing for this one*/
				xml_delete( nnode );
				goto out; /* assume it's right for now */
			} else {
				xml_delete( nnode );
			}
		}

	}
out:
	return p;
}

void
xml_draw( xml *node, int n )
{
	slist_index j;
	int i;

	if ( !node ) return;

	for ( i=0; i<n; ++i ) printf( "    " );

	printf("n=%d tag='%s' value='%s'\n", n, str_cstr( &(node->tag) ), str_cstr( &(node->value) ) );

	for ( j=0; j<node->attributes.n; ++j ) {
		for ( i=0; i<n; ++i ) printf( "    " );
		printf( "    attribute='%s' value='%s'\n",
			slist_cstr( &(node->attributes), j ),
			slist_cstr( &(node->attribute_values), j )
		);
	}

	if ( node->down ) xml_draw( node->down, n+1 );
	if ( node->next ) xml_draw( node->next, n );
}

char *
xml_find_start( char *buffer, char *tag )
{
	str starttag;
	char *p;

	str_initstrsc( &starttag, "<", tag, " ", NULL );

	p = strsearch( buffer, str_cstr( &starttag ) );
	if ( !p ) {
		starttag.data[ starttag.len-1 ] = '>';
		p = strsearch( buffer, str_cstr( &starttag ) );
	}

	str_free( &starttag );

	return p;
}

char *
xml_find_end( char *buffer, char *tag )
{
	str endtag;
	char *p;

	if ( xml_pns )
		str_initstrsc( &endtag, "</", xml_pns, ":", tag, ">", NULL );
	else
		str_initstrsc( &endtag, "</", tag, ">", NULL );

	p = strsearch( buffer, str_cstr( &endtag ) );
	if ( p && *p ) {
		if ( *p ) p++;  /* skip <random_tag></end> combo */
		while ( *p && *(p-1)!='>' ) p++;
	}

	str_free( &endtag );
	return p;
}

static int
xml_tag_matches_simple( xml* node, const char *tag )
{
	if ( node->tag.len!=strlen( tag ) ) return 0;
	if ( strcasecmp( str_cstr( &(node->tag) ), tag ) ) return 0;
	return 1;
}
static int
xml_tag_matches_pns( xml* node, const char *tag )
{
	int found = 0;
	str pnstag;

	str_initstrsc( &pnstag, xml_pns, ":", tag, NULL );
	if ( node->tag.len==pnstag.len &&
			!strcasecmp( str_cstr( &(node->tag) ), str_cstr( &pnstag ) ) )
		found = 1;
	str_free( &pnstag );

	return found;
}
int
xml_tag_matches( xml *node, const char *tag )
{
	if ( xml_pns ) return xml_tag_matches_pns   ( node, tag );
	else           return xml_tag_matches_simple( node, tag );
}

int
xml_tag_matches_has_value( xml *node, const char *tag )
{
	if ( xml_tag_matches( node, tag ) && xml_has_value( node ) ) return 1;
	return 0;
}

int
xml_has_attribute( xml *node, const char *attribute, const char *attribute_value )
{
	slist_index i;
	char *a, *v;

	for ( i=0; i<node->attributes.n; ++i ) {
		a = slist_cstr( &(node->attributes), i );
		v = slist_cstr( &(node->attribute_values), i );
		if ( !a || !v ) continue;
		if ( !strcasecmp( a, attribute ) && !strcasecmp( v, attribute_value ) )
			return 1;
	}

	return 0;
}

int
xml_tag_has_attribute( xml *node, const char *tag, const char *attribute, const char *attribute_value )
{
	if ( !xml_tag_matches( node, tag ) ) return 0;
	return xml_has_attribute( node, attribute, attribute_value );
}

str *
xml_attribute( xml *node, const char *attribute )
{
	slist_index n;

	n = slist_findc( &(node->attributes), attribute );
	if ( slist_wasnotfound( &(node->attributes), n ) ) return NULL;
	else return slist_str( &(node->attribute_values), n );
}

int
xml_has_value( xml *node )
{
	if ( node && str_has_value( &(node->value) ) ) return 1;
	return 0;
}

str *
xml_tag( xml *node )
{
	return &(node->tag);
}

char *
xml_tag_cstr( xml *node )
{
	return str_cstr( &(node->tag) );
}

str *
xml_value( xml *node )
{
	return &(node->value);
}

char *
xml_value_cstr( xml *node )
{
	return str_cstr( &(node->value) );
}