-
Notifications
You must be signed in to change notification settings - Fork 125
/
foomaticrip.c
1307 lines (1129 loc) · 33 KB
/
foomaticrip.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
//
// foomaticrip.c
//
// Copyright (C) 2008 Till Kamppeter <till.kamppeter@gmail.com>
// Copyright (C) 2008 Lars Karlitski (formerly Uebernickel) <lars@karlitski.net>
//
// This file is part of foomatic-rip.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
#include "foomaticrip.h"
#include "util.h"
#include "options.h"
#include "pdf.h"
#include "postscript.h"
#include "process.h"
#include "spooler.h"
#include "renderer.h"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <errno.h>
#include <memory.h>
#include <ctype.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/wait.h>
#include <math.h>
#include <signal.h>
#include <pwd.h>
#include <cupsfilters/colormanager.h>
#include <cupsfilters/filter.h>
// Logging
FILE* logh = NULL;
void
_logv(const char *msg,
va_list ap)
{
if (!logh)
return;
vfprintf(logh, msg, ap);
fflush(logh);
}
void
_log(const char* msg,
...)
{
va_list ap;
va_start(ap, msg);
_logv(msg, ap);
va_end(ap);
}
void
close_log()
{
if (logh && logh != stderr)
fclose(logh);
}
int
redirect_log_to_stderr()
{
if (dup2(fileno(logh), fileno(stderr)) < 0)
{
_log("Could not dup logh to stderr\n");
return (0);
}
return (1);
}
void
rip_die(int status,
const char *msg,
...)
{
va_list ap;
_log("Process is dying with \"");
va_start(ap, msg);
_logv(msg, ap);
va_end(ap);
_log("\", exit stat %d\n", status);
_log("Cleaning up...\n");
kill_all_processes();
exit(status);
}
jobparams_t *job = NULL;
jobparams_t *
get_current_job()
{
return (job);
}
dstr_t *postpipe = NULL; // command into which the output of this filter
// should be piped
FILE *postpipe_fh = NULL;
FILE *
open_postpipe()
{
const char *p;
if (postpipe_fh)
return (postpipe_fh);
if (isempty(postpipe->data))
return (stdout);
// Delete possible '|' symbol in the beginning
p = skip_whitespace(postpipe->data);
if (*p && *p == '|')
p += 1;
if (start_system_process("postpipe", p, &postpipe_fh, NULL) < 0)
rip_die(EXIT_PRNERR_NORETRY_BAD_SETTINGS,
"Cannot execute postpipe %s\n", postpipe->data);
return (postpipe_fh);
}
char printer_model[256] = "";
char attrpath[256] = "";
int spooler = SPOOLER_DIRECT;
int dontparse = 0;
int jobhasjcl;
int pdfconvertedtops;
// Streaming mode: Assume PostScript input, no zero-page job check
int streaming = 0;
// cm-calibration flag
int cm_calibrate = 0;
int cm_disabled = 0;
// These variables were in 'dat' before
char colorprofile [128];
char cupsfilter[256];
char **jclprepend = NULL;
dstr_t *jclappend;
// Set debug to 1 to enable the debug logfile for this filter; it will
// appear as defined by LOG_FILE. It will contain status from this
// filter, plus the renderer's stderr output. You can also add a line
// "debug: 1" to your /etc/cups/foomatic-rip.conf or
// /etc/foomatic/filter.conf to get all your Foomatic filters into
// debug mode. WARNING: This logfile is a security hole; do not use
// in production.
int debug = 0;
// Path to the GhostScript which foomatic-rip shall use
char gspath[PATH_MAX] = "gs";
// What 'echo' program to use. It needs -e and -n. Linux's builtin
// and regular echo work fine; non-GNU platforms may need to install
// gnu echo and put gecho here or something.
char echopath[PATH_MAX] = "echo";
// CUPS raster drivers are searched here
char cupsfilterpath[PATH_MAX] = "/usr/local/lib/cups/filter:"
"/usr/local/libexec/cups/filter:"
"/opt/cups/filter:"
"/usr/lib/cups/filter";
char modern_shell[] = SHELL;
void
config_set_option(const char *key,
const char *value)
{
if (strcmp(key, "debug") == 0)
debug = atoi(value);
// What path to use for filter programs and such
//
// Your printer driver must be in the path, as must be the renderer,
// and possibly other stuff. The default path is often fine on Linux,
// but may not be on other systems.
else if (strcmp(key, "execpath") == 0 && !isempty(value))
setenv("PATH", value, 1);
else if (strcmp(key, "cupsfilterpath") == 0)
strlcpy(cupsfilterpath, value, PATH_MAX);
else if (strcmp(key, "preferred_shell") == 0)
strlcpy(modern_shell, value, 32);
else if (strcmp(key, "gspath") == 0)
strlcpy(gspath, value, PATH_MAX);
else if (strcmp(key, "echo") == 0)
strlcpy(echopath, value, PATH_MAX);
}
int
config_from_file(const char *filename)
{
FILE *fh;
char line[256];
char *key, *value;
fh = fopen(filename, "r");
if (fh == NULL)
return 0;
while (fgets(line, 256, fh) != NULL)
{
key = strtok(line, " :\t\r\n");
if (key == NULL || key[0] == '#')
continue;
value = strtok(NULL, " \t\r\n#");
config_set_option(key, value);
}
fclose(fh);
return (1);
}
const char *
get_modern_shell()
{
return (modern_shell);
}
// returns position in 'str' after the option
char *
extract_next_option(char *str,
char **pagerange,
char **key,
char **value)
{
char *p = str;
char quotechar;
*pagerange = NULL;
*key = NULL;
*value = NULL;
if (!str)
return (NULL);
// skip whitespace
while (*p && isspace(*p))
p ++;
if (!*p)
return (NULL);
// read the pagerange if we have one
if (prefixcmp(p, "even:") == 0 || prefixcmp(p, "odd:") == 0 || isdigit(*p))
{
*pagerange = p;
p = strchr(p, ':');
if (!p)
return (NULL);
*p = '\0';
p++;
}
// read the key
if (*p == '\'' || *p == '\"')
{
quotechar = *p;
*key = p +1;
p = strchr(*key, quotechar);
if (!p)
return (NULL);
}
else
{
*key = p;
while (*p && *p != ':' && *p != '=' && !isspace(*p))
p ++;
}
if (*p != ':' && *p != '=')
{ // no value for this option
if (!*p)
return NULL;
else if (isspace(*p)) {
*p = '\0';
return p +1;
}
return p;
}
*p++ = '\0'; // remove the separator char
if (*p == '\"' || *p == '\'')
{
quotechar = *p;
*value = p +1;
p = strchr(*value, quotechar);
if (!p)
return (NULL);
*p = '\0';
p++;
}
else
{
*value = p;
while (*p && !isspace(*p))
p ++;
if (*p == '\0')
return (NULL);
*p = '\0';
p++;
}
return *p ? p : NULL;
}
// processes job->optstr
void
process_cmdline_options()
{
char *p, *cmdlineopts, *nextopt, *pagerange, *key, *value;
option_t *opt, *opt2;
int optset;
char tmp [256];
_log("Printing system options:\n");
cmdlineopts = strdup(job->optstr->data);
for (nextopt = extract_next_option(cmdlineopts, &pagerange, &key, &value);
key;
nextopt = extract_next_option(nextopt, &pagerange, &key, &value))
{
// Consider only options which are not in the PPD file here
if ((opt = find_option(key)) != NULL)
continue;
if (value)
_log("Pondering option '%s=%s'\n", key, value);
else
_log("Pondering option '%s'\n", key);
// "profile" option to supply a color correction profile to a CUPS raster
// driver
if (!strcmp(key, "profile"))
{
strlcpy(colorprofile, value, 128);
continue;
}
// option to set color calibration mode
if (!strcmp(key, "cm-calibration"))
{
cm_calibrate = 1;
continue;
}
// option to set color calibration mode
if (!strcmp(key, "filter-streaming-mode") &&
(!value ||
(strcasecmp(value, "false") && strcasecmp(value, "off") &&
strcasecmp(value, "no"))))
{
streaming = 1;
continue;
}
// Solaris options that have no reason to be
if (!strcmp(key, "nobanner") || !strcmp(key, "dest") ||
!strcmp(key, "protocol"))
continue;
if (pagerange)
{
snprintf(tmp, 256, "pages:%s", pagerange);
optset = optionset(tmp);
}
else
optset = optionset("userval");
if (value)
{
if (strcasecmp(key, "media") == 0)
{
// Standard arguments?
// media=x,y,z
// sides=one|two-sided-long|short-edge
//
// Rummage around in the media= option for known media, source,
// etc types.
// We ought to do something sensible to make the common manual
// boolean option work when specified as a media= tray thing.
//
// Note that this fails miserably when the option value is in
// fact a number; they all look alike. It's unclear how many
// drivers do that. We may have to standardize the verbose
// names to make them work as selections, too.
if (value[0] == '\0')
continue;
p = strtok(value, ",");
do
{
if ((opt = find_option("PageSize")) && option_accepts_value(opt, p))
option_set_value(opt, optset, p);
else if ((opt = find_option("MediaType")) &&
option_has_choice(opt, p))
option_set_value(opt, optset, p);
else if ((opt = find_option("InputSlot")) &&
option_has_choice(opt, p))
option_set_value(opt, optset, p);
else if (!strcasecmp(p, "manualfeed"))
{
// Special case for our typical boolean manual
// feeder option if we didn't match an InputSlot above
if ((opt = find_option("ManualFeed")))
option_set_value(opt, optset, "1");
}
else
_log("Unknown \"media\" component: \"%s\".\n", p);
}
while ((p = strtok(NULL, ",")));
}
else if (!strcasecmp(key, "sides"))
{
// Handle the standard duplex option, mostly
if (!prefixcasecmp(value, "two-sided"))
{
if ((opt = find_option("Duplex")))
{
// Default to long-edge binding here, for the case that
// there is no binding setting
option_set_value(opt, optset, "DuplexNoTumble");
// Check the binding: "long edge" or "short edge"
if (strcasestr(value, "long-edge"))
{
if ((opt2 = find_option("Binding")))
option_set_value(opt2, optset, "LongEdge");
else
option_set_value(opt, optset, "DuplexNoTumble");
}
else if (strcasestr(value, "short-edge"))
{
if ((opt2 = find_option("Binding")))
option_set_value(opt2, optset, "ShortEdge");
else
option_set_value(opt, optset, "DuplexTumble");
}
}
}
else if (!prefixcasecmp(value, "one-sided"))
{
if ((opt = find_option("Duplex")))
option_set_value(opt, optset, "0");
}
// TODO
// We should handle the other half of this option - the
// BindEdge bit. Also, are there well-known ipp/cups options
// for Collate and StapleLocation? These may be here...
}
else
_log("Unknown option %s=%s.\n", key, value);
}
// Custom paper size
else if ((opt = find_option("PageSize")) &&
option_set_value(opt, optset, key))
{
// do nothing, if the value could be set, it has been set
}
else
_log("Unknown boolean option \"%s\".\n", key);
}
free(cmdlineopts);
// We 'clear' the profile if cm-calibration mode was specified
if (cm_calibrate)
{
colorprofile[0] = '\0';
cm_disabled = 1;
}
_log("Streaming Mode: %s\n", streaming ? "Activated" : "Off");
_log("CM Color Calibration Mode in CUPS: %s\n", cm_calibrate ?
"Activated" : "Off");
_log("Options from the PPD file:\n");
cmdlineopts = strdup(job->optstr->data);
for (nextopt = extract_next_option(cmdlineopts, &pagerange, &key, &value);
key;
nextopt = extract_next_option(nextopt, &pagerange, &key, &value))
{
// Consider only PPD file options here
if ((opt = find_option(key)) == NULL) continue;
if (value)
_log("Pondering option '%s=%s'\n", key, value);
else
_log("Pondering option '%s'\n", key);
if (pagerange)
{
snprintf(tmp, 256, "pages:%s", pagerange);
optset = optionset(tmp);
if (opt && (option_get_section(opt) != SECTION_ANYSETUP &&
option_get_section(opt) != SECTION_PAGESETUP))
{
_log("This option (%s) is not a \"PageSetup\" or \"AnySetup\" option, so it cannot be restricted to a page range.\n", key);
continue;
}
}
else
optset = optionset("userval");
if (value)
{
// Various non-standard printer-specific options
if (!option_set_value(opt, optset, value)) {
_log(" invalid choice \"%s\", using \"%s\" instead\n",
value, option_get_value(opt, optset));
}
}
// Standard bool args:
// landscape; what to do here?
// duplex; we should just handle this one OK now?
else if (!prefixcasecmp(key, "no"))
option_set_value(opt, optset, "0");
else
option_set_value(opt, optset, "1");
}
free(cmdlineopts);
}
//
// Functions to let foomatic-rip fork to do several tasks in parallel.
//
// To do the filtering without loading the whole file into memory we work
// on a data stream, we read the data line by line analyse it to decide what
// filters to use and start the filters if we have found out which we need.
// We buffer the data only as long as we didn't determing which filters to
// use for this piece of data and with which options. There are no temporary
// files used.
//
// foomatic-rip splits into up to 3 parallel processes to do the whole
// filtering (listed in the order of the data flow):
//
// MAIN: Prepare the job auto-detecting the spooler, reading the PPD,
// extracting the options from the command line, and parsing
// the job data itself. It analyses the job data to check
// whether it is PostScript or PDF, it also stuffs PostScript
// code from option settings into the PostScript data stream.
// It starts the renderer (KID3/KID4) as soon as it knows its
// command line and restarts it when page-specific option
// settings need another command line or different JCL commands.
// KID3: The rendering process. In most cases Ghostscript, "cat"
// for native PostScript printers with their manufacturer's
// PPD files.
// KID4: Put together the JCL commands and the renderer's output
// and send all that either to STDOUT or pipe it into the
// command line defined with $postpipe.
//
void
write_output(void *data,
size_t len)
{
const char *p = (const char *)data;
size_t left = len;
FILE *postpipe = open_postpipe();
// Remove leading whitespace
while (isspace(*p++) && left-- > 0);
fwrite_or_die((void *)p, left, 1, postpipe);
fflush(postpipe);
}
enum FileType
{
UNKNOWN_FILE,
PDF_FILE,
PS_FILE
};
int
guess_file_type(const char *begin,
size_t len,
int *startpos)
{
const char *p, *end;
p = begin;
end = begin + len;
while (p < end)
{
p = memchr(p, '%', end - p);
if (!p)
return UNKNOWN_FILE;
*startpos = p - begin;
if ((end - p) >= 2 && !memcmp(p, "%!", 2))
return PS_FILE;
else if ((end - p) > 7 && !memcmp(p, "%PDF-1.", 7))
return PDF_FILE;
++ p;
}
*startpos = 0;
return UNKNOWN_FILE;
}
//
// Prints 'filename'. If 'convert' is true, the file will be converted if it is
// not Postscript or PDF
//
int
print_file(const char *filename,
int convert)
{
FILE *file = NULL;
char buf[8192];
char tmpfilename[PATH_MAX] = "";
int type;
int startpos;
size_t n;
int ret;
if (!strcasecmp(filename, "<STDIN>"))
file = stdin;
else
{
file = fopen(filename, "r");
if (!file)
{
_log("Could not open \"%s\" for reading\n", filename);
return (0);
}
}
if (streaming == 0 || file != stdin)
{
n = fread_or_die(buf, 1, sizeof(buf) - 1, file);
if (!n) {
_log("Input is empty, outputting empty file.\n");
if (strcasecmp(filename, "<STDIN>"))
fclose(file);
return (1);
}
buf[n] = '\0';
type = guess_file_type(buf, n, &startpos);
// We do not use any JCL preceeded to the input data, as it is simply
// the PJL commands from the PPD file, and these commands we can also
// generate, end we even merge them with PJl from the driver
//if (startpos > 0)
//{
// jobhasjcl = 1;
// write_output(buf, startpos);
//}
if (file != stdin)
rewind(file);
if (convert) pdfconvertedtops = 0;
}
else
{
n = 0;
buf[0] = '\n';
type = PS_FILE;
}
switch (type)
{
case PDF_FILE:
_log("Filetype: PDF\n");
if (!ppd_supports_pdf())
{
char pdf2ps_cmd[CMDLINE_MAX];
FILE *out, *in;
int renderer_pid;
_log("Driver does not understand PDF input, "
"converting to PostScript\n");
pdfconvertedtops = 1;
// If reading from stdin, write everything into a temporary file
if (file == stdin)
{
int fd;
FILE *tmpfile;
snprintf(tmpfilename, PATH_MAX, "%s/foomatic-XXXXXX", temp_dir());
fd = mkstemp(tmpfilename);
if (fd < 0)
{
_log("Could not create temporary file: %s\n", strerror(errno));
return (EXIT_PRNERR_NORETRY_BAD_SETTINGS);
}
tmpfile = fdopen(fd, "r+");
copy_file(tmpfile, stdin, buf, n);
fclose(tmpfile);
filename = tmpfilename;
}
// If the spooler is CUPS we use the pdftops filter of CUPS,
// to have always the same PDF->PostScript conversion method
// in the whole printing environment, including incompatibility
// workarounds in the CUPS filter (so this way we also have to
// maintain all these quirks only once).
//
// The "-dNOINTERPOLATE" makes Ghostscript rendering
// significantly faster.
//
// The "-dNOMEDIAATTRS" makes Ghostscript not checking the
// page sizes against a list of known sizes and try to
// correct them.
//
// Note that Ghostscript's "pswrite" output device turns text
// into bitmaps and therefore produces huge PostScript files.
// In addition, this output device is deprecated. Therefore
// we use "ps2write".
//
// We give priority to Ghostscript here and use Poppler if
// Ghostscript is not available.
if (spooler == SPOOLER_CUPS)
snprintf(pdf2ps_cmd, CMDLINE_MAX,
"pdftops '%s' '%s' '%s' '%s' '%s' '%s'",
job->id, job->user, job->title, "1", job->optstr->data,
filename);
else
snprintf(pdf2ps_cmd, CMDLINE_MAX,
"gs -q -sstdout=%%stderr -sDEVICE=ps2write -sOutputFile=- "
"-dBATCH -dNOPAUSE -dSAFER -dNOINTERPOLATE -dNOMEDIAATTRS -dShowAcroForm %s 2>/dev/null || "
"pdftops -level2 -origpagesizes %s - 2>/dev/null",
filename, filename);
renderer_pid = start_system_process("pdf-to-ps", pdf2ps_cmd, &in, &out);
if (dup2(fileno(out), fileno(stdin)) < 0)
rip_die(EXIT_PRNERR_NORETRY_BAD_SETTINGS,
"Couldn't dup stdout of pdf-to-ps\n");
clearerr(stdin);
ret = print_file("<STDIN>", 0);
wait_for_process(renderer_pid);
if (in != NULL)
fclose(in);
if (out != NULL)
fclose(out);
// Delete temp file if we created one
if ( *tmpfilename )
unlink(tmpfilename);
return ret;
}
if (file == stdin)
return (print_pdf(stdin, buf, n, filename, startpos));
else
return (print_pdf(file, NULL, 0, filename, startpos));
case PS_FILE:
_log("Filetype: PostScript\n");
if (file == stdin)
return (print_ps(stdin, buf, n, filename));
else
return (print_ps(file, NULL, 0, filename));
case UNKNOWN_FILE:
_log("Cannot process \"%s\": Unknown filetype.\n", filename);
if (file != NULL)
fclose(file);
return (0);
}
fclose(file);
return (1);
}
void
signal_terminate(int signal)
{
rip_die(EXIT_PRINTED, "Caught termination signal: Job canceled\n");
}
jobparams_t *
create_job()
{
jobparams_t *job = calloc(1, sizeof(jobparams_t));
struct passwd *passwd;
job->optstr = create_dstr();
job->time = time(NULL);
strcpy(job->copies, "1");
gethostname(job->host, 128);
passwd = getpwuid(getuid());
if (passwd)
strlcpy(job->user, passwd->pw_name, 128);
snprintf(job->title, 2048, "%s@%s", job->user, job->host);
return (job);
}
void
free_job(jobparams_t *job)
{
free_dstr(job->optstr);
free(job);
}
int
main(int argc,
char** argv)
{
int i;
int verbose = 0, quiet = 0;
const char* str;
char *p, *filename;
const char *path;
char tmp[1024], profile_arg[256], gstoraster[512];
int havefilter, havegstoraster;
dstr_t *filelist;
list_t * arglist;
cf_filter_data_t temp;
cf_filter_data_t *data = &temp;
data->logdata = NULL;
data->logfunc = cfCUPSLogFunc;
arglist = list_create_from_array(argc -1, (void**)&argv[1]);
if (argc == 2 && (arglist_find(arglist, "--version") ||
arglist_find(arglist, "--help") ||
arglist_find(arglist, "-v") ||
arglist_find(arglist, "-h")))
{
printf("foomatic-rip of cups-filters version "VERSION"\n");
printf("\"man foomatic-rip\" for help.\n");
list_free(arglist);
return (0);
}
filelist = create_dstr();
job = create_job();
jclprepend = NULL;
jclappend = create_dstr();
postpipe = create_dstr();
options_init();
signal(SIGTERM, signal_terminate);
signal(SIGINT, signal_terminate);
signal(SIGPIPE, SIG_IGN);
// First try to find a config file in the CUPS config directory, like
// /etc/cups/foomatic-rip.conf
i = 0;
if ((str = getenv("CUPS_SERVERROOT")) != NULL)
{
snprintf(tmp, sizeof(tmp), "%s/foomatic-rip.conf", str);
i = config_from_file(tmp);
}
// If there is none, fall back to /etc/foomatic/filter.conf
if (i == 0)
i = config_from_file(CONFIG_PATH "/filter.conf");
// Command line options for verbosity
if (arglist_remove_flag(arglist, "-v"))
verbose = 1;
if (arglist_remove_flag(arglist, "-q"))
quiet = 1;
if (arglist_remove_flag(arglist, "--debug"))
debug = 1;
if (debug)
{
#if defined(__UCLIBC__) || defined(__NetBSD__)
sprintf(tmp, "%s-log-XXXXXX", LOG_FILE);
int fd = mkstemp (tmp);
#else
sprintf(tmp, "%s-XXXXXX.log", LOG_FILE);
int fd = mkstemps (tmp, 4);
#endif
if (fd != -1)
logh = fdopen(fd, "w");
else
logh = stderr;
}
else if (quiet && !verbose)
logh = NULL; // Quiet mode, do not log
else
logh = stderr; // Default: log to stderr
// Start debug logging
if (debug)
{
// If we are not in debug mode, we do this later, as we must find out at
// first which spooler is used. When printing without spooler we
// suppress logging because foomatic-rip is called directly on the
// command line and so we avoid logging onto the console.
// _log("foomatic-rip version "VERSION" running...\n");
// Print the command line only in debug mode, Mac OS X adds very many
// options so that CUPS cannot handle the output of the command line
// in its log files. If CUPS encounters a line with more than 1024
// characters sent into its log files, it aborts the job with an error.
if (spooler != SPOOLER_CUPS)
{
_log("called with arguments: ");
for (i = 1; i < argc -1; i++)
_log("\'%s\', ", argv[i]);
_log("\'%s\'\n", argv[i]);
}
}
if (getenv("PPD"))
{
strncpy(job->ppdfile, getenv("PPD"), sizeof(job->ppdfile) - 1);
if (strlen(getenv("PPD")) > 2047)
job->ppdfile[2047] = '\0';
spooler = SPOOLER_CUPS;
strncpy_omit(job->printer, getenv("PRINTER"), 256, omit_shellescapes);
if (getenv("CUPS_SERVERBIN"))
{
strncpy(cupsfilterpath, getenv("CUPS_SERVERBIN"),
sizeof(cupsfilterpath) - 1);
if (strlen(getenv("CUPS_SERVERBIN")) > PATH_MAX-1)
cupsfilterpath[PATH_MAX-1] = '\0';
}
}
// CUPS calls foomatic-rip only with 5 or 6 positional parameters,
// not with named options, like for example "-p <string>".
if (spooler != SPOOLER_CUPS)
{
if ((str = arglist_get_value(arglist, "-j")) ||
(str = arglist_get_value(arglist, "-J")))
{
strncpy_omit(job->title, str, 2048, omit_shellescapes);
if (!arglist_remove(arglist, "-j"))
arglist_remove(arglist, "-J");
}
// PPD file name given via the command line
// allow duplicates, and use the last specified one
while ((str = arglist_get_value(arglist, "-p")))
{
strncpy(job->ppdfile, str, sizeof(job->ppdfile) - 1);
if (strlen(str) > 2047)
job->ppdfile[2047] = '\0';
arglist_remove(arglist, "-p");
}
while ((str = arglist_get_value(arglist, "--ppd")))
{
strncpy(job->ppdfile, str, sizeof(job->ppdfile) - 1);
if (strlen(str) > 2047)
job->ppdfile[2047] = '\0';
arglist_remove(arglist, "--ppd");
}
// Options for spooler-less printing
while ((str = arglist_get_value(arglist, "-o")))
{
strncpy_omit(tmp, str, 1024, omit_shellescapes);
dstrcatf(job->optstr, "%s ", tmp);
// if "-o cm-calibration" was passed, we raise a flag
if (!strcmp(tmp, "cm-calibration"))
{
cm_calibrate = 1;
cm_disabled = 1;