]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/ppd-emit.c
Remove all of the Subversion keywords from various source files.
[thirdparty/cups.git] / cups / ppd-emit.c
CommitLineData
ef416fc2 1/*
7e86f2f6 2 * PPD code emission routines for CUPS.
ef416fc2 3 *
f787e1e3 4 * Copyright 2007-2015 by Apple Inc.
7e86f2f6 5 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 6 *
7e86f2f6
MS
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 12 *
7e86f2f6 13 * PostScript is a trademark of Adobe Systems, Inc.
ef416fc2 14 *
7e86f2f6 15 * This file is subject to the Apple OS-Developed Software exception.
ef416fc2 16 */
17
18/*
19 * Include necessary headers...
20 */
21
71e16022 22#include "cups-private.h"
f787e1e3 23#include "ppd.h"
ef416fc2 24#if defined(WIN32) || defined(__EMX__)
25# include <io.h>
26#else
27# include <unistd.h>
28#endif /* WIN32 || __EMX__ */
29
30
31/*
32 * Local functions...
33 */
34
ee571f26 35static int ppd_compare_cparams(ppd_cparam_t *a, ppd_cparam_t *b);
ef416fc2 36static void ppd_handle_media(ppd_file_t *ppd);
ef416fc2 37
38
39/*
40 * Local globals...
41 */
42
43static const char ppd_custom_code[] =
44 "pop pop pop\n"
45 "<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n";
46
47
48/*
49 * 'ppdCollect()' - Collect all marked options that reside in the specified
50 * section.
5a738aea
MS
51 *
52 * The choices array should be freed using @code free@ when you are
53 * finished with it.
ef416fc2 54 */
55
56int /* O - Number of options marked */
57ppdCollect(ppd_file_t *ppd, /* I - PPD file data */
58 ppd_section_t section, /* I - Section to collect */
59 ppd_choice_t ***choices) /* O - Pointers to choices */
fa73b229 60{
61 return (ppdCollect2(ppd, section, 0.0, choices));
62}
63
64
65/*
66 * 'ppdCollect2()' - Collect all marked options that reside in the
67 * specified section and minimum order.
68 *
5a738aea
MS
69 * The choices array should be freed using @code free@ when you are
70 * finished with it.
71 *
f3c17241 72 * @since CUPS 1.2/OS X 10.5@
fa73b229 73 */
74
75int /* O - Number of options marked */
76ppdCollect2(ppd_file_t *ppd, /* I - PPD file data */
77 ppd_section_t section, /* I - Section to collect */
78 float min_order, /* I - Minimum OrderDependency value */
79 ppd_choice_t ***choices) /* O - Pointers to choices */
ef416fc2 80{
ef416fc2 81 ppd_choice_t *c; /* Current choice */
0a682745
MS
82 ppd_section_t csection; /* Current section */
83 float corder; /* Current OrderDependency value */
ef416fc2 84 int count; /* Number of choices collected */
85 ppd_choice_t **collect; /* Collected choices */
0a682745 86 float *orders; /* Collected order values */
ef416fc2 87
88
e07d4801 89 DEBUG_printf(("ppdCollect2(ppd=%p, section=%d, min_order=%f, choices=%p)",
8ca02f3c 90 ppd, section, min_order, choices));
91
91c84a35
MS
92 if (!ppd || !choices)
93 {
94 if (choices)
95 *choices = NULL;
96
ef416fc2 97 return (0);
91c84a35 98 }
ef416fc2 99
100 /*
0a682745 101 * Allocate memory for up to N selected choices...
ef416fc2 102 */
103
91c84a35
MS
104 count = 0;
105 if ((collect = calloc(sizeof(ppd_choice_t *),
7e86f2f6 106 (size_t)cupsArrayCount(ppd->marked))) == NULL)
91c84a35
MS
107 {
108 *choices = NULL;
109 return (0);
110 }
111
7e86f2f6 112 if ((orders = calloc(sizeof(float), (size_t)cupsArrayCount(ppd->marked))) == NULL)
91c84a35
MS
113 {
114 *choices = NULL;
115 free(collect);
116 return (0);
117 }
ef416fc2 118
119 /*
120 * Loop through all options and add choices as needed...
121 */
122
0a682745
MS
123 for (c = (ppd_choice_t *)cupsArrayFirst(ppd->marked);
124 c;
125 c = (ppd_choice_t *)cupsArrayNext(ppd->marked))
ef416fc2 126 {
0a682745
MS
127 csection = c->option->section;
128 corder = c->option->order;
129
130 if (!strcmp(c->choice, "Custom"))
131 {
132 ppd_attr_t *attr; /* NonUIOrderDependency value */
133 float aorder; /* Order value */
134 char asection[17], /* Section name */
135 amain[PPD_MAX_NAME + 1],
136 aoption[PPD_MAX_NAME];
137 /* *CustomFoo and True */
138
139
140 for (attr = ppdFindAttr(ppd, "NonUIOrderDependency", NULL);
141 attr;
142 attr = ppdFindNextAttr(ppd, "NonUIOrderDependency", NULL))
143 if (attr->value &&
144 sscanf(attr->value, "%f%16s%41s%40s", &aorder, asection, amain,
145 aoption) == 4 &&
146 !strncmp(amain, "*Custom", 7) &&
147 !strcmp(amain + 7, c->option->keyword) && !strcmp(aoption, "True"))
148 {
149 /*
150 * Use this NonUIOrderDependency...
151 */
ef416fc2 152
0a682745
MS
153 corder = aorder;
154
155 if (!strcmp(asection, "DocumentSetup"))
156 csection = PPD_ORDER_DOCUMENT;
157 else if (!strcmp(asection, "ExitServer"))
158 csection = PPD_ORDER_EXIT;
159 else if (!strcmp(asection, "JCLSetup"))
160 csection = PPD_ORDER_JCL;
161 else if (!strcmp(asection, "PageSetup"))
162 csection = PPD_ORDER_PAGE;
163 else if (!strcmp(asection, "Prolog"))
164 csection = PPD_ORDER_PROLOG;
165 else
166 csection = PPD_ORDER_ANY;
167
168 break;
169 }
170 }
171
172 if (csection == section && corder >= min_order)
173 {
174 collect[count] = c;
175 orders[count] = corder;
176 count ++;
177 }
ef416fc2 178 }
179
180 /*
181 * If we have more than 1 marked choice, sort them...
182 */
183
184 if (count > 1)
0a682745
MS
185 {
186 int i, j; /* Looping vars */
187
188 for (i = 0; i < (count - 1); i ++)
189 for (j = i + 1; j < count; j ++)
190 if (orders[i] > orders[j])
191 {
192 c = collect[i];
193 corder = orders[i];
194 collect[i] = collect[j];
195 orders[i] = orders[j];
196 collect[j] = c;
197 orders[j] = corder;
198 }
199 }
200
201 free(orders);
ef416fc2 202
e07d4801 203 DEBUG_printf(("2ppdCollect2: %d marked choices...", count));
8ca02f3c 204
ef416fc2 205 /*
206 * Return the array and number of choices; if 0, free the array since
207 * it isn't needed.
208 */
209
210 if (count > 0)
211 {
212 *choices = collect;
213 return (count);
214 }
215 else
216 {
217 *choices = NULL;
218 free(collect);
219 return (0);
220 }
221}
222
223
224/*
225 * 'ppdEmit()' - Emit code for marked options to a file.
226 */
227
228int /* O - 0 on success, -1 on failure */
229ppdEmit(ppd_file_t *ppd, /* I - PPD file record */
230 FILE *fp, /* I - File to write to */
231 ppd_section_t section) /* I - Section to write */
fa73b229 232{
233 return (ppdEmitAfterOrder(ppd, fp, section, 0, 0.0));
234}
235
236
237/*
238 * 'ppdEmitAfterOrder()' - Emit a subset of the code for marked options to a file.
239 *
240 * When "limit" is non-zero, this function only emits options whose
241 * OrderDependency value is greater than or equal to "min_order".
242 *
243 * When "limit" is zero, this function is identical to ppdEmit().
244 *
f3c17241 245 * @since CUPS 1.2/OS X 10.5@
fa73b229 246 */
247
248int /* O - 0 on success, -1 on failure */
249ppdEmitAfterOrder(
250 ppd_file_t *ppd, /* I - PPD file record */
251 FILE *fp, /* I - File to write to */
252 ppd_section_t section, /* I - Section to write */
757d2cad 253 int limit, /* I - Non-zero to use min_order */
254 float min_order) /* I - Lowest OrderDependency */
ef416fc2 255{
757d2cad 256 char *buffer; /* Option code */
257 int status; /* Return status */
ef416fc2 258
259
260 /*
757d2cad 261 * Range check input...
ef416fc2 262 */
263
757d2cad 264 if (!ppd || !fp)
265 return (-1);
ef416fc2 266
267 /*
757d2cad 268 * Get the string...
ef416fc2 269 */
270
83e08001 271 buffer = ppdEmitString(ppd, section, limit ? min_order : 0.0f);
ef416fc2 272
757d2cad 273 /*
274 * Write it as needed and return...
275 */
ef416fc2 276
757d2cad 277 if (buffer)
278 {
279 status = fputs(buffer, fp) < 0 ? -1 : 0;
ef416fc2 280
757d2cad 281 free(buffer);
282 }
283 else
284 status = 0;
ef416fc2 285
757d2cad 286 return (status);
ef416fc2 287}
288
289
290/*
291 * 'ppdEmitFd()' - Emit code for marked options to a file.
292 */
293
294int /* O - 0 on success, -1 on failure */
295ppdEmitFd(ppd_file_t *ppd, /* I - PPD file record */
296 int fd, /* I - File to write to */
297 ppd_section_t section) /* I - Section to write */
298{
757d2cad 299 char *buffer, /* Option code */
300 *bufptr; /* Pointer into code */
301 size_t buflength; /* Length of option code */
302 ssize_t bytes; /* Bytes written */
303 int status; /* Return status */
ef416fc2 304
305
306 /*
757d2cad 307 * Range check input...
ef416fc2 308 */
309
757d2cad 310 if (!ppd || fd < 0)
311 return (-1);
ef416fc2 312
313 /*
757d2cad 314 * Get the string...
ef416fc2 315 */
316
757d2cad 317 buffer = ppdEmitString(ppd, section, 0.0);
ef416fc2 318
757d2cad 319 /*
320 * Write it as needed and return...
321 */
ef416fc2 322
757d2cad 323 if (buffer)
324 {
325 buflength = strlen(buffer);
326 bufptr = buffer;
327 bytes = 0;
ef416fc2 328
757d2cad 329 while (buflength > 0)
330 {
b86bc4cf 331#ifdef WIN32
332 if ((bytes = (ssize_t)write(fd, bufptr, (unsigned)buflength)) < 0)
333#else
757d2cad 334 if ((bytes = write(fd, bufptr, buflength)) < 0)
b86bc4cf 335#endif /* WIN32 */
ef416fc2 336 {
757d2cad 337 if (errno == EAGAIN || errno == EINTR)
338 continue;
ef416fc2 339
757d2cad 340 break;
ef416fc2 341 }
757d2cad 342
7e86f2f6 343 buflength -= (size_t)bytes;
757d2cad 344 bufptr += bytes;
ef416fc2 345 }
346
757d2cad 347 status = bytes < 0 ? -1 : 0;
348
349 free(buffer);
350 }
351 else
352 status = 0;
353
354 return (status);
ef416fc2 355}
356
357
358/*
359 * 'ppdEmitJCL()' - Emit code for JCL options to a file.
360 */
361
362int /* O - 0 on success, -1 on failure */
363ppdEmitJCL(ppd_file_t *ppd, /* I - PPD file record */
364 FILE *fp, /* I - File to write to */
365 int job_id, /* I - Job ID */
366 const char *user, /* I - Username */
367 const char *title) /* I - Title */
368{
369 char *ptr; /* Pointer into JCL string */
8b116e60
MS
370 char temp[65], /* Local title string */
371 displaymsg[33]; /* Local display string */
ef416fc2 372
373
374 /*
375 * Range check the input...
376 */
377
757d2cad 378 if (!ppd || !ppd->jcl_begin || !ppd->jcl_ps)
ef416fc2 379 return (0);
380
381 /*
382 * See if the printer supports HP PJL...
383 */
384
385 if (!strncmp(ppd->jcl_begin, "\033%-12345X@", 10))
386 {
387 /*
388 * This printer uses HP PJL commands for output; filter the output
389 * so that we only have a single "@PJL JOB" command in the header...
390 *
391 * To avoid bugs in the PJL implementation of certain vendors' products
392 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
393 * of the PJL commands to initialize PJL processing.
394 */
395
f7deaa1a 396 ppd_attr_t *charset; /* PJL charset */
634763e8 397 ppd_attr_t *display; /* PJL display command */
f7deaa1a 398
399
400 if ((charset = ppdFindAttr(ppd, "cupsPJLCharset", NULL)) != NULL)
401 {
88f9aafc 402 if (!charset->value || _cups_strcasecmp(charset->value, "UTF-8"))
f7deaa1a 403 charset = NULL;
404 }
405
634763e8
MS
406 if ((display = ppdFindAttr(ppd, "cupsPJLDisplay", NULL)) != NULL)
407 {
408 if (!display->value)
409 display = NULL;
410 }
411
ef416fc2 412 fputs("\033%-12345X@PJL\n", fp);
413 for (ptr = ppd->jcl_begin + 9; *ptr;)
414 if (!strncmp(ptr, "@PJL JOB", 8))
415 {
416 /*
417 * Skip job command...
418 */
419
420 for (;*ptr; ptr ++)
421 if (*ptr == '\n')
422 break;
423
424 if (*ptr)
425 ptr ++;
426 }
427 else
428 {
429 /*
430 * Copy line...
431 */
432
433 for (;*ptr; ptr ++)
434 {
435 putc(*ptr, fp);
436 if (*ptr == '\n')
437 break;
438 }
439
440 if (*ptr)
441 ptr ++;
442 }
443
444 /*
8b116e60 445 * Clean up the job title...
ef416fc2 446 */
447
448 if ((ptr = strrchr(title, '/')) != NULL)
8b116e60
MS
449 {
450 /*
451 * Only show basename of file path...
452 */
453
ef416fc2 454 title = ptr + 1;
8b116e60
MS
455 }
456
457 if (!strncmp(title, "smbprn.", 7))
458 {
459 /*
460 * Skip leading smbprn.######## from Samba jobs...
461 */
462
463 for (title += 7; *title && isdigit(*title & 255); title ++);
7cf5915e
MS
464 while (_cups_isspace(*title))
465 title ++;
8b116e60
MS
466
467 if ((ptr = strstr(title, " - ")) != NULL)
468 {
469 /*
470 * Skip application name in "Some Application - Title of job"...
471 */
472
473 title = ptr + 3;
474 }
475 }
ef416fc2 476
477 /*
8b116e60 478 * Replace double quotes with single quotes and UTF-8 characters with
07725fee 479 * question marks so that the title does not cause a PJL syntax error.
ef416fc2 480 */
481
482 strlcpy(temp, title, sizeof(temp));
483
484 for (ptr = temp; *ptr; ptr ++)
485 if (*ptr == '\"')
486 *ptr = '\'';
8b116e60 487 else if (!charset && (*ptr & 128))
07725fee 488 *ptr = '?';
ef416fc2 489
8b116e60
MS
490 /*
491 * CUPS STR #3125: Long PJL JOB NAME causes problems with some printers
492 *
493 * Generate the display message, truncating at 32 characters + nul to avoid
494 * issues with some printer's PJL implementations...
495 */
496
497 snprintf(displaymsg, sizeof(displaymsg), "%d %s %s", job_id, user, temp);
498
ef416fc2 499 /*
500 * Send PJL JOB and PJL RDYMSG commands before we enter PostScript mode...
501 */
502
634763e8 503 if (display && strcmp(display->value, "job"))
634763e8 504 fprintf(fp, "@PJL JOB NAME = \"%s\"\n", temp);
3e7fe0ca
MS
505 else if (display && !strcmp(display->value, "rdymsg"))
506 fprintf(fp, "@PJL RDYMSG DISPLAY = \"%s\"\n", displaymsg);
634763e8 507 else
8b116e60
MS
508 fprintf(fp, "@PJL JOB NAME = \"%s\" DISPLAY = \"%s\"\n", temp,
509 displaymsg);
3e7fe0ca
MS
510
511 /*
512 * Replace double quotes with single quotes and UTF-8 characters with
513 * question marks so that the user does not cause a PJL syntax error.
514 */
515
516 strlcpy(temp, user, sizeof(temp));
517
518 for (ptr = temp; *ptr; ptr ++)
519 if (*ptr == '\"')
520 *ptr = '\'';
521 else if (!charset && (*ptr & 128))
522 *ptr = '?';
523
524 fprintf(fp, "@PJL SET USERNAME = \"%s\"\n", temp);
ef416fc2 525 }
526 else
527 fputs(ppd->jcl_begin, fp);
528
529 ppdEmit(ppd, fp, PPD_ORDER_JCL);
530 fputs(ppd->jcl_ps, fp);
531
532 return (0);
533}
534
535
536/*
537 * 'ppdEmitJCLEnd()' - Emit JCLEnd code to a file.
538 *
f3c17241 539 * @since CUPS 1.2/OS X 10.5@
ef416fc2 540 */
541
542int /* O - 0 on success, -1 on failure */
543ppdEmitJCLEnd(ppd_file_t *ppd, /* I - PPD file record */
544 FILE *fp) /* I - File to write to */
545{
ef416fc2 546 /*
547 * Range check the input...
548 */
549
757d2cad 550 if (!ppd)
ef416fc2 551 return (0);
552
757d2cad 553 if (!ppd->jcl_end)
ef416fc2 554 {
555 if (ppd->num_filters == 0)
757d2cad 556 putc(0x04, fp);
ef416fc2 557
558 return (0);
559 }
560
561 /*
562 * See if the printer supports HP PJL...
563 */
564
565 if (!strncmp(ppd->jcl_end, "\033%-12345X@", 10))
566 {
567 /*
568 * This printer uses HP PJL commands for output; filter the output
569 * so that we only have a single "@PJL JOB" command in the header...
570 *
571 * To avoid bugs in the PJL implementation of certain vendors' products
572 * (Xerox in particular), we add a dummy "@PJL" command at the beginning
573 * of the PJL commands to initialize PJL processing.
574 */
575
576 fputs("\033%-12345X@PJL\n", fp);
06d4e77b 577 fputs("@PJL RDYMSG DISPLAY = \"\"\n", fp);
ef416fc2 578 fputs(ppd->jcl_end + 9, fp);
579 }
580 else
581 fputs(ppd->jcl_end, fp);
582
583 return (0);
584}
585
586
757d2cad 587/*
588 * 'ppdEmitString()' - Get a string containing the code for marked options.
589 *
590 * When "min_order" is greater than zero, this function only includes options
591 * whose OrderDependency value is greater than or equal to "min_order".
592 * Otherwise, all options in the specified section are included in the
593 * returned string.
594 *
595 * The return string is allocated on the heap and should be freed using
5a738aea 596 * @code free@ when you are done with it.
757d2cad 597 *
f3c17241 598 * @since CUPS 1.2/OS X 10.5@
757d2cad 599 */
600
5a738aea 601char * /* O - String containing option code or @code NULL@ if there is no option code */
757d2cad 602ppdEmitString(ppd_file_t *ppd, /* I - PPD file record */
603 ppd_section_t section, /* I - Section to write */
604 float min_order) /* I - Lowest OrderDependency */
605{
606 int i, j, /* Looping vars */
607 count; /* Number of choices */
608 ppd_choice_t **choices; /* Choices */
609 ppd_size_t *size; /* Custom page size */
610 ppd_coption_t *coption; /* Custom option */
611 ppd_cparam_t *cparam; /* Custom parameter */
612 size_t bufsize; /* Size of string buffer needed */
613 char *buffer, /* String buffer */
614 *bufptr, /* Pointer into buffer */
615 *bufend; /* End of buffer */
616 struct lconv *loc; /* Locale data */
617
618
e07d4801 619 DEBUG_printf(("ppdEmitString(ppd=%p, section=%d, min_order=%f)",
8ca02f3c 620 ppd, section, min_order));
621
757d2cad 622 /*
623 * Range check input...
624 */
625
626 if (!ppd)
627 return (NULL);
628
629 /*
630 * Use PageSize or PageRegion as required...
631 */
632
633 ppd_handle_media(ppd);
634
635 /*
636 * Collect the options we need to emit...
637 */
638
639 if ((count = ppdCollect2(ppd, section, min_order, &choices)) == 0)
640 return (NULL);
641
642 /*
643 * Count the number of bytes that are required to hold all of the
644 * option code...
645 */
646
647 for (i = 0, bufsize = 1; i < count; i ++)
648 {
005dd1eb
MS
649 if (section == PPD_ORDER_JCL)
650 {
88f9aafc 651 if (!_cups_strcasecmp(choices[i]->choice, "Custom") &&
005dd1eb
MS
652 (coption = ppdFindCustomOption(ppd, choices[i]->option->keyword))
653 != NULL)
654 {
655 /*
656 * Add space to account for custom parameter substitution...
657 */
658
659 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
660 cparam;
661 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
662 {
663 switch (cparam->type)
664 {
665 case PPD_CUSTOM_CURVE :
666 case PPD_CUSTOM_INVCURVE :
667 case PPD_CUSTOM_POINTS :
668 case PPD_CUSTOM_REAL :
669 case PPD_CUSTOM_INT :
670 bufsize += 10;
671 break;
672
673 case PPD_CUSTOM_PASSCODE :
674 case PPD_CUSTOM_PASSWORD :
675 case PPD_CUSTOM_STRING :
4220952d
MS
676 if (cparam->current.custom_string)
677 bufsize += strlen(cparam->current.custom_string);
005dd1eb
MS
678 break;
679 }
680 }
681 }
682 }
683 else if (section != PPD_ORDER_EXIT)
757d2cad 684 {
685 bufsize += 3; /* [{\n */
686
88f9aafc
MS
687 if ((!_cups_strcasecmp(choices[i]->option->keyword, "PageSize") ||
688 !_cups_strcasecmp(choices[i]->option->keyword, "PageRegion")) &&
689 !_cups_strcasecmp(choices[i]->choice, "Custom"))
757d2cad 690 {
e07d4801 691 DEBUG_puts("2ppdEmitString: Custom size set!");
8ca02f3c 692
e1d6a774 693 bufsize += 37; /* %%BeginFeature: *CustomPageSize True\n */
757d2cad 694 bufsize += 50; /* Five 9-digit numbers + newline */
695 }
88f9aafc 696 else if (!_cups_strcasecmp(choices[i]->choice, "Custom") &&
757d2cad 697 (coption = ppdFindCustomOption(ppd,
698 choices[i]->option->keyword))
699 != NULL)
700 {
238c3832
MS
701 bufsize += 23 + strlen(choices[i]->option->keyword) + 6;
702 /* %%BeginFeature: *Customkeyword True\n */
757d2cad 703
88f9aafc 704
757d2cad 705 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
706 cparam;
707 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
708 {
709 switch (cparam->type)
710 {
711 case PPD_CUSTOM_CURVE :
712 case PPD_CUSTOM_INVCURVE :
713 case PPD_CUSTOM_POINTS :
714 case PPD_CUSTOM_REAL :
715 case PPD_CUSTOM_INT :
716 bufsize += 10;
717 break;
718
719 case PPD_CUSTOM_PASSCODE :
720 case PPD_CUSTOM_PASSWORD :
721 case PPD_CUSTOM_STRING :
4220952d
MS
722 bufsize += 3;
723 if (cparam->current.custom_string)
724 bufsize += 4 * strlen(cparam->current.custom_string);
757d2cad 725 break;
726 }
727 }
728 }
729 else
e1d6a774 730 bufsize += 17 + strlen(choices[i]->option->keyword) + 1 +
731 strlen(choices[i]->choice) + 1;
732 /* %%BeginFeature: *keyword choice\n */
757d2cad 733
734 bufsize += 13; /* %%EndFeature\n */
735 bufsize += 22; /* } stopped cleartomark\n */
736 }
737
738 if (choices[i]->code)
e1d6a774 739 bufsize += strlen(choices[i]->code) + 1;
757d2cad 740 else
741 bufsize += strlen(ppd_custom_code);
742 }
743
744 /*
745 * Allocate memory...
746 */
747
e07d4801 748 DEBUG_printf(("2ppdEmitString: Allocating %d bytes for string...",
ae71f5de 749 (int)bufsize));
8ca02f3c 750
757d2cad 751 if ((buffer = calloc(1, bufsize)) == NULL)
752 {
753 free(choices);
754 return (NULL);
755 }
756
757 bufend = buffer + bufsize - 1;
758 loc = localeconv();
759
760 /*
761 * Copy the option code to the buffer...
762 */
763
764 for (i = 0, bufptr = buffer; i < count; i ++, bufptr += strlen(bufptr))
005dd1eb
MS
765 if (section == PPD_ORDER_JCL)
766 {
88f9aafc 767 if (!_cups_strcasecmp(choices[i]->choice, "Custom") &&
41681883 768 choices[i]->code &&
005dd1eb
MS
769 (coption = ppdFindCustomOption(ppd, choices[i]->option->keyword))
770 != NULL)
771 {
772 /*
773 * Handle substitutions in custom JCL options...
774 */
775
776 char *cptr; /* Pointer into code */
777 int pnum; /* Parameter number */
778
779
780 for (cptr = choices[i]->code; *cptr && bufptr < bufend;)
781 {
782 if (*cptr == '\\')
783 {
784 cptr ++;
785
786 if (isdigit(*cptr & 255))
787 {
788 /*
789 * Substitute parameter...
790 */
791
792 pnum = *cptr++ - '0';
4220952d
MS
793 while (isdigit(*cptr & 255))
794 pnum = pnum * 10 + *cptr++ - '0';
005dd1eb
MS
795
796 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
797 cparam;
798 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
799 if (cparam->order == pnum)
800 break;
801
802 if (cparam)
803 {
804 switch (cparam->type)
805 {
806 case PPD_CUSTOM_CURVE :
807 case PPD_CUSTOM_INVCURVE :
808 case PPD_CUSTOM_POINTS :
809 case PPD_CUSTOM_REAL :
810 bufptr = _cupsStrFormatd(bufptr, bufend,
811 cparam->current.custom_real,
812 loc);
813 break;
814
815 case PPD_CUSTOM_INT :
07623986 816 snprintf(bufptr, (size_t)(bufend - bufptr), "%d", cparam->current.custom_int);
005dd1eb
MS
817 bufptr += strlen(bufptr);
818 break;
819
820 case PPD_CUSTOM_PASSCODE :
821 case PPD_CUSTOM_PASSWORD :
822 case PPD_CUSTOM_STRING :
4220952d
MS
823 if (cparam->current.custom_string)
824 {
07623986 825 strlcpy(bufptr, cparam->current.custom_string, (size_t)(bufend - bufptr));
4220952d
MS
826 bufptr += strlen(bufptr);
827 }
005dd1eb
MS
828 break;
829 }
830 }
831 }
832 else if (*cptr)
833 *bufptr++ = *cptr++;
834 }
835 else
836 *bufptr++ = *cptr++;
837 }
838 }
839 else
840 {
841 /*
842 * Otherwise just copy the option code directly...
843 */
844
07623986 845 strlcpy(bufptr, choices[i]->code, (size_t)(bufend - bufptr + 1));
005dd1eb
MS
846 bufptr += strlen(bufptr);
847 }
848 }
849 else if (section != PPD_ORDER_EXIT)
757d2cad 850 {
851 /*
852 * Add wrapper commands to prevent printer errors for unsupported
853 * options...
854 */
855
07623986 856 strlcpy(bufptr, "[{\n", (size_t)(bufend - bufptr + 1));
757d2cad 857 bufptr += 3;
858
859 /*
860 * Send DSC comments with option...
861 */
862
e07d4801
MS
863 DEBUG_printf(("2ppdEmitString: Adding code for %s=%s...",
864 choices[i]->option->keyword, choices[i]->choice));
8ca02f3c 865
88f9aafc
MS
866 if ((!_cups_strcasecmp(choices[i]->option->keyword, "PageSize") ||
867 !_cups_strcasecmp(choices[i]->option->keyword, "PageRegion")) &&
868 !_cups_strcasecmp(choices[i]->choice, "Custom"))
757d2cad 869 {
870 /*
871 * Variable size; write out standard size options, using the
872 * parameter positions defined in the PPD file...
873 */
874
875 ppd_attr_t *attr; /* PPD attribute */
876 int pos, /* Position of custom value */
877 orientation; /* Orientation to use */
878 float values[5]; /* Values for custom command */
879
880
07623986 881 strlcpy(bufptr, "%%BeginFeature: *CustomPageSize True\n", (size_t)(bufend - bufptr + 1));
757d2cad 882 bufptr += 37;
883
884 size = ppdPageSize(ppd, "Custom");
885
886 memset(values, 0, sizeof(values));
887
888 if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize", "Width")) != NULL)
889 {
890 pos = atoi(attr->value) - 1;
891
892 if (pos < 0 || pos > 4)
893 pos = 0;
894 }
895 else
896 pos = 0;
897
898 values[pos] = size->width;
899
900 if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize", "Height")) != NULL)
901 {
902 pos = atoi(attr->value) - 1;
903
904 if (pos < 0 || pos > 4)
905 pos = 1;
906 }
907 else
908 pos = 1;
909
910 values[pos] = size->length;
911
912 /*
913 * According to the Adobe PPD specification, an orientation of 1
914 * will produce a print that comes out upside-down with the X
915 * axis perpendicular to the direction of feed, which is exactly
916 * what we want to be consistent with non-PS printers.
917 *
918 * We could also use an orientation of 3 to produce output that
919 * comes out rightside-up (this is the default for many large format
920 * printer PPDs), however for consistency we will stick with the
921 * value 1.
922 *
923 * If we wanted to get fancy, we could use orientations of 0 or
924 * 2 and swap the width and length, however we don't want to get
925 * fancy, we just want it to work consistently.
926 *
927 * The orientation value is range limited by the Orientation
928 * parameter definition, so certain non-PS printer drivers that
929 * only support an Orientation of 0 will get the value 0 as
930 * expected.
931 */
932
933 orientation = 1;
934
935 if ((attr = ppdFindAttr(ppd, "ParamCustomPageSize",
936 "Orientation")) != NULL)
937 {
938 int min_orient, max_orient; /* Minimum and maximum orientations */
939
940
941 if (sscanf(attr->value, "%d%*s%d%d", &pos, &min_orient,
942 &max_orient) != 3)
943 pos = 4;
944 else
945 {
946 pos --;
947
948 if (pos < 0 || pos > 4)
949 pos = 4;
950
951 if (orientation > max_orient)
952 orientation = max_orient;
953 else if (orientation < min_orient)
954 orientation = min_orient;
955 }
956 }
957 else
958 pos = 4;
959
b86bc4cf 960 values[pos] = (float)orientation;
757d2cad 961
962 for (pos = 0; pos < 5; pos ++)
963 {
964 bufptr = _cupsStrFormatd(bufptr, bufend, values[pos], loc);
965 *bufptr++ = '\n';
966 }
967
968 if (!choices[i]->code)
969 {
970 /*
971 * This can happen with certain buggy PPD files that don't include
972 * a CustomPageSize command sequence... We just use a generic
973 * Level 2 command sequence...
974 */
975
07623986 976 strlcpy(bufptr, ppd_custom_code, (size_t)(bufend - bufptr + 1));
757d2cad 977 bufptr += strlen(bufptr);
978 }
979 }
88f9aafc 980 else if (!_cups_strcasecmp(choices[i]->choice, "Custom") &&
005dd1eb 981 (coption = ppdFindCustomOption(ppd, choices[i]->option->keyword))
757d2cad 982 != NULL)
983 {
984 /*
985 * Custom option...
986 */
987
988 const char *s; /* Pointer into string value */
ee571f26 989 cups_array_t *params; /* Parameters in the correct output order */
757d2cad 990
991
ee571f26
MS
992 params = cupsArrayNew((cups_array_func_t)ppd_compare_cparams, NULL);
993
994 for (cparam = (ppd_cparam_t *)cupsArrayFirst(coption->params);
995 cparam;
996 cparam = (ppd_cparam_t *)cupsArrayNext(coption->params))
997 cupsArrayAdd(params, cparam);
998
07623986 999 snprintf(bufptr, (size_t)(bufend - bufptr + 1), "%%%%BeginFeature: *Custom%s True\n", coption->keyword);
757d2cad 1000 bufptr += strlen(bufptr);
1001
ee571f26 1002 for (cparam = (ppd_cparam_t *)cupsArrayFirst(params);
757d2cad 1003 cparam;
ee571f26 1004 cparam = (ppd_cparam_t *)cupsArrayNext(params))
757d2cad 1005 {
1006 switch (cparam->type)
1007 {
1008 case PPD_CUSTOM_CURVE :
1009 case PPD_CUSTOM_INVCURVE :
1010 case PPD_CUSTOM_POINTS :
1011 case PPD_CUSTOM_REAL :
1012 bufptr = _cupsStrFormatd(bufptr, bufend,
1013 cparam->current.custom_real, loc);
1014 *bufptr++ = '\n';
1015 break;
1016
1017 case PPD_CUSTOM_INT :
07623986 1018 snprintf(bufptr, (size_t)(bufend - bufptr + 1), "%d\n", cparam->current.custom_int);
757d2cad 1019 bufptr += strlen(bufptr);
1020 break;
1021
1022 case PPD_CUSTOM_PASSCODE :
1023 case PPD_CUSTOM_PASSWORD :
1024 case PPD_CUSTOM_STRING :
1025 *bufptr++ = '(';
1026
4220952d
MS
1027 if (cparam->current.custom_string)
1028 {
1029 for (s = cparam->current.custom_string; *s; s ++)
757d2cad 1030 {
4220952d
MS
1031 if (*s < ' ' || *s == '(' || *s == ')' || *s >= 127)
1032 {
07623986 1033 snprintf(bufptr, (size_t)(bufend - bufptr + 1), "\\%03o", *s & 255);
4220952d
MS
1034 bufptr += strlen(bufptr);
1035 }
1036 else
1037 *bufptr++ = *s;
757d2cad 1038 }
4220952d 1039 }
757d2cad 1040
1041 *bufptr++ = ')';
1042 *bufptr++ = '\n';
1043 break;
1044 }
1045 }
ee571f26
MS
1046
1047 cupsArrayDelete(params);
757d2cad 1048 }
1049 else
1050 {
07623986 1051 snprintf(bufptr, (size_t)(bufend - bufptr + 1), "%%%%BeginFeature: *%s %s\n", choices[i]->option->keyword, choices[i]->choice);
757d2cad 1052 bufptr += strlen(bufptr);
1053 }
1054
1055 if (choices[i]->code && choices[i]->code[0])
1056 {
b86bc4cf 1057 j = (int)strlen(choices[i]->code);
07623986 1058 memcpy(bufptr, choices[i]->code, (size_t)j);
757d2cad 1059 bufptr += j;
1060
1061 if (choices[i]->code[j - 1] != '\n')
1062 *bufptr++ = '\n';
1063 }
1064
1065 strlcpy(bufptr, "%%EndFeature\n"
07623986 1066 "} stopped cleartomark\n", (size_t)(bufend - bufptr + 1));
757d2cad 1067 bufptr += strlen(bufptr);
8ca02f3c 1068
e07d4801 1069 DEBUG_printf(("2ppdEmitString: Offset in string is %d...",
ae71f5de 1070 (int)(bufptr - buffer)));
757d2cad 1071 }
1072 else
1073 {
07623986 1074 strlcpy(bufptr, choices[i]->code, (size_t)(bufend - bufptr + 1));
757d2cad 1075 bufptr += strlen(bufptr);
1076 }
1077
1078 /*
1079 * Nul-terminate, free, and return...
1080 */
1081
1082 *bufptr = '\0';
1083
1084 free(choices);
1085
1086 return (buffer);
1087}
1088
1089
ee571f26
MS
1090/*
1091 * 'ppd_compare_cparams()' - Compare the order of two custom parameters.
1092 */
1093
1094static int /* O - Result of comparison */
1095ppd_compare_cparams(ppd_cparam_t *a, /* I - First parameter */
1096 ppd_cparam_t *b) /* I - Second parameter */
1097{
1098 return (a->order - b->order);
1099}
1100
1101
ef416fc2 1102/*
1103 * 'ppd_handle_media()' - Handle media selection...
1104 */
1105
1106static void
ee571f26 1107ppd_handle_media(ppd_file_t *ppd) /* I - PPD file */
ef416fc2 1108{
1109 ppd_choice_t *manual_feed, /* ManualFeed choice, if any */
f11a948a 1110 *input_slot; /* InputSlot choice, if any */
ef416fc2 1111 ppd_size_t *size; /* Current media size */
1112 ppd_attr_t *rpr; /* RequiresPageRegion value */
1113
1114
1115 /*
f11a948a
MS
1116 * This function determines what page size code to use, if any, for the
1117 * current media size, InputSlot, and ManualFeed selections.
1118 *
1119 * We use the PageSize code if:
1120 *
1121 * 1. A custom media size is selected.
1122 * 2. ManualFeed and InputSlot are not selected (or do not exist).
1123 * 3. ManualFeed is selected but is False and InputSlot is not selected or
1124 * the selection has no code - the latter check done to support "auto" or
1125 * "printer default" InputSlot options.
1126 *
1127 * We use the PageRegion code if:
1128 *
1129 * 4. RequiresPageRegion does not exist and the PPD contains cupsFilter
1130 * keywords, indicating this is a CUPS-based driver.
1131 * 5. RequiresPageRegion exists for the selected InputSlot (or "All" for any
1132 * InputSlot or ManualFeed selection) and is True.
1133 *
1134 * If none of the 5 conditions are true, no page size code is used and we
1135 * unmark any existing PageSize or PageRegion choices.
ef416fc2 1136 */
1137
1138 if ((size = ppdPageSize(ppd, NULL)) == NULL)
1139 return;
1140
1141 manual_feed = ppdFindMarkedChoice(ppd, "ManualFeed");
1142 input_slot = ppdFindMarkedChoice(ppd, "InputSlot");
1143
1144 if (input_slot != NULL)
1145 rpr = ppdFindAttr(ppd, "RequiresPageRegion", input_slot->choice);
1146 else
1147 rpr = NULL;
1148
1149 if (!rpr)
1150 rpr = ppdFindAttr(ppd, "RequiresPageRegion", "All");
1151
88f9aafc 1152 if (!_cups_strcasecmp(size->name, "Custom") ||
f11a948a 1153 (!manual_feed && !input_slot) ||
88f9aafc 1154 (manual_feed && !_cups_strcasecmp(manual_feed->choice, "False") &&
178cb736
MS
1155 (!input_slot || (input_slot->code && !input_slot->code[0]))) ||
1156 (!rpr && ppd->num_filters > 0))
ef416fc2 1157 {
1158 /*
f11a948a 1159 * Use PageSize code...
ef416fc2 1160 */
1161
1162 ppdMarkOption(ppd, "PageSize", size->name);
1163 }
88f9aafc 1164 else if (rpr && rpr->value && !_cups_strcasecmp(rpr->value, "True"))
ef416fc2 1165 {
1166 /*
f11a948a 1167 * Use PageRegion code...
ef416fc2 1168 */
1169
1170 ppdMarkOption(ppd, "PageRegion", size->name);
f11a948a
MS
1171 }
1172 else
1173 {
ef416fc2 1174 /*
f11a948a 1175 * Do not use PageSize or PageRegion code...
ef416fc2 1176 */
1177
f11a948a
MS
1178 ppd_choice_t *page; /* PageSize/Region choice, if any */
1179
1180 if ((page = ppdFindMarkedChoice(ppd, "PageSize")) != NULL)
ef416fc2 1181 {
1182 /*
f11a948a 1183 * Unmark PageSize...
ef416fc2 1184 */
1185
f11a948a
MS
1186 page->marked = 0;
1187 cupsArrayRemove(ppd->marked, page);
1188 }
1189
1190 if ((page = ppdFindMarkedChoice(ppd, "PageRegion")) != NULL)
1191 {
1192 /*
1193 * Unmark PageRegion...
1194 */
ef416fc2 1195
f11a948a
MS
1196 page->marked = 0;
1197 cupsArrayRemove(ppd->marked, page);
ef416fc2 1198 }
1199 }
1200}