| 1 | /* |
| 2 | * |
| 3 | * XML parser - process Gutenprint XML data with mxml. |
| 4 | * |
| 5 | * Copyright 2002-2003 Roger Leigh ([email protected]) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the Free |
| 9 | * Software Foundation; either version 2 of the License, or (at your option) |
| 10 | * any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 | * for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | #include <config.h> |
| 24 | #endif |
| 25 | #include <gutenprint/gutenprint.h> |
| 26 | #include "gutenprint-internal.h" |
| 27 | #include <gutenprint/gutenprint-intl-internal.h> |
| 28 | #include <ctype.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <math.h> |
| 33 | #include <errno.h> |
| 34 | #ifdef HAVE_LIMITS_H |
| 35 | #include <limits.h> |
| 36 | #endif |
| 37 | #if defined(HAVE_VARARGS_H) && !defined(HAVE_STDARG_H) |
| 38 | #include <varargs.h> |
| 39 | #else |
| 40 | #include <stdarg.h> |
| 41 | #endif |
| 42 | |
| 43 | typedef struct |
| 44 | { |
| 45 | char *name; |
| 46 | stp_xml_parse_func parse_func; |
| 47 | } stpi_xml_parse_registry; |
| 48 | |
| 49 | static stp_list_t *stpi_xml_registry; |
| 50 | |
| 51 | static stp_list_t *stpi_xml_preloads; |
| 52 | |
| 53 | static stp_list_t *stpi_xml_files_loaded; |
| 54 | |
| 55 | static stp_string_list_t *cached_xml_files; |
| 56 | |
| 57 | static const char * |
| 58 | xml_registry_namefunc(const void *item) |
| 59 | { |
| 60 | const stpi_xml_parse_registry *xmlp = (const stpi_xml_parse_registry *) item; |
| 61 | return xmlp->name; |
| 62 | } |
| 63 | |
| 64 | static void |
| 65 | xml_registry_freefunc(void *item) |
| 66 | { |
| 67 | stpi_xml_parse_registry *xmlp = (stpi_xml_parse_registry *) item; |
| 68 | stp_free(xmlp->name); |
| 69 | stp_free(xmlp); |
| 70 | } |
| 71 | |
| 72 | static const char * |
| 73 | xml_preload_namefunc(const void *item) |
| 74 | { |
| 75 | return (const char *) item; |
| 76 | } |
| 77 | |
| 78 | static void |
| 79 | xml_preload_freefunc(void *item) |
| 80 | { |
| 81 | stp_free(item); |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | stp_register_xml_parser(const char *name, stp_xml_parse_func parse_func) |
| 86 | { |
| 87 | stpi_xml_parse_registry *xmlp; |
| 88 | stp_list_item_t *item = stp_list_get_item_by_name(stpi_xml_registry, name); |
| 89 | if (item) |
| 90 | xmlp = (stpi_xml_parse_registry *) stp_list_item_get_data(item); |
| 91 | else |
| 92 | { |
| 93 | xmlp = stp_malloc(sizeof(stpi_xml_parse_registry)); |
| 94 | xmlp->name = stp_strdup(name); |
| 95 | stp_list_item_create(stpi_xml_registry, NULL, xmlp); |
| 96 | } |
| 97 | xmlp->parse_func = parse_func; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | stp_unregister_xml_parser(const char *name) |
| 102 | { |
| 103 | stp_list_item_t *item = stp_list_get_item_by_name(stpi_xml_registry, name); |
| 104 | if (item) |
| 105 | stp_list_item_destroy(stpi_xml_registry, item); |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | stp_register_xml_preload(const char *filename) |
| 110 | { |
| 111 | stp_list_item_t *item = stp_list_get_item_by_name(stpi_xml_preloads, filename); |
| 112 | if (!item) |
| 113 | { |
| 114 | char *the_filename = stp_strdup(filename); |
| 115 | stp_list_item_create(stpi_xml_preloads, NULL, the_filename); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | stp_unregister_xml_preload(const char *name) |
| 121 | { |
| 122 | stp_list_item_t *item = stp_list_get_item_by_name(stpi_xml_preloads, name); |
| 123 | if (item) |
| 124 | stp_list_item_destroy(stpi_xml_preloads, item); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | static void stpi_xml_process_gutenprint(stp_mxml_node_t *gutenprint, const char *file); |
| 129 | |
| 130 | static char *saved_locale; /* Saved LC_ALL */ |
| 131 | static int xml_is_initialised; /* Flag for init */ |
| 132 | |
| 133 | void |
| 134 | stp_xml_preinit(void) |
| 135 | { |
| 136 | if (! stpi_xml_registry) |
| 137 | { |
| 138 | stpi_xml_registry = stp_list_create(); |
| 139 | stp_list_set_freefunc(stpi_xml_registry, xml_registry_freefunc); |
| 140 | stp_list_set_namefunc(stpi_xml_registry, xml_registry_namefunc); |
| 141 | } |
| 142 | if (! stpi_xml_preloads) |
| 143 | { |
| 144 | stpi_xml_preloads = stp_list_create(); |
| 145 | stp_list_set_freefunc(stpi_xml_preloads, xml_preload_freefunc); |
| 146 | stp_list_set_namefunc(stpi_xml_preloads, xml_preload_namefunc); |
| 147 | } |
| 148 | if (! stpi_xml_files_loaded) |
| 149 | { |
| 150 | stpi_xml_files_loaded = stp_list_create(); |
| 151 | stp_list_set_freefunc(stpi_xml_files_loaded, xml_preload_freefunc); |
| 152 | stp_list_set_namefunc(stpi_xml_files_loaded, xml_preload_namefunc); |
| 153 | } |
| 154 | if (! cached_xml_files) |
| 155 | { |
| 156 | cached_xml_files = stp_string_list_create(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Call before using any of the static functions in this file. All |
| 162 | * public functions should call this before using any mxml |
| 163 | * functions. |
| 164 | */ |
| 165 | void |
| 166 | stp_xml_init(void) |
| 167 | { |
| 168 | stp_deprintf(STP_DBG_XML, "stp_xml_init: entering at level %d\n", |
| 169 | xml_is_initialised); |
| 170 | if (xml_is_initialised >= 1) |
| 171 | { |
| 172 | xml_is_initialised++; |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | /* Set some locale facets to "C" */ |
| 177 | #ifdef HAVE_LOCALE_H |
| 178 | saved_locale = stp_strdup(setlocale(LC_ALL, NULL)); |
| 179 | stp_deprintf(STP_DBG_XML, "stp_xml_init: saving locale %s\n", saved_locale); |
| 180 | setlocale(LC_ALL, "C"); |
| 181 | #endif |
| 182 | |
| 183 | xml_is_initialised = 1; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Call after using any of the static functions in this file. All |
| 188 | * public functions should call this after using any mxml functions. |
| 189 | */ |
| 190 | void |
| 191 | stp_xml_exit(void) |
| 192 | { |
| 193 | stp_deprintf(STP_DBG_XML, "stp_xml_exit: entering at level %d\n", |
| 194 | xml_is_initialised); |
| 195 | if (xml_is_initialised > 1) /* don't restore original state */ |
| 196 | { |
| 197 | xml_is_initialised--; |
| 198 | return; |
| 199 | } |
| 200 | else if (xml_is_initialised < 1) |
| 201 | { |
| 202 | stp_erprintf("stp_xml_exit: unmatched stp_xml_init!\n"); |
| 203 | stp_abort(); |
| 204 | } |
| 205 | |
| 206 | /* Restore locale */ |
| 207 | #ifdef HAVE_LOCALE_H |
| 208 | stp_deprintf(STP_DBG_XML, "stp_xml_exit: restoring locale %s\n", saved_locale); |
| 209 | setlocale(LC_ALL, saved_locale); |
| 210 | stp_free(saved_locale); |
| 211 | saved_locale = NULL; |
| 212 | #endif |
| 213 | xml_is_initialised = 0; |
| 214 | } |
| 215 | |
| 216 | void |
| 217 | stp_xml_parse_file_named(const char *name) |
| 218 | { |
| 219 | stp_xml_preinit(); |
| 220 | stp_deprintf(STP_DBG_XML, "stp_xml_parse_file_named(%s)\n", name); |
| 221 | if (! stp_list_get_item_by_name(stpi_xml_files_loaded, name)) |
| 222 | { |
| 223 | char *file_name = stp_path_find_file(NULL, name); |
| 224 | if (file_name) |
| 225 | { |
| 226 | stp_xml_parse_file(file_name); |
| 227 | free(file_name); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Read all available XML files. |
| 234 | */ |
| 235 | int |
| 236 | stp_xml_init_defaults(void) |
| 237 | { |
| 238 | stp_list_item_t *item; /* Pointer to current list item */ |
| 239 | |
| 240 | stp_xml_init(); |
| 241 | |
| 242 | /* Parse each XML file */ |
| 243 | item = stp_list_get_start(stpi_xml_preloads); |
| 244 | while (item) |
| 245 | { |
| 246 | stp_deprintf(STP_DBG_XML, "stp_xml_init_defaults: source file: %s\n", |
| 247 | (const char *) stp_list_item_get_data(item)); |
| 248 | stp_xml_parse_file_named((const char *) stp_list_item_get_data(item)); |
| 249 | item = stp_list_item_next(item); |
| 250 | } |
| 251 | stp_list_destroy(stpi_xml_preloads); |
| 252 | |
| 253 | stp_xml_exit(); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Parse a single XML file. |
| 260 | */ |
| 261 | int |
| 262 | stp_xml_parse_file(const char *file) /* File to parse */ |
| 263 | { |
| 264 | stp_mxml_node_t *doc; |
| 265 | stp_mxml_node_t *cur; |
| 266 | int status = 0; |
| 267 | |
| 268 | stp_deprintf(STP_DBG_XML, "stp_xml_parse_file: reading `%s'...\n", file); |
| 269 | |
| 270 | stp_xml_init(); |
| 271 | |
| 272 | doc = stp_mxmlLoadFromFile(NULL, file, STP_MXML_NO_CALLBACK); |
| 273 | |
| 274 | if ((cur = stp_xml_get_node(doc, "gutenprint", NULL)) == NULL) |
| 275 | { |
| 276 | stp_erprintf("stp_xml_parse_file: %s: parse error\n", file); |
| 277 | status = 1; |
| 278 | } |
| 279 | else |
| 280 | /* The XML file was read and is the right format */ |
| 281 | stpi_xml_process_gutenprint(cur, file); |
| 282 | |
| 283 | stp_mxmlDelete(doc); |
| 284 | |
| 285 | stp_xml_exit(); |
| 286 | |
| 287 | return status; |
| 288 | } |
| 289 | |
| 290 | static stp_mxml_node_t * |
| 291 | xml_try_parse_file_1(const char *pathname, const char *topnodename) |
| 292 | { |
| 293 | stp_mxml_node_t *root = |
| 294 | stp_mxmlLoadFromFile(NULL, pathname, STP_MXML_NO_CALLBACK); |
| 295 | if (root) |
| 296 | { |
| 297 | stp_mxml_node_t *answer = |
| 298 | stp_xml_get_node(root, "gutenprint", topnodename, NULL); |
| 299 | if (answer) |
| 300 | return answer; |
| 301 | stp_mxmlDelete(root); |
| 302 | return NULL; |
| 303 | } |
| 304 | else |
| 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | static stp_mxml_node_t * |
| 309 | xml_try_parse_file(const char *pathname, const char *topnodename) |
| 310 | { |
| 311 | stp_xml_init(); |
| 312 | stp_mxml_node_t *answer = xml_try_parse_file_1(pathname, topnodename); |
| 313 | stp_xml_exit(); |
| 314 | return answer; |
| 315 | } |
| 316 | |
| 317 | static void |
| 318 | xml_cache_file(const char *name, const char *cache, stp_mxml_node_t *node) |
| 319 | { |
| 320 | char *addr_string; |
| 321 | stp_asprintf(&addr_string, "%p", (void *) node); |
| 322 | /* |
| 323 | * A given XML object should never be in multiple caches! However, |
| 324 | * it's possible that different nodes of the same file will be in different |
| 325 | * caches. |
| 326 | */ |
| 327 | STPI_ASSERT(!stp_string_list_is_present(cached_xml_files, addr_string), NULL); |
| 328 | if (cache) |
| 329 | { |
| 330 | stp_refcache_add_item(cache, name, node); |
| 331 | stp_string_list_add_string_unsafe(cached_xml_files, addr_string, cache); |
| 332 | } |
| 333 | else |
| 334 | stp_string_list_add_string_unsafe(cached_xml_files, addr_string, ""); |
| 335 | stp_free(addr_string); |
| 336 | } |
| 337 | |
| 338 | static stp_mxml_node_t * |
| 339 | xml_parse_file_from_path(const char *name, const char *topnodename, |
| 340 | const char *path, const char *cache) |
| 341 | { |
| 342 | stp_mxml_node_t *answer = NULL; |
| 343 | if (!(name[0] != '/' && strncmp(name, "./", 2) && strncmp(name, "../", 3))) |
| 344 | answer = xml_try_parse_file(name, topnodename); |
| 345 | else |
| 346 | { |
| 347 | stp_list_t *path_to_search; |
| 348 | stp_list_item_t *item; |
| 349 | if (path) |
| 350 | path_to_search = stp_generate_path(path); |
| 351 | else |
| 352 | path_to_search = stp_data_path(); |
| 353 | item = stp_list_get_start(path_to_search); |
| 354 | while (item) |
| 355 | { |
| 356 | const char *dn = (const char *) stp_list_item_get_data(item); |
| 357 | char *ffn = stpi_path_merge(dn, name); |
| 358 | answer = xml_try_parse_file(ffn, topnodename); |
| 359 | stp_free(ffn); |
| 360 | if (answer) |
| 361 | break; |
| 362 | item = stp_list_item_next(item); |
| 363 | } |
| 364 | stp_list_destroy(path_to_search); |
| 365 | } |
| 366 | if (answer) |
| 367 | xml_cache_file(name, cache, answer); |
| 368 | return answer; |
| 369 | } |
| 370 | |
| 371 | stp_mxml_node_t * |
| 372 | stp_xml_parse_file_from_path_uncached(const char *name, const char *topnodename, |
| 373 | const char *path) |
| 374 | { |
| 375 | return xml_parse_file_from_path(name, topnodename, path, NULL); |
| 376 | } |
| 377 | |
| 378 | stp_mxml_node_t * |
| 379 | stp_xml_parse_file_from_path_uncached_safe(const char *name, |
| 380 | const char *topnodename, |
| 381 | const char *path) |
| 382 | { |
| 383 | stp_mxml_node_t *answer = |
| 384 | xml_parse_file_from_path(name, topnodename, path, NULL); |
| 385 | if (! answer) |
| 386 | { |
| 387 | stp_erprintf("Cannot find file %s of type %s\n", name, topnodename); |
| 388 | stp_abort(); |
| 389 | } |
| 390 | return answer; |
| 391 | } |
| 392 | |
| 393 | stp_mxml_node_t * |
| 394 | stp_xml_parse_file_from_path(const char *name, const char *topnodename, |
| 395 | const char *path) |
| 396 | { |
| 397 | char *cache; |
| 398 | void *data; |
| 399 | stp_asprintf(&cache, "%s_%s_%s", "xml_cache", topnodename, |
| 400 | path ? path : "DEFAULT"); |
| 401 | data = stp_refcache_find_item(cache, name); |
| 402 | if (! data) |
| 403 | data = xml_parse_file_from_path(name, topnodename, path, cache); |
| 404 | stp_free(cache); |
| 405 | return (stp_mxml_node_t *) data; |
| 406 | } |
| 407 | |
| 408 | stp_mxml_node_t * |
| 409 | stp_xml_parse_file_from_path_safe(const char *name, const char *topnodename, |
| 410 | const char *path) |
| 411 | { |
| 412 | stp_mxml_node_t *answer = stp_xml_parse_file_from_path(name, topnodename, |
| 413 | path); |
| 414 | if (! answer) |
| 415 | { |
| 416 | stp_erprintf("FATAL: Cannot find file %s of type %s\n", name, topnodename); |
| 417 | stp_abort(); |
| 418 | } |
| 419 | return answer; |
| 420 | } |
| 421 | |
| 422 | void |
| 423 | stp_xml_free_parsed_file(stp_mxml_node_t *node) |
| 424 | { |
| 425 | char *addr_string; |
| 426 | /* free(NULL) is legal and a no-op. */ |
| 427 | if (! node) |
| 428 | return; |
| 429 | stp_asprintf(&addr_string, "%p", (void *) node); |
| 430 | stp_param_string_t *cache_entry = |
| 431 | stp_string_list_find(cached_xml_files, addr_string); |
| 432 | if (! cache_entry) |
| 433 | { |
| 434 | stp_erprintf("FATAL: Trying to free unrecorded node %s\n", addr_string); |
| 435 | stp_abort(); |
| 436 | } |
| 437 | if (cache_entry->text && cache_entry->text[0] != '\0') |
| 438 | stp_refcache_remove_item(cache_entry->text, addr_string); |
| 439 | stp_string_list_remove_string(cached_xml_files, addr_string); |
| 440 | stp_free(addr_string); |
| 441 | while (node->parent && node->parent != node) |
| 442 | node = node->parent; |
| 443 | stp_xml_init(); |
| 444 | stp_mxmlDelete(node); |
| 445 | stp_xml_exit(); |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | * Convert a text string into an integer. |
| 450 | */ |
| 451 | long |
| 452 | stp_xmlstrtol(const char *textval) |
| 453 | { |
| 454 | long val; /* The value to return */ |
| 455 | val = strtol(textval, (char **)NULL, 0); |
| 456 | |
| 457 | return val; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Convert a text string into an unsigned int. |
| 462 | */ |
| 463 | unsigned long |
| 464 | stp_xmlstrtoul(const char *textval) |
| 465 | { |
| 466 | unsigned long val; /* The value to return */ |
| 467 | val = strtoul(textval, (char **)NULL, 0); |
| 468 | |
| 469 | return val; |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | * Convert a text string into a double. |
| 474 | */ |
| 475 | double |
| 476 | stp_xmlstrtod(const char *textval) |
| 477 | { |
| 478 | double val; /* The value to return */ |
| 479 | val = strtod(textval, (char **)NULL); |
| 480 | |
| 481 | return val; |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * Convert a text string into a dimension. |
| 486 | */ |
| 487 | stp_dimension_t |
| 488 | stp_xmlstrtodim(const char *textval) |
| 489 | { |
| 490 | double val; /* The value to return */ |
| 491 | val = (stp_dimension_t) strtod(textval, (char **)NULL); |
| 492 | |
| 493 | return val; |
| 494 | } |
| 495 | |
| 496 | /* |
| 497 | * Convert an encoded text string into a raw. |
| 498 | */ |
| 499 | stp_raw_t * |
| 500 | stp_xmlstrtoraw(const char *textval) |
| 501 | { |
| 502 | size_t tcount; |
| 503 | stp_raw_t *raw; |
| 504 | unsigned char *answer; |
| 505 | unsigned char *aptr; |
| 506 | if (! textval || *textval == 0) |
| 507 | return NULL; |
| 508 | tcount = strlen(textval); |
| 509 | raw = stp_zalloc(sizeof(stp_raw_t)); |
| 510 | answer = stp_malloc(tcount + 1); /* Worst case -- we may not need it all */ |
| 511 | aptr = answer; |
| 512 | raw->data = answer; |
| 513 | while (*textval) |
| 514 | { |
| 515 | if (*textval != '\\') |
| 516 | { |
| 517 | *aptr++ = *textval++; |
| 518 | raw->bytes++; |
| 519 | } |
| 520 | else |
| 521 | { |
| 522 | textval++; |
| 523 | if (textval[0] >= '0' && textval[0] <= '3' && |
| 524 | textval[1] >= '0' && textval[1] <= '7' && |
| 525 | textval[2] >= '0' && textval[2] <= '7') |
| 526 | { |
| 527 | *aptr++ = (((textval[0] - '0') << 6) + |
| 528 | ((textval[1] - '0') << 3) + |
| 529 | ((textval[2] - '0') << 0)); |
| 530 | raw->bytes++; |
| 531 | textval += 3; |
| 532 | } |
| 533 | else if (textval[0] == '\0' || textval[1] == '\0' || textval[2] == '\0') |
| 534 | break; |
| 535 | else |
| 536 | textval += 3; |
| 537 | } |
| 538 | } |
| 539 | *aptr = '\0'; |
| 540 | return raw; |
| 541 | } |
| 542 | |
| 543 | char * |
| 544 | stp_rawtoxmlstr(const stp_raw_t *raw) |
| 545 | { |
| 546 | if (raw && raw->bytes > 0) |
| 547 | { |
| 548 | unsigned int i; |
| 549 | const unsigned char *data = (const unsigned char *) (raw->data); |
| 550 | char *answer = stp_malloc((raw->bytes * 4) + 1); /* \012 */ |
| 551 | unsigned char *aptr = (unsigned char *) answer; |
| 552 | for (i = 0; i < raw->bytes; i++) |
| 553 | { |
| 554 | if (data[i] > ' ' && data[i] < '\177' && data[i] != '\\' && |
| 555 | data[i] != '<' && data[i] != '>' && data[i] != '&') |
| 556 | *aptr++ = data[i]; |
| 557 | else |
| 558 | { |
| 559 | *aptr++ = '\\'; |
| 560 | *aptr++ = '0' + ((data[i] & '\300') >> 6); |
| 561 | *aptr++ = '0' + ((data[i] & '\070') >> 3); |
| 562 | *aptr++ = '0' + ((data[i] & '\007') >> 0); |
| 563 | } |
| 564 | } |
| 565 | *aptr = '\0'; |
| 566 | return answer; |
| 567 | } |
| 568 | return NULL; |
| 569 | } |
| 570 | |
| 571 | char * |
| 572 | stp_strtoxmlstr(const char *str) |
| 573 | { |
| 574 | if (str && strlen(str) > 0) |
| 575 | { |
| 576 | unsigned int i; |
| 577 | unsigned int bytes = strlen(str); |
| 578 | const unsigned char *data = (const unsigned char *) (str); |
| 579 | char *answer = stp_malloc((bytes * 4) + 1); /* "\012" is worst case */ |
| 580 | unsigned char *aptr = (unsigned char *) answer; |
| 581 | for (i = 0; i < bytes; i++) |
| 582 | { |
| 583 | if (data[i] > ' ' && data[i] < '\177' && data[i] != '\\' && |
| 584 | data[i] != '<' && data[i] != '>' && data[i] != '&') |
| 585 | *aptr++ = data[i]; |
| 586 | else |
| 587 | { |
| 588 | *aptr++ = '\\'; |
| 589 | *aptr++ = '0' + ((data[i] & '\300') >> 6); |
| 590 | *aptr++ = '0' + ((data[i] & '\070') >> 3); |
| 591 | *aptr++ = '0' + ((data[i] & '\007') >> 0); |
| 592 | } |
| 593 | } |
| 594 | *aptr = '\0'; |
| 595 | return answer; |
| 596 | } |
| 597 | return NULL; |
| 598 | } |
| 599 | |
| 600 | void |
| 601 | stp_prtraw(const stp_raw_t *raw, FILE *fp) |
| 602 | { |
| 603 | if (raw && raw->bytes > 0) |
| 604 | { |
| 605 | unsigned int i; |
| 606 | const unsigned char *data = (const unsigned char *) (raw->data); |
| 607 | for (i = 0; i < raw->bytes; i++) |
| 608 | { |
| 609 | if (data[i] > ' ' && data[i] < '\177' && data[i] != '\\' && |
| 610 | data[i] != '<' && data[i] != '>' && data[i] != '&') |
| 611 | fputc(data[i], fp); |
| 612 | else |
| 613 | { |
| 614 | fputc('\\', fp); |
| 615 | fputc('0' + ((data[i] & '\300') >> 6), fp); |
| 616 | fputc('0' + ((data[i] & '\070') >> 3), fp); |
| 617 | fputc('0' + ((data[i] & '\007') >> 0), fp); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | |
| 624 | /* |
| 625 | * Find a node in an XML tree. This function takes an xmlNodePtr, |
| 626 | * followed by a NULL-terminated list of nodes which are required. |
| 627 | * For example stp_xml_get_node(myroot, "gutenprint", "dither") will |
| 628 | * return the first dither node in the tree. Additional dither nodes |
| 629 | * cannot be accessed with this function. |
| 630 | */ |
| 631 | stp_mxml_node_t * |
| 632 | stp_xml_get_node(stp_mxml_node_t *xmlroot, ...) |
| 633 | { |
| 634 | stp_mxml_node_t *child; |
| 635 | va_list ap; |
| 636 | const char *target = NULL; |
| 637 | |
| 638 | va_start(ap, xmlroot); |
| 639 | |
| 640 | child = xmlroot; |
| 641 | target = va_arg(ap, const char *); |
| 642 | |
| 643 | stp_xml_init(); |
| 644 | while (target && child) |
| 645 | { |
| 646 | child = stp_mxmlFindElement(child, child, target, NULL, NULL, |
| 647 | STP_MXML_DESCEND); |
| 648 | target = va_arg(ap, const char *); |
| 649 | } |
| 650 | stp_xml_exit(); |
| 651 | va_end(ap); |
| 652 | return child; |
| 653 | } |
| 654 | |
| 655 | static void |
| 656 | stpi_xml_process_node(stp_mxml_node_t *node, const char *file) |
| 657 | { |
| 658 | stp_list_item_t *item = |
| 659 | stp_list_get_item_by_name(stpi_xml_registry, node->value.element.name); |
| 660 | if (item) |
| 661 | { |
| 662 | stpi_xml_parse_registry *xmlp = |
| 663 | (stpi_xml_parse_registry *) stp_list_item_get_data(item); |
| 664 | (xmlp->parse_func)(node, file); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * Parse the <gutenprint> root node. |
| 670 | */ |
| 671 | static void |
| 672 | stpi_xml_process_gutenprint(stp_mxml_node_t *cur, const char *file) /* The node to parse */ |
| 673 | { |
| 674 | stp_mxml_node_t *child; /* Child node pointer */ |
| 675 | |
| 676 | child = cur->child; |
| 677 | while (child) |
| 678 | { |
| 679 | /* process nodes with corresponding parser */ |
| 680 | if (child->type == STP_MXML_ELEMENT) |
| 681 | stpi_xml_process_node(child, file); |
| 682 | child = child->next; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Create a basic gutenprint XML document tree root |
| 688 | */ |
| 689 | stp_mxml_node_t * |
| 690 | stp_xmldoc_create_generic(void) |
| 691 | { |
| 692 | stp_mxml_node_t *doc; |
| 693 | stp_mxml_node_t *rootnode; |
| 694 | |
| 695 | stp_xml_init(); |
| 696 | /* Create the XML tree */ |
| 697 | doc = stp_mxmlNewElement(NULL, "?xml"); |
| 698 | stp_mxmlElementSetAttr(doc, "version", "1.0"); |
| 699 | |
| 700 | rootnode = stp_mxmlNewElement(doc, "gutenprint"); |
| 701 | stp_mxmlElementSetAttr |
| 702 | (rootnode, "xmlns", "http://gimp-print.sourceforge.net/xsd/gp.xsd-1.0"); |
| 703 | stp_mxmlElementSetAttr |
| 704 | (rootnode, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); |
| 705 | stp_mxmlElementSetAttr |
| 706 | (rootnode, "xsi:schemaLocation", |
| 707 | "http://gimp-print.sourceforge.net/xsd/gp.xsd-1.0 gutenprint.xsd"); |
| 708 | stp_xml_exit(); |
| 709 | |
| 710 | return doc; |
| 711 | } |
| 712 | |
| 713 | void |
| 714 | stpi_print_xml_node(stp_mxml_node_t *node) |
| 715 | { |
| 716 | int i; |
| 717 | stp_erprintf("Node @%p:\n", (void *) node); |
| 718 | stp_erprintf(" Type %d\n", node->type); |
| 719 | stp_erprintf(" Next @%p\n", (void *) node->next); |
| 720 | stp_erprintf(" Prev @%p\n", (void *) node->prev); |
| 721 | stp_erprintf(" Parent @%p\n", (void *) node->parent); |
| 722 | stp_erprintf(" Child @%p\n", (void *) node->child); |
| 723 | stp_erprintf(" Last @%p\n", (void *) node->last_child); |
| 724 | stp_erprintf(" Value: "); |
| 725 | switch (node->type) |
| 726 | { |
| 727 | case STP_MXML_ELEMENT: |
| 728 | stp_erprintf("\n Element, name: %s\n", node->value.element.name); |
| 729 | stp_erprintf(" Attrs: %d\n", node->value.element.num_attrs); |
| 730 | for (i = 0; i < node->value.element.num_attrs; i++) |
| 731 | stp_erprintf(" %s => %s\n", |
| 732 | node->value.element.attrs[i].name, |
| 733 | node->value.element.attrs[i].value); |
| 734 | break; |
| 735 | case STP_MXML_INTEGER: |
| 736 | stp_erprintf(" Integer: %d\n", node->value.integer); |
| 737 | break; |
| 738 | case STP_MXML_REAL: |
| 739 | stp_erprintf(" Real: %f\n", node->value.real); |
| 740 | break; |
| 741 | case STP_MXML_DIMENSION: |
| 742 | stp_erprintf(" Dimension: %f\n", node->value.real); |
| 743 | break; |
| 744 | case STP_MXML_OPAQUE: |
| 745 | stp_erprintf(" Opaque: '%s'\n", node->value.opaque); |
| 746 | break; |
| 747 | case STP_MXML_TEXT: |
| 748 | stp_erprintf(" Text: %d '%s'\n", node->value.text.whitespace, |
| 749 | node->value.text.string); |
| 750 | break; |
| 751 | default: |
| 752 | stp_erprintf("UNKNOWN!\n"); |
| 753 | } |
| 754 | } |