summaryrefslogtreecommitdiff
path: root/components/help/converters/gnome-db2html2/gdb3html.c
blob: ff034960a075b92dee523c5b44d4458a135db39d (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
#include "gdb3html.h"
#include "toc-elements.h"
#include "sect-elements.h"

/* 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)
{
	g_print ("</BODY>\n</HTML>\n");
}

void
artheader_start_element (Context *context, const gchar *name, const xmlChar **atrs)
{
	g_print ("<HEAD>\n");
}

void
write_characters (Context *context,
		  const gchar *chars,
		  int len)
{
	char *temp;

	temp = g_strndup (chars, len);
	g_print ("chars:%s:\n", temp);
	g_free (temp);
	
}

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->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 gchar *name)
{
#ifdef ERROR_OUTPUT
	g_print ("in getEntity:%s\n", name);
#endif
	return xmlGetPredefinedEntity (name);
}

static void
start_document (Context *context)
{
}

static void
end_document (Context *context)
{

}

static void
start_element(Context *context,
	      const gchar *name,
	      const xmlChar **attrs)
{
	ElementInfo *element;
	StackElement *stack_el = g_new0 (StackElement, 1);

	element = find_element_info (context->elements, name);

	stack_el->info = element;
	context->stack = g_list_prepend (context->stack, stack_el);

	if (element && element->start_element_func)
		(* element->start_element_func) (context, name, attrs);
}

static void
end_element (Context *context,
	     const gchar *name)
{
	ElementInfo *element;
	StackElement *stack_el;
	gchar **atrs_ptr;

	element = find_element_info (context->elements, name);
	stack_el = (StackElement *) context->stack->data;
	g_assert (stack_el->info == element);
	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
characters (Context *context,
	    const gchar *chars,
	    int len)
{
	ElementInfo *element;


	element = ((StackElement *)context->stack->data)->info;

	if (element && element->characters_func)
		(* element->characters_func) (context, chars, len);
}

static void
comment (Context *context, const char *msg)
{
#ifdef ERROR_OUTPUT
	g_log("XML", G_LOG_LEVEL_MESSAGE, "%s", msg);
#endif
}

static void
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
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 xmlSAXHandler parser = {
	NULL,  /* internalSubset */
	NULL, /* isStandalone */
	NULL, /* hasInternalSubset */
	NULL, /* hasExternalSubset */
	NULL, /* resolveEntity */
	(getEntitySAXFunc) get_entity, /* getEntity */
	NULL, /* entityDecl */
	NULL, /* notationDecl */
	NULL, /* attributeDecl */
	NULL, /* elementDecl */
	NULL, /* unparsedEntityDecl */
	NULL, /* setDocumentLocator */
	(startDocumentSAXFunc) start_document, /* startDocument */
	(endDocumentSAXFunc) end_document, /* endDocument */
	(startElementSAXFunc) start_element, /* startElement */
	(endElementSAXFunc) end_element, /* endElement */
	NULL, /* reference */
	(charactersSAXFunc) characters, /* characters */
	NULL, /* ignorableWhitespace */
	NULL, /* processingInstruction */
	(commentSAXFunc) comment, /* comment */
	(warningSAXFunc) warning, /* warning */
	(errorSAXFunc) error, /* error */
	(fatalErrorSAXFunc) fatal_error /* fatalError */
};

static void
parse_file (gchar *filename, gchar *section)
{
	Context *context = g_new0 (Context, 1);

	if (section) {
		context->target_section = g_strdup (section);
		context->elements = sect_elements;
		context->data = sect_init_data ();
		context->base_file = g_strdup (filename);
	} 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_print ("error\n");
	};
}

int
main (int argc, char *argv[])
{
	gchar *section = NULL;
	gchar *ptr;

	if (argc != 2) {
		g_print ("Usage:  gdb2html FILE[#SECTIONID]\n\n");
		return 0;
	}

	for (ptr = argv[1]; *ptr; ptr++){
		if (*ptr == '#') {
			*ptr = '\000';
			if (*(ptr + 1))
				section = ptr + 1;
			break;
		}
	}
	parse_file (argv[1], section);

	return 0;
}