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