]>
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...
33 * ppd_sort() - Sort options by ordering numbers...
37 * Include necessary headers...
46 #if defined(WIN32) || defined(__EMX__)
50 #endif /* WIN32 || __EMX__ */
57 static void ppd_handle_media(ppd_file_t
*ppd
);
58 static int ppd_sort(ppd_choice_t
**c1
, ppd_choice_t
**c2
);
65 static const char ppd_custom_code
[] =
67 "<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n";
71 * 'ppdCollect()' - Collect all marked options that reside in the specified
75 int /* O - Number of options marked */
76 ppdCollect(ppd_file_t
*ppd
, /* I - PPD file data */
77 ppd_section_t section
, /* I - Section to collect */
78 ppd_choice_t
***choices
) /* O - Pointers to choices */
80 return (ppdCollect2(ppd
, section
, 0.0, choices
));
85 * 'ppdCollect2()' - Collect all marked options that reside in the
86 * specified section and minimum order.
91 int /* O - Number of options marked */
92 ppdCollect2(ppd_file_t
*ppd
, /* I - PPD file data */
93 ppd_section_t section
, /* I - Section to collect */
94 float min_order
, /* I - Minimum OrderDependency value */
95 ppd_choice_t
***choices
) /* O - Pointers to choices */
97 int i
, j
, k
, m
; /* Looping vars */
98 ppd_group_t
*g
, /* Current group */
99 *sg
; /* Current sub-group */
100 ppd_option_t
*o
; /* Current option */
101 ppd_choice_t
*c
; /* Current choice */
102 int count
; /* Number of choices collected */
103 ppd_choice_t
**collect
; /* Collected choices */
106 DEBUG_printf(("ppdCollect2(ppd=%p, section=%d, min_order=%f, choices=%p)\n",
107 ppd
, section
, min_order
, choices
));
113 * Allocate memory for up to 1000 selected choices...
117 collect
= calloc(sizeof(ppd_choice_t
*), 1000);
120 * Loop through all options and add choices as needed...
123 for (i
= ppd
->num_groups
, g
= ppd
->groups
; i
> 0; i
--, g
++)
125 for (j
= g
->num_options
, o
= g
->options
; j
> 0; j
--, o
++)
126 if (o
->section
== section
&& o
->order
>= min_order
)
127 for (k
= o
->num_choices
, c
= o
->choices
; k
> 0; k
--, c
++)
128 if (c
->marked
&& count
< 1000)
130 DEBUG_printf(("ppdCollect2: %s=%s marked...\n", o
->keyword
,
136 for (j
= g
->num_subgroups
, sg
= g
->subgroups
; j
> 0; j
--, sg
++)
137 for (k
= sg
->num_options
, o
= sg
->options
; k
> 0; k
--, o
++)
138 if (o
->section
== section
&& o
->order
>= min_order
)
139 for (m
= o
->num_choices
, c
= o
->choices
; m
> 0; m
--, c
++)
140 if (c
->marked
&& count
< 1000)
142 DEBUG_printf(("ppdCollect2: %s=%s marked...\n", o
->keyword
,
150 * If we have more than 1 marked choice, sort them...
154 qsort(collect
, count
, sizeof(ppd_choice_t
*),
155 (int (*)(const void *, const void *))ppd_sort
);
157 DEBUG_printf(("ppdCollect2: %d marked choices...\n", count
));
160 * Return the array and number of choices; if 0, free the array since
179 * 'ppdEmit()' - Emit code for marked options to a file.
182 int /* O - 0 on success, -1 on failure */
183 ppdEmit(ppd_file_t
*ppd
, /* I - PPD file record */
184 FILE *fp
, /* I - File to write to */
185 ppd_section_t section
) /* I - Section to write */
187 return (ppdEmitAfterOrder(ppd
, fp
, section
, 0, 0.0));
192 * 'ppdEmitAfterOrder()' - Emit a subset of the code for marked options to a file.
194 * When "limit" is non-zero, this function only emits options whose
195 * OrderDependency value is greater than or equal to "min_order".
197 * When "limit" is zero, this function is identical to ppdEmit().
202 int /* O - 0 on success, -1 on failure */
204 ppd_file_t
*ppd
, /* I - PPD file record */
205 FILE *fp
, /* I - File to write to */
206 ppd_section_t section
, /* I - Section to write */
207 int limit
, /* I - Non-zero to use min_order */
208 float min_order
) /* I - Lowest OrderDependency */
210 char *buffer
; /* Option code */
211 int status
; /* Return status */
215 * Range check input...
225 buffer
= ppdEmitString(ppd
, section
, min_order
);
228 * Write it as needed and return...
233 status
= fputs(buffer
, fp
) < 0 ? -1 : 0;
245 * 'ppdEmitFd()' - Emit code for marked options to a file.
248 int /* O - 0 on success, -1 on failure */
249 ppdEmitFd(ppd_file_t
*ppd
, /* I - PPD file record */
250 int fd
, /* I - File to write to */
251 ppd_section_t section
) /* I - Section to write */
253 char *buffer
, /* Option code */
254 *bufptr
; /* Pointer into code */
255 size_t buflength
; /* Length of option code */
256 ssize_t bytes
; /* Bytes written */
257 int status
; /* Return status */
261 * Range check input...
271 buffer
= ppdEmitString(ppd
, section
, 0.0);
274 * Write it as needed and return...
279 buflength
= strlen(buffer
);
283 while (buflength
> 0)
286 if ((bytes
= (ssize_t
)write(fd
, bufptr
, (unsigned)buflength
)) < 0)
288 if ((bytes
= write(fd
, bufptr
, buflength
)) < 0)
291 if (errno
== EAGAIN
|| errno
== EINTR
)
301 status
= bytes
< 0 ? -1 : 0;
313 * 'ppdEmitJCL()' - Emit code for JCL options to a file.
316 int /* O - 0 on success, -1 on failure */
317 ppdEmitJCL(ppd_file_t
*ppd
, /* I - PPD file record */
318 FILE *fp
, /* I - File to write to */
319 int job_id
, /* I - Job ID */
320 const char *user
, /* I - Username */
321 const char *title
) /* I - Title */
323 char *ptr
; /* Pointer into JCL string */
324 char temp
[81]; /* Local title string */
328 * Range check the input...
331 if (!ppd
|| !ppd
->jcl_begin
|| !ppd
->jcl_ps
)
335 * See if the printer supports HP PJL...
338 if (!strncmp(ppd
->jcl_begin
, "\033%-12345X@", 10))
341 * This printer uses HP PJL commands for output; filter the output
342 * so that we only have a single "@PJL JOB" command in the header...
344 * To avoid bugs in the PJL implementation of certain vendors' products
345 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
346 * of the PJL commands to initialize PJL processing.
349 ppd_attr_t
*charset
; /* PJL charset */
352 if ((charset
= ppdFindAttr(ppd
, "cupsPJLCharset", NULL
)) != NULL
)
354 if (!charset
->value
|| strcasecmp(charset
->value
, "UTF-8"))
358 fputs("\033%-12345X@PJL\n", fp
);
359 for (ptr
= ppd
->jcl_begin
+ 9; *ptr
;)
360 if (!strncmp(ptr
, "@PJL JOB", 8))
363 * Skip job command...
391 * Eliminate any path info from the job title...
394 if ((ptr
= strrchr(title
, '/')) != NULL
)
398 * Replace double quotes with single quotes and 8-bit characters with
399 * question marks so that the title does not cause a PJL syntax error.
402 strlcpy(temp
, title
, sizeof(temp
));
404 for (ptr
= temp
; *ptr
; ptr
++)
407 else if (charset
&& (*ptr
& 128))
411 * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
414 fprintf(fp
, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp
,
416 fprintf(fp
, "@PJL RDYMSG DISPLAY = \"%d %s %s\"\n", job_id
, user
, temp
);
419 fputs(ppd
->jcl_begin
, fp
);
421 ppdEmit(ppd
, fp
, PPD_ORDER_JCL
);
422 fputs(ppd
->jcl_ps
, fp
);
429 * 'ppdEmitJCLEnd()' - Emit JCLEnd code to a file.
434 int /* O - 0 on success, -1 on failure */
435 ppdEmitJCLEnd(ppd_file_t
*ppd
, /* I - PPD file record */
436 FILE *fp
) /* I - File to write to */
439 * Range check the input...
447 if (ppd
->num_filters
== 0)
454 * See if the printer supports HP PJL...
457 if (!strncmp(ppd
->jcl_end
, "\033%-12345X@", 10))
460 * This printer uses HP PJL commands for output; filter the output
461 * so that we only have a single "@PJL JOB" command in the header...
463 * To avoid bugs in the PJL implementation of certain vendors' products
464 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
465 * of the PJL commands to initialize PJL processing.
468 fputs("\033%-12345X@PJL\n", fp
);
469 fputs("@PJL RDYMSG DISPLAY = \"READY\"\n", fp
);
470 fputs(ppd
->jcl_end
+ 9, fp
);
473 fputs(ppd
->jcl_end
, fp
);
480 * 'ppdEmitString()' - Get a string containing the code for marked options.
482 * When "min_order" is greater than zero, this function only includes options
483 * whose OrderDependency value is greater than or equal to "min_order".
484 * Otherwise, all options in the specified section are included in the
487 * The return string is allocated on the heap and should be freed using
488 * free() when you are done with it.
493 char * /* O - String containing option code */
494 ppdEmitString(ppd_file_t
*ppd
, /* I - PPD file record */
495 ppd_section_t section
, /* I - Section to write */
496 float min_order
) /* I - Lowest OrderDependency */
498 int i
, j
, /* Looping vars */
499 count
; /* Number of choices */
500 ppd_choice_t
**choices
; /* Choices */
501 ppd_size_t
*size
; /* Custom page size */
502 ppd_coption_t
*coption
; /* Custom option */
503 ppd_cparam_t
*cparam
; /* Custom parameter */
504 size_t bufsize
; /* Size of string buffer needed */
505 char *buffer
, /* String buffer */
506 *bufptr
, /* Pointer into buffer */
507 *bufend
; /* End of buffer */
508 struct lconv
*loc
; /* Locale data */
511 DEBUG_printf(("ppdEmitString(ppd=%p, section=%d, min_order=%f)\n",
512 ppd
, section
, min_order
));
515 * Range check input...
522 * Use PageSize or PageRegion as required...
525 ppd_handle_media(ppd
);
528 * Collect the options we need to emit...
531 if ((count
= ppdCollect2(ppd
, section
, min_order
, &choices
)) == 0)
535 * Count the number of bytes that are required to hold all of the
539 for (i
= 0, bufsize
= 1; i
< count
; i
++)
541 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
543 bufsize
+= 3; /* [{\n */
545 if ((!strcasecmp(choices
[i
]->option
->keyword
, "PageSize") ||
546 !strcasecmp(choices
[i
]->option
->keyword
, "PageRegion")) &&
547 !strcasecmp(choices
[i
]->choice
, "Custom"))
549 DEBUG_puts("ppdEmitString: Custom size set!");
551 bufsize
+= 37; /* %%BeginFeature: *CustomPageSize True\n */
552 bufsize
+= 50; /* Five 9-digit numbers + newline */
554 else if (!strcasecmp(choices
[i
]->choice
, "Custom") &&
555 (coption
= ppdFindCustomOption(ppd
,
556 choices
[i
]->option
->keyword
))
559 bufsize
+= 17 + strlen(choices
[i
]->option
->keyword
) + 6;
560 /* %%BeginFeature: *keyword True\n */
563 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
565 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
567 switch (cparam
->type
)
569 case PPD_CUSTOM_CURVE
:
570 case PPD_CUSTOM_INVCURVE
:
571 case PPD_CUSTOM_POINTS
:
572 case PPD_CUSTOM_REAL
:
573 case PPD_CUSTOM_INT
:
577 case PPD_CUSTOM_PASSCODE
:
578 case PPD_CUSTOM_PASSWORD
:
579 case PPD_CUSTOM_STRING
:
580 bufsize
+= 3 + 4 * strlen(cparam
->current
.custom_string
);
586 bufsize
+= 17 + strlen(choices
[i
]->option
->keyword
) + 1 +
587 strlen(choices
[i
]->choice
) + 1;
588 /* %%BeginFeature: *keyword choice\n */
590 bufsize
+= 13; /* %%EndFeature\n */
591 bufsize
+= 22; /* } stopped cleartomark\n */
594 if (choices
[i
]->code
)
595 bufsize
+= strlen(choices
[i
]->code
) + 1;
597 bufsize
+= strlen(ppd_custom_code
);
604 DEBUG_printf(("ppdEmitString: Allocating %d bytes for string...\n", bufsize
));
606 if ((buffer
= calloc(1, bufsize
)) == NULL
)
612 bufend
= buffer
+ bufsize
- 1;
616 * Copy the option code to the buffer...
619 for (i
= 0, bufptr
= buffer
; i
< count
; i
++, bufptr
+= strlen(bufptr
))
620 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
623 * Add wrapper commands to prevent printer errors for unsupported
627 strlcpy(bufptr
, "[{\n", bufend
- bufptr
+ 1);
631 * Send DSC comments with option...
634 DEBUG_printf(("Adding code for %s=%s...\n", choices
[i
]->option
->keyword
,
635 choices
[i
]->choice
));
637 if ((!strcasecmp(choices
[i
]->option
->keyword
, "PageSize") ||
638 !strcasecmp(choices
[i
]->option
->keyword
, "PageRegion")) &&
639 !strcasecmp(choices
[i
]->choice
, "Custom"))
642 * Variable size; write out standard size options, using the
643 * parameter positions defined in the PPD file...
646 ppd_attr_t
*attr
; /* PPD attribute */
647 int pos
, /* Position of custom value */
648 orientation
; /* Orientation to use */
649 float values
[5]; /* Values for custom command */
652 strlcpy(bufptr
, "%%BeginFeature: *CustomPageSize True\n",
653 bufend
- bufptr
+ 1);
656 size
= ppdPageSize(ppd
, "Custom");
658 memset(values
, 0, sizeof(values
));
660 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Width")) != NULL
)
662 pos
= atoi(attr
->value
) - 1;
664 if (pos
< 0 || pos
> 4)
670 values
[pos
] = size
->width
;
672 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Height")) != NULL
)
674 pos
= atoi(attr
->value
) - 1;
676 if (pos
< 0 || pos
> 4)
682 values
[pos
] = size
->length
;
685 * According to the Adobe PPD specification, an orientation of 1
686 * will produce a print that comes out upside-down with the X
687 * axis perpendicular to the direction of feed, which is exactly
688 * what we want to be consistent with non-PS printers.
690 * We could also use an orientation of 3 to produce output that
691 * comes out rightside-up (this is the default for many large format
692 * printer PPDs), however for consistency we will stick with the
695 * If we wanted to get fancy, we could use orientations of 0 or
696 * 2 and swap the width and length, however we don't want to get
697 * fancy, we just want it to work consistently.
699 * The orientation value is range limited by the Orientation
700 * parameter definition, so certain non-PS printer drivers that
701 * only support an Orientation of 0 will get the value 0 as
707 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize",
708 "Orientation")) != NULL
)
710 int min_orient
, max_orient
; /* Minimum and maximum orientations */
713 if (sscanf(attr
->value
, "%d%*s%d%d", &pos
, &min_orient
,
720 if (pos
< 0 || pos
> 4)
723 if (orientation
> max_orient
)
724 orientation
= max_orient
;
725 else if (orientation
< min_orient
)
726 orientation
= min_orient
;
732 values
[pos
] = (float)orientation
;
734 for (pos
= 0; pos
< 5; pos
++)
736 bufptr
= _cupsStrFormatd(bufptr
, bufend
, values
[pos
], loc
);
740 if (!choices
[i
]->code
)
743 * This can happen with certain buggy PPD files that don't include
744 * a CustomPageSize command sequence... We just use a generic
745 * Level 2 command sequence...
748 strlcpy(bufptr
, ppd_custom_code
, bufend
- bufptr
+ 1);
749 bufptr
+= strlen(bufptr
);
752 else if (!strcasecmp(choices
[i
]->choice
, "Custom") &&
753 (coption
= ppdFindCustomOption(ppd
,
754 choices
[i
]->option
->keyword
))
761 const char *s
; /* Pointer into string value */
764 snprintf(bufptr
, bufend
- bufptr
+ 1,
765 "%%%%BeginFeature: *Custom%s True\n", coption
->keyword
);
766 bufptr
+= strlen(bufptr
);
768 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
770 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
772 switch (cparam
->type
)
774 case PPD_CUSTOM_CURVE
:
775 case PPD_CUSTOM_INVCURVE
:
776 case PPD_CUSTOM_POINTS
:
777 case PPD_CUSTOM_REAL
:
778 bufptr
= _cupsStrFormatd(bufptr
, bufend
,
779 cparam
->current
.custom_real
, loc
);
783 case PPD_CUSTOM_INT
:
784 snprintf(bufptr
, bufend
- bufptr
+ 1, "%d\n",
785 cparam
->current
.custom_int
);
786 bufptr
+= strlen(bufptr
);
789 case PPD_CUSTOM_PASSCODE
:
790 case PPD_CUSTOM_PASSWORD
:
791 case PPD_CUSTOM_STRING
:
794 for (s
= cparam
->current
.custom_string
; *s
; s
++)
795 if (*s
< ' ' || *s
== '(' || *s
== ')' || *s
>= 127)
797 snprintf(bufptr
, bufend
- bufptr
+ 1, "\\%03o", *s
& 255);
798 bufptr
+= strlen(bufptr
);
811 snprintf(bufptr
, bufend
- bufptr
+ 1, "%%%%BeginFeature: *%s %s\n",
812 choices
[i
]->option
->keyword
, choices
[i
]->choice
);
813 bufptr
+= strlen(bufptr
);
816 if (choices
[i
]->code
&& choices
[i
]->code
[0])
818 j
= (int)strlen(choices
[i
]->code
);
819 memcpy(bufptr
, choices
[i
]->code
, j
);
822 if (choices
[i
]->code
[j
- 1] != '\n')
826 strlcpy(bufptr
, "%%EndFeature\n"
827 "} stopped cleartomark\n", bufend
- bufptr
+ 1);
828 bufptr
+= strlen(bufptr
);
830 DEBUG_printf(("ppdEmitString: Offset in string is %d...\n",
835 strlcpy(bufptr
, choices
[i
]->code
, bufend
- bufptr
+ 1);
836 bufptr
+= strlen(bufptr
);
840 * Nul-terminate, free, and return...
852 * 'ppd_handle_media()' - Handle media selection...
856 ppd_handle_media(ppd_file_t
*ppd
)
858 ppd_choice_t
*manual_feed
, /* ManualFeed choice, if any */
859 *input_slot
, /* InputSlot choice, if any */
860 *page
; /* PageSize/PageRegion */
861 ppd_size_t
*size
; /* Current media size */
862 ppd_attr_t
*rpr
; /* RequiresPageRegion value */
866 * This function determines if the user has selected a media source
867 * via the InputSlot or ManualFeed options; if so, it marks the
868 * PageRegion option corresponding to the current media size.
869 * Otherwise it marks the PageSize option.
872 if ((size
= ppdPageSize(ppd
, NULL
)) == NULL
)
875 manual_feed
= ppdFindMarkedChoice(ppd
, "ManualFeed");
876 input_slot
= ppdFindMarkedChoice(ppd
, "InputSlot");
878 if (input_slot
!= NULL
)
879 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", input_slot
->choice
);
884 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", "All");
886 if (!strcasecmp(size
->name
, "Custom") || (!manual_feed
&& !input_slot
) ||
887 !((manual_feed
&& !strcasecmp(manual_feed
->choice
, "True")) ||
888 (input_slot
&& input_slot
->code
&& input_slot
->code
[0])))
891 * Manual feed was not selected and/or the input slot selection does
892 * not contain any PostScript code. Use the PageSize option...
895 ppdMarkOption(ppd
, "PageSize", size
->name
);
900 * Manual feed was selected and/or the input slot selection contains
901 * PostScript code. Use the PageRegion option...
904 ppdMarkOption(ppd
, "PageRegion", size
->name
);
907 * RequiresPageRegion does not apply to manual feed so we need to
908 * check that we are not doing manual feed before unmarking PageRegion.
911 if (!(manual_feed
&& !strcasecmp(manual_feed
->choice
, "True")) &&
912 ((rpr
&& rpr
->value
&& !strcmp(rpr
->value
, "False")) ||
913 (!rpr
&& !ppd
->num_filters
)))
916 * Either the PPD file specifies no PageRegion code or the PPD file
917 * not for a CUPS raster driver and thus defaults to no PageRegion
918 * code... Unmark the PageRegion choice so that we don't output the
922 page
= ppdFindMarkedChoice(ppd
, "PageRegion");
932 * 'ppd_sort()' - Sort options by ordering numbers...
935 static int /* O - -1 if c1 < c2, 0 if equal, 1 otherwise */
936 ppd_sort(ppd_choice_t
**c1
, /* I - First choice */
937 ppd_choice_t
**c2
) /* I - Second choice */
939 if ((*c1
)->option
->order
< (*c2
)->option
->order
)
941 else if ((*c1
)->option
->order
> (*c2
)->option
->order
)
949 * End of "$Id: emit.c 6649 2007-07-11 21:46:42Z mike $".