]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/emit.c
Merge changes from CUPS 1.4svn-r7282.
[thirdparty/cups.git] / cups / emit.c
1 /*
2 * "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * PPD code emission routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * PostScript is a trademark of Adobe Systems, Inc.
16 *
17 * This file is subject to the Apple OS-Developed Software exception.
18 *
19 * Contents:
20 *
21 * ppdCollect() - Collect all marked options that reside in the
22 * specified section.
23 * ppdCollect2() - Collect all marked options that reside in the
24 * specified section and minimum order.
25 * ppdEmit() - Emit code for marked options to a file.
26 * ppdEmitAfterOrder() - Emit a subset of the code for marked options to a
27 * file.
28 * ppdEmitFd() - Emit code for marked options to a file.
29 * ppdEmitJCL() - Emit code for JCL options to a file.
30 * ppdEmitJCLEnd() - Emit JCLEnd code to a file.
31 * ppdEmitString() - Get a string containing the code for marked options.
32 * ppd_handle_media() - Handle media selection...
33 */
34
35 /*
36 * Include necessary headers...
37 */
38
39 #include "ppd.h"
40 #include <stdlib.h>
41 #include "string.h"
42 #include <errno.h>
43 #include "debug.h"
44
45 #if defined(WIN32) || defined(__EMX__)
46 # include <io.h>
47 #else
48 # include <unistd.h>
49 #endif /* WIN32 || __EMX__ */
50
51
52 /*
53 * Local functions...
54 */
55
56 static void ppd_handle_media(ppd_file_t *ppd);
57
58
59 /*
60 * Local globals...
61 */
62
63 static const char ppd_custom_code[] =
64 "pop pop pop\n"
65 "<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n";
66
67
68 /*
69 * 'ppdCollect()' - Collect all marked options that reside in the specified
70 * section.
71 *
72 * The choices array should be freed using @code free@ when you are
73 * finished with it.
74 */
75
76 int /* O - Number of options marked */
77 ppdCollect(ppd_file_t *ppd, /* I - PPD file data */
78 ppd_section_t section, /* I - Section to collect */
79 ppd_choice_t ***choices) /* O - Pointers to choices */
80 {
81 return (ppdCollect2(ppd, section, 0.0, choices));
82 }
83
84
85 /*
86 * 'ppdCollect2()' - Collect all marked options that reside in the
87 * specified section and minimum order.
88 *
89 * The choices array should be freed using @code free@ when you are
90 * finished with it.
91 *
92 * @since CUPS 1.2@
93 */
94
95 int /* O - Number of options marked */
96 ppdCollect2(ppd_file_t *ppd, /* I - PPD file data */
97 ppd_section_t section, /* I - Section to collect */
98 float min_order, /* I - Minimum OrderDependency value */
99 ppd_choice_t ***choices) /* O - Pointers to choices */
100 {
101 ppd_choice_t *c; /* Current choice */
102 ppd_section_t csection; /* Current section */
103 float corder; /* Current OrderDependency value */
104 int count; /* Number of choices collected */
105 ppd_choice_t **collect; /* Collected choices */
106 float *orders; /* Collected order values */
107
108
109 DEBUG_printf(("ppdCollect2(ppd=%p, section=%d, min_order=%f, choices=%p)\n",
110 ppd, section, min_order, choices));
111
112 if (!ppd || !choices)
113 {
114 if (choices)
115 *choices = NULL;
116
117 return (0);
118 }
119
120 /*
121 * Allocate memory for up to N selected choices...
122 */
123
124 count = 0;
125 if ((collect = calloc(sizeof(ppd_choice_t *),
126 cupsArrayCount(ppd->marked))) == NULL)
127 {
128 *choices = NULL;
129 return (0);
130 }
131
132 if ((orders = calloc(sizeof(float), cupsArrayCount(ppd->marked))) == NULL)
133 {
134 *choices = NULL;
135 free(collect);
136 return (0);
137 }
138
139 /*
140 * Loop through all options and add choices as needed...
141 */
142
143 for (c = (ppd_choice_t *)cupsArrayFirst(ppd->marked);
144 c;
145 c = (ppd_choice_t *)cupsArrayNext(ppd->marked))
146 {
147 csection = c->option->section;
148 corder = c->option->order;
149
150 if (!strcmp(c->choice, "Custom"))
151 {
152 ppd_attr_t *attr; /* NonUIOrderDependency value */
153 float aorder; /* Order value */
154 char asection[17], /* Section name */
155 amain[PPD_MAX_NAME + 1],
156 aoption[PPD_MAX_NAME];
157 /* *CustomFoo and True */
158
159
160 for (attr = ppdFindAttr(ppd, "NonUIOrderDependency", NULL);
161 attr;
162 attr = ppdFindNextAttr(ppd, "NonUIOrderDependency", NULL))
163 if (attr->value &&
164 sscanf(attr->value, "%f%16s%41s%40s", &aorder, asection, amain,
165 aoption) == 4 &&
166 !strncmp(amain, "*Custom", 7) &&
167 !strcmp(amain + 7, c->option->keyword) && !strcmp(aoption, "True"))
168 {
169 /*
170 * Use this NonUIOrderDependency...
171 */
172
173 corder = aorder;
174
175 if (!strcmp(asection, "DocumentSetup"))
176 csection = PPD_ORDER_DOCUMENT;
177 else if (!strcmp(asection, "ExitServer"))
178 csection = PPD_ORDER_EXIT;
179 else if (!strcmp(asection, "JCLSetup"))
180 csection = PPD_ORDER_JCL;
181 else if (!strcmp(asection, "PageSetup"))
182 csection = PPD_ORDER_PAGE;
183 else if (!strcmp(asection, "Prolog"))
184 csection = PPD_ORDER_PROLOG;
185 else
186 csection = PPD_ORDER_ANY;
187
188 break;
189 }
190 }
191
192 if (csection == section && corder >= min_order)
193 {
194 collect[count] = c;
195 orders[count] = corder;
196 count ++;
197 }
198 }
199
200 /*
201 * If we have more than 1 marked choice, sort them...
202 */
203
204 if (count > 1)
205 {
206 int i, j; /* Looping vars */
207
208 for (i = 0; i < (count - 1); i ++)
209 for (j = i + 1; j < count; j ++)
210 if (orders[i] > orders[j])
211 {
212 c = collect[i];
213 corder = orders[i];
214 collect[i] = collect[j];
215 orders[i] = orders[j];
216 collect[j] = c;
217 orders[j] = corder;
218 }
219 }
220
221 free(orders);
222
223 DEBUG_printf(("ppdCollect2: %d marked choices...\n", count));
224
225 /*
226 * Return the array and number of choices; if 0, free the array since
227 * it isn't needed.
228 */
229
230 if (count > 0)
231 {
232 *choices = collect;
233 return (count);
234 }
235 else
236 {
237 *choices = NULL;
238 free(collect);
239 return (0);
240 }
241 }
242
243
244 /*
245 * 'ppdEmit()' - Emit code for marked options to a file.
246 */
247
248 int /* O - 0 on success, -1 on failure */
249 ppdEmit(ppd_file_t *ppd, /* I - PPD file record */
250 FILE *fp, /* I - File to write to */
251 ppd_section_t section) /* I - Section to write */
252 {
253 return (ppdEmitAfterOrder(ppd, fp, section, 0, 0.0));
254 }
255
256
257 /*
258 * 'ppdEmitAfterOrder()' - Emit a subset of the code for marked options to a file.
259 *
260 * When "limit" is non-zero, this function only emits options whose
261 * OrderDependency value is greater than or equal to "min_order".
262 *
263 * When "limit" is zero, this function is identical to ppdEmit().
264 *
265 * @since CUPS 1.2@
266 */
267
268 int /* O - 0 on success, -1 on failure */
269 ppdEmitAfterOrder(
270 ppd_file_t *ppd, /* I - PPD file record */
271 FILE *fp, /* I - File to write to */
272 ppd_section_t section, /* I - Section to write */
273 int limit, /* I - Non-zero to use min_order */
274 float min_order) /* I - Lowest OrderDependency */
275 {
276 char *buffer; /* Option code */
277 int status; /* Return status */
278
279
280 /*
281 * Range check input...
282 */
283
284 if (!ppd || !fp)
285 return (-1);
286
287 /*
288 * Get the string...
289 */
290
291 buffer = ppdEmitString(ppd, section, min_order);
292
293 /*
294 * Write it as needed and return...
295 */
296
297 if (buffer)
298 {
299 status = fputs(buffer, fp) < 0 ? -1 : 0;
300
301 free(buffer);
302 }
303 else
304 status = 0;
305
306 return (status);
307 }
308
309
310 /*
311 * 'ppdEmitFd()' - Emit code for marked options to a file.
312 */
313
314 int /* O - 0 on success, -1 on failure */
315 ppdEmitFd(ppd_file_t *ppd, /* I - PPD file record */
316 int fd, /* I - File to write to */
317 ppd_section_t section) /* I - Section to write */
318 {
319 char *buffer, /* Option code */
320 *bufptr; /* Pointer into code */
321 size_t buflength; /* Length of option code */
322 ssize_t bytes; /* Bytes written */
323 int status; /* Return status */
324
325
326 /*
327 * Range check input...
328 */
329
330 if (!ppd || fd < 0)
331 return (-1);
332
333 /*
334 * Get the string...
335 */
336
337 buffer = ppdEmitString(ppd, section, 0.0);
338
339 /*
340 * Write it as needed and return...
341 */
342
343 if (buffer)
344 {
345 buflength = strlen(buffer);
346 bufptr = buffer;
347 bytes = 0;
348
349 while (buflength > 0)
350 {
351 #ifdef WIN32
352 if ((bytes = (ssize_t)write(fd, bufptr, (unsigned)buflength)) < 0)
353 #else
354 if ((bytes = write(fd, bufptr, buflength)) < 0)
355 #endif /* WIN32 */
356 {
357 if (errno == EAGAIN || errno == EINTR)
358 continue;
359
360 break;
361 }
362
363 buflength -= bytes;
364 bufptr += bytes;
365 }
366
367 status = bytes < 0 ? -1 : 0;
368
369 free(buffer);
370 }
371 else
372 status = 0;
373
374 return (status);
375 }
376
377
378 /*
379 * 'ppdEmitJCL()' - Emit code for JCL options to a file.
380 */
381
382 int /* O - 0 on success, -1 on failure */
383 ppdEmitJCL(ppd_file_t *ppd, /* I - PPD file record */
384 FILE *fp, /* I - File to write to */
385 int job_id, /* I - Job ID */
386 const char *user, /* I - Username */
387 const char *title) /* I - Title */
388 {
389 char *ptr; /* Pointer into JCL string */
390 char temp[81]; /* Local title string */
391
392
393 /*
394 * Range check the input...
395 */
396
397 if (!ppd || !ppd->jcl_begin || !ppd->jcl_ps)
398 return (0);
399
400 /*
401 * See if the printer supports HP PJL...
402 */
403
404 if (!strncmp(ppd->jcl_begin, "\033%-12345X@", 10))
405 {
406 /*
407 * This printer uses HP PJL commands for output; filter the output
408 * so that we only have a single "@PJL JOB" command in the header...
409 *
410 * To avoid bugs in the PJL implementation of certain vendors' products
411 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
412 * of the PJL commands to initialize PJL processing.
413 */
414
415 ppd_attr_t *charset; /* PJL charset */
416
417
418 if ((charset = ppdFindAttr(ppd, "cupsPJLCharset", NULL)) != NULL)
419 {
420 if (!charset->value || strcasecmp(charset->value, "UTF-8"))
421 charset = NULL;
422 }
423
424 fputs("\033%-12345X@PJL\n", fp);
425 for (ptr = ppd->jcl_begin + 9; *ptr;)
426 if (!strncmp(ptr, "@PJL JOB", 8))
427 {
428 /*
429 * Skip job command...
430 */
431
432 for (;*ptr; ptr ++)
433 if (*ptr == '\n')
434 break;
435
436 if (*ptr)
437 ptr ++;
438 }
439 else
440 {
441 /*
442 * Copy line...
443 */
444
445 for (;*ptr; ptr ++)
446 {
447 putc(*ptr, fp);
448 if (*ptr == '\n')
449 break;
450 }
451
452 if (*ptr)
453 ptr ++;
454 }
455
456 /*
457 * Eliminate any path info from the job title...
458 */
459
460 if ((ptr = strrchr(title, '/')) != NULL)
461 title = ptr + 1;
462
463 /*
464 * Replace double quotes with single quotes and 8-bit characters with
465 * question marks so that the title does not cause a PJL syntax error.
466 */
467
468 strlcpy(temp, title, sizeof(temp));
469
470 for (ptr = temp; *ptr; ptr ++)
471 if (*ptr == '\"')
472 *ptr = '\'';
473 else if (charset && (*ptr & 128))
474 *ptr = '?';
475
476 /*
477 * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
478 */
479
480 fprintf(fp, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp,
481 job_id, user, temp);
482 fprintf(fp, "@PJL RDYMSG DISPLAY = \"%d %s %s\"\n", job_id, user, temp);
483 }
484 else
485 fputs(ppd->jcl_begin, fp);
486
487 ppdEmit(ppd, fp, PPD_ORDER_JCL);
488 fputs(ppd->jcl_ps, fp);
489
490 return (0);
491 }
492
493
494 /*
495 * 'ppdEmitJCLEnd()' - Emit JCLEnd code to a file.
496 *
497 * @since CUPS 1.2@
498 */
499
500 int /* O - 0 on success, -1 on failure */
501 ppdEmitJCLEnd(ppd_file_t *ppd, /* I - PPD file record */
502 FILE *fp) /* I - File to write to */
503 {
504 /*
505 * Range check the input...
506 */
507
508 if (!ppd)
509 return (0);
510
511 if (!ppd->jcl_end)
512 {
513 if (ppd->num_filters == 0)
514 putc(0x04, fp);
515
516 return (0);
517 }
518
519 /*
520 * See if the printer supports HP PJL...
521 */
522
523 if (!strncmp(ppd->jcl_end, "\033%-12345X@", 10))
524 {
525 /*
526 * This printer uses HP PJL commands for output; filter the output
527 * so that we only have a single "@PJL JOB" command in the header...
528 *
529 * To avoid bugs in the PJL implementation of certain vendors' products
530 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
531 * of the PJL commands to initialize PJL processing.
532 */
533
534 fputs("\033%-12345X@PJL\n", fp);
535 fputs("@PJL RDYMSG DISPLAY = \"READY\"\n", fp);
536 fputs(ppd->jcl_end + 9, fp);
537 }
538 else
539 fputs(ppd->jcl_end, fp);
540
541 return (0);
542 }
543
544
545 /*
546 * 'ppdEmitString()' - Get a string containing the code for marked options.
547 *
548 * When "min_order" is greater than zero, this function only includes options
549 * whose OrderDependency value is greater than or equal to "min_order".
550 * Otherwise, all options in the specified section are included in the
551 * returned string.
552 *
553 * The return string is allocated on the heap and should be freed using
554 * @code free@ when you are done with it.
555 *
556 * @since CUPS 1.2@
557 */
558
559 char * /* O - String containing option code or @code NULL@ if there is no option code */
560 ppdEmitString(ppd_file_t *ppd, /* I - PPD file record */
561 ppd_section_t section, /* I - Section to write */
562 float min_order) /* I - Lowest OrderDependency */
563 {
564 int i, j, /* Looping vars */
565 count; /* Number of choices */
566 ppd_choice_t **choices; /* Choices */
567 ppd_size_t *size; /* Custom page size */
568 ppd_coption_t *coption; /* Custom option */
569 ppd_cparam_t *cparam; /* Custom parameter */
570 size_t bufsize; /* Size of string buffer needed */
571 char *buffer, /* String buffer */
572 *bufptr, /* Pointer into buffer */
573 *bufend; /* End of buffer */
574 struct lconv *loc; /* Locale data */
575
576
577 DEBUG_printf(("ppdEmitString(ppd=%p, section=%d, min_order=%f)\n",
578 ppd, section, min_order));
579
580 /*
581 * Range check input...
582 */
583
584 if (!ppd)
585 return (NULL);
586
587 /*
588 * Use PageSize or PageRegion as required...
589 */
590
591 ppd_handle_media(ppd);
592
593 /*
594 * Collect the options we need to emit...
595 */
596
597 if ((count = ppdCollect2(ppd, section, min_order, &choices)) == 0)
598 return (NULL);
599
600 /*
601 * Count the number of bytes that are required to hold all of the
602 * option code...
603 */
604
605 for (i = 0, bufsize = 1; i < count; i ++)
606 {
607 if (section != PPD_ORDER_EXIT && section != PPD_ORDER_JCL)
608 {
609 bufsize += 3; /* [{\n */
610
611 if ((!strcasecmp(choices[i]->option->keyword, "PageSize") ||
612 !strcasecmp(choices[i]->option->keyword, "PageRegion")) &&
613 !strcasecmp(choices[i]->choice, "Custom"))
614 {
615 DEBUG_puts("ppdEmitString: Custom size set!");
616
617 bufsize += 37; /* %%BeginFeature: *CustomPageSize True\n */
618 bufsize += 50; /* Five 9-digit numbers + newline */
619 }
620 else if (!strcasecmp(choices[i]->choice, "Custom") &&
621 (coption = ppdFindCustomOption(ppd,
622 choices[i]->option->keyword))
623 != NULL)
624 {
625 bufsize += 17 + strlen(choices[i]->option->keyword) + 6;
626 /* %%BeginFeature: *keyword True\n */
627
628
629 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
630 cparam;
631 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
632 {
633 switch (cparam->type)
634 {
635 case PPD_CUSTOM_CURVE :
636 case PPD_CUSTOM_INVCURVE :
637 case PPD_CUSTOM_POINTS :
638 case PPD_CUSTOM_REAL :
639 case PPD_CUSTOM_INT :
640 bufsize += 10;
641 break;
642
643 case PPD_CUSTOM_PASSCODE :
644 case PPD_CUSTOM_PASSWORD :
645 case PPD_CUSTOM_STRING :
646 bufsize += 3 + 4 * strlen(cparam->current.custom_string);
647 break;
648 }
649 }
650 }
651 else
652 bufsize += 17 + strlen(choices[i]->option->keyword) + 1 +
653 strlen(choices[i]->choice) + 1;
654 /* %%BeginFeature: *keyword choice\n */
655
656 bufsize += 13; /* %%EndFeature\n */
657 bufsize += 22; /* } stopped cleartomark\n */
658 }
659
660 if (choices[i]->code)
661 bufsize += strlen(choices[i]->code) + 1;
662 else
663 bufsize += strlen(ppd_custom_code);
664 }
665
666 /*
667 * Allocate memory...
668 */
669
670 DEBUG_printf(("ppdEmitString: Allocating %d bytes for string...\n", bufsize));
671
672 if ((buffer = calloc(1, bufsize)) == NULL)
673 {
674 free(choices);
675 return (NULL);
676 }
677
678 bufend = buffer + bufsize - 1;
679 loc = localeconv();
680
681 /*
682 * Copy the option code to the buffer...
683 */
684
685 for (i = 0, bufptr = buffer; i < count; i ++, bufptr += strlen(bufptr))
686 if (section != PPD_ORDER_EXIT && section != PPD_ORDER_JCL)
687 {
688 /*
689 * Add wrapper commands to prevent printer errors for unsupported
690 * options...
691 */
692
693 strlcpy(bufptr, "[{\n", bufend - bufptr + 1);
694 bufptr += 3;
695
696 /*
697 * Send DSC comments with option...
698 */
699
700 DEBUG_printf(("Adding code for %s=%s...\n", choices[i]->option->keyword,
701 choices[i]->choice));
702
703 if ((!strcasecmp(choices[i]->option->keyword, "PageSize") ||
704 !strcasecmp(choices[i]->option->keyword, "PageRegion")) &&
705 !strcasecmp(choices[i]->choice, "Custom"))
706 {
707 /*
708 * Variable size; write out standard size options, using the
709 * parameter positions defined in the PPD file...
710 */
711
712 ppd_attr_t *attr; /* PPD attribute */
713 int pos, /* Position of custom value */
714 orientation; /* Orientation to use */
715 float values[5]; /* Values for custom command */
716
717
718 strlcpy(bufptr, "%%BeginFeature: *CustomPageSize True\n",
719 bufend - bufptr + 1);
720 bufptr += 37;
721
722 size = ppdPageSize(ppd, "Custom");
723
724 memset(values, 0, sizeof(values));
725
726 if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize", "Width")) != NULL)
727 {
728 pos = atoi(attr->value) - 1;
729
730 if (pos < 0 || pos > 4)
731 pos = 0;
732 }
733 else
734 pos = 0;
735
736 values[pos] = size->width;
737
738 if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize", "Height")) != NULL)
739 {
740 pos = atoi(attr->value) - 1;
741
742 if (pos < 0 || pos > 4)
743 pos = 1;
744 }
745 else
746 pos = 1;
747
748 values[pos] = size->length;
749
750 /*
751 * According to the Adobe PPD specification, an orientation of 1
752 * will produce a print that comes out upside-down with the X
753 * axis perpendicular to the direction of feed, which is exactly
754 * what we want to be consistent with non-PS printers.
755 *
756 * We could also use an orientation of 3 to produce output that
757 * comes out rightside-up (this is the default for many large format
758 * printer PPDs), however for consistency we will stick with the
759 * value 1.
760 *
761 * If we wanted to get fancy, we could use orientations of 0 or
762 * 2 and swap the width and length, however we don't want to get
763 * fancy, we just want it to work consistently.
764 *
765 * The orientation value is range limited by the Orientation
766 * parameter definition, so certain non-PS printer drivers that
767 * only support an Orientation of 0 will get the value 0 as
768 * expected.
769 */
770
771 orientation = 1;
772
773 if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize",
774 "Orientation")) != NULL)
775 {
776 int min_orient, max_orient; /* Minimum and maximum orientations */
777
778
779 if (sscanf(attr->value, "%d%*s%d%d", &pos, &min_orient,
780 &max_orient) != 3)
781 pos = 4;
782 else
783 {
784 pos --;
785
786 if (pos < 0 || pos > 4)
787 pos = 4;
788
789 if (orientation > max_orient)
790 orientation = max_orient;
791 else if (orientation < min_orient)
792 orientation = min_orient;
793 }
794 }
795 else
796 pos = 4;
797
798 values[pos] = (float)orientation;
799
800 for (pos = 0; pos < 5; pos ++)
801 {
802 bufptr = _cupsStrFormatd(bufptr, bufend, values[pos], loc);
803 *bufptr++ = '\n';
804 }
805
806 if (!choices[i]->code)
807 {
808 /*
809 * This can happen with certain buggy PPD files that don't include
810 * a CustomPageSize command sequence... We just use a generic
811 * Level 2 command sequence...
812 */
813
814 strlcpy(bufptr, ppd_custom_code, bufend - bufptr + 1);
815 bufptr += strlen(bufptr);
816 }
817 }
818 else if (!strcasecmp(choices[i]->choice, "Custom") &&
819 (coption = ppdFindCustomOption(ppd,
820 choices[i]->option->keyword))
821 != NULL)
822 {
823 /*
824 * Custom option...
825 */
826
827 const char *s; /* Pointer into string value */
828
829
830 snprintf(bufptr, bufend - bufptr + 1,
831 "%%%%BeginFeature: *Custom%s True\n", coption->keyword);
832 bufptr += strlen(bufptr);
833
834 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
835 cparam;
836 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
837 {
838 switch (cparam->type)
839 {
840 case PPD_CUSTOM_CURVE :
841 case PPD_CUSTOM_INVCURVE :
842 case PPD_CUSTOM_POINTS :
843 case PPD_CUSTOM_REAL :
844 bufptr = _cupsStrFormatd(bufptr, bufend,
845 cparam->current.custom_real, loc);
846 *bufptr++ = '\n';
847 break;
848
849 case PPD_CUSTOM_INT :
850 snprintf(bufptr, bufend - bufptr + 1, "%d\n",
851 cparam->current.custom_int);
852 bufptr += strlen(bufptr);
853 break;
854
855 case PPD_CUSTOM_PASSCODE :
856 case PPD_CUSTOM_PASSWORD :
857 case PPD_CUSTOM_STRING :
858 *bufptr++ = '(';
859
860 for (s = cparam->current.custom_string; *s; s ++)
861 if (*s < ' ' || *s == '(' || *s == ')' || *s >= 127)
862 {
863 snprintf(bufptr, bufend - bufptr + 1, "\\%03o", *s & 255);
864 bufptr += strlen(bufptr);
865 }
866 else
867 *bufptr++ = *s;
868
869 *bufptr++ = ')';
870 *bufptr++ = '\n';
871 break;
872 }
873 }
874 }
875 else
876 {
877 snprintf(bufptr, bufend - bufptr + 1, "%%%%BeginFeature: *%s %s\n",
878 choices[i]->option->keyword, choices[i]->choice);
879 bufptr += strlen(bufptr);
880 }
881
882 if (choices[i]->code && choices[i]->code[0])
883 {
884 j = (int)strlen(choices[i]->code);
885 memcpy(bufptr, choices[i]->code, j);
886 bufptr += j;
887
888 if (choices[i]->code[j - 1] != '\n')
889 *bufptr++ = '\n';
890 }
891
892 strlcpy(bufptr, "%%EndFeature\n"
893 "} stopped cleartomark\n", bufend - bufptr + 1);
894 bufptr += strlen(bufptr);
895
896 DEBUG_printf(("ppdEmitString: Offset in string is %d...\n",
897 bufptr - buffer));
898 }
899 else
900 {
901 strlcpy(bufptr, choices[i]->code, bufend - bufptr + 1);
902 bufptr += strlen(bufptr);
903 }
904
905 /*
906 * Nul-terminate, free, and return...
907 */
908
909 *bufptr = '\0';
910
911 free(choices);
912
913 return (buffer);
914 }
915
916
917 /*
918 * 'ppd_handle_media()' - Handle media selection...
919 */
920
921 static void
922 ppd_handle_media(ppd_file_t *ppd)
923 {
924 ppd_choice_t *manual_feed, /* ManualFeed choice, if any */
925 *input_slot, /* InputSlot choice, if any */
926 *page; /* PageSize/PageRegion */
927 ppd_size_t *size; /* Current media size */
928 ppd_attr_t *rpr; /* RequiresPageRegion value */
929
930
931 /*
932 * This function determines if the user has selected a media source
933 * via the InputSlot or ManualFeed options; if so, it marks the
934 * PageRegion option corresponding to the current media size.
935 * Otherwise it marks the PageSize option.
936 */
937
938 if ((size = ppdPageSize(ppd, NULL)) == NULL)
939 return;
940
941 manual_feed = ppdFindMarkedChoice(ppd, "ManualFeed");
942 input_slot = ppdFindMarkedChoice(ppd, "InputSlot");
943
944 if (input_slot != NULL)
945 rpr = ppdFindAttr(ppd, "RequiresPageRegion", input_slot->choice);
946 else
947 rpr = NULL;
948
949 if (!rpr)
950 rpr = ppdFindAttr(ppd, "RequiresPageRegion", "All");
951
952 if (!strcasecmp(size->name, "Custom") || (!manual_feed && !input_slot) ||
953 !((manual_feed && !strcasecmp(manual_feed->choice, "True")) ||
954 (input_slot && input_slot->code && input_slot->code[0])))
955 {
956 /*
957 * Manual feed was not selected and/or the input slot selection does
958 * not contain any PostScript code. Use the PageSize option...
959 */
960
961 ppdMarkOption(ppd, "PageSize", size->name);
962 }
963 else
964 {
965 /*
966 * Manual feed was selected and/or the input slot selection contains
967 * PostScript code. Use the PageRegion option...
968 */
969
970 ppdMarkOption(ppd, "PageRegion", size->name);
971
972 /*
973 * RequiresPageRegion does not apply to manual feed so we need to
974 * check that we are not doing manual feed before unmarking PageRegion.
975 */
976
977 if (!(manual_feed && !strcasecmp(manual_feed->choice, "True")) &&
978 ((rpr && rpr->value && !strcmp(rpr->value, "False")) ||
979 (!rpr && !ppd->num_filters)))
980 {
981 /*
982 * Either the PPD file specifies no PageRegion code or the PPD file
983 * not for a CUPS raster driver and thus defaults to no PageRegion
984 * code... Unmark the PageRegion choice so that we don't output the
985 * code...
986 */
987
988 page = ppdFindMarkedChoice(ppd, "PageRegion");
989
990 if (page)
991 page->marked = 0;
992 }
993 }
994 }
995
996
997 /*
998 * End of "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $".
999 */