]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
1a18c85c | 2 | * "$Id: lp.c 11558 2014-02-06 18:33:34Z msweet $" |
ef416fc2 | 3 | * |
1a18c85c | 4 | * "lp" command for CUPS. |
ef416fc2 | 5 | * |
1a18c85c MS |
6 | * Copyright 2007-2014 by Apple Inc. |
7 | * Copyright 1997-2007 by Easy Software Products. | |
ef416fc2 | 8 | * |
1a18c85c 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 | */ |
15 | ||
16 | /* | |
17 | * Include necessary headers... | |
18 | */ | |
19 | ||
71e16022 | 20 | #include <cups/cups-private.h> |
ef416fc2 | 21 | |
22 | ||
ef416fc2 | 23 | /* |
24 | * Local functions. | |
25 | */ | |
26 | ||
fa73b229 | 27 | int restart_job(const char *command, int job_id); |
28 | int set_job_attrs(const char *command, int job_id, int num_options, | |
29 | cups_option_t *options); | |
ef416fc2 | 30 | |
31 | ||
ef416fc2 | 32 | /* |
33 | * 'main()' - Parse options and send files for printing. | |
34 | */ | |
35 | ||
36 | int | |
37 | main(int argc, /* I - Number of command-line arguments */ | |
38 | char *argv[]) /* I - Command-line arguments */ | |
39 | { | |
40 | int i, j; /* Looping vars */ | |
41 | int job_id; /* Job ID */ | |
42 | char *printer, /* Printer name */ | |
a29fd7dd | 43 | *instance, /* Instance name */ |
ef416fc2 | 44 | *val, /* Option value */ |
45 | *title; /* Job title */ | |
46 | int priority; /* Job priority (1-100) */ | |
47 | int num_copies; /* Number of copies per file */ | |
48 | int num_files; /* Number of files to print */ | |
49 | const char *files[1000]; /* Files to print */ | |
a4924f6c | 50 | cups_dest_t *dest; /* Selected destination */ |
ef416fc2 | 51 | int num_options; /* Number of options */ |
52 | cups_option_t *options; /* Options */ | |
fa73b229 | 53 | int end_options; /* No more options? */ |
ef416fc2 | 54 | int silent; /* Silent or verbose output? */ |
55 | char buffer[8192]; /* Copy buffer */ | |
ef416fc2 | 56 | |
57 | ||
58 | #ifdef __sun | |
59 | /* | |
60 | * Solaris does some rather strange things to re-queue remote print | |
61 | * jobs. On bootup, the "lp" command is run as "printd" to re-spool | |
62 | * any remote jobs in /var/spool/print. Since CUPS doesn't need this | |
63 | * nonsense, we just need to add the necessary check here to prevent | |
64 | * lp from causing boot problems... | |
65 | */ | |
66 | ||
67 | if ((val = strrchr(argv[0], '/')) != NULL) | |
68 | val ++; | |
69 | else | |
70 | val = argv[0]; | |
71 | ||
72 | if (!strcmp(val, "printd")) | |
73 | return (0); | |
74 | #endif /* __sun */ | |
75 | ||
07725fee | 76 | _cupsSetLocale(argv); |
d09495fa | 77 | |
ef416fc2 | 78 | silent = 0; |
79 | printer = NULL; | |
a4924f6c | 80 | dest = NULL; |
ef416fc2 | 81 | num_options = 0; |
82 | options = NULL; | |
83 | num_files = 0; | |
84 | title = NULL; | |
85 | job_id = 0; | |
fa73b229 | 86 | end_options = 0; |
ef416fc2 | 87 | |
88 | for (i = 1; i < argc; i ++) | |
fa73b229 | 89 | if (argv[i][0] == '-' && argv[i][1] && !end_options) |
ef416fc2 | 90 | switch (argv[i][1]) |
91 | { | |
92 | case 'E' : /* Encrypt */ | |
93 | #ifdef HAVE_SSL | |
94 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); | |
95 | #else | |
0837b7e8 | 96 | _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."), |
ef416fc2 | 97 | argv[0]); |
98 | #endif /* HAVE_SSL */ | |
99 | break; | |
100 | ||
fa73b229 | 101 | case 'U' : /* Username */ |
102 | if (argv[i][2] != '\0') | |
103 | cupsSetUser(argv[i] + 2); | |
104 | else | |
105 | { | |
106 | i ++; | |
107 | if (i >= argc) | |
108 | { | |
109 | _cupsLangPrintf(stderr, | |
0837b7e8 MS |
110 | _("%s: Error - expected username after \"-U\" " |
111 | "option."), argv[0]); | |
fa73b229 | 112 | return (1); |
113 | } | |
114 | ||
115 | cupsSetUser(argv[i]); | |
116 | } | |
117 | break; | |
a29fd7dd | 118 | |
ef416fc2 | 119 | case 'c' : /* Copy to spool dir (always enabled) */ |
120 | break; | |
121 | ||
122 | case 'd' : /* Destination printer or class */ | |
123 | if (argv[i][2] != '\0') | |
124 | printer = argv[i] + 2; | |
125 | else | |
126 | { | |
127 | i ++; | |
128 | ||
129 | if (i >= argc) | |
130 | { | |
fa73b229 | 131 | _cupsLangPrintf(stderr, |
132 | _("%s: Error - expected destination after " | |
0837b7e8 | 133 | "\"-d\" option."), argv[0]); |
ef416fc2 | 134 | return (1); |
135 | } | |
136 | ||
137 | printer = argv[i]; | |
138 | } | |
139 | ||
140 | if ((instance = strrchr(printer, '/')) != NULL) | |
141 | *instance++ = '\0'; | |
142 | ||
c606bcae MS |
143 | if ((dest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, printer, |
144 | instance)) != NULL) | |
ef416fc2 | 145 | { |
146 | for (j = 0; j < dest->num_options; j ++) | |
0837b7e8 MS |
147 | if (cupsGetOption(dest->options[j].name, num_options, |
148 | options) == NULL) | |
ef416fc2 | 149 | num_options = cupsAddOption(dest->options[j].name, |
150 | dest->options[j].value, | |
151 | num_options, &options); | |
152 | } | |
c606bcae MS |
153 | else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || |
154 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) | |
155 | { | |
156 | _cupsLangPrintf(stderr, | |
157 | _("%s: Error - add '/version=1.1' to server " | |
158 | "name."), argv[0]); | |
159 | return (1); | |
160 | } | |
ef416fc2 | 161 | break; |
162 | ||
163 | case 'f' : /* Form */ | |
164 | if (!argv[i][2]) | |
165 | { | |
166 | i ++; | |
167 | ||
168 | if (i >= argc) | |
169 | { | |
fa73b229 | 170 | _cupsLangPrintf(stderr, |
0837b7e8 MS |
171 | _("%s: Error - expected form after \"-f\" " |
172 | "option."), | |
fa73b229 | 173 | argv[0]); |
ef416fc2 | 174 | return (1); |
175 | } | |
176 | } | |
177 | ||
0837b7e8 | 178 | _cupsLangPrintf(stderr, _("%s: Warning - form option ignored."), |
fa73b229 | 179 | argv[0]); |
ef416fc2 | 180 | break; |
181 | ||
182 | case 'h' : /* Destination host */ | |
183 | if (argv[i][2] != '\0') | |
184 | cupsSetServer(argv[i] + 2); | |
185 | else | |
186 | { | |
187 | i ++; | |
188 | ||
189 | if (i >= argc) | |
190 | { | |
fa73b229 | 191 | _cupsLangPrintf(stderr, |
192 | _("%s: Error - expected hostname after " | |
0837b7e8 | 193 | "\"-h\" option."), argv[0]); |
ef416fc2 | 194 | return (1); |
195 | } | |
196 | ||
197 | cupsSetServer(argv[i]); | |
198 | } | |
199 | break; | |
200 | ||
201 | case 'i' : /* Change job */ | |
202 | if (argv[i][2]) | |
203 | val = argv[i] + 2; | |
204 | else | |
205 | { | |
206 | i ++; | |
207 | ||
208 | if (i >= argc) | |
209 | { | |
fa73b229 | 210 | _cupsLangPrintf(stderr, |
0837b7e8 | 211 | _("%s: Expected job ID after \"-i\" option."), |
fa73b229 | 212 | argv[0]); |
ef416fc2 | 213 | return (1); |
214 | } | |
215 | ||
216 | val = argv[i]; | |
217 | } | |
218 | ||
219 | if (num_files > 0) | |
220 | { | |
fa73b229 | 221 | _cupsLangPrintf(stderr, |
222 | _("%s: Error - cannot print files and alter " | |
0837b7e8 | 223 | "jobs simultaneously."), argv[0]); |
ef416fc2 | 224 | return (1); |
225 | } | |
226 | ||
227 | if (strrchr(val, '-') != NULL) | |
228 | job_id = atoi(strrchr(val, '-') + 1); | |
229 | else | |
230 | job_id = atoi(val); | |
231 | ||
232 | if (job_id < 0) | |
233 | { | |
0837b7e8 | 234 | _cupsLangPrintf(stderr, _("%s: Error - bad job ID."), argv[0]); |
ef416fc2 | 235 | break; |
236 | } | |
237 | break; | |
238 | ||
239 | case 'm' : /* Send email when job is done */ | |
240 | #ifdef __sun | |
241 | case 'p' : /* Notify on completion */ | |
242 | #endif /* __sun */ | |
243 | case 'w' : /* Write to console or email */ | |
fa73b229 | 244 | { |
245 | char email[1024]; /* EMail address */ | |
246 | ||
247 | ||
248 | snprintf(email, sizeof(email), "mailto:%s@%s", cupsUser(), | |
757d2cad | 249 | httpGetHostname(NULL, buffer, sizeof(buffer))); |
7594b224 | 250 | num_options = cupsAddOption("notify-recipient-uri", email, |
fa73b229 | 251 | num_options, &options); |
252 | } | |
253 | ||
254 | silent = 1; | |
ef416fc2 | 255 | break; |
256 | ||
257 | case 'n' : /* Number of copies */ | |
258 | if (argv[i][2] != '\0') | |
259 | num_copies = atoi(argv[i] + 2); | |
260 | else | |
261 | { | |
262 | i ++; | |
263 | ||
264 | if (i >= argc) | |
265 | { | |
fa73b229 | 266 | _cupsLangPrintf(stderr, |
267 | _("%s: Error - expected copies after " | |
0837b7e8 | 268 | "\"-n\" option."), argv[0]); |
ef416fc2 | 269 | return (1); |
270 | } | |
271 | ||
272 | num_copies = atoi(argv[i]); | |
273 | } | |
274 | ||
275 | sprintf(buffer, "%d", num_copies); | |
0837b7e8 MS |
276 | num_options = cupsAddOption("copies", buffer, num_options, |
277 | &options); | |
ef416fc2 | 278 | break; |
279 | ||
280 | case 'o' : /* Option */ | |
281 | if (argv[i][2] != '\0') | |
0837b7e8 MS |
282 | num_options = cupsParseOptions(argv[i] + 2, num_options, |
283 | &options); | |
ef416fc2 | 284 | else |
285 | { | |
286 | i ++; | |
287 | ||
288 | if (i >= argc) | |
289 | { | |
fa73b229 | 290 | _cupsLangPrintf(stderr, |
84315f46 | 291 | _("%s: Error - expected option=value after " |
0837b7e8 | 292 | "\"-o\" option."), argv[0]); |
ef416fc2 | 293 | return (1); |
294 | } | |
295 | ||
296 | num_options = cupsParseOptions(argv[i], num_options, &options); | |
297 | } | |
298 | break; | |
299 | ||
300 | #ifndef __sun | |
301 | case 'p' : /* Queue priority */ | |
302 | #endif /* !__sun */ | |
303 | case 'q' : /* Queue priority */ | |
304 | if (argv[i][2] != '\0') | |
305 | priority = atoi(argv[i] + 2); | |
306 | else | |
307 | { | |
308 | if ((i + 1) >= argc) | |
309 | { | |
fa73b229 | 310 | _cupsLangPrintf(stderr, |
311 | _("%s: Error - expected priority after " | |
0837b7e8 | 312 | "\"-%c\" option."), argv[0], argv[i][1]); |
ef416fc2 | 313 | return (1); |
314 | } | |
315 | ||
316 | i ++; | |
317 | ||
318 | priority = atoi(argv[i]); | |
319 | } | |
320 | ||
321 | /* | |
322 | * For 100% Solaris compatibility, need to add: | |
323 | * | |
324 | * priority = 99 * (39 - priority) / 39 + 1; | |
325 | * | |
326 | * However, to keep CUPS lp the same across all platforms | |
327 | * we will break compatibility this far... | |
328 | */ | |
329 | ||
330 | if (priority < 1 || priority > 100) | |
331 | { | |
fa73b229 | 332 | _cupsLangPrintf(stderr, |
333 | _("%s: Error - priority must be between 1 and " | |
0837b7e8 | 334 | "100."), argv[0]); |
ef416fc2 | 335 | return (1); |
336 | } | |
337 | ||
338 | sprintf(buffer, "%d", priority); | |
0837b7e8 MS |
339 | num_options = cupsAddOption("job-priority", buffer, num_options, |
340 | &options); | |
ef416fc2 | 341 | break; |
342 | ||
343 | case 's' : /* Silent */ | |
344 | silent = 1; | |
345 | break; | |
346 | ||
347 | case 't' : /* Title */ | |
348 | if (argv[i][2] != '\0') | |
349 | title = argv[i] + 2; | |
350 | else | |
351 | { | |
352 | i ++; | |
353 | ||
354 | if (i >= argc) | |
355 | { | |
fa73b229 | 356 | _cupsLangPrintf(stderr, |
357 | _("%s: Error - expected title after " | |
0837b7e8 | 358 | "\"-t\" option."), argv[0]); |
ef416fc2 | 359 | return (1); |
360 | } | |
361 | ||
362 | title = argv[i]; | |
363 | } | |
364 | break; | |
365 | ||
366 | case 'y' : /* mode-list */ | |
367 | if (!argv[i][2]) | |
368 | { | |
369 | i ++; | |
370 | ||
371 | if (i >= argc) | |
372 | { | |
fa73b229 | 373 | _cupsLangPrintf(stderr, |
374 | _("%s: Error - expected mode list after " | |
0837b7e8 | 375 | "\"-y\" option."), argv[0]); |
ef416fc2 | 376 | return (1); |
377 | } | |
378 | } | |
379 | ||
fa73b229 | 380 | _cupsLangPrintf(stderr, |
0837b7e8 | 381 | _("%s: Warning - mode option ignored."), argv[0]); |
ef416fc2 | 382 | break; |
383 | ||
384 | case 'H' : /* Hold job */ | |
385 | if (argv[i][2]) | |
386 | val = argv[i] + 2; | |
387 | else | |
388 | { | |
389 | i ++; | |
390 | ||
391 | if (i >= argc) | |
392 | { | |
fa73b229 | 393 | _cupsLangPrintf(stderr, |
394 | _("%s: Error - expected hold name after " | |
0837b7e8 | 395 | "\"-H\" option."), argv[0]); |
ef416fc2 | 396 | return (1); |
397 | } | |
398 | ||
399 | val = argv[i]; | |
400 | } | |
401 | ||
402 | if (!strcmp(val, "hold")) | |
403 | num_options = cupsAddOption("job-hold-until", "indefinite", | |
404 | num_options, &options); | |
405 | else if (!strcmp(val, "resume") || | |
406 | !strcmp(val, "release")) | |
407 | num_options = cupsAddOption("job-hold-until", "no-hold", | |
408 | num_options, &options); | |
409 | else if (!strcmp(val, "immediate")) | |
52f6f666 MS |
410 | { |
411 | num_options = cupsAddOption("job-hold-until", "no-hold", | |
412 | num_options, &options); | |
ef416fc2 | 413 | num_options = cupsAddOption("job-priority", "100", |
414 | num_options, &options); | |
52f6f666 | 415 | } |
ef416fc2 | 416 | else if (!strcmp(val, "restart")) |
417 | { | |
418 | if (job_id < 1) | |
419 | { | |
fa73b229 | 420 | _cupsLangPrintf(stderr, |
0837b7e8 MS |
421 | _("%s: Need job ID (\"-i jobid\") before " |
422 | "\"-H restart\"."), argv[0]); | |
ef416fc2 | 423 | return (1); |
424 | } | |
425 | ||
fa73b229 | 426 | if (restart_job(argv[0], job_id)) |
ef416fc2 | 427 | return (1); |
428 | } | |
429 | else | |
430 | num_options = cupsAddOption("job-hold-until", val, | |
431 | num_options, &options); | |
432 | break; | |
433 | ||
434 | case 'P' : /* Page list */ | |
435 | if (argv[i][2]) | |
436 | val = argv[i] + 2; | |
437 | else | |
438 | { | |
439 | i ++; | |
440 | ||
441 | if (i >= argc) | |
442 | { | |
fa73b229 | 443 | _cupsLangPrintf(stderr, |
444 | _("%s: Error - expected page list after " | |
0837b7e8 | 445 | "\"-P\" option."), argv[0]); |
ef416fc2 | 446 | return (1); |
447 | } | |
448 | ||
449 | val = argv[i]; | |
450 | } | |
451 | ||
452 | num_options = cupsAddOption("page-ranges", val, num_options, | |
453 | &options); | |
454 | break; | |
455 | ||
456 | case 'S' : /* character set */ | |
457 | if (!argv[i][2]) | |
458 | { | |
459 | i ++; | |
460 | ||
461 | if (i >= argc) | |
462 | { | |
fa73b229 | 463 | _cupsLangPrintf(stderr, |
464 | _("%s: Error - expected character set after " | |
0837b7e8 | 465 | "\"-S\" option."), argv[0]); |
ef416fc2 | 466 | return (1); |
467 | } | |
468 | } | |
469 | ||
fa73b229 | 470 | _cupsLangPrintf(stderr, |
0837b7e8 | 471 | _("%s: Warning - character set option ignored."), |
fa73b229 | 472 | argv[0]); |
ef416fc2 | 473 | break; |
474 | ||
475 | case 'T' : /* Content-Type */ | |
476 | if (!argv[i][2]) | |
477 | { | |
478 | i ++; | |
479 | ||
480 | if (i >= argc) | |
481 | { | |
fa73b229 | 482 | _cupsLangPrintf(stderr, |
483 | _("%s: Error - expected content type after " | |
0837b7e8 | 484 | "\"-T\" option."), argv[0]); |
ef416fc2 | 485 | return (1); |
486 | } | |
487 | } | |
488 | ||
fa73b229 | 489 | _cupsLangPrintf(stderr, |
0837b7e8 | 490 | _("%s: Warning - content type option ignored."), |
fa73b229 | 491 | argv[0]); |
492 | break; | |
493 | ||
494 | case '-' : /* Stop processing options */ | |
db8b865d MS |
495 | if (argv[i][2]) |
496 | { | |
497 | _cupsLangPrintf(stderr, _("%s: Error - unknown option \"%s\"."), | |
498 | argv[0], argv[i]); | |
499 | return (1); | |
500 | } | |
501 | ||
fa73b229 | 502 | end_options = 1; |
ef416fc2 | 503 | break; |
504 | ||
505 | default : | |
0837b7e8 | 506 | _cupsLangPrintf(stderr, _("%s: Error - unknown option \"%c\"."), |
fa73b229 | 507 | argv[0], argv[i][1]); |
ef416fc2 | 508 | return (1); |
509 | } | |
510 | else if (!strcmp(argv[i], "-")) | |
511 | { | |
512 | if (num_files || job_id) | |
513 | { | |
fa73b229 | 514 | _cupsLangPrintf(stderr, |
515 | _("%s: Error - cannot print from stdin if files or a " | |
0837b7e8 | 516 | "job ID are provided."), argv[0]); |
ef416fc2 | 517 | return (1); |
518 | } | |
519 | ||
520 | break; | |
521 | } | |
522 | else if (num_files < 1000 && job_id == 0) | |
523 | { | |
524 | /* | |
525 | * Print a file... | |
526 | */ | |
527 | ||
528 | if (access(argv[i], R_OK) != 0) | |
529 | { | |
0837b7e8 | 530 | _cupsLangPrintf(stderr, _("%s: Error - unable to access \"%s\" - %s"), |
fa73b229 | 531 | argv[0], argv[i], strerror(errno)); |
ef416fc2 | 532 | return (1); |
533 | } | |
534 | ||
535 | files[num_files] = argv[i]; | |
536 | num_files ++; | |
537 | ||
538 | if (title == NULL) | |
539 | { | |
540 | if ((title = strrchr(argv[i], '/')) != NULL) | |
541 | title ++; | |
542 | else | |
543 | title = argv[i]; | |
544 | } | |
545 | } | |
546 | else | |
0837b7e8 | 547 | _cupsLangPrintf(stderr, _("%s: Error - too many files - \"%s\"."), |
fa73b229 | 548 | argv[0], argv[i]); |
ef416fc2 | 549 | |
550 | /* | |
551 | * See if we are altering an existing job... | |
552 | */ | |
553 | ||
554 | if (job_id) | |
fa73b229 | 555 | return (set_job_attrs(argv[0], job_id, num_options, options)); |
ef416fc2 | 556 | |
557 | /* | |
558 | * See if we have any files to print; if not, print from stdin... | |
559 | */ | |
560 | ||
561 | if (printer == NULL) | |
562 | { | |
a4924f6c | 563 | if ((dest = cupsGetNamedDest(NULL, NULL, NULL)) != NULL) |
ef416fc2 | 564 | { |
565 | printer = dest->name; | |
566 | ||
567 | for (j = 0; j < dest->num_options; j ++) | |
568 | if (cupsGetOption(dest->options[j].name, num_options, options) == NULL) | |
569 | num_options = cupsAddOption(dest->options[j].name, | |
570 | dest->options[j].value, | |
571 | num_options, &options); | |
572 | } | |
c606bcae MS |
573 | else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || |
574 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) | |
575 | { | |
576 | _cupsLangPrintf(stderr, | |
577 | _("%s: Error - add '/version=1.1' to server " | |
578 | "name."), argv[0]); | |
579 | return (1); | |
580 | } | |
ef416fc2 | 581 | } |
582 | ||
583 | if (printer == NULL) | |
584 | { | |
585 | val = NULL; | |
586 | ||
587 | if ((printer = getenv("LPDEST")) == NULL) | |
588 | { | |
589 | if ((printer = getenv("PRINTER")) != NULL) | |
590 | { | |
591 | if (!strcmp(printer, "lp")) | |
592 | printer = NULL; | |
593 | else | |
594 | val = "PRINTER"; | |
595 | } | |
596 | } | |
597 | else | |
598 | val = "LPDEST"; | |
599 | ||
a4924f6c | 600 | if (printer && !cupsGetNamedDest(NULL, printer, NULL)) |
fa73b229 | 601 | _cupsLangPrintf(stderr, |
602 | _("%s: Error - %s environment variable names " | |
0837b7e8 MS |
603 | "non-existent destination \"%s\"."), argv[0], val, |
604 | printer); | |
ef416fc2 | 605 | else if (cupsLastError() == IPP_NOT_FOUND) |
fa73b229 | 606 | _cupsLangPrintf(stderr, |
0837b7e8 | 607 | _("%s: Error - no default destination available."), |
fa73b229 | 608 | argv[0]); |
ef416fc2 | 609 | else |
0837b7e8 | 610 | _cupsLangPrintf(stderr, _("%s: Error - scheduler not responding."), |
fa73b229 | 611 | argv[0]); |
ef416fc2 | 612 | |
613 | return (1); | |
614 | } | |
615 | ||
616 | if (num_files > 0) | |
617 | job_id = cupsPrintFiles(printer, num_files, files, title, num_options, options); | |
3d052e43 MS |
618 | else if ((job_id = cupsCreateJob(CUPS_HTTP_DEFAULT, printer, |
619 | title ? title : "(stdin)", | |
620 | num_options, options)) > 0) | |
ef416fc2 | 621 | { |
3d052e43 MS |
622 | http_status_t status; /* Write status */ |
623 | const char *format; /* Document format */ | |
624 | ssize_t bytes; /* Bytes read */ | |
ef416fc2 | 625 | |
3d052e43 MS |
626 | if (cupsGetOption("raw", num_options, options)) |
627 | format = CUPS_FORMAT_RAW; | |
628 | else if ((format = cupsGetOption("document-format", num_options, | |
629 | options)) == NULL) | |
630 | format = CUPS_FORMAT_AUTO; | |
631 | ||
632 | status = cupsStartDocument(CUPS_HTTP_DEFAULT, printer, job_id, NULL, | |
633 | format, 1); | |
ef416fc2 | 634 | |
3d052e43 MS |
635 | while (status == HTTP_CONTINUE && |
636 | (bytes = read(0, buffer, sizeof(buffer))) > 0) | |
1a18c85c | 637 | status = cupsWriteRequestData(CUPS_HTTP_DEFAULT, buffer, (size_t)bytes); |
3d052e43 MS |
638 | |
639 | if (status != HTTP_CONTINUE) | |
ef416fc2 | 640 | { |
0837b7e8 | 641 | _cupsLangPrintf(stderr, _("%s: Error - unable to queue from stdin - %s."), |
3d052e43 | 642 | argv[0], httpStatus(status)); |
12f89d24 MS |
643 | cupsFinishDocument(CUPS_HTTP_DEFAULT, printer); |
644 | cupsCancelJob2(CUPS_HTTP_DEFAULT, printer, job_id, 0); | |
ef416fc2 | 645 | return (1); |
646 | } | |
647 | ||
3d052e43 | 648 | if (cupsFinishDocument(CUPS_HTTP_DEFAULT, printer) != IPP_OK) |
12f89d24 | 649 | { |
a29fd7dd | 650 | _cupsLangPrintf(stderr, "%s: %s", argv[0], cupsLastErrorString()); |
12f89d24 | 651 | cupsCancelJob2(CUPS_HTTP_DEFAULT, printer, job_id, 0); |
a29fd7dd | 652 | return (1); |
12f89d24 | 653 | } |
ef416fc2 | 654 | } |
655 | ||
656 | if (job_id < 1) | |
657 | { | |
0837b7e8 | 658 | _cupsLangPrintf(stderr, "%s: %s", argv[0], cupsLastErrorString()); |
ef416fc2 | 659 | return (1); |
660 | } | |
661 | else if (!silent) | |
0837b7e8 | 662 | _cupsLangPrintf(stdout, _("request id is %s-%d (%d file(s))"), |
ef416fc2 | 663 | printer, job_id, num_files); |
664 | ||
665 | return (0); | |
666 | } | |
667 | ||
668 | ||
669 | /* | |
670 | * 'restart_job()' - Restart a job. | |
671 | */ | |
672 | ||
673 | int /* O - Exit status */ | |
fa73b229 | 674 | restart_job(const char *command, /* I - Command name */ |
675 | int job_id) /* I - Job ID */ | |
ef416fc2 | 676 | { |
fa73b229 | 677 | ipp_t *request; /* IPP request */ |
ef416fc2 | 678 | char uri[HTTP_MAX_URI]; /* URI for job */ |
679 | ||
680 | ||
fa73b229 | 681 | request = ippNewRequest(IPP_RESTART_JOB); |
ef416fc2 | 682 | |
683 | sprintf(uri, "ipp://localhost/jobs/%d", job_id); | |
684 | ||
685 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
686 | "job-uri", NULL, uri); | |
687 | ||
688 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
689 | "requesting-user-name", NULL, cupsUser()); | |
690 | ||
3d052e43 | 691 | ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/jobs")); |
ef416fc2 | 692 | |
c606bcae MS |
693 | if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || |
694 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) | |
695 | { | |
696 | _cupsLangPrintf(stderr, | |
697 | _("%s: Error - add '/version=1.1' to server " | |
698 | "name."), command); | |
699 | return (1); | |
700 | } | |
701 | else if (cupsLastError() > IPP_OK_CONFLICT) | |
ef416fc2 | 702 | { |
0837b7e8 | 703 | _cupsLangPrintf(stderr, "%s: %s", command, cupsLastErrorString()); |
ef416fc2 | 704 | return (1); |
705 | } | |
706 | ||
707 | return (0); | |
708 | } | |
709 | ||
710 | ||
711 | /* | |
712 | * 'set_job_attrs()' - Set job attributes. | |
713 | */ | |
714 | ||
715 | int /* O - Exit status */ | |
fa73b229 | 716 | set_job_attrs(const char *command, /* I - Command name */ |
717 | int job_id, /* I - Job ID */ | |
ef416fc2 | 718 | int num_options,/* I - Number of options */ |
719 | cups_option_t *options) /* I - Options */ | |
720 | { | |
fa73b229 | 721 | ipp_t *request; /* IPP request */ |
ef416fc2 | 722 | char uri[HTTP_MAX_URI]; /* URI for job */ |
723 | ||
724 | ||
725 | if (num_options == 0) | |
726 | return (0); | |
727 | ||
fa73b229 | 728 | request = ippNewRequest(IPP_SET_JOB_ATTRIBUTES); |
ef416fc2 | 729 | |
730 | sprintf(uri, "ipp://localhost/jobs/%d", job_id); | |
731 | ||
732 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
733 | "job-uri", NULL, uri); | |
734 | ||
735 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
736 | "requesting-user-name", NULL, cupsUser()); | |
737 | ||
738 | cupsEncodeOptions(request, num_options, options); | |
739 | ||
3d052e43 | 740 | ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/jobs")); |
ef416fc2 | 741 | |
c606bcae MS |
742 | if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST || |
743 | cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) | |
744 | { | |
745 | _cupsLangPrintf(stderr, | |
746 | _("%s: Error - add '/version=1.1' to server " | |
747 | "name."), command); | |
748 | return (1); | |
749 | } | |
750 | else if (cupsLastError() > IPP_OK_CONFLICT) | |
ef416fc2 | 751 | { |
0837b7e8 | 752 | _cupsLangPrintf(stderr, "%s: %s", command, cupsLastErrorString()); |
ef416fc2 | 753 | return (1); |
754 | } | |
755 | ||
756 | return (0); | |
757 | } | |
758 | ||
759 | ||
ef416fc2 | 760 | /* |
1a18c85c | 761 | * End of "$Id: lp.c 11558 2014-02-06 18:33:34Z msweet $". |
ef416fc2 | 762 | */ |