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