Removed HEAP_strdupA.
[wine] / dlls / wineps / ppd.c
1 /*      PostScript Printer Description (PPD) file parser
2  *
3  *      See  http://www.adobe.com/supportservice/devrelations/PDFS/TN/5003.PPD_Spec_v4.3.pdf
4  *
5  *      Copyright 1998  Huw D M Davies
6  */
7
8 #include <string.h>
9 #include <stdio.h>
10 #include <ctype.h>
11 #include "winnt.h" /* HEAP_ZERO_MEMORY */
12 #include "debugtools.h"
13 #include "psdrv.h"
14 #include "winspool.h"
15
16 DEFAULT_DEBUG_CHANNEL(psdrv);
17
18 typedef struct {
19 char    *key;
20 char    *option;
21 char    *opttrans;
22 char    *value;
23 char    *valtrans;
24 } PPDTuple;
25
26
27 /* map of page names in ppd file to Windows paper constants */
28
29 static struct {
30   char *PSName;
31   WORD WinPage;
32 } PageTrans[] = {
33   {"10x11",                   DMPAPER_10X11},
34   {"10x14",                   DMPAPER_10X14},
35   {"11x17",                   DMPAPER_11X17},   /* not in Adobe PPD file spec */
36   {"12x11",                   DMPAPER_12X11},
37   {"15x11",                   DMPAPER_15X11},
38   {"9x11",                    DMPAPER_9X11},
39   {"A2",                      DMPAPER_A2},
40   {"A3",                      DMPAPER_A3},
41   {"A3.Transverse",           DMPAPER_A3_TRANSVERSE},
42   {"A3Extra",                 DMPAPER_A3_EXTRA},
43   {"A3Extra.Transverse",      DMPAPER_A3_EXTRA_TRANSVERSE},
44   {"A3Rotated",               DMPAPER_A3_ROTATED},
45   {"A4",                      DMPAPER_A4},
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},
51   {"A5",                      DMPAPER_A5},
52   {"A5.Transverse",           DMPAPER_A5_TRANSVERSE},
53   {"A5Extra",                 DMPAPER_A5_EXTRA},
54   {"A5Rotated",               DMPAPER_A5_ROTATED},
55   {"A6",                      DMPAPER_A6},
56   {"A6Rotated",               DMPAPER_A6_ROTATED},
57   {"ARCHC",                   DMPAPER_CSHEET},
58   {"ARCHD",                   DMPAPER_DSHEET},
59   {"ARCHE",                   DMPAPER_ESHEET},
60   {"B4",                      DMPAPER_B4},
61   {"B4Rotated",               DMPAPER_B4_JIS_ROTATED},
62   {"B5",                      DMPAPER_B5},
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},          /*  use EnvC4 */
68   {"C5",                      DMPAPER_ENV_C5},          /*  use EnvC5 */
69   {"C6",                      DMPAPER_ENV_C6},          /*  use EnvC6 */
70   {"Comm10",                  DMPAPER_ENV_10},          /*  use Env10 */
71   {"DL",                      DMPAPER_ENV_DL},          /*  use EnvDL */
72   {"DoublePostcard",          DMPAPER_DBL_JAPANESE_POSTCARD},
73   {"DoublePostcardRotated",   DMPAPER_DBL_JAPANESE_POSTCARD_ROTATED},
74   {"Env10",                   DMPAPER_ENV_10},
75   {"Env11",                   DMPAPER_ENV_11},
76   {"Env12",                   DMPAPER_ENV_12},
77   {"Env14",                   DMPAPER_ENV_14},
78   {"Env9",                    DMPAPER_ENV_9},
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   {"EnvISOB4",                DMPAPER_ENV_B4},
90   {"EnvISOB5",                DMPAPER_ENV_B5},
91   {"EnvISOB6",                DMPAPER_ENV_B6},
92   {"EnvInvite",               DMPAPER_ENV_INVITE},
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   {"EnvPRC1",                 DMPAPER_PENV_1},
100   {"EnvPRC10",                DMPAPER_PENV_10},
101   {"EnvPRC10Rotated",         DMPAPER_PENV_10_ROTATED},
102   {"EnvPRC1Rotated",          DMPAPER_PENV_1_ROTATED},
103   {"EnvPRC2",                 DMPAPER_PENV_2},
104   {"EnvPRC2Rotated",          DMPAPER_PENV_2_ROTATED},
105   {"EnvPRC3",                 DMPAPER_PENV_3},
106   {"EnvPRC3Rotated",          DMPAPER_PENV_3_ROTATED},
107   {"EnvPRC4",                 DMPAPER_PENV_4},
108   {"EnvPRC4Rotated",          DMPAPER_PENV_4_ROTATED},
109   {"EnvPRC5",                 DMPAPER_PENV_5},
110   {"EnvPRC5Rotated",          DMPAPER_PENV_5_ROTATED},
111   {"EnvPRC6",                 DMPAPER_PENV_6},
112   {"EnvPRC6Rotated",          DMPAPER_PENV_6_ROTATED},
113   {"EnvPRC7",                 DMPAPER_PENV_7},
114   {"EnvPRC7Rotated",          DMPAPER_PENV_7_ROTATED},
115   {"EnvPRC8",                 DMPAPER_PENV_8},
116   {"EnvPRC8Rotated",          DMPAPER_PENV_8_ROTATED},
117   {"EnvPRC9",                 DMPAPER_PENV_9},
118   {"EnvPRC9Rotated",          DMPAPER_PENV_9_ROTATED},
119   {"EnvPersonal",             DMPAPER_ENV_PERSONAL},
120   {"EnvYou4",                 DMPAPER_JENV_YOU4},
121   {"EnvYou4Rotated",          DMPAPER_JENV_YOU4_ROTATED},
122   {"Executive",               DMPAPER_EXECUTIVE},
123   {"FanFoldGerman",           DMPAPER_FANFOLD_STD_GERMAN},
124   {"FanFoldGermanLegal",      DMPAPER_FANFOLD_LGL_GERMAN},
125   {"FanFoldUS",               DMPAPER_FANFOLD_US},
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},     /* use EnvMonarch */
140   {"Note",                    DMPAPER_NOTE},
141   {"PRC16K",                  DMPAPER_P16K},
142   {"PRC16KRotated",           DMPAPER_P16K_ROTATED},
143   {"PRC32K",                  DMPAPER_P32K},
144   {"PRC32KBig",               DMPAPER_P32KBIG},
145   {"PRC32KBigRotated",        DMPAPER_P32KBIG_ROTATED},
146   {"PRC32KRotated",           DMPAPER_P32K_ROTATED},
147   {"Postcard",                DMPAPER_JAPANESE_POSTCARD},
148   {"PostcardRotated",         DMPAPER_JAPANESE_POSTCARD_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},
155   {NULL,                      0}
156 };
157
158 static WORD UserPageType = DMPAPER_USER;
159 static WORD UserBinType = DMBIN_USER;
160
161 /***********************************************************************
162  *
163  *              PSDRV_PPDDecodeHex
164  *
165  * Copies str into a newly allocated string from the process heap subsituting
166  * hex strings enclosed in '<' and '>' for their byte codes.
167  *
168  */
169 static char *PSDRV_PPDDecodeHex(char *str)
170 {
171     char *buf, *in, *out;
172     BOOL inhex = FALSE;
173
174     buf = HeapAlloc(PSDRV_Heap, 0, strlen(str) + 1);
175     if(!buf)
176         return NULL;
177
178     for(in = str, out = buf; *in; in++) {
179         if(!inhex) {
180             if(*in != '<')
181                 *out++ = *in;
182             else
183                 inhex = TRUE;
184         } else {
185             if(*in == '>') {
186                 inhex = FALSE;
187                 continue;
188             }
189             else if(isspace(*in))
190                 continue;
191             else {
192                 int i;
193                 if(!isxdigit(*in) || !isxdigit(*(in + 1))) {
194                     ERR("Invalid hex char in hex string\n");
195                     HeapFree(PSDRV_Heap, 0, buf);
196                     return NULL;
197                 }
198                 *out = 0;
199                 for(i = 0; i < 2; i++) {
200                     if(isdigit(*(in + i)))
201                         *out |= (*(in + i) - '0') << ((1-i) * 4);
202                     else
203                         *out |= (toupper(*(in + i)) - 'A' + 10) << ((1-i) * 4);
204                 }
205                 out++;
206                 in++;
207             }
208         }
209     }
210     *out = '\0';
211     return buf;
212 }
213
214
215 /***********************************************************************
216  *
217  *              PSDRV_PPDGetTransValue
218  *
219  */
220 static BOOL PSDRV_PPDGetTransValue(char *start, PPDTuple *tuple)
221 {
222     char *buf, *end;
223
224     end = strpbrk(start, "\r\n");
225     if(end == start) return FALSE;
226     if(!end) end = start + strlen(start);
227     buf = HeapAlloc( PSDRV_Heap, 0, end - start + 1 );
228     memcpy(buf, start, end - start);
229     *(buf + (end - start)) = '\0';
230     tuple->valtrans = PSDRV_PPDDecodeHex(buf);
231     HeapFree( PSDRV_Heap, 0, buf );
232     return TRUE;
233 }
234
235
236 /***********************************************************************
237  *
238  *              PSDRV_PPDGetInvocationValue
239  *
240  * Passed string that should be surrounded by `"'s, return string alloced
241  * from process heap.
242  */
243 static BOOL PSDRV_PPDGetInvocationValue(FILE *fp, char *pos, PPDTuple *tuple)
244 {
245     char *start, *end, *buf;
246     char line[257];
247     int len;
248
249     start = pos + 1;
250     buf = HeapAlloc( PSDRV_Heap, 0, strlen(start) + 1 );
251     len = 0;
252     do {
253         end = strchr(start, '"');
254         if(end) {
255             buf = HeapReAlloc( PSDRV_Heap, 0, buf, 
256                                len + (end - start) + 1 );
257             memcpy(buf + len, start, end - start);
258             *(buf + len + (end - start)) = '\0';
259             tuple->value = buf;
260             start = strchr(end, '/');
261             if(start)
262                 return PSDRV_PPDGetTransValue(start + 1, tuple);
263             return TRUE;
264         } else {
265             int sl = strlen(start);
266             buf = HeapReAlloc( PSDRV_Heap, 0, buf, len + sl + 1 );
267             strcpy(buf + len, start);
268             len += sl;
269         }
270     } while( fgets((start = line), sizeof(line), fp) );
271
272     tuple->value = NULL;
273     HeapFree( PSDRV_Heap, 0, buf );
274     return FALSE;
275 }
276
277
278 /***********************************************************************
279  *
280  *              PSDRV_PPDGetQuotedValue
281  *
282  * Passed string that should be surrounded by `"'s. Expand <xx> as hex
283  * return string alloced from process heap.
284  */
285 static BOOL PSDRV_PPDGetQuotedValue(FILE *fp, char *pos, PPDTuple *tuple)
286 {
287     char *buf;
288
289     if(!PSDRV_PPDGetInvocationValue(fp, pos, tuple))
290         return FALSE;
291     buf = PSDRV_PPDDecodeHex(tuple->value);
292     HeapFree(PSDRV_Heap, 0, tuple->value);
293     tuple->value = buf;
294     return TRUE;
295 }
296
297
298 /***********************************************************************
299  *
300  *              PSDRV_PPDGetStringValue
301  *
302  * Just strip leading white space.
303  */
304 static BOOL PSDRV_PPDGetStringValue(char *str, PPDTuple *tuple)
305 {
306     char *start = str, *end;
307
308     while(*start != '\0' && isspace(*start))
309         start++;
310
311     end = strpbrk(start, "/\r\n");
312     if(!end) end = start + strlen(start);
313     tuple->value = HeapAlloc( PSDRV_Heap, 0, (end - start) + 1 );
314     memcpy(tuple->value, start, end - start);
315     *(tuple->value + (end - start)) = '\0';
316     if(*end == '/')
317         PSDRV_PPDGetTransValue(end + 1, tuple);
318     return TRUE;
319 }
320
321
322 /***********************************************************************
323  *
324  *              PSDRV_PPDSymbolValue
325  *
326  * Not implemented yet.
327  */
328 static BOOL PSDRV_PPDGetSymbolValue(char *pos, PPDTuple *tuple)
329 {
330     FIXME("Stub\n");
331     return FALSE;
332 }
333
334
335 /*********************************************************************
336  *
337  *              PSDRV_PPDGetNextTuple
338  *
339  * Gets the next Keyword Option Value tuple from the file. Allocs space off
340  * the process heap which should be free()ed by the caller if not needed.
341  */
342 static BOOL PSDRV_PPDGetNextTuple(FILE *fp, PPDTuple *tuple)
343 {
344     char line[257], *opt = NULL, *cp, *trans, *endkey;
345     BOOL gotoption = TRUE;
346
347     memset(tuple, 0, sizeof(*tuple));
348
349     do {
350         if(!fgets(line, sizeof(line), fp))
351             return FALSE;
352         if(line[0] == '*' && line[1] != '%' && strncmp(line, "*End", 4))
353             break;
354     } while(1);
355
356     if(line[strlen(line)-1] != '\n') {
357         ERR("Line too long.\n");
358         return FALSE;
359     }
360
361     for(cp = line; !isspace(*cp) && *cp != ':'; cp++)
362         ;
363
364     endkey = cp;
365     if(*cp == ':') { /* <key>: */
366         gotoption = FALSE;
367     } else {
368         while(isspace(*cp))
369             cp++;
370         if(*cp == ':') { /* <key>  : */
371             gotoption = FALSE;
372         } else { /* <key> <option> */
373             opt = cp;
374         }
375     }
376
377     tuple->key = HeapAlloc( PSDRV_Heap, 0, endkey - line + 1 );
378     if(!tuple->key) return FALSE;
379
380     memcpy(tuple->key, line, endkey - line);
381     tuple->key[endkey - line] = '\0';
382
383     if(gotoption) { /* opt points to 1st non-space character of the option */
384         cp = strpbrk(opt, ":/");
385         if(!cp) {
386             ERR("Error in line '%s'?\n", line);
387             return FALSE;
388         }
389         tuple->option = HeapAlloc( PSDRV_Heap, 0, cp - opt + 1 );
390         if(!tuple->option) return FALSE;
391         memcpy(tuple->option, opt, cp - opt);
392         tuple->option[cp - opt] = '\0';
393         if(*cp == '/') {
394             char *buf;
395             trans = cp + 1;
396             cp = strchr(trans, ':');
397             if(!cp) {
398                 ERR("Error in line '%s'?\n", line);
399                 return FALSE;
400             }
401             buf = HeapAlloc( PSDRV_Heap, 0, cp - trans + 1 );
402             if(!buf) return FALSE;
403             memcpy(buf, trans, cp - trans);
404             buf[cp - trans] = '\0';
405             tuple->opttrans = PSDRV_PPDDecodeHex(buf);
406             HeapFree( PSDRV_Heap, 0, buf );
407         }
408     }
409
410     /* cp should point to a ':', so we increment past it */
411         cp++;
412
413     while(isspace(*cp))
414         cp++;
415
416     switch(*cp) {
417     case '"':
418         if( (!gotoption && strncmp(tuple->key, "*?", 2) ) ||
419              !strncmp(tuple->key, "*JCL", 4))
420             PSDRV_PPDGetQuotedValue(fp, cp, tuple);
421         else
422             PSDRV_PPDGetInvocationValue(fp, cp, tuple);
423         break;
424
425     case '^':
426         PSDRV_PPDGetSymbolValue(cp, tuple);
427         break;
428
429     default:
430         PSDRV_PPDGetStringValue(cp, tuple);
431     }
432     return TRUE;
433 }
434
435 /*********************************************************************
436  *
437  *              PSDRV_PPDGetPageSizeInfo
438  *
439  * Searches ppd PageSize list to return entry matching name or creates new
440  * entry which is appended to the list if name is not found.
441  *
442  */
443 PAGESIZE *PSDRV_PPDGetPageSizeInfo(PPD *ppd, char *name)
444 {
445     PAGESIZE *page = ppd->PageSizes, *lastpage;
446     
447     if(!page) {
448        page = ppd->PageSizes = HeapAlloc( PSDRV_Heap,
449                                             HEAP_ZERO_MEMORY, sizeof(*page) );
450        return page;
451     } else {
452         for( ; page; page = page->next) {
453             if(!strcmp(page->Name, name))
454                  return page;
455             lastpage = page;
456         }
457
458         lastpage->next = HeapAlloc( PSDRV_Heap,
459                                            HEAP_ZERO_MEMORY, sizeof(*page) );
460         return lastpage->next;
461     }
462 }
463
464 /**********************************************************************
465  *
466  *              PSDRV_PPDGetWord
467  *
468  * Returns ptr alloced from heap to first word in str. Strips leading spaces.
469  * Puts ptr to next word in next
470  */
471 static char *PSDRV_PPDGetWord(char *str, char **next)
472 {
473     char *start, *end, *ret;
474
475     start = str;
476     while(start && *start && isspace(*start))
477         start++;
478     if(!start || !*start) return FALSE;
479
480     end = start;
481     while(*end && !isspace(*end))
482         end++;
483
484     ret = HeapAlloc( PSDRV_Heap, 0, end - start + 1 );
485     memcpy(ret, start, end - start );
486     *(ret + (end - start)) = '\0';
487
488     while(*end && isspace(*end))
489         end++;
490     if(*end)
491         *next = end;
492     else
493         *next = NULL;
494
495     return ret;
496 }
497
498 /*******************************************************************************
499  *      PSDRV_AddSlot
500  *
501  */
502 static INT PSDRV_AddSlot(PPD *ppd, LPSTR szName, LPSTR szFullName,
503         LPSTR szInvocationString, WORD wWinBin)
504 {
505     INPUTSLOT   *slot, **insert = &ppd->InputSlots;
506
507     while (*insert)
508         insert = &((*insert)->next);
509
510     slot = *insert = HeapAlloc(PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(INPUTSLOT));
511     if (!slot)
512     {
513         ERR("Failed to allocate %i bytes of memory\n", sizeof(INPUTSLOT));
514         return 1;
515     }
516
517     slot->Name = szName;
518     slot->FullName = szFullName;
519     slot->InvocationString = szInvocationString;
520     slot->WinBin = wWinBin;
521
522     return 0;
523 }
524
525 /***********************************************************************
526  *
527  *              PSDRV_ParsePPD
528  *
529  *
530  */
531 PPD *PSDRV_ParsePPD(char *fname)
532 {
533     FILE *fp;
534     PPD *ppd;
535     PPDTuple tuple;
536
537     TRACE("%s\n", fname);
538
539     if((fp = fopen(fname, "r")) == NULL) {
540         WARN("Couldn't open ppd file '%s'\n", fname);
541         return NULL;
542     }
543
544     ppd = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY, sizeof(*ppd));
545     if(!ppd) {
546         ERR("Unable to allocate memory for ppd\n");
547         fclose(fp);
548         return NULL;
549     }
550
551     /*
552      *  The Windows PostScript drivers create the following "virtual bin" for
553      *  every PostScript printer
554      */
555     if (PSDRV_AddSlot(ppd, NULL, "Automatically Select", NULL,
556             DMBIN_FORMSOURCE))
557     {
558         HeapFree (PSDRV_Heap, 0, ppd);
559         fclose(fp);
560         return NULL;
561     }
562     
563     while( PSDRV_PPDGetNextTuple(fp, &tuple)) {
564
565         if(!strcmp("*NickName", tuple.key)) {
566             ppd->NickName = tuple.value;
567             tuple.value = NULL;
568             TRACE("NickName = '%s'\n", ppd->NickName);
569         }
570
571         else if(!strcmp("*LanguageLevel", tuple.key)) {
572             sscanf(tuple.value, "%d", &(ppd->LanguageLevel));
573             TRACE("LanguageLevel = %d\n", ppd->LanguageLevel);
574         }
575
576         else if(!strcmp("*ColorDevice", tuple.key)) {
577             if(!strcasecmp(tuple.value, "true"))
578                 ppd->ColorDevice = TRUE;
579             TRACE("ColorDevice = %d\n", (int)ppd->ColorDevice);
580         }
581
582         else if((!strcmp("*DefaultResolution", tuple.key)) ||
583                 (!strcmp("*DefaultJCLResolution", tuple.key))) {
584             sscanf(tuple.value, "%d", &(ppd->DefaultResolution));
585             TRACE("DefaultResolution = %d\n", ppd->DefaultResolution);
586         }
587
588         else if(!strcmp("*Font", tuple.key)) {
589             FONTNAME *fn;
590
591             for(fn = ppd->InstalledFonts; fn && fn->next; fn = fn->next)
592                 ;
593             if(!fn) {
594                 ppd->InstalledFonts = HeapAlloc(PSDRV_Heap,
595                                                HEAP_ZERO_MEMORY, sizeof(*fn));
596                 fn = ppd->InstalledFonts;
597             } else {
598                fn->next = HeapAlloc(PSDRV_Heap,
599                                                HEAP_ZERO_MEMORY, sizeof(*fn));
600                fn = fn->next;
601             }
602             fn->Name = tuple.option;
603             tuple.option = NULL;
604         }
605
606         else if(!strcmp("*DefaultFont", tuple.key)) {
607             ppd->DefaultFont = tuple.value;
608             tuple.value = NULL;
609         }
610
611         else if(!strcmp("*JCLBegin", tuple.key)) {
612             ppd->JCLBegin = tuple.value;
613             tuple.value = NULL;
614         }
615
616         else if(!strcmp("*JCLToPSInterpreter", tuple.key)) {
617             ppd->JCLToPSInterpreter = tuple.value;
618             tuple.value = NULL;
619         }
620
621         else if(!strcmp("*JCLEnd", tuple.key)) {
622             ppd->JCLEnd = tuple.value;
623             tuple.value = NULL;
624         }
625
626         else if(!strcmp("*PageSize", tuple.key)) {
627             PAGESIZE *page;
628             page = PSDRV_PPDGetPageSizeInfo(ppd, tuple.option);
629
630             if(!page->Name) {
631                 int i;
632
633                 page->Name = tuple.option;
634                 tuple.option = NULL;
635                 
636                 for(i = 0; PageTrans[i].PSName; i++) {
637                     if(!strcmp(PageTrans[i].PSName, page->Name)) { /* case ? */
638                         page->WinPage = PageTrans[i].WinPage;
639                         break;
640                     }
641                 }
642                 if(!page->WinPage) {
643                     TRACE("Can't find Windows page type for '%s' - using %u\n",
644                           page->Name, UserPageType);
645                     page->WinPage = UserPageType++;
646                 }
647             }
648             if(!page->FullName) {
649                 if(tuple.opttrans) {
650                     page->FullName = tuple.opttrans;
651                     tuple.opttrans = NULL;
652                 } else
653                 {
654                     page->FullName = HeapAlloc( PSDRV_Heap, 0, strlen(page->Name)+1 );
655                     strcpy( page->FullName, page->Name );
656                 }
657             }
658             if(!page->InvocationString) {
659                 page->InvocationString = tuple.value;
660                 tuple.value = NULL;
661             }
662         }
663
664         else if(!strcmp("*ImageableArea", tuple.key)) {
665             PAGESIZE *page;
666             page = PSDRV_PPDGetPageSizeInfo(ppd, tuple.option);
667             
668             if(!page->Name) {
669                 page->Name = tuple.option;
670                 tuple.option = NULL;
671             }
672             if(!page->FullName) {
673                 page->FullName = tuple.opttrans;
674                 tuple.opttrans = NULL;
675             }
676
677 #define PIA page->ImageableArea
678             if(!PIA) {
679                 PIA = HeapAlloc( PSDRV_Heap, 0, sizeof(*PIA) );
680                 sscanf(tuple.value, "%f%f%f%f", &PIA->llx, &PIA->lly,
681                                                 &PIA->urx, &PIA->ury);
682             }
683 #undef PIA
684         }
685
686
687         else if(!strcmp("*PaperDimension", tuple.key)) {
688             PAGESIZE *page;
689             page = PSDRV_PPDGetPageSizeInfo(ppd, tuple.option);
690             
691             if(!page->Name) {
692                 page->Name = tuple.option;
693                 tuple.option = NULL;
694             }
695             if(!page->FullName) {
696                 page->FullName = tuple.opttrans;
697                 tuple.opttrans = NULL;
698             }
699
700 #define PD page->PaperDimension
701             if(!PD) {
702                 PD = HeapAlloc( PSDRV_Heap, 0, sizeof(*PD) );
703                 sscanf(tuple.value, "%f%f", &PD->x, &PD->y);
704             }
705 #undef PD
706         }
707
708         else if(!strcmp("*LandscapeOrientation", tuple.key)) {
709             if(!strcmp(tuple.value, "Plus90"))
710                 ppd->LandscapeOrientation = 90;
711             else if(!strcmp(tuple.value, "Minus90"))
712                 ppd->LandscapeOrientation = -90;
713
714             /* anything else, namely 'any', leaves value at 0 */
715
716             TRACE("LandscapeOrientation = %d\n", 
717                   ppd->LandscapeOrientation);
718         }
719         
720         else if(!strcmp("*UIConstraints", tuple.key)) {
721             char *start;
722             CONSTRAINT *con, **insert = &ppd->Constraints;
723
724             while(*insert)
725                 insert = &((*insert)->next);
726             
727             con = *insert = HeapAlloc( PSDRV_Heap, HEAP_ZERO_MEMORY,
728                                        sizeof(*con) );
729
730             start = tuple.value;
731             
732             con->Feature1 = PSDRV_PPDGetWord(start, &start);
733             con->Value1 = PSDRV_PPDGetWord(start, &start);
734             con->Feature2 = PSDRV_PPDGetWord(start, &start);
735             con->Value2 = PSDRV_PPDGetWord(start, &start);
736         }
737             
738         else if (!strcmp("*InputSlot", tuple.key))
739         {
740
741             if (!tuple.opttrans)
742                 tuple.opttrans = tuple.option;
743
744             TRACE("Using Windows bin type %u for '%s'\n", UserBinType,
745                     tuple.option);
746
747             /* FIXME - should check for failure */
748             PSDRV_AddSlot(ppd, tuple.option, tuple.opttrans, tuple.value,
749                     UserBinType++);
750
751             tuple.option = tuple.opttrans = tuple.value = NULL;
752         }   
753
754         /*
755          *  Windows treats "manual feed" as another paper source.  Most PPD
756          *  files, however, treat it as a separate option which is either on or
757          *  off.
758          */
759         else if (!strcmp("*ManualFeed", tuple.key) && tuple.option &&
760                 !strcmp ("True", tuple.option))
761         {
762             /* FIXME - should check for failure */
763             PSDRV_AddSlot(ppd, NULL, "Manual Feed", tuple.value, DMBIN_MANUAL);
764             tuple.value = NULL;
765         }
766
767         if(tuple.key) HeapFree(PSDRV_Heap, 0, tuple.key);
768         if(tuple.option) HeapFree(PSDRV_Heap, 0, tuple.option);
769         if(tuple.value) HeapFree(PSDRV_Heap, 0, tuple.value);
770         if(tuple.opttrans) HeapFree(PSDRV_Heap, 0, tuple.opttrans);
771         if(tuple.valtrans) HeapFree(PSDRV_Heap, 0, tuple.valtrans);    
772         
773     }
774
775
776     {
777         FONTNAME *fn;
778         PAGESIZE *page;
779         CONSTRAINT *con;
780         INPUTSLOT *slot;
781         OPTION *option;
782         OPTIONENTRY *optionEntry;
783
784         for(fn = ppd->InstalledFonts; fn; fn = fn->next)
785             TRACE("'%s'\n", fn->Name);
786         
787         for(page = ppd->PageSizes; page; page = page->next) {
788             TRACE("'%s' aka '%s' (%d) invoked by '%s'\n", page->Name,
789               page->FullName, page->WinPage, page->InvocationString);
790             if(page->ImageableArea)
791                 TRACE("Area = %.2f,%.2f - %.2f, %.2f\n", 
792                       page->ImageableArea->llx, page->ImageableArea->lly,
793                       page->ImageableArea->urx, page->ImageableArea->ury);
794             if(page->PaperDimension)
795                 TRACE("Dimension = %.2f x %.2f\n", 
796                       page->PaperDimension->x, page->PaperDimension->y);
797         }
798
799         for(con = ppd->Constraints; con; con = con->next)
800             TRACE("CONSTRAINTS@ %s %s %s %s\n", con->Feature1,
801                   con->Value1, con->Feature2, con->Value2);
802
803         for(option = ppd->InstalledOptions; option; option = option->next) {
804             TRACE("OPTION: %s %s %s\n", option->OptionName,
805                   option->FullName, option->DefaultOption);
806             for(optionEntry = option->Options; optionEntry;
807                 optionEntry = optionEntry->next)
808                 TRACE("\tOPTIONENTRY: %s %s %s\n", optionEntry->Name,
809                       optionEntry->FullName, optionEntry->InvocationString);
810         }
811
812         for(slot = ppd->InputSlots; slot; slot = slot->next)
813             TRACE("INPUTSLOTS '%s' Name '%s' (%d) Invocation '%s'\n",
814                   slot->Name, slot->FullName, slot->WinBin, 
815                   slot->InvocationString);
816     }
817
818     return ppd;
819 }
820