2 * "$Id: printers.c 4921 2006-01-12 21:26:26Z mike $"
4 * Printer status CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * main() - Main entry for CGI.
30 * Include necessary headers...
33 #include "cgi-private.h"
37 * 'main()' - Main entry for CGI.
40 int /* O - Exit status */
41 main(int argc
, /* I - Number of command-line arguments */
42 char *argv
[]) /* I - Command-line arguments */
44 cups_lang_t
*language
; /* Language information */
45 char *printer
; /* Printer name */
46 http_t
*http
; /* Connection to the server */
47 ipp_t
*request
, /* IPP request */
48 *response
; /* IPP response */
49 ipp_attribute_t
*attr
; /* IPP attribute */
50 ipp_status_t status
; /* Operation status... */
51 char uri
[HTTP_MAX_URI
]; /* Printer URI */
52 const char *which_jobs
; /* Which jobs to show */
53 const char *op
; /* Operation to perform, if any */
54 char refresh
[1024]; /* Refresh URL */
55 static const char *def_attrs
[] = /* Attributes for default printer */
58 "printer-uri-supported"
63 * Get any form variables...
67 op
= cgiGetVariable("OP");
70 * Get the request language...
73 language
= cupsLangDefault();
76 * Set the web interface section...
79 cgiSetVariable("SECTION", "printers");
82 * Connect to the HTTP server...
85 http
= httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
88 * Tell the client to expect UTF-8 encoded HTML...
91 puts("Content-Type: text/html;charset=utf-8\n");
94 * See if we need to show a list of printers or the status of a
98 cgiSetServerVersion();
101 if (strcmp(printer
, "/") == 0 || strstr(printer
, "printers.cgi") != NULL
)
104 cgiSetVariable("TITLE", _cupsLangString(language
, _("Printer")));
107 cgiSetVariable("TITLE", printer
);
109 if (op
== NULL
|| strcasecmp(op
, "print-test-page") != 0)
112 * Get the default destination...
116 request
->request
.op
.operation_id
= CUPS_GET_DEFAULT
;
117 request
->request
.op
.request_id
= 1;
119 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
120 "attributes-charset", NULL
, cupsLangEncoding(language
));
122 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
123 "attributes-natural-language", NULL
, language
->language
);
125 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
126 "requested-attributes",
127 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
129 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
131 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
132 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
134 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
136 char url
[HTTP_MAX_URI
]; /* New URL */
139 cgiSetVariable("DEFAULT_URI",
140 cgiRewriteURL(attr
->values
[0].string
.text
,
141 url
, sizeof(url
), NULL
));
148 * Get the printer info...
153 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
154 "attributes-charset", NULL
, cupsLangEncoding(language
));
156 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
157 "attributes-natural-language", NULL
, language
->language
);
162 * Build a CUPS_GET_PRINTERS request, which requires the following
166 * attributes-natural-language
169 request
->request
.op
.operation_id
= CUPS_GET_PRINTERS
;
170 request
->request
.op
.request_id
= 1;
172 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
174 ippAddInteger(request
, IPP_TAG_OPERATION
, IPP_TAG_ENUM
,
175 "printer-type-mask", CUPS_PRINTER_CLASS
);
177 if (getenv("REMOTE_USER") != NULL
)
178 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
179 NULL
, getenv("REMOTE_USER"));
184 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
188 * attributes-natural-language
192 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
193 request
->request
.op
.request_id
= 1;
195 httpAssembleURIf(uri
, sizeof(uri
), "ipp", NULL
, "localhost", 0,
196 "/printers/%s", printer
);
197 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
201 cgiGetAttributes(request
, "printers.tmpl");
204 * Do the request and get back a response...
207 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
210 * Got the result; set the CGI variables and check the status of a
211 * single-queue request...
214 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
216 if (printer
&& (attr
= ippFindAttribute(response
, "printer-state",
217 IPP_TAG_ENUM
)) != NULL
&&
218 attr
->values
[0].integer
== IPP_PRINTER_PROCESSING
)
221 * Printer is processing - automatically refresh the page until we
222 * are done printing...
225 cgiFormEncode(uri
, printer
, sizeof(uri
));
226 snprintf(refresh
, sizeof(refresh
), "10;/printers/%s", uri
);
227 cgiSetVariable("refresh_page", refresh
);
231 * Delete the response...
237 fprintf(stderr
, "ERROR: Get-Printer-Attributes request failed - %s (%x)\n",
238 ippErrorString(cupsLastError()), cupsLastError());
240 fprintf(stderr
, "ERROR: CUPS-Get-Printers request failed - %s (%x)\n",
241 ippErrorString(cupsLastError()), cupsLastError());
244 * Show the standard header...
247 cgiCopyTemplateLang("header.tmpl");
250 * Write the report...
253 cgiCopyTemplateLang("printers.tmpl");
256 * Get jobs for the specified printer if a printer has been chosen...
262 * Build an IPP_GET_JOBS request, which requires the following
266 * attributes-natural-language
272 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
273 "attributes-charset", NULL
, cupsLangEncoding(language
));
275 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
276 "attributes-natural-language", NULL
, language
->language
);
278 request
->request
.op
.operation_id
= IPP_GET_JOBS
;
279 request
->request
.op
.request_id
= 1;
281 httpAssembleURIf(uri
, sizeof(uri
), "ipp", NULL
, "localhost", 0,
282 "/printers/%s", printer
);
283 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
286 if ((which_jobs
= cgiGetVariable("which_jobs")) != NULL
)
287 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "which-jobs",
290 if (getenv("REMOTE_USER") != NULL
)
292 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
293 NULL
, getenv("REMOTE_USER"));
295 if (strcmp(getenv("REMOTE_USER"), "root"))
296 ippAddBoolean(request
, IPP_TAG_OPERATION
, "my-jobs", 1);
299 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
302 cgiGetAttributes(request
, "jobs.tmpl");
305 * Do the request and get back a response...
308 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
310 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
313 cgiCopyTemplateLang("jobs.tmpl");
316 fprintf(stderr
, "ERROR: Get-Jobs request failed - %s (%x)\n",
317 ippErrorString(cupsLastError()), cupsLastError());
323 * Print a test page...
326 char filename
[1024]; /* Test page filename */
327 const char *datadir
; /* CUPS_DATADIR env var */
330 cgiFormEncode(uri
, printer
, sizeof(uri
));
331 snprintf(refresh
, sizeof(refresh
), "2;/printers/%s", uri
);
332 cgiSetVariable("refresh_page", refresh
);
334 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
335 datadir
= CUPS_DATADIR
;
337 snprintf(filename
, sizeof(filename
), "%s/data/testprint.ps", datadir
);
338 httpAssembleURIf(uri
, sizeof(uri
), "ipp", NULL
, "localhost", 0,
339 "/printers/%s", printer
);
342 * Build an IPP_PRINT_JOB request, which requires the following
346 * attributes-natural-language
348 * requesting-user-name
354 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
355 request
->request
.op
.request_id
= 1;
357 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
358 "attributes-charset", NULL
, cupsLangEncoding(language
));
360 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
361 "attributes-natural-language", NULL
, language
->language
);
363 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
366 if (getenv("REMOTE_USER") != NULL
)
367 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
368 NULL
, getenv("REMOTE_USER"));
370 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
373 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
376 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
377 NULL
, "application/postscript");
380 * Do the request and get back a response...
383 if ((response
= cupsDoFileRequest(http
, request
, uri
+ 15,
386 status
= response
->request
.status
.status_code
;
387 cgiSetIPPVars(response
, NULL
, NULL
, NULL
, 0);
392 status
= cupsLastError();
394 cgiSetVariable("PRINTER_NAME", printer
);
397 * Show the standard header...
400 cgiCopyTemplateLang("header.tmpl");
406 if (status
> IPP_OK_CONFLICT
)
408 cgiSetVariable("ERROR", ippErrorString(status
));
409 cgiCopyTemplateLang("error.tmpl");
412 cgiCopyTemplateLang("test-page.tmpl");
415 cgiCopyTemplateLang("trailer.tmpl");
418 * Close the HTTP server connection...
422 cupsLangFree(language
);
425 * Return with no errors...
433 * End of "$Id: printers.c 4921 2006-01-12 21:26:26Z mike $".