save crash log button works; some other minor tweaks
[claws.git] / src / xml.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #include <glib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <ctype.h>
24
25 #include "xml.h"
26 #include "main.h"
27 #include "utils.h"
28
29 #define SPARSE_MEMORY
30 /* if this is defined all attr.names and tag.names are stored
31  * in a hash table */
32 #if defined(SPARSE_MEMORY)
33 #include "stringtable.h" 
34
35 static StringTable *xml_string_table;
36
37 static void xml_string_table_create(void)
38 {
39         if (xml_string_table == NULL)
40                 xml_string_table = string_table_new();
41 }
42 #define XML_STRING_ADD(str) \
43         string_table_insert_string(xml_string_table, (str))
44 #define XML_STRING_FREE(str) \
45         string_table_free_string(xml_string_table, (str))
46
47 #define XML_STRING_TABLE_CREATE() \
48         xml_string_table_create()
49
50 #else /* !SPARSE_MEMORY */
51
52 #define XML_STRING_ADD(str) \
53         g_strdup(str)
54 #define XML_STRING_FREE(str) \
55         g_free(str)
56
57 #define XML_STRING_TABLE_CREATE()
58
59 #endif /* SPARSE_MEMORY */
60
61 static void xml_free_tag        (XMLTag         *tag);
62 static gint xml_get_parenthesis (XMLFile        *file,
63                                  gchar          *buf,
64                                  gint            len);
65
66 XMLFile *xml_open_file(const gchar *path)
67 {
68         XMLFile *newfile;
69
70         g_return_val_if_fail(path != NULL, NULL);
71
72         XML_STRING_TABLE_CREATE();
73
74         newfile = g_new(XMLFile, 1);
75
76         newfile->fp = fopen(path, "rb");
77         if (!newfile->fp) {
78                 g_free(newfile);
79                 return NULL;
80         }
81
82         newfile->buf = g_string_new(NULL);
83         newfile->bufp = newfile->buf->str;
84
85         newfile->dtd = NULL;
86         newfile->tag_stack = NULL;
87         newfile->level = 0;
88         newfile->is_empty_element = FALSE;
89
90         return newfile;
91 }
92
93 void xml_close_file(XMLFile *file)
94 {
95         g_return_if_fail(file != NULL);
96
97         if (file->fp) fclose(file->fp);
98
99         g_string_free(file->buf, TRUE);
100
101         g_free(file->dtd);
102
103         while (file->tag_stack != NULL)
104                 xml_pop_tag(file);
105
106         g_free(file);
107 }
108
109 static GNode *xml_build_tree(XMLFile *file, GNode *parent, guint level)
110 {
111         GNode *node = NULL;
112         XMLNode *xmlnode;
113         XMLTag *tag;
114
115         while (xml_parse_next_tag(file) == 0) {
116                 if (file->level < level) break;
117                 if (file->level == level) {
118                         g_warning("xml_build_tree(): Parse error\n");
119                         break;
120                 }
121
122                 tag = xml_get_current_tag(file);
123                 if (!tag) break;
124                 xmlnode = g_new(XMLNode, 1);
125                 xmlnode->tag = xml_copy_tag(tag);
126                 xmlnode->element = xml_get_element(file);
127                 if (!parent)
128                         node = g_node_new(xmlnode);
129                 else
130                         node = g_node_append_data(parent, xmlnode);
131
132                 xml_build_tree(file, node, file->level);
133                 if (file->level == 0) break;
134         }
135
136         return node;
137 }
138
139 GNode *xml_parse_file(const gchar *path)
140 {
141         XMLFile *file;
142         GNode *node;
143
144         file = xml_open_file(path);
145         g_return_val_if_fail(file != NULL, NULL);
146
147         xml_get_dtd(file);
148
149         node = xml_build_tree(file, NULL, file->level);
150
151         xml_close_file(file);
152
153 #if defined(SPARSE_MEMORY)
154         if (debug_mode)
155                 string_table_get_stats(xml_string_table);
156 #endif
157
158         return node;
159 }
160
161 gint xml_get_dtd(XMLFile *file)
162 {
163         gchar buf[XMLBUFSIZE];
164         gchar *bufp = buf;
165
166         if (xml_get_parenthesis(file, buf, sizeof(buf)) < 0) return -1;
167
168         if ((*bufp++ == '?') &&
169             (bufp = strcasestr(bufp, "xml")) &&
170             (bufp = strcasestr(bufp + 3, "version")) &&
171             (bufp = strchr(bufp + 7, '?')))
172                 file->dtd = g_strdup(buf);
173         else {
174                 g_warning("Can't get xml dtd\n");
175                 return -1;
176         }
177
178         return 0;
179 }
180
181 gint xml_parse_next_tag(XMLFile *file)
182 {
183         gchar buf[XMLBUFSIZE];
184         gchar *bufp = buf;
185         XMLTag *tag;
186         gint len;
187
188         if (file->is_empty_element == TRUE) {
189                 file->is_empty_element = FALSE;
190                 xml_pop_tag(file);
191                 return 0;
192         }
193
194         if (xml_get_parenthesis(file, buf, sizeof(buf)) < 0) {
195                 g_warning("xml_parse_next_tag(): Can't parse next tag\n");
196                 return -1;
197         }
198
199         /* end-tag */
200         if (buf[0] == '/') {
201                 if (strcmp(xml_get_current_tag(file)->tag, buf + 1) != 0) {
202                         g_warning("xml_parse_next_tag(): Tag name mismatch: %s\n", buf);
203                         return -1;
204                 }
205                 xml_pop_tag(file);
206                 return 0;
207         }
208
209         tag = g_new0(XMLTag, 1);
210         xml_push_tag(file, tag);
211
212         len = strlen(buf);
213         if (len > 0 && buf[len - 1] == '/') {
214                 file->is_empty_element = TRUE;
215                 buf[len - 1] = '\0';
216                 g_strchomp(buf);
217         }
218         if (strlen(buf) == 0) {
219                 g_warning("xml_parse_next_tag(): Tag name is empty\n");
220                 return -1;
221         }
222
223         while (*bufp != '\0' && !isspace(*bufp)) bufp++;
224         if (*bufp == '\0') {
225                 tag->tag = XML_STRING_ADD(buf);
226                 return 0;
227         } else {
228                 *bufp++ = '\0';
229                 tag->tag = XML_STRING_ADD(buf);
230         }
231
232         /* parse attributes ( name=value ) */
233         while (*bufp) {
234                 XMLAttr *attr;
235                 gchar *attr_name;
236                 gchar *attr_value;
237                 gchar *p;
238                 gchar quote;
239
240                 while (isspace(*bufp)) bufp++;
241                 attr_name = bufp;
242                 if ((p = strchr(attr_name, '=')) == NULL) {
243                         g_warning("xml_parse_next_tag(): Syntax error in tag\n");
244                         return -1;
245                 }
246                 bufp = p;
247                 *bufp++ = '\0';
248                 while (isspace(*bufp)) bufp++;
249
250                 if (*bufp != '"' && *bufp != '\'') {
251                         g_warning("xml_parse_next_tag(): Syntax error in tag\n");
252                         return -1;
253                 }
254                 quote = *bufp;
255                 bufp++;
256                 attr_value = bufp;
257                 if ((p = strchr(attr_value, quote)) == NULL) {
258                         g_warning("xml_parse_next_tag(): Syntax error in tag\n");
259                         return -1;
260                 }
261                 bufp = p;
262                 *bufp++ = '\0';
263
264                 g_strchomp(attr_name);
265                 xml_unescape_str(attr_value);
266
267                 attr = g_new(XMLAttr, 1);
268                 attr->name  = XML_STRING_ADD(attr_name);
269                 attr->value = g_strdup(attr_value);
270                 tag->attr   = g_list_append(tag->attr, attr);
271         }
272
273         return 0;
274 }
275
276 void xml_push_tag(XMLFile *file, XMLTag *tag)
277 {
278         g_return_if_fail(tag != NULL);
279
280         file->tag_stack = g_list_prepend(file->tag_stack, tag);
281         file->level++;
282 }
283
284 void xml_pop_tag(XMLFile *file)
285 {
286         XMLTag *tag;
287
288         if (!file->tag_stack) return;
289
290         tag = (XMLTag *)file->tag_stack->data;
291
292         xml_free_tag(tag);
293         file->tag_stack = g_list_remove(file->tag_stack, tag);
294         file->level--;
295 }
296
297 XMLTag *xml_get_current_tag(XMLFile *file)
298 {
299         if (file->tag_stack)
300                 return (XMLTag *)file->tag_stack->data;
301         else
302                 return NULL;
303 }
304
305 GList *xml_get_current_tag_attr(XMLFile *file)
306 {
307         XMLTag *tag;
308
309         tag = xml_get_current_tag(file);
310         if (!tag) return NULL;
311
312         return tag->attr;
313 }
314
315 gchar *xml_get_element(XMLFile *file)
316 {
317         gchar *str;
318         gchar *end;
319
320         while ((end = strchr(file->bufp, '<')) == NULL)
321                 if (xml_read_line(file) < 0) return NULL;
322
323         if (end == file->bufp)
324                 return NULL;
325
326         str = g_strndup(file->bufp, end - file->bufp);
327         /* this is not XML1.0 strict */
328         g_strstrip(str);
329         xml_unescape_str(str);
330
331         file->bufp = end;
332         xml_truncate_buf(file);
333
334         if (str[0] == '\0') {
335                 g_free(str);
336                 return NULL;
337         }
338
339         return str;
340 }
341
342 gint xml_read_line(XMLFile *file)
343 {
344         gchar buf[XMLBUFSIZE];
345         gint index;
346
347         if (fgets(buf, sizeof(buf), file->fp) == NULL)
348                 return -1;
349
350         index = file->bufp - file->buf->str;
351
352         g_string_append(file->buf, buf);
353
354         file->bufp = file->buf->str + index;
355
356         return 0;
357 }
358
359 void xml_truncate_buf(XMLFile *file)
360 {
361         gint len;
362
363         len = file->bufp - file->buf->str;
364         if (len > 0) {
365                 g_string_erase(file->buf, 0, len);
366                 file->bufp = file->buf->str;
367         }
368 }
369
370 gboolean xml_compare_tag(XMLFile *file, const gchar *name)
371 {
372         XMLTag *tag;
373
374         tag = xml_get_current_tag(file);
375
376         if (tag && strcmp(tag->tag, name) == 0)
377                 return TRUE;
378         else
379                 return FALSE;
380 }
381
382 XMLTag *xml_copy_tag(XMLTag *tag)
383 {
384         XMLTag *new_tag;
385         XMLAttr *attr;
386         GList *list;
387
388         new_tag = g_new(XMLTag, 1);
389         new_tag->tag = XML_STRING_ADD(tag->tag);
390         new_tag->attr = NULL;
391         for (list = tag->attr; list != NULL; list = list->next) {
392                 attr = xml_copy_attr((XMLAttr *)list->data);
393                 new_tag->attr = g_list_append(new_tag->attr, attr);
394         }
395
396         return new_tag;
397 }
398
399 XMLAttr *xml_copy_attr(XMLAttr *attr)
400 {
401         XMLAttr *new_attr;
402
403         new_attr = g_new(XMLAttr, 1);
404         new_attr->name  = XML_STRING_ADD(attr->name);
405         new_attr->value = g_strdup(attr->value);
406
407         return new_attr;
408 }
409
410 gint xml_unescape_str(gchar *str)
411 {
412         gchar *start;
413         gchar *end;
414         gchar *p = str;
415         gchar *esc_str;
416         gchar ch;
417         gint len;
418
419         while ((start = strchr(p, '&')) != NULL) {
420                 if ((end = strchr(start + 1, ';')) == NULL) {
421                         g_warning("Unescaped `&' appeared\n");
422                         p = start + 1;
423                         continue;
424                 }
425                 len = end - start + 1;
426                 if (len < 3) {
427                         p = end + 1;
428                         continue;
429                 }
430
431                 Xstrndup_a(esc_str, start, len, return -1);
432                 if (!strcmp(esc_str, "&lt;"))
433                         ch = '<';
434                 else if (!strcmp(esc_str, "&gt;"))
435                         ch = '>';
436                 else if (!strcmp(esc_str, "&amp;"))
437                         ch = '&';
438                 else if (!strcmp(esc_str, "&apos;"))
439                         ch = '\'';
440                 else if (!strcmp(esc_str, "&quot;"))
441                         ch = '\"';
442                 else {
443                         p = end + 1;
444                         continue;
445                 }
446
447                 *start = ch;
448                 memmove(start + 1, end + 1, strlen(end + 1) + 1);
449                 p = start + 1;
450         }
451
452         return 0;
453 }
454
455 gint xml_file_put_escape_str(FILE *fp, const gchar *str)
456 {
457         const gchar *p;
458
459         g_return_val_if_fail(fp != NULL, -1);
460
461         if (!str) return 0;
462
463         for (p = str; *p != '\0'; p++) {
464                 switch (*p) {
465                 case '<':
466                         fputs("&lt;", fp);
467                         break;
468                 case '>':
469                         fputs("&gt;", fp);
470                         break;
471                 case '&':
472                         fputs("&amp;", fp);
473                         break;
474                 case '\'':
475                         fputs("&apos;", fp);
476                         break;
477                 case '\"':
478                         fputs("&quot;", fp);
479                         break;
480                 default:
481                         fputc(*p, fp);
482                 }
483         }
484
485         return 0;
486 }
487
488 void xml_free_node(XMLNode *node)
489 {
490         if (!node) return;
491
492         xml_free_tag(node->tag);
493         g_free(node->element);
494         g_free(node);
495 }
496
497 static gboolean xml_free_func(GNode *node, gpointer data)
498 {
499         XMLNode *xmlnode = node->data;
500
501         xml_free_node(xmlnode);
502         return FALSE;
503 }
504
505 void xml_free_tree(GNode *node)
506 {
507         g_return_if_fail(node != NULL);
508
509         g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, xml_free_func,
510                         NULL);
511
512         g_node_destroy(node);
513 }
514
515 static void xml_free_tag(XMLTag *tag)
516 {
517         if (!tag) return;
518
519         XML_STRING_FREE(tag->tag);
520         while (tag->attr != NULL) {
521                 XMLAttr *attr = (XMLAttr *)tag->attr->data;
522                 XML_STRING_FREE(attr->name);
523                 g_free(attr->value);
524                 g_free(attr);
525                 tag->attr = g_list_remove(tag->attr, tag->attr->data);
526         }
527         g_free(tag);
528 }
529
530 static gint xml_get_parenthesis(XMLFile *file, gchar *buf, gint len)
531 {
532         gchar *start;
533         gchar *end;
534
535         buf[0] = '\0';
536
537         while ((start = strchr(file->bufp, '<')) == NULL)
538                 if (xml_read_line(file) < 0) return -1;
539
540         start++;
541         file->bufp = start;
542
543         while ((end = strchr(file->bufp, '>')) == NULL)
544                 if (xml_read_line(file) < 0) return -1;
545
546         strncpy2(buf, file->bufp, MIN(end - file->bufp + 1, len));
547         g_strstrip(buf);
548         file->bufp = end + 1;
549         xml_truncate_buf(file);
550
551         return 0;
552 }