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