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