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