]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
b19ccc9e | 2 | * "$Id: util.c 7850 2008-08-20 00:07:25Z mike $" |
ef416fc2 | 3 | * |
4 | * Printing utilities for the Common UNIX Printing System (CUPS). | |
5 | * | |
3d052e43 | 6 | * Copyright 2007-2008 by Apple Inc. |
ef416fc2 | 7 | * Copyright 1997-2006 by Easy Software Products. |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
fa73b229 | 19 | * cupsCancelJob() - Cancel a print job on the default server. |
3d052e43 MS |
20 | * cupsCancelJob2() - Cancel or purge a print job. |
21 | * cupsCreateJob() - Create an empty job. | |
22 | * cupsFinishDocument() - Finish sending a document. | |
fa73b229 | 23 | * cupsFreeJobs() - Free memory used by job data. |
24 | * cupsGetClasses() - Get a list of printer classes from the default | |
25 | * server. | |
3d052e43 MS |
26 | * cupsGetDefault() - Get the default printer or class for the default |
27 | * server. | |
28 | * cupsGetDefault2() - Get the default printer or class for the specified | |
fa73b229 | 29 | * server. |
fa73b229 | 30 | * cupsGetJobs() - Get the jobs from the default server. |
31 | * cupsGetJobs2() - Get the jobs from the specified server. | |
32 | * cupsGetPPD() - Get the PPD file for a printer on the default | |
33 | * server. | |
3d052e43 | 34 | * cupsGetPPD2() - Get the PPD file for a printer from the specified |
fa73b229 | 35 | * server. |
2e4ff8af MS |
36 | * cupsGetPPD3() - Get the PPD file for a printer on the specified |
37 | * server if it has changed. | |
fa73b229 | 38 | * cupsGetPrinters() - Get a list of printers from the default server. |
b94498cf | 39 | * cupsGetServerPPD() - Get an available PPD file from the server. |
fa73b229 | 40 | * cupsLastError() - Return the last IPP status code. |
41 | * cupsLastErrorString() - Return the last IPP status-message. | |
42 | * cupsPrintFile() - Print a file to a printer or class on the default | |
43 | * server. | |
44 | * cupsPrintFile2() - Print a file to a printer or class on the | |
45 | * specified server. | |
46 | * cupsPrintFiles() - Print one or more files to a printer or class on | |
47 | * the default server. | |
48 | * cupsPrintFiles2() - Print one or more files to a printer or class on | |
49 | * the specified server. | |
3d052e43 MS |
50 | * cupsStartDocument() - Add a document to a job created with |
51 | * cupsCreateJob(). | |
52 | * _cupsConnect() - Get the default server connection... | |
fa73b229 | 53 | * cups_get_printer_uri() - Get the printer-uri-supported attribute for the |
54 | * first printer in a class. | |
ef416fc2 | 55 | */ |
56 | ||
57 | /* | |
58 | * Include necessary headers... | |
59 | */ | |
60 | ||
61 | #include "globals.h" | |
62 | #include "debug.h" | |
63 | #include <stdlib.h> | |
64 | #include <errno.h> | |
65 | #include <fcntl.h> | |
66 | #include <sys/stat.h> | |
67 | #if defined(WIN32) || defined(__EMX__) | |
68 | # include <io.h> | |
69 | #else | |
70 | # include <unistd.h> | |
71 | #endif /* WIN32 || __EMX__ */ | |
72 | ||
73 | ||
74 | /* | |
75 | * Local functions... | |
76 | */ | |
77 | ||
fa73b229 | 78 | static int cups_get_printer_uri(http_t *http, const char *name, |
79 | char *host, int hostsize, int *port, | |
80 | char *resource, int resourcesize, | |
81 | int depth); | |
ef416fc2 | 82 | |
83 | ||
84 | /* | |
85 | * 'cupsCancelJob()' - Cancel a print job on the default server. | |
86 | * | |
5a738aea MS |
87 | * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@ |
88 | * to cancel the current job on the named destination. | |
89 | * | |
90 | * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get | |
ef416fc2 | 91 | * the cause of any failure. |
92 | */ | |
93 | ||
94 | int /* O - 1 on success, 0 on failure */ | |
95 | cupsCancelJob(const char *name, /* I - Name of printer or class */ | |
568fa3fa | 96 | int job_id) /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */ |
ef416fc2 | 97 | { |
5a738aea | 98 | return (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0) |
3d052e43 MS |
99 | < IPP_REDIRECTION_OTHER_SITE); |
100 | } | |
101 | ||
102 | ||
103 | /* | |
104 | * 'cupsCancelJob2()' - Cancel or purge a print job. | |
105 | * | |
106 | * Canceled jobs remain in the job history while purged jobs are removed | |
107 | * from the job history. | |
108 | * | |
5a738aea MS |
109 | * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@ |
110 | * to cancel the current job on the named destination. | |
111 | * | |
112 | * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get | |
3d052e43 MS |
113 | * the cause of any failure. |
114 | * | |
115 | * @since CUPS 1.4@ | |
116 | */ | |
117 | ||
118 | ipp_status_t /* O - IPP status */ | |
568fa3fa | 119 | cupsCancelJob2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
5a738aea | 120 | const char *name, /* I - Name of printer or class */ |
568fa3fa | 121 | int job_id, /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */ |
5a738aea | 122 | int purge) /* I - 1 to purge, 0 to cancel */ |
3d052e43 | 123 | { |
5a738aea | 124 | char uri[HTTP_MAX_URI]; /* Job/printer URI */ |
3d052e43 | 125 | ipp_t *request; /* IPP request */ |
ef416fc2 | 126 | |
127 | ||
128 | /* | |
3d052e43 | 129 | * Range check input... |
ef416fc2 | 130 | */ |
131 | ||
5a738aea | 132 | if (job_id < -1 || (!name && job_id == 0)) |
ef416fc2 | 133 | { |
749b1e90 | 134 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); |
ef416fc2 | 135 | return (0); |
136 | } | |
137 | ||
138 | /* | |
3d052e43 | 139 | * Connect to the default server as needed... |
ef416fc2 | 140 | */ |
141 | ||
3d052e43 MS |
142 | if (!http) |
143 | if ((http = _cupsConnect()) == NULL) | |
144 | return (IPP_SERVICE_UNAVAILABLE); | |
ef416fc2 | 145 | |
146 | /* | |
5a738aea | 147 | * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following |
ef416fc2 | 148 | * attributes: |
149 | * | |
150 | * attributes-charset | |
151 | * attributes-natural-language | |
5a738aea | 152 | * job-uri or printer-uri + job-id |
3d052e43 | 153 | * requesting-user-name |
5a738aea | 154 | * [purge-job] or [purge-jobs] |
ef416fc2 | 155 | */ |
156 | ||
5a738aea | 157 | request = ippNewRequest(job_id < 0 ? IPP_PURGE_JOBS : IPP_CANCEL_JOB); |
ef416fc2 | 158 | |
5a738aea MS |
159 | if (name) |
160 | { | |
161 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
162 | "localhost", ippPort(), "/printers/%s", name); | |
163 | ||
164 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, | |
165 | uri); | |
166 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", | |
167 | job_id); | |
168 | } | |
169 | else if (job_id > 0) | |
170 | { | |
171 | snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id); | |
172 | ||
173 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri); | |
174 | } | |
ef416fc2 | 175 | |
176 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
177 | NULL, cupsUser()); | |
5a738aea MS |
178 | |
179 | if (purge && job_id >= 0) | |
3d052e43 | 180 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-job", 1); |
5a738aea MS |
181 | else if (!purge && job_id < 0) |
182 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", 0); | |
ef416fc2 | 183 | |
184 | /* | |
185 | * Do the request... | |
186 | */ | |
187 | ||
3d052e43 MS |
188 | ippDelete(cupsDoRequest(http, request, "/jobs/")); |
189 | ||
190 | return (cupsLastError()); | |
191 | } | |
192 | ||
193 | ||
194 | /* | |
568fa3fa | 195 | * 'cupsCreateJob()' - Create an empty job for streaming. |
3d052e43 | 196 | * |
568fa3fa MS |
197 | * Use this function when you want to stream print data using the |
198 | * @link cupsStartDocument@, @link cupsWriteRequestData@, and | |
199 | * @link cupsFinishDocument@ functions. If you have one or more files to | |
200 | * print, use the @link cupsPrintFile2@ or @link cupsPrintFiles2@ function | |
201 | * instead. | |
3d052e43 MS |
202 | * |
203 | * @since CUPS 1.4@ | |
204 | */ | |
205 | ||
206 | int /* O - Job ID or 0 on error */ | |
207 | cupsCreateJob( | |
568fa3fa MS |
208 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
209 | const char *name, /* I - Destination name */ | |
3d052e43 MS |
210 | const char *title, /* I - Title of job */ |
211 | int num_options, /* I - Number of options */ | |
212 | cups_option_t *options) /* I - Options */ | |
213 | { | |
214 | char printer_uri[1024], /* Printer URI */ | |
215 | resource[1024]; /* Printer resource */ | |
216 | ipp_t *request, /* Create-Job request */ | |
217 | *response; /* Create-Job response */ | |
218 | ipp_attribute_t *attr; /* job-id attribute */ | |
219 | int job_id = 0; /* job-id value */ | |
220 | ||
221 | ||
222 | DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", " | |
223 | "num_options=%d, options=%p)\n", | |
224 | http, name, title, num_options, options)); | |
225 | ||
226 | /* | |
227 | * Range check input... | |
228 | */ | |
229 | ||
230 | if (!name) | |
231 | { | |
749b1e90 | 232 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); |
3d052e43 MS |
233 | return (0); |
234 | } | |
235 | ||
236 | /* | |
237 | * Build a Create-Job request... | |
238 | */ | |
239 | ||
240 | if ((request = ippNewRequest(IPP_CREATE_JOB)) == NULL) | |
241 | { | |
749b1e90 | 242 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0); |
3d052e43 MS |
243 | return (0); |
244 | } | |
245 | ||
246 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp", | |
247 | NULL, "localhost", ippPort(), "/printers/%s", name); | |
248 | snprintf(resource, sizeof(resource), "/printers/%s", name); | |
249 | ||
250 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
251 | NULL, printer_uri); | |
252 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
253 | NULL, cupsUser()); | |
254 | if (title) | |
255 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL, | |
256 | title); | |
257 | cupsEncodeOptions(request, num_options, options); | |
258 | ||
259 | /* | |
260 | * Send the request and get the job-id... | |
261 | */ | |
262 | ||
263 | response = cupsDoRequest(http, request, resource); | |
264 | ||
265 | if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) != NULL) | |
266 | job_id = attr->values[0].integer; | |
267 | ||
268 | ippDelete(response); | |
269 | ||
270 | /* | |
271 | * Return it... | |
272 | */ | |
273 | ||
274 | return (job_id); | |
275 | } | |
276 | ||
277 | ||
278 | /* | |
279 | * 'cupsFinishDocument()' - Finish sending a document. | |
280 | * | |
5a738aea MS |
281 | * The document must have been started using @link cupsStartDocument@. |
282 | * | |
3d052e43 MS |
283 | * @since CUPS 1.4@ |
284 | */ | |
285 | ||
286 | ipp_status_t /* O - Status of document submission */ | |
568fa3fa MS |
287 | cupsFinishDocument(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
288 | const char *name) /* I - Destination name */ | |
3d052e43 MS |
289 | { |
290 | char resource[1024]; /* Printer resource */ | |
291 | ||
292 | ||
293 | snprintf(resource, sizeof(resource), "/printers/%s", name); | |
ef416fc2 | 294 | |
3d052e43 MS |
295 | ippDelete(cupsGetResponse(http, resource)); |
296 | ||
297 | return (cupsLastError()); | |
ef416fc2 | 298 | } |
299 | ||
300 | ||
ef416fc2 | 301 | /* |
302 | * 'cupsFreeJobs()' - Free memory used by job data. | |
303 | */ | |
304 | ||
305 | void | |
306 | cupsFreeJobs(int num_jobs, /* I - Number of jobs */ | |
307 | cups_job_t *jobs) /* I - Jobs */ | |
308 | { | |
3d052e43 MS |
309 | int i; /* Looping var */ |
310 | cups_job_t *job; /* Current job */ | |
ef416fc2 | 311 | |
312 | ||
3d052e43 | 313 | if (num_jobs <= 0 || !jobs) |
ef416fc2 | 314 | return; |
315 | ||
3d052e43 | 316 | for (i = num_jobs, job = jobs; i > 0; i --, job ++) |
ef416fc2 | 317 | { |
3d052e43 MS |
318 | _cupsStrFree(job->dest); |
319 | _cupsStrFree(job->user); | |
320 | _cupsStrFree(job->format); | |
321 | _cupsStrFree(job->title); | |
ef416fc2 | 322 | } |
323 | ||
324 | free(jobs); | |
325 | } | |
326 | ||
327 | ||
328 | /* | |
329 | * 'cupsGetClasses()' - Get a list of printer classes from the default server. | |
330 | * | |
5a738aea | 331 | * This function is deprecated - use @link cupsGetDests@ instead. |
ef416fc2 | 332 | * |
333 | * @deprecated@ | |
334 | */ | |
335 | ||
336 | int /* O - Number of classes */ | |
337 | cupsGetClasses(char ***classes) /* O - Classes */ | |
338 | { | |
339 | int n; /* Number of classes */ | |
340 | ipp_t *request, /* IPP Request */ | |
341 | *response; /* IPP Response */ | |
342 | ipp_attribute_t *attr; /* Current attribute */ | |
ef416fc2 | 343 | char **temp; /* Temporary pointer */ |
3d052e43 | 344 | http_t *http; /* Connection to server */ |
ef416fc2 | 345 | |
346 | ||
3d052e43 | 347 | if (!classes) |
ef416fc2 | 348 | { |
749b1e90 | 349 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); |
ef416fc2 | 350 | |
351 | return (0); | |
352 | } | |
353 | ||
3d052e43 | 354 | *classes = NULL; |
ef416fc2 | 355 | |
3d052e43 | 356 | if ((http = _cupsConnect()) == NULL) |
ef416fc2 | 357 | return (0); |
ef416fc2 | 358 | |
359 | /* | |
360 | * Build a CUPS_GET_CLASSES request, which requires the following | |
361 | * attributes: | |
362 | * | |
363 | * attributes-charset | |
364 | * attributes-natural-language | |
365 | * requested-attributes | |
366 | */ | |
367 | ||
2e4ff8af | 368 | request = ippNewRequest(CUPS_GET_CLASSES); |
ef416fc2 | 369 | |
370 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
371 | "requested-attributes", NULL, "printer-name"); | |
372 | ||
373 | /* | |
374 | * Do the request and get back a response... | |
375 | */ | |
376 | ||
3d052e43 | 377 | n = 0; |
ef416fc2 | 378 | |
3d052e43 | 379 | if ((response = cupsDoRequest(http, request, "/")) != NULL) |
ef416fc2 | 380 | { |
381 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
382 | if (attr->name != NULL && | |
383 | strcasecmp(attr->name, "printer-name") == 0 && | |
384 | attr->value_tag == IPP_TAG_NAME) | |
385 | { | |
386 | if (n == 0) | |
387 | temp = malloc(sizeof(char *)); | |
388 | else | |
389 | temp = realloc(*classes, sizeof(char *) * (n + 1)); | |
390 | ||
391 | if (temp == NULL) | |
392 | { | |
393 | /* | |
394 | * Ran out of memory! | |
395 | */ | |
396 | ||
397 | while (n > 0) | |
398 | { | |
399 | n --; | |
400 | free((*classes)[n]); | |
401 | } | |
402 | ||
403 | free(*classes); | |
404 | ippDelete(response); | |
405 | return (0); | |
406 | } | |
407 | ||
408 | *classes = temp; | |
409 | temp[n] = strdup(attr->values[0].string.text); | |
410 | n ++; | |
411 | } | |
412 | ||
413 | ippDelete(response); | |
414 | } | |
415 | ||
416 | return (n); | |
417 | } | |
418 | ||
419 | ||
420 | /* | |
421 | * 'cupsGetDefault()' - Get the default printer or class for the default server. | |
422 | * | |
423 | * This function returns the default printer or class as defined by | |
424 | * the LPDEST or PRINTER environment variables. If these environment | |
425 | * variables are not set, the server default destination is returned. | |
5a738aea MS |
426 | * Applications should use the @link cupsGetDests@ and @link cupsGetDest@ |
427 | * functions to get the user-defined default printer, as this function does | |
428 | * not support the lpoptions-defined default printer. | |
ef416fc2 | 429 | */ |
430 | ||
5a738aea | 431 | const char * /* O - Default printer or @code NULL@ */ |
ef416fc2 | 432 | cupsGetDefault(void) |
433 | { | |
ef416fc2 | 434 | /* |
435 | * Return the default printer... | |
436 | */ | |
437 | ||
3d052e43 | 438 | return (cupsGetDefault2(CUPS_HTTP_DEFAULT)); |
ef416fc2 | 439 | } |
440 | ||
441 | ||
442 | /* | |
443 | * 'cupsGetDefault2()' - Get the default printer or class for the specified server. | |
444 | * | |
445 | * This function returns the default printer or class as defined by | |
446 | * the LPDEST or PRINTER environment variables. If these environment | |
447 | * variables are not set, the server default destination is returned. | |
5a738aea MS |
448 | * Applications should use the @link cupsGetDests@ and @link cupsGetDest@ |
449 | * functions to get the user-defined default printer, as this function does | |
450 | * not support the lpoptions-defined default printer. | |
ef416fc2 | 451 | * |
452 | * @since CUPS 1.1.21@ | |
453 | */ | |
454 | ||
5a738aea | 455 | const char * /* O - Default printer or @code NULL@ */ |
568fa3fa | 456 | cupsGetDefault2(http_t *http) /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 457 | { |
458 | ipp_t *request, /* IPP Request */ | |
459 | *response; /* IPP Response */ | |
460 | ipp_attribute_t *attr; /* Current attribute */ | |
ef416fc2 | 461 | const char *var; /* Environment variable */ |
462 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
463 | ||
464 | ||
465 | /* | |
466 | * First see if the LPDEST or PRINTER environment variables are | |
467 | * set... However, if PRINTER is set to "lp", ignore it to work | |
468 | * around a "feature" in most Linux distributions - the default | |
469 | * user login scripts set PRINTER to "lp"... | |
470 | */ | |
471 | ||
472 | if ((var = getenv("LPDEST")) != NULL) | |
473 | return (var); | |
474 | else if ((var = getenv("PRINTER")) != NULL && strcmp(var, "lp") != 0) | |
475 | return (var); | |
476 | ||
477 | /* | |
3d052e43 | 478 | * Connect to the server as needed... |
ef416fc2 | 479 | */ |
480 | ||
481 | if (!http) | |
3d052e43 MS |
482 | if ((http = _cupsConnect()) == NULL) |
483 | return (NULL); | |
ef416fc2 | 484 | |
485 | /* | |
486 | * Build a CUPS_GET_DEFAULT request, which requires the following | |
487 | * attributes: | |
488 | * | |
489 | * attributes-charset | |
490 | * attributes-natural-language | |
491 | */ | |
492 | ||
2e4ff8af | 493 | request = ippNewRequest(CUPS_GET_DEFAULT); |
ef416fc2 | 494 | |
495 | /* | |
496 | * Do the request and get back a response... | |
497 | */ | |
498 | ||
499 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
500 | { | |
3d052e43 MS |
501 | if ((attr = ippFindAttribute(response, "printer-name", |
502 | IPP_TAG_NAME)) != NULL) | |
ef416fc2 | 503 | { |
3d052e43 MS |
504 | strlcpy(cg->def_printer, attr->values[0].string.text, |
505 | sizeof(cg->def_printer)); | |
ef416fc2 | 506 | ippDelete(response); |
507 | return (cg->def_printer); | |
508 | } | |
509 | ||
510 | ippDelete(response); | |
511 | } | |
512 | ||
513 | return (NULL); | |
514 | } | |
515 | ||
516 | ||
517 | /* | |
518 | * 'cupsGetJobs()' - Get the jobs from the default server. | |
5a738aea MS |
519 | * |
520 | * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless | |
521 | * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are | |
522 | * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns | |
523 | * jobs that are stopped, canceled, aborted, or completed. | |
ef416fc2 | 524 | */ |
525 | ||
526 | int /* O - Number of jobs */ | |
527 | cupsGetJobs(cups_job_t **jobs, /* O - Job data */ | |
568fa3fa | 528 | const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */ |
ecdc0628 | 529 | int myjobs, /* I - 0 = all users, 1 = mine */ |
5a738aea | 530 | int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */ |
ef416fc2 | 531 | { |
ef416fc2 | 532 | /* |
533 | * Return the jobs... | |
534 | */ | |
535 | ||
5a738aea | 536 | return (cupsGetJobs2(CUPS_HTTP_DEFAULT, jobs, name, myjobs, whichjobs)); |
ef416fc2 | 537 | } |
538 | ||
539 | ||
540 | ||
541 | /* | |
542 | * 'cupsGetJobs2()' - Get the jobs from the specified server. | |
543 | * | |
5a738aea MS |
544 | * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless |
545 | * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are | |
546 | * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns | |
547 | * jobs that are stopped, canceled, aborted, or completed. | |
548 | * | |
ef416fc2 | 549 | * @since CUPS 1.1.21@ |
550 | */ | |
551 | ||
552 | int /* O - Number of jobs */ | |
568fa3fa | 553 | cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
ef416fc2 | 554 | cups_job_t **jobs, /* O - Job data */ |
568fa3fa | 555 | const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */ |
ecdc0628 | 556 | int myjobs, /* I - 0 = all users, 1 = mine */ |
5a738aea | 557 | int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */ |
ef416fc2 | 558 | { |
559 | int n; /* Number of jobs */ | |
560 | ipp_t *request, /* IPP Request */ | |
561 | *response; /* IPP Response */ | |
562 | ipp_attribute_t *attr; /* Current attribute */ | |
ef416fc2 | 563 | cups_job_t *temp; /* Temporary pointer */ |
564 | int id, /* job-id */ | |
565 | priority, /* job-priority */ | |
566 | size; /* job-k-octets */ | |
567 | ipp_jstate_t state; /* job-state */ | |
568 | time_t completed_time, /* time-at-completed */ | |
569 | creation_time, /* time-at-creation */ | |
570 | processing_time; /* time-at-processing */ | |
571 | const char *dest, /* job-printer-uri */ | |
572 | *format, /* document-format */ | |
573 | *title, /* job-name */ | |
574 | *user; /* job-originating-user-name */ | |
575 | char uri[HTTP_MAX_URI]; /* URI for jobs */ | |
576 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
577 | static const char * const attrs[] = /* Requested attributes */ | |
578 | { | |
579 | "job-id", | |
580 | "job-priority", | |
581 | "job-k-octets", | |
582 | "job-state", | |
583 | "time-at-completed", | |
584 | "time-at-creation", | |
585 | "time-at-processing", | |
586 | "job-printer-uri", | |
587 | "document-format", | |
588 | "job-name", | |
589 | "job-originating-user-name" | |
590 | }; | |
591 | ||
592 | ||
593 | /* | |
594 | * Range check input... | |
595 | */ | |
596 | ||
3d052e43 | 597 | if (!jobs) |
ef416fc2 | 598 | { |
749b1e90 | 599 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); |
ef416fc2 | 600 | |
601 | return (-1); | |
602 | } | |
603 | ||
604 | /* | |
605 | * Get the right URI... | |
606 | */ | |
607 | ||
5a738aea | 608 | if (name) |
ef416fc2 | 609 | { |
a4d04587 | 610 | if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
5a738aea | 611 | "localhost", 0, "/printers/%s", name) != HTTP_URI_OK) |
ef416fc2 | 612 | { |
749b1e90 | 613 | _cupsSetError(IPP_INTERNAL_ERROR, _("Unable to create printer-uri!"), 1); |
ef416fc2 | 614 | |
615 | return (-1); | |
616 | } | |
617 | } | |
618 | else | |
619 | strcpy(uri, "ipp://localhost/jobs"); | |
620 | ||
3d052e43 MS |
621 | if (!http) |
622 | if ((http = _cupsConnect()) == NULL) | |
623 | return (-1); | |
ef416fc2 | 624 | |
625 | /* | |
626 | * Build an IPP_GET_JOBS request, which requires the following | |
627 | * attributes: | |
628 | * | |
629 | * attributes-charset | |
630 | * attributes-natural-language | |
631 | * printer-uri | |
632 | * requesting-user-name | |
633 | * which-jobs | |
634 | * my-jobs | |
635 | * requested-attributes | |
636 | */ | |
637 | ||
2e4ff8af | 638 | request = ippNewRequest(IPP_GET_JOBS); |
ef416fc2 | 639 | |
640 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
641 | "printer-uri", NULL, uri); | |
642 | ||
643 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
644 | "requesting-user-name", NULL, cupsUser()); | |
645 | ||
646 | if (myjobs) | |
647 | ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1); | |
648 | ||
5a738aea | 649 | if (whichjobs == CUPS_WHICHJOBS_COMPLETED) |
ef416fc2 | 650 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
651 | "which-jobs", NULL, "completed"); | |
5a738aea | 652 | else if (whichjobs == CUPS_WHICHJOBS_ALL) |
ecdc0628 | 653 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, |
654 | "which-jobs", NULL, "all"); | |
ef416fc2 | 655 | |
656 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
657 | "requested-attributes", sizeof(attrs) / sizeof(attrs[0]), | |
658 | NULL, attrs); | |
659 | ||
660 | /* | |
661 | * Do the request and get back a response... | |
662 | */ | |
663 | ||
664 | n = 0; | |
665 | *jobs = NULL; | |
666 | ||
667 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
668 | { | |
3d052e43 | 669 | for (attr = response->attrs; attr; attr = attr->next) |
ef416fc2 | 670 | { |
671 | /* | |
672 | * Skip leading attributes until we hit a job... | |
673 | */ | |
674 | ||
3d052e43 | 675 | while (attr && attr->group_tag != IPP_TAG_JOB) |
ef416fc2 | 676 | attr = attr->next; |
677 | ||
3d052e43 | 678 | if (!attr) |
ef416fc2 | 679 | break; |
680 | ||
681 | /* | |
682 | * Pull the needed attributes from this job... | |
683 | */ | |
684 | ||
685 | id = 0; | |
686 | size = 0; | |
687 | priority = 50; | |
688 | state = IPP_JOB_PENDING; | |
689 | user = "unknown"; | |
690 | dest = NULL; | |
691 | format = "application/octet-stream"; | |
692 | title = "untitled"; | |
693 | creation_time = 0; | |
694 | completed_time = 0; | |
695 | processing_time = 0; | |
696 | ||
3d052e43 | 697 | while (attr && attr->group_tag == IPP_TAG_JOB) |
ef416fc2 | 698 | { |
3d052e43 | 699 | if (!strcmp(attr->name, "job-id") && |
ef416fc2 | 700 | attr->value_tag == IPP_TAG_INTEGER) |
701 | id = attr->values[0].integer; | |
3d052e43 | 702 | else if (!strcmp(attr->name, "job-state") && |
ef416fc2 | 703 | attr->value_tag == IPP_TAG_ENUM) |
704 | state = (ipp_jstate_t)attr->values[0].integer; | |
3d052e43 | 705 | else if (!strcmp(attr->name, "job-priority") && |
ef416fc2 | 706 | attr->value_tag == IPP_TAG_INTEGER) |
707 | priority = attr->values[0].integer; | |
3d052e43 | 708 | else if (!strcmp(attr->name, "job-k-octets") && |
ef416fc2 | 709 | attr->value_tag == IPP_TAG_INTEGER) |
710 | size = attr->values[0].integer; | |
3d052e43 | 711 | else if (!strcmp(attr->name, "time-at-completed") && |
ef416fc2 | 712 | attr->value_tag == IPP_TAG_INTEGER) |
713 | completed_time = attr->values[0].integer; | |
3d052e43 | 714 | else if (!strcmp(attr->name, "time-at-creation") && |
ef416fc2 | 715 | attr->value_tag == IPP_TAG_INTEGER) |
716 | creation_time = attr->values[0].integer; | |
3d052e43 | 717 | else if (!strcmp(attr->name, "time-at-processing") && |
ef416fc2 | 718 | attr->value_tag == IPP_TAG_INTEGER) |
719 | processing_time = attr->values[0].integer; | |
3d052e43 | 720 | else if (!strcmp(attr->name, "job-printer-uri") && |
ef416fc2 | 721 | attr->value_tag == IPP_TAG_URI) |
722 | { | |
723 | if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL) | |
724 | dest ++; | |
725 | } | |
3d052e43 | 726 | else if (!strcmp(attr->name, "job-originating-user-name") && |
ef416fc2 | 727 | attr->value_tag == IPP_TAG_NAME) |
728 | user = attr->values[0].string.text; | |
3d052e43 | 729 | else if (!strcmp(attr->name, "document-format") && |
ef416fc2 | 730 | attr->value_tag == IPP_TAG_MIMETYPE) |
731 | format = attr->values[0].string.text; | |
3d052e43 | 732 | else if (!strcmp(attr->name, "job-name") && |
ef416fc2 | 733 | (attr->value_tag == IPP_TAG_TEXT || |
734 | attr->value_tag == IPP_TAG_NAME)) | |
735 | title = attr->values[0].string.text; | |
736 | ||
737 | attr = attr->next; | |
738 | } | |
739 | ||
740 | /* | |
741 | * See if we have everything needed... | |
742 | */ | |
743 | ||
3d052e43 | 744 | if (!dest || !id) |
ef416fc2 | 745 | { |
3d052e43 | 746 | if (!attr) |
ef416fc2 | 747 | break; |
748 | else | |
749 | continue; | |
750 | } | |
751 | ||
752 | /* | |
753 | * Allocate memory for the job... | |
754 | */ | |
755 | ||
756 | if (n == 0) | |
757 | temp = malloc(sizeof(cups_job_t)); | |
758 | else | |
759 | temp = realloc(*jobs, sizeof(cups_job_t) * (n + 1)); | |
760 | ||
3d052e43 | 761 | if (!temp) |
ef416fc2 | 762 | { |
763 | /* | |
764 | * Ran out of memory! | |
765 | */ | |
766 | ||
749b1e90 | 767 | _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); |
3d052e43 | 768 | |
ef416fc2 | 769 | cupsFreeJobs(n, *jobs); |
770 | *jobs = NULL; | |
771 | ||
772 | ippDelete(response); | |
3d052e43 MS |
773 | |
774 | return (-1); | |
ef416fc2 | 775 | } |
776 | ||
777 | *jobs = temp; | |
778 | temp += n; | |
779 | n ++; | |
780 | ||
781 | /* | |
782 | * Copy the data over... | |
783 | */ | |
784 | ||
3d052e43 MS |
785 | temp->dest = _cupsStrAlloc(dest); |
786 | temp->user = _cupsStrAlloc(user); | |
787 | temp->format = _cupsStrAlloc(format); | |
788 | temp->title = _cupsStrAlloc(title); | |
ef416fc2 | 789 | temp->id = id; |
790 | temp->priority = priority; | |
791 | temp->state = state; | |
792 | temp->size = size; | |
793 | temp->completed_time = completed_time; | |
794 | temp->creation_time = creation_time; | |
795 | temp->processing_time = processing_time; | |
796 | ||
3d052e43 | 797 | if (!attr) |
ef416fc2 | 798 | break; |
799 | } | |
800 | ||
801 | ippDelete(response); | |
802 | } | |
803 | ||
804 | if (n == 0 && cg->last_error >= IPP_BAD_REQUEST) | |
805 | return (-1); | |
806 | else | |
807 | return (n); | |
808 | } | |
809 | ||
810 | ||
811 | /* | |
812 | * 'cupsGetPPD()' - Get the PPD file for a printer on the default server. | |
813 | * | |
5a738aea | 814 | * For classes, @code cupsGetPPD@ returns the PPD file for the first printer |
ef416fc2 | 815 | * in the class. |
568fa3fa MS |
816 | * |
817 | * The returned filename is stored in a static buffer and is overwritten with | |
818 | * each call to @code cupsGetPPD@ or @link cupsGetPPD2@. | |
ef416fc2 | 819 | */ |
820 | ||
821 | const char * /* O - Filename for PPD file */ | |
568fa3fa | 822 | cupsGetPPD(const char *name) /* I - Destination name */ |
ef416fc2 | 823 | { |
824 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
2e4ff8af MS |
825 | time_t modtime = 0; /* Modification time */ |
826 | ||
ef416fc2 | 827 | |
ef416fc2 | 828 | /* |
829 | * Return the PPD file... | |
830 | */ | |
831 | ||
2e4ff8af MS |
832 | cg->ppd_filename[0] = '\0'; |
833 | ||
3d052e43 | 834 | if (cupsGetPPD3(CUPS_HTTP_DEFAULT, name, &modtime, cg->ppd_filename, |
2e4ff8af MS |
835 | sizeof(cg->ppd_filename)) == HTTP_OK) |
836 | return (cg->ppd_filename); | |
837 | else | |
838 | return (NULL); | |
ef416fc2 | 839 | } |
840 | ||
841 | ||
842 | /* | |
843 | * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server. | |
844 | * | |
5a738aea | 845 | * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer |
ef416fc2 | 846 | * in the class. |
847 | * | |
568fa3fa MS |
848 | * The returned filename is stored in a static buffer and is overwritten with |
849 | * each call to @link cupsGetPPD@ or @code cupsGetPPD2@. | |
850 | * | |
ef416fc2 | 851 | * @since CUPS 1.1.21@ |
852 | */ | |
853 | ||
854 | const char * /* O - Filename for PPD file */ | |
568fa3fa MS |
855 | cupsGetPPD2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
856 | const char *name) /* I - Destination name */ | |
2e4ff8af MS |
857 | { |
858 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
859 | time_t modtime = 0; /* Modification time */ | |
860 | ||
861 | ||
862 | cg->ppd_filename[0] = '\0'; | |
863 | ||
864 | if (cupsGetPPD3(http, name, &modtime, cg->ppd_filename, | |
865 | sizeof(cg->ppd_filename)) == HTTP_OK) | |
866 | return (cg->ppd_filename); | |
867 | else | |
868 | return (NULL); | |
869 | } | |
870 | ||
871 | ||
872 | /* | |
873 | * 'cupsGetPPD3()' - Get the PPD file for a printer on the specified | |
874 | * server if it has changed. | |
875 | * | |
876 | * The "modtime" parameter contains the modification time of any | |
877 | * locally-cached content and is updated with the time from the PPD file on | |
878 | * the server. | |
879 | * | |
880 | * The "buffer" parameter contains the local PPD filename. If it contains | |
881 | * the empty string, a new temporary file is created, otherwise the existing | |
882 | * file will be overwritten as needed. | |
883 | * | |
5a738aea MS |
884 | * On success, @code HTTP_OK@ is returned for a new PPD file and |
885 | * @code HTTP_NOT_MODIFIED@ if the existing PPD file is up-to-date. Any other | |
886 | * status is an error. | |
887 | * | |
888 | * For classes, @code cupsGetPPD3@ returns the PPD file for the first printer | |
889 | * in the class. | |
568fa3fa MS |
890 | * |
891 | * @since CUPS 1.4@ | |
2e4ff8af MS |
892 | */ |
893 | ||
894 | http_status_t /* O - HTTP status */ | |
5a738aea | 895 | cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */ |
568fa3fa | 896 | const char *name, /* I - Destination name */ |
2e4ff8af MS |
897 | time_t *modtime, /* IO - Modification time */ |
898 | char *buffer, /* I - Filename buffer */ | |
899 | size_t bufsize) /* I - Size of filename buffer */ | |
ef416fc2 | 900 | { |
ef416fc2 | 901 | int http_port; /* Port number */ |
ed486911 | 902 | char http_hostname[HTTP_MAX_HOST]; |
903 | /* Hostname associated with connection */ | |
ef416fc2 | 904 | http_t *http2; /* Alternate HTTP connection */ |
ef416fc2 | 905 | int fd; /* PPD file */ |
fa73b229 | 906 | char localhost[HTTP_MAX_URI],/* Local hostname */ |
ef416fc2 | 907 | hostname[HTTP_MAX_URI], /* Hostname */ |
908 | resource[HTTP_MAX_URI]; /* Resource name */ | |
909 | int port; /* Port number */ | |
910 | http_status_t status; /* HTTP status from server */ | |
911 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
ef416fc2 | 912 | |
913 | ||
914 | /* | |
915 | * Range check input... | |
916 | */ | |
917 | ||
2e4ff8af MS |
918 | DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, " |
919 | "bufsize=%d)\n", http, name ? name : "(null)", modtime, | |
ae71f5de | 920 | modtime ? (int)*modtime : 0, buffer, (int)bufsize)); |
ef416fc2 | 921 | |
2e4ff8af MS |
922 | if (!name) |
923 | { | |
749b1e90 | 924 | _cupsSetError(IPP_INTERNAL_ERROR, _("No printer name!"), 1); |
2e4ff8af MS |
925 | return (HTTP_NOT_ACCEPTABLE); |
926 | } | |
927 | ||
928 | if (!modtime) | |
929 | { | |
749b1e90 | 930 | _cupsSetError(IPP_INTERNAL_ERROR, _("No modification time!"), 1); |
2e4ff8af MS |
931 | return (HTTP_NOT_ACCEPTABLE); |
932 | } | |
933 | ||
934 | if (!buffer || bufsize <= 1) | |
935 | { | |
749b1e90 | 936 | _cupsSetError(IPP_INTERNAL_ERROR, _("Bad filename buffer!"), 1); |
2e4ff8af | 937 | return (HTTP_NOT_ACCEPTABLE); |
ef416fc2 | 938 | } |
939 | ||
940 | /* | |
fa73b229 | 941 | * Try finding a printer URI for this printer... |
ef416fc2 | 942 | */ |
943 | ||
3d052e43 | 944 | if (!http) |
a603edef MS |
945 | if ((http = _cupsConnect()) == NULL) |
946 | return (HTTP_SERVICE_UNAVAILABLE); | |
3d052e43 | 947 | |
fa73b229 | 948 | if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port, |
949 | resource, sizeof(resource), 0)) | |
2e4ff8af | 950 | return (HTTP_NOT_FOUND); |
ef416fc2 | 951 | |
1ff0402e MS |
952 | DEBUG_printf(("cupsGetPPD3: Printer hostname=\"%s\", port=%d\n", hostname, |
953 | port)); | |
8ca02f3c | 954 | |
ef416fc2 | 955 | /* |
fa73b229 | 956 | * Remap local hostname to localhost... |
957 | */ | |
958 | ||
757d2cad | 959 | httpGetHostname(NULL, localhost, sizeof(localhost)); |
fa73b229 | 960 | |
1ff0402e | 961 | DEBUG_printf(("cupsGetPPD3: Local hostname=\"%s\"\n", localhost)); |
8ca02f3c | 962 | |
fa73b229 | 963 | if (!strcasecmp(localhost, hostname)) |
964 | strcpy(hostname, "localhost"); | |
965 | ||
966 | /* | |
ed486911 | 967 | * Get the hostname and port number we are connected to... |
ef416fc2 | 968 | */ |
969 | ||
ed486911 | 970 | httpGetHostname(http, http_hostname, sizeof(http_hostname)); |
1ff0402e | 971 | http_port = _httpAddrPort(http->hostaddr); |
ed486911 | 972 | |
1ff0402e MS |
973 | DEBUG_printf(("cupsGetPPD3: Connection hostname=\"%s\", port=%d\n", |
974 | http_hostname, http_port)); | |
8ca02f3c | 975 | |
ef416fc2 | 976 | /* |
977 | * Reconnect to the correct server as needed... | |
978 | */ | |
979 | ||
ed486911 | 980 | if (!strcasecmp(http_hostname, hostname) && port == http_port) |
ef416fc2 | 981 | http2 = http; |
982 | else if ((http2 = httpConnectEncrypt(hostname, port, | |
983 | cupsEncryption())) == NULL) | |
984 | { | |
1ff0402e | 985 | DEBUG_puts("cupsGetPPD3: Unable to connect to server!"); |
ef416fc2 | 986 | |
2e4ff8af | 987 | return (HTTP_SERVICE_UNAVAILABLE); |
ef416fc2 | 988 | } |
989 | ||
990 | /* | |
991 | * Get a temp file... | |
992 | */ | |
993 | ||
2e4ff8af MS |
994 | if (buffer[0]) |
995 | fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0600); | |
996 | else | |
997 | fd = cupsTempFd(buffer, bufsize); | |
998 | ||
999 | if (fd < 0) | |
ef416fc2 | 1000 | { |
1001 | /* | |
1002 | * Can't open file; close the server connection and return NULL... | |
1003 | */ | |
1004 | ||
749b1e90 | 1005 | _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); |
ef416fc2 | 1006 | |
1007 | if (http2 != http) | |
1008 | httpClose(http2); | |
1009 | ||
2e4ff8af | 1010 | return (HTTP_SERVER_ERROR); |
ef416fc2 | 1011 | } |
1012 | ||
1013 | /* | |
1014 | * And send a request to the HTTP server... | |
1015 | */ | |
1016 | ||
fa73b229 | 1017 | strlcat(resource, ".ppd", sizeof(resource)); |
ef416fc2 | 1018 | |
2e4ff8af MS |
1019 | if (*modtime > 0) |
1020 | httpSetField(http2, HTTP_FIELD_IF_MODIFIED_SINCE, | |
1021 | httpGetDateString(*modtime)); | |
1022 | ||
ef416fc2 | 1023 | status = cupsGetFd(http2, resource, fd); |
1024 | ||
1025 | close(fd); | |
1026 | ||
ef416fc2 | 1027 | /* |
1028 | * See if we actually got the file or an error... | |
1029 | */ | |
1030 | ||
2e4ff8af MS |
1031 | if (status == HTTP_OK) |
1032 | *modtime = httpGetDateTime(httpGetField(http2, HTTP_FIELD_DATE)); | |
1033 | else if (status != HTTP_NOT_MODIFIED) | |
ef416fc2 | 1034 | { |
1035 | switch (status) | |
1036 | { | |
1037 | case HTTP_NOT_FOUND : | |
749b1e90 | 1038 | _cupsSetError(IPP_NOT_FOUND, httpStatus(status), 0); |
ef416fc2 | 1039 | break; |
1040 | ||
1041 | case HTTP_UNAUTHORIZED : | |
749b1e90 | 1042 | _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status), 0); |
ef416fc2 | 1043 | break; |
1044 | ||
1045 | default : | |
1ff0402e MS |
1046 | DEBUG_printf(("cupsGetPPD3: HTTP error %d mapped to " |
1047 | "IPP_SERVICE_UNAVAILABLE!\n", status)); | |
749b1e90 | 1048 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status), 0); |
ef416fc2 | 1049 | break; |
1050 | } | |
1051 | ||
1052 | unlink(cg->ppd_filename); | |
ef416fc2 | 1053 | } |
1054 | ||
91c84a35 MS |
1055 | if (http2 != http) |
1056 | httpClose(http2); | |
1057 | ||
ef416fc2 | 1058 | /* |
1059 | * Return the PPD file... | |
1060 | */ | |
1061 | ||
2e4ff8af | 1062 | return (status); |
ef416fc2 | 1063 | } |
1064 | ||
1065 | ||
1066 | /* | |
1067 | * 'cupsGetPrinters()' - Get a list of printers from the default server. | |
1068 | * | |
5a738aea | 1069 | * This function is deprecated - use @link cupsGetDests@ instead. |
ef416fc2 | 1070 | * |
1071 | * @deprecated@ | |
1072 | */ | |
1073 | ||
1074 | int /* O - Number of printers */ | |
1075 | cupsGetPrinters(char ***printers) /* O - Printers */ | |
1076 | { | |
1077 | int n; /* Number of printers */ | |
1078 | ipp_t *request, /* IPP Request */ | |
1079 | *response; /* IPP Response */ | |
1080 | ipp_attribute_t *attr; /* Current attribute */ | |
ef416fc2 | 1081 | char **temp; /* Temporary pointer */ |
3d052e43 MS |
1082 | http_t *http; /* Connection to server */ |
1083 | ||
ef416fc2 | 1084 | |
3d052e43 MS |
1085 | /* |
1086 | * Range check input... | |
1087 | */ | |
ef416fc2 | 1088 | |
3d052e43 | 1089 | if (!printers) |
ef416fc2 | 1090 | { |
749b1e90 | 1091 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); |
ef416fc2 | 1092 | |
1093 | return (0); | |
1094 | } | |
1095 | ||
3d052e43 MS |
1096 | *printers = NULL; |
1097 | ||
ef416fc2 | 1098 | /* |
1099 | * Try to connect to the server... | |
1100 | */ | |
1101 | ||
3d052e43 | 1102 | if ((http = _cupsConnect()) == NULL) |
ef416fc2 | 1103 | return (0); |
ef416fc2 | 1104 | |
1105 | /* | |
1106 | * Build a CUPS_GET_PRINTERS request, which requires the following | |
1107 | * attributes: | |
1108 | * | |
1109 | * attributes-charset | |
1110 | * attributes-natural-language | |
1111 | * requested-attributes | |
1112 | */ | |
1113 | ||
2e4ff8af | 1114 | request = ippNewRequest(CUPS_GET_PRINTERS); |
ef416fc2 | 1115 | |
1116 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
1117 | "requested-attributes", NULL, "printer-name"); | |
1118 | ||
1119 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, | |
1120 | "printer-type", 0); | |
1121 | ||
1122 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, | |
1123 | "printer-type-mask", CUPS_PRINTER_CLASS); | |
1124 | ||
1125 | /* | |
1126 | * Do the request and get back a response... | |
1127 | */ | |
1128 | ||
3d052e43 | 1129 | n = 0; |
ef416fc2 | 1130 | |
3d052e43 | 1131 | if ((response = cupsDoRequest(http, request, "/")) != NULL) |
ef416fc2 | 1132 | { |
1133 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
1134 | if (attr->name != NULL && | |
1135 | strcasecmp(attr->name, "printer-name") == 0 && | |
1136 | attr->value_tag == IPP_TAG_NAME) | |
1137 | { | |
1138 | if (n == 0) | |
1139 | temp = malloc(sizeof(char *)); | |
1140 | else | |
1141 | temp = realloc(*printers, sizeof(char *) * (n + 1)); | |
1142 | ||
1143 | if (temp == NULL) | |
1144 | { | |
1145 | /* | |
1146 | * Ran out of memory! | |
1147 | */ | |
1148 | ||
1149 | while (n > 0) | |
1150 | { | |
1151 | n --; | |
1152 | free((*printers)[n]); | |
1153 | } | |
1154 | ||
1155 | free(*printers); | |
1156 | ippDelete(response); | |
1157 | return (0); | |
1158 | } | |
1159 | ||
1160 | *printers = temp; | |
1161 | temp[n] = strdup(attr->values[0].string.text); | |
1162 | n ++; | |
1163 | } | |
1164 | ||
1165 | ippDelete(response); | |
1166 | } | |
1167 | ||
1168 | return (n); | |
1169 | } | |
1170 | ||
1171 | ||
b94498cf | 1172 | /* |
1173 | * 'cupsGetServerPPD()' - Get an available PPD file from the server. | |
1174 | * | |
1175 | * This function returns the named PPD file from the server. The | |
5a738aea | 1176 | * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@ |
b94498cf | 1177 | * operation. |
1178 | * | |
1179 | * You must remove (unlink) the PPD file when you are finished with | |
1180 | * it. The PPD filename is stored in a static location that will be | |
5a738aea MS |
1181 | * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@, |
1182 | * or @link cupsGetServerPPD@. | |
b94498cf | 1183 | * |
1184 | * @since CUPS 1.3@ | |
1185 | */ | |
1186 | ||
5a738aea | 1187 | char * /* O - Name of PPD file or @code NULL@ on error */ |
568fa3fa | 1188 | cupsGetServerPPD(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
b94498cf | 1189 | const char *name) /* I - Name of PPD file ("ppd-name") */ |
1190 | { | |
1191 | int fd; /* PPD file descriptor */ | |
1192 | ipp_t *request; /* IPP request */ | |
1193 | _cups_globals_t *cg = _cupsGlobals(); | |
1194 | /* Pointer to library globals */ | |
1195 | ||
1196 | ||
1197 | /* | |
1198 | * Range check input... | |
1199 | */ | |
1200 | ||
3d052e43 | 1201 | if (!name) |
b94498cf | 1202 | { |
749b1e90 | 1203 | _cupsSetError(IPP_INTERNAL_ERROR, _("No PPD name!"), 1); |
b94498cf | 1204 | |
1205 | return (NULL); | |
1206 | } | |
1207 | ||
3d052e43 MS |
1208 | if (!http) |
1209 | if ((http = _cupsConnect()) == NULL) | |
1210 | return (NULL); | |
1211 | ||
b94498cf | 1212 | /* |
1213 | * Get a temp file... | |
1214 | */ | |
1215 | ||
1216 | if ((fd = cupsTempFd(cg->ppd_filename, sizeof(cg->ppd_filename))) < 0) | |
1217 | { | |
1218 | /* | |
1219 | * Can't open file; close the server connection and return NULL... | |
1220 | */ | |
1221 | ||
749b1e90 | 1222 | _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); |
b94498cf | 1223 | |
1224 | return (NULL); | |
1225 | } | |
1226 | ||
1227 | /* | |
1228 | * Get the PPD file... | |
1229 | */ | |
1230 | ||
1231 | request = ippNewRequest(CUPS_GET_PPD); | |
1232 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name", NULL, | |
1233 | name); | |
1234 | ||
1235 | ippDelete(cupsDoIORequest(http, request, "/", -1, fd)); | |
1236 | ||
1237 | close(fd); | |
1238 | ||
1239 | if (cupsLastError() != IPP_OK) | |
1240 | { | |
1241 | unlink(cg->ppd_filename); | |
1242 | return (NULL); | |
1243 | } | |
1244 | else | |
1245 | return (cg->ppd_filename); | |
1246 | } | |
1247 | ||
1248 | ||
ef416fc2 | 1249 | /* |
1250 | * 'cupsLastError()' - Return the last IPP status code. | |
1251 | */ | |
1252 | ||
1253 | ipp_status_t /* O - IPP status code from last request */ | |
1254 | cupsLastError(void) | |
1255 | { | |
1256 | return (_cupsGlobals()->last_error); | |
1257 | } | |
1258 | ||
1259 | ||
1260 | /* | |
1261 | * 'cupsLastErrorString()' - Return the last IPP status-message. | |
1262 | * | |
1263 | * @since CUPS 1.2@ | |
1264 | */ | |
1265 | ||
1266 | const char * /* O - status-message text from last request */ | |
1267 | cupsLastErrorString(void) | |
1268 | { | |
1269 | return (_cupsGlobals()->last_status_message); | |
1270 | } | |
1271 | ||
1272 | ||
1273 | /* | |
1274 | * 'cupsPrintFile()' - Print a file to a printer or class on the default server. | |
1275 | */ | |
1276 | ||
5a738aea | 1277 | int /* O - Job ID or 0 on error */ |
568fa3fa | 1278 | cupsPrintFile(const char *name, /* I - Destination name */ |
ef416fc2 | 1279 | const char *filename, /* I - File to print */ |
1280 | const char *title, /* I - Title of job */ | |
1281 | int num_options,/* I - Number of options */ | |
1282 | cups_option_t *options) /* I - Options */ | |
1283 | { | |
1284 | DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", " | |
1285 | "title=\"%s\", num_options=%d, options=%p)\n", | |
1286 | name, filename, title, num_options, options)); | |
1287 | ||
3d052e43 MS |
1288 | return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title, |
1289 | num_options, options)); | |
ef416fc2 | 1290 | } |
1291 | ||
1292 | ||
1293 | /* | |
5a738aea MS |
1294 | * 'cupsPrintFile2()' - Print a file to a printer or class on the specified |
1295 | * server. | |
ef416fc2 | 1296 | * |
1297 | * @since CUPS 1.1.21@ | |
1298 | */ | |
1299 | ||
5a738aea | 1300 | int /* O - Job ID or 0 on error */ |
3d052e43 | 1301 | cupsPrintFile2( |
568fa3fa MS |
1302 | http_t *http, /* I - Connection to server */ |
1303 | const char *name, /* I - Destination name */ | |
3d052e43 MS |
1304 | const char *filename, /* I - File to print */ |
1305 | const char *title, /* I - Title of job */ | |
1306 | int num_options, /* I - Number of options */ | |
1307 | cups_option_t *options) /* I - Options */ | |
ef416fc2 | 1308 | { |
1309 | DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", " | |
1310 | "title=\"%s\", num_options=%d, options=%p)\n", | |
1311 | http, name, filename, title, num_options, options)); | |
1312 | ||
3d052e43 MS |
1313 | return (cupsPrintFiles2(http, name, 1, &filename, title, num_options, |
1314 | options)); | |
ef416fc2 | 1315 | } |
1316 | ||
1317 | ||
1318 | /* | |
fa73b229 | 1319 | * 'cupsPrintFiles()' - Print one or more files to a printer or class on the |
1320 | * default server. | |
ef416fc2 | 1321 | */ |
1322 | ||
5a738aea | 1323 | int /* O - Job ID or 0 on error */ |
3d052e43 | 1324 | cupsPrintFiles( |
568fa3fa | 1325 | const char *name, /* I - Destination name */ |
3d052e43 MS |
1326 | int num_files, /* I - Number of files */ |
1327 | const char **files, /* I - File(s) to print */ | |
1328 | const char *title, /* I - Title of job */ | |
1329 | int num_options, /* I - Number of options */ | |
1330 | cups_option_t *options) /* I - Options */ | |
ef416fc2 | 1331 | { |
ef416fc2 | 1332 | DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, " |
1333 | "files=%p, title=\"%s\", num_options=%d, options=%p)\n", | |
1334 | name, num_files, files, title, num_options, options)); | |
1335 | ||
1336 | ||
ef416fc2 | 1337 | /* |
1338 | * Print the file(s)... | |
1339 | */ | |
1340 | ||
3d052e43 | 1341 | return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title, |
ef416fc2 | 1342 | num_options, options)); |
1343 | } | |
1344 | ||
1345 | ||
ef416fc2 | 1346 | /* |
fa73b229 | 1347 | * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the |
1348 | * specified server. | |
ef416fc2 | 1349 | * |
1350 | * @since CUPS 1.1.21@ | |
1351 | */ | |
1352 | ||
5a738aea | 1353 | int /* O - Job ID or 0 on error */ |
3d052e43 | 1354 | cupsPrintFiles2( |
568fa3fa MS |
1355 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
1356 | const char *name, /* I - Destination name */ | |
3d052e43 MS |
1357 | int num_files, /* I - Number of files */ |
1358 | const char **files, /* I - File(s) to print */ | |
1359 | const char *title, /* I - Title of job */ | |
1360 | int num_options, /* I - Number of options */ | |
1361 | cups_option_t *options) /* I - Options */ | |
ef416fc2 | 1362 | { |
1363 | int i; /* Looping var */ | |
3d052e43 MS |
1364 | int job_id; /* New job ID */ |
1365 | const char *docname; /* Basename of current filename */ | |
1366 | const char *format; /* Document format */ | |
1367 | cups_file_t *fp; /* Current file */ | |
1368 | char buffer[8192]; /* Copy buffer */ | |
1369 | ssize_t bytes; /* Bytes in buffer */ | |
1370 | http_status_t status; /* Status of write */ | |
ef416fc2 | 1371 | |
1372 | ||
3d052e43 | 1373 | DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, " |
ef416fc2 | 1374 | "files=%p, title=\"%s\", num_options=%d, options=%p)\n", |
1375 | http, name, num_files, files, title, num_options, options)); | |
1376 | ||
1377 | /* | |
1378 | * Range check input... | |
1379 | */ | |
1380 | ||
3d052e43 | 1381 | if (!name || num_files < 1 || !files) |
ef416fc2 | 1382 | { |
749b1e90 | 1383 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); |
ef416fc2 | 1384 | |
1385 | return (0); | |
1386 | } | |
1387 | ||
1388 | /* | |
3d052e43 | 1389 | * Create the print job... |
ef416fc2 | 1390 | */ |
1391 | ||
3d052e43 | 1392 | if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0) |
ef416fc2 | 1393 | return (0); |
ef416fc2 | 1394 | |
1395 | /* | |
3d052e43 | 1396 | * Send each of the files... |
ef416fc2 | 1397 | */ |
1398 | ||
3d052e43 MS |
1399 | if (cupsGetOption("raw", num_options, options)) |
1400 | format = CUPS_FORMAT_RAW; | |
1401 | else if ((format = cupsGetOption("document-format", num_options, | |
1402 | options)) == NULL) | |
1403 | format = CUPS_FORMAT_AUTO; | |
ef416fc2 | 1404 | |
3d052e43 MS |
1405 | for (i = 0; i < num_files; i ++) |
1406 | { | |
1407 | /* | |
1408 | * Start the next file... | |
1409 | */ | |
ef416fc2 | 1410 | |
3d052e43 MS |
1411 | if ((docname = strrchr(files[i], '/')) != NULL) |
1412 | docname ++; | |
1413 | else | |
1414 | docname = files[i]; | |
ef416fc2 | 1415 | |
3d052e43 MS |
1416 | if ((fp = cupsFileOpen(files[i], "rb")) == NULL) |
1417 | { | |
1418 | /* | |
1419 | * Unable to open print file, cancel the job and return... | |
1420 | */ | |
ef416fc2 | 1421 | |
5a738aea | 1422 | cupsCancelJob2(http, name, job_id, 0); |
3d052e43 MS |
1423 | return (0); |
1424 | } | |
ef416fc2 | 1425 | |
3d052e43 MS |
1426 | status = cupsStartDocument(http, name, job_id, docname, format, |
1427 | i == (num_files - 1)); | |
ef416fc2 | 1428 | |
3d052e43 MS |
1429 | while (status == HTTP_CONTINUE && |
1430 | (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0) | |
1431 | status = cupsWriteRequestData(http, buffer, bytes); | |
ef416fc2 | 1432 | |
3d052e43 | 1433 | cupsFileClose(fp); |
ef416fc2 | 1434 | |
3d052e43 | 1435 | if (status != HTTP_CONTINUE || cupsFinishDocument(http, name) != IPP_OK) |
ef416fc2 | 1436 | { |
1437 | /* | |
3d052e43 | 1438 | * Unable to queue, cancel the job and return... |
ef416fc2 | 1439 | */ |
1440 | ||
5a738aea | 1441 | cupsCancelJob2(http, name, job_id, 0); |
3d052e43 MS |
1442 | return (0); |
1443 | } | |
1444 | } | |
ef416fc2 | 1445 | |
3d052e43 MS |
1446 | return (job_id); |
1447 | } | |
ef416fc2 | 1448 | |
ef416fc2 | 1449 | |
3d052e43 MS |
1450 | /* |
1451 | * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob(). | |
1452 | * | |
5a738aea MS |
1453 | * Use @link cupsWriteRequestData@ to write data for the document and |
1454 | * @link cupsFinishDocument@ to finish the document and get the submission status. | |
3d052e43 | 1455 | * |
5a738aea MS |
1456 | * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@, |
1457 | * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and | |
1458 | * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although | |
1459 | * any supported MIME type string can be supplied. | |
3d052e43 MS |
1460 | * |
1461 | * @since CUPS 1.4@ | |
1462 | */ | |
ef416fc2 | 1463 | |
3d052e43 MS |
1464 | http_status_t /* O - HTTP status of request */ |
1465 | cupsStartDocument( | |
568fa3fa MS |
1466 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ |
1467 | const char *name, /* I - Destination name */ | |
5a738aea | 1468 | int job_id, /* I - Job ID from @link cupsCreateJob@ */ |
3d052e43 | 1469 | const char *docname, /* I - Name of document */ |
5a738aea | 1470 | const char *format, /* I - MIME type or @code CUPS_FORMAT_foo@ */ |
3d052e43 MS |
1471 | int last_document) /* I - 1 for last document in job, 0 otherwise */ |
1472 | { | |
1473 | char resource[1024], /* Resource for destinatio */ | |
1474 | printer_uri[1024]; /* Printer URI */ | |
1475 | ipp_t *request; /* Send-Document request */ | |
1476 | http_status_t status; /* HTTP status */ | |
ef416fc2 | 1477 | |
bd7854cb | 1478 | |
3d052e43 MS |
1479 | /* |
1480 | * Create a Send-Document request... | |
1481 | */ | |
bd7854cb | 1482 | |
3d052e43 MS |
1483 | if ((request = ippNewRequest(IPP_SEND_DOCUMENT)) == NULL) |
1484 | { | |
749b1e90 | 1485 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0); |
3d052e43 MS |
1486 | return (0); |
1487 | } | |
bd7854cb | 1488 | |
3d052e43 MS |
1489 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp", |
1490 | NULL, "localhost", ippPort(), "/printers/%s", name); | |
1491 | snprintf(resource, sizeof(resource), "/printers/%s", name); | |
ef416fc2 | 1492 | |
3d052e43 MS |
1493 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", |
1494 | NULL, printer_uri); | |
1495 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id); | |
1496 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
1497 | NULL, cupsUser()); | |
1498 | if (docname) | |
1499 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name", | |
1500 | NULL, docname); | |
1501 | if (format) | |
1502 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, | |
1503 | "document-format", NULL, format); | |
1504 | ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", last_document); | |
ef416fc2 | 1505 | |
3d052e43 MS |
1506 | /* |
1507 | * Send and delete the request, then return the status... | |
1508 | */ | |
ef416fc2 | 1509 | |
3d052e43 | 1510 | status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE); |
ef416fc2 | 1511 | |
3d052e43 | 1512 | ippDelete(request); |
ef416fc2 | 1513 | |
3d052e43 | 1514 | return (status); |
ef416fc2 | 1515 | } |
1516 | ||
1517 | ||
1518 | /* | |
3d052e43 | 1519 | * '_cupsConnect()' - Get the default server connection... |
ef416fc2 | 1520 | */ |
1521 | ||
3d052e43 MS |
1522 | http_t * /* O - HTTP connection */ |
1523 | _cupsConnect(void) | |
ef416fc2 | 1524 | { |
3d052e43 | 1525 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ |
ef416fc2 | 1526 | |
ef416fc2 | 1527 | |
1528 | /* | |
3d052e43 | 1529 | * See if we are connected to the same server... |
ef416fc2 | 1530 | */ |
1531 | ||
3d052e43 MS |
1532 | if (cg->http) |
1533 | { | |
3d052e43 MS |
1534 | /* |
1535 | * Compare the connection hostname, port, and encryption settings to | |
1536 | * the cached defaults; these were initialized the first time we | |
1537 | * connected... | |
1538 | */ | |
1539 | ||
1ff0402e MS |
1540 | if (strcmp(cg->http->hostname, cg->server) || |
1541 | cg->ipp_port != _httpAddrPort(cg->http->hostaddr) || | |
3d052e43 MS |
1542 | (cg->http->encryption != cg->encryption && |
1543 | cg->http->encryption == HTTP_ENCRYPT_NEVER)) | |
1544 | { | |
1545 | /* | |
1546 | * Need to close the current connection because something has changed... | |
1547 | */ | |
ef416fc2 | 1548 | |
3d052e43 MS |
1549 | httpClose(cg->http); |
1550 | cg->http = NULL; | |
1551 | } | |
ef416fc2 | 1552 | } |
1553 | ||
3d052e43 MS |
1554 | /* |
1555 | * (Re)connect as needed... | |
1556 | */ | |
ef416fc2 | 1557 | |
3d052e43 | 1558 | if (!cg->http) |
ef416fc2 | 1559 | { |
3d052e43 MS |
1560 | if ((cg->http = httpConnectEncrypt(cupsServer(), ippPort(), |
1561 | cupsEncryption())) == NULL) | |
749b1e90 MS |
1562 | { |
1563 | if (errno) | |
1564 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
1565 | else | |
1566 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, | |
1567 | _("Unable to connect to host."), 1); | |
1568 | } | |
3d052e43 | 1569 | } |
ef416fc2 | 1570 | |
3d052e43 MS |
1571 | /* |
1572 | * Return the cached connection... | |
1573 | */ | |
ef416fc2 | 1574 | |
3d052e43 | 1575 | return (cg->http); |
ef416fc2 | 1576 | } |
1577 | ||
1578 | ||
fa73b229 | 1579 | /* |
58dc1933 MS |
1580 | * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the |
1581 | * first printer in a class. | |
fa73b229 | 1582 | */ |
1583 | ||
1584 | static int /* O - 1 on success, 0 on failure */ | |
1585 | cups_get_printer_uri( | |
568fa3fa | 1586 | http_t *http, /* I - Connection to server */ |
fa73b229 | 1587 | const char *name, /* I - Name of printer or class */ |
1588 | char *host, /* I - Hostname buffer */ | |
1589 | int hostsize, /* I - Size of hostname buffer */ | |
1590 | int *port, /* O - Port number */ | |
1591 | char *resource, /* I - Resource buffer */ | |
1592 | int resourcesize, /* I - Size of resource buffer */ | |
1593 | int depth) /* I - Depth of query */ | |
1594 | { | |
1595 | int i; /* Looping var */ | |
1596 | int http_port; /* Port number */ | |
1597 | http_t *http2; /* Alternate HTTP connection */ | |
1598 | ipp_t *request, /* IPP request */ | |
1599 | *response; /* IPP response */ | |
1600 | ipp_attribute_t *attr; /* Current attribute */ | |
1601 | char uri[HTTP_MAX_URI], /* printer-uri attribute */ | |
1602 | scheme[HTTP_MAX_URI], /* Scheme name */ | |
1603 | username[HTTP_MAX_URI], /* Username:password */ | |
ed486911 | 1604 | classname[255], /* Temporary class name */ |
1605 | http_hostname[HTTP_MAX_HOST]; | |
1606 | /* Hostname associated with connection */ | |
fa73b229 | 1607 | static const char * const requested_attrs[] = |
1608 | { /* Requested attributes */ | |
58dc1933 | 1609 | "member-uris", |
fa73b229 | 1610 | "printer-uri-supported", |
58dc1933 | 1611 | "printer-type" |
fa73b229 | 1612 | }; |
1613 | ||
1614 | ||
1615 | DEBUG_printf(("cups_get_printer_uri(http=%p, name=\"%s\", host=%p, " | |
1616 | "hostsize=%d, resource=%p, resourcesize=%d, depth=%d)\n", | |
1617 | http, name ? name : "(null)", host, hostsize, | |
1618 | resource, resourcesize, depth)); | |
1619 | ||
1620 | /* | |
1621 | * Setup the printer URI... | |
1622 | */ | |
1623 | ||
a4d04587 | 1624 | if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
1625 | "localhost", 0, "/printers/%s", name) != HTTP_URI_OK) | |
fa73b229 | 1626 | { |
749b1e90 | 1627 | _cupsSetError(IPP_INTERNAL_ERROR, _("Unable to create printer-uri!"), 1); |
fa73b229 | 1628 | |
1629 | *host = '\0'; | |
1630 | *resource = '\0'; | |
1631 | ||
1632 | return (0); | |
1633 | } | |
1634 | ||
1635 | DEBUG_printf(("cups_get_printer_uri: printer-uri=\"%s\"\n", uri)); | |
1636 | ||
1637 | /* | |
ed486911 | 1638 | * Get the hostname and port number we are connected to... |
fa73b229 | 1639 | */ |
1640 | ||
ed486911 | 1641 | httpGetHostname(http, http_hostname, sizeof(http_hostname)); |
1ff0402e | 1642 | http_port = _httpAddrPort(http->hostaddr); |
fa73b229 | 1643 | |
1644 | /* | |
1645 | * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following | |
1646 | * attributes: | |
1647 | * | |
1648 | * attributes-charset | |
1649 | * attributes-natural-language | |
1650 | * printer-uri | |
1651 | * requested-attributes | |
1652 | */ | |
1653 | ||
1654 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); | |
1655 | ||
1656 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
1657 | NULL, uri); | |
1658 | ||
1659 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
1660 | "requested-attributes", | |
1661 | sizeof(requested_attrs) / sizeof(requested_attrs[0]), | |
1662 | NULL, requested_attrs); | |
1663 | ||
1664 | /* | |
1665 | * Do the request and get back a response... | |
1666 | */ | |
1667 | ||
1668 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
1669 | { | |
1670 | if ((attr = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL) | |
1671 | { | |
1672 | /* | |
1673 | * Get the first actual printer name in the class... | |
1674 | */ | |
1675 | ||
1676 | for (i = 0; i < attr->num_values; i ++) | |
1677 | { | |
a4d04587 | 1678 | httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text, |
1679 | scheme, sizeof(scheme), username, sizeof(username), | |
1680 | host, hostsize, port, resource, resourcesize); | |
fa73b229 | 1681 | if (!strncmp(resource, "/printers/", 10)) |
1682 | { | |
1683 | /* | |
1684 | * Found a printer! | |
1685 | */ | |
1686 | ||
1687 | ippDelete(response); | |
1688 | ||
1689 | return (1); | |
1690 | } | |
1691 | } | |
1692 | ||
1693 | /* | |
1694 | * No printers in this class - try recursively looking for a printer, | |
1695 | * but not more than 3 levels deep... | |
1696 | */ | |
1697 | ||
1698 | if (depth < 3) | |
1699 | { | |
1700 | for (i = 0; i < attr->num_values; i ++) | |
1701 | { | |
a4d04587 | 1702 | httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text, |
1703 | scheme, sizeof(scheme), username, sizeof(username), | |
1704 | host, hostsize, port, resource, resourcesize); | |
fa73b229 | 1705 | if (!strncmp(resource, "/classes/", 9)) |
1706 | { | |
1707 | /* | |
1708 | * Found a class! Connect to the right server... | |
1709 | */ | |
1710 | ||
ed486911 | 1711 | if (!strcasecmp(http_hostname, host) && *port == http_port) |
fa73b229 | 1712 | http2 = http; |
1713 | else if ((http2 = httpConnectEncrypt(host, *port, | |
1714 | cupsEncryption())) == NULL) | |
1715 | { | |
1716 | DEBUG_puts("Unable to connect to server!"); | |
1717 | ||
1718 | continue; | |
1719 | } | |
1720 | ||
1721 | /* | |
1722 | * Look up printers on that server... | |
1723 | */ | |
1724 | ||
1725 | strlcpy(classname, resource + 9, sizeof(classname)); | |
1726 | ||
1727 | cups_get_printer_uri(http2, classname, host, hostsize, port, | |
1728 | resource, resourcesize, depth + 1); | |
1729 | ||
1730 | /* | |
1731 | * Close the connection as needed... | |
1732 | */ | |
1733 | ||
1734 | if (http2 != http) | |
1735 | httpClose(http2); | |
1736 | ||
1737 | if (*host) | |
1738 | return (1); | |
1739 | } | |
1740 | } | |
1741 | } | |
1742 | } | |
1743 | else if ((attr = ippFindAttribute(response, "printer-uri-supported", | |
1744 | IPP_TAG_URI)) != NULL) | |
1745 | { | |
58dc1933 MS |
1746 | httpSeparateURI(HTTP_URI_CODING_ALL, |
1747 | _httpResolveURI(attr->values[0].string.text, uri, | |
1748 | sizeof(uri), 0), | |
a4d04587 | 1749 | scheme, sizeof(scheme), username, sizeof(username), |
1750 | host, hostsize, port, resource, resourcesize); | |
fa73b229 | 1751 | ippDelete(response); |
1752 | ||
dd1abb6b MS |
1753 | if (!strncmp(resource, "/classes/", 9)) |
1754 | { | |
749b1e90 MS |
1755 | _cupsSetError(IPP_INTERNAL_ERROR, |
1756 | _("No printer-uri found for class!"), 1); | |
dd1abb6b MS |
1757 | |
1758 | *host = '\0'; | |
1759 | *resource = '\0'; | |
1760 | ||
1761 | return (0); | |
1762 | } | |
1763 | ||
fa73b229 | 1764 | return (1); |
1765 | } | |
1766 | ||
1767 | ippDelete(response); | |
1768 | } | |
1769 | ||
b86bc4cf | 1770 | if (cupsLastError() != IPP_NOT_FOUND) |
749b1e90 | 1771 | _cupsSetError(IPP_INTERNAL_ERROR, _("No printer-uri found!"), 1); |
b423cd4c | 1772 | |
fa73b229 | 1773 | *host = '\0'; |
1774 | *resource = '\0'; | |
1775 | ||
1776 | return (0); | |
1777 | } | |
1778 | ||
1779 | ||
ef416fc2 | 1780 | /* |
b19ccc9e | 1781 | * End of "$Id: util.c 7850 2008-08-20 00:07:25Z mike $". |
ef416fc2 | 1782 | */ |