1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Simple XML (stream) parse tree handling code (Jabber/XMPP, mainly) * |
---|
5 | * * |
---|
6 | * Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
7 | * * |
---|
8 | * This library is free software; you can redistribute it and/or * |
---|
9 | * modify it under the terms of the GNU Lesser General Public * |
---|
10 | * License as published by the Free Software Foundation, version * |
---|
11 | * 2.1. * |
---|
12 | * * |
---|
13 | * This library is distributed in the hope that it will be useful, * |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
---|
16 | * Lesser General Public License for more details. * |
---|
17 | * * |
---|
18 | * You should have received a copy of the GNU Lesser General Public License * |
---|
19 | * along with this library; if not, write to the Free Software Foundation, * |
---|
20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
---|
21 | * * |
---|
22 | ****************************************************************************/ |
---|
23 | |
---|
24 | #include <glib.h> |
---|
25 | #include <string.h> |
---|
26 | #include <unistd.h> |
---|
27 | #include <ctype.h> |
---|
28 | #include <stdio.h> |
---|
29 | |
---|
30 | #include "xmltree.h" |
---|
31 | |
---|
32 | static void xt_start_element( GMarkupParseContext *ctx, const gchar *element_name, const gchar **attr_names, const gchar **attr_values, gpointer data, GError **error ) |
---|
33 | { |
---|
34 | struct xt_parser *xt = data; |
---|
35 | struct xt_node *node = g_new0( struct xt_node, 1 ), *nt; |
---|
36 | int i; |
---|
37 | |
---|
38 | node->parent = xt->cur; |
---|
39 | node->name = g_strdup( element_name ); |
---|
40 | |
---|
41 | /* First count the number of attributes */ |
---|
42 | for( i = 0; attr_names[i]; i ++ ); |
---|
43 | |
---|
44 | /* Then allocate a NULL-terminated array. */ |
---|
45 | node->attr = g_new0( struct xt_attr, i + 1 ); |
---|
46 | |
---|
47 | /* And fill it, saving one variable by starting at the end. */ |
---|
48 | for( i --; i >= 0; i -- ) |
---|
49 | { |
---|
50 | node->attr[i].key = g_strdup( attr_names[i] ); |
---|
51 | node->attr[i].value = g_strdup( attr_values[i] ); |
---|
52 | } |
---|
53 | |
---|
54 | /* Add it to the linked list of children nodes, if we have a current |
---|
55 | node yet. */ |
---|
56 | if( xt->cur ) |
---|
57 | { |
---|
58 | if( xt->cur->children ) |
---|
59 | { |
---|
60 | for( nt = xt->cur->children; nt->next; nt = nt->next ); |
---|
61 | nt->next = node; |
---|
62 | } |
---|
63 | else |
---|
64 | { |
---|
65 | xt->cur->children = node; |
---|
66 | } |
---|
67 | } |
---|
68 | else if( xt->root ) |
---|
69 | { |
---|
70 | /* ERROR situation: A second root-element??? */ |
---|
71 | } |
---|
72 | |
---|
73 | /* Now this node will be the new current node. */ |
---|
74 | xt->cur = node; |
---|
75 | /* And maybe this is the root? */ |
---|
76 | if( xt->root == NULL ) |
---|
77 | xt->root = node; |
---|
78 | } |
---|
79 | |
---|
80 | static void xt_text( GMarkupParseContext *ctx, const gchar *text, gsize text_len, gpointer data, GError **error ) |
---|
81 | { |
---|
82 | struct xt_parser *xt = data; |
---|
83 | struct xt_node *node = xt->cur; |
---|
84 | |
---|
85 | if( node == NULL ) |
---|
86 | return; |
---|
87 | |
---|
88 | /* FIXME: Does g_renew also OFFICIALLY accept NULL arguments? */ |
---|
89 | node->text = g_renew( char, node->text, node->text_len + text_len + 1 ); |
---|
90 | memcpy( node->text + node->text_len, text, text_len ); |
---|
91 | node->text_len += text_len; |
---|
92 | /* Zero termination is always nice to have. */ |
---|
93 | node->text[node->text_len] = 0; |
---|
94 | } |
---|
95 | |
---|
96 | static void xt_end_element( GMarkupParseContext *ctx, const gchar *element_name, gpointer data, GError **error ) |
---|
97 | { |
---|
98 | struct xt_parser *xt = data; |
---|
99 | |
---|
100 | xt->cur->flags |= XT_COMPLETE; |
---|
101 | xt->cur = xt->cur->parent; |
---|
102 | } |
---|
103 | |
---|
104 | GMarkupParser xt_parser_funcs = |
---|
105 | { |
---|
106 | xt_start_element, |
---|
107 | xt_end_element, |
---|
108 | xt_text, |
---|
109 | NULL, |
---|
110 | NULL |
---|
111 | }; |
---|
112 | |
---|
113 | struct xt_parser *xt_new( const struct xt_handler_entry *handlers, gpointer data ) |
---|
114 | { |
---|
115 | struct xt_parser *xt = g_new0( struct xt_parser, 1 ); |
---|
116 | |
---|
117 | xt->data = data; |
---|
118 | xt->handlers = handlers; |
---|
119 | xt_reset( xt ); |
---|
120 | |
---|
121 | return xt; |
---|
122 | } |
---|
123 | |
---|
124 | /* Reset the parser, flush everything we have so far. For example, we need |
---|
125 | this for XMPP when doing TLS/SASL to restart the stream. */ |
---|
126 | void xt_reset( struct xt_parser *xt ) |
---|
127 | { |
---|
128 | if( xt->parser ) |
---|
129 | g_markup_parse_context_free( xt->parser ); |
---|
130 | |
---|
131 | xt->parser = g_markup_parse_context_new( &xt_parser_funcs, 0, xt, NULL ); |
---|
132 | |
---|
133 | if( xt->root ) |
---|
134 | { |
---|
135 | xt_free_node( xt->root ); |
---|
136 | xt->root = NULL; |
---|
137 | xt->cur = NULL; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | /* Feed the parser, don't execute any handler. Returns -1 on errors, 0 on |
---|
142 | end-of-stream and 1 otherwise. */ |
---|
143 | int xt_feed( struct xt_parser *xt, const char *text, int text_len ) |
---|
144 | { |
---|
145 | if( !g_markup_parse_context_parse( xt->parser, text, text_len, &xt->gerr ) ) |
---|
146 | { |
---|
147 | return -1; |
---|
148 | } |
---|
149 | |
---|
150 | return !( xt->root && xt->root->flags & XT_COMPLETE ); |
---|
151 | } |
---|
152 | |
---|
153 | /* Find completed nodes and see if a handler has to be called. Passing |
---|
154 | a node isn't necessary if you want to start at the root, just pass |
---|
155 | NULL. This second argument is needed for recursive calls. */ |
---|
156 | int xt_handle( struct xt_parser *xt, struct xt_node *node, int depth ) |
---|
157 | { |
---|
158 | struct xt_node *c; |
---|
159 | xt_status st; |
---|
160 | int i; |
---|
161 | |
---|
162 | /* Just in case someone likes infinite loops... */ |
---|
163 | if( xt->root == NULL ) |
---|
164 | return 0; |
---|
165 | |
---|
166 | if( node == NULL ) |
---|
167 | return xt_handle( xt, xt->root, depth ); |
---|
168 | |
---|
169 | if( depth != 0 ) |
---|
170 | for( c = node->children; c; c = c->next ) |
---|
171 | if( !xt_handle( xt, c, depth > 0 ? depth - 1 : depth ) ) |
---|
172 | return 0; |
---|
173 | |
---|
174 | if( node->flags & XT_COMPLETE && !( node->flags & XT_SEEN ) ) |
---|
175 | { |
---|
176 | if( xt->handlers ) for( i = 0; xt->handlers[i].func; i ++ ) |
---|
177 | { |
---|
178 | /* This one is fun! \o/ */ |
---|
179 | |
---|
180 | /* If handler.name == NULL it means it should always match. */ |
---|
181 | if( ( xt->handlers[i].name == NULL || |
---|
182 | /* If it's not, compare. There should always be a name. */ |
---|
183 | g_strcasecmp( xt->handlers[i].name, node->name ) == 0 ) && |
---|
184 | /* If handler.parent == NULL, it's a match. */ |
---|
185 | ( xt->handlers[i].parent == NULL || |
---|
186 | /* If there's a parent node, see if the name matches. */ |
---|
187 | ( node->parent ? g_strcasecmp( xt->handlers[i].parent, node->parent->name ) == 0 : |
---|
188 | /* If there's no parent, the handler should mention <root> as a parent. */ |
---|
189 | strcmp( xt->handlers[i].parent, "<root>" ) == 0 ) ) ) |
---|
190 | { |
---|
191 | st = xt->handlers[i].func( node, xt->data ); |
---|
192 | |
---|
193 | if( st == XT_ABORT ) |
---|
194 | return 0; |
---|
195 | else if( st != XT_NEXT ) |
---|
196 | break; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | node->flags |= XT_SEEN; |
---|
201 | } |
---|
202 | |
---|
203 | return 1; |
---|
204 | } |
---|
205 | |
---|
206 | /* Garbage collection: Cleans up all nodes that are handled. Useful for |
---|
207 | streams because there's no reason to keep a complete packet history |
---|
208 | in memory. */ |
---|
209 | void xt_cleanup( struct xt_parser *xt, struct xt_node *node, int depth ) |
---|
210 | { |
---|
211 | struct xt_node *c, *prev; |
---|
212 | |
---|
213 | if( !xt || !xt->root ) |
---|
214 | return; |
---|
215 | |
---|
216 | if( node == NULL ) |
---|
217 | { |
---|
218 | xt_cleanup( xt, xt->root, depth ); |
---|
219 | return; |
---|
220 | } |
---|
221 | |
---|
222 | if( node->flags & XT_SEEN && node == xt->root ) |
---|
223 | { |
---|
224 | xt_free_node( xt->root ); |
---|
225 | xt->root = xt->cur = NULL; |
---|
226 | /* xt->cur should be NULL already, BTW... */ |
---|
227 | |
---|
228 | return; |
---|
229 | } |
---|
230 | |
---|
231 | /* c contains the current node, prev the previous node (or NULL). |
---|
232 | I admit, this one's pretty horrible. */ |
---|
233 | for( c = node->children, prev = NULL; c; prev = c, c = c ? c->next : node->children ) |
---|
234 | { |
---|
235 | if( c->flags & XT_SEEN ) |
---|
236 | { |
---|
237 | /* Remove the node from the linked list. */ |
---|
238 | if( prev ) |
---|
239 | prev->next = c->next; |
---|
240 | else |
---|
241 | node->children = c->next; |
---|
242 | |
---|
243 | xt_free_node( c ); |
---|
244 | |
---|
245 | /* Since the for loop wants to get c->next, make sure |
---|
246 | c points at something that exists (and that c->next |
---|
247 | will actually be the next item we should check). c |
---|
248 | can be NULL now, if we just removed the first item. |
---|
249 | That explains the ? thing in for(). */ |
---|
250 | c = prev; |
---|
251 | } |
---|
252 | else |
---|
253 | { |
---|
254 | /* This node can't be cleaned up yet, but maybe a |
---|
255 | subnode can. */ |
---|
256 | if( depth != 0 ) |
---|
257 | xt_cleanup( xt, c, depth > 0 ? depth - 1 : depth ); |
---|
258 | } |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | struct xt_node *xt_from_string( const char *in ) |
---|
263 | { |
---|
264 | struct xt_parser *parser; |
---|
265 | struct xt_node *ret; |
---|
266 | |
---|
267 | parser = xt_new( NULL, NULL ); |
---|
268 | xt_feed( parser, in, strlen( in ) ); |
---|
269 | ret = parser->root; |
---|
270 | parser->root = NULL; |
---|
271 | xt_free( parser ); |
---|
272 | |
---|
273 | return ret; |
---|
274 | } |
---|
275 | |
---|
276 | static void xt_to_string_real( struct xt_node *node, GString *str ) |
---|
277 | { |
---|
278 | char *buf; |
---|
279 | struct xt_node *c; |
---|
280 | int i; |
---|
281 | |
---|
282 | g_string_append_printf( str, "<%s", node->name ); |
---|
283 | |
---|
284 | for( i = 0; node->attr[i].key; i ++ ) |
---|
285 | { |
---|
286 | buf = g_markup_printf_escaped( " %s=\"%s\"", node->attr[i].key, node->attr[i].value ); |
---|
287 | g_string_append( str, buf ); |
---|
288 | g_free( buf ); |
---|
289 | } |
---|
290 | |
---|
291 | if( node->text == NULL && node->children == NULL ) |
---|
292 | { |
---|
293 | g_string_append( str, "/>" ); |
---|
294 | return; |
---|
295 | } |
---|
296 | |
---|
297 | g_string_append( str, ">" ); |
---|
298 | if( node->text_len > 0 ) |
---|
299 | { |
---|
300 | buf = g_markup_escape_text( node->text, node->text_len ); |
---|
301 | g_string_append( str, buf ); |
---|
302 | g_free( buf ); |
---|
303 | } |
---|
304 | |
---|
305 | for( c = node->children; c; c = c->next ) |
---|
306 | xt_to_string_real( c, str ); |
---|
307 | |
---|
308 | g_string_append_printf( str, "</%s>", node->name ); |
---|
309 | } |
---|
310 | |
---|
311 | char *xt_to_string( struct xt_node *node ) |
---|
312 | { |
---|
313 | GString *ret; |
---|
314 | char *real; |
---|
315 | |
---|
316 | ret = g_string_new( "" ); |
---|
317 | xt_to_string_real( node, ret ); |
---|
318 | |
---|
319 | real = ret->str; |
---|
320 | g_string_free( ret, FALSE ); |
---|
321 | |
---|
322 | return real; |
---|
323 | } |
---|
324 | |
---|
325 | #ifdef DEBUG |
---|
326 | void xt_print( struct xt_node *node ) |
---|
327 | { |
---|
328 | int i; |
---|
329 | struct xt_node *c; |
---|
330 | |
---|
331 | /* Indentation */ |
---|
332 | for( c = node; c->parent; c = c->parent ) |
---|
333 | printf( " " ); |
---|
334 | |
---|
335 | /* Start the tag */ |
---|
336 | printf( "<%s", node->name ); |
---|
337 | |
---|
338 | /* Print the attributes */ |
---|
339 | for( i = 0; node->attr[i].key; i ++ ) |
---|
340 | { |
---|
341 | char *v = g_markup_escape_text( node->attr[i].value, -1 ); |
---|
342 | printf( " %s=\"%s\"", node->attr[i].key, v ); |
---|
343 | g_free( v ); |
---|
344 | } |
---|
345 | |
---|
346 | /* /> in case there's really *nothing* inside this tag, otherwise |
---|
347 | just >. */ |
---|
348 | /* If this tag doesn't have any content at all... */ |
---|
349 | if( node->text == NULL && node->children == NULL ) |
---|
350 | { |
---|
351 | printf( "/>\n" ); |
---|
352 | return; |
---|
353 | /* Then we're finished! */ |
---|
354 | } |
---|
355 | |
---|
356 | /* Otherwise... */ |
---|
357 | printf( ">" ); |
---|
358 | |
---|
359 | /* Only print the text if it contains more than whitespace (TEST). */ |
---|
360 | if( node->text_len > 0 ) |
---|
361 | { |
---|
362 | for( i = 0; node->text[i] && isspace( node->text[i] ); i ++ ); |
---|
363 | if( node->text[i] ) |
---|
364 | { |
---|
365 | char *v = g_markup_escape_text( node->text, -1 ); |
---|
366 | printf( "%s", v ); |
---|
367 | g_free( v ); |
---|
368 | } |
---|
369 | } |
---|
370 | |
---|
371 | if( node->children ) |
---|
372 | printf( "\n" ); |
---|
373 | |
---|
374 | for( c = node->children; c; c = c->next ) |
---|
375 | xt_print( c ); |
---|
376 | |
---|
377 | if( node->children ) |
---|
378 | for( c = node; c->parent; c = c->parent ) |
---|
379 | printf( " " ); |
---|
380 | |
---|
381 | /* Non-empty tag is now finished. */ |
---|
382 | printf( "</%s>\n", node->name ); |
---|
383 | } |
---|
384 | #endif |
---|
385 | |
---|
386 | struct xt_node *xt_dup( struct xt_node *node ) |
---|
387 | { |
---|
388 | struct xt_node *dup = g_new0( struct xt_node, 1 ); |
---|
389 | struct xt_node *c, *dc = NULL; |
---|
390 | int i; |
---|
391 | |
---|
392 | /* Let's NOT copy the parent element here BTW! Only do it for children. */ |
---|
393 | |
---|
394 | dup->name = g_strdup( node->name ); |
---|
395 | dup->flags = node->flags; |
---|
396 | if( node->text ) |
---|
397 | { |
---|
398 | dup->text = g_memdup( node->text, node->text_len + 1 ); |
---|
399 | dup->text_len = node->text_len; |
---|
400 | } |
---|
401 | |
---|
402 | /* Count the number of attributes and allocate the new array. */ |
---|
403 | for( i = 0; node->attr[i].key; i ++ ); |
---|
404 | dup->attr = g_new0( struct xt_attr, i + 1 ); |
---|
405 | |
---|
406 | /* Copy them all! */ |
---|
407 | for( i --; i >= 0; i -- ) |
---|
408 | { |
---|
409 | dup->attr[i].key = g_strdup( node->attr[i].key ); |
---|
410 | dup->attr[i].value = g_strdup( node->attr[i].value ); |
---|
411 | } |
---|
412 | |
---|
413 | /* This nice mysterious loop takes care of the children. */ |
---|
414 | for( c = node->children; c; c = c->next ) |
---|
415 | { |
---|
416 | if( dc == NULL ) |
---|
417 | dc = dup->children = xt_dup( c ); |
---|
418 | else |
---|
419 | dc = ( dc->next = xt_dup( c ) ); |
---|
420 | |
---|
421 | dc->parent = dup; |
---|
422 | } |
---|
423 | |
---|
424 | return dup; |
---|
425 | } |
---|
426 | |
---|
427 | /* Frees a node. This doesn't clean up references to itself from parents! */ |
---|
428 | void xt_free_node( struct xt_node *node ) |
---|
429 | { |
---|
430 | int i; |
---|
431 | |
---|
432 | if( !node ) |
---|
433 | return; |
---|
434 | |
---|
435 | g_free( node->name ); |
---|
436 | g_free( node->text ); |
---|
437 | |
---|
438 | for( i = 0; node->attr[i].key; i ++ ) |
---|
439 | { |
---|
440 | g_free( node->attr[i].key ); |
---|
441 | g_free( node->attr[i].value ); |
---|
442 | } |
---|
443 | g_free( node->attr ); |
---|
444 | |
---|
445 | while( node->children ) |
---|
446 | { |
---|
447 | struct xt_node *next = node->children->next; |
---|
448 | |
---|
449 | xt_free_node( node->children ); |
---|
450 | node->children = next; |
---|
451 | } |
---|
452 | |
---|
453 | g_free( node ); |
---|
454 | } |
---|
455 | |
---|
456 | void xt_free( struct xt_parser *xt ) |
---|
457 | { |
---|
458 | if( !xt ) |
---|
459 | return; |
---|
460 | |
---|
461 | if( xt->root ) |
---|
462 | xt_free_node( xt->root ); |
---|
463 | |
---|
464 | g_markup_parse_context_free( xt->parser ); |
---|
465 | |
---|
466 | g_free( xt ); |
---|
467 | } |
---|
468 | |
---|
469 | /* To find a node's child with a specific name, pass the node's children |
---|
470 | list, not the node itself! The reason you have to do this by hand: So |
---|
471 | that you can also use this function as a find-next. */ |
---|
472 | struct xt_node *xt_find_node( struct xt_node *node, const char *name ) |
---|
473 | { |
---|
474 | while( node ) |
---|
475 | { |
---|
476 | char *colon; |
---|
477 | |
---|
478 | if( g_strcasecmp( node->name, name ) == 0 || |
---|
479 | ( ( colon = strchr( node->name, ':' ) ) && |
---|
480 | g_strcasecmp( colon + 1, name ) == 0 ) ) |
---|
481 | break; |
---|
482 | |
---|
483 | node = node->next; |
---|
484 | } |
---|
485 | |
---|
486 | return node; |
---|
487 | } |
---|
488 | |
---|
489 | /* More advanced than the one above, understands something like |
---|
490 | ../foo/bar to find a subnode bar of a node foo which is a child |
---|
491 | of node's parent. Pass the node directly, not its list of children. */ |
---|
492 | struct xt_node *xt_find_path( struct xt_node *node, const char *name ) |
---|
493 | { |
---|
494 | while( name && *name && node ) |
---|
495 | { |
---|
496 | char *colon, *slash; |
---|
497 | int n; |
---|
498 | |
---|
499 | if( ( slash = strchr( name, '/' ) ) ) |
---|
500 | n = slash - name; |
---|
501 | else |
---|
502 | n = strlen( name ); |
---|
503 | |
---|
504 | if( strncmp( name, "..", n ) == 0 ) |
---|
505 | { |
---|
506 | node = node->parent; |
---|
507 | } |
---|
508 | else |
---|
509 | { |
---|
510 | node = node->children; |
---|
511 | |
---|
512 | while( node ) |
---|
513 | { |
---|
514 | if( g_strncasecmp( node->name, name, n ) == 0 || |
---|
515 | ( ( colon = strchr( node->name, ':' ) ) && |
---|
516 | g_strncasecmp( colon + 1, name, n ) == 0 ) ) |
---|
517 | break; |
---|
518 | |
---|
519 | node = node->next; |
---|
520 | } |
---|
521 | } |
---|
522 | |
---|
523 | name = slash ? slash + 1 : NULL; |
---|
524 | } |
---|
525 | |
---|
526 | return node; |
---|
527 | } |
---|
528 | |
---|
529 | char *xt_find_attr( struct xt_node *node, const char *key ) |
---|
530 | { |
---|
531 | int i; |
---|
532 | char *colon; |
---|
533 | |
---|
534 | if( !node ) |
---|
535 | return NULL; |
---|
536 | |
---|
537 | for( i = 0; node->attr[i].key; i ++ ) |
---|
538 | if( g_strcasecmp( node->attr[i].key, key ) == 0 ) |
---|
539 | break; |
---|
540 | |
---|
541 | /* This is an awful hack that only takes care of namespace prefixes |
---|
542 | inside a tag. Since IMHO excessive namespace usage in XMPP is |
---|
543 | massive overkill anyway (this code exists for almost four years |
---|
544 | now and never really missed it): Meh. */ |
---|
545 | if( !node->attr[i].key && strcmp( key, "xmlns" ) == 0 && |
---|
546 | ( colon = strchr( node->name, ':' ) ) ) |
---|
547 | { |
---|
548 | *colon = '\0'; |
---|
549 | for( i = 0; node->attr[i].key; i ++ ) |
---|
550 | if( strncmp( node->attr[i].key, "xmlns:", 6 ) == 0 && |
---|
551 | strcmp( node->attr[i].key + 6, node->name ) == 0 ) |
---|
552 | break; |
---|
553 | *colon = ':'; |
---|
554 | } |
---|
555 | |
---|
556 | return node->attr[i].value; |
---|
557 | } |
---|
558 | |
---|
559 | /* Strip a few non-printable characters that aren't allowed in XML streams |
---|
560 | (and upset some XMPP servers for example). */ |
---|
561 | void xt_strip_text( char *in ) |
---|
562 | { |
---|
563 | char *out = in; |
---|
564 | static const char nonprint[32] = { |
---|
565 | 0, 0, 0, 0, 0, 0, 0, 0, /* 0..7 */ |
---|
566 | 0, 1, 1, 0, 0, 1, 0, 0, /* 9 (tab), 10 (\n), 13 (\r) */ |
---|
567 | }; |
---|
568 | |
---|
569 | if( !in ) |
---|
570 | return; |
---|
571 | |
---|
572 | while( *in ) |
---|
573 | { |
---|
574 | if( (unsigned int) *in >= ' ' || nonprint[(unsigned int) *in] ) |
---|
575 | *out ++ = *in; |
---|
576 | in ++; |
---|
577 | } |
---|
578 | *out = *in; |
---|
579 | } |
---|
580 | |
---|
581 | struct xt_node *xt_new_node( char *name, const char *text, struct xt_node *children ) |
---|
582 | { |
---|
583 | struct xt_node *node, *c; |
---|
584 | |
---|
585 | node = g_new0( struct xt_node, 1 ); |
---|
586 | node->name = g_strdup( name ); |
---|
587 | node->children = children; |
---|
588 | node->attr = g_new0( struct xt_attr, 1 ); |
---|
589 | |
---|
590 | if( text ) |
---|
591 | { |
---|
592 | node->text = g_strdup( text ); |
---|
593 | xt_strip_text( node->text ); |
---|
594 | node->text_len = strlen( node->text ); |
---|
595 | } |
---|
596 | |
---|
597 | for( c = children; c; c = c->next ) |
---|
598 | { |
---|
599 | if( c->parent != NULL ) |
---|
600 | { |
---|
601 | /* ERROR CONDITION: They seem to have a parent already??? */ |
---|
602 | } |
---|
603 | |
---|
604 | c->parent = node; |
---|
605 | } |
---|
606 | |
---|
607 | return node; |
---|
608 | } |
---|
609 | |
---|
610 | void xt_add_child( struct xt_node *parent, struct xt_node *child ) |
---|
611 | { |
---|
612 | struct xt_node *node; |
---|
613 | |
---|
614 | /* This function can actually be used to add more than one child, so |
---|
615 | do handle this properly. */ |
---|
616 | for( node = child; node; node = node->next ) |
---|
617 | { |
---|
618 | if( node->parent != NULL ) |
---|
619 | { |
---|
620 | /* ERROR CONDITION: They seem to have a parent already??? */ |
---|
621 | } |
---|
622 | |
---|
623 | node->parent = parent; |
---|
624 | } |
---|
625 | |
---|
626 | if( parent->children == NULL ) |
---|
627 | { |
---|
628 | parent->children = child; |
---|
629 | } |
---|
630 | else |
---|
631 | { |
---|
632 | for( node = parent->children; node->next; node = node->next ); |
---|
633 | node->next = child; |
---|
634 | } |
---|
635 | } |
---|
636 | |
---|
637 | /* Same, but at the beginning. */ |
---|
638 | void xt_insert_child( struct xt_node *parent, struct xt_node *child ) |
---|
639 | { |
---|
640 | struct xt_node *node, *last = NULL; |
---|
641 | |
---|
642 | if( child == NULL ) |
---|
643 | return; /* BUG */ |
---|
644 | |
---|
645 | for( node = child; node; node = node->next ) |
---|
646 | { |
---|
647 | if( node->parent != NULL ) |
---|
648 | { |
---|
649 | /* ERROR CONDITION: They seem to have a parent already??? */ |
---|
650 | } |
---|
651 | |
---|
652 | node->parent = parent; |
---|
653 | last = node; |
---|
654 | } |
---|
655 | |
---|
656 | last->next = parent->children; |
---|
657 | parent->children = child; |
---|
658 | } |
---|
659 | |
---|
660 | void xt_add_attr( struct xt_node *node, const char *key, const char *value ) |
---|
661 | { |
---|
662 | int i; |
---|
663 | |
---|
664 | /* Now actually it'd be nice if we can also change existing attributes |
---|
665 | (which actually means this function doesn't have the right name). |
---|
666 | So let's find out if we have this attribute already... */ |
---|
667 | for( i = 0; node->attr[i].key; i ++ ) |
---|
668 | if( strcmp( node->attr[i].key, key ) == 0 ) |
---|
669 | break; |
---|
670 | |
---|
671 | if( node->attr[i].key == NULL ) |
---|
672 | { |
---|
673 | /* If not, allocate space for a new attribute. */ |
---|
674 | node->attr = g_renew( struct xt_attr, node->attr, i + 2 ); |
---|
675 | node->attr[i].key = g_strdup( key ); |
---|
676 | node->attr[i+1].key = NULL; |
---|
677 | } |
---|
678 | else |
---|
679 | { |
---|
680 | /* Otherwise, free the old value before setting the new one. */ |
---|
681 | g_free( node->attr[i].value ); |
---|
682 | } |
---|
683 | |
---|
684 | node->attr[i].value = g_strdup( value ); |
---|
685 | } |
---|
686 | |
---|
687 | int xt_remove_attr( struct xt_node *node, const char *key ) |
---|
688 | { |
---|
689 | int i, last; |
---|
690 | |
---|
691 | for( i = 0; node->attr[i].key; i ++ ) |
---|
692 | if( strcmp( node->attr[i].key, key ) == 0 ) |
---|
693 | break; |
---|
694 | |
---|
695 | /* If we didn't find the attribute... */ |
---|
696 | if( node->attr[i].key == NULL ) |
---|
697 | return 0; |
---|
698 | |
---|
699 | g_free( node->attr[i].key ); |
---|
700 | g_free( node->attr[i].value ); |
---|
701 | |
---|
702 | /* If it's the last, this is easy: */ |
---|
703 | if( node->attr[i+1].key == NULL ) |
---|
704 | { |
---|
705 | node->attr[i].key = node->attr[i].value = NULL; |
---|
706 | } |
---|
707 | else /* It's also pretty easy, actually. */ |
---|
708 | { |
---|
709 | /* Find the last item. */ |
---|
710 | for( last = i + 1; node->attr[last+1].key; last ++ ); |
---|
711 | |
---|
712 | node->attr[i] = node->attr[last]; |
---|
713 | node->attr[last].key = NULL; |
---|
714 | node->attr[last].value = NULL; |
---|
715 | } |
---|
716 | |
---|
717 | /* Let's not bother with reallocating memory here. It takes time and |
---|
718 | most packets don't stay in memory for long anyway. */ |
---|
719 | |
---|
720 | return 1; |
---|
721 | } |
---|