2 * PostScript command filter for CUPS.
4 * Copyright 2008-2014 by Apple Inc.
6 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
10 * Include necessary headers...
13 #include <cups/cups-private.h>
15 #include <cups/sidechannel.h>
22 static int auto_configure(ppd_file_t
*ppd
, const char *user
);
23 static void begin_ps(ppd_file_t
*ppd
, const char *user
);
24 static void end_ps(ppd_file_t
*ppd
);
25 static void print_self_test_page(ppd_file_t
*ppd
, const char *user
);
26 static void report_levels(ppd_file_t
*ppd
, const char *user
);
30 * 'main()' - Process a CUPS command file.
33 int /* O - Exit status */
34 main(int argc
, /* I - Number of command-line arguments */
35 char *argv
[]) /* I - Command-line arguments */
37 int status
= 0; /* Exit status */
38 cups_file_t
*fp
; /* Command file */
39 char line
[1024], /* Line from file */
40 *value
; /* Value on line */
41 int linenum
; /* Line number in file */
42 ppd_file_t
*ppd
; /* PPD file */
46 * Check for valid arguments...
49 if (argc
< 6 || argc
> 7)
52 * We don't have the correct number of arguments; write an error message
56 _cupsLangPrintf(stderr
,
57 _("Usage: %s job-id user title copies options [file]"),
63 * Open the PPD file...
66 if ((ppd
= ppdOpenFile(getenv("PPD"))) == NULL
)
68 fputs("ERROR: Unable to open PPD file!\n", stderr
);
73 * Open the command file as needed...
78 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
80 perror("ERROR: Unable to open command file - ");
88 * Read the commands from the file and send the appropriate commands...
93 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
96 * Parse the command...
99 if (!_cups_strcasecmp(line
, "AutoConfigure"))
100 status
|= auto_configure(ppd
, argv
[2]);
101 else if (!_cups_strcasecmp(line
, "PrintSelfTestPage"))
102 print_self_test_page(ppd
, argv
[2]);
103 else if (!_cups_strcasecmp(line
, "ReportLevels"))
104 report_levels(ppd
, argv
[2]);
107 _cupsLangPrintFilter(stderr
, "ERROR",
108 _("Invalid printer command \"%s\"."), line
);
118 * 'auto_configure()' - Automatically configure the printer using PostScript
119 * query commands and/or SNMP lookups.
122 static int /* O - Exit status */
123 auto_configure(ppd_file_t
*ppd
, /* I - PPD file */
124 const char *user
) /* I - Printing user */
126 int status
= 0; /* Exit status */
127 ppd_option_t
*option
; /* Current option in PPD */
128 ppd_attr_t
*attr
; /* Query command attribute */
129 const char *valptr
; /* Pointer into attribute value */
130 char buffer
[1024], /* String buffer */
131 *bufptr
; /* Pointer into buffer */
132 ssize_t bytes
; /* Number of bytes read */
133 int datalen
; /* Side-channel data length */
137 * See if the backend supports bidirectional I/O...
141 if (cupsSideChannelDoRequest(CUPS_SC_CMD_GET_BIDI
, buffer
, &datalen
,
142 30.0) != CUPS_SC_STATUS_OK
||
143 buffer
[0] != CUPS_SC_BIDI_SUPPORTED
)
145 fputs("DEBUG: Unable to auto-configure PostScript Printer - no "
146 "bidirectional I/O available!\n", stderr
);
151 * Put the printer in PostScript mode...
159 * As a lot of PPDs contain bad PostScript query code, we need to prevent one
160 * bad query sequence from affecting all auto-configuration. The following
161 * error handler allows us to log PostScript errors to cupsd.
164 puts("/cups_handleerror {\n"
165 " $error /newerror false put\n"
166 " (:PostScript error in \") print cups_query_keyword print (\": ) "
168 " $error /errorname get 128 string cvs print\n"
169 " (; offending command:) print $error /command get 128 string cvs "
170 "print (\n) print flush\n"
172 "errordict /timeout {} put\n"
173 "/cups_query_keyword (?Unknown) def\n");
177 * Wait for the printer to become connected...
185 while (cupsSideChannelDoRequest(CUPS_SC_CMD_GET_CONNECTED
, buffer
, &datalen
,
186 5.0) == CUPS_SC_STATUS_OK
&& !buffer
[0]);
189 * Then loop through every option in the PPD file and ask for the current
193 fputs("DEBUG: Auto-configuring PostScript printer...\n", stderr
);
195 for (option
= ppdFirstOption(ppd
); option
; option
= ppdNextOption(ppd
))
198 * See if we have a query command for this option...
201 snprintf(buffer
, sizeof(buffer
), "?%s", option
->keyword
);
203 if ((attr
= ppdFindAttr(ppd
, buffer
, NULL
)) == NULL
|| !attr
->value
)
205 fprintf(stderr
, "DEBUG: Skipping %s option...\n", option
->keyword
);
210 * Send the query code to the printer...
213 fprintf(stderr
, "DEBUG: Querying %s...\n", option
->keyword
);
215 for (bufptr
= buffer
, valptr
= attr
->value
; *valptr
; valptr
++)
218 * Log the query code, breaking at newlines...
224 fprintf(stderr
, "DEBUG: %s\\n\n", buffer
);
227 else if (*valptr
< ' ')
229 if (bufptr
>= (buffer
+ sizeof(buffer
) - 4))
232 fprintf(stderr
, "DEBUG: %s\n", buffer
);
241 else if (*valptr
== '\t')
249 *bufptr
++ = '0' + ((*valptr
/ 64) & 7);
250 *bufptr
++ = '0' + ((*valptr
/ 8) & 7);
251 *bufptr
++ = '0' + (*valptr
& 7);
256 if (bufptr
>= (buffer
+ sizeof(buffer
) - 1))
259 fprintf(stderr
, "DEBUG: %s\n", buffer
);
270 fprintf(stderr
, "DEBUG: %s\n", buffer
);
273 printf("/cups_query_keyword (?%s) def\n", option
->keyword
);
274 /* Set keyword for error reporting */
275 fputs("{ (", stdout
);
276 for (valptr
= attr
->value
; *valptr
; valptr
++)
278 if (*valptr
== '(' || *valptr
== ')' || *valptr
== '\\')
282 fputs(") cvx exec } stopped { cups_handleerror } if clear\n", stdout
);
283 /* Send query code */
287 cupsSideChannelDoRequest(CUPS_SC_CMD_DRAIN_OUTPUT
, buffer
, &datalen
, 5.0);
290 * Read the response data...
295 while ((bytes
= cupsBackChannelRead(bufptr
, sizeof(buffer
) - (size_t)(bufptr
- buffer
) - 1, 10.0)) > 0)
298 * No newline at the end? Go on reading ...
305 (bufptr
> buffer
&& bufptr
[-1] != '\r' && bufptr
[-1] != '\n'))
309 * Trim whitespace and control characters from both ends...
312 bytes
= bufptr
- buffer
;
314 for (bufptr
--; bufptr
>= buffer
; bufptr
--)
315 if (isspace(*bufptr
& 255) || iscntrl(*bufptr
& 255))
320 for (bufptr
= buffer
; isspace(*bufptr
& 255) || iscntrl(*bufptr
& 255);
325 _cups_strcpy(buffer
, bufptr
);
329 fprintf(stderr
, "DEBUG: Got %d bytes.\n", (int)bytes
);
332 * Skip blank lines...
339 * Check the response...
342 if ((bufptr
= strchr(buffer
, ':')) != NULL
)
345 * PostScript code for this option in the PPD is broken; show the
346 * interpreter's error message that came back...
349 fprintf(stderr
, "DEBUG%s\n", bufptr
);
354 * Verify the result is a valid option choice...
357 if (!ppdFindChoice(option
, buffer
))
359 if (!strcasecmp(buffer
, "Unknown"))
368 * Write out the result and move on to the next option...
371 fprintf(stderr
, "PPD: Default%s=%s\n", option
->keyword
, buffer
);
376 * Printer did not answer this option's query
382 "DEBUG: No answer to query for option %s within 10 seconds.\n",
400 _cupsLangPrintFilter(stderr
, "WARNING",
401 _("Unable to configure printer options."));
408 * 'begin_ps()' - Send the standard PostScript prolog.
412 begin_ps(ppd_file_t
*ppd
, /* I - PPD file */
413 const char *user
) /* I - Username */
419 fputs(ppd
->jcl_begin
, stdout
);
420 fputs(ppd
->jcl_ps
, stdout
);
424 puts("userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
431 * 'end_ps()' - Send the standard PostScript trailer.
435 end_ps(ppd_file_t
*ppd
) /* I - PPD file */
438 fputs(ppd
->jcl_end
, stdout
);
447 * 'print_self_test_page()' - Print a self-test page.
451 print_self_test_page(ppd_file_t
*ppd
, /* I - PPD file */
452 const char *user
) /* I - Printing user */
455 * Put the printer in PostScript mode...
461 * Send a simple file the draws a box around the imageable area and shows
462 * the product/interpreter information...
465 puts("\r%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
467 "\r%%%% If you can read this, you are using the wrong driver for your "
469 "\r%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
473 "initclip newpath clippath gsave stroke grestore pathbbox\n"
474 "exch pop exch pop exch 9 add exch 9 sub moveto\n"
475 "/Courier findfont 12 scalefont setfont\n"
476 "0 -12 rmoveto gsave product show grestore\n"
477 "0 -12 rmoveto gsave version show ( ) show revision 20 string cvs show "
479 "0 -12 rmoveto gsave serialnumber 20 string cvs show grestore\n"
491 * 'report_levels()' - Report supply levels.
495 report_levels(ppd_file_t
*ppd
, /* I - PPD file */
496 const char *user
) /* I - Printing user */
499 * Put the printer in PostScript mode...
505 * Don't bother sending any additional PostScript commands, since we just
506 * want the backend to have enough time to collect the supply info.