-
Notifications
You must be signed in to change notification settings - Fork 13
/
ppd-generator.c
2834 lines (2544 loc) · 92.6 KB
/
ppd-generator.c
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
//
// PWG Raster/Apple Raster/PCLm/PDF/IPP legacy PPD generator for libppd.
//
// Copyright 2016-2019 by Till Kamppeter.
// Copyright 2017-2019 by Sahil Arora.
// Copyright 2018-2019 by Deepak Patankar.
//
// The PPD generator is based on the PPD generator for the CUPS
// "lpadmin -m everywhere" functionality in the cups/ppd-cache.c
// file. The copyright of this file is:
//
// Copyright 2010-2016 by Apple Inc.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
//
// Include necessary headers.
//
#include <config.h>
#include <cups/cups.h>
#include <cups/dir.h>
#include <cups/pwg.h>
#include <ppd/ppd.h>
#include <ppd/string-private.h>
#include <ppd/libcups2-private.h>
#include <cupsfilters/ipp.h>
#include <cupsfilters/catalog.h>
#include <limits.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
//
// Macros to work around typos in older libcups version
//
#if (CUPS_VERSION_MAJOR < 2) || ((CUPS_VERSION_MAJOR == 2) && ((CUPS_VERSION_MINOR < 3) || ((CUPS_VERSION_MINOR == 3) && (CUPS_VERSION_PATCH < 1))))
#define IPP_FINISHINGS_CUPS_FOLD_ACCORDION IPP_FINISHINGS_CUPS_FOLD_ACCORDIAN
#define IPP_FINISHINGS_FOLD_ACCORDION IPP_FINISHINGS_FOLD_ACCORDIAN
#endif
//
// Types...
//
typedef struct _ppd_size_s // **** Media Size (cups_size_t of libcups2) ****
{
char media[128]; // Media name to use
int width, // Width in hundredths of millimeters
length, // Length in hundredths of
// millimeters
bottom, // Bottom margin in hundredths of
// millimeters
left, // Left margin in hundredths of
// millimeters
right, // Right margin in hundredths of
// millimeters
top; // Top margin in hundredths of
// millimeters
} _ppd_size_t;
//
// Local functions...
//
static int http_connect(http_t **http, const char *url, char *resource,
size_t ressize);
//
// The code below is borrowed from the CUPS 2.2.x upstream repository
// (via patches attached to https://www.cups.org/str.php?L4258). This
// allows for automatic PPD generation already with CUPS versions older
// than CUPS 2.2.x. We have also an additional test and development
// platform for this code. Taken from cups/ppd-cache.c,
// cups/string-private.h, cups/string.c.
//
// The advantage of PPD generation instead of working with System V
// interface scripts is that the print dialogs of the clients do not
// need to ask the printer for its options via IPP. So we have access
// to the options with the current PPD-based dialogs and can even share
// the automatically created print queues to other CUPS-based machines
// without problems.
//
char ppdgenerator_msg[1024];
//
// Human-readable strings in the PPDs generated by the PPD generator for
// using driverless IPP printers with CUPS
//
// To allow for translated/localized PPDs we use the standard set
// of human-readable strings from the PWG:
//
// https://ftp.pwg.org/pub/pwg/ipp/examples/ipp.pot
// https://ftp.pwg.org/pub/pwg/ipp/examples/ipp.strings
//
// Creating those with translated human-readable strings would allow us to
// easily produce PPDs in other languges.
//
// Translations are supposed to go here (but none are available yet):
//
// https://github.com/istopwg/ippregistry/tree/master/localizations
//
// These standard strings are also part of CUPS' translation files:
//
// https://github.com/OpenPrinting/cups/tree/master/locale
//
// Here translations take actually place as part of the translations
// of CUPS itself.
//
// We take the files from the CUPS installed into the system (as the PPD
// generator actually does not make sense without CUPS) but currently
// only use the English version. It is not complicated to check the user
// language in the printer's IPP response via the
// attributes-natural-language attribute and then request an appropriate
// language version of the files if available. The printer-specific
// strings are downloaded from the printer following the URI in the
// printer-strings-uri attribute and are in the selected language.
//
// It is not clear whether PPD translation will get fixed in the PPD
// generator as the need of PPDs in CUPS will go away with version
// 3.x.
//
// See also:
//
// https://lists.linuxfoundation.org/pipermail/printing-architecture/2021/003992.html
// https://lists.linuxfoundation.org/pipermail/printing-architecture/2021/003995.html
//
//
// 'ppdCreatePPDFromIPP()' - Create a PPD file describing the capabilities
// of an IPP printer, using info from DNS-SD record
// as fallback (for poor IPP responses, especially
// IPP 1.x legacy)
//
char * // O - PPD filename or NULL
// on error
ppdCreatePPDFromIPP(char *buffer, // I - Filename buffer
size_t bufsize, // I - Size of filename
// buffer
ipp_t *supported, // I - Get-Printer-Attributes
// response
const char *make_model, // I - Make and model from
// DNS-SD
const char *pdl, // I - List of PDLs from
// DNS-SD
int color, // I - Color printer? (from
// DNS-SD)
int duplex, // I - Duplex printer? (from
// DNS-SD)
char *status_msg, // I - Status message buffer,
// NULL to ignore
// message
size_t status_msg_size) // I - Size of status message
// buffer
{
return ppdCreatePPDFromIPP2(buffer, bufsize, supported, make_model, pdl,
color, duplex, NULL, NULL, NULL, NULL,
status_msg, status_msg_size);
}
//
// 'ppdCreatePPDFromIPP2()' - Create a PPD file describing the
// capabilities of an IPP printer, with
// extra parameters for PPDs from a merged
// IPP record for printer clusters
//
char * // O - PPD filename or NULL
// on error
ppdCreatePPDFromIPP2(char *buffer, // I - Filename buffer
size_t bufsize, // I - Size of filename
// buffer
ipp_t *supported, // I - Get-Printer-
// Attributes response
const char *make_model, // I - Make and model from
// DNS-SD
const char *pdl, // I - List of PDLs from
// DNS-SD
int color, // I - Color printer? (from
// DNS-SD)
int duplex, // I - Duplex printer? (from
// DNS-SD)
cups_array_t *conflicts, // I - Array of
// constraints
cups_array_t *sizes, // I - Media sizes we've
// added
char* default_pagesize, // I - Default page size
const char *default_cluster_color, // I - cluster def
// color (if cluster's
// attributes are
// returned)
char *status_msg, // I - Status message
// buffer, NULL to
// ignore message
size_t status_msg_size) // I - Size of status
// message buffer
{
cups_file_t *fp; // PPD file
cups_array_t *printer_sizes; // Media sizes we've added
_ppd_size_t *size; // Current media size
ipp_attribute_t *attr, // xxx-supported
*attr2,
*lang_supp, // printer-strings-languages-supported
*defattr, // xxx-default
*quality, // print-quality-supported
*x_dim, *y_dim; // Media dimensions
ipp_t *media_col, // Media collection
*media_size; // Media size collection
char make[256], // Make and model
*model, // Model name
ppdname[PPD_MAX_NAME];
// PPD keyword
int i, j, // Looping vars
count = 0, // Number of values
bottom, // Largest bottom margin
left, // Largest left margin
right, // Largest right margin
top, // Largest top margin
max_length = 0, // Maximum custom size
max_width = 0,
min_length = INT_MAX,
// Minimum custom size
min_width = INT_MAX,
is_apple = 0, // Does the printer support Apple
// Raster?
is_pwg = 0, // Does the printer support PWG
// Raster?
is_pclm = 0, // Does the printer support PCLm?
is_pdf = 0; // Does the printer support PDF?
pwg_media_t *pwg; // PWG media size
int xres, yres; // Resolution values
cups_array_t *common_res, // Common resolutions of all PDLs
*current_res, // Resolutions of current PDL
*pdl_list; // List of PDLs
cf_res_t *common_def, // Common default resolution
*current_def, // Default resolution of current PDL
*min_res, // Minimum common resolution
*max_res; // Maximum common resolution
struct lconv *loc = localeconv();
// Locale data
cups_array_t *opt_strings_catalog = NULL;
// Standard option UI strings
cups_array_t *printer_opt_strings_catalog = NULL;
// Printer-specific option UI strings
char *human_readable,
*human_readable2;
const char *keyword; // Keyword value
cups_array_t *fin_options = NULL;
// Finishing options
char buf[256];
char *defaultoutbin = NULL;
const char *outbin;
char outbin_properties[1024];
cups_len_t octet_str_len;
void *outbin_properties_octet;
int outputorderinfofound = 0,
faceupdown = 1,
firsttolast = 1;
int manual_copies = -1,
is_fax = 0;
//
// Range check input...
//
if (buffer)
*buffer = '\0';
if (!buffer || bufsize < 1)
{
if (status_msg && status_msg_size)
snprintf(status_msg, status_msg_size, "%s", strerror(EINVAL));
return (NULL);
}
if (!supported)
{
if (status_msg && status_msg_size)
snprintf(status_msg, status_msg_size, "No IPP attributes.");
return (NULL);
}
//
// Open a temporary file for the PPD...
//
if ((fp = cupsCreateTempFile(NULL, NULL, buffer, (int)bufsize)) == NULL)
{
if (status_msg && status_msg_size)
snprintf(status_msg, status_msg_size, "%s", strerror(errno));
return (NULL);
}
//
// Standard stuff for PPD file...
//
cupsFilePuts(fp, "*PPD-Adobe: \"4.3\"\n");
cupsFilePuts(fp, "*FormatVersion: \"4.3\"\n");
cupsFilePrintf(fp, "*FileVersion: \"%s\"\n", VERSION);
cupsFilePuts(fp, "*LanguageVersion: English\n");
cupsFilePuts(fp, "*LanguageEncoding: ISOLatin1\n");
cupsFilePuts(fp, "*PSVersion: \"(3010.000) 0\"\n");
cupsFilePuts(fp, "*LanguageLevel: \"3\"\n");
cupsFilePuts(fp, "*FileSystem: False\n");
cupsFilePuts(fp, "*PCFileName: \"drvless.ppd\"\n");
if ((attr = ippFindAttribute(supported, "ipp-features-supported",
IPP_TAG_KEYWORD)) != NULL &&
ippContainsString(attr, "faxout"))
{
attr = ippFindAttribute(supported, "printer-uri-supported",
IPP_TAG_URI);
if (attr)
{
ippAttributeString(attr, buf, sizeof(buf));
if (strcasestr(buf, "faxout"))
is_fax = 1;
}
}
if ((attr = ippFindAttribute(supported, "printer-make-and-model",
IPP_TAG_TEXT)) != NULL)
strlcpy(make, ippGetString(attr, 0, NULL), sizeof(make));
else if (make_model && make_model[0] != '\0')
strlcpy(make, make_model, sizeof(make));
else
strlcpy(make, "Unknown Printer", sizeof(make));
if (!strncasecmp(make, "Hewlett Packard ", 16) ||
!strncasecmp(make, "Hewlett-Packard ", 16))
{
model = make + 16;
strlcpy(make, "HP", sizeof(make));
}
else if ((model = strchr(make, ' ')) != NULL)
*model++ = '\0';
else
model = make;
cupsFilePrintf(fp, "*Manufacturer: \"%s\"\n", make);
cupsFilePrintf(fp, "*ModelName: \"%s %s\"\n", make, model);
cupsFilePrintf(fp, "*Product: \"(%s %s)\"\n", make, model);
cupsFilePrintf(fp, "*NickName: \"%s %s, %sdriverless, %s\"\n",
make, model, (is_fax ? "Fax, " : ""), VERSION);
cupsFilePrintf(fp, "*ShortNickName: \"%s %s\"\n", make, model);
// Which is the default output bin?
if ((attr = ippFindAttribute(supported, "output-bin-default", IPP_TAG_ZERO))
!= NULL)
defaultoutbin = strdup(ippGetString(attr, 0, NULL));
// Find out on which position of the list of output bins the default one is,
// if there is no default bin, take the first of this list
i = 0;
if ((attr = ippFindAttribute(supported, "output-bin-supported",
IPP_TAG_ZERO)) != NULL)
{
count = ippGetCount(attr);
for (i = 0; i < count; i ++)
{
outbin = ippGetString(attr, i, NULL);
if (outbin == NULL)
continue;
if (defaultoutbin == NULL)
{
defaultoutbin = strdup(outbin);
break;
} else if (strcasecmp(outbin, defaultoutbin) == 0)
break;
}
}
if ((attr = ippFindAttribute(supported, "printer-output-tray",
IPP_TAG_STRING)) != NULL &&
i < ippGetCount(attr))
{
outbin_properties_octet = ippGetOctetString(attr, i, &octet_str_len);
memset(outbin_properties, 0, sizeof(outbin_properties));
memcpy(outbin_properties, outbin_properties_octet,
((size_t)octet_str_len < sizeof(outbin_properties) - 1 ?
(size_t)octet_str_len : sizeof(outbin_properties) - 1));
if (strcasestr(outbin_properties, "pagedelivery=faceUp"))
{
outputorderinfofound = 1;
faceupdown = -1;
}
if (strcasestr(outbin_properties, "stackingorder=lastToFirst"))
firsttolast = -1;
}
if (outputorderinfofound == 0 && defaultoutbin &&
strcasestr(defaultoutbin, "face-up"))
faceupdown = -1;
if (defaultoutbin)
free (defaultoutbin);
if (firsttolast * faceupdown < 0)
cupsFilePuts(fp, "*DefaultOutputOrder: Reverse\n");
else
cupsFilePuts(fp, "*DefaultOutputOrder: Normal\n");
// Do we have a color printer?
if (((attr = ippFindAttribute(supported,
"color-supported", IPP_TAG_BOOLEAN)) != NULL &&
ippGetBoolean(attr, 0)) ||
color)
cupsFilePuts(fp, "*ColorDevice: True\n");
else
cupsFilePuts(fp, "*ColorDevice: False\n");
if ((attr = ippFindAttribute(supported,
"landscape-orientation-requested-preferred",
IPP_TAG_ZERO)) != NULL)
{
if (ippGetInteger(attr, 0) == 4)
cupsFilePuts(fp, "*LandscapeOrientation: Plus90\n");
else if (ippGetInteger(attr, 0) == 5)
cupsFilePuts(fp, "*LandscapeOrientation: Minus90\n");
}
cupsFilePrintf(fp, "*cupsVersion: %d.%d\n", CUPS_VERSION_MAJOR,
CUPS_VERSION_MINOR);
cupsFilePuts(fp, "*cupsSNMPSupplies: False\n");
cupsFilePuts(fp, "*cupsLanguages: \"en");
if ((lang_supp = ippFindAttribute(supported,
"printer-strings-languages-supported",
IPP_TAG_LANGUAGE)) != NULL)
{
for (i = 0, count = ippGetCount(lang_supp); i < count; i ++)
{
keyword = ippGetString(lang_supp, i, NULL);
if (strcmp(keyword, "en"))
cupsFilePrintf(fp, " %s", keyword);
}
}
cupsFilePuts(fp, "\"\n");
if ((attr = ippFindAttribute(supported, "printer-more-info", IPP_TAG_URI)) !=
NULL)
cupsFilePrintf(fp, "*APSupplies: \"%s\"\n", ippGetString(attr, 0, NULL));
if ((attr = ippFindAttribute(supported, "printer-charge-info-uri",
IPP_TAG_URI)) != NULL)
cupsFilePrintf(fp, "*cupsChargeInfoURI: \"%s\"\n", ippGetString(attr, 0,
NULL));
// Message catalogs for UI strings
opt_strings_catalog = cfCatalogOptionArrayNew();
cfCatalogLoad(NULL, NULL, opt_strings_catalog);
if ((attr = ippFindAttribute(supported, "printer-strings-uri",
IPP_TAG_URI)) != NULL)
{
printer_opt_strings_catalog = cfCatalogOptionArrayNew();
cfCatalogLoad(ippGetString(attr, 0, NULL), NULL,
printer_opt_strings_catalog);
if (cupsArrayGetCount(printer_opt_strings_catalog) > 0)
{
http_t *http = NULL; // Connection to printer
const char *printer_uri =
ippGetString(ippFindAttribute(supported, "printer-uri-supported",
IPP_TAG_URI), 0, NULL);
// Printer URI
char resource[256]; // Resource path
ipp_t *request, // Get-Printer-Attributes request
*response; // Response to request
//
// Load strings and save the URL for clients using the destination API
// instead of this PPD file...
//
cupsFilePrintf(fp, "*cupsStringsURI: \"%s\"\n", ippGetString(attr, 0,
NULL));
if (lang_supp && printer_uri && http_connect(&http, printer_uri,
resource, sizeof(resource)))
{
//
// Loop through all of the languages and save their URIs...
//
for (i = 0, count = ippGetCount(lang_supp); i < count; i ++)
{
keyword = ippGetString(lang_supp, i, NULL);
request = ippNew();
ippSetOperation(request, IPP_OP_GET_PRINTER_ATTRIBUTES);
ippSetRequestId(request, i + 1);
ippAddString(request, IPP_TAG_OPERATION,
IPP_CONST_TAG(IPP_TAG_CHARSET),
"attributes-charset", NULL, "utf-8");
ippAddString(request, IPP_TAG_OPERATION,
IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, keyword);
ippAddString(request, IPP_TAG_OPERATION,
IPP_TAG_URI, "printer-uri", NULL, printer_uri);
ippAddString(request, IPP_TAG_OPERATION,
IPP_CONST_TAG(IPP_TAG_KEYWORD),
"requested-attributes", NULL, "printer-strings-uri");
response = cupsDoRequest(http, request, resource);
if ((attr = ippFindAttribute(response, "printer-strings-uri",
IPP_TAG_URI)) != NULL)
cupsFilePrintf(fp, "*cupsStringsURI %s: \"%s\"\n", keyword,
ippGetString(attr, 0, NULL));
ippDelete(response);
}
}
if (http)
httpClose(http);
}
}
//
// Accounting...
//
if (ippGetBoolean(ippFindAttribute(supported, "job-account-id-supported",
IPP_TAG_BOOLEAN), 0))
cupsFilePuts(fp, "*cupsJobAccountId: True\n");
if (ippGetBoolean(ippFindAttribute(supported,
"job-accounting-user-id-supported",
IPP_TAG_BOOLEAN), 0))
cupsFilePuts(fp, "*cupsJobAccountingUserId: True\n");
if ((attr = ippFindAttribute(supported, "printer-privacy-policy-uri",
IPP_TAG_URI)) != NULL)
cupsFilePrintf(fp, "*cupsPrivacyURI: \"%s\"\n",
ippGetString(attr, 0, NULL));
if ((attr = ippFindAttribute(supported, "printer-mandatory-job-attributes",
IPP_TAG_KEYWORD)) != NULL)
{
char prefix = '\"'; // Prefix for string
cupsFilePuts(fp, "*cupsMandatory: \"");
for (i = 0, count = ippGetCount(attr); i < count; i ++)
{
keyword = ippGetString(attr, i, NULL);
if (strcmp(keyword, "attributes-charset") &&
strcmp(keyword, "attributes-natural-language") &&
strcmp(keyword, "printer-uri"))
{
cupsFilePrintf(fp, "%c%s", prefix, keyword);
prefix = ',';
}
}
cupsFilePuts(fp, "\"\n");
}
if ((attr = ippFindAttribute(supported, "printer-requested-job-attributes",
IPP_TAG_KEYWORD)) != NULL)
{
char prefix = '\"'; // Prefix for string
cupsFilePuts(fp, "*cupsRequested: \"");
for (i = 0, count = ippGetCount(attr); i < count; i ++)
{
keyword = ippGetString(attr, i, NULL);
if (strcmp(keyword, "attributes-charset") &&
strcmp(keyword, "attributes-natural-language") &&
strcmp(keyword, "printer-uri"))
{
cupsFilePrintf(fp, "%c%s", prefix, keyword);
prefix = ',';
}
}
cupsFilePuts(fp, "\"\n");
}
//
// Password/PIN printing...
//
if ((attr = ippFindAttribute(supported, "job-password-supported",
IPP_TAG_INTEGER)) != NULL)
{
char pattern[33]; // Password pattern
int maxlen = ippGetInteger(attr, 0);
// Maximum length
const char *repertoire =
ippGetString(ippFindAttribute(supported,
"job-password-repertoire-configured",
IPP_TAG_KEYWORD), 0, NULL);
// Type of password
if (maxlen > (int)(sizeof(pattern) - 1))
maxlen = (int)sizeof(pattern) - 1;
if (!repertoire || !strcmp(repertoire, "iana_us-ascii_digits"))
memset(pattern, '1', (size_t)maxlen);
else if (!strcmp(repertoire, "iana_us-ascii_letters"))
memset(pattern, 'A', (size_t)maxlen);
else if (!strcmp(repertoire, "iana_us-ascii_complex"))
memset(pattern, 'C', (size_t)maxlen);
else if (!strcmp(repertoire, "iana_us-ascii_any"))
memset(pattern, '.', (size_t)maxlen);
else if (!strcmp(repertoire, "iana_utf-8_digits"))
memset(pattern, 'N', (size_t)maxlen);
else if (!strcmp(repertoire, "iana_utf-8_letters"))
memset(pattern, 'U', (size_t)maxlen);
else
memset(pattern, '*', (size_t)maxlen);
pattern[maxlen] = '\0';
cupsFilePrintf(fp, "*cupsJobPassword: \"%s\"\n", pattern);
}
//
// Add cupsSingleFile to support multiple files printing on printers
// which don't support multiple files in its firmware...
//
// Adding the keyword degrades printing performance (there is 1-2 seconds
// pause between files).
//
cupsFilePuts(fp, "*cupsSingleFile: True\n");
//
// PDLs and common resolutions ...
//
common_res = NULL;
current_res = NULL;
common_def = NULL;
current_def = NULL;
min_res = NULL;
max_res = NULL;
// Put all available PDls into a simple case-insensitevely searchable
// sorted string list
if ((pdl_list = cupsArrayNew((cups_array_cb_t)strcasecmp, NULL, NULL, 0,
(cups_acopy_cb_t)strdup,
(cups_afree_cb_t)free)) == NULL)
goto bad_ppd;
int formatfound = 0;
if (((attr = ippFindAttribute(supported, "document-format-supported",
IPP_TAG_MIMETYPE)) != NULL) ||
(pdl && pdl[0] != '\0'))
{
const char *format = pdl;
i = 0;
count = ippGetCount(attr);
while ((attr && i < count) || // Go through formats in attribute
(!attr && pdl && pdl[0] != '\0' && format[0] != '\0'))
{
// Go through formats in pdl string (from DNS-SD record)
// Pick next format from attribute
if (attr) format = ippGetString(attr, i, NULL);
// Add format to list of supported PDLs, skip duplicates
if (!cupsArrayFind(pdl_list, (void *)format))
cupsArrayAdd(pdl_list, (void *)format);
if (attr)
// Next format in attribute
i ++;
else {
// Find the next format in the string pdl, if there is none left,
// go to the terminating zero
while (!isspace(*format) && *format != ',' && *format != '\0')
format ++;
while ((isspace(*format) || *format == ',') && *format != '\0')
format ++;
}
}
}
//
// Fax
//
if (is_fax)
{
cupsFilePuts(fp, "*cupsFax: True\n");
cupsFilePuts(fp, "*cupsIPPFaxOut: True\n");
}
//
// Check for each CUPS/cups-filters-supported PDL, starting with the
// most desirable going to the least desirable. If a PDL requires a
// certain set of resolutions (the raster-based PDLs), find the
// resolutions and find out which are the common resolutions of all
// supported PDLs. Choose the default resolution from the most
// desirable of all resolution-requiring PDLs if it is common in all
// of them. Skip a resolution-requiring PDL if its resolution list
// attribute is missing or contains only broken entries. Use the
// general resolution list and default resolution of the printer
// only if it does not support any resolution-requiring PDL. Use 300
// dpi if there is no resolution info at all in the attributes.
// In case of PDF as PDL check whether also the
// "application/vnd.cups-pdf" MIME type is accepted. In this case
// our printer is a remote CUPS queue which already runs the
// pdftopdf filter on the server, so let the PPD take
// "application/pdf" as input format so that pdftopdf does not also
// get executed on the client, applying option settings twice. See
// https://github.com/apple/cups/issues/5361
//
if (cupsArrayFind(pdl_list, "application/vnd.cups-pdf"))
{
// Remote CUPS queue
cupsFilePuts(fp, "*cupsFilter2: \"application/pdf application/pdf 0 -\"\n");
manual_copies = 0;
formatfound = 1;
is_pdf = 1;
}
else if (cupsArrayFind(pdl_list, "application/pdf"))
{
// PDF printer
cupsFilePuts(fp, "*cupsFilter2: \"application/vnd.cups-pdf application/pdf 0 -\"\n");
manual_copies = 0;
formatfound = 1;
is_pdf = 1;
}
#ifdef CUPS_RASTER_HAVE_APPLERASTER
else if (cupsArrayFind(pdl_list, "image/urf") &&
(ippFindAttribute(supported, "urf-supported", IPP_TAG_KEYWORD) != NULL))
{
int resStore = 0; // Variable for storing the no. of resolutions in the resolution array
int resArray[__INT16_MAX__]; // Creating a resolution array supporting a maximum of 32767 resolutions.
int lowdpi = 0, middpi = 0, hidpi = 0; // Lower , middle and higher resolution
if ((attr = ippFindAttribute(supported, "urf-supported",
IPP_TAG_KEYWORD)) != NULL)
{
for (int i = 0, count = ippGetCount(attr); i < count; i ++)
{
const char *rs = ippGetString(attr, i, NULL); // RS values
const char *rsCopy = ippGetString(attr, i, NULL); // RS values(copy)
if (strncasecmp(rs, "RS", 2)) // Comparing attributes to have RS in
// the beginning to indicate the
// resolution feature
continue;
int resCount = 0; // Using a count variable which can be reset
while (*rsCopy != '\0') // Parsing through the copy pointer to
// determine the no. of resolutions
{
if (*rsCopy == '-')
{
resCount ++;
}
rsCopy ++;
}
resCount ++;
resStore = resCount;
resCount = 0;
resArray[resCount] = atoi(rs + 2);
resCount ++;
while (*rs != '\0') // Parsing through the entire pointer and
// appending each resolution to an array
{
if (*rs == '-')
{
resArray[resCount] = atoi(rs + 1);
resCount ++;
}
rs ++;
}
// Finding and storing the important dpi.
// Lowdpi the lowest resolution, hidpi the highest resolution and
// middpi finding the middle resolution
// The middpi takes the rounded down middle value
lowdpi = resArray[0];
middpi = resArray[(resStore - 1) / 2];
hidpi = resArray[resStore - 1];
break;
}
if (lowdpi == 0)
{
// Invalid "urf-supported" value...
goto bad_ppd;
}
else
{
if ((current_res = cfNewResolutionArray()) != NULL)
{
// Adding to the resolution list
if ((current_def = cfNewResolution(lowdpi, lowdpi)) != NULL)
{
cupsArrayAdd(current_res, current_def);
cfFreeResolution(current_def, NULL);
}
if (hidpi != lowdpi &&
(current_def = cfNewResolution(hidpi, hidpi)) != NULL)
{
cupsArrayAdd(current_res, current_def);
cfFreeResolution(current_def, NULL);
}
if (middpi != hidpi && middpi != lowdpi &&
(current_def = cfNewResolution(middpi, middpi)) != NULL)
{
cupsArrayAdd(current_res, current_def);
cfFreeResolution(current_def, NULL);
}
current_def = NULL;
// Checking if there is printer-default-resolution and this
// resolution is in the list, use it. If not, use the
// middpi, rounding down if the number of available
// resolutions is even.
if ((attr = ippFindAttribute(supported,
"printer-resolution-supported",
IPP_TAG_RESOLUTION)) != NULL)
{
if ((defattr = ippFindAttribute(supported,
"printer-resolution-default",
IPP_TAG_RESOLUTION)) != NULL)
{
current_def = cfIPPResToResolution(defattr, 0);
for (int j = 0; j < resStore; j ++)
{
if (current_def == cfNewResolution(resArray[i], resArray[i]))
{
current_def = cfIPPResToResolution(defattr, 0);
break;
}
else
{
current_def = cfNewResolution(middpi, middpi);
}
}
}
}
if (cupsArrayGetCount(current_res) > 0 &&
cfJoinResolutionArrays(&common_res, ¤t_res, &common_def,
¤t_def))
{
cupsFilePuts(fp, "*cupsFilter2: \"image/urf image/urf 0 -\"\n");
manual_copies = 1;
formatfound = 1;
is_apple = 1;
}
}
}
}
}
#endif
else if (cupsArrayFind(pdl_list, "image/pwg-raster") &&
ippFindAttribute(supported, "pwg-raster-document-type-supported", IPP_TAG_KEYWORD) != NULL &&
(attr = ippFindAttribute(supported, "pwg-raster-document-resolution-supported", IPP_TAG_RESOLUTION)) != NULL)
{
current_def = NULL;
if ((current_res = cfIPPAttrToResolutionArray(attr)) != NULL &&
cfJoinResolutionArrays(&common_res, ¤t_res, &common_def,
¤t_def))
{
cupsFilePuts(fp, "*cupsFilter2: \"image/pwg-raster image/pwg-raster 0 -\"\n");
if (formatfound == 0) manual_copies = 1;
formatfound = 1;
is_pwg = 1;
}
}
else if (cupsArrayFind(pdl_list, "application/PCLm") &&
(attr = ippFindAttribute(supported, "pclm-source-resolution-supported", IPP_TAG_RESOLUTION)) != NULL)
{
if ((defattr = ippFindAttribute(supported,
"pclm-source-resolution-default",
IPP_TAG_RESOLUTION)) != NULL)
current_def = cfIPPResToResolution(defattr, 0);
else
current_def = NULL;
if ((current_res = cfIPPAttrToResolutionArray(attr)) != NULL &&
cfJoinResolutionArrays(&common_res, ¤t_res, &common_def,
¤t_def))
{
cupsFilePuts(fp, "*cupsFilter2: \"application/PCLm application/PCLm 0 -\"\n");
if (formatfound == 0) manual_copies = 1;
formatfound = 1;
is_pclm = 1;
}
}
// Legacy formats only if we have no driverless format
else if (cupsArrayFind(pdl_list, "application/vnd.hp-pclxl"))
{
cupsFilePrintf(fp, "*cupsFilter2: \"application/vnd.cups-pdf application/vnd.hp-pclxl 100 gstopxl\"\n");
if (formatfound == 0)
manual_copies = 1;
formatfound = 1;
}
else if (cupsArrayFind(pdl_list, "application/postscript"))
{
// Higher cost value as PostScript interpreters are often buggy
cupsFilePuts(fp, "*cupsFilter2: \"application/vnd.cups-postscript application/postscript 0 -\"\n");
if (formatfound == 0)
manual_copies = 0;
formatfound = 1;
}
else if (cupsArrayFind(pdl_list, "application/vnd.hp-pcl"))
{
cupsFilePrintf(fp, "*cupsFilter2: \"application/vnd.cups-raster application/vnd.hp-pcl 100 rastertopclx\"\n");
if (formatfound == 0)
manual_copies = 1;
formatfound = 1;
}
if (cupsArrayFind(pdl_list, "image/jpeg"))
cupsFilePuts(fp, "*cupsFilter2: \"image/jpeg image/jpeg 0 -\"\n");
if (cupsArrayFind(pdl_list, "image/png"))
cupsFilePuts(fp, "*cupsFilter2: \"image/png image/png 0 -\"\n");
cupsArrayDelete(pdl_list);
if (manual_copies < 0)
manual_copies = 1;
if (formatfound == 0)
goto bad_ppd;
// For the case that we will print in a raster format and not in a high-level
// format, we need to create multiple copies on the client. We add a line to
// the PPD which tells the pdftopdf filter to generate the copies
if (manual_copies == 1)
cupsFilePuts(fp, "*cupsManualCopies: True\n");
// No resolution requirements by any of the supported PDLs?
// Use "printer-resolution-supported" attribute
if (common_res == NULL)
{
if ((attr = ippFindAttribute(supported, "printer-resolution-supported",
IPP_TAG_RESOLUTION)) != NULL)
{
if ((defattr = ippFindAttribute(supported, "printer-resolution-default",
IPP_TAG_RESOLUTION)) != NULL)
current_def = cfIPPResToResolution(defattr, 0);
else
current_def = NULL;
if ((current_res = cfIPPAttrToResolutionArray(attr)) != NULL)
cfJoinResolutionArrays(&common_res, ¤t_res, &common_def,
¤t_def);
}
}
// Still no resolution found? Default to 300 dpi
if (common_res == NULL)
{
if ((common_res = cfNewResolutionArray()) != NULL)
{
if ((current_def = cfNewResolution(300, 300)) != NULL)
{
cupsArrayAdd(common_res, current_def);
cfFreeResolution(current_def, NULL);
}
current_def = NULL;
} else
goto bad_ppd;
}
// No default resolution determined yet
if (common_def == NULL)
{
if ((defattr = ippFindAttribute(supported, "printer-resolution-default",
IPP_TAG_RESOLUTION)) != NULL)
{
common_def = cfIPPResToResolution(defattr, 0);
if (!cupsArrayFind(common_res, common_def))
{
free(common_def);
common_def = NULL;
}
}
if (common_def == NULL) {
count = cupsArrayGetCount(common_res);
common_def =
cfCopyResolution(cupsArrayGetElement(common_res, count / 2), NULL);
}
}
// Get minimum and maximum resolution
min_res = cfCopyResolution(cupsArrayGetFirst(common_res), NULL);
max_res = cfCopyResolution(cupsArrayGetLast(common_res), NULL);
cupsArrayDelete(common_res);
//
// Generically check for Raster-format-related attributes in IPP
// supported and ppdize them one by one
//
attr = ippGetFirstAttribute(supported); // first attribute
while (attr) // loop through all the attributes
{
if ((is_apple && strncasecmp(ippGetName(attr), "urf-", 4) == 0) ||
(is_pwg && strncasecmp(ippGetName(attr), "pwg-raster-", 11) == 0) ||
(is_pclm && strncasecmp(ippGetName(attr), "pclm-", 5) == 0))
{
ppdPwgPpdizeName(ippGetName(attr), ppdname, sizeof(ppdname));
cupsFilePrintf(fp, "*cups%s: ", ppdname);
ipp_tag_t tag = ippGetValueTag(attr);
count = ippGetCount(attr);
if (tag == IPP_TAG_RESOLUTION) // ppdize values of type resolution
{