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