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