]>
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 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
));
110 * Allocate memory for up to N selected choices...
114 collect
= calloc(sizeof(ppd_choice_t
*), cupsArrayCount(ppd
->marked
));
115 orders
= calloc(sizeof(float), cupsArrayCount(ppd
->marked
));
118 * Loop through all options and add choices as needed...
121 for (c
= (ppd_choice_t
*)cupsArrayFirst(ppd
->marked
);
123 c
= (ppd_choice_t
*)cupsArrayNext(ppd
->marked
))
125 csection
= c
->option
->section
;
126 corder
= c
->option
->order
;
128 if (!strcmp(c
->choice
, "Custom"))
130 ppd_attr_t
*attr
; /* NonUIOrderDependency value */
131 float aorder
; /* Order value */
132 char asection
[17], /* Section name */
133 amain
[PPD_MAX_NAME
+ 1],
134 aoption
[PPD_MAX_NAME
];
135 /* *CustomFoo and True */
138 for (attr
= ppdFindAttr(ppd
, "NonUIOrderDependency", NULL
);
140 attr
= ppdFindNextAttr(ppd
, "NonUIOrderDependency", NULL
))
142 sscanf(attr
->value
, "%f%16s%41s%40s", &aorder
, asection
, amain
,
144 !strncmp(amain
, "*Custom", 7) &&
145 !strcmp(amain
+ 7, c
->option
->keyword
) && !strcmp(aoption
, "True"))
148 * Use this NonUIOrderDependency...
153 if (!strcmp(asection
, "DocumentSetup"))
154 csection
= PPD_ORDER_DOCUMENT
;
155 else if (!strcmp(asection
, "ExitServer"))
156 csection
= PPD_ORDER_EXIT
;
157 else if (!strcmp(asection
, "JCLSetup"))
158 csection
= PPD_ORDER_JCL
;
159 else if (!strcmp(asection
, "PageSetup"))
160 csection
= PPD_ORDER_PAGE
;
161 else if (!strcmp(asection
, "Prolog"))
162 csection
= PPD_ORDER_PROLOG
;
164 csection
= PPD_ORDER_ANY
;
170 if (csection
== section
&& corder
>= min_order
)
173 orders
[count
] = corder
;
179 * If we have more than 1 marked choice, sort them...
184 int i
, j
; /* Looping vars */
186 for (i
= 0; i
< (count
- 1); i
++)
187 for (j
= i
+ 1; j
< count
; j
++)
188 if (orders
[i
] > orders
[j
])
192 collect
[i
] = collect
[j
];
193 orders
[i
] = orders
[j
];
201 DEBUG_printf(("ppdCollect2: %d marked choices...\n", count
));
204 * Return the array and number of choices; if 0, free the array since
223 * 'ppdEmit()' - Emit code for marked options to a file.
226 int /* O - 0 on success, -1 on failure */
227 ppdEmit(ppd_file_t
*ppd
, /* I - PPD file record */
228 FILE *fp
, /* I - File to write to */
229 ppd_section_t section
) /* I - Section to write */
231 return (ppdEmitAfterOrder(ppd
, fp
, section
, 0, 0.0));
236 * 'ppdEmitAfterOrder()' - Emit a subset of the code for marked options to a file.
238 * When "limit" is non-zero, this function only emits options whose
239 * OrderDependency value is greater than or equal to "min_order".
241 * When "limit" is zero, this function is identical to ppdEmit().
246 int /* O - 0 on success, -1 on failure */
248 ppd_file_t
*ppd
, /* I - PPD file record */
249 FILE *fp
, /* I - File to write to */
250 ppd_section_t section
, /* I - Section to write */
251 int limit
, /* I - Non-zero to use min_order */
252 float min_order
) /* I - Lowest OrderDependency */
254 char *buffer
; /* Option code */
255 int status
; /* Return status */
259 * Range check input...
269 buffer
= ppdEmitString(ppd
, section
, min_order
);
272 * Write it as needed and return...
277 status
= fputs(buffer
, fp
) < 0 ? -1 : 0;
289 * 'ppdEmitFd()' - Emit code for marked options to a file.
292 int /* O - 0 on success, -1 on failure */
293 ppdEmitFd(ppd_file_t
*ppd
, /* I - PPD file record */
294 int fd
, /* I - File to write to */
295 ppd_section_t section
) /* I - Section to write */
297 char *buffer
, /* Option code */
298 *bufptr
; /* Pointer into code */
299 size_t buflength
; /* Length of option code */
300 ssize_t bytes
; /* Bytes written */
301 int status
; /* Return status */
305 * Range check input...
315 buffer
= ppdEmitString(ppd
, section
, 0.0);
318 * Write it as needed and return...
323 buflength
= strlen(buffer
);
327 while (buflength
> 0)
330 if ((bytes
= (ssize_t
)write(fd
, bufptr
, (unsigned)buflength
)) < 0)
332 if ((bytes
= write(fd
, bufptr
, buflength
)) < 0)
335 if (errno
== EAGAIN
|| errno
== EINTR
)
345 status
= bytes
< 0 ? -1 : 0;
357 * 'ppdEmitJCL()' - Emit code for JCL options to a file.
360 int /* O - 0 on success, -1 on failure */
361 ppdEmitJCL(ppd_file_t
*ppd
, /* I - PPD file record */
362 FILE *fp
, /* I - File to write to */
363 int job_id
, /* I - Job ID */
364 const char *user
, /* I - Username */
365 const char *title
) /* I - Title */
367 char *ptr
; /* Pointer into JCL string */
368 char temp
[81]; /* Local title string */
372 * Range check the input...
375 if (!ppd
|| !ppd
->jcl_begin
|| !ppd
->jcl_ps
)
379 * See if the printer supports HP PJL...
382 if (!strncmp(ppd
->jcl_begin
, "\033%-12345X@", 10))
385 * This printer uses HP PJL commands for output; filter the output
386 * so that we only have a single "@PJL JOB" command in the header...
388 * To avoid bugs in the PJL implementation of certain vendors' products
389 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
390 * of the PJL commands to initialize PJL processing.
393 ppd_attr_t
*charset
; /* PJL charset */
396 if ((charset
= ppdFindAttr(ppd
, "cupsPJLCharset", NULL
)) != NULL
)
398 if (!charset
->value
|| strcasecmp(charset
->value
, "UTF-8"))
402 fputs("\033%-12345X@PJL\n", fp
);
403 for (ptr
= ppd
->jcl_begin
+ 9; *ptr
;)
404 if (!strncmp(ptr
, "@PJL JOB", 8))
407 * Skip job command...
435 * Eliminate any path info from the job title...
438 if ((ptr
= strrchr(title
, '/')) != NULL
)
442 * Replace double quotes with single quotes and 8-bit characters with
443 * question marks so that the title does not cause a PJL syntax error.
446 strlcpy(temp
, title
, sizeof(temp
));
448 for (ptr
= temp
; *ptr
; ptr
++)
451 else if (charset
&& (*ptr
& 128))
455 * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
458 fprintf(fp
, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp
,
460 fprintf(fp
, "@PJL RDYMSG DISPLAY = \"%d %s %s\"\n", job_id
, user
, temp
);
463 fputs(ppd
->jcl_begin
, fp
);
465 ppdEmit(ppd
, fp
, PPD_ORDER_JCL
);
466 fputs(ppd
->jcl_ps
, fp
);
473 * 'ppdEmitJCLEnd()' - Emit JCLEnd code to a file.
478 int /* O - 0 on success, -1 on failure */
479 ppdEmitJCLEnd(ppd_file_t
*ppd
, /* I - PPD file record */
480 FILE *fp
) /* I - File to write to */
483 * Range check the input...
491 if (ppd
->num_filters
== 0)
498 * See if the printer supports HP PJL...
501 if (!strncmp(ppd
->jcl_end
, "\033%-12345X@", 10))
504 * This printer uses HP PJL commands for output; filter the output
505 * so that we only have a single "@PJL JOB" command in the header...
507 * To avoid bugs in the PJL implementation of certain vendors' products
508 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
509 * of the PJL commands to initialize PJL processing.
512 fputs("\033%-12345X@PJL\n", fp
);
513 fputs("@PJL RDYMSG DISPLAY = \"READY\"\n", fp
);
514 fputs(ppd
->jcl_end
+ 9, fp
);
517 fputs(ppd
->jcl_end
, fp
);
524 * 'ppdEmitString()' - Get a string containing the code for marked options.
526 * When "min_order" is greater than zero, this function only includes options
527 * whose OrderDependency value is greater than or equal to "min_order".
528 * Otherwise, all options in the specified section are included in the
531 * The return string is allocated on the heap and should be freed using
532 * free() when you are done with it.
537 char * /* O - String containing option code */
538 ppdEmitString(ppd_file_t
*ppd
, /* I - PPD file record */
539 ppd_section_t section
, /* I - Section to write */
540 float min_order
) /* I - Lowest OrderDependency */
542 int i
, j
, /* Looping vars */
543 count
; /* Number of choices */
544 ppd_choice_t
**choices
; /* Choices */
545 ppd_size_t
*size
; /* Custom page size */
546 ppd_coption_t
*coption
; /* Custom option */
547 ppd_cparam_t
*cparam
; /* Custom parameter */
548 size_t bufsize
; /* Size of string buffer needed */
549 char *buffer
, /* String buffer */
550 *bufptr
, /* Pointer into buffer */
551 *bufend
; /* End of buffer */
552 struct lconv
*loc
; /* Locale data */
555 DEBUG_printf(("ppdEmitString(ppd=%p, section=%d, min_order=%f)\n",
556 ppd
, section
, min_order
));
559 * Range check input...
566 * Use PageSize or PageRegion as required...
569 ppd_handle_media(ppd
);
572 * Collect the options we need to emit...
575 if ((count
= ppdCollect2(ppd
, section
, min_order
, &choices
)) == 0)
579 * Count the number of bytes that are required to hold all of the
583 for (i
= 0, bufsize
= 1; i
< count
; i
++)
585 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
587 bufsize
+= 3; /* [{\n */
589 if ((!strcasecmp(choices
[i
]->option
->keyword
, "PageSize") ||
590 !strcasecmp(choices
[i
]->option
->keyword
, "PageRegion")) &&
591 !strcasecmp(choices
[i
]->choice
, "Custom"))
593 DEBUG_puts("ppdEmitString: Custom size set!");
595 bufsize
+= 37; /* %%BeginFeature: *CustomPageSize True\n */
596 bufsize
+= 50; /* Five 9-digit numbers + newline */
598 else if (!strcasecmp(choices
[i
]->choice
, "Custom") &&
599 (coption
= ppdFindCustomOption(ppd
,
600 choices
[i
]->option
->keyword
))
603 bufsize
+= 17 + strlen(choices
[i
]->option
->keyword
) + 6;
604 /* %%BeginFeature: *keyword True\n */
607 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
609 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
611 switch (cparam
->type
)
613 case PPD_CUSTOM_CURVE
:
614 case PPD_CUSTOM_INVCURVE
:
615 case PPD_CUSTOM_POINTS
:
616 case PPD_CUSTOM_REAL
:
617 case PPD_CUSTOM_INT
:
621 case PPD_CUSTOM_PASSCODE
:
622 case PPD_CUSTOM_PASSWORD
:
623 case PPD_CUSTOM_STRING
:
624 bufsize
+= 3 + 4 * strlen(cparam
->current
.custom_string
);
630 bufsize
+= 17 + strlen(choices
[i
]->option
->keyword
) + 1 +
631 strlen(choices
[i
]->choice
) + 1;
632 /* %%BeginFeature: *keyword choice\n */
634 bufsize
+= 13; /* %%EndFeature\n */
635 bufsize
+= 22; /* } stopped cleartomark\n */
638 if (choices
[i
]->code
)
639 bufsize
+= strlen(choices
[i
]->code
) + 1;
641 bufsize
+= strlen(ppd_custom_code
);
648 DEBUG_printf(("ppdEmitString: Allocating %d bytes for string...\n", bufsize
));
650 if ((buffer
= calloc(1, bufsize
)) == NULL
)
656 bufend
= buffer
+ bufsize
- 1;
660 * Copy the option code to the buffer...
663 for (i
= 0, bufptr
= buffer
; i
< count
; i
++, bufptr
+= strlen(bufptr
))
664 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
667 * Add wrapper commands to prevent printer errors for unsupported
671 strlcpy(bufptr
, "[{\n", bufend
- bufptr
+ 1);
675 * Send DSC comments with option...
678 DEBUG_printf(("Adding code for %s=%s...\n", choices
[i
]->option
->keyword
,
679 choices
[i
]->choice
));
681 if ((!strcasecmp(choices
[i
]->option
->keyword
, "PageSize") ||
682 !strcasecmp(choices
[i
]->option
->keyword
, "PageRegion")) &&
683 !strcasecmp(choices
[i
]->choice
, "Custom"))
686 * Variable size; write out standard size options, using the
687 * parameter positions defined in the PPD file...
690 ppd_attr_t
*attr
; /* PPD attribute */
691 int pos
, /* Position of custom value */
692 orientation
; /* Orientation to use */
693 float values
[5]; /* Values for custom command */
696 strlcpy(bufptr
, "%%BeginFeature: *CustomPageSize True\n",
697 bufend
- bufptr
+ 1);
700 size
= ppdPageSize(ppd
, "Custom");
702 memset(values
, 0, sizeof(values
));
704 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Width")) != NULL
)
706 pos
= atoi(attr
->value
) - 1;
708 if (pos
< 0 || pos
> 4)
714 values
[pos
] = size
->width
;
716 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Height")) != NULL
)
718 pos
= atoi(attr
->value
) - 1;
720 if (pos
< 0 || pos
> 4)
726 values
[pos
] = size
->length
;
729 * According to the Adobe PPD specification, an orientation of 1
730 * will produce a print that comes out upside-down with the X
731 * axis perpendicular to the direction of feed, which is exactly
732 * what we want to be consistent with non-PS printers.
734 * We could also use an orientation of 3 to produce output that
735 * comes out rightside-up (this is the default for many large format
736 * printer PPDs), however for consistency we will stick with the
739 * If we wanted to get fancy, we could use orientations of 0 or
740 * 2 and swap the width and length, however we don't want to get
741 * fancy, we just want it to work consistently.
743 * The orientation value is range limited by the Orientation
744 * parameter definition, so certain non-PS printer drivers that
745 * only support an Orientation of 0 will get the value 0 as
751 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize",
752 "Orientation")) != NULL
)
754 int min_orient
, max_orient
; /* Minimum and maximum orientations */
757 if (sscanf(attr
->value
, "%d%*s%d%d", &pos
, &min_orient
,
764 if (pos
< 0 || pos
> 4)
767 if (orientation
> max_orient
)
768 orientation
= max_orient
;
769 else if (orientation
< min_orient
)
770 orientation
= min_orient
;
776 values
[pos
] = (float)orientation
;
778 for (pos
= 0; pos
< 5; pos
++)
780 bufptr
= _cupsStrFormatd(bufptr
, bufend
, values
[pos
], loc
);
784 if (!choices
[i
]->code
)
787 * This can happen with certain buggy PPD files that don't include
788 * a CustomPageSize command sequence... We just use a generic
789 * Level 2 command sequence...
792 strlcpy(bufptr
, ppd_custom_code
, bufend
- bufptr
+ 1);
793 bufptr
+= strlen(bufptr
);
796 else if (!strcasecmp(choices
[i
]->choice
, "Custom") &&
797 (coption
= ppdFindCustomOption(ppd
,
798 choices
[i
]->option
->keyword
))
805 const char *s
; /* Pointer into string value */
808 snprintf(bufptr
, bufend
- bufptr
+ 1,
809 "%%%%BeginFeature: *Custom%s True\n", coption
->keyword
);
810 bufptr
+= strlen(bufptr
);
812 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
814 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
816 switch (cparam
->type
)
818 case PPD_CUSTOM_CURVE
:
819 case PPD_CUSTOM_INVCURVE
:
820 case PPD_CUSTOM_POINTS
:
821 case PPD_CUSTOM_REAL
:
822 bufptr
= _cupsStrFormatd(bufptr
, bufend
,
823 cparam
->current
.custom_real
, loc
);
827 case PPD_CUSTOM_INT
:
828 snprintf(bufptr
, bufend
- bufptr
+ 1, "%d\n",
829 cparam
->current
.custom_int
);
830 bufptr
+= strlen(bufptr
);
833 case PPD_CUSTOM_PASSCODE
:
834 case PPD_CUSTOM_PASSWORD
:
835 case PPD_CUSTOM_STRING
:
838 for (s
= cparam
->current
.custom_string
; *s
; s
++)
839 if (*s
< ' ' || *s
== '(' || *s
== ')' || *s
>= 127)
841 snprintf(bufptr
, bufend
- bufptr
+ 1, "\\%03o", *s
& 255);
842 bufptr
+= strlen(bufptr
);
855 snprintf(bufptr
, bufend
- bufptr
+ 1, "%%%%BeginFeature: *%s %s\n",
856 choices
[i
]->option
->keyword
, choices
[i
]->choice
);
857 bufptr
+= strlen(bufptr
);
860 if (choices
[i
]->code
&& choices
[i
]->code
[0])
862 j
= (int)strlen(choices
[i
]->code
);
863 memcpy(bufptr
, choices
[i
]->code
, j
);
866 if (choices
[i
]->code
[j
- 1] != '\n')
870 strlcpy(bufptr
, "%%EndFeature\n"
871 "} stopped cleartomark\n", bufend
- bufptr
+ 1);
872 bufptr
+= strlen(bufptr
);
874 DEBUG_printf(("ppdEmitString: Offset in string is %d...\n",
879 strlcpy(bufptr
, choices
[i
]->code
, bufend
- bufptr
+ 1);
880 bufptr
+= strlen(bufptr
);
884 * Nul-terminate, free, and return...
896 * 'ppd_handle_media()' - Handle media selection...
900 ppd_handle_media(ppd_file_t
*ppd
)
902 ppd_choice_t
*manual_feed
, /* ManualFeed choice, if any */
903 *input_slot
, /* InputSlot choice, if any */
904 *page
; /* PageSize/PageRegion */
905 ppd_size_t
*size
; /* Current media size */
906 ppd_attr_t
*rpr
; /* RequiresPageRegion value */
910 * This function determines if the user has selected a media source
911 * via the InputSlot or ManualFeed options; if so, it marks the
912 * PageRegion option corresponding to the current media size.
913 * Otherwise it marks the PageSize option.
916 if ((size
= ppdPageSize(ppd
, NULL
)) == NULL
)
919 manual_feed
= ppdFindMarkedChoice(ppd
, "ManualFeed");
920 input_slot
= ppdFindMarkedChoice(ppd
, "InputSlot");
922 if (input_slot
!= NULL
)
923 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", input_slot
->choice
);
928 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", "All");
930 if (!strcasecmp(size
->name
, "Custom") || (!manual_feed
&& !input_slot
) ||
931 !((manual_feed
&& !strcasecmp(manual_feed
->choice
, "True")) ||
932 (input_slot
&& input_slot
->code
&& input_slot
->code
[0])))
935 * Manual feed was not selected and/or the input slot selection does
936 * not contain any PostScript code. Use the PageSize option...
939 ppdMarkOption(ppd
, "PageSize", size
->name
);
944 * Manual feed was selected and/or the input slot selection contains
945 * PostScript code. Use the PageRegion option...
948 ppdMarkOption(ppd
, "PageRegion", size
->name
);
951 * RequiresPageRegion does not apply to manual feed so we need to
952 * check that we are not doing manual feed before unmarking PageRegion.
955 if (!(manual_feed
&& !strcasecmp(manual_feed
->choice
, "True")) &&
956 ((rpr
&& rpr
->value
&& !strcmp(rpr
->value
, "False")) ||
957 (!rpr
&& !ppd
->num_filters
)))
960 * Either the PPD file specifies no PageRegion code or the PPD file
961 * not for a CUPS raster driver and thus defaults to no PageRegion
962 * code... Unmark the PageRegion choice so that we don't output the
966 page
= ppdFindMarkedChoice(ppd
, "PageRegion");
976 * End of "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $".