]>
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/".
17 * main() - Process a CUPS command file.
18 * auto_configure() - Automatically configure the printer using
19 * PostScript query commands and/or SNMP lookups.
20 * begin_ps() - Send the standard PostScript prolog.
21 * end_ps() - Send the standard PostScript trailer.
22 * print_self_test_page() - Print a self-test page.
23 * report_levels() - Report supply levels.
27 * Include necessary headers...
30 #include <cups/cups.h>
31 #include <cups/string.h>
32 #include <cups/sidechannel.h>
39 static void auto_configure(ppd_file_t
*ppd
, const char *user
);
40 static void begin_ps(ppd_file_t
*ppd
, const char *user
);
41 static void end_ps(ppd_file_t
*ppd
);
42 static void print_self_test_page(ppd_file_t
*ppd
, const char *user
);
43 static void report_levels(ppd_file_t
*ppd
, const char *user
);
47 * 'main()' - Process a CUPS command file.
50 int /* O - Exit status */
51 main(int argc
, /* I - Number of command-line arguments */
52 char *argv
[]) /* I - Command-line arguments */
54 cups_file_t
*fp
; /* Command file */
55 char line
[1024], /* Line from file */
56 *value
; /* Value on line */
57 int linenum
; /* Line number in file */
58 ppd_file_t
*ppd
; /* PPD file */
62 * Check for valid arguments...
65 if (argc
< 6 || argc
> 7)
68 * We don't have the correct number of arguments; write an error message
72 fputs("ERROR: commandtops job-id user title copies options [file]\n", stderr
);
77 * Open the PPD file...
80 if ((ppd
= ppdOpenFile(getenv("PPD"))) == NULL
)
82 fputs("ERROR: Unable to open PPD file!\n", stderr
);
87 * Open the command file as needed...
92 if ((fp
= cupsFileOpen(argv
[6], "r")) == NULL
)
94 perror("ERROR: Unable to open command file - ");
102 * Read the commands from the file and send the appropriate commands...
107 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
110 * Parse the command...
113 if (!strcasecmp(line
, "AutoConfigure"))
114 auto_configure(ppd
, argv
[2]);
115 else if (!strcasecmp(line
, "PrintSelfTestPage"))
116 print_self_test_page(ppd
, argv
[2]);
117 else if (!strcasecmp(line
, "ReportLevels"))
118 report_levels(ppd
, argv
[2]);
120 fprintf(stderr
, "ERROR: Invalid printer command \"%s\"!\n", line
);
128 * 'auto_configure()' - Automatically configure the printer using PostScript
129 * query commands and/or SNMP lookups.
133 auto_configure(ppd_file_t
*ppd
, /* I - PPD file */
134 const char *user
) /* I - Printing user */
136 ppd_option_t
*option
; /* Current option in PPD */
137 ppd_attr_t
*attr
; /* Query command attribute */
138 char buffer
[1024], /* String buffer */
139 *bufptr
; /* Pointer into buffer */
140 ssize_t bytes
; /* Number of bytes read */
141 int datalen
; /* Side-channel data length */
145 * See if the backend supports bidirectional I/O...
149 if (cupsSideChannelDoRequest(CUPS_SC_CMD_GET_BIDI
, buffer
, &datalen
,
150 30.0) != CUPS_SC_STATUS_OK
||
151 buffer
[0] != CUPS_SC_BIDI_SUPPORTED
)
153 fputs("DEBUG: Unable to auto-configure PostScript Printer - no "
154 "bidirectional I/O available!\n", stderr
);
159 * Put the printer in PostScript mode...
165 * Then loop through every option in the PPD file and ask for the current
169 fputs("DEBUG: Auto-configuring PostScript printer...\n", stderr
);
171 for (option
= ppdFirstOption(ppd
); option
; option
= ppdNextOption(ppd
))
174 * See if we have a query command for this option...
177 snprintf(buffer
, sizeof(buffer
), "?%s", option
->keyword
);
179 if ((attr
= ppdFindAttr(ppd
, buffer
, NULL
)) == NULL
|| !attr
->value
)
181 fprintf(stderr
, "DEBUG: Skipping %s option...\n", option
->keyword
);
186 * Send the query code to the printer...
189 fprintf(stderr
, "DEBUG: Querying %s...\n", option
->keyword
);
190 fputs(attr
->value
, stdout
);
194 cupsSideChannelDoRequest(CUPS_SC_CMD_DRAIN_OUTPUT
, buffer
, &datalen
, 5.0);
197 * Read the response data...
200 while ((bytes
= cupsBackChannelRead(buffer
, sizeof(buffer
) - 1, 5.0)) > 0)
203 * Trim whitespace from both ends...
206 buffer
[bytes
] = '\0';
208 for (bufptr
= buffer
+ bytes
- 1; bufptr
>= buffer
; bufptr
--)
209 if (isspace(*bufptr
& 255))
214 for (bufptr
= buffer
; isspace(*bufptr
& 255); bufptr
++);
217 * Skip blank lines...
224 * Write out the result and move on to the next option...
227 fprintf(stderr
, "DEBUG: Default%s=%s\n", option
->keyword
, bufptr
);
228 fprintf(stderr
, "PPD: Default%s=%s\n", option
->keyword
, bufptr
);
242 * 'begin_ps()' - Send the standard PostScript prolog.
246 begin_ps(ppd_file_t
*ppd
, /* I - PPD file */
247 const char *user
) /* I - Username */
253 fputs(ppd
->jcl_begin
, stdout
);
254 fputs(ppd
->jcl_ps
, stdout
);
258 puts("userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
264 * 'end_ps()' - Send the standard PostScript trailer.
268 end_ps(ppd_file_t
*ppd
) /* I - PPD file */
271 fputs(ppd
->jcl_begin
, stdout
);
280 * 'print_self_test_page()' - Print a self-test page.
284 print_self_test_page(ppd_file_t
*ppd
, /* I - PPD file */
285 const char *user
) /* I - Printing user */
288 * Put the printer in PostScript mode...
294 * Send a simple file the draws a box around the imageable area and shows
295 * the product/interpreter information...
298 puts("% You are using the wrong driver for your printer!\n"
301 "initclip newpath clippath gsave stroke grestore pathbbox\n"
302 "exch pop exch pop exch 9 add exch 9 sub moveto\n"
303 "/Courier findfont 12 scalefont setfont\n"
304 "0 -12 rmoveto gsave product show grestore\n"
305 "0 -12 rmoveto gsave version show ( ) show revision 20 string cvs show "
307 "0 -12 rmoveto gsave serialnumber 20 string cvs show grestore\n"
319 * 'report_levels()' - Report supply levels.
323 report_levels(ppd_file_t
*ppd
, /* I - PPD file */
324 const char *user
) /* I - Printing user */
327 * Put the printer in PostScript mode...
333 * Don't bother sending any additional PostScript commands, since we just
334 * want the backend to have enough time to collect the supply info.