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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
|
#include <config.h>
#include "gdb3html.h"
#include "toc-elements.h"
#include "sect-elements.h"
#include "sect-preparse.h"
#include "gnome.h"
#if 0
#define ERROR_OUTPUT
#endif
/* Generic Function. Used by toc_elements and sect_preparse */
void
sect1_start_element (Context *context,
const char *name,
const xmlChar **atrs)
{
char **atrs_ptr;
atrs_ptr = (char **) atrs;
while (atrs_ptr && *atrs_ptr) {
if (g_strcasecmp (*atrs_ptr, "id") == 0) {
atrs_ptr++;
context->sect1id_stack =
g_list_prepend (context->sect1id_stack, g_strdup (*atrs_ptr));
break;
}
atrs_ptr += 2;
}
}
/* Generic functions. Used by both elements and toc_elements */
void
article_start_element (Context *context, const gchar *name, const xmlChar **atrs)
{
g_print ("<HTML>\n");
}
void
article_end_element (Context *context, const gchar *name)
{
/* This should now be covered by the print_footer function */
/* g_print ("</BODY>\n</HTML>\n"); */
}
void
ulink_start_element (Context *context, const gchar *name, const xmlChar **atrs)
{
gint url_found = FALSE;
GSList *list;
gchar **atrs_ptr;
ElementIndex index;
list = g_slist_prepend (NULL, GINT_TO_POINTER (FOOTNOTE));
index = find_first_parent (context, list);
atrs_ptr = (gchar **) atrs;
while (atrs_ptr && *atrs_ptr) {
if (!g_strcasecmp (*atrs_ptr, "url")) {
atrs_ptr++;
if (index == UNDEFINED) {
g_print ("<A HREF=\"%s\">", *atrs_ptr);
} else {
GString *footnote;
list = g_slist_last (context->footnotes);
footnote = (GString *) list->data;
g_string_append (footnote, "<A HREF=\"");
g_string_append (footnote, *atrs_ptr);
g_string_append (footnote, "\">");
}
url_found = TRUE;
break;
}
atrs_ptr += 2;
}
if (!url_found)
g_print ("<A>");
}
void
ulink_end_element (Context *context, const gchar *name)
{
GSList *list;
ElementIndex index;
list = g_slist_prepend (NULL, GINT_TO_POINTER (FOOTNOTE));
index = find_first_parent (context, list);
if (index == UNDEFINED) {
g_print ("</A>\n");
} else {
GString *footnote;
list = g_slist_last (context->footnotes);
footnote = (GString *) list->data;
g_string_append (footnote, "</A>\n");
}
}
void
html_em_start_element (Context *context,
const gchar *name,
const xmlChar **atrs)
{
g_print ("<EM>");
}
void
html_em_end_element (Context *context,
const gchar *name)
{
g_print ("</EM>");
}
void
html_tt_start_element (Context *context,
const gchar *name,
const xmlChar **atrs)
{
g_print ("<TT>");
}
void
html_tt_end_element (Context *context,
const gchar *name)
{
g_print ("</TT>");
}
void
artheader_start_element (Context *context, const gchar *name, const xmlChar **atrs)
{
g_print ("<HEAD>\n");
}
StackElement *
find_first_element (Context *context, GSList *args)
{
GList *ptr;
GSList *element_ptr;
for (ptr = context->stack; ptr; ptr = ptr->next) {
for (element_ptr = args; element_ptr; element_ptr = element_ptr->next) {
if (((StackElement*) ptr->data)->info &&
((StackElement*) ptr->data)->info->index == GPOINTER_TO_INT (element_ptr->data))
return (StackElement *) ptr->data;
}
}
return NULL;
}
ElementIndex
find_first_parent (Context *context, GSList *args)
{
StackElement *stack_el;
stack_el = find_first_element (context, args);
if (stack_el == NULL)
return UNDEFINED;
else
return stack_el->info->index;
}
/* helper functions */
static ElementInfo *
find_element_info (ElementInfo *elements,
const gchar *name)
{
while (elements->name != NULL) {
if (!g_strcasecmp (elements->name, name))
return elements;
elements++;
}
return NULL;
}
/* our callbacks for the xmlSAXHandler */
static xmlEntityPtr
get_entity (Context *context, const char *name)
{
xmlEntityPtr ret;
#ifdef ERROR_OUTPUT
g_print ("in getEntity:%s\n", name);
#endif
ret = getEntity (context->ParserCtxt, name);
/* return xmlGetPredefinedEntity (name); */
return (ret);
}
static void
start_document (Context *context)
{
startDocument (context->ParserCtxt);
}
static void
end_document (Context *context)
{
endDocument (context->ParserCtxt);
}
static void
start_element(Context *context,
const gchar *name,
const xmlChar **attrs)
{
ElementInfo *element;
StackElement *stack_el = g_new0 (StackElement, 1);
startElement (context->ParserCtxt, name, attrs);
element = find_element_info (context->elements, name);
stack_el->info = element;
context->stack = g_list_prepend (context->stack, stack_el);
context->empty_element = TRUE;
if (element && element->start_element_func)
(* element->start_element_func) (context, name, attrs);
if (!g_strcasecmp (name, "xref")) {
context->stack = g_list_remove_link (context->stack, context->stack);
} else if (!g_strcasecmp (name, "void")) {
context->stack = g_list_remove_link (context->stack, context->stack);
}
}
static void
end_element (Context *context,
const gchar *name)
{
ElementInfo *element;
StackElement *stack_el;
gchar **atrs_ptr;
endElement (context->ParserCtxt, name);
element = find_element_info (context->elements, name);
stack_el = (StackElement *) context->stack->data;
if (stack_el->info != element) {
/* Prolly a tag we ignored */
return;
}
if (element && element->end_element_func)
(* element->end_element_func) (context, name);
context->stack = g_list_remove_link (context->stack, context->stack);
atrs_ptr = stack_el->atrs;
while (atrs_ptr && *atrs_ptr) {
g_free (*atrs_ptr);
atrs_ptr++;
};
g_free (stack_el->atrs);
g_free (stack_el);
}
static void
gdb3html_characters (Context *context,
const gchar *chars,
int len)
{
ElementInfo *element;
characters (context->ParserCtxt, chars, len);
if (context->stack == NULL)
return;
element = ((StackElement *)context->stack->data)->info;
context->empty_element = FALSE;
if (element && element->characters_func)
(* element->characters_func) (context, chars, len);
}
static void
gdb3html_comment (Context *context, const char *msg)
{
#ifdef ERROR_OUTPUT
g_log("XML", G_LOG_LEVEL_MESSAGE, "%s", msg);
#endif
}
static void
gdb3html_warning (Context *context, const char *msg, ...)
{
va_list args;
va_start(args, msg);
#ifdef ERROR_OUTPUT
g_logv("XML", G_LOG_LEVEL_WARNING, msg, args);
#endif
va_end(args);
}
static void
gdb3html_error (Context *context, const char *msg, ...)
{
va_list args;
va_start(args, msg);
#ifdef ERROR_OUTPUT
g_logv("XML", G_LOG_LEVEL_CRITICAL, msg, args);
#endif
va_end(args);
}
static void
fatal_error (Context *context, const char *msg, ...)
{
va_list args;
va_start(args, msg);
#ifdef ERROR_OUTPUT
g_logv("XML", G_LOG_LEVEL_ERROR, msg, args);
#endif
va_end(args);
}
static void
cdata_block (Context *context, const xmlChar *value, int len)
{
ElementInfo *element;
StackElement *stack_el = g_new0 (StackElement, 1);
cdataBlock (context->ParserCtxt, value, len);
element = find_element_info (context->elements, "cdata");
stack_el->info = element;
context->stack = g_list_prepend (context->stack, stack_el);
if (element && element->characters_func)
(* element->characters_func) (context, value, len);
context->stack = g_list_remove_link (context->stack, context->stack);
}
static int
gdb3html_isStandalone (Context *context)
{
int ret;
ret = isStandalone (context->ParserCtxt);
return (ret);
}
static int
gdb3html_hasInternalSubset (Context *context)
{
int ret;
ret = hasInternalSubset (context->ParserCtxt);
return (ret);
}
static int
gdb3html_hasExternalSubset (Context *context)
{
int ret;
ret = hasExternalSubset (context->ParserCtxt);
return (ret);
}
static void
gdb3html_internalSubset (Context *context, const xmlChar *name,
const xmlChar *ExternalID, const xmlChar *SystemID)
{
/* This function is copied from SAX.c in libxml so we can 'silence'
* the warning messages */
xmlParserCtxtPtr ctxt;
ctxt = context->ParserCtxt;
xmlCreateIntSubset (ctxt->myDoc, name, ExternalID, SystemID);
if (((ExternalID != NULL) || (SystemID != NULL)) &&
(ctxt->validate && ctxt->wellFormed && ctxt->myDoc)) {
xmlDtdPtr ret = NULL;
xmlParserCtxtPtr dtdCtxt;
xmlParserInputPtr input = NULL;
xmlCharEncoding enc;
dtdCtxt = xmlNewParserCtxt();
if (dtdCtxt == NULL) {
return;
}
/* Ask entity resolve to load it */
if ((ctxt->directory != NULL) && (dtdCtxt->directory == NULL)) {
dtdCtxt->directory = (char *) xmlStrdup (BAD_CAST ctxt->directory);
}
if ((dtdCtxt->sax != NULL) && (dtdCtxt->sax->resolveEntity != NULL)) {
dtdCtxt->sax->warning = (warningSAXFunc) gdb3html_warning;
input = dtdCtxt->sax->resolveEntity (dtdCtxt->userData, ExternalID, SystemID);
}
if (input == NULL) {
xmlFreeParserCtxt (dtdCtxt);
return;
}
/* Plug some encoding conversion routines */
xmlPushInput (dtdCtxt, input);
enc = xmlDetectCharEncoding (dtdCtxt->input->cur);
xmlSwitchEncoding (dtdCtxt, enc);
if (input->filename == NULL) {
input->filename = (char *) xmlStrdup (SystemID);
}
input->line = 1;
input->col = 1;
input->base = dtdCtxt->input->cur;
input->cur = dtdCtxt->input->cur;
input->free = NULL;
/* lets parse the entity knowing it's an external subset */
xmlParseExternalSubset (dtdCtxt, ExternalID, SystemID);
if (dtdCtxt->myDoc != NULL) {
if (dtdCtxt->wellFormed) {
ret = dtdCtxt->myDoc->intSubset;
dtdCtxt->myDoc->intSubset = NULL;
} else {
ret = NULL;
}
xmlFreeDoc (dtdCtxt->myDoc);
dtdCtxt->myDoc = NULL;
}
xmlFreeParserCtxt (dtdCtxt);
ctxt->myDoc->extSubset = ret;
}
}
static xmlParserInputPtr
gdb3html_resolveEntity (Context *context, const xmlChar *publicId, const xmlChar *systemId)
{
xmlParserInputPtr ret;
ret = resolveEntity (context->ParserCtxt, publicId, systemId);
return ret;
}
static void
gdb3html_entityDecl (Context *context, const xmlChar *name, int type,
const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
{
entityDecl (context->ParserCtxt, name, type, publicId, systemId, content);
}
static void
gdb3html_attributeDecl (Context *context, const xmlChar *elem, const xmlChar *name,
int type, int def, const xmlChar *defaultValue,
xmlEnumerationPtr tree)
{
attributeDecl(context->ParserCtxt, elem, name, type, def, defaultValue, tree);
}
static void
gdb3html_elementDecl (Context *context, const xmlChar *name, int type,
xmlElementContentPtr content)
{
elementDecl(context->ParserCtxt, name, type, content);
}
static void
gdb3html_notationDecl (Context *context, const xmlChar *name,
const xmlChar *publicId, const xmlChar *systemId)
{
notationDecl(context->ParserCtxt, name, publicId, systemId);
}
static void
gdb3html_unparsedEntityDecl (Context *context, const xmlChar *name,
const xmlChar *publicId, const xmlChar *systemId,
const xmlChar *notationName)
{
unparsedEntityDecl (context->ParserCtxt, name, publicId, systemId, notationName);
}
static void
gdb3html_reference (Context *context, const xmlChar *name)
{
reference (context->ParserCtxt, name);
}
static void
gdb3html_processingInstruction (Context *context, const xmlChar *target,
const xmlChar *data)
{
processingInstruction (context->ParserCtxt, target, data);
}
static xmlEntityPtr
gdb3html_getParameterEntity (Context *context, const xmlChar *name)
{
xmlEntityPtr ret;
ret = getParameterEntity (context->ParserCtxt, name);
return ret;
}
static xmlSAXHandler parser = {
(internalSubsetSAXFunc) gdb3html_internalSubset, /* internalSubset */
(isStandaloneSAXFunc) gdb3html_isStandalone, /* isStandalone */
(hasInternalSubsetSAXFunc) gdb3html_hasInternalSubset, /* hasInternalSubset */
(hasExternalSubsetSAXFunc) gdb3html_hasExternalSubset, /* hasExternalSubset */
(resolveEntitySAXFunc) gdb3html_resolveEntity, /* resolveEntity */
(getEntitySAXFunc) get_entity, /* getEntity */
(entityDeclSAXFunc) gdb3html_entityDecl, /* entityDecl */
(notationDeclSAXFunc) gdb3html_notationDecl, /* notationDecl */
(attributeDeclSAXFunc) gdb3html_attributeDecl, /* attributeDecl */
(elementDeclSAXFunc) gdb3html_elementDecl, /* elementDecl */
(unparsedEntityDeclSAXFunc) gdb3html_unparsedEntityDecl, /* unparsedEntityDecl */
NULL, /* setDocumentLocator */
(startDocumentSAXFunc) start_document, /* startDocument */
(endDocumentSAXFunc) end_document, /* endDocument */
(startElementSAXFunc) start_element, /* startElement */
(endElementSAXFunc) end_element, /* endElement */
(referenceSAXFunc) gdb3html_reference, /* reference */
(charactersSAXFunc) gdb3html_characters, /* characters */
NULL, /* ignorableWhitespace */
(processingInstructionSAXFunc) gdb3html_processingInstruction, /* processingInstruction */
(commentSAXFunc) gdb3html_comment, /* comment */
(warningSAXFunc) gdb3html_warning, /* warning */
(errorSAXFunc) gdb3html_error, /* error */
(fatalErrorSAXFunc) fatal_error, /* fatalError */
(getParameterEntitySAXFunc) gdb3html_getParameterEntity, /*parameterEntity */
(cdataBlockSAXFunc) cdata_block
};
static xmlDocPtr
xml_parse_document (gchar *filename)
{
/* This function is ripped from parser.c in libxml but slightly
* modified so as not to spew debug warnings all around */
xmlDocPtr ret;
xmlParserCtxtPtr ctxt;
char *directory;
directory = NULL;
ctxt = xmlCreateFileParserCtxt(filename);
if (ctxt == NULL) {
return (NULL);
}
ctxt->sax = NULL; /* This line specifically stops the warnings */
if ((ctxt->directory == NULL) && (directory == NULL))
directory = xmlParserGetDirectory (filename);
if ((ctxt->directory == NULL) && (directory != NULL))
ctxt->directory = (char *) xmlStrdup ((xmlChar *) directory);
xmlParseDocument (ctxt);
if (ctxt->wellFormed) {
ret = ctxt->myDoc;
} else {
ret = NULL;
xmlFreeDoc (ctxt->myDoc);
ctxt->myDoc = NULL;
}
xmlFreeParserCtxt (ctxt);
return (ret);
}
static void
print_footer (const char *prev, const char *home, const char *next)
{
g_print ("\n<HR ALIGN=\"LEFT\" WIDTH=\"100%%\">\n");
g_print ("<TABLE WIDTH=\"100%%\" BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\">\n");
g_print ("<TR>\n<TD WIDTH=\"33%%\" ALIGN=\"LEFT\" VALIGN=\"TOP\">");
if (prev == NULL) {
g_print (" ");
} else {
g_print ("<A HREF=\"%s\"><<< Previous</A>", prev);
}
g_print ("</TD>\n<TD WIDTH=\"34%%\" ALIGN=\"CENTER\" VALIGN=\"TOP\">");
if (home == NULL) {
g_print (" ");
} else {
g_print ("<A HREF=\"%s\">Home</A>", home);
}
g_print ("</TD>\n<TD WIDTH=\"33%%\" ALIGN=\"RIGHT\" VALIGN=\"TOP\">");
if (next == NULL) {
g_print (" ");
} else {
g_print ("<A HREF=\"%s\">Next >>></A>", next);
}
g_print ("</TD>\n</TR></TABLE>\n");
g_print ("</BODY>\n</HTML>\n");
}
static void
sect_footer (Context *context, const char *section)
{
GList *temp_list;
GList *next_node;
GList *prev_node;
char *next_uri;
char *prev_uri;
char *home_uri;
if (context->sect1id_stack == NULL) {
return;
}
temp_list = context->sect1id_stack;
while (temp_list != NULL) {
if (g_strcasecmp ((char *)temp_list->data, section) == 0) {
/* Yes the below is correct because we are using a 'stack' */
next_node = g_list_previous (temp_list);
prev_node = g_list_next (temp_list);
if (next_node == NULL) {
next_uri = NULL;
} else {
next_uri = g_strdup_printf ("help:%s?%s", context->base_file, (char *) next_node->data);
}
if (prev_node == NULL) {
/* link back to the TOC */
prev_uri = g_strdup_printf ("help:%s", context->base_file);
} else {
prev_uri = g_strdup_printf ("help:%s?%s", context->base_file, (char *)prev_node->data);
}
home_uri = g_strdup_printf ("help:%s", context->base_file);
print_footer (prev_uri, home_uri, next_uri);
g_free (home_uri);
g_free (prev_uri);
g_free (next_uri);
break;
}
temp_list = g_list_next (temp_list);
}
}
static void
parse_file (gchar *filename, gchar *section)
{
GList *temp_list;
Context *context = g_new0 (Context, 1);
context->ParserCtxt = xmlNewParserCtxt ();
xmlInitParserCtxt (context->ParserCtxt);
context->ParserCtxt->sax = &parser;
context->ParserCtxt->validate = 1;
/* FIXME bugzilla.eazel.com 2399:
* Is the below correct? version needs to be set so as not to
* segfault in startDocument (in SAX.h) */
context->ParserCtxt->version = xmlStrdup ("1.0");
context->ParserCtxt->myDoc = xml_parse_document (filename);
xmlSubstituteEntitiesDefault (1);
if (section) {
context->target_section = g_strdup (section);
context->elements = sect_preparse;
context->data = sect_init_data ();
context->base_file = g_strdup (filename);
if (xmlSAXUserParseFile (&parser, context, context->base_file) < 0) {
g_error ("error");
};
context->elements = sect_elements;
if (xmlSAXUserParseFile (&parser, context, context->base_file) < 0) {
g_error ("error");
};
sect_footer (context, section);
} else {
context->elements = toc_elements;
context->data = toc_init_data ();
context->base_file = g_strdup (filename);
if (xmlSAXUserParseFile (&parser, context, context->base_file) < 0) {
g_error ("error");
};
temp_list = g_list_last (context->sect1id_stack);
if (temp_list != NULL) {
char *temp_uri;
temp_uri = g_strdup_printf ("help:%s?%s", context->base_file, (char *)temp_list->data);
print_footer (NULL, NULL, temp_uri);
g_free (temp_uri);
}
}
}
int
main (int argc, char *argv[])
{
gchar *section = NULL;
gchar *ptr;
gchar *ptr2;
if (argc != 2) {
/* It is '?SECTIONID' not '#SECTIONID' */
g_print ("Usage: gnome-db2html2 FILE[?SECTIONID]\n\n");
return 0;
}
if (!strncmp (argv[1], "file://", strlen ("file://"))) {
ptr = argv[1] + strlen ("file://");
} else
ptr = argv[1];
for (ptr2 = ptr; *ptr2; ptr2++){
if (*ptr2 == '?') {
*ptr2 = '\000';
if (*(ptr2 + 1))
section = ptr2 + 1;
break;
}
}
parse_file (ptr, section);
return 0;
}
|