]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/emit.c
5ac480f35e07a3dcaa03f4756d9ded5045d875d0
2 * "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $"
4 * PPD code emission routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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/".
15 * PostScript is a trademark of Adobe Systems, Inc.
17 * This file is subject to the Apple OS-Developed Software exception.
21 * ppdCollect() - Collect all marked options that reside in the
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
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...
36 * Include necessary headers...
45 #if defined(WIN32) || defined(__EMX__)
49 #endif /* WIN32 || __EMX__ */
56 static void ppd_handle_media(ppd_file_t
*ppd
);
63 static const char ppd_custom_code
[] =
65 "<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n";
69 * 'ppdCollect()' - Collect all marked options that reside in the specified
73 int /* O - Number of options marked */
74 ppdCollect(ppd_file_t
*ppd
, /* I - PPD file data */
75 ppd_section_t section
, /* I - Section to collect */
76 ppd_choice_t
***choices
) /* O - Pointers to choices */
78 return (ppdCollect2(ppd
, section
, 0.0, choices
));
83 * 'ppdCollect2()' - Collect all marked options that reside in the
84 * specified section and minimum order.
89 int /* O - Number of options marked */
90 ppdCollect2(ppd_file_t
*ppd
, /* I - PPD file data */
91 ppd_section_t section
, /* I - Section to collect */
92 float min_order
, /* I - Minimum OrderDependency value */
93 ppd_choice_t
***choices
) /* O - Pointers to choices */
95 ppd_choice_t
*c
; /* Current choice */
96 ppd_section_t csection
; /* Current section */
97 float corder
; /* Current OrderDependency value */
98 int count
; /* Number of choices collected */
99 ppd_choice_t
**collect
; /* Collected choices */
100 float *orders
; /* Collected order values */
103 DEBUG_printf(("ppdCollect2(ppd=%p, section=%d, min_order=%f, choices=%p)\n",
104 ppd
, section
, min_order
, choices
));
106 if (!ppd
|| !choices
)
115 * Allocate memory for up to N selected choices...
119 if ((collect
= calloc(sizeof(ppd_choice_t
*),
120 cupsArrayCount(ppd
->marked
))) == NULL
)
126 if ((orders
= calloc(sizeof(float), cupsArrayCount(ppd
->marked
))) == NULL
)
134 * Loop through all options and add choices as needed...
137 for (c
= (ppd_choice_t
*)cupsArrayFirst(ppd
->marked
);
139 c
= (ppd_choice_t
*)cupsArrayNext(ppd
->marked
))
141 csection
= c
->option
->section
;
142 corder
= c
->option
->order
;
144 if (!strcmp(c
->choice
, "Custom"))
146 ppd_attr_t
*attr
; /* NonUIOrderDependency value */
147 float aorder
; /* Order value */
148 char asection
[17], /* Section name */
149 amain
[PPD_MAX_NAME
+ 1],
150 aoption
[PPD_MAX_NAME
];
151 /* *CustomFoo and True */
154 for (attr
= ppdFindAttr(ppd
, "NonUIOrderDependency", NULL
);
156 attr
= ppdFindNextAttr(ppd
, "NonUIOrderDependency", NULL
))
158 sscanf(attr
->value
, "%f%16s%41s%40s", &aorder
, asection
, amain
,
160 !strncmp(amain
, "*Custom", 7) &&
161 !strcmp(amain
+ 7, c
->option
->keyword
) && !strcmp(aoption
, "True"))
164 * Use this NonUIOrderDependency...
169 if (!strcmp(asection
, "DocumentSetup"))
170 csection
= PPD_ORDER_DOCUMENT
;
171 else if (!strcmp(asection
, "ExitServer"))
172 csection
= PPD_ORDER_EXIT
;
173 else if (!strcmp(asection
, "JCLSetup"))
174 csection
= PPD_ORDER_JCL
;
175 else if (!strcmp(asection
, "PageSetup"))
176 csection
= PPD_ORDER_PAGE
;
177 else if (!strcmp(asection
, "Prolog"))
178 csection
= PPD_ORDER_PROLOG
;
180 csection
= PPD_ORDER_ANY
;
186 if (csection
== section
&& corder
>= min_order
)
189 orders
[count
] = corder
;
195 * If we have more than 1 marked choice, sort them...
200 int i
, j
; /* Looping vars */
202 for (i
= 0; i
< (count
- 1); i
++)
203 for (j
= i
+ 1; j
< count
; j
++)
204 if (orders
[i
] > orders
[j
])
208 collect
[i
] = collect
[j
];
209 orders
[i
] = orders
[j
];
217 DEBUG_printf(("ppdCollect2: %d marked choices...\n", count
));
220 * Return the array and number of choices; if 0, free the array since
239 * 'ppdEmit()' - Emit code for marked options to a file.
242 int /* O - 0 on success, -1 on failure */
243 ppdEmit(ppd_file_t
*ppd
, /* I - PPD file record */
244 FILE *fp
, /* I - File to write to */
245 ppd_section_t section
) /* I - Section to write */
247 return (ppdEmitAfterOrder(ppd
, fp
, section
, 0, 0.0));
252 * 'ppdEmitAfterOrder()' - Emit a subset of the code for marked options to a file.
254 * When "limit" is non-zero, this function only emits options whose
255 * OrderDependency value is greater than or equal to "min_order".
257 * When "limit" is zero, this function is identical to ppdEmit().
262 int /* O - 0 on success, -1 on failure */
264 ppd_file_t
*ppd
, /* I - PPD file record */
265 FILE *fp
, /* I - File to write to */
266 ppd_section_t section
, /* I - Section to write */
267 int limit
, /* I - Non-zero to use min_order */
268 float min_order
) /* I - Lowest OrderDependency */
270 char *buffer
; /* Option code */
271 int status
; /* Return status */
275 * Range check input...
285 buffer
= ppdEmitString(ppd
, section
, min_order
);
288 * Write it as needed and return...
293 status
= fputs(buffer
, fp
) < 0 ? -1 : 0;
305 * 'ppdEmitFd()' - Emit code for marked options to a file.
308 int /* O - 0 on success, -1 on failure */
309 ppdEmitFd(ppd_file_t
*ppd
, /* I - PPD file record */
310 int fd
, /* I - File to write to */
311 ppd_section_t section
) /* I - Section to write */
313 char *buffer
, /* Option code */
314 *bufptr
; /* Pointer into code */
315 size_t buflength
; /* Length of option code */
316 ssize_t bytes
; /* Bytes written */
317 int status
; /* Return status */
321 * Range check input...
331 buffer
= ppdEmitString(ppd
, section
, 0.0);
334 * Write it as needed and return...
339 buflength
= strlen(buffer
);
343 while (buflength
> 0)
346 if ((bytes
= (ssize_t
)write(fd
, bufptr
, (unsigned)buflength
)) < 0)
348 if ((bytes
= write(fd
, bufptr
, buflength
)) < 0)
351 if (errno
== EAGAIN
|| errno
== EINTR
)
361 status
= bytes
< 0 ? -1 : 0;
373 * 'ppdEmitJCL()' - Emit code for JCL options to a file.
376 int /* O - 0 on success, -1 on failure */
377 ppdEmitJCL(ppd_file_t
*ppd
, /* I - PPD file record */
378 FILE *fp
, /* I - File to write to */
379 int job_id
, /* I - Job ID */
380 const char *user
, /* I - Username */
381 const char *title
) /* I - Title */
383 char *ptr
; /* Pointer into JCL string */
384 char temp
[81]; /* Local title string */
388 * Range check the input...
391 if (!ppd
|| !ppd
->jcl_begin
|| !ppd
->jcl_ps
)
395 * See if the printer supports HP PJL...
398 if (!strncmp(ppd
->jcl_begin
, "\033%-12345X@", 10))
401 * This printer uses HP PJL commands for output; filter the output
402 * so that we only have a single "@PJL JOB" command in the header...
404 * To avoid bugs in the PJL implementation of certain vendors' products
405 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
406 * of the PJL commands to initialize PJL processing.
409 ppd_attr_t
*charset
; /* PJL charset */
412 if ((charset
= ppdFindAttr(ppd
, "cupsPJLCharset", NULL
)) != NULL
)
414 if (!charset
->value
|| strcasecmp(charset
->value
, "UTF-8"))
418 fputs("\033%-12345X@PJL\n", fp
);
419 for (ptr
= ppd
->jcl_begin
+ 9; *ptr
;)
420 if (!strncmp(ptr
, "@PJL JOB", 8))
423 * Skip job command...
451 * Eliminate any path info from the job title...
454 if ((ptr
= strrchr(title
, '/')) != NULL
)
458 * Replace double quotes with single quotes and 8-bit characters with
459 * question marks so that the title does not cause a PJL syntax error.
462 strlcpy(temp
, title
, sizeof(temp
));
464 for (ptr
= temp
; *ptr
; ptr
++)
467 else if (charset
&& (*ptr
& 128))
471 * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
474 fprintf(fp
, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp
,
476 fprintf(fp
, "@PJL RDYMSG DISPLAY = \"%d %s %s\"\n", job_id
, user
, temp
);
479 fputs(ppd
->jcl_begin
, fp
);
481 ppdEmit(ppd
, fp
, PPD_ORDER_JCL
);
482 fputs(ppd
->jcl_ps
, fp
);
489 * 'ppdEmitJCLEnd()' - Emit JCLEnd code to a file.
494 int /* O - 0 on success, -1 on failure */
495 ppdEmitJCLEnd(ppd_file_t
*ppd
, /* I - PPD file record */
496 FILE *fp
) /* I - File to write to */
499 * Range check the input...
507 if (ppd
->num_filters
== 0)
514 * See if the printer supports HP PJL...
517 if (!strncmp(ppd
->jcl_end
, "\033%-12345X@", 10))
520 * This printer uses HP PJL commands for output; filter the output
521 * so that we only have a single "@PJL JOB" command in the header...
523 * To avoid bugs in the PJL implementation of certain vendors' products
524 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
525 * of the PJL commands to initialize PJL processing.
528 fputs("\033%-12345X@PJL\n", fp
);
529 fputs("@PJL RDYMSG DISPLAY = \"READY\"\n", fp
);
530 fputs(ppd
->jcl_end
+ 9, fp
);
533 fputs(ppd
->jcl_end
, fp
);
540 * 'ppdEmitString()' - Get a string containing the code for marked options.
542 * When "min_order" is greater than zero, this function only includes options
543 * whose OrderDependency value is greater than or equal to "min_order".
544 * Otherwise, all options in the specified section are included in the
547 * The return string is allocated on the heap and should be freed using
548 * free() when you are done with it.
553 char * /* O - String containing option code */
554 ppdEmitString(ppd_file_t
*ppd
, /* I - PPD file record */
555 ppd_section_t section
, /* I - Section to write */
556 float min_order
) /* I - Lowest OrderDependency */
558 int i
, j
, /* Looping vars */
559 count
; /* Number of choices */
560 ppd_choice_t
**choices
; /* Choices */
561 ppd_size_t
*size
; /* Custom page size */
562 ppd_coption_t
*coption
; /* Custom option */
563 ppd_cparam_t
*cparam
; /* Custom parameter */
564 size_t bufsize
; /* Size of string buffer needed */
565 char *buffer
, /* String buffer */
566 *bufptr
, /* Pointer into buffer */
567 *bufend
; /* End of buffer */
568 struct lconv
*loc
; /* Locale data */
571 DEBUG_printf(("ppdEmitString(ppd=%p, section=%d, min_order=%f)\n",
572 ppd
, section
, min_order
));
575 * Range check input...
582 * Use PageSize or PageRegion as required...
585 ppd_handle_media(ppd
);
588 * Collect the options we need to emit...
591 if ((count
= ppdCollect2(ppd
, section
, min_order
, &choices
)) == 0)
595 * Count the number of bytes that are required to hold all of the
599 for (i
= 0, bufsize
= 1; i
< count
; i
++)
601 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
603 bufsize
+= 3; /* [{\n */
605 if ((!strcasecmp(choices
[i
]->option
->keyword
, "PageSize") ||
606 !strcasecmp(choices
[i
]->option
->keyword
, "PageRegion")) &&
607 !strcasecmp(choices
[i
]->choice
, "Custom"))
609 DEBUG_puts("ppdEmitString: Custom size set!");
611 bufsize
+= 37; /* %%BeginFeature: *CustomPageSize True\n */
612 bufsize
+= 50; /* Five 9-digit numbers + newline */
614 else if (!strcasecmp(choices
[i
]->choice
, "Custom") &&
615 (coption
= ppdFindCustomOption(ppd
,
616 choices
[i
]->option
->keyword
))
619 bufsize
+= 17 + strlen(choices
[i
]->option
->keyword
) + 6;
620 /* %%BeginFeature: *keyword True\n */
623 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
625 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
627 switch (cparam
->type
)
629 case PPD_CUSTOM_CURVE
:
630 case PPD_CUSTOM_INVCURVE
:
631 case PPD_CUSTOM_POINTS
:
632 case PPD_CUSTOM_REAL
:
633 case PPD_CUSTOM_INT
:
637 case PPD_CUSTOM_PASSCODE
:
638 case PPD_CUSTOM_PASSWORD
:
639 case PPD_CUSTOM_STRING
:
640 bufsize
+= 3 + 4 * strlen(cparam
->current
.custom_string
);
646 bufsize
+= 17 + strlen(choices
[i
]->option
->keyword
) + 1 +
647 strlen(choices
[i
]->choice
) + 1;
648 /* %%BeginFeature: *keyword choice\n */
650 bufsize
+= 13; /* %%EndFeature\n */
651 bufsize
+= 22; /* } stopped cleartomark\n */
654 if (choices
[i
]->code
)
655 bufsize
+= strlen(choices
[i
]->code
) + 1;
657 bufsize
+= strlen(ppd_custom_code
);
664 DEBUG_printf(("ppdEmitString: Allocating %d bytes for string...\n", bufsize
));
666 if ((buffer
= calloc(1, bufsize
)) == NULL
)
672 bufend
= buffer
+ bufsize
- 1;
676 * Copy the option code to the buffer...
679 for (i
= 0, bufptr
= buffer
; i
< count
; i
++, bufptr
+= strlen(bufptr
))
680 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
683 * Add wrapper commands to prevent printer errors for unsupported
687 strlcpy(bufptr
, "[{\n", bufend
- bufptr
+ 1);
691 * Send DSC comments with option...
694 DEBUG_printf(("Adding code for %s=%s...\n", choices
[i
]->option
->keyword
,
695 choices
[i
]->choice
));
697 if ((!strcasecmp(choices
[i
]->option
->keyword
, "PageSize") ||
698 !strcasecmp(choices
[i
]->option
->keyword
, "PageRegion")) &&
699 !strcasecmp(choices
[i
]->choice
, "Custom"))
702 * Variable size; write out standard size options, using the
703 * parameter positions defined in the PPD file...
706 ppd_attr_t
*attr
; /* PPD attribute */
707 int pos
, /* Position of custom value */
708 orientation
; /* Orientation to use */
709 float values
[5]; /* Values for custom command */
712 strlcpy(bufptr
, "%%BeginFeature: *CustomPageSize True\n",
713 bufend
- bufptr
+ 1);
716 size
= ppdPageSize(ppd
, "Custom");
718 memset(values
, 0, sizeof(values
));
720 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Width")) != NULL
)
722 pos
= atoi(attr
->value
) - 1;
724 if (pos
< 0 || pos
> 4)
730 values
[pos
] = size
->width
;
732 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Height")) != NULL
)
734 pos
= atoi(attr
->value
) - 1;
736 if (pos
< 0 || pos
> 4)
742 values
[pos
] = size
->length
;
745 * According to the Adobe PPD specification, an orientation of 1
746 * will produce a print that comes out upside-down with the X
747 * axis perpendicular to the direction of feed, which is exactly
748 * what we want to be consistent with non-PS printers.
750 * We could also use an orientation of 3 to produce output that
751 * comes out rightside-up (this is the default for many large format
752 * printer PPDs), however for consistency we will stick with the
755 * If we wanted to get fancy, we could use orientations of 0 or
756 * 2 and swap the width and length, however we don't want to get
757 * fancy, we just want it to work consistently.
759 * The orientation value is range limited by the Orientation
760 * parameter definition, so certain non-PS printer drivers that
761 * only support an Orientation of 0 will get the value 0 as
767 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize",
768 "Orientation")) != NULL
)
770 int min_orient
, max_orient
; /* Minimum and maximum orientations */
773 if (sscanf(attr
->value
, "%d%*s%d%d", &pos
, &min_orient
,
780 if (pos
< 0 || pos
> 4)
783 if (orientation
> max_orient
)
784 orientation
= max_orient
;
785 else if (orientation
< min_orient
)
786 orientation
= min_orient
;
792 values
[pos
] = (float)orientation
;
794 for (pos
= 0; pos
< 5; pos
++)
796 bufptr
= _cupsStrFormatd(bufptr
, bufend
, values
[pos
], loc
);
800 if (!choices
[i
]->code
)
803 * This can happen with certain buggy PPD files that don't include
804 * a CustomPageSize command sequence... We just use a generic
805 * Level 2 command sequence...
808 strlcpy(bufptr
, ppd_custom_code
, bufend
- bufptr
+ 1);
809 bufptr
+= strlen(bufptr
);
812 else if (!strcasecmp(choices
[i
]->choice
, "Custom") &&
813 (coption
= ppdFindCustomOption(ppd
,
814 choices
[i
]->option
->keyword
))
821 const char *s
; /* Pointer into string value */
824 snprintf(bufptr
, bufend
- bufptr
+ 1,
825 "%%%%BeginFeature: *Custom%s True\n", coption
->keyword
);
826 bufptr
+= strlen(bufptr
);
828 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
830 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
832 switch (cparam
->type
)
834 case PPD_CUSTOM_CURVE
:
835 case PPD_CUSTOM_INVCURVE
:
836 case PPD_CUSTOM_POINTS
:
837 case PPD_CUSTOM_REAL
:
838 bufptr
= _cupsStrFormatd(bufptr
, bufend
,
839 cparam
->current
.custom_real
, loc
);
843 case PPD_CUSTOM_INT
:
844 snprintf(bufptr
, bufend
- bufptr
+ 1, "%d\n",
845 cparam
->current
.custom_int
);
846 bufptr
+= strlen(bufptr
);
849 case PPD_CUSTOM_PASSCODE
:
850 case PPD_CUSTOM_PASSWORD
:
851 case PPD_CUSTOM_STRING
:
854 for (s
= cparam
->current
.custom_string
; *s
; s
++)
855 if (*s
< ' ' || *s
== '(' || *s
== ')' || *s
>= 127)
857 snprintf(bufptr
, bufend
- bufptr
+ 1, "\\%03o", *s
& 255);
858 bufptr
+= strlen(bufptr
);
871 snprintf(bufptr
, bufend
- bufptr
+ 1, "%%%%BeginFeature: *%s %s\n",
872 choices
[i
]->option
->keyword
, choices
[i
]->choice
);
873 bufptr
+= strlen(bufptr
);
876 if (choices
[i
]->code
&& choices
[i
]->code
[0])
878 j
= (int)strlen(choices
[i
]->code
);
879 memcpy(bufptr
, choices
[i
]->code
, j
);
882 if (choices
[i
]->code
[j
- 1] != '\n')
886 strlcpy(bufptr
, "%%EndFeature\n"
887 "} stopped cleartomark\n", bufend
- bufptr
+ 1);
888 bufptr
+= strlen(bufptr
);
890 DEBUG_printf(("ppdEmitString: Offset in string is %d...\n",
895 strlcpy(bufptr
, choices
[i
]->code
, bufend
- bufptr
+ 1);
896 bufptr
+= strlen(bufptr
);
900 * Nul-terminate, free, and return...
912 * 'ppd_handle_media()' - Handle media selection...
916 ppd_handle_media(ppd_file_t
*ppd
)
918 ppd_choice_t
*manual_feed
, /* ManualFeed choice, if any */
919 *input_slot
, /* InputSlot choice, if any */
920 *page
; /* PageSize/PageRegion */
921 ppd_size_t
*size
; /* Current media size */
922 ppd_attr_t
*rpr
; /* RequiresPageRegion value */
926 * This function determines if the user has selected a media source
927 * via the InputSlot or ManualFeed options; if so, it marks the
928 * PageRegion option corresponding to the current media size.
929 * Otherwise it marks the PageSize option.
932 if ((size
= ppdPageSize(ppd
, NULL
)) == NULL
)
935 manual_feed
= ppdFindMarkedChoice(ppd
, "ManualFeed");
936 input_slot
= ppdFindMarkedChoice(ppd
, "InputSlot");
938 if (input_slot
!= NULL
)
939 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", input_slot
->choice
);
944 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", "All");
946 if (!strcasecmp(size
->name
, "Custom") || (!manual_feed
&& !input_slot
) ||
947 !((manual_feed
&& !strcasecmp(manual_feed
->choice
, "True")) ||
948 (input_slot
&& input_slot
->code
&& input_slot
->code
[0])))
951 * Manual feed was not selected and/or the input slot selection does
952 * not contain any PostScript code. Use the PageSize option...
955 ppdMarkOption(ppd
, "PageSize", size
->name
);
960 * Manual feed was selected and/or the input slot selection contains
961 * PostScript code. Use the PageRegion option...
964 ppdMarkOption(ppd
, "PageRegion", size
->name
);
967 * RequiresPageRegion does not apply to manual feed so we need to
968 * check that we are not doing manual feed before unmarking PageRegion.
971 if (!(manual_feed
&& !strcasecmp(manual_feed
->choice
, "True")) &&
972 ((rpr
&& rpr
->value
&& !strcmp(rpr
->value
, "False")) ||
973 (!rpr
&& !ppd
->num_filters
)))
976 * Either the PPD file specifies no PageRegion code or the PPD file
977 * not for a CUPS raster driver and thus defaults to no PageRegion
978 * code... Unmark the PageRegion choice so that we don't output the
982 page
= ppdFindMarkedChoice(ppd
, "PageRegion");
992 * End of "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $".