]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/emit.c
2 * "$Id: emit.c 5238 2006-03-07 04:41:42Z mike $"
4 * PPD code emission routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * PostScript is a trademark of Adobe Systems, Inc.
26 * This file is subject to the Apple OS-Developed Software exception.
30 * ppdCollect() - Collect all marked options that reside in the
32 * ppdCollect2() - Collect all marked options that reside in the
33 * specified section and minimum order.
34 * ppdEmit() - Emit code for marked options to a file.
35 * ppdEmitAfterOrder() - Emit a subset of the code for marked options to a
37 * ppdEmitFd() - Emit code for marked options to a file.
38 * ppdEmitJCL() - Emit code for JCL options to a file.
39 * ppdEmitJCLEnd() - Emit JCLEnd code to a file.
40 * ppdEmitString() - Get a string containing the code for marked options.
41 * ppd_handle_media() - Handle media selection...
42 * ppd_sort() - Sort options by ordering numbers...
46 * Include necessary headers...
54 #if defined(WIN32) || defined(__EMX__)
58 #endif /* WIN32 || __EMX__ */
65 static void ppd_handle_media(ppd_file_t
*ppd
);
66 static int ppd_sort(ppd_choice_t
**c1
, ppd_choice_t
**c2
);
73 static const char ppd_custom_code
[] =
75 "<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n";
79 * 'ppdCollect()' - Collect all marked options that reside in the specified
83 int /* O - Number of options marked */
84 ppdCollect(ppd_file_t
*ppd
, /* I - PPD file data */
85 ppd_section_t section
, /* I - Section to collect */
86 ppd_choice_t
***choices
) /* O - Pointers to choices */
88 return (ppdCollect2(ppd
, section
, 0.0, choices
));
93 * 'ppdCollect2()' - Collect all marked options that reside in the
94 * specified section and minimum order.
99 int /* O - Number of options marked */
100 ppdCollect2(ppd_file_t
*ppd
, /* I - PPD file data */
101 ppd_section_t section
, /* I - Section to collect */
102 float min_order
, /* I - Minimum OrderDependency value */
103 ppd_choice_t
***choices
) /* O - Pointers to choices */
105 int i
, j
, k
, m
; /* Looping vars */
106 ppd_group_t
*g
, /* Current group */
107 *sg
; /* Current sub-group */
108 ppd_option_t
*o
; /* Current option */
109 ppd_choice_t
*c
; /* Current choice */
110 int count
; /* Number of choices collected */
111 ppd_choice_t
**collect
; /* Collected choices */
118 * Allocate memory for up to 1000 selected choices...
122 collect
= calloc(sizeof(ppd_choice_t
*), 1000);
125 * Loop through all options and add choices as needed...
128 for (i
= ppd
->num_groups
, g
= ppd
->groups
; i
> 0; i
--, g
++)
130 for (j
= g
->num_options
, o
= g
->options
; j
> 0; j
--, o
++)
131 if (o
->section
== section
&& o
->order
>= min_order
)
132 for (k
= o
->num_choices
, c
= o
->choices
; k
> 0; k
--, c
++)
133 if (c
->marked
&& count
< 1000)
139 for (j
= g
->num_subgroups
, sg
= g
->subgroups
; j
> 0; j
--, sg
++)
140 for (k
= sg
->num_options
, o
= sg
->options
; k
> 0; k
--, o
++)
141 if (o
->section
== section
&& o
->order
>= min_order
)
142 for (m
= o
->num_choices
, c
= o
->choices
; m
> 0; m
--, c
++)
143 if (c
->marked
&& count
< 1000)
151 * If we have more than 1 marked choice, sort them...
155 qsort(collect
, count
, sizeof(ppd_choice_t
*),
156 (int (*)(const void *, const void *))ppd_sort
);
159 * Return the array and number of choices; if 0, free the array since
178 * 'ppdEmit()' - Emit code for marked options to a file.
181 int /* O - 0 on success, -1 on failure */
182 ppdEmit(ppd_file_t
*ppd
, /* I - PPD file record */
183 FILE *fp
, /* I - File to write to */
184 ppd_section_t section
) /* I - Section to write */
186 return (ppdEmitAfterOrder(ppd
, fp
, section
, 0, 0.0));
191 * 'ppdEmitAfterOrder()' - Emit a subset of the code for marked options to a file.
193 * When "limit" is non-zero, this function only emits options whose
194 * OrderDependency value is greater than or equal to "min_order".
196 * When "limit" is zero, this function is identical to ppdEmit().
201 int /* O - 0 on success, -1 on failure */
203 ppd_file_t
*ppd
, /* I - PPD file record */
204 FILE *fp
, /* I - File to write to */
205 ppd_section_t section
, /* I - Section to write */
206 int limit
, /* I - Non-zero to use min_order */
207 float min_order
) /* I - Lowest OrderDependency */
209 char *buffer
; /* Option code */
210 int status
; /* Return status */
214 * Range check input...
224 buffer
= ppdEmitString(ppd
, section
, min_order
);
227 * Write it as needed and return...
232 status
= fputs(buffer
, fp
) < 0 ? -1 : 0;
244 * 'ppdEmitFd()' - Emit code for marked options to a file.
247 int /* O - 0 on success, -1 on failure */
248 ppdEmitFd(ppd_file_t
*ppd
, /* I - PPD file record */
249 int fd
, /* I - File to write to */
250 ppd_section_t section
) /* I - Section to write */
252 char *buffer
, /* Option code */
253 *bufptr
; /* Pointer into code */
254 size_t buflength
; /* Length of option code */
255 ssize_t bytes
; /* Bytes written */
256 int status
; /* Return status */
260 * Range check input...
270 buffer
= ppdEmitString(ppd
, section
, 0.0);
273 * Write it as needed and return...
278 buflength
= strlen(buffer
);
282 while (buflength
> 0)
284 if ((bytes
= write(fd
, bufptr
, buflength
)) < 0)
286 if (errno
== EAGAIN
|| errno
== EINTR
)
296 status
= bytes
< 0 ? -1 : 0;
308 * 'ppdEmitJCL()' - Emit code for JCL options to a file.
311 int /* O - 0 on success, -1 on failure */
312 ppdEmitJCL(ppd_file_t
*ppd
, /* I - PPD file record */
313 FILE *fp
, /* I - File to write to */
314 int job_id
, /* I - Job ID */
315 const char *user
, /* I - Username */
316 const char *title
) /* I - Title */
318 char *ptr
; /* Pointer into JCL string */
319 char temp
[81]; /* Local title string */
323 * Range check the input...
326 if (!ppd
|| !ppd
->jcl_begin
|| !ppd
->jcl_ps
)
330 * See if the printer supports HP PJL...
333 if (!strncmp(ppd
->jcl_begin
, "\033%-12345X@", 10))
336 * This printer uses HP PJL commands for output; filter the output
337 * so that we only have a single "@PJL JOB" command in the header...
339 * To avoid bugs in the PJL implementation of certain vendors' products
340 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
341 * of the PJL commands to initialize PJL processing.
344 fputs("\033%-12345X@PJL\n", fp
);
345 for (ptr
= ppd
->jcl_begin
+ 9; *ptr
;)
346 if (!strncmp(ptr
, "@PJL JOB", 8))
349 * Skip job command...
377 * Eliminate any path info from the job title...
380 if ((ptr
= strrchr(title
, '/')) != NULL
)
384 * Replace double quotes with single quotes so that the title
385 * does not cause a PJL syntax error.
388 strlcpy(temp
, title
, sizeof(temp
));
390 for (ptr
= temp
; *ptr
; ptr
++)
395 * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
398 fprintf(fp
, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp
,
400 fprintf(fp
, "@PJL RDYMSG DISPLAY = \"%d %s %s\"\n", job_id
, user
, temp
);
403 fputs(ppd
->jcl_begin
, fp
);
405 ppdEmit(ppd
, fp
, PPD_ORDER_JCL
);
406 fputs(ppd
->jcl_ps
, fp
);
413 * 'ppdEmitJCLEnd()' - Emit JCLEnd code to a file.
418 int /* O - 0 on success, -1 on failure */
419 ppdEmitJCLEnd(ppd_file_t
*ppd
, /* I - PPD file record */
420 FILE *fp
) /* I - File to write to */
423 * Range check the input...
431 if (ppd
->num_filters
== 0)
438 * See if the printer supports HP PJL...
441 if (!strncmp(ppd
->jcl_end
, "\033%-12345X@", 10))
444 * This printer uses HP PJL commands for output; filter the output
445 * so that we only have a single "@PJL JOB" command in the header...
447 * To avoid bugs in the PJL implementation of certain vendors' products
448 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
449 * of the PJL commands to initialize PJL processing.
452 fputs("\033%-12345X@PJL\n", fp
);
453 fputs("@PJL RDYMSG DISPLAY = \"READY\"\n", fp
);
454 fputs(ppd
->jcl_end
+ 9, fp
);
457 fputs(ppd
->jcl_end
, fp
);
464 * 'ppdEmitString()' - Get a string containing the code for marked options.
466 * When "min_order" is greater than zero, this function only includes options
467 * whose OrderDependency value is greater than or equal to "min_order".
468 * Otherwise, all options in the specified section are included in the
471 * The return string is allocated on the heap and should be freed using
472 * free() when you are done with it.
477 char * /* O - String containing option code */
478 ppdEmitString(ppd_file_t
*ppd
, /* I - PPD file record */
479 ppd_section_t section
, /* I - Section to write */
480 float min_order
) /* I - Lowest OrderDependency */
482 int i
, j
, /* Looping vars */
483 count
; /* Number of choices */
484 ppd_choice_t
**choices
; /* Choices */
485 ppd_size_t
*size
; /* Custom page size */
486 ppd_coption_t
*coption
; /* Custom option */
487 ppd_cparam_t
*cparam
; /* Custom parameter */
488 size_t bufsize
; /* Size of string buffer needed */
489 char *buffer
, /* String buffer */
490 *bufptr
, /* Pointer into buffer */
491 *bufend
; /* End of buffer */
492 struct lconv
*loc
; /* Locale data */
496 * Range check input...
503 * Use PageSize or PageRegion as required...
506 ppd_handle_media(ppd
);
509 * Collect the options we need to emit...
512 if ((count
= ppdCollect2(ppd
, section
, min_order
, &choices
)) == 0)
516 * Count the number of bytes that are required to hold all of the
520 for (i
= 0, bufsize
= 1; i
< count
; i
++)
522 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
524 bufsize
+= 3; /* [{\n */
526 if ((!strcasecmp(choices
[i
]->option
->keyword
, "PageSize") ||
527 !strcasecmp(choices
[i
]->option
->keyword
, "PageRegion")) &&
528 !strcasecmp(choices
[i
]->choice
, "Custom"))
530 bufsize
+= 37; /* %%BeginFeature: *CustomPageSize True */
531 bufsize
+= 50; /* Five 9-digit numbers + newline */
533 else if (!strcasecmp(choices
[i
]->choice
, "Custom") &&
534 (coption
= ppdFindCustomOption(ppd
,
535 choices
[i
]->option
->keyword
))
538 bufsize
+= 23 + strlen(choices
[i
]->option
->keyword
);
539 /* %%BeginFeature: *keyword True */
542 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
544 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
546 switch (cparam
->type
)
548 case PPD_CUSTOM_CURVE
:
549 case PPD_CUSTOM_INVCURVE
:
550 case PPD_CUSTOM_POINTS
:
551 case PPD_CUSTOM_REAL
:
552 case PPD_CUSTOM_INT
:
556 case PPD_CUSTOM_PASSCODE
:
557 case PPD_CUSTOM_PASSWORD
:
558 case PPD_CUSTOM_STRING
:
559 bufsize
+= 3 + 4 * strlen(cparam
->current
.custom_string
);
565 bufsize
+= 19 + strlen(choices
[i
]->option
->keyword
) +
566 strlen(choices
[i
]->choice
);
567 /* %%BeginFeature: *keyword choice */
569 bufsize
+= 13; /* %%EndFeature\n */
570 bufsize
+= 22; /* } stopped cleartomark\n */
573 if (choices
[i
]->code
)
574 bufsize
+= strlen(choices
[i
]->code
);
576 bufsize
+= strlen(ppd_custom_code
);
583 if ((buffer
= calloc(1, bufsize
)) == NULL
)
589 bufend
= buffer
+ bufsize
- 1;
593 * Copy the option code to the buffer...
596 for (i
= 0, bufptr
= buffer
; i
< count
; i
++, bufptr
+= strlen(bufptr
))
597 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
600 * Add wrapper commands to prevent printer errors for unsupported
604 strlcpy(bufptr
, "[{\n", bufend
- bufptr
+ 1);
608 * Send DSC comments with option...
611 if ((!strcasecmp(choices
[i
]->option
->keyword
, "PageSize") ||
612 !strcasecmp(choices
[i
]->option
->keyword
, "PageRegion")) &&
613 !strcasecmp(choices
[i
]->choice
, "Custom"))
616 * Variable size; write out standard size options, using the
617 * parameter positions defined in the PPD file...
620 ppd_attr_t
*attr
; /* PPD attribute */
621 int pos
, /* Position of custom value */
622 orientation
; /* Orientation to use */
623 float values
[5]; /* Values for custom command */
626 strlcpy(bufptr
, "%%BeginFeature: *CustomPageSize True\n",
627 bufend
- bufptr
+ 1);
630 size
= ppdPageSize(ppd
, "Custom");
632 memset(values
, 0, sizeof(values
));
634 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Width")) != NULL
)
636 pos
= atoi(attr
->value
) - 1;
638 if (pos
< 0 || pos
> 4)
644 values
[pos
] = size
->width
;
646 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Height")) != NULL
)
648 pos
= atoi(attr
->value
) - 1;
650 if (pos
< 0 || pos
> 4)
656 values
[pos
] = size
->length
;
659 * According to the Adobe PPD specification, an orientation of 1
660 * will produce a print that comes out upside-down with the X
661 * axis perpendicular to the direction of feed, which is exactly
662 * what we want to be consistent with non-PS printers.
664 * We could also use an orientation of 3 to produce output that
665 * comes out rightside-up (this is the default for many large format
666 * printer PPDs), however for consistency we will stick with the
669 * If we wanted to get fancy, we could use orientations of 0 or
670 * 2 and swap the width and length, however we don't want to get
671 * fancy, we just want it to work consistently.
673 * The orientation value is range limited by the Orientation
674 * parameter definition, so certain non-PS printer drivers that
675 * only support an Orientation of 0 will get the value 0 as
681 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize",
682 "Orientation")) != NULL
)
684 int min_orient
, max_orient
; /* Minimum and maximum orientations */
687 if (sscanf(attr
->value
, "%d%*s%d%d", &pos
, &min_orient
,
694 if (pos
< 0 || pos
> 4)
697 if (orientation
> max_orient
)
698 orientation
= max_orient
;
699 else if (orientation
< min_orient
)
700 orientation
= min_orient
;
706 values
[pos
] = orientation
;
708 for (pos
= 0; pos
< 5; pos
++)
710 bufptr
= _cupsStrFormatd(bufptr
, bufend
, values
[pos
], loc
);
714 if (!choices
[i
]->code
)
717 * This can happen with certain buggy PPD files that don't include
718 * a CustomPageSize command sequence... We just use a generic
719 * Level 2 command sequence...
722 strlcpy(bufptr
, ppd_custom_code
, bufend
- bufptr
+ 1);
723 bufptr
+= strlen(bufptr
);
726 else if (!strcasecmp(choices
[i
]->choice
, "Custom") &&
727 (coption
= ppdFindCustomOption(ppd
,
728 choices
[i
]->option
->keyword
))
735 const char *s
; /* Pointer into string value */
738 snprintf(bufptr
, bufend
- bufptr
+ 1,
739 "%%%%BeginFeature: *Custom%s True\n", coption
->keyword
);
740 bufptr
+= strlen(bufptr
);
742 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
744 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
746 switch (cparam
->type
)
748 case PPD_CUSTOM_CURVE
:
749 case PPD_CUSTOM_INVCURVE
:
750 case PPD_CUSTOM_POINTS
:
751 case PPD_CUSTOM_REAL
:
752 bufptr
= _cupsStrFormatd(bufptr
, bufend
,
753 cparam
->current
.custom_real
, loc
);
757 case PPD_CUSTOM_INT
:
758 snprintf(bufptr
, bufend
- bufptr
+ 1, "%d\n",
759 cparam
->current
.custom_int
);
760 bufptr
+= strlen(bufptr
);
763 case PPD_CUSTOM_PASSCODE
:
764 case PPD_CUSTOM_PASSWORD
:
765 case PPD_CUSTOM_STRING
:
768 for (s
= cparam
->current
.custom_string
; *s
; s
++)
769 if (*s
< ' ' || *s
== '(' || *s
== ')' || *s
>= 127)
771 snprintf(bufptr
, bufend
- bufptr
+ 1, "\\%03o", *s
& 255);
772 bufptr
+= strlen(bufptr
);
785 snprintf(bufptr
, bufend
- bufptr
+ 1, "%%%%BeginFeature: *%s %s\n",
786 choices
[i
]->option
->keyword
, choices
[i
]->choice
);
787 bufptr
+= strlen(bufptr
);
790 if (choices
[i
]->code
&& choices
[i
]->code
[0])
792 j
= strlen(choices
[i
]->code
);
793 memcpy(bufptr
, choices
[i
]->code
, j
);
796 if (choices
[i
]->code
[j
- 1] != '\n')
800 strlcpy(bufptr
, "%%EndFeature\n"
801 "} stopped cleartomark\n", bufend
- bufptr
+ 1);
802 bufptr
+= strlen(bufptr
);
806 strlcpy(bufptr
, choices
[i
]->code
, bufend
- bufptr
+ 1);
807 bufptr
+= strlen(bufptr
);
811 * Nul-terminate, free, and return...
823 * 'ppd_handle_media()' - Handle media selection...
827 ppd_handle_media(ppd_file_t
*ppd
)
829 ppd_choice_t
*manual_feed
, /* ManualFeed choice, if any */
830 *input_slot
, /* InputSlot choice, if any */
831 *page
; /* PageSize/PageRegion */
832 ppd_size_t
*size
; /* Current media size */
833 ppd_attr_t
*rpr
; /* RequiresPageRegion value */
837 * This function determines if the user has selected a media source
838 * via the InputSlot or ManualFeed options; if so, it marks the
839 * PageRegion option corresponding to the current media size.
840 * Otherwise it marks the PageSize option.
843 if ((size
= ppdPageSize(ppd
, NULL
)) == NULL
)
846 manual_feed
= ppdFindMarkedChoice(ppd
, "ManualFeed");
847 input_slot
= ppdFindMarkedChoice(ppd
, "InputSlot");
849 if (input_slot
!= NULL
)
850 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", input_slot
->choice
);
855 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", "All");
857 if (!strcasecmp(size
->name
, "Custom") || (!manual_feed
&& !input_slot
) ||
858 !((manual_feed
&& !strcasecmp(manual_feed
->choice
, "True")) ||
859 (input_slot
&& input_slot
->code
&& input_slot
->code
[0])))
862 * Manual feed was not selected and/or the input slot selection does
863 * not contain any PostScript code. Use the PageSize option...
866 ppdMarkOption(ppd
, "PageSize", size
->name
);
871 * Manual feed was selected and/or the input slot selection contains
872 * PostScript code. Use the PageRegion option...
875 ppdMarkOption(ppd
, "PageRegion", size
->name
);
878 * RequiresPageRegion does not apply to manual feed so we need to
879 * check that we are not doing manual feed before unmarking PageRegion.
882 if (!(manual_feed
&& !strcasecmp(manual_feed
->choice
, "True")) &&
883 ((rpr
&& rpr
->value
&& !strcmp(rpr
->value
, "False")) ||
884 (!rpr
&& !ppd
->num_filters
)))
887 * Either the PPD file specifies no PageRegion code or the PPD file
888 * not for a CUPS raster driver and thus defaults to no PageRegion
889 * code... Unmark the PageRegion choice so that we don't output the
893 page
= ppdFindMarkedChoice(ppd
, "PageRegion");
903 * 'ppd_sort()' - Sort options by ordering numbers...
906 static int /* O - -1 if c1 < c2, 0 if equal, 1 otherwise */
907 ppd_sort(ppd_choice_t
**c1
, /* I - First choice */
908 ppd_choice_t
**c2
) /* I - Second choice */
910 if ((*c1
)->option
->order
< (*c2
)->option
->order
)
912 else if ((*c1
)->option
->order
> (*c2
)->option
->order
)
920 * End of "$Id: emit.c 5238 2006-03-07 04:41:42Z mike $".