]> git.ipfire.org Git - thirdparty/cups.git/blame - cgi-bin/printers.c
Update copyright notices, addresses, etc.
[thirdparty/cups.git] / cgi-bin / printers.c
CommitLineData
c4adf588 1/*
c9d3f842 2 * "$Id$"
c4adf588 3 *
4 * Printer status CGI for the Common UNIX Printing System (CUPS).
5 *
c9d3f842 6 * Copyright 1997-2005 by Easy Software Products.
c4adf588 7 *
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
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
c9d3f842 18 * Hollywood, Maryland 20636 USA
c4adf588 19 *
9639c4de 20 * Voice: (301) 373-9600
c4adf588 21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
13c1a6d9 26 * main() - Main entry for CGI.
c4adf588 27 */
28
29/*
30 * Include necessary headers...
31 */
32
13c1a6d9 33#include "ipp-var.h"
c4adf588 34
35
36/*
37 * 'main()' - Main entry for CGI.
38 */
39
ae6a971c 40int /* O - Exit status */
41main(int argc, /* I - Number of command-line arguments */
42 char *argv[]) /* I - Command-line arguments */
c4adf588 43{
ae6a971c 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 static const char *def_attrs[] = /* Attributes for default printer */
55 {
56 "printer-name",
57 "printer-uri-supported"
58 };
59
dd63ebe2 60
f63a2256 61 /*
62 * Get any form variables...
63 */
c4adf588 64
f63a2256 65 cgiInitialize();
3d9e2586 66 op = cgiGetVariable("OP");
c4adf588 67
68 /*
69 * Get the request language...
70 */
71
da376774 72 language = cupsLangDefault();
c4adf588 73
74 /*
75 * Connect to the HTTP server...
76 */
77
b5cb0608 78 http = httpConnectEncrypt("localhost", ippPort(), cupsEncryption());
c4adf588 79
80 /*
81 * Tell the client to expect HTML...
82 */
83
dd63ebe2 84 printf("Content-Type: text/html;charset=%s\r\n\r\n",
85 cupsLangEncoding(language));
c4adf588 86
87 /*
88 * See if we need to show a list of printers or the status of a
89 * single printer...
90 */
91
3d9e2586 92 ippSetServerVersion();
93
c4adf588 94 printer = argv[0];
a9775e85 95 if (strcmp(printer, "/") == 0 || strstr(printer, "printers.cgi") != NULL)
13c1a6d9 96 {
c4adf588 97 printer = NULL;
3d9e2586 98 cgiSetVariable("TITLE", cupsLangString(language, CUPS_MSG_PRINTER));
13c1a6d9 99 }
c4adf588 100 else
13c1a6d9 101 cgiSetVariable("TITLE", printer);
c4adf588 102
3d9e2586 103 cgiCopyTemplateLang(stdout, TEMPLATES, "header.tmpl", getenv("LANG"));
c4adf588 104
3d9e2586 105 if (op == NULL || strcasecmp(op, "print-test-page") != 0)
106 {
96aa5c42 107 /*
108 * Get the default destination...
109 */
110
111 request = ippNew();
0a3ac972 112 request->request.op.operation_id = CUPS_GET_DEFAULT;
113 request->request.op.request_id = 1;
96aa5c42 114
115 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
116 "attributes-charset", NULL, cupsLangEncoding(language));
117
118 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
119 "attributes-natural-language", NULL, language->language);
120
ae6a971c 121 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
122 "requested-attributes",
123 sizeof(def_attrs) / sizeof(def_attrs[0]), NULL, def_attrs);
124
96aa5c42 125 if ((response = cupsDoRequest(http, request, "/")) != NULL)
126 {
127 if ((attr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL)
128 cgiSetVariable("DEFAULT_NAME", attr->values[0].string.text);
129
130 if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
131 {
d68320d4 132 char url[HTTP_MAX_URI]; /* New URL */
96aa5c42 133
134
d68320d4 135 cgiSetVariable("DEFAULT_URI",
136 ippRewriteURL(attr->values[0].string.text,
00966aa1 137 url, sizeof(url), NULL));
96aa5c42 138 }
139
140 ippDelete(response);
141 }
142
3d9e2586 143 /*
144 * Get the printer info...
145 */
146
147 request = ippNew();
c4adf588 148
3d9e2586 149 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
150 "attributes-charset", NULL, cupsLangEncoding(language));
c4adf588 151
3d9e2586 152 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
153 "attributes-natural-language", NULL, language->language);
154
155 if (printer == NULL)
156 {
157 /*
158 * Build a CUPS_GET_PRINTERS request, which requires the following
159 * attributes:
160 *
161 * attributes-charset
162 * attributes-natural-language
163 */
164
0a3ac972 165 request->request.op.operation_id = CUPS_GET_PRINTERS;
166 request->request.op.request_id = 1;
3d9e2586 167 }
168 else
169 {
170 /*
171 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
172 * attributes:
173 *
174 * attributes-charset
175 * attributes-natural-language
176 * printer-uri
177 */
178
0a3ac972 179 request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
180 request->request.op.request_id = 1;
3d9e2586 181
182 snprintf(uri, sizeof(uri), "ipp://%s/printers/%s", getenv("SERVER_NAME"),
183 printer);
184 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
185 uri);
186 }
c4adf588 187
ae6a971c 188 ippGetAttributes(request, TEMPLATES, "printers.tmpl", getenv("LANG"));
71e8876a 189
c4adf588 190 /*
3d9e2586 191 * Do the request and get back a response...
c4adf588 192 */
193
3d9e2586 194 if ((response = cupsDoRequest(http, request, "/")) != NULL)
195 {
dd63ebe2 196 ippSetCGIVars(response, NULL, NULL, NULL, 0);
3d9e2586 197 ippDelete(response);
198 }
dd63ebe2 199 else if (printer)
200 fprintf(stderr, "ERROR: Get-Printer-Attributes request failed - %s (%x)\n",
201 ippErrorString(cupsLastError()), cupsLastError());
202 else
203 fprintf(stderr, "ERROR: CUPS-Get-Printers request failed - %s (%x)\n",
204 ippErrorString(cupsLastError()), cupsLastError());
3d9e2586 205
c4adf588 206 /*
3d9e2586 207 * Write the report...
c4adf588 208 */
209
3d9e2586 210 cgiCopyTemplateLang(stdout, TEMPLATES, "printers.tmpl", getenv("LANG"));
580af8cf 211
3d9e2586 212 /*
213 * Get jobs for the specified printer if a printer has been chosen...
214 */
580af8cf 215
3d9e2586 216 if (printer != NULL)
217 {
218 /*
219 * Build an IPP_GET_JOBS request, which requires the following
220 * attributes:
221 *
222 * attributes-charset
223 * attributes-natural-language
224 * printer-uri
225 */
226
227 request = ippNew();
228
229 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
230 "attributes-charset", NULL, cupsLangEncoding(language));
231
232 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
233 "attributes-natural-language", NULL, language->language);
234
0a3ac972 235 request->request.op.operation_id = IPP_GET_JOBS;
236 request->request.op.request_id = 1;
3d9e2586 237
238 snprintf(uri, sizeof(uri), "ipp://%s/printers/%s", getenv("SERVER_NAME"),
239 printer);
240 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
241 uri);
242
243 if ((which_jobs = cgiGetVariable("which_jobs")) != NULL)
244 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "which-jobs",
245 NULL, which_jobs);
246
ae6a971c 247 ippGetAttributes(request, TEMPLATES, "jobs.tmpl", getenv("LANG"));
248
3d9e2586 249 /*
250 * Do the request and get back a response...
251 */
252
253 if ((response = cupsDoRequest(http, request, "/")) != NULL)
254 {
dd63ebe2 255 ippSetCGIVars(response, NULL, NULL, NULL, 0);
3d9e2586 256 ippDelete(response);
257
258 cgiCopyTemplateLang(stdout, TEMPLATES, "jobs.tmpl", getenv("LANG"));
259 }
dd63ebe2 260 else
261 fprintf(stderr, "ERROR: Get-Jobs request failed - %s (%x)\n",
262 ippErrorString(cupsLastError()), cupsLastError());
3d9e2586 263 }
13c1a6d9 264 }
3d9e2586 265 else
266 {
267 /*
268 * Print a test page...
269 */
c4adf588 270
3d9e2586 271 snprintf(uri, sizeof(uri), "ipp://localhost/printers/%s", printer);
100cd8c8 272
100cd8c8 273 /*
3d9e2586 274 * Build an IPP_PRINT_JOB request, which requires the following
100cd8c8 275 * attributes:
276 *
277 * attributes-charset
278 * attributes-natural-language
279 * printer-uri
3d9e2586 280 * requesting-user-name
281 * document-format
100cd8c8 282 */
283
284 request = ippNew();
285
0a3ac972 286 request->request.op.operation_id = IPP_PRINT_JOB;
287 request->request.op.request_id = 1;
3d9e2586 288
100cd8c8 289 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
290 "attributes-charset", NULL, cupsLangEncoding(language));
291
292 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
293 "attributes-natural-language", NULL, language->language);
294
3d9e2586 295 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
296 NULL, uri);
100cd8c8 297
3d9e2586 298 if (getenv("REMOTE_USER") != NULL)
299 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
300 NULL, getenv("REMOTE_USER"));
301 else
302 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
303 NULL, "root");
100cd8c8 304
3d9e2586 305 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name",
306 NULL, "Test Page");
307
308 ippAddString(request, IPP_TAG_JOB, IPP_TAG_MIMETYPE, "document-format",
309 NULL, "application/postscript");
f63a2256 310
100cd8c8 311 /*
312 * Do the request and get back a response...
313 */
314
3d9e2586 315 if ((response = cupsDoFileRequest(http, request, uri + 15,
316 CUPS_DATADIR "/data/testprint.ps")) != NULL)
100cd8c8 317 {
0a3ac972 318 status = response->request.status.status_code;
dd63ebe2 319 ippSetCGIVars(response, NULL, NULL, NULL, 0);
3d9e2586 320
100cd8c8 321 ippDelete(response);
3d9e2586 322 }
323 else
dd44d54e 324 status = cupsLastError();
100cd8c8 325
3d9e2586 326 cgiSetVariable("PRINTER_NAME", printer);
327
328 if (status > IPP_OK_CONFLICT)
329 {
330 cgiSetVariable("ERROR", ippErrorString(status));
331 cgiCopyTemplateLang(stdout, TEMPLATES, "error.tmpl", getenv("LANG"));
100cd8c8 332 }
3d9e2586 333 else
334 cgiCopyTemplateLang(stdout, TEMPLATES, "test-page.tmpl", getenv("LANG"));
100cd8c8 335 }
336
a3e17a89 337 cgiCopyTemplateLang(stdout, TEMPLATES, "trailer.tmpl", getenv("LANG"));
c4adf588 338
13c1a6d9 339 /*
340 * Close the HTTP server connection...
341 */
c4adf588 342
13c1a6d9 343 httpClose(http);
344 cupsLangFree(language);
580af8cf 345
13c1a6d9 346 /*
347 * Return with no errors...
348 */
580af8cf 349
13c1a6d9 350 return (0);
c4adf588 351}
352
353
354/*
c9d3f842 355 * End of "$Id$".
c4adf588 356 */