| 1 | /* |
| 2 | * "xmlppd.c" |
| 3 | * |
| 4 | * PPD to XML converter. |
| 5 | * |
| 6 | * Copyright 2007 by Michael R Sweet and Robert Krawitz |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 21 | * 02110-1301 USA |
| 22 | */ |
| 23 | |
| 24 | #include <ctype.h> |
| 25 | #include <stdio.h> |
| 26 | #include <gutenprint/mxml.h> |
| 27 | #include <gutenprint/util.h> |
| 28 | #include <gutenprint/string-list.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <strings.h> |
| 31 | #include "xmlppd.h" |
| 32 | |
| 33 | typedef struct |
| 34 | { |
| 35 | int order; |
| 36 | const char *name; |
| 37 | } order_t; |
| 38 | |
| 39 | static int |
| 40 | order_compare(const void *a, const void *b) |
| 41 | { |
| 42 | const order_t *aa = (const order_t *)a; |
| 43 | const order_t *bb = (const order_t *)b; |
| 44 | if (aa->order < bb->order) |
| 45 | return -1; |
| 46 | else if (aa->order > bb->order) |
| 47 | return 1; |
| 48 | else |
| 49 | return strcmp(aa->name, bb->name); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | static stp_mxml_node_t * |
| 54 | find_element_named(stp_mxml_node_t *root, const char *name, const char *what) |
| 55 | { |
| 56 | stp_mxml_node_t *element; |
| 57 | if (root && name) |
| 58 | { |
| 59 | for (element = stp_mxmlFindElement(root, root, what, NULL, NULL, |
| 60 | STP_MXML_DESCEND); |
| 61 | element; |
| 62 | element = stp_mxmlFindElement(element, root, what, NULL, NULL, |
| 63 | STP_MXML_DESCEND)) |
| 64 | { |
| 65 | if (!strcmp(stp_mxmlElementGetAttr(element, "name"), name)) |
| 66 | return element; |
| 67 | } |
| 68 | } |
| 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | static stp_mxml_node_t * |
| 73 | find_element_index(stp_mxml_node_t *root, int idx, const char *what) |
| 74 | { |
| 75 | stp_mxml_node_t *element; |
| 76 | int count = 0; |
| 77 | if (root && idx >= 0) |
| 78 | { |
| 79 | for (element = stp_mxmlFindElement(root, root, what, NULL, NULL, |
| 80 | STP_MXML_DESCEND); |
| 81 | element; |
| 82 | element = stp_mxmlFindElement(element, root, what, NULL, NULL, |
| 83 | STP_MXML_DESCEND)) |
| 84 | { |
| 85 | if (count++ == idx) |
| 86 | return element; |
| 87 | } |
| 88 | } |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | static size_t |
| 93 | find_element_count(stp_mxml_node_t *root, const char *what) |
| 94 | { |
| 95 | stp_mxml_node_t *element; |
| 96 | size_t count = 0; |
| 97 | if (root) |
| 98 | { |
| 99 | for (element = stp_mxmlFindElement(root, root, what, NULL, NULL, |
| 100 | STP_MXML_DESCEND); |
| 101 | element; |
| 102 | element = stp_mxmlFindElement(element, root, what, NULL, NULL, |
| 103 | STP_MXML_DESCEND)) |
| 104 | count++; |
| 105 | } |
| 106 | return count; |
| 107 | } |
| 108 | |
| 109 | stp_mxml_node_t * |
| 110 | stpi_xmlppd_find_group_named(stp_mxml_node_t *root, const char *name) |
| 111 | { |
| 112 | return find_element_named(root, name, "group"); |
| 113 | } |
| 114 | |
| 115 | stp_mxml_node_t * |
| 116 | stpi_xmlppd_find_group_index(stp_mxml_node_t *root, int idx) |
| 117 | { |
| 118 | return find_element_index(root, idx, "group"); |
| 119 | } |
| 120 | |
| 121 | int |
| 122 | stpi_xmlppd_find_group_count(stp_mxml_node_t *root) |
| 123 | { |
| 124 | return find_element_count(root, "group"); |
| 125 | } |
| 126 | |
| 127 | stp_mxml_node_t * |
| 128 | stpi_xmlppd_find_option_named(stp_mxml_node_t *root, const char *name) |
| 129 | { |
| 130 | return find_element_named(root, name, "option"); |
| 131 | } |
| 132 | |
| 133 | stp_mxml_node_t * |
| 134 | stpi_xmlppd_find_option_index(stp_mxml_node_t *root, int idx) |
| 135 | { |
| 136 | return find_element_index(root, idx, "option"); |
| 137 | } |
| 138 | |
| 139 | int |
| 140 | stpi_xmlppd_find_option_count(stp_mxml_node_t *root) |
| 141 | { |
| 142 | return find_element_count(root, "option"); |
| 143 | } |
| 144 | |
| 145 | stp_mxml_node_t * |
| 146 | stpi_xmlppd_find_choice_named(stp_mxml_node_t *option, const char *name) |
| 147 | { |
| 148 | return find_element_named(option, name, "choice"); |
| 149 | } |
| 150 | |
| 151 | stp_mxml_node_t * |
| 152 | stpi_xmlppd_find_choice_index(stp_mxml_node_t *option, int idx) |
| 153 | { |
| 154 | return find_element_index(option, idx, "choice"); |
| 155 | } |
| 156 | |
| 157 | int |
| 158 | stpi_xmlppd_find_choice_count(stp_mxml_node_t *option) |
| 159 | { |
| 160 | return find_element_count(option, "choice"); |
| 161 | } |
| 162 | |
| 163 | stp_mxml_node_t * |
| 164 | stpi_xmlppd_find_page_size(stp_mxml_node_t *root, const char *name) |
| 165 | { |
| 166 | return stpi_xmlppd_find_choice_named(stpi_xmlppd_find_option_named(root, "PageSize"), name); |
| 167 | } |
| 168 | |
| 169 | static void |
| 170 | parse_values(const char **data, int limit, char *value) |
| 171 | { |
| 172 | int dptr = 0; |
| 173 | char *where = value; |
| 174 | char *end = value; |
| 175 | for (dptr = 0; dptr < limit; dptr++) |
| 176 | data[dptr] = NULL; |
| 177 | for (dptr = 0; *where && dptr < limit; dptr++) |
| 178 | { |
| 179 | where = end; |
| 180 | while (*where && isspace(*where)) |
| 181 | where++; |
| 182 | end = where; |
| 183 | while (*end && !isspace(*end)) |
| 184 | end++; |
| 185 | *end++ = '\0'; |
| 186 | data[dptr] = where; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * 'read_ppd_file()' - Read a PPD file into XML data. |
| 192 | */ |
| 193 | |
| 194 | stp_mxml_node_t * /* O - PPD file as XML */ |
| 195 | stpi_xmlppd_read_ppd_file(const char *filename) /* I - PPD file */ |
| 196 | { |
| 197 | stp_mxml_node_t *ppd, /* Root node of "ppd" group */ |
| 198 | *group, /* Current group */ |
| 199 | *option, /* Current option */ |
| 200 | *choice; /* Current choice */ |
| 201 | FILE *fp; /* PPD file */ |
| 202 | int ch, /* Current character */ |
| 203 | sawcolon, /* Saw a colon? */ |
| 204 | inquote, /* In a quoted string? */ |
| 205 | num_choices = 0; |
| 206 | char buffer[32768], /* Line buffer */ |
| 207 | *bufptr, /* Pointer into line */ |
| 208 | *xptr, /* Pointer into line */ |
| 209 | option_name[42], /* Option name */ |
| 210 | stp_option_data_name[64], /* Option name */ |
| 211 | *keyword, /* Pointer to option keyword */ |
| 212 | *text, /* Pointer to text */ |
| 213 | *value; /* Pointer to value */ |
| 214 | order_t *order_array; /* Precedence order of options */ |
| 215 | unsigned int i; |
| 216 | unsigned int option_count; |
| 217 | int order_length; |
| 218 | char *order_list; |
| 219 | stp_string_list_t *ialist = stp_string_list_create(); |
| 220 | stp_string_list_t *pdlist = stp_string_list_create(); |
| 221 | |
| 222 | |
| 223 | /* |
| 224 | * Open the file... |
| 225 | */ |
| 226 | |
| 227 | if ((fp = fopen(filename, "rb")) == NULL) |
| 228 | { |
| 229 | perror(filename); |
| 230 | return (NULL); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Create the base PPD file tree; the completed tree will look like: |
| 235 | * |
| 236 | * <?xml version="1.0"?> |
| 237 | * <ppd color="0/1" level="1/2/3"> |
| 238 | * <option name="..." text="..." default="..." section="..." order="..." |
| 239 | * type="..."> |
| 240 | * <choice name="..." text="...">code</choice> |
| 241 | * ... |
| 242 | * </option> |
| 243 | * ... |
| 244 | * <group name="..." text="..."> |
| 245 | * <option ...> |
| 246 | * <choice ...>code</choice> |
| 247 | * </option> |
| 248 | * </group> |
| 249 | * </ppd> |
| 250 | */ |
| 251 | |
| 252 | /* xml = stp_mxmlNewXML("1.0"); */ |
| 253 | /* ppd = stp_mxmlNewElement(xml, "ppd"); */ |
| 254 | ppd = stp_mxmlNewElement(STP_MXML_NO_PARENT, "ppd"); |
| 255 | |
| 256 | stp_mxmlElementSetAttr(ppd, "color", "0"); |
| 257 | stp_mxmlElementSetAttr(ppd, "level", "2"); |
| 258 | |
| 259 | /* |
| 260 | * Read all of the lines of the form: |
| 261 | * |
| 262 | * *% comment |
| 263 | * *Keyword: value |
| 264 | * *Keyword OptionKeyword: value |
| 265 | * *Keyword OptionKeyword/Text: value |
| 266 | * |
| 267 | * Only save groups, options, and choices, along with specific metadata |
| 268 | * from the file... |
| 269 | */ |
| 270 | |
| 271 | group = NULL; |
| 272 | option = NULL; |
| 273 | option_name[0] = '\0'; |
| 274 | |
| 275 | while ((ch = getc(fp)) != EOF) |
| 276 | { |
| 277 | /* |
| 278 | * Read the line... |
| 279 | */ |
| 280 | |
| 281 | buffer[0] = ch; |
| 282 | bufptr = buffer + 1; |
| 283 | inquote = 0; |
| 284 | sawcolon = 0; |
| 285 | if (ch == '*') |
| 286 | { |
| 287 | if ((ch = getc(fp)) == '%') |
| 288 | { |
| 289 | while (1) |
| 290 | { |
| 291 | ch = getc(fp); |
| 292 | if (ch == '\n' || ch == EOF) |
| 293 | break; |
| 294 | } |
| 295 | continue; |
| 296 | } |
| 297 | else |
| 298 | ungetc(ch, fp); |
| 299 | } |
| 300 | |
| 301 | while ((ch != '\n' || inquote) && bufptr < (buffer + sizeof(buffer) - 1)) |
| 302 | { |
| 303 | if ((ch = getc(fp)) == '\r') |
| 304 | { |
| 305 | if ((ch = getc(fp)) != '\n') |
| 306 | ungetc(ch, fp); |
| 307 | } |
| 308 | |
| 309 | *bufptr++ = ch; |
| 310 | |
| 311 | if (ch == ':' && !sawcolon) |
| 312 | sawcolon = 1; |
| 313 | else if (ch == '\"' && sawcolon) |
| 314 | inquote = !inquote; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Strip trailing whitespace... |
| 319 | */ |
| 320 | |
| 321 | for (bufptr --; bufptr > buffer && isspace(bufptr[-1] & 255); bufptr --); |
| 322 | |
| 323 | *bufptr = '\0'; |
| 324 | |
| 325 | /* |
| 326 | * Now parse it... |
| 327 | */ |
| 328 | |
| 329 | if (!strncmp(buffer, "*%", 2) || !buffer[0]) |
| 330 | continue; |
| 331 | |
| 332 | if ((value = strchr(buffer, ':')) == NULL) |
| 333 | continue; |
| 334 | |
| 335 | for (*value++ = '\0'; |
| 336 | *value && (isspace(*value & 255) || (*value & 255) == '"'); |
| 337 | value ++); |
| 338 | for (xptr = value; |
| 339 | *xptr && (*xptr & 255) != '"'; |
| 340 | xptr ++); |
| 341 | if (*xptr == '"') |
| 342 | *xptr = '\0'; |
| 343 | |
| 344 | for (keyword = buffer; *keyword && !isspace(*keyword & 255); keyword ++); |
| 345 | |
| 346 | while (isspace(*keyword & 255)) |
| 347 | *keyword++ = '\0'; |
| 348 | |
| 349 | if ((text = strchr(keyword, '/')) != NULL) |
| 350 | *text++ = '\0'; |
| 351 | |
| 352 | /* |
| 353 | * And then use the parsed values... |
| 354 | */ |
| 355 | |
| 356 | if (!strcmp(buffer, "*ColorDevice")) |
| 357 | { |
| 358 | /* |
| 359 | * Color support... |
| 360 | */ |
| 361 | |
| 362 | if (!strcasecmp(value, "true")) |
| 363 | stp_mxmlElementSetAttr(ppd, "color", "1"); |
| 364 | else |
| 365 | stp_mxmlElementSetAttr(ppd, "color", "0"); |
| 366 | } |
| 367 | else if (!strcmp(buffer, "*LanguageLevel") && atoi(value) > 0) |
| 368 | { |
| 369 | /* |
| 370 | * PostScript language level... |
| 371 | */ |
| 372 | |
| 373 | stp_mxmlElementSetAttr(ppd, "level", value); |
| 374 | } |
| 375 | else if (!strcmp(buffer, "*StpDriverName")) |
| 376 | stp_mxmlElementSetAttr(ppd, "driver", value); |
| 377 | else if (!strcmp(buffer, "*ModelName")) |
| 378 | stp_mxmlElementSetAttr(ppd, "modelname", value); |
| 379 | else if (!strcmp(buffer, "*ShortNickName")) |
| 380 | stp_mxmlElementSetAttr(ppd, "shortnickname", value); |
| 381 | else if (!strcmp(buffer, "*NickName")) |
| 382 | stp_mxmlElementSetAttr(ppd, "nickname", value); |
| 383 | else if (!strcmp(buffer, "*OpenGroup")) |
| 384 | { |
| 385 | if ((text = strchr(value, '/')) != NULL) |
| 386 | *text++ = '\0'; |
| 387 | |
| 388 | group = stp_mxmlNewElement(ppd, "group"); |
| 389 | stp_mxmlElementSetAttr(group, "name", value); |
| 390 | stp_mxmlElementSetAttr(group, "text", text ? text : value); |
| 391 | } |
| 392 | else if (!strcmp(buffer, "*CloseGroup")) |
| 393 | group = NULL; |
| 394 | else if ((!strcmp(buffer, "*OpenUI") || !strcmp(buffer, "*JCLOpenUI")) && |
| 395 | keyword[0] == '*' && keyword[1]) |
| 396 | { |
| 397 | /* |
| 398 | * Start a new option... |
| 399 | */ |
| 400 | |
| 401 | option = stp_mxmlNewElement(group ? group : ppd, "option"); |
| 402 | stp_mxmlElementSetAttr(option, "name", keyword + 1); |
| 403 | stp_mxmlElementSetAttr(option, "text", text ? text : keyword + 1); |
| 404 | stp_mxmlElementSetAttr(option, "ui", value); |
| 405 | |
| 406 | strncpy(option_name, keyword, sizeof(option_name) - 1); |
| 407 | option_name[sizeof(option_name) - 1] = '\0'; |
| 408 | strcpy(stp_option_data_name, "*Stp"); |
| 409 | strcpy(stp_option_data_name + 4, option_name + 1); |
| 410 | if (group) |
| 411 | { |
| 412 | stp_mxmlElementSetAttr(option, "groupname", stp_mxmlElementGetAttr(group, "name")); |
| 413 | stp_mxmlElementSetAttr(option, "grouptext", stp_mxmlElementGetAttr(group, "text")); |
| 414 | } |
| 415 | num_choices = 0; |
| 416 | } |
| 417 | else if (option && !strcmp(buffer, stp_option_data_name)) |
| 418 | { |
| 419 | const char *data[8]; |
| 420 | parse_values(data, 8, value); |
| 421 | if (data[7]) |
| 422 | { |
| 423 | stp_mxmlElementSetAttr(option, "stptype", data[0]); |
| 424 | stp_mxmlElementSetAttr(option, "stpmandatory", data[1]); |
| 425 | stp_mxmlElementSetAttr(option, "stpclass", data[2]); |
| 426 | stp_mxmlElementSetAttr(option, "stplevel", data[3]); |
| 427 | stp_mxmlElementSetAttr(option, "stpchannel", data[4]); |
| 428 | stp_mxmlElementSetAttr(option, "stplower", data[5]); |
| 429 | stp_mxmlElementSetAttr(option, "stpupper", data[6]); |
| 430 | stp_mxmlElementSetAttr(option, "stpdefault", data[7]); |
| 431 | stp_mxmlElementSetAttr(option, "stpname", stp_option_data_name + 7); |
| 432 | } |
| 433 | } |
| 434 | else if (!strcmp(buffer, "*OrderDependency") && option) |
| 435 | { |
| 436 | /* |
| 437 | * Get order and section for option |
| 438 | */ |
| 439 | |
| 440 | char order[256], /* Order number */ |
| 441 | section[256]; /* Section name */ |
| 442 | |
| 443 | |
| 444 | if (sscanf(value, "%255s%255s", order, section) == 2) |
| 445 | { |
| 446 | stp_mxmlElementSetAttr(option, "order", order); |
| 447 | stp_mxmlElementSetAttr(option, "section", section); |
| 448 | } |
| 449 | } |
| 450 | else if (!strncmp(buffer, "*Default", 8) && option && |
| 451 | !strcmp(buffer + 8, option_name + 1)) |
| 452 | stp_mxmlElementSetAttr(option, "default", value); |
| 453 | else if (!strcmp(buffer, "*CloseUI") || !strcmp(buffer, "*JCLCloseUI")) |
| 454 | { |
| 455 | char buf[64]; |
| 456 | (void) sprintf(buf, "%d", num_choices); |
| 457 | stp_mxmlElementSetAttr(option, "num_choices", buf); |
| 458 | option = NULL; |
| 459 | stp_option_data_name[0] = '\0'; |
| 460 | } |
| 461 | else if (option && !strcmp(buffer, option_name)) |
| 462 | { |
| 463 | /* |
| 464 | * A choice... |
| 465 | */ |
| 466 | |
| 467 | choice = stp_mxmlNewElement(option, "choice"); |
| 468 | stp_mxmlElementSetAttr(choice, "name", keyword); |
| 469 | stp_mxmlElementSetAttr(choice, "text", text ? text : keyword); |
| 470 | |
| 471 | if (value[0] == '\"') |
| 472 | value ++; |
| 473 | |
| 474 | if (bufptr > buffer && bufptr[-1] == '\"') |
| 475 | { |
| 476 | bufptr --; |
| 477 | *bufptr = '\0'; |
| 478 | } |
| 479 | |
| 480 | stp_mxmlNewOpaque(choice, value); |
| 481 | num_choices++; |
| 482 | } |
| 483 | else if (!option && !strcmp(buffer, "*ImageableArea")) |
| 484 | { |
| 485 | stp_string_list_add_string(ialist, keyword, value); |
| 486 | } |
| 487 | else if (!option && !strcmp(buffer, "*PaperDimension")) |
| 488 | { |
| 489 | stp_string_list_add_string(pdlist, keyword, value); |
| 490 | } |
| 491 | } |
| 492 | if (option) |
| 493 | { |
| 494 | char buf[64]; |
| 495 | (void) sprintf(buf, "%d", num_choices); |
| 496 | stp_mxmlElementSetAttr(option, "num_choices", buf); |
| 497 | option = NULL; |
| 498 | stp_option_data_name[0] = '\0'; |
| 499 | } |
| 500 | |
| 501 | for (i = 0; i < stp_string_list_count(ialist); i++) |
| 502 | { |
| 503 | stp_param_string_t *pstr = stp_string_list_param(ialist, i); |
| 504 | stp_mxml_node_t *psize = stpi_xmlppd_find_page_size(ppd, pstr->name); |
| 505 | if (psize) |
| 506 | { |
| 507 | const char *data[4]; |
| 508 | value = stp_strdup(pstr->text); |
| 509 | parse_values(data, 4, value); |
| 510 | if (data[3]) |
| 511 | { |
| 512 | stp_mxmlElementSetAttr(psize, "left", data[0]); |
| 513 | stp_mxmlElementSetAttr(psize, "bottom", data[1]); |
| 514 | stp_mxmlElementSetAttr(psize, "right", data[2]); |
| 515 | stp_mxmlElementSetAttr(psize, "top", data[3]); |
| 516 | } |
| 517 | stp_free(value); |
| 518 | } |
| 519 | } |
| 520 | stp_string_list_destroy(ialist); |
| 521 | for (i = 0; i < stp_string_list_count(pdlist); i++) |
| 522 | { |
| 523 | stp_param_string_t *pstr = stp_string_list_param(pdlist, i); |
| 524 | stp_mxml_node_t *psize = stpi_xmlppd_find_page_size(ppd, pstr->name); |
| 525 | if (psize) |
| 526 | { |
| 527 | const char *data[2]; |
| 528 | value = stp_strdup(pstr->text); |
| 529 | parse_values(data, 2, value); |
| 530 | if (data[1]) |
| 531 | { |
| 532 | stp_mxmlElementSetAttr(psize, "width", data[0]); |
| 533 | stp_mxmlElementSetAttr(psize, "height", data[1]); |
| 534 | } |
| 535 | stp_free(value); |
| 536 | } |
| 537 | } |
| 538 | stp_string_list_destroy(pdlist); |
| 539 | option_count = stpi_xmlppd_find_option_count(ppd); |
| 540 | order_length = 1; /* Terminating null */ |
| 541 | order_array = malloc(sizeof(order_t) * option_count); |
| 542 | i = 0; |
| 543 | for (option = stp_mxmlFindElement(ppd, ppd, "option", NULL, NULL, |
| 544 | STP_MXML_DESCEND); |
| 545 | option && i < option_count; |
| 546 | option = stp_mxmlFindElement(option, ppd, "option", NULL, NULL, |
| 547 | STP_MXML_DESCEND)) |
| 548 | { |
| 549 | if (stp_mxmlElementGetAttr(option, "order")) |
| 550 | { |
| 551 | order_array[i].name = stp_mxmlElementGetAttr(option, "name"); |
| 552 | order_length += strlen(order_array[i].name) + 1; |
| 553 | order_array[i].order = atoi(stp_mxmlElementGetAttr(option, "order")); |
| 554 | i++; |
| 555 | } |
| 556 | } |
| 557 | option_count = i; |
| 558 | qsort(order_array, option_count, sizeof(order_t), &order_compare); |
| 559 | order_list = malloc(order_length); |
| 560 | order_length = 0; |
| 561 | for (i = 0; i < option_count; i++) |
| 562 | { |
| 563 | if (i > 0) |
| 564 | order_list[order_length++] = ' '; |
| 565 | strcpy(order_list + order_length, order_array[i].name); |
| 566 | order_length += strlen(order_array[i].name); |
| 567 | } |
| 568 | stp_mxmlElementSetAttr(ppd, "optionorder", order_list); |
| 569 | free(order_list); |
| 570 | free(order_array); |
| 571 | return (ppd); |
| 572 | } |
| 573 | |
| 574 | /* |
| 575 | * End of "xmlppd.c". |
| 576 | */ |