summaryrefslogtreecommitdiff
path: root/src/main/xml.c
blob: fdbeff1ddf2d6a8eb1169b42964436db6e8868a3 (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
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
/*
 * "$Id: xml.c,v 1.43 2010/08/04 00:33:57 rlk Exp $"
 *
 *   XML parser - process Gutenprint XML data with mxml.
 *
 *   Copyright 2002-2003 Roger Leigh (rleigh@debian.org)
 *
 *   This program is free software; you can redistribute it and/or modify it
 *   under the terms of the GNU General Public License as published by the Free
 *   Software Foundation; either version 2 of the License, or (at your option)
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful, but
 *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *   for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gutenprint/gutenprint.h>
#include "gutenprint-internal.h"
#include <gutenprint/gutenprint-intl-internal.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#if defined(HAVE_VARARGS_H) && !defined(HAVE_STDARG_H)
#include <varargs.h>
#else
#include <stdarg.h>
#endif

typedef struct
{
  char *name;
  stp_xml_parse_func parse_func;
} stpi_xml_parse_registry;

static stp_list_t *stpi_xml_registry;

static stp_list_t *stpi_xml_preloads;

static const char *
xml_registry_namefunc(const void *item)
{
  const stpi_xml_parse_registry *xmlp = (const stpi_xml_parse_registry *) item;
  return xmlp->name;
}

static void
xml_registry_freefunc(void *item)
{
  stpi_xml_parse_registry *xmlp = (stpi_xml_parse_registry *) item;
  stp_free(xmlp->name);
  stp_free(xmlp);
}

static const char *
xml_preload_namefunc(const void *item)
{
  return (const char *) item;
}

static void
xml_preload_freefunc(void *item)
{
  stp_free(item);
}

void
stp_register_xml_parser(const char *name, stp_xml_parse_func parse_func)
{
  stpi_xml_parse_registry *xmlp;
  stp_list_item_t *item = stp_list_get_item_by_name(stpi_xml_registry, name);
  if (item)
    xmlp = (stpi_xml_parse_registry *) stp_list_item_get_data(item);
  else
    {
      xmlp = stp_malloc(sizeof(stpi_xml_parse_registry));
      xmlp->name = stp_strdup(name);
      stp_list_item_create(stpi_xml_registry, NULL, xmlp);
    }
  xmlp->parse_func = parse_func;
}

void
stp_unregister_xml_parser(const char *name)
{
  stp_list_item_t *item = stp_list_get_item_by_name(stpi_xml_registry, name);
  if (item)
    stp_list_item_destroy(stpi_xml_registry, item);
}

void
stp_register_xml_preload(const char *filename)
{
  stp_list_item_t *item = stp_list_get_item_by_name(stpi_xml_preloads, filename);
  if (!item)
    {
      char *the_filename = stp_strdup(filename);
      stp_list_item_create(stpi_xml_preloads, NULL, the_filename);
    }
}

void
stp_unregister_xml_preload(const char *name)
{
  stp_list_item_t *item = stp_list_get_item_by_name(stpi_xml_preloads, name);
  if (item)
    stp_list_item_destroy(stpi_xml_preloads, item);
}


static void stpi_xml_process_gutenprint(stp_mxml_node_t *gutenprint, const char *file);

static char *saved_locale;                 /* Saved LC_ALL */
static int xml_is_initialised;                 /* Flag for init */

void
stp_xml_preinit(void)
{
  static int xml_is_preinitialized = 0;
  if (!xml_is_preinitialized)
    {
      stpi_xml_registry = stp_list_create();
      stp_list_set_freefunc(stpi_xml_registry, xml_registry_freefunc);
      stp_list_set_namefunc(stpi_xml_registry, xml_registry_namefunc);
      stpi_xml_preloads = stp_list_create();
      stp_list_set_freefunc(stpi_xml_preloads, xml_preload_freefunc);
      stp_list_set_namefunc(stpi_xml_preloads, xml_preload_namefunc);
    }
}    

/*
 * Call before using any of the static functions in this file.  All
 * public functions should call this before using any mxml
 * functions.
 */
void
stp_xml_init(void)
{
  stp_deprintf(STP_DBG_XML, "stp_xml_init: entering at level %d\n",
	       xml_is_initialised);
  if (xml_is_initialised >= 1)
    {
      xml_is_initialised++;
      return;
    }

  /* Set some locale facets to "C" */
#ifdef HAVE_LOCALE_H
  saved_locale = stp_strdup(setlocale(LC_ALL, NULL));
  stp_deprintf(STP_DBG_XML, "stp_xml_init: saving locale %s\n", saved_locale);
  setlocale(LC_ALL, "C");
#endif

  xml_is_initialised = 1;
}

/*
 * Call after using any of the static functions in this file.  All
 * public functions should call this after using any mxml functions.
 */
void
stp_xml_exit(void)
{
  stp_deprintf(STP_DBG_XML, "stp_xml_exit: entering at level %d\n",
	       xml_is_initialised);
  if (xml_is_initialised > 1) /* don't restore original state */
    {
      xml_is_initialised--;
      return;
    }
  else if (xml_is_initialised < 1)
    return;

  /* Restore locale */
#ifdef HAVE_LOCALE_H
  stp_deprintf(STP_DBG_XML, "stp_xml_init: restoring locale %s\n", saved_locale);
  setlocale(LC_ALL, saved_locale);
  stp_free(saved_locale);
  saved_locale = NULL;
#endif
  xml_is_initialised = 0;
}

void
stp_xml_parse_file_named(const char *name)
{
  stp_list_t *file_list = stpi_list_files_on_data_path(name); /* List of XML files */
  stp_list_item_t *item;                 /* Pointer to current list item */
  item = stp_list_get_start(file_list);
  while (item)
    {
      stp_deprintf(STP_DBG_XML,
		   "stp_xml_parse_file_named: source file: %s\n",
		   (const char *) stp_list_item_get_data(item));
      stp_xml_parse_file((const char *) stp_list_item_get_data(item));
      item = stp_list_item_next(item);
    }
  stp_list_destroy(file_list);
}
  

/*
 * Read all available XML files.
 */
int
stp_xml_init_defaults(void)
{
  stp_list_item_t *item;                 /* Pointer to current list item */

  stp_xml_init();

  /* Parse each XML file */
  item = stp_list_get_start(stpi_xml_preloads);
  while (item)
    {
      stp_deprintf(STP_DBG_XML, "stp_xml_init_defaults: source file: %s\n",
		   (const char *) stp_list_item_get_data(item));
      stp_xml_parse_file_named((const char *) stp_list_item_get_data(item));
      item = stp_list_item_next(item);
    }
  stp_list_destroy(stpi_xml_preloads);

  stp_xml_exit();

  return 0;
}


/*
 * Parse a single XML file.
 */
int
stp_xml_parse_file(const char *file) /* File to parse */
{
  stp_mxml_node_t *doc;
  stp_mxml_node_t *cur;
  FILE *fp;

  stp_deprintf(STP_DBG_XML, "stp_xml_parse_file: reading  `%s'...\n", file);

  fp = fopen(file, "r");
  if (!fp)
    {
      stp_erprintf("stp_xml_parse_file: unable to open %s: %s\n", file,
		   strerror(errno));
      return 1;
    }

  stp_xml_init();

  doc = stp_mxmlLoadFile(NULL, fp, STP_MXML_NO_CALLBACK);
  fclose(fp);

  cur = doc->child;
  while (cur &&
	 (cur->type != STP_MXML_ELEMENT ||
	  (strcmp(cur->value.element.name, "gutenprint") != 0 &&
	   strcmp(cur->value.element.name, "gimp-print") != 0)))
    cur = cur->next;

  if (cur == NULL || cur->type != STP_MXML_ELEMENT)
    {
      stp_erprintf("stp_xml_parse_file: %s: parse error\n", file);
      stp_mxmlDelete(doc);
      return 1;
    }

  if (strcmp(cur->value.element.name, "gutenprint") != 0 &&
      strcmp(cur->value.element.name, "gimp-print") != 0)
    {
      stp_erprintf
	("XML file of the wrong type, root node is %s != (gutenprint || gimp-print)",
	 cur->value.element.name);
      stp_mxmlDelete(doc);
      return 1;
    }

  /* The XML file was read and is the right format */

  stpi_xml_process_gutenprint(cur, file);
  stp_mxmlDelete(doc);

  stp_xml_exit();

  return 0;
}

/*
 * Convert a text string into an integer.
 */
long
stp_xmlstrtol(const char *textval)
{
  long val; /* The value to return */
  val = strtol(textval, (char **)NULL, 0);

  return val;
}

/*
 * Convert a text string into an unsigned int.
 */
unsigned long
stp_xmlstrtoul(const char *textval)
{
  unsigned long val; /* The value to return */
  val = strtoul(textval, (char **)NULL, 0);

  return val;
}

/*
 * Convert a text string into a double.
 */
double
stp_xmlstrtod(const char *textval)
{
  double val; /* The value to return */
  val = strtod(textval, (char **)NULL);

  return val;
}

/*
 * Convert an encoded text string into a raw.
 */
stp_raw_t *
stp_xmlstrtoraw(const char *textval)
{
  size_t tcount;
  stp_raw_t *raw;
  unsigned char *answer;
  unsigned char *aptr;
  if (! textval || *textval == 0)
    return NULL;
  tcount = strlen(textval);
  raw = stp_zalloc(sizeof(stp_raw_t));
  answer = stp_malloc(tcount + 1); /* Worst case -- we may not need it all */
  aptr = answer;
  raw->data = answer;
  while (*textval)
    {
      if (*textval != '\\')
	{
	  *aptr++ = *textval++;
	  raw->bytes++;
	}
      else
	{
	  textval++;
	  if (textval[0] >= '0' && textval[0] <= '3' &&
	      textval[1] >= '0' && textval[1] <= '7' &&
	      textval[2] >= '0' && textval[2] <= '7')
	    {
	      *aptr++ = (((textval[0] - '0') << 6) +
			 ((textval[1] - '0') << 3) +
			 ((textval[2] - '0') << 0));
	      raw->bytes++;
	      textval += 3;
	    }
	  else if (textval[0] == '\0' || textval[1] == '\0' || textval[2] == '\0')
	    break;
	  else
	    textval += 3;
	}
    }
  *aptr = '\0';
  return raw;
}

char *
stp_rawtoxmlstr(const stp_raw_t *raw)
{
  if (raw && raw->bytes > 0)
    {
      int i;
      const unsigned char *data = (const unsigned char *) (raw->data);
      char *answer = stp_malloc((raw->bytes * 4) + 1); /* \012 */
      unsigned char *aptr = (unsigned char *) answer;
      for (i = 0; i < raw->bytes; i++)
	{
	  if (data[i] > ' ' && data[i] < '\177' && data[i] != '\\' &&
	      data[i] != '<' && data[i] != '>' && data[i] != '&')
	    *aptr++ = data[i];
	  else
	    {
	      *aptr++ = '\\';
	      *aptr++ = '0' + ((data[i] & '\300') >> 6);
	      *aptr++ = '0' + ((data[i] & '\070') >> 3);
	      *aptr++ = '0' + ((data[i] & '\007') >> 0);
	    }
	}
      *aptr = '\0';
      return answer;
    }
  return NULL;
}

char *
stp_strtoxmlstr(const char *str)
{
  if (str && strlen(str) > 0)
    {
      int i;
      int bytes = strlen(str);
      const unsigned char *data = (const unsigned char *) (str);
      char *answer = stp_malloc((bytes * 4) + 1); /* "\012" is worst case */
      unsigned char *aptr = (unsigned char *) answer;
      for (i = 0; i < bytes; i++)
	{
	  if (data[i] > ' ' && data[i] < '\177' && data[i] != '\\' &&
	      data[i] != '<' && data[i] != '>' && data[i] != '&')
	    *aptr++ = data[i];
	  else
	    {
	      *aptr++ = '\\';
	      *aptr++ = '0' + ((data[i] & '\300') >> 6);
	      *aptr++ = '0' + ((data[i] & '\070') >> 3);
	      *aptr++ = '0' + ((data[i] & '\007') >> 0);
	    }
	}
      *aptr = '\0';
      return answer;
    }
  return NULL;
}

void
stp_prtraw(const stp_raw_t *raw, FILE *fp)
{
  if (raw && raw->bytes > 0)
    {
      int i;
      const unsigned char *data = (const unsigned char *) (raw->data);
      for (i = 0; i < raw->bytes; i++)
	{
	  if (data[i] > ' ' && data[i] < '\177' && data[i] != '\\' &&
	      data[i] != '<' && data[i] != '>' && data[i] != '&')
	    fputc(data[i], fp);
	  else
	    {
	      fputc('\\', fp);
	      fputc('0' + ((data[i] & '\300') >> 6), fp);
	      fputc('0' + ((data[i] & '\070') >> 3), fp);
	      fputc('0' + ((data[i] & '\007') >> 0), fp);
	    }
	}
    }
}


/*
 * Find a node in an XML tree.  This function takes an xmlNodePtr,
 * followed by a NULL-terminated list of nodes which are required.
 * For example stp_xml_get_node(myroot, "gutenprint", "dither") will
 * return the first dither node in the tree.  Additional dither nodes
 * cannot be accessed with this function.
 */
stp_mxml_node_t *
stp_xml_get_node(stp_mxml_node_t *xmlroot, ...)
{
  stp_mxml_node_t *child;
  va_list ap;
  const char *target = NULL;

  va_start(ap, xmlroot);

  child = xmlroot;
  target = va_arg(ap, const char *);

  while (target && child)
    {
      child = stp_mxmlFindElement(child, child, target, NULL, NULL, STP_MXML_DESCEND);
      target = va_arg(ap, const char *);
    }
  va_end(ap);
  return child;
}

static void
stpi_xml_process_node(stp_mxml_node_t *node, const char *file)
{
  stp_list_item_t *item =
    stp_list_get_item_by_name(stpi_xml_registry, node->value.element.name);
  if (item)
    {
      stpi_xml_parse_registry *xmlp =
	(stpi_xml_parse_registry *) stp_list_item_get_data(item);
      (xmlp->parse_func)(node, file);
    }
}

/*
 * Parse the <gutenprint> root node.
 */
static void
stpi_xml_process_gutenprint(stp_mxml_node_t *cur, const char *file) /* The node to parse */
{
  stp_mxml_node_t *child;                       /* Child node pointer */

  child = cur->child;
  while (child)
    {
      /* process nodes with corresponding parser */
      if (child->type == STP_MXML_ELEMENT)
	stpi_xml_process_node(child, file);
      child = child->next;
    }
}

/*
 * Create a basic gutenprint XML document tree root
 */
stp_mxml_node_t *
stp_xmldoc_create_generic(void)
{
  stp_mxml_node_t *doc;
  stp_mxml_node_t *rootnode;

  /* Create the XML tree */
  doc = stp_mxmlNewElement(NULL, "?xml");
  stp_mxmlElementSetAttr(doc, "version", "1.0");

  rootnode = stp_mxmlNewElement(doc, "gutenprint");
  stp_mxmlElementSetAttr
    (rootnode, "xmlns", "http://gimp-print.sourceforge.net/xsd/gp.xsd-1.0");
  stp_mxmlElementSetAttr
    (rootnode, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
  stp_mxmlElementSetAttr
    (rootnode, "xsi:schemaLocation",
     "http://gimp-print.sourceforge.net/xsd/gp.xsd-1.0 gutenprint.xsd");

  return doc;
}