1 /* PostScript Printer Description (PPD) file parser
3 * See http://www.adobe.com/supportservice/devrelations/PDFS/TN/5003.PPD_Spec_v4.3.pdf
5 * Copyright 1998 Huw D M Davies
11 #include "winnt.h" /* HEAP_ZERO_MEMORY */
13 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(psdrv);
28 /* map of page names in ppd file to Windows paper constants */
34 {"10x11", DMPAPER_10X11},
35 {"10x14", DMPAPER_10X14},
36 {"12x11", DMPAPER_12X11},
37 {"15x11", DMPAPER_15X11},
38 {"9x11", DMPAPER_9X11},
41 {"A3.Transverse", DMPAPER_A3_TRANSVERSE},
42 {"A3Extra", DMPAPER_A3_EXTRA},
43 {"A3Extra.Transverse", DMPAPER_A3_EXTRA_TRANSVERSE},
44 {"A3Rotated", DMPAPER_A3_ROTATED},
46 {"A4.Transverse", DMPAPER_A4_TRANSVERSE},
47 {"A4Extra", DMPAPER_A4_EXTRA},
48 {"A4Plus", DMPAPER_A4_PLUS},
49 {"A4Rotated", DMPAPER_A4_ROTATED},
50 {"A4Small", DMPAPER_A4SMALL},
52 {"A5.Transverse", DMPAPER_A5_TRANSVERSE},
53 {"A5Extra", DMPAPER_A5_EXTRA},
54 {"A5Rotated", DMPAPER_A5_ROTATED},
56 {"A6Rotated", DMPAPER_A6_ROTATED},
57 {"ARCHC", DMPAPER_CSHEET},
58 {"ARCHD", DMPAPER_DSHEET},
59 {"ARCHE", DMPAPER_ESHEET},
61 {"B4Rotated", DMPAPER_B4_JIS_ROTATED},
63 {"B5.Transverse", DMPAPER_B5_TRANSVERSE},
64 {"B5Rotated", DMPAPER_B5_JIS_ROTATED},
65 {"B6", DMPAPER_B6_JIS},
66 {"B6Rotated", DMPAPER_B6_JIS_ROTATED},
67 {"C4", DMPAPER_ENV_C4},
68 {"C5", DMPAPER_ENV_C5},
69 {"C6", DMPAPER_ENV_C6},
70 {"Comm10", DMPAPER_ENV_10},
71 {"DL", DMPAPER_ENV_DL},
72 {"DoublePostcard", DMPAPER_DBL_JAPANESE_POSTCARD},
73 {"DoublePostcardRotated", DMPAPER_DBL_JAPANESE_POSTCARD_ROTATED},
74 {"Env9", DMPAPER_ENV_9},
75 {"Env10", DMPAPER_ENV_10},
76 {"Env11", DMPAPER_ENV_11},
77 {"Env12", DMPAPER_ENV_12},
78 {"Env14", DMPAPER_ENV_14},
79 {"EnvC3", DMPAPER_ENV_C3},
80 {"EnvC4", DMPAPER_ENV_C4},
81 {"EnvC5", DMPAPER_ENV_C5},
82 {"EnvC6", DMPAPER_ENV_C6},
83 {"EnvC65", DMPAPER_ENV_C65},
84 {"EnvChou3", DMPAPER_JENV_CHOU3},
85 {"EnvChou3Rotated", DMPAPER_JENV_CHOU3_ROTATED},
86 {"EnvChou4", DMPAPER_JENV_CHOU4},
87 {"EnvChou4Rotated", DMPAPER_JENV_CHOU4_ROTATED},
88 {"EnvDL", DMPAPER_ENV_DL},
89 {"EnvInvite", DMPAPER_ENV_INVITE},
90 {"EnvISOB4", DMPAPER_ENV_B4},
91 {"EnvISOB5", DMPAPER_ENV_B5},
92 {"EnvISOB6", DMPAPER_ENV_B6},
93 {"EnvItalian", DMPAPER_ENV_ITALY},
94 {"EnvKaku2", DMPAPER_JENV_KAKU2},
95 {"EnvKaku2Rotated", DMPAPER_JENV_KAKU2_ROTATED},
96 {"EnvKaku3", DMPAPER_JENV_KAKU3},
97 {"EnvKaku3Rotated", DMPAPER_JENV_KAKU3_ROTATED},
98 {"EnvMonarch", DMPAPER_ENV_MONARCH},
99 {"EnvPersonal", DMPAPER_ENV_PERSONAL},
100 {"EnvPRC1", DMPAPER_PENV_1},
101 {"EnvPRC1Rotated", DMPAPER_PENV_1_ROTATED},
102 {"EnvPRC2", DMPAPER_PENV_2},
103 {"EnvPRC2Rotated", DMPAPER_PENV_2_ROTATED},
104 {"EnvPRC3", DMPAPER_PENV_3},
105 {"EnvPRC3Rotated", DMPAPER_PENV_3_ROTATED},
106 {"EnvPRC4", DMPAPER_PENV_4},
107 {"EnvPRC4Rotated", DMPAPER_PENV_4_ROTATED},
108 {"EnvPRC5", DMPAPER_PENV_5},
109 {"EnvPRC5Rotated", DMPAPER_PENV_5_ROTATED},
110 {"EnvPRC6", DMPAPER_PENV_6},
111 {"EnvPRC6Rotated", DMPAPER_PENV_6_ROTATED},
112 {"EnvPRC7", DMPAPER_PENV_7},
113 {"EnvPRC7Rotated", DMPAPER_PENV_7_ROTATED},
114 {"EnvPRC8", DMPAPER_PENV_8},
115 {"EnvPRC8Rotated", DMPAPER_PENV_8_ROTATED},
116 {"EnvPRC9", DMPAPER_PENV_9},
117 {"EnvPRC9Rotated", DMPAPER_PENV_9_ROTATED},
118 {"EnvPRC10", DMPAPER_PENV_10},
119 {"EnvPRC10Rotated", DMPAPER_PENV_10_ROTATED},
120 {"EnvYou4", DMPAPER_JENV_YOU4},
121 {"EnvYou4Rotated", DMPAPER_JENV_YOU4_ROTATED},
122 {"Executive", DMPAPER_EXECUTIVE},
123 {"FanFoldUS", DMPAPER_FANFOLD_US},
124 {"FanFoldGerman", DMPAPER_FANFOLD_STD_GERMAN},
125 {"FanFoldGermanLegal", DMPAPER_FANFOLD_LGL_GERMAN},
126 {"Folio", DMPAPER_FOLIO},
127 {"ISOB4", DMPAPER_ISO_B4},
128 {"ISOB5Extra", DMPAPER_B5_EXTRA},
129 {"Ledger", DMPAPER_LEDGER},
130 {"Legal", DMPAPER_LEGAL},
131 {"LegalExtra", DMPAPER_LEGAL_EXTRA},
132 {"Letter", DMPAPER_LETTER},
133 {"Letter.Transverse", DMPAPER_LETTER_TRANSVERSE},
134 {"LetterExtra", DMPAPER_LETTER_EXTRA},
135 {"LetterExtra.Transverse", DMPAPER_LETTER_EXTRA_TRANSVERSE},
136 {"LetterPlus", DMPAPER_LETTER_PLUS},
137 {"LetterRotated", DMPAPER_LETTER_ROTATED},
138 {"LetterSmall", DMPAPER_LETTERSMALL},
139 {"Monarch", DMPAPER_ENV_MONARCH},
140 {"Note", DMPAPER_NOTE},
141 {"Postcard", DMPAPER_JAPANESE_POSTCARD},
142 {"PostcardRotated", DMPAPER_JAPANESE_POSTCARD_ROTATED},
143 {"PRC16K", DMPAPER_P16K},
144 {"PRC16KRotated", DMPAPER_P16K_ROTATED},
145 {"PRC32K", DMPAPER_P32K},
146 {"PRC32KBig", DMPAPER_P32KBIG},
147 {"PRC32KBigRotated", DMPAPER_P32KBIG_ROTATED},
148 {"PRC32KRotated", DMPAPER_P32K_ROTATED},
149 {"Quarto", DMPAPER_QUARTO},
150 {"Statement", DMPAPER_STATEMENT},
151 {"SuperA", DMPAPER_A_PLUS},
152 {"SuperB", DMPAPER_B_PLUS},
153 {"Tabloid", DMPAPER_TABLOID},
154 {"TabloidExtra", DMPAPER_TABLOID_EXTRA},
158 /* the same for bin names */
164 {"Cassette", DMBIN_CASSETTE},
165 {"Envelope", DMBIN_ENVELOPE},
166 {"LargeCapacity", DMBIN_LARGECAPACITY},
167 {"Lower", DMBIN_LOWER},
168 {"ManualFeed", DMBIN_MANUAL},
169 {"Middle", DMBIN_MIDDLE},
170 {"Upper", DMBIN_UPPER},
174 /***********************************************************************
178 * Copies str into a newly allocated string from the process heap subsituting
179 * hex strings enclosed in '<' and '>' for their byte codes.
182 static char *PSDRV_PPDDecodeHex(char *str)
184 char *buf, *in, *out;
187 buf = HeapAlloc(PSDRV_Heap, 0, strlen(str) + 1);
191 for(in = str, out = buf; *in; in++) {
202 else if(isspace(*in))
206 if(!isxdigit(*in) || !isxdigit(*(in + 1))) {
207 ERR("Invalid hex char in hex string\n");
208 HeapFree(PSDRV_Heap, 0, buf);
212 for(i = 0; i < 2; i++) {
213 if(isdigit(*(in + i)))
214 *out |= (*(in + i) - '0') << ((1-i) * 4);
216 *out |= (toupper(*(in + i)) - 'A' + 10) << ((1-i) * 4);
228 /***********************************************************************
230 * PSDRV_PPDGetTransValue
233 static BOOL PSDRV_PPDGetTransValue(char *start, PPDTuple *tuple)
237 end = strpbrk(start, "\r\n");
238 if(end == start) return FALSE;
239 if(!end) end = start + strlen(start);
240 buf = HeapAlloc( PSDRV_Heap, 0, end - start + 1 );
241 memcpy(buf, start, end - start);
242 *(buf + (end - start)) = '\0';
243 tuple->valtrans = PSDRV_PPDDecodeHex(buf);
244 HeapFree( PSDRV_Heap, 0, buf );
249 /***********************************************************************
251 * PSDRV_PPDGetInvocationValue
253 * Passed string that should be surrounded by `"'s, return string alloced
256 static BOOL PSDRV_PPDGetInvocationValue(FILE *fp, char *pos, PPDTuple *tuple)
258 char *start, *end, *buf;
263 buf = HeapAlloc( PSDRV_Heap, 0, strlen(start) + 1 );
266 end = strchr(start, '"');
268 buf = HeapReAlloc( PSDRV_Heap, 0, buf,
269 len + (end - start) + 1 );
270 memcpy(buf + len, start, end - start);
271 *(buf + len + (end - start)) = '\0';
273 start = strchr(end, '/');
275 return PSDRV_PPDGetTransValue(start + 1, tuple);
278 int sl = strlen(start);
279 buf = HeapReAlloc( PSDRV_Heap, 0, buf, len + sl + 1 );
280 strcpy(buf + len, start);
283 } while( fgets((start = line), sizeof(line), fp) );
286 HeapFree( PSDRV_Heap, 0, buf );
291 /***********************************************************************
293 * PSDRV_PPDGetQuotedValue
295 * Passed string that should be surrounded by `"'s. Expand <xx> as hex
296 * return string alloced from process heap.
298 static BOOL PSDRV_PPDGetQuotedValue(FILE *fp, char *pos, PPDTuple *tuple)
302 if(!PSDRV_PPDGetInvocationValue(fp, pos, tuple))
304 buf = PSDRV_PPDDecodeHex(tuple->value);
305 HeapFree(PSDRV_Heap, 0, tuple->value);
311 /***********************************************************************
313 * PSDRV_PPDGetStringValue
315 * Just strip leading white space.
317 static BOOL PSDRV_PPDGetStringValue(char *str, PPDTuple *tuple)
319 char *start = str, *end;
321 while(*start != '\0' && isspace(*start))
324 end = strpbrk(start, "/\r\n");
325 if(!end) end = start + strlen(start);
326 tuple->value = HeapAlloc( PSDRV_Heap, 0, (end - start) + 1 );
327 memcpy(tuple->value, start, end - start);
328 *(tuple->value + (end - start)) = '\0';
330 PSDRV_PPDGetTransValue(end + 1, tuple);
335 /***********************************************************************
337 * PSDRV_PPDSymbolValue
339 * Not implemented yet.
341 static BOOL PSDRV_PPDGetSymbolValue(char *pos, PPDTuple *tuple)
348 /*********************************************************************
350 * PSDRV_PPDGetNextTuple
352 * Gets the next Keyword Option Value tuple from the file. Allocs space off
353 * the process heap which should be free()ed by the caller if not needed.
355 static BOOL PSDRV_PPDGetNextTuple(FILE *fp, PPDTuple *tuple)
357 char line[257], *opt = NULL, *cp, *trans;
358 BOOL gotoption = TRUE;
360 memset(tuple, 0, sizeof(*tuple));
363 if(!fgets(line, sizeof(line), fp))
365 if(line[0] == '*' && line[1] != '%' && strncmp(line, "*End", 4))
369 if(line[strlen(line)-1] != '\n') {
370 ERR("Line too long.\n");
374 for(cp = line; !isspace(*cp); cp++)
384 tuple->key = HeapAlloc( PSDRV_Heap, 0, cp - line + 1 );
385 if(!tuple->key) return FALSE;
387 memcpy(tuple->key, line, cp - line);
388 tuple->key[cp - line] = '\0';
393 cp = strpbrk(opt, ":/");
395 ERR("Error in line '%s'?\n", line);
398 tuple->option = HeapAlloc( PSDRV_Heap, 0, cp - opt + 1 );
399 if(!tuple->option) return FALSE;
400 memcpy(tuple->option, opt, cp - opt);
401 tuple->option[cp - opt] = '\0';
405 cp = strchr(trans, ':');
407 ERR("Error in line '%s'?\n", line);
410 buf = HeapAlloc( PSDRV_Heap, 0, cp - trans + 1 );
411 if(!buf) return FALSE;
412 memcpy(buf, trans, cp - trans);
413 buf[cp - trans] = '\0';
414 tuple->opttrans = PSDRV_PPDDecodeHex(buf);
415 HeapFree( PSDRV_Heap, 0, buf );
426 if( (!gotoption && strncmp(tuple->key, "*?", 2) ) ||
427 !strncmp(tuple->key, "*JCL", 4))
428 PSDRV_PPDGetQuotedValue(fp, cp, tuple);
430 PSDRV_PPDGetInvocationValue(fp, cp, tuple);
434 PSDRV_PPDGetSymbolValue(cp, tuple);
438 PSDRV_PPDGetStringValue(cp, tuple);
443 /*********************************************************************
445 * PSDRV_PPDGetPageSizeInfo
447 * Searches ppd PageSize list to return entry matching name or creates new
448 * entry which is appended to the list if name is not found.
451 PAGESIZE *PSDRV_PPDGetPageSizeInfo(PPD *ppd, char *name)
453 PAGESIZE *page = ppd->PageSizes, *lastpage;
456 page = ppd->PageSizes = HeapAlloc( PSDRV_Heap,
457 HEAP_ZERO_MEMORY, sizeof(*page) );
460 for( ; page; page = page->next) {
461 if(!strcmp(page->Name, name))
466 lastpage->next = HeapAlloc( PSDRV_Heap,
467 HEAP_ZERO_MEMORY, sizeof(*page) );
468 return lastpage->next;
472 /**********************************************************************
476 * Returns ptr alloced from heap to first word in str. Strips leading spaces.
477 * Puts ptr to next word in next
479 static char *PSDRV_PPDGetWord(char *str, char **next)
481 char *start, *end, *ret;
484 while(start && *start && isspace(*start))
486 if(!start || !*start) return FALSE;
489 while(*end && !isspace(*end))
492 ret = HeapAlloc( PSDRV_Heap, 0, end - start + 1 );
493 memcpy(ret, start, end - start );
494 *(ret + (end - start)) = '\0';
496 while(*end && isspace(*end))
506 /***********************************************************************
512 PPD *PSDRV_ParsePPD(char *fname)
518 TRACE("%s\n", fname);
520 if((fp = fopen(fname, "r")) == NULL) {
521 WARN("Couldn't open ppd file '%s'\n", fname);
525 ppd = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*ppd));
527 ERR("Unable to allocate memory for ppd\n");
533 while( PSDRV_PPDGetNextTuple(fp, &tuple)) {
535 if(!strcmp("*NickName", tuple.key)) {
536 ppd->NickName = tuple.value;
538 TRACE("NickName = '%s'\n", ppd->NickName);
541 else if(!strcmp("*LanguageLevel", tuple.key)) {
542 sscanf(tuple.value, "%d", &(ppd->LanguageLevel));
543 TRACE("LanguageLevel = %d\n", ppd->LanguageLevel);
546 else if(!strcmp("*ColorDevice", tuple.key)) {
547 if(!strcasecmp(tuple.value, "true"))
548 ppd->ColorDevice = TRUE;
549 TRACE("ColorDevice = %d\n", (int)ppd->ColorDevice);
552 else if((!strcmp("*DefaultResolution", tuple.key)) ||
553 (!strcmp("*DefaultJCLResolution", tuple.key))) {
554 sscanf(tuple.value, "%d", &(ppd->DefaultResolution));
555 TRACE("DefaultResolution = %d\n", ppd->DefaultResolution);
558 else if(!strcmp("*Font", tuple.key)) {
561 for(fn = ppd->InstalledFonts; fn && fn->next; fn = fn->next)
564 ppd->InstalledFonts = HeapAlloc(PSDRV_Heap,
565 HEAP_ZERO_MEMORY, sizeof(*fn));
566 fn = ppd->InstalledFonts;
568 fn->next = HeapAlloc(PSDRV_Heap,
569 HEAP_ZERO_MEMORY, sizeof(*fn));
572 fn->Name = tuple.option;
576 else if(!strcmp("*DefaultFont", tuple.key)) {
577 ppd->DefaultFont = tuple.value;
581 else if(!strcmp("*JCLBegin", tuple.key)) {
582 ppd->JCLBegin = tuple.value;
586 else if(!strcmp("*JCLToPSInterpreter", tuple.key)) {
587 ppd->JCLToPSInterpreter = tuple.value;
591 else if(!strcmp("*JCLEnd", tuple.key)) {
592 ppd->JCLEnd = tuple.value;
596 else if(!strcmp("*PageSize", tuple.key)) {
598 page = PSDRV_PPDGetPageSizeInfo(ppd, tuple.option);
603 page->Name = tuple.option;
606 for(i = 0; PageTrans[i].PSName; i++) {
607 if(!strcmp(PageTrans[i].PSName, page->Name)) { /* case ? */
608 page->WinPage = PageTrans[i].WinPage;
613 FIXME("Can't find Windows page type for '%s'\n",
616 if(!page->FullName) {
618 page->FullName = tuple.opttrans;
619 tuple.opttrans = NULL;
621 page->FullName = HEAP_strdupA( PSDRV_Heap, 0, page->Name );
624 if(!page->InvocationString) {
625 page->InvocationString = tuple.value;
630 else if(!strcmp("*ImageableArea", tuple.key)) {
632 page = PSDRV_PPDGetPageSizeInfo(ppd, tuple.option);
635 page->Name = tuple.option;
638 if(!page->FullName) {
639 page->FullName = tuple.opttrans;
640 tuple.opttrans = NULL;
643 #define PIA page->ImageableArea
645 PIA = HeapAlloc( PSDRV_Heap, 0, sizeof(*PIA) );
646 sscanf(tuple.value, "%f%f%f%f", &PIA->llx, &PIA->lly,
647 &PIA->urx, &PIA->ury);
653 else if(!strcmp("*PaperDimension", tuple.key)) {
655 page = PSDRV_PPDGetPageSizeInfo(ppd, tuple.option);
658 page->Name = tuple.option;
661 if(!page->FullName) {
662 page->FullName = tuple.opttrans;
663 tuple.opttrans = NULL;
666 #define PD page->PaperDimension
668 PD = HeapAlloc( PSDRV_Heap, 0, sizeof(*PD) );
669 sscanf(tuple.value, "%f%f", &PD->x, &PD->y);
674 else if(!strcmp("*LandscapeOrientation", tuple.key)) {
675 if(!strcmp(tuple.value, "Plus90"))
676 ppd->LandscapeOrientation = 90;
677 else if(!strcmp(tuple.value, "Minus90"))
678 ppd->LandscapeOrientation = -90;
680 /* anything else, namely 'any', leaves value at 0 */
682 TRACE("LandscapeOrientation = %d\n",
683 ppd->LandscapeOrientation);
686 else if(!strcmp("*UIConstraints", tuple.key)) {
688 CONSTRAINT *con, **insert = &ppd->Constraints;
691 insert = &((*insert)->next);
693 con = *insert = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
698 con->Feature1 = PSDRV_PPDGetWord(start, &start);
699 con->Value1 = PSDRV_PPDGetWord(start, &start);
700 con->Feature2 = PSDRV_PPDGetWord(start, &start);
701 con->Value2 = PSDRV_PPDGetWord(start, &start);
704 else if(!strcmp("*InputSlot", tuple.key)) {
705 INPUTSLOT *slot, **insert = &ppd->InputSlots;
709 insert = &((*insert)->next);
711 slot = *insert = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
714 slot->Name = tuple.option;
717 slot->FullName = tuple.opttrans;
718 tuple.opttrans = NULL;
720 slot->FullName = HEAP_strdupA( PSDRV_Heap, 0, slot->Name );
724 slot->InvocationString = tuple.value;
729 for(i = 0; BinTrans[i].PSName; i++) {
730 if(!strcmp(BinTrans[i].PSName, slot->Name)) { /* case ? */
731 slot->WinBin = BinTrans[i].WinBin;
736 FIXME("Can't find Windows bin type for '%s'\n",
741 if(tuple.key) HeapFree(PSDRV_Heap, 0, tuple.key);
742 if(tuple.option) HeapFree(PSDRV_Heap, 0, tuple.option);
743 if(tuple.value) HeapFree(PSDRV_Heap, 0, tuple.value);
744 if(tuple.opttrans) HeapFree(PSDRV_Heap, 0, tuple.opttrans);
745 if(tuple.valtrans) HeapFree(PSDRV_Heap, 0, tuple.valtrans);
756 OPTIONENTRY *optionEntry;
758 for(fn = ppd->InstalledFonts; fn; fn = fn->next)
759 TRACE("'%s'\n", fn->Name);
761 for(page = ppd->PageSizes; page; page = page->next) {
762 TRACE("'%s' aka '%s' (%d) invoked by '%s'\n", page->Name,
763 page->FullName, page->WinPage, page->InvocationString);
764 if(page->ImageableArea)
765 TRACE("Area = %.2f,%.2f - %.2f, %.2f\n",
766 page->ImageableArea->llx, page->ImageableArea->lly,
767 page->ImageableArea->urx, page->ImageableArea->ury);
768 if(page->PaperDimension)
769 TRACE("Dimension = %.2f x %.2f\n",
770 page->PaperDimension->x, page->PaperDimension->y);
773 for(con = ppd->Constraints; con; con = con->next)
774 TRACE("CONSTRAINTS@ %s %s %s %s\n", con->Feature1,
775 con->Value1, con->Feature2, con->Value2);
777 for(option = ppd->InstalledOptions; option; option = option->next) {
778 TRACE("OPTION: %s %s %s\n", option->OptionName,
779 option->FullName, option->DefaultOption);
780 for(optionEntry = option->Options; optionEntry;
781 optionEntry = optionEntry->next)
782 TRACE("\tOPTIONENTRY: %s %s %s\n", optionEntry->Name,
783 optionEntry->FullName, optionEntry->InvocationString);
786 for(slot = ppd->InputSlots; slot; slot = slot->next)
787 TRACE("INPUTSLOTS '%s' Name '%s' (%d) Invocation '%s'\n",
788 slot->Name, slot->FullName, slot->WinBin,
789 slot->InvocationString);