]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/emit.c
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
72 * The choices array should be freed using @code free@ when you are
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 */
81 return (ppdCollect2(ppd
, section
, 0.0, choices
));
86 * 'ppdCollect2()' - Collect all marked options that reside in the
87 * specified section and minimum order.
89 * The choices array should be freed using @code free@ when you are
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 */
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 */
109 DEBUG_printf(("ppdCollect2(ppd=%p, section=%d, min_order=%f, choices=%p)\n",
110 ppd
, section
, min_order
, choices
));
112 if (!ppd
|| !choices
)
121 * Allocate memory for up to N selected choices...
125 if ((collect
= calloc(sizeof(ppd_choice_t
*),
126 cupsArrayCount(ppd
->marked
))) == NULL
)
132 if ((orders
= calloc(sizeof(float), cupsArrayCount(ppd
->marked
))) == NULL
)
140 * Loop through all options and add choices as needed...
143 for (c
= (ppd_choice_t
*)cupsArrayFirst(ppd
->marked
);
145 c
= (ppd_choice_t
*)cupsArrayNext(ppd
->marked
))
147 csection
= c
->option
->section
;
148 corder
= c
->option
->order
;
150 if (!strcmp(c
->choice
, "Custom"))
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 */
160 for (attr
= ppdFindAttr(ppd
, "NonUIOrderDependency", NULL
);
162 attr
= ppdFindNextAttr(ppd
, "NonUIOrderDependency", NULL
))
164 sscanf(attr
->value
, "%f%16s%41s%40s", &aorder
, asection
, amain
,
166 !strncmp(amain
, "*Custom", 7) &&
167 !strcmp(amain
+ 7, c
->option
->keyword
) && !strcmp(aoption
, "True"))
170 * Use this NonUIOrderDependency...
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
;
186 csection
= PPD_ORDER_ANY
;
192 if (csection
== section
&& corder
>= min_order
)
195 orders
[count
] = corder
;
201 * If we have more than 1 marked choice, sort them...
206 int i
, j
; /* Looping vars */
208 for (i
= 0; i
< (count
- 1); i
++)
209 for (j
= i
+ 1; j
< count
; j
++)
210 if (orders
[i
] > orders
[j
])
214 collect
[i
] = collect
[j
];
215 orders
[i
] = orders
[j
];
223 DEBUG_printf(("ppdCollect2: %d marked choices...\n", count
));
226 * Return the array and number of choices; if 0, free the array since
245 * 'ppdEmit()' - Emit code for marked options to a file.
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 */
253 return (ppdEmitAfterOrder(ppd
, fp
, section
, 0, 0.0));
258 * 'ppdEmitAfterOrder()' - Emit a subset of the code for marked options to a file.
260 * When "limit" is non-zero, this function only emits options whose
261 * OrderDependency value is greater than or equal to "min_order".
263 * When "limit" is zero, this function is identical to ppdEmit().
268 int /* O - 0 on success, -1 on failure */
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 */
276 char *buffer
; /* Option code */
277 int status
; /* Return status */
281 * Range check input...
291 buffer
= ppdEmitString(ppd
, section
, min_order
);
294 * Write it as needed and return...
299 status
= fputs(buffer
, fp
) < 0 ? -1 : 0;
311 * 'ppdEmitFd()' - Emit code for marked options to a file.
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 */
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 */
327 * Range check input...
337 buffer
= ppdEmitString(ppd
, section
, 0.0);
340 * Write it as needed and return...
345 buflength
= strlen(buffer
);
349 while (buflength
> 0)
352 if ((bytes
= (ssize_t
)write(fd
, bufptr
, (unsigned)buflength
)) < 0)
354 if ((bytes
= write(fd
, bufptr
, buflength
)) < 0)
357 if (errno
== EAGAIN
|| errno
== EINTR
)
367 status
= bytes
< 0 ? -1 : 0;
379 * 'ppdEmitJCL()' - Emit code for JCL options to a file.
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 */
389 char *ptr
; /* Pointer into JCL string */
390 char temp
[81]; /* Local title string */
394 * Range check the input...
397 if (!ppd
|| !ppd
->jcl_begin
|| !ppd
->jcl_ps
)
401 * See if the printer supports HP PJL...
404 if (!strncmp(ppd
->jcl_begin
, "\033%-12345X@", 10))
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...
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.
415 ppd_attr_t
*charset
; /* PJL charset */
418 if ((charset
= ppdFindAttr(ppd
, "cupsPJLCharset", NULL
)) != NULL
)
420 if (!charset
->value
|| strcasecmp(charset
->value
, "UTF-8"))
424 fputs("\033%-12345X@PJL\n", fp
);
425 for (ptr
= ppd
->jcl_begin
+ 9; *ptr
;)
426 if (!strncmp(ptr
, "@PJL JOB", 8))
429 * Skip job command...
457 * Eliminate any path info from the job title...
460 if ((ptr
= strrchr(title
, '/')) != NULL
)
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.
468 strlcpy(temp
, title
, sizeof(temp
));
470 for (ptr
= temp
; *ptr
; ptr
++)
473 else if (charset
&& (*ptr
& 128))
477 * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
480 fprintf(fp
, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp
,
482 fprintf(fp
, "@PJL RDYMSG DISPLAY = \"%d %s %s\"\n", job_id
, user
, temp
);
485 fputs(ppd
->jcl_begin
, fp
);
487 ppdEmit(ppd
, fp
, PPD_ORDER_JCL
);
488 fputs(ppd
->jcl_ps
, fp
);
495 * 'ppdEmitJCLEnd()' - Emit JCLEnd code to a file.
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 */
505 * Range check the input...
513 if (ppd
->num_filters
== 0)
520 * See if the printer supports HP PJL...
523 if (!strncmp(ppd
->jcl_end
, "\033%-12345X@", 10))
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...
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.
534 fputs("\033%-12345X@PJL\n", fp
);
535 fputs("@PJL RDYMSG DISPLAY = \"READY\"\n", fp
);
536 fputs(ppd
->jcl_end
+ 9, fp
);
539 fputs(ppd
->jcl_end
, fp
);
546 * 'ppdEmitString()' - Get a string containing the code for marked options.
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
553 * The return string is allocated on the heap and should be freed using
554 * @code free@ when you are done with it.
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 */
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 */
577 DEBUG_printf(("ppdEmitString(ppd=%p, section=%d, min_order=%f)\n",
578 ppd
, section
, min_order
));
581 * Range check input...
588 * Use PageSize or PageRegion as required...
591 ppd_handle_media(ppd
);
594 * Collect the options we need to emit...
597 if ((count
= ppdCollect2(ppd
, section
, min_order
, &choices
)) == 0)
601 * Count the number of bytes that are required to hold all of the
605 for (i
= 0, bufsize
= 1; i
< count
; i
++)
607 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
609 bufsize
+= 3; /* [{\n */
611 if ((!strcasecmp(choices
[i
]->option
->keyword
, "PageSize") ||
612 !strcasecmp(choices
[i
]->option
->keyword
, "PageRegion")) &&
613 !strcasecmp(choices
[i
]->choice
, "Custom"))
615 DEBUG_puts("ppdEmitString: Custom size set!");
617 bufsize
+= 37; /* %%BeginFeature: *CustomPageSize True\n */
618 bufsize
+= 50; /* Five 9-digit numbers + newline */
620 else if (!strcasecmp(choices
[i
]->choice
, "Custom") &&
621 (coption
= ppdFindCustomOption(ppd
,
622 choices
[i
]->option
->keyword
))
625 bufsize
+= 17 + strlen(choices
[i
]->option
->keyword
) + 6;
626 /* %%BeginFeature: *keyword True\n */
629 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
631 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
633 switch (cparam
->type
)
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
:
643 case PPD_CUSTOM_PASSCODE
:
644 case PPD_CUSTOM_PASSWORD
:
645 case PPD_CUSTOM_STRING
:
646 bufsize
+= 3 + 4 * strlen(cparam
->current
.custom_string
);
652 bufsize
+= 17 + strlen(choices
[i
]->option
->keyword
) + 1 +
653 strlen(choices
[i
]->choice
) + 1;
654 /* %%BeginFeature: *keyword choice\n */
656 bufsize
+= 13; /* %%EndFeature\n */
657 bufsize
+= 22; /* } stopped cleartomark\n */
660 if (choices
[i
]->code
)
661 bufsize
+= strlen(choices
[i
]->code
) + 1;
663 bufsize
+= strlen(ppd_custom_code
);
670 DEBUG_printf(("ppdEmitString: Allocating %d bytes for string...\n", bufsize
));
672 if ((buffer
= calloc(1, bufsize
)) == NULL
)
678 bufend
= buffer
+ bufsize
- 1;
682 * Copy the option code to the buffer...
685 for (i
= 0, bufptr
= buffer
; i
< count
; i
++, bufptr
+= strlen(bufptr
))
686 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
689 * Add wrapper commands to prevent printer errors for unsupported
693 strlcpy(bufptr
, "[{\n", bufend
- bufptr
+ 1);
697 * Send DSC comments with option...
700 DEBUG_printf(("Adding code for %s=%s...\n", choices
[i
]->option
->keyword
,
701 choices
[i
]->choice
));
703 if ((!strcasecmp(choices
[i
]->option
->keyword
, "PageSize") ||
704 !strcasecmp(choices
[i
]->option
->keyword
, "PageRegion")) &&
705 !strcasecmp(choices
[i
]->choice
, "Custom"))
708 * Variable size; write out standard size options, using the
709 * parameter positions defined in the PPD file...
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 */
718 strlcpy(bufptr
, "%%BeginFeature: *CustomPageSize True\n",
719 bufend
- bufptr
+ 1);
722 size
= ppdPageSize(ppd
, "Custom");
724 memset(values
, 0, sizeof(values
));
726 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Width")) != NULL
)
728 pos
= atoi(attr
->value
) - 1;
730 if (pos
< 0 || pos
> 4)
736 values
[pos
] = size
->width
;
738 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Height")) != NULL
)
740 pos
= atoi(attr
->value
) - 1;
742 if (pos
< 0 || pos
> 4)
748 values
[pos
] = size
->length
;
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.
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
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.
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
773 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize",
774 "Orientation")) != NULL
)
776 int min_orient
, max_orient
; /* Minimum and maximum orientations */
779 if (sscanf(attr
->value
, "%d%*s%d%d", &pos
, &min_orient
,
786 if (pos
< 0 || pos
> 4)
789 if (orientation
> max_orient
)
790 orientation
= max_orient
;
791 else if (orientation
< min_orient
)
792 orientation
= min_orient
;
798 values
[pos
] = (float)orientation
;
800 for (pos
= 0; pos
< 5; pos
++)
802 bufptr
= _cupsStrFormatd(bufptr
, bufend
, values
[pos
], loc
);
806 if (!choices
[i
]->code
)
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...
814 strlcpy(bufptr
, ppd_custom_code
, bufend
- bufptr
+ 1);
815 bufptr
+= strlen(bufptr
);
818 else if (!strcasecmp(choices
[i
]->choice
, "Custom") &&
819 (coption
= ppdFindCustomOption(ppd
,
820 choices
[i
]->option
->keyword
))
827 const char *s
; /* Pointer into string value */
830 snprintf(bufptr
, bufend
- bufptr
+ 1,
831 "%%%%BeginFeature: *Custom%s True\n", coption
->keyword
);
832 bufptr
+= strlen(bufptr
);
834 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
836 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
838 switch (cparam
->type
)
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
);
849 case PPD_CUSTOM_INT
:
850 snprintf(bufptr
, bufend
- bufptr
+ 1, "%d\n",
851 cparam
->current
.custom_int
);
852 bufptr
+= strlen(bufptr
);
855 case PPD_CUSTOM_PASSCODE
:
856 case PPD_CUSTOM_PASSWORD
:
857 case PPD_CUSTOM_STRING
:
860 for (s
= cparam
->current
.custom_string
; *s
; s
++)
861 if (*s
< ' ' || *s
== '(' || *s
== ')' || *s
>= 127)
863 snprintf(bufptr
, bufend
- bufptr
+ 1, "\\%03o", *s
& 255);
864 bufptr
+= strlen(bufptr
);
877 snprintf(bufptr
, bufend
- bufptr
+ 1, "%%%%BeginFeature: *%s %s\n",
878 choices
[i
]->option
->keyword
, choices
[i
]->choice
);
879 bufptr
+= strlen(bufptr
);
882 if (choices
[i
]->code
&& choices
[i
]->code
[0])
884 j
= (int)strlen(choices
[i
]->code
);
885 memcpy(bufptr
, choices
[i
]->code
, j
);
888 if (choices
[i
]->code
[j
- 1] != '\n')
892 strlcpy(bufptr
, "%%EndFeature\n"
893 "} stopped cleartomark\n", bufend
- bufptr
+ 1);
894 bufptr
+= strlen(bufptr
);
896 DEBUG_printf(("ppdEmitString: Offset in string is %d...\n",
901 strlcpy(bufptr
, choices
[i
]->code
, bufend
- bufptr
+ 1);
902 bufptr
+= strlen(bufptr
);
906 * Nul-terminate, free, and return...
918 * 'ppd_handle_media()' - Handle media selection...
922 ppd_handle_media(ppd_file_t
*ppd
)
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 */
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.
938 if ((size
= ppdPageSize(ppd
, NULL
)) == NULL
)
941 manual_feed
= ppdFindMarkedChoice(ppd
, "ManualFeed");
942 input_slot
= ppdFindMarkedChoice(ppd
, "InputSlot");
944 if (input_slot
!= NULL
)
945 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", input_slot
->choice
);
950 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", "All");
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])))
957 * Manual feed was not selected and/or the input slot selection does
958 * not contain any PostScript code. Use the PageSize option...
961 ppdMarkOption(ppd
, "PageSize", size
->name
);
966 * Manual feed was selected and/or the input slot selection contains
967 * PostScript code. Use the PageRegion option...
970 ppdMarkOption(ppd
, "PageRegion", size
->name
);
973 * RequiresPageRegion does not apply to manual feed so we need to
974 * check that we are not doing manual feed before unmarking PageRegion.
977 if (!(manual_feed
&& !strcasecmp(manual_feed
->choice
, "True")) &&
978 ((rpr
&& rpr
->value
&& !strcmp(rpr
->value
, "False")) ||
979 (!rpr
&& !ppd
->num_filters
)))
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
988 page
= ppdFindMarkedChoice(ppd
, "PageRegion");
998 * End of "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $".