]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lp.c
Merge changes from CUPS 1.5svn-r9641
[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-2011 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 end_options = 1;
493 break;
494
495 default :
496 _cupsLangPrintf(stderr, _("%s: Error - unknown option \"%c\"."),
497 argv[0], argv[i][1]);
498 return (1);
499 }
500 else if (!strcmp(argv[i], "-"))
501 {
502 if (num_files || job_id)
503 {
504 _cupsLangPrintf(stderr,
505 _("%s: Error - cannot print from stdin if files or a "
506 "job ID are provided."), argv[0]);
507 return (1);
508 }
509
510 break;
511 }
512 else if (num_files < 1000 && job_id == 0)
513 {
514 /*
515 * Print a file...
516 */
517
518 if (access(argv[i], R_OK) != 0)
519 {
520 _cupsLangPrintf(stderr, _("%s: Error - unable to access \"%s\" - %s"),
521 argv[0], argv[i], strerror(errno));
522 return (1);
523 }
524
525 files[num_files] = argv[i];
526 num_files ++;
527
528 if (title == NULL)
529 {
530 if ((title = strrchr(argv[i], '/')) != NULL)
531 title ++;
532 else
533 title = argv[i];
534 }
535 }
536 else
537 _cupsLangPrintf(stderr, _("%s: Error - too many files - \"%s\"."),
538 argv[0], argv[i]);
539
540 /*
541 * See if we are altering an existing job...
542 */
543
544 if (job_id)
545 return (set_job_attrs(argv[0], job_id, num_options, options));
546
547 /*
548 * See if we have any files to print; if not, print from stdin...
549 */
550
551 if (printer == NULL)
552 {
553 if ((dest = cupsGetNamedDest(NULL, NULL, NULL)) != NULL)
554 {
555 printer = dest->name;
556
557 for (j = 0; j < dest->num_options; j ++)
558 if (cupsGetOption(dest->options[j].name, num_options, options) == NULL)
559 num_options = cupsAddOption(dest->options[j].name,
560 dest->options[j].value,
561 num_options, &options);
562 }
563 }
564
565 if (printer == NULL)
566 {
567 val = NULL;
568
569 if ((printer = getenv("LPDEST")) == NULL)
570 {
571 if ((printer = getenv("PRINTER")) != NULL)
572 {
573 if (!strcmp(printer, "lp"))
574 printer = NULL;
575 else
576 val = "PRINTER";
577 }
578 }
579 else
580 val = "LPDEST";
581
582 if (printer && !cupsGetNamedDest(NULL, printer, NULL))
583 _cupsLangPrintf(stderr,
584 _("%s: Error - %s environment variable names "
585 "non-existent destination \"%s\"."), argv[0], val,
586 printer);
587 else if (cupsLastError() == IPP_NOT_FOUND)
588 _cupsLangPrintf(stderr,
589 _("%s: Error - no default destination available."),
590 argv[0]);
591 else
592 _cupsLangPrintf(stderr, _("%s: Error - scheduler not responding."),
593 argv[0]);
594
595 return (1);
596 }
597
598 if (num_files > 0)
599 job_id = cupsPrintFiles(printer, num_files, files, title, num_options, options);
600 else if ((job_id = cupsCreateJob(CUPS_HTTP_DEFAULT, printer,
601 title ? title : "(stdin)",
602 num_options, options)) > 0)
603 {
604 http_status_t status; /* Write status */
605 const char *format; /* Document format */
606 ssize_t bytes; /* Bytes read */
607
608
609 if (cupsGetOption("raw", num_options, options))
610 format = CUPS_FORMAT_RAW;
611 else if ((format = cupsGetOption("document-format", num_options,
612 options)) == NULL)
613 format = CUPS_FORMAT_AUTO;
614
615 status = cupsStartDocument(CUPS_HTTP_DEFAULT, printer, job_id, NULL,
616 format, 1);
617
618 while (status == HTTP_CONTINUE &&
619 (bytes = read(0, buffer, sizeof(buffer))) > 0)
620 status = cupsWriteRequestData(CUPS_HTTP_DEFAULT, buffer, bytes);
621
622 if (status != HTTP_CONTINUE)
623 {
624 _cupsLangPrintf(stderr, _("%s: Error - unable to queue from stdin - %s."),
625 argv[0], httpStatus(status));
626 return (1);
627 }
628
629 if (cupsFinishDocument(CUPS_HTTP_DEFAULT, printer) != IPP_OK)
630 job_id = 0;
631 }
632
633 if (job_id < 1)
634 {
635 _cupsLangPrintf(stderr, "%s: %s", argv[0], cupsLastErrorString());
636 return (1);
637 }
638 else if (!silent)
639 _cupsLangPrintf(stdout, _("request id is %s-%d (%d file(s))"),
640 printer, job_id, num_files);
641
642 return (0);
643 }
644
645
646 /*
647 * 'restart_job()' - Restart a job.
648 */
649
650 int /* O - Exit status */
651 restart_job(const char *command, /* I - Command name */
652 int job_id) /* I - Job ID */
653 {
654 ipp_t *request; /* IPP request */
655 char uri[HTTP_MAX_URI]; /* URI for job */
656
657
658 request = ippNewRequest(IPP_RESTART_JOB);
659
660 sprintf(uri, "ipp://localhost/jobs/%d", job_id);
661
662 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
663 "job-uri", NULL, uri);
664
665 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
666 "requesting-user-name", NULL, cupsUser());
667
668 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/jobs"));
669
670 if (cupsLastError() > IPP_OK_CONFLICT)
671 {
672 _cupsLangPrintf(stderr, "%s: %s", command, cupsLastErrorString());
673 return (1);
674 }
675
676 return (0);
677 }
678
679
680 /*
681 * 'set_job_attrs()' - Set job attributes.
682 */
683
684 int /* O - Exit status */
685 set_job_attrs(const char *command, /* I - Command name */
686 int job_id, /* I - Job ID */
687 int num_options,/* I - Number of options */
688 cups_option_t *options) /* I - Options */
689 {
690 ipp_t *request; /* IPP request */
691 char uri[HTTP_MAX_URI]; /* URI for job */
692
693
694 if (num_options == 0)
695 return (0);
696
697 request = ippNewRequest(IPP_SET_JOB_ATTRIBUTES);
698
699 sprintf(uri, "ipp://localhost/jobs/%d", job_id);
700
701 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
702 "job-uri", NULL, uri);
703
704 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
705 "requesting-user-name", NULL, cupsUser());
706
707 cupsEncodeOptions(request, num_options, options);
708
709 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/jobs"));
710
711 if (cupsLastError() > IPP_OK_CONFLICT)
712 {
713 _cupsLangPrintf(stderr, "%s: %s", command, cupsLastErrorString());
714 return (1);
715 }
716
717 return (0);
718 }
719
720
721 /*
722 * End of "$Id: lp.c 7170 2008-01-04 02:21:30Z mike $".
723 */