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