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