4 * Class status CGI for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 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...
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 *pclass
; /* Printer class 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 */
57 "printer-uri-supported"
62 * Get any form variables...
66 op
= cgiGetVariable("OP");
69 * Get the request language...
72 language
= cupsLangDefault();
75 * Connect to the HTTP server...
78 http
= httpConnectEncrypt("localhost", ippPort(), cupsEncryption());
81 * Tell the client to expect HTML...
84 printf("Content-Type: text/html;charset=%s\n\n", cupsLangEncoding(language
));
87 * See if we need to show a list of printers or the status of a
91 ippSetServerVersion();
94 if (strcmp(pclass
, "/") == 0 || strcmp(pclass
, "classes.cgi") == 0)
97 cgiSetVariable("TITLE", cupsLangString(language
, CUPS_MSG_CLASS
));
100 cgiSetVariable("TITLE", pclass
);
102 cgiCopyTemplateLang(stdout
, TEMPLATES
, "header.tmpl", getenv("LANG"));
104 if (op
== NULL
|| strcasecmp(op
, "print-test-page") != 0)
107 * Get the default destination...
111 request
->request
.op
.operation_id
= CUPS_GET_DEFAULT
;
112 request
->request
.op
.request_id
= 1;
114 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
115 "attributes-charset", NULL
, cupsLangEncoding(language
));
117 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
118 "attributes-natural-language", NULL
, language
->language
);
120 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
,
121 "requested-attributes",
122 sizeof(def_attrs
) / sizeof(def_attrs
[0]), NULL
, def_attrs
);
124 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
126 if ((attr
= ippFindAttribute(response
, "printer-name", IPP_TAG_NAME
)) != NULL
)
127 cgiSetVariable("DEFAULT_NAME", attr
->values
[0].string
.text
);
129 if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
131 char url
[HTTP_MAX_URI
]; /* New URL */
134 cgiSetVariable("DEFAULT_URI",
135 ippRewriteURL(attr
->values
[0].string
.text
,
136 url
, sizeof(url
), NULL
));
143 * Get the class info...
148 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
149 "attributes-charset", NULL
, cupsLangEncoding(language
));
151 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
152 "attributes-natural-language", NULL
, language
->language
);
157 * Build a CUPS_GET_CLASSES request, which requires the following
161 * attributes-natural-language
164 request
->request
.op
.operation_id
= CUPS_GET_CLASSES
;
165 request
->request
.op
.request_id
= 1;
170 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
174 * attributes-natural-language
178 request
->request
.op
.operation_id
= IPP_GET_PRINTER_ATTRIBUTES
;
179 request
->request
.op
.request_id
= 1;
181 snprintf(uri
, sizeof(uri
), "ipp://%s/classes/%s", getenv("SERVER_NAME"),
183 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
187 ippGetAttributes(request
, TEMPLATES
, "classes.tmpl", getenv("LANG"));
190 * Do the request and get back a response...
193 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
195 ippSetCGIVars(response
, NULL
, NULL
, NULL
, 0);
200 * Write the report...
203 cgiCopyTemplateLang(stdout
, TEMPLATES
, "classes.tmpl", getenv("LANG"));
206 * Get jobs for the specified class if a class has been chosen...
212 * Build an IPP_GET_JOBS request, which requires the following
216 * attributes-natural-language
222 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
223 "attributes-charset", NULL
, cupsLangEncoding(language
));
225 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
226 "attributes-natural-language", NULL
, language
->language
);
228 request
->request
.op
.operation_id
= IPP_GET_JOBS
;
229 request
->request
.op
.request_id
= 1;
231 snprintf(uri
, sizeof(uri
), "ipp://%s/classes/%s", getenv("SERVER_NAME"),
233 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
,
236 if ((which_jobs
= cgiGetVariable("which_jobs")) != NULL
)
237 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "which-jobs",
240 ippGetAttributes(request
, TEMPLATES
, "jobs.tmpl", getenv("LANG"));
243 * Do the request and get back a response...
246 if ((response
= cupsDoRequest(http
, request
, "/")) != NULL
)
248 ippSetCGIVars(response
, NULL
, NULL
, NULL
, 0);
251 cgiCopyTemplateLang(stdout
, TEMPLATES
, "jobs.tmpl", getenv("LANG"));
258 * Print a test page...
261 snprintf(uri
, sizeof(uri
), "ipp://localhost/classes/%s", pclass
);
264 * Build an IPP_PRINT_JOB request, which requires the following
268 * attributes-natural-language
270 * requesting-user-name
276 request
->request
.op
.operation_id
= IPP_PRINT_JOB
;
277 request
->request
.op
.request_id
= 1;
279 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_CHARSET
,
280 "attributes-charset", NULL
, cupsLangEncoding(language
));
282 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_LANGUAGE
,
283 "attributes-natural-language", NULL
, language
->language
);
285 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri",
288 if (getenv("REMOTE_USER") != NULL
)
289 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
290 NULL
, getenv("REMOTE_USER"));
292 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "requesting-user-name",
295 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "job-name",
298 ippAddString(request
, IPP_TAG_JOB
, IPP_TAG_MIMETYPE
, "document-format",
299 NULL
, "application/postscript");
302 * Do the request and get back a response...
305 if ((response
= cupsDoFileRequest(http
, request
, uri
+ 15,
306 CUPS_DATADIR
"/data/testprint.ps")) != NULL
)
308 status
= response
->request
.status
.status_code
;
309 ippSetCGIVars(response
, NULL
, NULL
, NULL
, 0);
314 status
= cupsLastError();
316 cgiSetVariable("PRINTER_NAME", pclass
);
318 if (status
> IPP_OK_CONFLICT
)
320 cgiSetVariable("ERROR", ippErrorString(status
));
321 cgiCopyTemplateLang(stdout
, TEMPLATES
, "error.tmpl", getenv("LANG"));
324 cgiCopyTemplateLang(stdout
, TEMPLATES
, "test-page.tmpl", getenv("LANG"));
327 cgiCopyTemplateLang(stdout
, TEMPLATES
, "trailer.tmpl", getenv("LANG"));
330 * Close the HTTP server connection...
334 cupsLangFree(language
);
337 * Return with no errors...