]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/emit.c
2 * "$Id: emit.c 5934 2006-09-11 14:54:40Z 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...
55 #if defined(WIN32) || defined(__EMX__)
59 #endif /* WIN32 || __EMX__ */
66 static void ppd_handle_media(ppd_file_t
*ppd
);
67 static int ppd_sort(ppd_choice_t
**c1
, ppd_choice_t
**c2
);
74 static const char ppd_custom_code
[] =
76 "<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n";
80 * 'ppdCollect()' - Collect all marked options that reside in the specified
84 int /* O - Number of options marked */
85 ppdCollect(ppd_file_t
*ppd
, /* I - PPD file data */
86 ppd_section_t section
, /* I - Section to collect */
87 ppd_choice_t
***choices
) /* O - Pointers to choices */
89 return (ppdCollect2(ppd
, section
, 0.0, choices
));
94 * 'ppdCollect2()' - Collect all marked options that reside in the
95 * specified section and minimum order.
100 int /* O - Number of options marked */
101 ppdCollect2(ppd_file_t
*ppd
, /* I - PPD file data */
102 ppd_section_t section
, /* I - Section to collect */
103 float min_order
, /* I - Minimum OrderDependency value */
104 ppd_choice_t
***choices
) /* O - Pointers to choices */
106 int i
, j
, k
, m
; /* Looping vars */
107 ppd_group_t
*g
, /* Current group */
108 *sg
; /* Current sub-group */
109 ppd_option_t
*o
; /* Current option */
110 ppd_choice_t
*c
; /* Current choice */
111 int count
; /* Number of choices collected */
112 ppd_choice_t
**collect
; /* Collected choices */
115 DEBUG_printf(("ppdCollect2(ppd=%p, section=%d, min_order=%f, choices=%p)\n",
116 ppd
, section
, min_order
, choices
));
122 * Allocate memory for up to 1000 selected choices...
126 collect
= calloc(sizeof(ppd_choice_t
*), 1000);
129 * Loop through all options and add choices as needed...
132 for (i
= ppd
->num_groups
, g
= ppd
->groups
; i
> 0; i
--, g
++)
134 for (j
= g
->num_options
, o
= g
->options
; j
> 0; j
--, o
++)
135 if (o
->section
== section
&& o
->order
>= min_order
)
136 for (k
= o
->num_choices
, c
= o
->choices
; k
> 0; k
--, c
++)
137 if (c
->marked
&& count
< 1000)
139 DEBUG_printf(("ppdCollect2: %s=%s marked...\n", o
->keyword
,
145 for (j
= g
->num_subgroups
, sg
= g
->subgroups
; j
> 0; j
--, sg
++)
146 for (k
= sg
->num_options
, o
= sg
->options
; k
> 0; k
--, o
++)
147 if (o
->section
== section
&& o
->order
>= min_order
)
148 for (m
= o
->num_choices
, c
= o
->choices
; m
> 0; m
--, c
++)
149 if (c
->marked
&& count
< 1000)
151 DEBUG_printf(("ppdCollect2: %s=%s marked...\n", o
->keyword
,
159 * If we have more than 1 marked choice, sort them...
163 qsort(collect
, count
, sizeof(ppd_choice_t
*),
164 (int (*)(const void *, const void *))ppd_sort
);
166 DEBUG_printf(("ppdCollect2: %d marked choices...\n", count
));
169 * Return the array and number of choices; if 0, free the array since
188 * 'ppdEmit()' - Emit code for marked options to a file.
191 int /* O - 0 on success, -1 on failure */
192 ppdEmit(ppd_file_t
*ppd
, /* I - PPD file record */
193 FILE *fp
, /* I - File to write to */
194 ppd_section_t section
) /* I - Section to write */
196 return (ppdEmitAfterOrder(ppd
, fp
, section
, 0, 0.0));
201 * 'ppdEmitAfterOrder()' - Emit a subset of the code for marked options to a file.
203 * When "limit" is non-zero, this function only emits options whose
204 * OrderDependency value is greater than or equal to "min_order".
206 * When "limit" is zero, this function is identical to ppdEmit().
211 int /* O - 0 on success, -1 on failure */
213 ppd_file_t
*ppd
, /* I - PPD file record */
214 FILE *fp
, /* I - File to write to */
215 ppd_section_t section
, /* I - Section to write */
216 int limit
, /* I - Non-zero to use min_order */
217 float min_order
) /* I - Lowest OrderDependency */
219 char *buffer
; /* Option code */
220 int status
; /* Return status */
224 * Range check input...
234 buffer
= ppdEmitString(ppd
, section
, min_order
);
237 * Write it as needed and return...
242 status
= fputs(buffer
, fp
) < 0 ? -1 : 0;
254 * 'ppdEmitFd()' - Emit code for marked options to a file.
257 int /* O - 0 on success, -1 on failure */
258 ppdEmitFd(ppd_file_t
*ppd
, /* I - PPD file record */
259 int fd
, /* I - File to write to */
260 ppd_section_t section
) /* I - Section to write */
262 char *buffer
, /* Option code */
263 *bufptr
; /* Pointer into code */
264 size_t buflength
; /* Length of option code */
265 ssize_t bytes
; /* Bytes written */
266 int status
; /* Return status */
270 * Range check input...
280 buffer
= ppdEmitString(ppd
, section
, 0.0);
283 * Write it as needed and return...
288 buflength
= strlen(buffer
);
292 while (buflength
> 0)
294 if ((bytes
= write(fd
, bufptr
, buflength
)) < 0)
296 if (errno
== EAGAIN
|| errno
== EINTR
)
306 status
= bytes
< 0 ? -1 : 0;
318 * 'ppdEmitJCL()' - Emit code for JCL options to a file.
321 int /* O - 0 on success, -1 on failure */
322 ppdEmitJCL(ppd_file_t
*ppd
, /* I - PPD file record */
323 FILE *fp
, /* I - File to write to */
324 int job_id
, /* I - Job ID */
325 const char *user
, /* I - Username */
326 const char *title
) /* I - Title */
328 char *ptr
; /* Pointer into JCL string */
329 char temp
[81]; /* Local title string */
333 * Range check the input...
336 if (!ppd
|| !ppd
->jcl_begin
|| !ppd
->jcl_ps
)
340 * See if the printer supports HP PJL...
343 if (!strncmp(ppd
->jcl_begin
, "\033%-12345X@", 10))
346 * This printer uses HP PJL commands for output; filter the output
347 * so that we only have a single "@PJL JOB" command in the header...
349 * To avoid bugs in the PJL implementation of certain vendors' products
350 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
351 * of the PJL commands to initialize PJL processing.
354 fputs("\033%-12345X@PJL\n", fp
);
355 for (ptr
= ppd
->jcl_begin
+ 9; *ptr
;)
356 if (!strncmp(ptr
, "@PJL JOB", 8))
359 * Skip job command...
387 * Eliminate any path info from the job title...
390 if ((ptr
= strrchr(title
, '/')) != NULL
)
394 * Replace double quotes with single quotes and 8-bit characters with
395 * question marks so that the title does not cause a PJL syntax error.
398 strlcpy(temp
, title
, sizeof(temp
));
400 for (ptr
= temp
; *ptr
; ptr
++)
407 * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
410 fprintf(fp
, "@PJL JOB NAME = \"%s\" DISPLAY = \"%d %s %s\"\n", temp
,
412 fprintf(fp
, "@PJL RDYMSG DISPLAY = \"%d %s %s\"\n", job_id
, user
, temp
);
415 fputs(ppd
->jcl_begin
, fp
);
417 ppdEmit(ppd
, fp
, PPD_ORDER_JCL
);
418 fputs(ppd
->jcl_ps
, fp
);
425 * 'ppdEmitJCLEnd()' - Emit JCLEnd code to a file.
430 int /* O - 0 on success, -1 on failure */
431 ppdEmitJCLEnd(ppd_file_t
*ppd
, /* I - PPD file record */
432 FILE *fp
) /* I - File to write to */
435 * Range check the input...
443 if (ppd
->num_filters
== 0)
450 * See if the printer supports HP PJL...
453 if (!strncmp(ppd
->jcl_end
, "\033%-12345X@", 10))
456 * This printer uses HP PJL commands for output; filter the output
457 * so that we only have a single "@PJL JOB" command in the header...
459 * To avoid bugs in the PJL implementation of certain vendors' products
460 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
461 * of the PJL commands to initialize PJL processing.
464 fputs("\033%-12345X@PJL\n", fp
);
465 fputs("@PJL RDYMSG DISPLAY = \"READY\"\n", fp
);
466 fputs(ppd
->jcl_end
+ 9, fp
);
469 fputs(ppd
->jcl_end
, fp
);
476 * 'ppdEmitString()' - Get a string containing the code for marked options.
478 * When "min_order" is greater than zero, this function only includes options
479 * whose OrderDependency value is greater than or equal to "min_order".
480 * Otherwise, all options in the specified section are included in the
483 * The return string is allocated on the heap and should be freed using
484 * free() when you are done with it.
489 char * /* O - String containing option code */
490 ppdEmitString(ppd_file_t
*ppd
, /* I - PPD file record */
491 ppd_section_t section
, /* I - Section to write */
492 float min_order
) /* I - Lowest OrderDependency */
494 int i
, j
, /* Looping vars */
495 count
; /* Number of choices */
496 ppd_choice_t
**choices
; /* Choices */
497 ppd_size_t
*size
; /* Custom page size */
498 ppd_coption_t
*coption
; /* Custom option */
499 ppd_cparam_t
*cparam
; /* Custom parameter */
500 size_t bufsize
; /* Size of string buffer needed */
501 char *buffer
, /* String buffer */
502 *bufptr
, /* Pointer into buffer */
503 *bufend
; /* End of buffer */
504 struct lconv
*loc
; /* Locale data */
507 DEBUG_printf(("ppdEmitString(ppd=%p, section=%d, min_order=%f)\n",
508 ppd
, section
, min_order
));
511 * Range check input...
518 * Use PageSize or PageRegion as required...
521 ppd_handle_media(ppd
);
524 * Collect the options we need to emit...
527 if ((count
= ppdCollect2(ppd
, section
, min_order
, &choices
)) == 0)
531 * Count the number of bytes that are required to hold all of the
535 for (i
= 0, bufsize
= 1; i
< count
; i
++)
537 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
539 bufsize
+= 3; /* [{\n */
541 if ((!strcasecmp(choices
[i
]->option
->keyword
, "PageSize") ||
542 !strcasecmp(choices
[i
]->option
->keyword
, "PageRegion")) &&
543 !strcasecmp(choices
[i
]->choice
, "Custom"))
545 DEBUG_puts("ppdEmitString: Custom size set!");
547 bufsize
+= 37; /* %%BeginFeature: *CustomPageSize True\n */
548 bufsize
+= 50; /* Five 9-digit numbers + newline */
550 else if (!strcasecmp(choices
[i
]->choice
, "Custom") &&
551 (coption
= ppdFindCustomOption(ppd
,
552 choices
[i
]->option
->keyword
))
555 bufsize
+= 17 + strlen(choices
[i
]->option
->keyword
) + 6;
556 /* %%BeginFeature: *keyword True\n */
559 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
561 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
563 switch (cparam
->type
)
565 case PPD_CUSTOM_CURVE
:
566 case PPD_CUSTOM_INVCURVE
:
567 case PPD_CUSTOM_POINTS
:
568 case PPD_CUSTOM_REAL
:
569 case PPD_CUSTOM_INT
:
573 case PPD_CUSTOM_PASSCODE
:
574 case PPD_CUSTOM_PASSWORD
:
575 case PPD_CUSTOM_STRING
:
576 bufsize
+= 3 + 4 * strlen(cparam
->current
.custom_string
);
582 bufsize
+= 17 + strlen(choices
[i
]->option
->keyword
) + 1 +
583 strlen(choices
[i
]->choice
) + 1;
584 /* %%BeginFeature: *keyword choice\n */
586 bufsize
+= 13; /* %%EndFeature\n */
587 bufsize
+= 22; /* } stopped cleartomark\n */
590 if (choices
[i
]->code
)
591 bufsize
+= strlen(choices
[i
]->code
) + 1;
593 bufsize
+= strlen(ppd_custom_code
);
600 DEBUG_printf(("ppdEmitString: Allocating %d bytes for string...\n", bufsize
));
602 if ((buffer
= calloc(1, bufsize
)) == NULL
)
608 bufend
= buffer
+ bufsize
- 1;
612 * Copy the option code to the buffer...
615 for (i
= 0, bufptr
= buffer
; i
< count
; i
++, bufptr
+= strlen(bufptr
))
616 if (section
!= PPD_ORDER_EXIT
&& section
!= PPD_ORDER_JCL
)
619 * Add wrapper commands to prevent printer errors for unsupported
623 strlcpy(bufptr
, "[{\n", bufend
- bufptr
+ 1);
627 * Send DSC comments with option...
630 DEBUG_printf(("Adding code for %s=%s...\n", choices
[i
]->option
->keyword
,
631 choices
[i
]->choice
));
633 if ((!strcasecmp(choices
[i
]->option
->keyword
, "PageSize") ||
634 !strcasecmp(choices
[i
]->option
->keyword
, "PageRegion")) &&
635 !strcasecmp(choices
[i
]->choice
, "Custom"))
638 * Variable size; write out standard size options, using the
639 * parameter positions defined in the PPD file...
642 ppd_attr_t
*attr
; /* PPD attribute */
643 int pos
, /* Position of custom value */
644 orientation
; /* Orientation to use */
645 float values
[5]; /* Values for custom command */
648 strlcpy(bufptr
, "%%BeginFeature: *CustomPageSize True\n",
649 bufend
- bufptr
+ 1);
652 size
= ppdPageSize(ppd
, "Custom");
654 memset(values
, 0, sizeof(values
));
656 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Width")) != NULL
)
658 pos
= atoi(attr
->value
) - 1;
660 if (pos
< 0 || pos
> 4)
666 values
[pos
] = size
->width
;
668 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize", "Height")) != NULL
)
670 pos
= atoi(attr
->value
) - 1;
672 if (pos
< 0 || pos
> 4)
678 values
[pos
] = size
->length
;
681 * According to the Adobe PPD specification, an orientation of 1
682 * will produce a print that comes out upside-down with the X
683 * axis perpendicular to the direction of feed, which is exactly
684 * what we want to be consistent with non-PS printers.
686 * We could also use an orientation of 3 to produce output that
687 * comes out rightside-up (this is the default for many large format
688 * printer PPDs), however for consistency we will stick with the
691 * If we wanted to get fancy, we could use orientations of 0 or
692 * 2 and swap the width and length, however we don't want to get
693 * fancy, we just want it to work consistently.
695 * The orientation value is range limited by the Orientation
696 * parameter definition, so certain non-PS printer drivers that
697 * only support an Orientation of 0 will get the value 0 as
703 if ((attr
= ppdFindAttr(ppd
, "ParamCustomPageSize",
704 "Orientation")) != NULL
)
706 int min_orient
, max_orient
; /* Minimum and maximum orientations */
709 if (sscanf(attr
->value
, "%d%*s%d%d", &pos
, &min_orient
,
716 if (pos
< 0 || pos
> 4)
719 if (orientation
> max_orient
)
720 orientation
= max_orient
;
721 else if (orientation
< min_orient
)
722 orientation
= min_orient
;
728 values
[pos
] = orientation
;
730 for (pos
= 0; pos
< 5; pos
++)
732 bufptr
= _cupsStrFormatd(bufptr
, bufend
, values
[pos
], loc
);
736 if (!choices
[i
]->code
)
739 * This can happen with certain buggy PPD files that don't include
740 * a CustomPageSize command sequence... We just use a generic
741 * Level 2 command sequence...
744 strlcpy(bufptr
, ppd_custom_code
, bufend
- bufptr
+ 1);
745 bufptr
+= strlen(bufptr
);
748 else if (!strcasecmp(choices
[i
]->choice
, "Custom") &&
749 (coption
= ppdFindCustomOption(ppd
,
750 choices
[i
]->option
->keyword
))
757 const char *s
; /* Pointer into string value */
760 snprintf(bufptr
, bufend
- bufptr
+ 1,
761 "%%%%BeginFeature: *Custom%s True\n", coption
->keyword
);
762 bufptr
+= strlen(bufptr
);
764 for (cparam
= (ppd_cparam_t
*)cupsArrayFirst(coption
->params
);
766 cparam
= (ppd_cparam_t
*)cupsArrayNext(coption
->params
))
768 switch (cparam
->type
)
770 case PPD_CUSTOM_CURVE
:
771 case PPD_CUSTOM_INVCURVE
:
772 case PPD_CUSTOM_POINTS
:
773 case PPD_CUSTOM_REAL
:
774 bufptr
= _cupsStrFormatd(bufptr
, bufend
,
775 cparam
->current
.custom_real
, loc
);
779 case PPD_CUSTOM_INT
:
780 snprintf(bufptr
, bufend
- bufptr
+ 1, "%d\n",
781 cparam
->current
.custom_int
);
782 bufptr
+= strlen(bufptr
);
785 case PPD_CUSTOM_PASSCODE
:
786 case PPD_CUSTOM_PASSWORD
:
787 case PPD_CUSTOM_STRING
:
790 for (s
= cparam
->current
.custom_string
; *s
; s
++)
791 if (*s
< ' ' || *s
== '(' || *s
== ')' || *s
>= 127)
793 snprintf(bufptr
, bufend
- bufptr
+ 1, "\\%03o", *s
& 255);
794 bufptr
+= strlen(bufptr
);
807 snprintf(bufptr
, bufend
- bufptr
+ 1, "%%%%BeginFeature: *%s %s\n",
808 choices
[i
]->option
->keyword
, choices
[i
]->choice
);
809 bufptr
+= strlen(bufptr
);
812 if (choices
[i
]->code
&& choices
[i
]->code
[0])
814 j
= strlen(choices
[i
]->code
);
815 memcpy(bufptr
, choices
[i
]->code
, j
);
818 if (choices
[i
]->code
[j
- 1] != '\n')
822 strlcpy(bufptr
, "%%EndFeature\n"
823 "} stopped cleartomark\n", bufend
- bufptr
+ 1);
824 bufptr
+= strlen(bufptr
);
826 DEBUG_printf(("ppdEmitString: Offset in string is %d...\n",
831 strlcpy(bufptr
, choices
[i
]->code
, bufend
- bufptr
+ 1);
832 bufptr
+= strlen(bufptr
);
836 * Nul-terminate, free, and return...
848 * 'ppd_handle_media()' - Handle media selection...
852 ppd_handle_media(ppd_file_t
*ppd
)
854 ppd_choice_t
*manual_feed
, /* ManualFeed choice, if any */
855 *input_slot
, /* InputSlot choice, if any */
856 *page
; /* PageSize/PageRegion */
857 ppd_size_t
*size
; /* Current media size */
858 ppd_attr_t
*rpr
; /* RequiresPageRegion value */
862 * This function determines if the user has selected a media source
863 * via the InputSlot or ManualFeed options; if so, it marks the
864 * PageRegion option corresponding to the current media size.
865 * Otherwise it marks the PageSize option.
868 if ((size
= ppdPageSize(ppd
, NULL
)) == NULL
)
871 manual_feed
= ppdFindMarkedChoice(ppd
, "ManualFeed");
872 input_slot
= ppdFindMarkedChoice(ppd
, "InputSlot");
874 if (input_slot
!= NULL
)
875 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", input_slot
->choice
);
880 rpr
= ppdFindAttr(ppd
, "RequiresPageRegion", "All");
882 if (!strcasecmp(size
->name
, "Custom") || (!manual_feed
&& !input_slot
) ||
883 !((manual_feed
&& !strcasecmp(manual_feed
->choice
, "True")) ||
884 (input_slot
&& input_slot
->code
&& input_slot
->code
[0])))
887 * Manual feed was not selected and/or the input slot selection does
888 * not contain any PostScript code. Use the PageSize option...
891 ppdMarkOption(ppd
, "PageSize", size
->name
);
896 * Manual feed was selected and/or the input slot selection contains
897 * PostScript code. Use the PageRegion option...
900 ppdMarkOption(ppd
, "PageRegion", size
->name
);
903 * RequiresPageRegion does not apply to manual feed so we need to
904 * check that we are not doing manual feed before unmarking PageRegion.
907 if (!(manual_feed
&& !strcasecmp(manual_feed
->choice
, "True")) &&
908 ((rpr
&& rpr
->value
&& !strcmp(rpr
->value
, "False")) ||
909 (!rpr
&& !ppd
->num_filters
)))
912 * Either the PPD file specifies no PageRegion code or the PPD file
913 * not for a CUPS raster driver and thus defaults to no PageRegion
914 * code... Unmark the PageRegion choice so that we don't output the
918 page
= ppdFindMarkedChoice(ppd
, "PageRegion");
928 * 'ppd_sort()' - Sort options by ordering numbers...
931 static int /* O - -1 if c1 < c2, 0 if equal, 1 otherwise */
932 ppd_sort(ppd_choice_t
**c1
, /* I - First choice */
933 ppd_choice_t
**c2
) /* I - Second choice */
935 if ((*c1
)->option
->order
< (*c2
)->option
->order
)
937 else if ((*c1
)->option
->order
> (*c2
)->option
->order
)
945 * End of "$Id: emit.c 5934 2006-09-11 14:54:40Z mike $".