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