2 * PostScript command filter for CUPS.
4 * Copyright 2008-2014 by Apple Inc.
6 * These coded instructions, statements, and computer programs are the
7 * property of Apple Inc. and are protected by Federal copyright
8 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
9 * which should have been included with this file. If this file is
10 * file is missing or damaged, see the license at "http://www.cups.org/".
14 * Include necessary headers...
17 #include <cups/cups-private.h>
19 #include <cups/sidechannel.h>
26 static int auto_configure(ppd_file_t
*ppd
, const char *user
);
27 static void begin_ps(ppd_file_t
*ppd
, const char *user
);
28 static void end_ps(ppd_file_t
*ppd
);
29 static void print_self_test_page(ppd_file_t
*ppd
, const char *user
);
30 static void report_levels(ppd_file_t
*ppd
, const char *user
);
34 * 'main()' - Process a CUPS command file.
37 int /* O - Exit status */
38 main(int argc
, /* I - Number of command-line arguments */
39 char *argv
[]) /* I - Command-line arguments */
41 int status
= 0; /* Exit status */
42 cups_file_t
*fp
; /* Command file */
43 char line
[1024], /* Line from file */
44 *value
; /* Value on line */
45 int linenum
; /* Line number in file */
46 ppd_file_t
*ppd
; /* PPD file */
50 * Check for valid arguments...
53 if (argc
< 6 || argc
> 7)
56 * We don't have the correct number of arguments; write an error message
60 _cupsLangPrintf(stderr
,
61 _("Usage: %s job-id user title copies options [file]"),
67 * Open the PPD file...
70 if ((ppd
= ppdOpenFile(getenv("PPD"))) == NULL
)
72 fputs("ERROR: Unable to open PPD file!\n", stderr
);
77 * Open the command file as needed...
82 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
84 perror("ERROR: Unable to open command file - ");
92 * Read the commands from the file and send the appropriate commands...
97 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
100 * Parse the command...
103 if (!_cups_strcasecmp(line
, "AutoConfigure"))
104 status
|= auto_configure(ppd
, argv
[2]);
105 else if (!_cups_strcasecmp(line
, "PrintSelfTestPage"))
106 print_self_test_page(ppd
, argv
[2]);
107 else if (!_cups_strcasecmp(line
, "ReportLevels"))
108 report_levels(ppd
, argv
[2]);
111 _cupsLangPrintFilter(stderr
, "ERROR",
112 _("Invalid printer command \"%s\"."), line
);
122 * 'auto_configure()' - Automatically configure the printer using PostScript
123 * query commands and/or SNMP lookups.
126 static int /* O - Exit status */
127 auto_configure(ppd_file_t
*ppd
, /* I - PPD file */
128 const char *user
) /* I - Printing user */
130 int status
= 0; /* Exit status */
131 ppd_option_t
*option
; /* Current option in PPD */
132 ppd_attr_t
*attr
; /* Query command attribute */
133 const char *valptr
; /* Pointer into attribute value */
134 char buffer
[1024], /* String buffer */
135 *bufptr
; /* Pointer into buffer */
136 ssize_t bytes
; /* Number of bytes read */
137 int datalen
; /* Side-channel data length */
141 * See if the backend supports bidirectional I/O...
145 if (cupsSideChannelDoRequest(CUPS_SC_CMD_GET_BIDI
, buffer
, &datalen
,
146 30.0) != CUPS_SC_STATUS_OK
||
147 buffer
[0] != CUPS_SC_BIDI_SUPPORTED
)
149 fputs("DEBUG: Unable to auto-configure PostScript Printer - no "
150 "bidirectional I/O available!\n", stderr
);
155 * Put the printer in PostScript mode...
163 * As a lot of PPDs contain bad PostScript query code, we need to prevent one
164 * bad query sequence from affecting all auto-configuration. The following
165 * error handler allows us to log PostScript errors to cupsd.
168 puts("/cups_handleerror {\n"
169 " $error /newerror false put\n"
170 " (:PostScript error in \") print cups_query_keyword print (\": ) "
172 " $error /errorname get 128 string cvs print\n"
173 " (; offending command:) print $error /command get 128 string cvs "
174 "print (\n) print flush\n"
176 "errordict /timeout {} put\n"
177 "/cups_query_keyword (?Unknown) def\n");
181 * Wait for the printer to become connected...
189 while (cupsSideChannelDoRequest(CUPS_SC_CMD_GET_CONNECTED
, buffer
, &datalen
,
190 5.0) == CUPS_SC_STATUS_OK
&& !buffer
[0]);
193 * Then loop through every option in the PPD file and ask for the current
197 fputs("DEBUG: Auto-configuring PostScript printer...\n", stderr
);
199 for (option
= ppdFirstOption(ppd
); option
; option
= ppdNextOption(ppd
))
202 * See if we have a query command for this option...
205 snprintf(buffer
, sizeof(buffer
), "?%s", option
->keyword
);
207 if ((attr
= ppdFindAttr(ppd
, buffer
, NULL
)) == NULL
|| !attr
->value
)
209 fprintf(stderr
, "DEBUG: Skipping %s option...\n", option
->keyword
);
214 * Send the query code to the printer...
217 fprintf(stderr
, "DEBUG: Querying %s...\n", option
->keyword
);
219 for (bufptr
= buffer
, valptr
= attr
->value
; *valptr
; valptr
++)
222 * Log the query code, breaking at newlines...
228 fprintf(stderr
, "DEBUG: %s\\n\n", buffer
);
231 else if (*valptr
< ' ')
233 if (bufptr
>= (buffer
+ sizeof(buffer
) - 4))
236 fprintf(stderr
, "DEBUG: %s\n", buffer
);
245 else if (*valptr
== '\t')
253 *bufptr
++ = '0' + ((*valptr
/ 64) & 7);
254 *bufptr
++ = '0' + ((*valptr
/ 8) & 7);
255 *bufptr
++ = '0' + (*valptr
& 7);
260 if (bufptr
>= (buffer
+ sizeof(buffer
) - 1))
263 fprintf(stderr
, "DEBUG: %s\n", buffer
);
274 fprintf(stderr
, "DEBUG: %s\n", buffer
);
277 printf("/cups_query_keyword (?%s) def\n", option
->keyword
);
278 /* Set keyword for error reporting */
279 fputs("{ (", stdout
);
280 for (valptr
= attr
->value
; *valptr
; valptr
++)
282 if (*valptr
== '(' || *valptr
== ')' || *valptr
== '\\')
286 fputs(") cvx exec } stopped { cups_handleerror } if clear\n", stdout
);
287 /* Send query code */
291 cupsSideChannelDoRequest(CUPS_SC_CMD_DRAIN_OUTPUT
, buffer
, &datalen
, 5.0);
294 * Read the response data...
299 while ((bytes
= cupsBackChannelRead(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
) - 1, 10.0)) > 0)
302 * No newline at the end? Go on reading ...
309 (bufptr
> buffer
&& bufptr
[-1] != '\r' && bufptr
[-1] != '\n'))
313 * Trim whitespace and control characters from both ends...
316 bytes
= bufptr
- buffer
;
318 for (bufptr
--; bufptr
>= buffer
; bufptr
--)
319 if (isspace(*bufptr
& 255) || iscntrl(*bufptr
& 255))
324 for (bufptr
= buffer
; isspace(*bufptr
& 255) || iscntrl(*bufptr
& 255);
329 _cups_strcpy(buffer
, bufptr
);
333 fprintf(stderr
, "DEBUG: Got %d bytes.\n", (int)bytes
);
336 * Skip blank lines...
343 * Check the response...
346 if ((bufptr
= strchr(buffer
, ':')) != NULL
)
349 * PostScript code for this option in the PPD is broken; show the
350 * interpreter's error message that came back...
353 fprintf(stderr
, "DEBUG%s\n", bufptr
);
358 * Verify the result is a valid option choice...
361 if (!ppdFindChoice(option
, buffer
))
363 if (!strcasecmp(buffer
, "Unknown"))
372 * Write out the result and move on to the next option...
375 fprintf(stderr
, "PPD: Default%s=%s\n", option
->keyword
, buffer
);
380 * Printer did not answer this option's query
386 "DEBUG: No answer to query for option %s within 10 seconds.\n",
404 _cupsLangPrintFilter(stderr
, "WARNING",
405 _("Unable to configure printer options."));
412 * 'begin_ps()' - Send the standard PostScript prolog.
416 begin_ps(ppd_file_t
*ppd
, /* I - PPD file */
417 const char *user
) /* I - Username */
423 fputs(ppd
->jcl_begin
, stdout
);
424 fputs(ppd
->jcl_ps
, stdout
);
428 puts("userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
435 * 'end_ps()' - Send the standard PostScript trailer.
439 end_ps(ppd_file_t
*ppd
) /* I - PPD file */
442 fputs(ppd
->jcl_end
, stdout
);
451 * 'print_self_test_page()' - Print a self-test page.
455 print_self_test_page(ppd_file_t
*ppd
, /* I - PPD file */
456 const char *user
) /* I - Printing user */
459 * Put the printer in PostScript mode...
465 * Send a simple file the draws a box around the imageable area and shows
466 * the product/interpreter information...
469 puts("\r%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
471 "\r%%%% If you can read this, you are using the wrong driver for your "
473 "\r%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
477 "initclip newpath clippath gsave stroke grestore pathbbox\n"
478 "exch pop exch pop exch 9 add exch 9 sub moveto\n"
479 "/Courier findfont 12 scalefont setfont\n"
480 "0 -12 rmoveto gsave product show grestore\n"
481 "0 -12 rmoveto gsave version show ( ) show revision 20 string cvs show "
483 "0 -12 rmoveto gsave serialnumber 20 string cvs show grestore\n"
495 * 'report_levels()' - Report supply levels.
499 report_levels(ppd_file_t
*ppd
, /* I - PPD file */
500 const char *user
) /* I - Printing user */
503 * Put the printer in PostScript mode...
509 * Don't bother sending any additional PostScript commands, since we just
510 * want the backend to have enough time to collect the supply info.