]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/commandtops.c
4 * PostScript command filter for CUPS.
6 * Copyright 2008 by Apple Inc.
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
20 * Include necessary headers...
23 #include <cups/cups.h>
24 #include <cups/string.h>
25 #include <cups/sidechannel.h>
32 static void auto_configure(ppd_file_t
*ppd
, const char *user
);
33 static void print_self_test_page(ppd_file_t
*ppd
, const char *user
);
34 static void report_levels(ppd_file_t
*ppd
, const char *user
);
38 * 'main()' - Process a CUPS command file.
41 int /* O - Exit status */
42 main(int argc
, /* I - Number of command-line arguments */
43 char *argv
[]) /* I - Command-line arguments */
45 cups_file_t
*fp
; /* Command file */
46 char line
[1024], /* Line from file */
47 *value
; /* Value on line */
48 int linenum
; /* Line number in file */
49 ppd_file_t
*ppd
; /* PPD file */
53 * Check for valid arguments...
56 if (argc
< 6 || argc
> 7)
59 * We don't have the correct number of arguments; write an error message
63 fputs("ERROR: commandtops job-id user title copies options [file]\n", stderr
);
68 * Open the PPD file...
71 if ((ppd
= ppdOpenFile(getenv("PPD"))) == NULL
)
73 fputs("ERROR: Unable to open PPD file!\n", stderr
);
78 * Open the command file as needed...
83 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
85 perror("ERROR: Unable to open command file - ");
93 * Read the commands from the file and send the appropriate commands...
98 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
101 * Parse the command...
104 if (!strcasecmp(line
, "AutoConfigure"))
105 auto_configure(ppd
, argv
[2]);
106 else if (!strcasecmp(line
, "PrintSelfTestPage"))
107 print_self_test_page(ppd
, argv
[2]);
108 else if (!strcasecmp(line
, "ReportLevels"))
109 report_levels(ppd
, argv
[2]);
111 fprintf(stderr
, "ERROR: Invalid printer command \"%s\"!\n", line
);
119 * 'auto_configure()' - Automatically configure the printer using PostScript
120 * query commands and/or SNMP lookups.
124 auto_configure(ppd_file_t
*ppd
, /* I - PPD file */
125 const char *user
) /* I - Printing user */
127 ppd_option_t
*option
; /* Current option in PPD */
128 ppd_attr_t
*attr
; /* Query command attribute */
129 char buffer
[1024], /* String buffer */
130 *bufptr
; /* Pointer into buffer */
131 ssize_t bytes
; /* Number of bytes read */
132 int datalen
; /* Side-channel data length */
136 * See if the backend supports bidirectional I/O...
140 if (cupsSideChannelDoRequest(CUPS_SC_CMD_GET_BIDI
, buffer
, &datalen
,
141 30.0) != CUPS_SC_STATUS_OK
||
142 buffer
[0] != CUPS_SC_BIDI_SUPPORTED
)
144 fputs("DEBUG: Unable to auto-configure PostScript Printer - no "
145 "bidirectional I/O available!\n", stderr
);
150 * Put the printer in PostScript mode...
155 fputs(ppd
->jcl_begin
, stdout
);
156 fputs(ppd
->jcl_ps
, stdout
);
163 * Then loop through every option in the PPD file and ask for the current
167 fputs("DEBUG: Auto-configuring PostScript printer...\n", stderr
);
169 for (option
= ppdFirstOption(ppd
); option
; option
= ppdNextOption(ppd
))
172 * See if we have a query command for this option...
175 snprintf(buffer
, sizeof(buffer
), "?%s", option
->keyword
);
177 if ((attr
= ppdFindAttr(ppd
, buffer
, NULL
)) == NULL
|| !attr
->value
)
179 fprintf(stderr
, "DEBUG: Skipping %s option...\n", option
->keyword
);
184 * Send the query code to the printer...
187 fprintf(stderr
, "DEBUG: Querying %s...\n", option
->keyword
);
188 fputs(attr
->value
, stdout
);
192 cupsSideChannelDoRequest(CUPS_SC_CMD_DRAIN_OUTPUT
, buffer
, &datalen
, 5.0);
195 * Read the response data...
198 while ((bytes
= cupsBackChannelRead(buffer
, sizeof(buffer
) - 1, 5.0)) > 0)
201 * Trim whitespace from both ends...
204 buffer
[bytes
] = '\0';
206 for (bufptr
= buffer
+ bytes
- 1; bufptr
>= buffer
; bufptr
--)
207 if (isspace(*bufptr
& 255))
212 for (bufptr
= buffer
; isspace(*bufptr
& 255); bufptr
++);
215 * Skip blank lines...
222 * Write out the result and move on to the next option...
225 fprintf(stderr
, "DEBUG: Default%s=%s\n", option
->keyword
, bufptr
);
226 fprintf(stderr
, "PPD: Default%s=%s\n", option
->keyword
, bufptr
);
236 fputs(ppd
->jcl_begin
, stdout
);
245 * 'print_self_test_page()' - Print a self-test page.
249 print_self_test_page(ppd_file_t
*ppd
, /* I - PPD file */
250 const char *user
) /* I - Printing user */
253 * Put the printer in PostScript mode...
258 fputs(ppd
->jcl_begin
, stdout
);
259 fputs(ppd
->jcl_ps
, stdout
);
265 * Send a simple file the draws a box around the imageable area and shows
266 * the product/interpreter information...
269 puts("% You are using the wrong driver for your printer!\n"
272 "initclip newpath clippath gsave stroke grestore pathbbox\n"
273 "exch pop exch pop exch 9 add exch 9 sub moveto\n"
274 "/Courier findfont 12 scalefont setfont\n"
275 "0 -12 rmoveto gsave product show grestore\n"
276 "0 -12 rmoveto gsave version show ( ) show revision 20 string cvs show "
278 "0 -12 rmoveto gsave serialnumber 20 string cvs show grestore\n"
286 fputs(ppd
->jcl_begin
, stdout
);
295 * 'report_levels()' - Report supply levels.
299 report_levels(ppd_file_t
*ppd
, /* I - PPD file */
300 const char *user
) /* I - Printing user */
303 * Put the printer in PostScript mode...
308 fputs(ppd
->jcl_begin
, stdout
);
309 fputs(ppd
->jcl_ps
, stdout
);
319 fputs(ppd
->jcl_begin
, stdout
);