]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * "$Id: util.c 7850 2008-08-20 00:07:25Z mike $" | |
3 | * | |
4 | * Printing utilities for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 2007-2008 by Apple Inc. | |
7 | * Copyright 1997-2006 by Easy Software Products. | |
8 | * | |
9 | * These coded instructions, statements, and computer programs are the | |
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/". | |
14 | * | |
15 | * This file is subject to the Apple OS-Developed Software exception. | |
16 | * | |
17 | * Contents: | |
18 | * | |
19 | * cupsCancelJob() - Cancel a print job on the default server. | |
20 | * cupsCancelJob2() - Cancel or purge a print job. | |
21 | * cupsCreateJob() - Create an empty job. | |
22 | * cupsFinishDocument() - Finish sending a document. | |
23 | * cupsFreeJobs() - Free memory used by job data. | |
24 | * cupsGetClasses() - Get a list of printer classes from the default | |
25 | * server. | |
26 | * cupsGetDefault() - Get the default printer or class for the default | |
27 | * server. | |
28 | * cupsGetDefault2() - Get the default printer or class for the specified | |
29 | * server. | |
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. | |
34 | * cupsGetPPD2() - Get the PPD file for a printer from the specified | |
35 | * server. | |
36 | * cupsGetPPD3() - Get the PPD file for a printer on the specified | |
37 | * server if it has changed. | |
38 | * cupsGetPrinters() - Get a list of printers from the default server. | |
39 | * cupsGetServerPPD() - Get an available PPD file from the server. | |
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. | |
50 | * cupsStartDocument() - Add a document to a job created with | |
51 | * cupsCreateJob(). | |
52 | * _cupsConnect() - Get the default server connection... | |
53 | * cups_get_printer_uri() - Get the printer-uri-supported attribute for the | |
54 | * first printer in a class. | |
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 | ||
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); | |
82 | ||
83 | ||
84 | /* | |
85 | * 'cupsCancelJob()' - Cancel a print job on the default server. | |
86 | * | |
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 | |
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 */ | |
96 | int job_id) /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */ | |
97 | { | |
98 | return (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0) | |
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 | * | |
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 | |
113 | * the cause of any failure. | |
114 | * | |
115 | * @since CUPS 1.4@ | |
116 | */ | |
117 | ||
118 | ipp_status_t /* O - IPP status */ | |
119 | cupsCancelJob2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
120 | const char *name, /* I - Name of printer or class */ | |
121 | int job_id, /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */ | |
122 | int purge) /* I - 1 to purge, 0 to cancel */ | |
123 | { | |
124 | char uri[HTTP_MAX_URI]; /* Job/printer URI */ | |
125 | ipp_t *request; /* IPP request */ | |
126 | ||
127 | ||
128 | /* | |
129 | * Range check input... | |
130 | */ | |
131 | ||
132 | if (job_id < -1 || (!name && job_id == 0)) | |
133 | { | |
134 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); | |
135 | return (0); | |
136 | } | |
137 | ||
138 | /* | |
139 | * Connect to the default server as needed... | |
140 | */ | |
141 | ||
142 | if (!http) | |
143 | if ((http = _cupsConnect()) == NULL) | |
144 | return (IPP_SERVICE_UNAVAILABLE); | |
145 | ||
146 | /* | |
147 | * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following | |
148 | * attributes: | |
149 | * | |
150 | * attributes-charset | |
151 | * attributes-natural-language | |
152 | * job-uri or printer-uri + job-id | |
153 | * requesting-user-name | |
154 | * [purge-job] or [purge-jobs] | |
155 | */ | |
156 | ||
157 | request = ippNewRequest(job_id < 0 ? IPP_PURGE_JOBS : IPP_CANCEL_JOB); | |
158 | ||
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 | } | |
175 | ||
176 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
177 | NULL, cupsUser()); | |
178 | ||
179 | if (purge && job_id >= 0) | |
180 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-job", 1); | |
181 | else if (!purge && job_id < 0) | |
182 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", 0); | |
183 | ||
184 | /* | |
185 | * Do the request... | |
186 | */ | |
187 | ||
188 | ippDelete(cupsDoRequest(http, request, "/jobs/")); | |
189 | ||
190 | return (cupsLastError()); | |
191 | } | |
192 | ||
193 | ||
194 | /* | |
195 | * 'cupsCreateJob()' - Create an empty job for streaming. | |
196 | * | |
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. | |
202 | * | |
203 | * @since CUPS 1.4@ | |
204 | */ | |
205 | ||
206 | int /* O - Job ID or 0 on error */ | |
207 | cupsCreateJob( | |
208 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
209 | const char *name, /* I - Destination name */ | |
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 | { | |
232 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); | |
233 | return (0); | |
234 | } | |
235 | ||
236 | /* | |
237 | * Build a Create-Job request... | |
238 | */ | |
239 | ||
240 | if ((request = ippNewRequest(IPP_CREATE_JOB)) == NULL) | |
241 | { | |
242 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0); | |
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 | * | |
281 | * The document must have been started using @link cupsStartDocument@. | |
282 | * | |
283 | * @since CUPS 1.4@ | |
284 | */ | |
285 | ||
286 | ipp_status_t /* O - Status of document submission */ | |
287 | cupsFinishDocument(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
288 | const char *name) /* I - Destination name */ | |
289 | { | |
290 | char resource[1024]; /* Printer resource */ | |
291 | ||
292 | ||
293 | snprintf(resource, sizeof(resource), "/printers/%s", name); | |
294 | ||
295 | ippDelete(cupsGetResponse(http, resource)); | |
296 | ||
297 | return (cupsLastError()); | |
298 | } | |
299 | ||
300 | ||
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 | { | |
309 | int i; /* Looping var */ | |
310 | cups_job_t *job; /* Current job */ | |
311 | ||
312 | ||
313 | if (num_jobs <= 0 || !jobs) | |
314 | return; | |
315 | ||
316 | for (i = num_jobs, job = jobs; i > 0; i --, job ++) | |
317 | { | |
318 | _cupsStrFree(job->dest); | |
319 | _cupsStrFree(job->user); | |
320 | _cupsStrFree(job->format); | |
321 | _cupsStrFree(job->title); | |
322 | } | |
323 | ||
324 | free(jobs); | |
325 | } | |
326 | ||
327 | ||
328 | /* | |
329 | * 'cupsGetClasses()' - Get a list of printer classes from the default server. | |
330 | * | |
331 | * This function is deprecated - use @link cupsGetDests@ instead. | |
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 */ | |
343 | char **temp; /* Temporary pointer */ | |
344 | http_t *http; /* Connection to server */ | |
345 | ||
346 | ||
347 | if (!classes) | |
348 | { | |
349 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); | |
350 | ||
351 | return (0); | |
352 | } | |
353 | ||
354 | *classes = NULL; | |
355 | ||
356 | if ((http = _cupsConnect()) == NULL) | |
357 | return (0); | |
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 | ||
368 | request = ippNewRequest(CUPS_GET_CLASSES); | |
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 | ||
377 | n = 0; | |
378 | ||
379 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
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. | |
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. | |
429 | */ | |
430 | ||
431 | const char * /* O - Default printer or @code NULL@ */ | |
432 | cupsGetDefault(void) | |
433 | { | |
434 | /* | |
435 | * Return the default printer... | |
436 | */ | |
437 | ||
438 | return (cupsGetDefault2(CUPS_HTTP_DEFAULT)); | |
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. | |
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. | |
451 | * | |
452 | * @since CUPS 1.1.21/Mac OS X 10.4@ | |
453 | */ | |
454 | ||
455 | const char * /* O - Default printer or @code NULL@ */ | |
456 | cupsGetDefault2(http_t *http) /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
457 | { | |
458 | ipp_t *request, /* IPP Request */ | |
459 | *response; /* IPP Response */ | |
460 | ipp_attribute_t *attr; /* Current attribute */ | |
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 | /* | |
478 | * Connect to the server as needed... | |
479 | */ | |
480 | ||
481 | if (!http) | |
482 | if ((http = _cupsConnect()) == NULL) | |
483 | return (NULL); | |
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 | ||
493 | request = ippNewRequest(CUPS_GET_DEFAULT); | |
494 | ||
495 | /* | |
496 | * Do the request and get back a response... | |
497 | */ | |
498 | ||
499 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
500 | { | |
501 | if ((attr = ippFindAttribute(response, "printer-name", | |
502 | IPP_TAG_NAME)) != NULL) | |
503 | { | |
504 | strlcpy(cg->def_printer, attr->values[0].string.text, | |
505 | sizeof(cg->def_printer)); | |
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. | |
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. | |
524 | */ | |
525 | ||
526 | int /* O - Number of jobs */ | |
527 | cupsGetJobs(cups_job_t **jobs, /* O - Job data */ | |
528 | const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */ | |
529 | int myjobs, /* I - 0 = all users, 1 = mine */ | |
530 | int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */ | |
531 | { | |
532 | /* | |
533 | * Return the jobs... | |
534 | */ | |
535 | ||
536 | return (cupsGetJobs2(CUPS_HTTP_DEFAULT, jobs, name, myjobs, whichjobs)); | |
537 | } | |
538 | ||
539 | ||
540 | ||
541 | /* | |
542 | * 'cupsGetJobs2()' - Get the jobs from the specified server. | |
543 | * | |
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 | * | |
549 | * @since CUPS 1.1.21/Mac OS X 10.4@ | |
550 | */ | |
551 | ||
552 | int /* O - Number of jobs */ | |
553 | cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
554 | cups_job_t **jobs, /* O - Job data */ | |
555 | const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */ | |
556 | int myjobs, /* I - 0 = all users, 1 = mine */ | |
557 | int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */ | |
558 | { | |
559 | int n; /* Number of jobs */ | |
560 | ipp_t *request, /* IPP Request */ | |
561 | *response; /* IPP Response */ | |
562 | ipp_attribute_t *attr; /* Current attribute */ | |
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 | ||
597 | if (!jobs) | |
598 | { | |
599 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); | |
600 | ||
601 | return (-1); | |
602 | } | |
603 | ||
604 | /* | |
605 | * Get the right URI... | |
606 | */ | |
607 | ||
608 | if (name) | |
609 | { | |
610 | if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
611 | "localhost", 0, "/printers/%s", name) != HTTP_URI_OK) | |
612 | { | |
613 | _cupsSetError(IPP_INTERNAL_ERROR, _("Unable to create printer-uri!"), 1); | |
614 | ||
615 | return (-1); | |
616 | } | |
617 | } | |
618 | else | |
619 | strcpy(uri, "ipp://localhost/jobs"); | |
620 | ||
621 | if (!http) | |
622 | if ((http = _cupsConnect()) == NULL) | |
623 | return (-1); | |
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 | ||
638 | request = ippNewRequest(IPP_GET_JOBS); | |
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 | ||
649 | if (whichjobs == CUPS_WHICHJOBS_COMPLETED) | |
650 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
651 | "which-jobs", NULL, "completed"); | |
652 | else if (whichjobs == CUPS_WHICHJOBS_ALL) | |
653 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
654 | "which-jobs", NULL, "all"); | |
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 | { | |
669 | for (attr = response->attrs; attr; attr = attr->next) | |
670 | { | |
671 | /* | |
672 | * Skip leading attributes until we hit a job... | |
673 | */ | |
674 | ||
675 | while (attr && attr->group_tag != IPP_TAG_JOB) | |
676 | attr = attr->next; | |
677 | ||
678 | if (!attr) | |
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 | ||
697 | while (attr && attr->group_tag == IPP_TAG_JOB) | |
698 | { | |
699 | if (!strcmp(attr->name, "job-id") && | |
700 | attr->value_tag == IPP_TAG_INTEGER) | |
701 | id = attr->values[0].integer; | |
702 | else if (!strcmp(attr->name, "job-state") && | |
703 | attr->value_tag == IPP_TAG_ENUM) | |
704 | state = (ipp_jstate_t)attr->values[0].integer; | |
705 | else if (!strcmp(attr->name, "job-priority") && | |
706 | attr->value_tag == IPP_TAG_INTEGER) | |
707 | priority = attr->values[0].integer; | |
708 | else if (!strcmp(attr->name, "job-k-octets") && | |
709 | attr->value_tag == IPP_TAG_INTEGER) | |
710 | size = attr->values[0].integer; | |
711 | else if (!strcmp(attr->name, "time-at-completed") && | |
712 | attr->value_tag == IPP_TAG_INTEGER) | |
713 | completed_time = attr->values[0].integer; | |
714 | else if (!strcmp(attr->name, "time-at-creation") && | |
715 | attr->value_tag == IPP_TAG_INTEGER) | |
716 | creation_time = attr->values[0].integer; | |
717 | else if (!strcmp(attr->name, "time-at-processing") && | |
718 | attr->value_tag == IPP_TAG_INTEGER) | |
719 | processing_time = attr->values[0].integer; | |
720 | else if (!strcmp(attr->name, "job-printer-uri") && | |
721 | attr->value_tag == IPP_TAG_URI) | |
722 | { | |
723 | if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL) | |
724 | dest ++; | |
725 | } | |
726 | else if (!strcmp(attr->name, "job-originating-user-name") && | |
727 | attr->value_tag == IPP_TAG_NAME) | |
728 | user = attr->values[0].string.text; | |
729 | else if (!strcmp(attr->name, "document-format") && | |
730 | attr->value_tag == IPP_TAG_MIMETYPE) | |
731 | format = attr->values[0].string.text; | |
732 | else if (!strcmp(attr->name, "job-name") && | |
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 | ||
744 | if (!dest || !id) | |
745 | { | |
746 | if (!attr) | |
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 | ||
761 | if (!temp) | |
762 | { | |
763 | /* | |
764 | * Ran out of memory! | |
765 | */ | |
766 | ||
767 | _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); | |
768 | ||
769 | cupsFreeJobs(n, *jobs); | |
770 | *jobs = NULL; | |
771 | ||
772 | ippDelete(response); | |
773 | ||
774 | return (-1); | |
775 | } | |
776 | ||
777 | *jobs = temp; | |
778 | temp += n; | |
779 | n ++; | |
780 | ||
781 | /* | |
782 | * Copy the data over... | |
783 | */ | |
784 | ||
785 | temp->dest = _cupsStrAlloc(dest); | |
786 | temp->user = _cupsStrAlloc(user); | |
787 | temp->format = _cupsStrAlloc(format); | |
788 | temp->title = _cupsStrAlloc(title); | |
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 | ||
797 | if (!attr) | |
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 | * | |
814 | * For classes, @code cupsGetPPD@ returns the PPD file for the first printer | |
815 | * in the class. | |
816 | * | |
817 | * The returned filename is stored in a static buffer and is overwritten with | |
818 | * each call to @code cupsGetPPD@ or @link cupsGetPPD2@. | |
819 | */ | |
820 | ||
821 | const char * /* O - Filename for PPD file */ | |
822 | cupsGetPPD(const char *name) /* I - Destination name */ | |
823 | { | |
824 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
825 | time_t modtime = 0; /* Modification time */ | |
826 | ||
827 | ||
828 | /* | |
829 | * Return the PPD file... | |
830 | */ | |
831 | ||
832 | cg->ppd_filename[0] = '\0'; | |
833 | ||
834 | if (cupsGetPPD3(CUPS_HTTP_DEFAULT, name, &modtime, cg->ppd_filename, | |
835 | sizeof(cg->ppd_filename)) == HTTP_OK) | |
836 | return (cg->ppd_filename); | |
837 | else | |
838 | return (NULL); | |
839 | } | |
840 | ||
841 | ||
842 | /* | |
843 | * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server. | |
844 | * | |
845 | * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer | |
846 | * in the class. | |
847 | * | |
848 | * The returned filename is stored in a static buffer and is overwritten with | |
849 | * each call to @link cupsGetPPD@ or @code cupsGetPPD2@. | |
850 | * | |
851 | * @since CUPS 1.1.21/Mac OS X 10.4@ | |
852 | */ | |
853 | ||
854 | const char * /* O - Filename for PPD file */ | |
855 | cupsGetPPD2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
856 | const char *name) /* I - Destination name */ | |
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 | * | |
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. | |
890 | * | |
891 | * @since CUPS 1.4@ | |
892 | */ | |
893 | ||
894 | http_status_t /* O - HTTP status */ | |
895 | cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */ | |
896 | const char *name, /* I - Destination name */ | |
897 | time_t *modtime, /* IO - Modification time */ | |
898 | char *buffer, /* I - Filename buffer */ | |
899 | size_t bufsize) /* I - Size of filename buffer */ | |
900 | { | |
901 | int http_port; /* Port number */ | |
902 | char http_hostname[HTTP_MAX_HOST]; | |
903 | /* Hostname associated with connection */ | |
904 | http_t *http2; /* Alternate HTTP connection */ | |
905 | int fd; /* PPD file */ | |
906 | char localhost[HTTP_MAX_URI],/* Local hostname */ | |
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 */ | |
912 | ||
913 | ||
914 | /* | |
915 | * Range check input... | |
916 | */ | |
917 | ||
918 | DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, " | |
919 | "bufsize=%d)\n", http, name ? name : "(null)", modtime, | |
920 | modtime ? (int)*modtime : 0, buffer, (int)bufsize)); | |
921 | ||
922 | if (!name) | |
923 | { | |
924 | _cupsSetError(IPP_INTERNAL_ERROR, _("No printer name!"), 1); | |
925 | return (HTTP_NOT_ACCEPTABLE); | |
926 | } | |
927 | ||
928 | if (!modtime) | |
929 | { | |
930 | _cupsSetError(IPP_INTERNAL_ERROR, _("No modification time!"), 1); | |
931 | return (HTTP_NOT_ACCEPTABLE); | |
932 | } | |
933 | ||
934 | if (!buffer || bufsize <= 1) | |
935 | { | |
936 | _cupsSetError(IPP_INTERNAL_ERROR, _("Bad filename buffer!"), 1); | |
937 | return (HTTP_NOT_ACCEPTABLE); | |
938 | } | |
939 | ||
940 | #ifndef WIN32 | |
941 | /* | |
942 | * See if the PPD file is available locally... | |
943 | */ | |
944 | ||
945 | if (!cg->servername[0]) | |
946 | cupsServer(); | |
947 | ||
948 | if (!strcasecmp(cg->servername, "localhost")) | |
949 | { | |
950 | char ppdname[1024]; /* PPD filename */ | |
951 | struct stat ppdinfo; /* PPD file information */ | |
952 | ||
953 | ||
954 | snprintf(ppdname, sizeof(ppdname), "%s/%s.ppd", cg->cups_serverroot, name); | |
955 | if (!stat(ppdname, &ppdinfo)) | |
956 | { | |
957 | /* | |
958 | * OK, the file exists, use it! | |
959 | */ | |
960 | ||
961 | if (buffer[0]) | |
962 | { | |
963 | unlink(buffer); | |
964 | ||
965 | if (symlink(ppdname, buffer) && errno != EEXIST) | |
966 | { | |
967 | _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); | |
968 | ||
969 | return (HTTP_SERVER_ERROR); | |
970 | } | |
971 | } | |
972 | else | |
973 | { | |
974 | int tries; /* Number of tries */ | |
975 | const char *tmpdir; /* TMPDIR environment variable */ | |
976 | struct timeval curtime; /* Current time */ | |
977 | ||
978 | /* | |
979 | * Previously we put root temporary files in the default CUPS temporary | |
980 | * directory under /var/spool/cups. However, since the scheduler cleans | |
981 | * out temporary files there and runs independently of the user apps, we | |
982 | * don't want to use it unless specifically told to by cupsd. | |
983 | */ | |
984 | ||
985 | if ((tmpdir = getenv("TMPDIR")) == NULL) | |
986 | # ifdef __APPLE__ | |
987 | tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */ | |
988 | # else | |
989 | tmpdir = "/tmp"; | |
990 | # endif /* __APPLE__ */ | |
991 | ||
992 | /* | |
993 | * Make the temporary name using the specified directory... | |
994 | */ | |
995 | ||
996 | tries = 0; | |
997 | ||
998 | do | |
999 | { | |
1000 | /* | |
1001 | * Get the current time of day... | |
1002 | */ | |
1003 | ||
1004 | gettimeofday(&curtime, NULL); | |
1005 | ||
1006 | /* | |
1007 | * Format a string using the hex time values... | |
1008 | */ | |
1009 | ||
1010 | snprintf(buffer, bufsize, "%s/%08lx%05lx", tmpdir, | |
1011 | (unsigned long)curtime.tv_sec, | |
1012 | (unsigned long)curtime.tv_usec); | |
1013 | ||
1014 | /* | |
1015 | * Try to make a symlink... | |
1016 | */ | |
1017 | ||
1018 | if (!symlink(ppdname, buffer)) | |
1019 | break; | |
1020 | ||
1021 | tries ++; | |
1022 | } | |
1023 | while (tries < 1000); | |
1024 | ||
1025 | if (tries >= 1000) | |
1026 | { | |
1027 | _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); | |
1028 | ||
1029 | return (HTTP_SERVER_ERROR); | |
1030 | } | |
1031 | } | |
1032 | ||
1033 | if (*modtime <= ppdinfo.st_mtime) | |
1034 | return (HTTP_NOT_MODIFIED); | |
1035 | else | |
1036 | { | |
1037 | *modtime = ppdinfo.st_mtime; | |
1038 | return (HTTP_OK); | |
1039 | } | |
1040 | } | |
1041 | } | |
1042 | #endif /* !WIN32 */ | |
1043 | ||
1044 | /* | |
1045 | * Try finding a printer URI for this printer... | |
1046 | */ | |
1047 | ||
1048 | if (!http) | |
1049 | if ((http = _cupsConnect()) == NULL) | |
1050 | return (HTTP_SERVICE_UNAVAILABLE); | |
1051 | ||
1052 | if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port, | |
1053 | resource, sizeof(resource), 0)) | |
1054 | return (HTTP_NOT_FOUND); | |
1055 | ||
1056 | DEBUG_printf(("cupsGetPPD3: Printer hostname=\"%s\", port=%d\n", hostname, | |
1057 | port)); | |
1058 | ||
1059 | /* | |
1060 | * Remap local hostname to localhost... | |
1061 | */ | |
1062 | ||
1063 | httpGetHostname(NULL, localhost, sizeof(localhost)); | |
1064 | ||
1065 | DEBUG_printf(("cupsGetPPD3: Local hostname=\"%s\"\n", localhost)); | |
1066 | ||
1067 | if (!strcasecmp(localhost, hostname)) | |
1068 | strcpy(hostname, "localhost"); | |
1069 | ||
1070 | /* | |
1071 | * Get the hostname and port number we are connected to... | |
1072 | */ | |
1073 | ||
1074 | httpGetHostname(http, http_hostname, sizeof(http_hostname)); | |
1075 | http_port = _httpAddrPort(http->hostaddr); | |
1076 | ||
1077 | DEBUG_printf(("cupsGetPPD3: Connection hostname=\"%s\", port=%d\n", | |
1078 | http_hostname, http_port)); | |
1079 | ||
1080 | /* | |
1081 | * Reconnect to the correct server as needed... | |
1082 | */ | |
1083 | ||
1084 | if (!strcasecmp(http_hostname, hostname) && port == http_port) | |
1085 | http2 = http; | |
1086 | else if ((http2 = httpConnectEncrypt(hostname, port, | |
1087 | cupsEncryption())) == NULL) | |
1088 | { | |
1089 | DEBUG_puts("cupsGetPPD3: Unable to connect to server!"); | |
1090 | ||
1091 | return (HTTP_SERVICE_UNAVAILABLE); | |
1092 | } | |
1093 | ||
1094 | /* | |
1095 | * Get a temp file... | |
1096 | */ | |
1097 | ||
1098 | if (buffer[0]) | |
1099 | fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0600); | |
1100 | else | |
1101 | fd = cupsTempFd(buffer, bufsize); | |
1102 | ||
1103 | if (fd < 0) | |
1104 | { | |
1105 | /* | |
1106 | * Can't open file; close the server connection and return NULL... | |
1107 | */ | |
1108 | ||
1109 | _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); | |
1110 | ||
1111 | if (http2 != http) | |
1112 | httpClose(http2); | |
1113 | ||
1114 | return (HTTP_SERVER_ERROR); | |
1115 | } | |
1116 | ||
1117 | /* | |
1118 | * And send a request to the HTTP server... | |
1119 | */ | |
1120 | ||
1121 | strlcat(resource, ".ppd", sizeof(resource)); | |
1122 | ||
1123 | if (*modtime > 0) | |
1124 | httpSetField(http2, HTTP_FIELD_IF_MODIFIED_SINCE, | |
1125 | httpGetDateString(*modtime)); | |
1126 | ||
1127 | status = cupsGetFd(http2, resource, fd); | |
1128 | ||
1129 | close(fd); | |
1130 | ||
1131 | /* | |
1132 | * See if we actually got the file or an error... | |
1133 | */ | |
1134 | ||
1135 | if (status == HTTP_OK) | |
1136 | *modtime = httpGetDateTime(httpGetField(http2, HTTP_FIELD_DATE)); | |
1137 | else if (status != HTTP_NOT_MODIFIED) | |
1138 | { | |
1139 | switch (status) | |
1140 | { | |
1141 | case HTTP_NOT_FOUND : | |
1142 | _cupsSetError(IPP_NOT_FOUND, httpStatus(status), 0); | |
1143 | break; | |
1144 | ||
1145 | case HTTP_UNAUTHORIZED : | |
1146 | _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status), 0); | |
1147 | break; | |
1148 | ||
1149 | default : | |
1150 | DEBUG_printf(("cupsGetPPD3: HTTP error %d mapped to " | |
1151 | "IPP_SERVICE_UNAVAILABLE!\n", status)); | |
1152 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status), 0); | |
1153 | break; | |
1154 | } | |
1155 | ||
1156 | unlink(cg->ppd_filename); | |
1157 | } | |
1158 | ||
1159 | if (http2 != http) | |
1160 | httpClose(http2); | |
1161 | ||
1162 | /* | |
1163 | * Return the PPD file... | |
1164 | */ | |
1165 | ||
1166 | return (status); | |
1167 | } | |
1168 | ||
1169 | ||
1170 | /* | |
1171 | * 'cupsGetPrinters()' - Get a list of printers from the default server. | |
1172 | * | |
1173 | * This function is deprecated - use @link cupsGetDests@ instead. | |
1174 | * | |
1175 | * @deprecated@ | |
1176 | */ | |
1177 | ||
1178 | int /* O - Number of printers */ | |
1179 | cupsGetPrinters(char ***printers) /* O - Printers */ | |
1180 | { | |
1181 | int n; /* Number of printers */ | |
1182 | ipp_t *request, /* IPP Request */ | |
1183 | *response; /* IPP Response */ | |
1184 | ipp_attribute_t *attr; /* Current attribute */ | |
1185 | char **temp; /* Temporary pointer */ | |
1186 | http_t *http; /* Connection to server */ | |
1187 | ||
1188 | ||
1189 | /* | |
1190 | * Range check input... | |
1191 | */ | |
1192 | ||
1193 | if (!printers) | |
1194 | { | |
1195 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); | |
1196 | ||
1197 | return (0); | |
1198 | } | |
1199 | ||
1200 | *printers = NULL; | |
1201 | ||
1202 | /* | |
1203 | * Try to connect to the server... | |
1204 | */ | |
1205 | ||
1206 | if ((http = _cupsConnect()) == NULL) | |
1207 | return (0); | |
1208 | ||
1209 | /* | |
1210 | * Build a CUPS_GET_PRINTERS request, which requires the following | |
1211 | * attributes: | |
1212 | * | |
1213 | * attributes-charset | |
1214 | * attributes-natural-language | |
1215 | * requested-attributes | |
1216 | */ | |
1217 | ||
1218 | request = ippNewRequest(CUPS_GET_PRINTERS); | |
1219 | ||
1220 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, | |
1221 | "requested-attributes", NULL, "printer-name"); | |
1222 | ||
1223 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, | |
1224 | "printer-type", 0); | |
1225 | ||
1226 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM, | |
1227 | "printer-type-mask", CUPS_PRINTER_CLASS); | |
1228 | ||
1229 | /* | |
1230 | * Do the request and get back a response... | |
1231 | */ | |
1232 | ||
1233 | n = 0; | |
1234 | ||
1235 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
1236 | { | |
1237 | for (attr = response->attrs; attr != NULL; attr = attr->next) | |
1238 | if (attr->name != NULL && | |
1239 | strcasecmp(attr->name, "printer-name") == 0 && | |
1240 | attr->value_tag == IPP_TAG_NAME) | |
1241 | { | |
1242 | if (n == 0) | |
1243 | temp = malloc(sizeof(char *)); | |
1244 | else | |
1245 | temp = realloc(*printers, sizeof(char *) * (n + 1)); | |
1246 | ||
1247 | if (temp == NULL) | |
1248 | { | |
1249 | /* | |
1250 | * Ran out of memory! | |
1251 | */ | |
1252 | ||
1253 | while (n > 0) | |
1254 | { | |
1255 | n --; | |
1256 | free((*printers)[n]); | |
1257 | } | |
1258 | ||
1259 | free(*printers); | |
1260 | ippDelete(response); | |
1261 | return (0); | |
1262 | } | |
1263 | ||
1264 | *printers = temp; | |
1265 | temp[n] = strdup(attr->values[0].string.text); | |
1266 | n ++; | |
1267 | } | |
1268 | ||
1269 | ippDelete(response); | |
1270 | } | |
1271 | ||
1272 | return (n); | |
1273 | } | |
1274 | ||
1275 | ||
1276 | /* | |
1277 | * 'cupsGetServerPPD()' - Get an available PPD file from the server. | |
1278 | * | |
1279 | * This function returns the named PPD file from the server. The | |
1280 | * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@ | |
1281 | * operation. | |
1282 | * | |
1283 | * You must remove (unlink) the PPD file when you are finished with | |
1284 | * it. The PPD filename is stored in a static location that will be | |
1285 | * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@, | |
1286 | * or @link cupsGetServerPPD@. | |
1287 | * | |
1288 | * @since CUPS 1.3/Mac OS X 10.5@ | |
1289 | */ | |
1290 | ||
1291 | char * /* O - Name of PPD file or @code NULL@ on error */ | |
1292 | cupsGetServerPPD(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
1293 | const char *name) /* I - Name of PPD file ("ppd-name") */ | |
1294 | { | |
1295 | int fd; /* PPD file descriptor */ | |
1296 | ipp_t *request; /* IPP request */ | |
1297 | _cups_globals_t *cg = _cupsGlobals(); | |
1298 | /* Pointer to library globals */ | |
1299 | ||
1300 | ||
1301 | /* | |
1302 | * Range check input... | |
1303 | */ | |
1304 | ||
1305 | if (!name) | |
1306 | { | |
1307 | _cupsSetError(IPP_INTERNAL_ERROR, _("No PPD name!"), 1); | |
1308 | ||
1309 | return (NULL); | |
1310 | } | |
1311 | ||
1312 | if (!http) | |
1313 | if ((http = _cupsConnect()) == NULL) | |
1314 | return (NULL); | |
1315 | ||
1316 | /* | |
1317 | * Get a temp file... | |
1318 | */ | |
1319 | ||
1320 | if ((fd = cupsTempFd(cg->ppd_filename, sizeof(cg->ppd_filename))) < 0) | |
1321 | { | |
1322 | /* | |
1323 | * Can't open file; close the server connection and return NULL... | |
1324 | */ | |
1325 | ||
1326 | _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); | |
1327 | ||
1328 | return (NULL); | |
1329 | } | |
1330 | ||
1331 | /* | |
1332 | * Get the PPD file... | |
1333 | */ | |
1334 | ||
1335 | request = ippNewRequest(CUPS_GET_PPD); | |
1336 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name", NULL, | |
1337 | name); | |
1338 | ||
1339 | ippDelete(cupsDoIORequest(http, request, "/", -1, fd)); | |
1340 | ||
1341 | close(fd); | |
1342 | ||
1343 | if (cupsLastError() != IPP_OK) | |
1344 | { | |
1345 | unlink(cg->ppd_filename); | |
1346 | return (NULL); | |
1347 | } | |
1348 | else | |
1349 | return (cg->ppd_filename); | |
1350 | } | |
1351 | ||
1352 | ||
1353 | /* | |
1354 | * 'cupsLastError()' - Return the last IPP status code. | |
1355 | */ | |
1356 | ||
1357 | ipp_status_t /* O - IPP status code from last request */ | |
1358 | cupsLastError(void) | |
1359 | { | |
1360 | return (_cupsGlobals()->last_error); | |
1361 | } | |
1362 | ||
1363 | ||
1364 | /* | |
1365 | * 'cupsLastErrorString()' - Return the last IPP status-message. | |
1366 | * | |
1367 | * @since CUPS 1.2/Mac OS X 10.5@ | |
1368 | */ | |
1369 | ||
1370 | const char * /* O - status-message text from last request */ | |
1371 | cupsLastErrorString(void) | |
1372 | { | |
1373 | return (_cupsGlobals()->last_status_message); | |
1374 | } | |
1375 | ||
1376 | ||
1377 | /* | |
1378 | * 'cupsPrintFile()' - Print a file to a printer or class on the default server. | |
1379 | */ | |
1380 | ||
1381 | int /* O - Job ID or 0 on error */ | |
1382 | cupsPrintFile(const char *name, /* I - Destination name */ | |
1383 | const char *filename, /* I - File to print */ | |
1384 | const char *title, /* I - Title of job */ | |
1385 | int num_options,/* I - Number of options */ | |
1386 | cups_option_t *options) /* I - Options */ | |
1387 | { | |
1388 | DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", " | |
1389 | "title=\"%s\", num_options=%d, options=%p)\n", | |
1390 | name, filename, title, num_options, options)); | |
1391 | ||
1392 | return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title, | |
1393 | num_options, options)); | |
1394 | } | |
1395 | ||
1396 | ||
1397 | /* | |
1398 | * 'cupsPrintFile2()' - Print a file to a printer or class on the specified | |
1399 | * server. | |
1400 | * | |
1401 | * @since CUPS 1.1.21/Mac OS X 10.4@ | |
1402 | */ | |
1403 | ||
1404 | int /* O - Job ID or 0 on error */ | |
1405 | cupsPrintFile2( | |
1406 | http_t *http, /* I - Connection to server */ | |
1407 | const char *name, /* I - Destination name */ | |
1408 | const char *filename, /* I - File to print */ | |
1409 | const char *title, /* I - Title of job */ | |
1410 | int num_options, /* I - Number of options */ | |
1411 | cups_option_t *options) /* I - Options */ | |
1412 | { | |
1413 | DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", " | |
1414 | "title=\"%s\", num_options=%d, options=%p)\n", | |
1415 | http, name, filename, title, num_options, options)); | |
1416 | ||
1417 | return (cupsPrintFiles2(http, name, 1, &filename, title, num_options, | |
1418 | options)); | |
1419 | } | |
1420 | ||
1421 | ||
1422 | /* | |
1423 | * 'cupsPrintFiles()' - Print one or more files to a printer or class on the | |
1424 | * default server. | |
1425 | */ | |
1426 | ||
1427 | int /* O - Job ID or 0 on error */ | |
1428 | cupsPrintFiles( | |
1429 | const char *name, /* I - Destination name */ | |
1430 | int num_files, /* I - Number of files */ | |
1431 | const char **files, /* I - File(s) to print */ | |
1432 | const char *title, /* I - Title of job */ | |
1433 | int num_options, /* I - Number of options */ | |
1434 | cups_option_t *options) /* I - Options */ | |
1435 | { | |
1436 | DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, " | |
1437 | "files=%p, title=\"%s\", num_options=%d, options=%p)\n", | |
1438 | name, num_files, files, title, num_options, options)); | |
1439 | ||
1440 | ||
1441 | /* | |
1442 | * Print the file(s)... | |
1443 | */ | |
1444 | ||
1445 | return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title, | |
1446 | num_options, options)); | |
1447 | } | |
1448 | ||
1449 | ||
1450 | /* | |
1451 | * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the | |
1452 | * specified server. | |
1453 | * | |
1454 | * @since CUPS 1.1.21/Mac OS X 10.4@ | |
1455 | */ | |
1456 | ||
1457 | int /* O - Job ID or 0 on error */ | |
1458 | cupsPrintFiles2( | |
1459 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
1460 | const char *name, /* I - Destination name */ | |
1461 | int num_files, /* I - Number of files */ | |
1462 | const char **files, /* I - File(s) to print */ | |
1463 | const char *title, /* I - Title of job */ | |
1464 | int num_options, /* I - Number of options */ | |
1465 | cups_option_t *options) /* I - Options */ | |
1466 | { | |
1467 | int i; /* Looping var */ | |
1468 | int job_id; /* New job ID */ | |
1469 | const char *docname; /* Basename of current filename */ | |
1470 | const char *format; /* Document format */ | |
1471 | cups_file_t *fp; /* Current file */ | |
1472 | char buffer[8192]; /* Copy buffer */ | |
1473 | ssize_t bytes; /* Bytes in buffer */ | |
1474 | http_status_t status; /* Status of write */ | |
1475 | ||
1476 | ||
1477 | DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, " | |
1478 | "files=%p, title=\"%s\", num_options=%d, options=%p)\n", | |
1479 | http, name, num_files, files, title, num_options, options)); | |
1480 | ||
1481 | /* | |
1482 | * Range check input... | |
1483 | */ | |
1484 | ||
1485 | if (!name || num_files < 1 || !files) | |
1486 | { | |
1487 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0); | |
1488 | ||
1489 | return (0); | |
1490 | } | |
1491 | ||
1492 | /* | |
1493 | * Create the print job... | |
1494 | */ | |
1495 | ||
1496 | if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0) | |
1497 | return (0); | |
1498 | ||
1499 | /* | |
1500 | * Send each of the files... | |
1501 | */ | |
1502 | ||
1503 | if (cupsGetOption("raw", num_options, options)) | |
1504 | format = CUPS_FORMAT_RAW; | |
1505 | else if ((format = cupsGetOption("document-format", num_options, | |
1506 | options)) == NULL) | |
1507 | format = CUPS_FORMAT_AUTO; | |
1508 | ||
1509 | for (i = 0; i < num_files; i ++) | |
1510 | { | |
1511 | /* | |
1512 | * Start the next file... | |
1513 | */ | |
1514 | ||
1515 | if ((docname = strrchr(files[i], '/')) != NULL) | |
1516 | docname ++; | |
1517 | else | |
1518 | docname = files[i]; | |
1519 | ||
1520 | if ((fp = cupsFileOpen(files[i], "rb")) == NULL) | |
1521 | { | |
1522 | /* | |
1523 | * Unable to open print file, cancel the job and return... | |
1524 | */ | |
1525 | ||
1526 | cupsCancelJob2(http, name, job_id, 0); | |
1527 | return (0); | |
1528 | } | |
1529 | ||
1530 | status = cupsStartDocument(http, name, job_id, docname, format, | |
1531 | i == (num_files - 1)); | |
1532 | ||
1533 | while (status == HTTP_CONTINUE && | |
1534 | (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0) | |
1535 | status = cupsWriteRequestData(http, buffer, bytes); | |
1536 | ||
1537 | cupsFileClose(fp); | |
1538 | ||
1539 | if (status != HTTP_CONTINUE || cupsFinishDocument(http, name) != IPP_OK) | |
1540 | { | |
1541 | /* | |
1542 | * Unable to queue, cancel the job and return... | |
1543 | */ | |
1544 | ||
1545 | cupsCancelJob2(http, name, job_id, 0); | |
1546 | return (0); | |
1547 | } | |
1548 | } | |
1549 | ||
1550 | return (job_id); | |
1551 | } | |
1552 | ||
1553 | ||
1554 | /* | |
1555 | * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob(). | |
1556 | * | |
1557 | * Use @link cupsWriteRequestData@ to write data for the document and | |
1558 | * @link cupsFinishDocument@ to finish the document and get the submission status. | |
1559 | * | |
1560 | * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@, | |
1561 | * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and | |
1562 | * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although | |
1563 | * any supported MIME type string can be supplied. | |
1564 | * | |
1565 | * @since CUPS 1.4@ | |
1566 | */ | |
1567 | ||
1568 | http_status_t /* O - HTTP status of request */ | |
1569 | cupsStartDocument( | |
1570 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
1571 | const char *name, /* I - Destination name */ | |
1572 | int job_id, /* I - Job ID from @link cupsCreateJob@ */ | |
1573 | const char *docname, /* I - Name of document */ | |
1574 | const char *format, /* I - MIME type or @code CUPS_FORMAT_foo@ */ | |
1575 | int last_document) /* I - 1 for last document in job, 0 otherwise */ | |
1576 | { | |
1577 | char resource[1024], /* Resource for destinatio */ | |
1578 | printer_uri[1024]; /* Printer URI */ | |
1579 | ipp_t *request; /* Send-Document request */ | |
1580 | http_status_t status; /* HTTP status */ | |
1581 | ||
1582 | ||
1583 | /* | |
1584 | * Create a Send-Document request... | |
1585 | */ | |
1586 | ||
1587 | if ((request = ippNewRequest(IPP_SEND_DOCUMENT)) == NULL) | |
1588 | { | |
1589 | _cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0); | |
1590 | return (0); | |
1591 | } | |
1592 | ||
1593 | httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp", | |
1594 | NULL, "localhost", ippPort(), "/printers/%s", name); | |
1595 | snprintf(resource, sizeof(resource), "/printers/%s", name); | |
1596 | ||
1597 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
1598 | NULL, printer_uri); | |
1599 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id); | |
1600 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", | |
1601 | NULL, cupsUser()); | |
1602 | if (docname) | |
1603 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name", | |
1604 | NULL, docname); | |
1605 | if (format) | |
1606 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, | |
1607 | "document-format", NULL, format); | |
1608 | ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", last_document); | |
1609 | ||
1610 | /* | |
1611 | * Send and delete the request, then return the status... | |
1612 | */ | |
1613 | ||
1614 | status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE); | |
1615 | ||
1616 | ippDelete(request); | |
1617 | ||
1618 | return (status); | |
1619 | } | |
1620 | ||
1621 | ||
1622 | /* | |
1623 | * '_cupsConnect()' - Get the default server connection... | |
1624 | */ | |
1625 | ||
1626 | http_t * /* O - HTTP connection */ | |
1627 | _cupsConnect(void) | |
1628 | { | |
1629 | _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ | |
1630 | ||
1631 | ||
1632 | /* | |
1633 | * See if we are connected to the same server... | |
1634 | */ | |
1635 | ||
1636 | if (cg->http) | |
1637 | { | |
1638 | /* | |
1639 | * Compare the connection hostname, port, and encryption settings to | |
1640 | * the cached defaults; these were initialized the first time we | |
1641 | * connected... | |
1642 | */ | |
1643 | ||
1644 | if (strcmp(cg->http->hostname, cg->server) || | |
1645 | cg->ipp_port != _httpAddrPort(cg->http->hostaddr) || | |
1646 | (cg->http->encryption != cg->encryption && | |
1647 | cg->http->encryption == HTTP_ENCRYPT_NEVER)) | |
1648 | { | |
1649 | /* | |
1650 | * Need to close the current connection because something has changed... | |
1651 | */ | |
1652 | ||
1653 | httpClose(cg->http); | |
1654 | cg->http = NULL; | |
1655 | } | |
1656 | } | |
1657 | ||
1658 | /* | |
1659 | * (Re)connect as needed... | |
1660 | */ | |
1661 | ||
1662 | if (!cg->http) | |
1663 | { | |
1664 | if ((cg->http = httpConnectEncrypt(cupsServer(), ippPort(), | |
1665 | cupsEncryption())) == NULL) | |
1666 | { | |
1667 | if (errno) | |
1668 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, NULL, 0); | |
1669 | else | |
1670 | _cupsSetError(IPP_SERVICE_UNAVAILABLE, | |
1671 | _("Unable to connect to host."), 1); | |
1672 | } | |
1673 | } | |
1674 | ||
1675 | /* | |
1676 | * Return the cached connection... | |
1677 | */ | |
1678 | ||
1679 | return (cg->http); | |
1680 | } | |
1681 | ||
1682 | ||
1683 | /* | |
1684 | * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the | |
1685 | * first printer in a class. | |
1686 | */ | |
1687 | ||
1688 | static int /* O - 1 on success, 0 on failure */ | |
1689 | cups_get_printer_uri( | |
1690 | http_t *http, /* I - Connection to server */ | |
1691 | const char *name, /* I - Name of printer or class */ | |
1692 | char *host, /* I - Hostname buffer */ | |
1693 | int hostsize, /* I - Size of hostname buffer */ | |
1694 | int *port, /* O - Port number */ | |
1695 | char *resource, /* I - Resource buffer */ | |
1696 | int resourcesize, /* I - Size of resource buffer */ | |
1697 | int depth) /* I - Depth of query */ | |
1698 | { | |
1699 | int i; /* Looping var */ | |
1700 | int http_port; /* Port number */ | |
1701 | http_t *http2; /* Alternate HTTP connection */ | |
1702 | ipp_t *request, /* IPP request */ | |
1703 | *response; /* IPP response */ | |
1704 | ipp_attribute_t *attr; /* Current attribute */ | |
1705 | char uri[HTTP_MAX_URI], /* printer-uri attribute */ | |
1706 | scheme[HTTP_MAX_URI], /* Scheme name */ | |
1707 | username[HTTP_MAX_URI], /* Username:password */ | |
1708 | classname[255], /* Temporary class name */ | |
1709 | http_hostname[HTTP_MAX_HOST]; | |
1710 | /* Hostname associated with connection */ | |
1711 | static const char * const requested_attrs[] = | |
1712 | { /* Requested attributes */ | |
1713 | "member-uris", | |
1714 | "printer-uri-supported", | |
1715 | "printer-type" | |
1716 | }; | |
1717 | ||
1718 | ||
1719 | DEBUG_printf(("cups_get_printer_uri(http=%p, name=\"%s\", host=%p, " | |
1720 | "hostsize=%d, resource=%p, resourcesize=%d, depth=%d)\n", | |
1721 | http, name ? name : "(null)", host, hostsize, | |
1722 | resource, resourcesize, depth)); | |
1723 | ||
1724 | /* | |
1725 | * Setup the printer URI... | |
1726 | */ | |
1727 | ||
1728 | if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
1729 | "localhost", 0, "/printers/%s", name) != HTTP_URI_OK) | |
1730 | { | |
1731 | _cupsSetError(IPP_INTERNAL_ERROR, _("Unable to create printer-uri!"), 1); | |
1732 | ||
1733 | *host = '\0'; | |
1734 | *resource = '\0'; | |
1735 | ||
1736 | return (0); | |
1737 | } | |
1738 | ||
1739 | DEBUG_printf(("cups_get_printer_uri: printer-uri=\"%s\"\n", uri)); | |
1740 | ||
1741 | /* | |
1742 | * Get the hostname and port number we are connected to... | |
1743 | */ | |
1744 | ||
1745 | httpGetHostname(http, http_hostname, sizeof(http_hostname)); | |
1746 | http_port = _httpAddrPort(http->hostaddr); | |
1747 | ||
1748 | /* | |
1749 | * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following | |
1750 | * attributes: | |
1751 | * | |
1752 | * attributes-charset | |
1753 | * attributes-natural-language | |
1754 | * printer-uri | |
1755 | * requested-attributes | |
1756 | */ | |
1757 | ||
1758 | request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); | |
1759 | ||
1760 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", | |
1761 | NULL, uri); | |
1762 | ||
1763 | ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
1764 | "requested-attributes", | |
1765 | sizeof(requested_attrs) / sizeof(requested_attrs[0]), | |
1766 | NULL, requested_attrs); | |
1767 | ||
1768 | /* | |
1769 | * Do the request and get back a response... | |
1770 | */ | |
1771 | ||
1772 | if ((response = cupsDoRequest(http, request, "/")) != NULL) | |
1773 | { | |
1774 | if ((attr = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL) | |
1775 | { | |
1776 | /* | |
1777 | * Get the first actual printer name in the class... | |
1778 | */ | |
1779 | ||
1780 | for (i = 0; i < attr->num_values; i ++) | |
1781 | { | |
1782 | httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text, | |
1783 | scheme, sizeof(scheme), username, sizeof(username), | |
1784 | host, hostsize, port, resource, resourcesize); | |
1785 | if (!strncmp(resource, "/printers/", 10)) | |
1786 | { | |
1787 | /* | |
1788 | * Found a printer! | |
1789 | */ | |
1790 | ||
1791 | ippDelete(response); | |
1792 | ||
1793 | return (1); | |
1794 | } | |
1795 | } | |
1796 | ||
1797 | /* | |
1798 | * No printers in this class - try recursively looking for a printer, | |
1799 | * but not more than 3 levels deep... | |
1800 | */ | |
1801 | ||
1802 | if (depth < 3) | |
1803 | { | |
1804 | for (i = 0; i < attr->num_values; i ++) | |
1805 | { | |
1806 | httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text, | |
1807 | scheme, sizeof(scheme), username, sizeof(username), | |
1808 | host, hostsize, port, resource, resourcesize); | |
1809 | if (!strncmp(resource, "/classes/", 9)) | |
1810 | { | |
1811 | /* | |
1812 | * Found a class! Connect to the right server... | |
1813 | */ | |
1814 | ||
1815 | if (!strcasecmp(http_hostname, host) && *port == http_port) | |
1816 | http2 = http; | |
1817 | else if ((http2 = httpConnectEncrypt(host, *port, | |
1818 | cupsEncryption())) == NULL) | |
1819 | { | |
1820 | DEBUG_puts("Unable to connect to server!"); | |
1821 | ||
1822 | continue; | |
1823 | } | |
1824 | ||
1825 | /* | |
1826 | * Look up printers on that server... | |
1827 | */ | |
1828 | ||
1829 | strlcpy(classname, resource + 9, sizeof(classname)); | |
1830 | ||
1831 | cups_get_printer_uri(http2, classname, host, hostsize, port, | |
1832 | resource, resourcesize, depth + 1); | |
1833 | ||
1834 | /* | |
1835 | * Close the connection as needed... | |
1836 | */ | |
1837 | ||
1838 | if (http2 != http) | |
1839 | httpClose(http2); | |
1840 | ||
1841 | if (*host) | |
1842 | return (1); | |
1843 | } | |
1844 | } | |
1845 | } | |
1846 | } | |
1847 | else if ((attr = ippFindAttribute(response, "printer-uri-supported", | |
1848 | IPP_TAG_URI)) != NULL) | |
1849 | { | |
1850 | httpSeparateURI(HTTP_URI_CODING_ALL, | |
1851 | _httpResolveURI(attr->values[0].string.text, uri, | |
1852 | sizeof(uri), 0), | |
1853 | scheme, sizeof(scheme), username, sizeof(username), | |
1854 | host, hostsize, port, resource, resourcesize); | |
1855 | ippDelete(response); | |
1856 | ||
1857 | if (!strncmp(resource, "/classes/", 9)) | |
1858 | { | |
1859 | _cupsSetError(IPP_INTERNAL_ERROR, | |
1860 | _("No printer-uri found for class!"), 1); | |
1861 | ||
1862 | *host = '\0'; | |
1863 | *resource = '\0'; | |
1864 | ||
1865 | return (0); | |
1866 | } | |
1867 | ||
1868 | return (1); | |
1869 | } | |
1870 | ||
1871 | ippDelete(response); | |
1872 | } | |
1873 | ||
1874 | if (cupsLastError() != IPP_NOT_FOUND) | |
1875 | _cupsSetError(IPP_INTERNAL_ERROR, _("No printer-uri found!"), 1); | |
1876 | ||
1877 | *host = '\0'; | |
1878 | *resource = '\0'; | |
1879 | ||
1880 | return (0); | |
1881 | } | |
1882 | ||
1883 | ||
1884 | /* | |
1885 | * End of "$Id: util.c 7850 2008-08-20 00:07:25Z mike $". | |
1886 | */ |