]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/util.c
Changes to eliminate warnings from new Clang.
[thirdparty/cups.git] / cups / util.c
CommitLineData
ef416fc2 1/*
f2d18633 2 * "$Id$"
ef416fc2 3 *
7e86f2f6 4 * Printing utilities for CUPS.
ef416fc2 5 *
4eff9409 6 * Copyright 2007-2015 by Apple Inc.
7e86f2f6 7 * Copyright 1997-2006 by Easy Software Products.
ef416fc2 8 *
7e86f2f6
MS
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/".
ef416fc2 14 *
7e86f2f6 15 * This file is subject to the Apple OS-Developed Software exception.
ef416fc2 16 */
17
18/*
19 * Include necessary headers...
20 */
21
71e16022 22#include "cups-private.h"
ef416fc2 23#include <fcntl.h>
24#include <sys/stat.h>
25#if defined(WIN32) || defined(__EMX__)
26# include <io.h>
27#else
28# include <unistd.h>
29#endif /* WIN32 || __EMX__ */
30
31
ef416fc2 32/*
33 * 'cupsCancelJob()' - Cancel a print job on the default server.
34 *
5a738aea
MS
35 * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@
36 * to cancel the current job on the named destination.
37 *
38 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
ef416fc2 39 * the cause of any failure.
40 */
41
42int /* O - 1 on success, 0 on failure */
43cupsCancelJob(const char *name, /* I - Name of printer or class */
568fa3fa 44 int job_id) /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */
ef416fc2 45{
5a738aea 46 return (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0)
cb7f98ee 47 < IPP_STATUS_REDIRECTION_OTHER_SITE);
3d052e43
MS
48}
49
50
51/*
52 * 'cupsCancelJob2()' - Cancel or purge a print job.
53 *
54 * Canceled jobs remain in the job history while purged jobs are removed
55 * from the job history.
56 *
5a738aea
MS
57 * Pass @code CUPS_JOBID_ALL@ to cancel all jobs or @code CUPS_JOBID_CURRENT@
58 * to cancel the current job on the named destination.
59 *
60 * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get
3d052e43
MS
61 * the cause of any failure.
62 *
f3c17241 63 * @since CUPS 1.4/OS X 10.6@
3d052e43
MS
64 */
65
66ipp_status_t /* O - IPP status */
568fa3fa 67cupsCancelJob2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
5a738aea 68 const char *name, /* I - Name of printer or class */
568fa3fa 69 int job_id, /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */
5a738aea 70 int purge) /* I - 1 to purge, 0 to cancel */
3d052e43 71{
5a738aea 72 char uri[HTTP_MAX_URI]; /* Job/printer URI */
3d052e43 73 ipp_t *request; /* IPP request */
ef416fc2 74
75
76 /*
3d052e43 77 * Range check input...
ef416fc2 78 */
79
5a738aea 80 if (job_id < -1 || (!name && job_id == 0))
ef416fc2 81 {
cb7f98ee 82 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
ef416fc2 83 return (0);
84 }
85
86 /*
3d052e43 87 * Connect to the default server as needed...
ef416fc2 88 */
89
3d052e43
MS
90 if (!http)
91 if ((http = _cupsConnect()) == NULL)
cb7f98ee 92 return (IPP_STATUS_ERROR_SERVICE_UNAVAILABLE);
ef416fc2 93
94 /*
5a738aea 95 * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following
ef416fc2 96 * attributes:
97 *
98 * attributes-charset
99 * attributes-natural-language
5a738aea 100 * job-uri or printer-uri + job-id
3d052e43 101 * requesting-user-name
5a738aea 102 * [purge-job] or [purge-jobs]
ef416fc2 103 */
104
cb7f98ee 105 request = ippNewRequest(job_id < 0 ? IPP_OP_PURGE_JOBS : IPP_OP_CANCEL_JOB);
ef416fc2 106
5a738aea
MS
107 if (name)
108 {
109 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
110 "localhost", ippPort(), "/printers/%s", name);
111
112 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
113 uri);
114 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
115 job_id);
116 }
117 else if (job_id > 0)
118 {
119 snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id);
120
121 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
122 }
ef416fc2 123
124 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
125 NULL, cupsUser());
5a738aea
MS
126
127 if (purge && job_id >= 0)
3d052e43 128 ippAddBoolean(request, IPP_TAG_OPERATION, "purge-job", 1);
5a738aea
MS
129 else if (!purge && job_id < 0)
130 ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", 0);
ef416fc2 131
132 /*
133 * Do the request...
134 */
135
3d052e43
MS
136 ippDelete(cupsDoRequest(http, request, "/jobs/"));
137
138 return (cupsLastError());
139}
140
141
142/*
568fa3fa 143 * 'cupsCreateJob()' - Create an empty job for streaming.
3d052e43 144 *
568fa3fa
MS
145 * Use this function when you want to stream print data using the
146 * @link cupsStartDocument@, @link cupsWriteRequestData@, and
147 * @link cupsFinishDocument@ functions. If you have one or more files to
148 * print, use the @link cupsPrintFile2@ or @link cupsPrintFiles2@ function
149 * instead.
3d052e43 150 *
f3c17241 151 * @since CUPS 1.4/OS X 10.6@
3d052e43
MS
152 */
153
154int /* O - Job ID or 0 on error */
155cupsCreateJob(
568fa3fa
MS
156 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
157 const char *name, /* I - Destination name */
3d052e43
MS
158 const char *title, /* I - Title of job */
159 int num_options, /* I - Number of options */
160 cups_option_t *options) /* I - Options */
161{
162 char printer_uri[1024], /* Printer URI */
163 resource[1024]; /* Printer resource */
164 ipp_t *request, /* Create-Job request */
165 *response; /* Create-Job response */
166 ipp_attribute_t *attr; /* job-id attribute */
167 int job_id = 0; /* job-id value */
168
169
807315e6 170 DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", num_options=%d, options=%p)", (void *)http, name, title, num_options, (void *)options));
3d052e43
MS
171
172 /*
173 * Range check input...
174 */
175
176 if (!name)
177 {
cb7f98ee 178 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
3d052e43
MS
179 return (0);
180 }
181
182 /*
183 * Build a Create-Job request...
184 */
185
cb7f98ee 186 if ((request = ippNewRequest(IPP_OP_CREATE_JOB)) == NULL)
3d052e43 187 {
cb7f98ee 188 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOMEM), 0);
3d052e43
MS
189 return (0);
190 }
191
192 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp",
193 NULL, "localhost", ippPort(), "/printers/%s", name);
194 snprintf(resource, sizeof(resource), "/printers/%s", name);
195
196 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
197 NULL, printer_uri);
198 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
199 NULL, cupsUser());
200 if (title)
201 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
202 title);
a469f8a5 203 cupsEncodeOptions2(request, num_options, options, IPP_TAG_OPERATION);
a29fd7dd
MS
204 cupsEncodeOptions2(request, num_options, options, IPP_TAG_JOB);
205 cupsEncodeOptions2(request, num_options, options, IPP_TAG_SUBSCRIPTION);
3d052e43
MS
206
207 /*
208 * Send the request and get the job-id...
209 */
210
211 response = cupsDoRequest(http, request, resource);
212
213 if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) != NULL)
214 job_id = attr->values[0].integer;
215
216 ippDelete(response);
217
218 /*
219 * Return it...
220 */
221
222 return (job_id);
223}
224
225
226/*
227 * 'cupsFinishDocument()' - Finish sending a document.
228 *
5a738aea
MS
229 * The document must have been started using @link cupsStartDocument@.
230 *
f3c17241 231 * @since CUPS 1.4/OS X 10.6@
3d052e43
MS
232 */
233
234ipp_status_t /* O - Status of document submission */
568fa3fa
MS
235cupsFinishDocument(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
236 const char *name) /* I - Destination name */
3d052e43
MS
237{
238 char resource[1024]; /* Printer resource */
239
240
241 snprintf(resource, sizeof(resource), "/printers/%s", name);
ef416fc2 242
3d052e43
MS
243 ippDelete(cupsGetResponse(http, resource));
244
245 return (cupsLastError());
ef416fc2 246}
247
248
ef416fc2 249/*
250 * 'cupsFreeJobs()' - Free memory used by job data.
251 */
252
253void
254cupsFreeJobs(int num_jobs, /* I - Number of jobs */
255 cups_job_t *jobs) /* I - Jobs */
256{
3d052e43
MS
257 int i; /* Looping var */
258 cups_job_t *job; /* Current job */
ef416fc2 259
260
3d052e43 261 if (num_jobs <= 0 || !jobs)
ef416fc2 262 return;
263
3d052e43 264 for (i = num_jobs, job = jobs; i > 0; i --, job ++)
ef416fc2 265 {
3d052e43
MS
266 _cupsStrFree(job->dest);
267 _cupsStrFree(job->user);
268 _cupsStrFree(job->format);
269 _cupsStrFree(job->title);
ef416fc2 270 }
271
272 free(jobs);
273}
274
275
276/*
277 * 'cupsGetClasses()' - Get a list of printer classes from the default server.
278 *
240214ef
MS
279 * This function is deprecated and no longer returns a list of printer
280 * classes - use @link cupsGetDests@ instead.
ef416fc2 281 *
282 * @deprecated@
283 */
284
285int /* O - Number of classes */
286cupsGetClasses(char ***classes) /* O - Classes */
287{
240214ef
MS
288 if (classes)
289 *classes = NULL;
ef416fc2 290
240214ef 291 return (0);
ef416fc2 292}
293
294
295/*
296 * 'cupsGetDefault()' - Get the default printer or class for the default server.
297 *
298 * This function returns the default printer or class as defined by
299 * the LPDEST or PRINTER environment variables. If these environment
300 * variables are not set, the server default destination is returned.
5a738aea
MS
301 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
302 * functions to get the user-defined default printer, as this function does
303 * not support the lpoptions-defined default printer.
ef416fc2 304 */
305
5a738aea 306const char * /* O - Default printer or @code NULL@ */
ef416fc2 307cupsGetDefault(void)
308{
ef416fc2 309 /*
310 * Return the default printer...
311 */
312
3d052e43 313 return (cupsGetDefault2(CUPS_HTTP_DEFAULT));
ef416fc2 314}
315
316
317/*
318 * 'cupsGetDefault2()' - Get the default printer or class for the specified server.
319 *
320 * This function returns the default printer or class as defined by
321 * the LPDEST or PRINTER environment variables. If these environment
322 * variables are not set, the server default destination is returned.
5a738aea
MS
323 * Applications should use the @link cupsGetDests@ and @link cupsGetDest@
324 * functions to get the user-defined default printer, as this function does
325 * not support the lpoptions-defined default printer.
ef416fc2 326 *
f3c17241 327 * @since CUPS 1.1.21/OS X 10.4@
ef416fc2 328 */
329
5a738aea 330const char * /* O - Default printer or @code NULL@ */
568fa3fa 331cupsGetDefault2(http_t *http) /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
ef416fc2 332{
333 ipp_t *request, /* IPP Request */
334 *response; /* IPP Response */
335 ipp_attribute_t *attr; /* Current attribute */
ef416fc2 336 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
337
338
339 /*
38e73f87 340 * See if we have a user default printer set...
ef416fc2 341 */
342
38e73f87
MS
343 if (_cupsUserDefault(cg->def_printer, sizeof(cg->def_printer)))
344 return (cg->def_printer);
ef416fc2 345
346 /*
3d052e43 347 * Connect to the server as needed...
ef416fc2 348 */
349
350 if (!http)
3d052e43
MS
351 if ((http = _cupsConnect()) == NULL)
352 return (NULL);
ef416fc2 353
354 /*
355 * Build a CUPS_GET_DEFAULT request, which requires the following
356 * attributes:
357 *
358 * attributes-charset
359 * attributes-natural-language
360 */
361
cb7f98ee 362 request = ippNewRequest(IPP_OP_CUPS_GET_DEFAULT);
ef416fc2 363
364 /*
365 * Do the request and get back a response...
366 */
367
368 if ((response = cupsDoRequest(http, request, "/")) != NULL)
369 {
3d052e43
MS
370 if ((attr = ippFindAttribute(response, "printer-name",
371 IPP_TAG_NAME)) != NULL)
ef416fc2 372 {
3d052e43
MS
373 strlcpy(cg->def_printer, attr->values[0].string.text,
374 sizeof(cg->def_printer));
ef416fc2 375 ippDelete(response);
376 return (cg->def_printer);
377 }
378
379 ippDelete(response);
380 }
381
382 return (NULL);
383}
384
385
386/*
387 * 'cupsGetJobs()' - Get the jobs from the default server.
5a738aea
MS
388 *
389 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
390 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
391 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
392 * jobs that are stopped, canceled, aborted, or completed.
ef416fc2 393 */
394
395int /* O - Number of jobs */
396cupsGetJobs(cups_job_t **jobs, /* O - Job data */
568fa3fa 397 const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */
ecdc0628 398 int myjobs, /* I - 0 = all users, 1 = mine */
5a738aea 399 int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
ef416fc2 400{
ef416fc2 401 /*
402 * Return the jobs...
403 */
404
5a738aea 405 return (cupsGetJobs2(CUPS_HTTP_DEFAULT, jobs, name, myjobs, whichjobs));
ef416fc2 406}
407
408
409
410/*
411 * 'cupsGetJobs2()' - Get the jobs from the specified server.
412 *
5a738aea
MS
413 * A "whichjobs" value of @code CUPS_WHICHJOBS_ALL@ returns all jobs regardless
414 * of state, while @code CUPS_WHICHJOBS_ACTIVE@ returns jobs that are
415 * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns
416 * jobs that are stopped, canceled, aborted, or completed.
417 *
f3c17241 418 * @since CUPS 1.1.21/OS X 10.4@
ef416fc2 419 */
420
421int /* O - Number of jobs */
568fa3fa 422cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
ef416fc2 423 cups_job_t **jobs, /* O - Job data */
568fa3fa 424 const char *name, /* I - @code NULL@ = all destinations, otherwise show jobs for named destination */
ecdc0628 425 int myjobs, /* I - 0 = all users, 1 = mine */
5a738aea 426 int whichjobs) /* I - @code CUPS_WHICHJOBS_ALL@, @code CUPS_WHICHJOBS_ACTIVE@, or @code CUPS_WHICHJOBS_COMPLETED@ */
ef416fc2 427{
428 int n; /* Number of jobs */
429 ipp_t *request, /* IPP Request */
430 *response; /* IPP Response */
431 ipp_attribute_t *attr; /* Current attribute */
ef416fc2 432 cups_job_t *temp; /* Temporary pointer */
433 int id, /* job-id */
434 priority, /* job-priority */
435 size; /* job-k-octets */
436 ipp_jstate_t state; /* job-state */
437 time_t completed_time, /* time-at-completed */
438 creation_time, /* time-at-creation */
439 processing_time; /* time-at-processing */
440 const char *dest, /* job-printer-uri */
441 *format, /* document-format */
442 *title, /* job-name */
443 *user; /* job-originating-user-name */
444 char uri[HTTP_MAX_URI]; /* URI for jobs */
445 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
446 static const char * const attrs[] = /* Requested attributes */
447 {
5a662dc0 448 "document-format",
ef416fc2 449 "job-id",
ef416fc2 450 "job-k-octets",
5a662dc0
MS
451 "job-name",
452 "job-originating-user-name",
453 "job-printer-uri",
454 "job-priority",
ef416fc2 455 "job-state",
456 "time-at-completed",
457 "time-at-creation",
5a662dc0 458 "time-at-processing"
ef416fc2 459 };
460
461
462 /*
463 * Range check input...
464 */
465
3d052e43 466 if (!jobs)
ef416fc2 467 {
cb7f98ee 468 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
ef416fc2 469
470 return (-1);
471 }
472
473 /*
474 * Get the right URI...
475 */
476
5a738aea 477 if (name)
ef416fc2 478 {
a4d04587 479 if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
cb7f98ee
MS
480 "localhost", 0, "/printers/%s",
481 name) < HTTP_URI_STATUS_OK)
ef416fc2 482 {
cb7f98ee
MS
483 _cupsSetError(IPP_STATUS_ERROR_INTERNAL,
484 _("Unable to create printer-uri"), 1);
ef416fc2 485
486 return (-1);
487 }
488 }
489 else
5a9febac 490 strlcpy(uri, "ipp://localhost/", sizeof(uri));
ef416fc2 491
3d052e43
MS
492 if (!http)
493 if ((http = _cupsConnect()) == NULL)
494 return (-1);
ef416fc2 495
496 /*
497 * Build an IPP_GET_JOBS request, which requires the following
498 * attributes:
499 *
500 * attributes-charset
501 * attributes-natural-language
502 * printer-uri
503 * requesting-user-name
504 * which-jobs
505 * my-jobs
506 * requested-attributes
507 */
508
cb7f98ee 509 request = ippNewRequest(IPP_OP_GET_JOBS);
ef416fc2 510
511 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
512 "printer-uri", NULL, uri);
513
514 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
515 "requesting-user-name", NULL, cupsUser());
516
517 if (myjobs)
518 ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
519
5a738aea 520 if (whichjobs == CUPS_WHICHJOBS_COMPLETED)
ef416fc2 521 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
522 "which-jobs", NULL, "completed");
5a738aea 523 else if (whichjobs == CUPS_WHICHJOBS_ALL)
ecdc0628 524 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
525 "which-jobs", NULL, "all");
ef416fc2 526
527 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
528 "requested-attributes", sizeof(attrs) / sizeof(attrs[0]),
529 NULL, attrs);
530
531 /*
532 * Do the request and get back a response...
533 */
534
535 n = 0;
536 *jobs = NULL;
537
538 if ((response = cupsDoRequest(http, request, "/")) != NULL)
539 {
3d052e43 540 for (attr = response->attrs; attr; attr = attr->next)
ef416fc2 541 {
542 /*
543 * Skip leading attributes until we hit a job...
544 */
545
3d052e43 546 while (attr && attr->group_tag != IPP_TAG_JOB)
ef416fc2 547 attr = attr->next;
548
3d052e43 549 if (!attr)
ef416fc2 550 break;
551
552 /*
553 * Pull the needed attributes from this job...
554 */
555
556 id = 0;
557 size = 0;
558 priority = 50;
cb7f98ee 559 state = IPP_JSTATE_PENDING;
ef416fc2 560 user = "unknown";
561 dest = NULL;
562 format = "application/octet-stream";
563 title = "untitled";
564 creation_time = 0;
565 completed_time = 0;
566 processing_time = 0;
567
3d052e43 568 while (attr && attr->group_tag == IPP_TAG_JOB)
ef416fc2 569 {
3d052e43 570 if (!strcmp(attr->name, "job-id") &&
ef416fc2 571 attr->value_tag == IPP_TAG_INTEGER)
572 id = attr->values[0].integer;
3d052e43 573 else if (!strcmp(attr->name, "job-state") &&
ef416fc2 574 attr->value_tag == IPP_TAG_ENUM)
575 state = (ipp_jstate_t)attr->values[0].integer;
3d052e43 576 else if (!strcmp(attr->name, "job-priority") &&
ef416fc2 577 attr->value_tag == IPP_TAG_INTEGER)
578 priority = attr->values[0].integer;
3d052e43 579 else if (!strcmp(attr->name, "job-k-octets") &&
ef416fc2 580 attr->value_tag == IPP_TAG_INTEGER)
581 size = attr->values[0].integer;
3d052e43 582 else if (!strcmp(attr->name, "time-at-completed") &&
ef416fc2 583 attr->value_tag == IPP_TAG_INTEGER)
584 completed_time = attr->values[0].integer;
3d052e43 585 else if (!strcmp(attr->name, "time-at-creation") &&
ef416fc2 586 attr->value_tag == IPP_TAG_INTEGER)
587 creation_time = attr->values[0].integer;
3d052e43 588 else if (!strcmp(attr->name, "time-at-processing") &&
ef416fc2 589 attr->value_tag == IPP_TAG_INTEGER)
590 processing_time = attr->values[0].integer;
3d052e43 591 else if (!strcmp(attr->name, "job-printer-uri") &&
ef416fc2 592 attr->value_tag == IPP_TAG_URI)
593 {
594 if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL)
595 dest ++;
596 }
3d052e43 597 else if (!strcmp(attr->name, "job-originating-user-name") &&
ef416fc2 598 attr->value_tag == IPP_TAG_NAME)
599 user = attr->values[0].string.text;
3d052e43 600 else if (!strcmp(attr->name, "document-format") &&
ef416fc2 601 attr->value_tag == IPP_TAG_MIMETYPE)
602 format = attr->values[0].string.text;
3d052e43 603 else if (!strcmp(attr->name, "job-name") &&
ef416fc2 604 (attr->value_tag == IPP_TAG_TEXT ||
605 attr->value_tag == IPP_TAG_NAME))
606 title = attr->values[0].string.text;
607
608 attr = attr->next;
609 }
610
611 /*
612 * See if we have everything needed...
613 */
614
3d052e43 615 if (!dest || !id)
ef416fc2 616 {
3d052e43 617 if (!attr)
ef416fc2 618 break;
619 else
620 continue;
621 }
622
623 /*
624 * Allocate memory for the job...
625 */
626
627 if (n == 0)
628 temp = malloc(sizeof(cups_job_t));
629 else
7e86f2f6 630 temp = realloc(*jobs, sizeof(cups_job_t) * (size_t)(n + 1));
ef416fc2 631
3d052e43 632 if (!temp)
ef416fc2 633 {
634 /*
635 * Ran out of memory!
636 */
637
cb7f98ee 638 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
3d052e43 639
ef416fc2 640 cupsFreeJobs(n, *jobs);
641 *jobs = NULL;
642
643 ippDelete(response);
3d052e43
MS
644
645 return (-1);
ef416fc2 646 }
647
648 *jobs = temp;
649 temp += n;
650 n ++;
651
652 /*
653 * Copy the data over...
654 */
655
3d052e43
MS
656 temp->dest = _cupsStrAlloc(dest);
657 temp->user = _cupsStrAlloc(user);
658 temp->format = _cupsStrAlloc(format);
659 temp->title = _cupsStrAlloc(title);
ef416fc2 660 temp->id = id;
661 temp->priority = priority;
662 temp->state = state;
663 temp->size = size;
664 temp->completed_time = completed_time;
665 temp->creation_time = creation_time;
666 temp->processing_time = processing_time;
667
3d052e43 668 if (!attr)
ef416fc2 669 break;
670 }
671
672 ippDelete(response);
673 }
674
cb7f98ee 675 if (n == 0 && cg->last_error >= IPP_STATUS_ERROR_BAD_REQUEST)
ef416fc2 676 return (-1);
677 else
678 return (n);
679}
680
681
ef416fc2 682/*
683 * 'cupsGetPrinters()' - Get a list of printers from the default server.
684 *
240214ef
MS
685 * This function is deprecated and no longer returns a list of printers - use
686 * @link cupsGetDests@ instead.
ef416fc2 687 *
688 * @deprecated@
689 */
690
691int /* O - Number of printers */
692cupsGetPrinters(char ***printers) /* O - Printers */
693{
240214ef
MS
694 if (printers)
695 *printers = NULL;
ef416fc2 696
240214ef 697 return (0);
ef416fc2 698}
699
700
ef416fc2 701/*
702 * 'cupsPrintFile()' - Print a file to a printer or class on the default server.
703 */
704
5a738aea 705int /* O - Job ID or 0 on error */
568fa3fa 706cupsPrintFile(const char *name, /* I - Destination name */
ef416fc2 707 const char *filename, /* I - File to print */
708 const char *title, /* I - Title of job */
709 int num_options,/* I - Number of options */
710 cups_option_t *options) /* I - Options */
711{
807315e6 712 DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", title=\"%s\", num_options=%d, options=%p)", name, filename, title, num_options, (void *)options));
ef416fc2 713
3d052e43
MS
714 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title,
715 num_options, options));
ef416fc2 716}
717
718
719/*
5a738aea
MS
720 * 'cupsPrintFile2()' - Print a file to a printer or class on the specified
721 * server.
ef416fc2 722 *
f3c17241 723 * @since CUPS 1.1.21/OS X 10.4@
ef416fc2 724 */
725
5a738aea 726int /* O - Job ID or 0 on error */
3d052e43 727cupsPrintFile2(
568fa3fa
MS
728 http_t *http, /* I - Connection to server */
729 const char *name, /* I - Destination name */
3d052e43
MS
730 const char *filename, /* I - File to print */
731 const char *title, /* I - Title of job */
732 int num_options, /* I - Number of options */
733 cups_option_t *options) /* I - Options */
ef416fc2 734{
807315e6 735 DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", title=\"%s\", num_options=%d, options=%p)", (void *)http, name, filename, title, num_options, (void *)options));
ef416fc2 736
3d052e43
MS
737 return (cupsPrintFiles2(http, name, 1, &filename, title, num_options,
738 options));
ef416fc2 739}
740
741
742/*
fa73b229 743 * 'cupsPrintFiles()' - Print one or more files to a printer or class on the
744 * default server.
ef416fc2 745 */
746
5a738aea 747int /* O - Job ID or 0 on error */
3d052e43 748cupsPrintFiles(
568fa3fa 749 const char *name, /* I - Destination name */
3d052e43
MS
750 int num_files, /* I - Number of files */
751 const char **files, /* I - File(s) to print */
752 const char *title, /* I - Title of job */
753 int num_options, /* I - Number of options */
754 cups_option_t *options) /* I - Options */
ef416fc2 755{
807315e6 756 DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, files=%p, title=\"%s\", num_options=%d, options=%p)", name, num_files, (void *)files, title, num_options, (void *)options));
ef416fc2 757
ef416fc2 758 /*
759 * Print the file(s)...
760 */
761
3d052e43 762 return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title,
ef416fc2 763 num_options, options));
764}
765
766
ef416fc2 767/*
fa73b229 768 * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the
769 * specified server.
ef416fc2 770 *
f3c17241 771 * @since CUPS 1.1.21/OS X 10.4@
ef416fc2 772 */
773
5a738aea 774int /* O - Job ID or 0 on error */
3d052e43 775cupsPrintFiles2(
568fa3fa
MS
776 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
777 const char *name, /* I - Destination name */
3d052e43
MS
778 int num_files, /* I - Number of files */
779 const char **files, /* I - File(s) to print */
780 const char *title, /* I - Title of job */
781 int num_options, /* I - Number of options */
782 cups_option_t *options) /* I - Options */
ef416fc2 783{
784 int i; /* Looping var */
3d052e43
MS
785 int job_id; /* New job ID */
786 const char *docname; /* Basename of current filename */
787 const char *format; /* Document format */
788 cups_file_t *fp; /* Current file */
789 char buffer[8192]; /* Copy buffer */
790 ssize_t bytes; /* Bytes in buffer */
791 http_status_t status; /* Status of write */
5a662dc0
MS
792 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
793 ipp_status_t cancel_status; /* Status code to preserve */
794 char *cancel_message; /* Error message to preserve */
ef416fc2 795
796
807315e6 797 DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, files=%p, title=\"%s\", num_options=%d, options=%p)", (void *)http, name, num_files, (void *)files, title, num_options, (void *)options));
ef416fc2 798
799 /*
800 * Range check input...
801 */
802
3d052e43 803 if (!name || num_files < 1 || !files)
ef416fc2 804 {
cb7f98ee 805 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0);
ef416fc2 806
807 return (0);
808 }
809
810 /*
3d052e43 811 * Create the print job...
ef416fc2 812 */
813
3d052e43 814 if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0)
ef416fc2 815 return (0);
ef416fc2 816
817 /*
3d052e43 818 * Send each of the files...
ef416fc2 819 */
820
3d052e43
MS
821 if (cupsGetOption("raw", num_options, options))
822 format = CUPS_FORMAT_RAW;
823 else if ((format = cupsGetOption("document-format", num_options,
824 options)) == NULL)
825 format = CUPS_FORMAT_AUTO;
ef416fc2 826
3d052e43
MS
827 for (i = 0; i < num_files; i ++)
828 {
829 /*
830 * Start the next file...
831 */
ef416fc2 832
3d052e43
MS
833 if ((docname = strrchr(files[i], '/')) != NULL)
834 docname ++;
835 else
836 docname = files[i];
ef416fc2 837
3d052e43
MS
838 if ((fp = cupsFileOpen(files[i], "rb")) == NULL)
839 {
840 /*
841 * Unable to open print file, cancel the job and return...
842 */
ef416fc2 843
cb7f98ee 844 _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_ACCESS, NULL, 0);
5a662dc0 845 goto cancel_job;
3d052e43 846 }
ef416fc2 847
ba55dc12
MS
848 status = cupsStartDocument(http, name, job_id, docname, format,
849 i == (num_files - 1));
ef416fc2 850
cb7f98ee 851 while (status == HTTP_STATUS_CONTINUE &&
ba55dc12 852 (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0)
7e86f2f6 853 status = cupsWriteRequestData(http, buffer, (size_t)bytes);
ef416fc2 854
3d052e43 855 cupsFileClose(fp);
ef416fc2 856
cb7f98ee 857 if (status != HTTP_STATUS_CONTINUE || cupsFinishDocument(http, name) != IPP_STATUS_OK)
ef416fc2 858 {
859 /*
3d052e43 860 * Unable to queue, cancel the job and return...
ef416fc2 861 */
862
5a662dc0 863 goto cancel_job;
3d052e43
MS
864 }
865 }
ef416fc2 866
3d052e43 867 return (job_id);
5a662dc0
MS
868
869 /*
870 * If we get here, something happened while sending the print job so we need
871 * to cancel the job without setting the last error (since we need to preserve
872 * the current error...
873 */
874
875 cancel_job:
876
877 cancel_status = cg->last_error;
878 cancel_message = cg->last_status_message ?
879 _cupsStrRetain(cg->last_status_message) : NULL;
880
881 cupsCancelJob2(http, name, job_id, 0);
882
883 cg->last_error = cancel_status;
884 cg->last_status_message = cancel_message;
885
886 return (0);
3d052e43 887}
ef416fc2 888
ef416fc2 889
3d052e43
MS
890/*
891 * 'cupsStartDocument()' - Add a document to a job created with cupsCreateJob().
892 *
5a738aea
MS
893 * Use @link cupsWriteRequestData@ to write data for the document and
894 * @link cupsFinishDocument@ to finish the document and get the submission status.
3d052e43 895 *
5a738aea
MS
896 * The MIME type constants @code CUPS_FORMAT_AUTO@, @code CUPS_FORMAT_PDF@,
897 * @code CUPS_FORMAT_POSTSCRIPT@, @code CUPS_FORMAT_RAW@, and
898 * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although
899 * any supported MIME type string can be supplied.
3d052e43 900 *
f3c17241 901 * @since CUPS 1.4/OS X 10.6@
3d052e43 902 */
ef416fc2 903
3d052e43
MS
904http_status_t /* O - HTTP status of request */
905cupsStartDocument(
568fa3fa
MS
906 http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
907 const char *name, /* I - Destination name */
5a738aea 908 int job_id, /* I - Job ID from @link cupsCreateJob@ */
3d052e43 909 const char *docname, /* I - Name of document */
5a738aea 910 const char *format, /* I - MIME type or @code CUPS_FORMAT_foo@ */
3d052e43
MS
911 int last_document) /* I - 1 for last document in job, 0 otherwise */
912{
913 char resource[1024], /* Resource for destinatio */
914 printer_uri[1024]; /* Printer URI */
915 ipp_t *request; /* Send-Document request */
916 http_status_t status; /* HTTP status */
ef416fc2 917
bd7854cb 918
3d052e43
MS
919 /*
920 * Create a Send-Document request...
921 */
bd7854cb 922
cb7f98ee 923 if ((request = ippNewRequest(IPP_OP_SEND_DOCUMENT)) == NULL)
3d052e43 924 {
cb7f98ee
MS
925 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOMEM), 0);
926 return (HTTP_STATUS_ERROR);
3d052e43 927 }
bd7854cb 928
3d052e43
MS
929 httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp",
930 NULL, "localhost", ippPort(), "/printers/%s", name);
931 snprintf(resource, sizeof(resource), "/printers/%s", name);
ef416fc2 932
3d052e43
MS
933 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
934 NULL, printer_uri);
935 ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id);
936 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
937 NULL, cupsUser());
938 if (docname)
939 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name",
940 NULL, docname);
941 if (format)
942 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
943 "document-format", NULL, format);
7e86f2f6 944 ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", (char)last_document);
ef416fc2 945
3d052e43
MS
946 /*
947 * Send and delete the request, then return the status...
948 */
ef416fc2 949
3d052e43 950 status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE);
ef416fc2 951
3d052e43 952 ippDelete(request);
ef416fc2 953
3d052e43 954 return (status);
ef416fc2 955}
956
957
ef416fc2 958/*
f2d18633 959 * End of "$Id$".
ef416fc2 960 */