]> git.ipfire.org Git - thirdparty/cups.git/blame - systemv/lp.c
Merge changes from CUPS 1.5svn-r9641
[thirdparty/cups.git] / systemv / lp.c
CommitLineData
ef416fc2 1/*
3d052e43 2 * "$Id: lp.c 7170 2008-01-04 02:21:30Z mike $"
ef416fc2 3 *
71e16022 4 * "lp" command for CUPS.
ef416fc2 5 *
84315f46 6 * Copyright 2007-2011 by Apple Inc.
7594b224 7 * Copyright 1997-2007 by Easy Software Products.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 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 * Contents:
16 *
17 * main() - Parse options and send files for printing.
18 * restart_job() - Restart a job.
19 * set_job_attrs() - Set job attributes.
ef416fc2 20 */
21
22/*
23 * Include necessary headers...
24 */
25
71e16022 26#include <cups/cups-private.h>
ef416fc2 27
28
ef416fc2 29/*
30 * Local functions.
31 */
32
fa73b229 33int restart_job(const char *command, int job_id);
34int set_job_attrs(const char *command, int job_id, int num_options,
35 cups_option_t *options);
ef416fc2 36
37
ef416fc2 38/*
39 * 'main()' - Parse options and send files for printing.
40 */
41
42int
43main(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 */
a4924f6c 56 cups_dest_t *dest; /* Selected destination */
ef416fc2 57 int num_options; /* Number of options */
58 cups_option_t *options; /* Options */
fa73b229 59 int end_options; /* No more options? */
ef416fc2 60 int silent; /* Silent or verbose output? */
61 char buffer[8192]; /* Copy buffer */
ef416fc2 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
07725fee 82 _cupsSetLocale(argv);
d09495fa 83
ef416fc2 84 silent = 0;
85 printer = NULL;
a4924f6c 86 dest = NULL;
ef416fc2 87 num_options = 0;
88 options = NULL;
89 num_files = 0;
90 title = NULL;
91 job_id = 0;
fa73b229 92 end_options = 0;
ef416fc2 93
94 for (i = 1; i < argc; i ++)
fa73b229 95 if (argv[i][0] == '-' && argv[i][1] && !end_options)
ef416fc2 96 switch (argv[i][1])
97 {
98 case 'E' : /* Encrypt */
99#ifdef HAVE_SSL
100 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
101#else
0837b7e8 102 _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."),
ef416fc2 103 argv[0]);
104#endif /* HAVE_SSL */
105 break;
106
fa73b229 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,
0837b7e8
MS
116 _("%s: Error - expected username after \"-U\" "
117 "option."), argv[0]);
fa73b229 118 return (1);
119 }
120
121 cupsSetUser(argv[i]);
122 }
123 break;
124
ef416fc2 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 {
fa73b229 137 _cupsLangPrintf(stderr,
138 _("%s: Error - expected destination after "
0837b7e8 139 "\"-d\" option."), argv[0]);
ef416fc2 140 return (1);
141 }
142
143 printer = argv[i];
144 }
145
146 if ((instance = strrchr(printer, '/')) != NULL)
147 *instance++ = '\0';
148
a4924f6c 149 if ((dest = cupsGetNamedDest(NULL, printer, instance)) != NULL)
ef416fc2 150 {
151 for (j = 0; j < dest->num_options; j ++)
0837b7e8
MS
152 if (cupsGetOption(dest->options[j].name, num_options,
153 options) == NULL)
ef416fc2 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 {
fa73b229 167 _cupsLangPrintf(stderr,
0837b7e8
MS
168 _("%s: Error - expected form after \"-f\" "
169 "option."),
fa73b229 170 argv[0]);
ef416fc2 171 return (1);
172 }
173 }
174
0837b7e8 175 _cupsLangPrintf(stderr, _("%s: Warning - form option ignored."),
fa73b229 176 argv[0]);
ef416fc2 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 {
fa73b229 188 _cupsLangPrintf(stderr,
189 _("%s: Error - expected hostname after "
0837b7e8 190 "\"-h\" option."), argv[0]);
ef416fc2 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 {
fa73b229 207 _cupsLangPrintf(stderr,
0837b7e8 208 _("%s: Expected job ID after \"-i\" option."),
fa73b229 209 argv[0]);
ef416fc2 210 return (1);
211 }
212
213 val = argv[i];
214 }
215
216 if (num_files > 0)
217 {
fa73b229 218 _cupsLangPrintf(stderr,
219 _("%s: Error - cannot print files and alter "
0837b7e8 220 "jobs simultaneously."), argv[0]);
ef416fc2 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 {
0837b7e8 231 _cupsLangPrintf(stderr, _("%s: Error - bad job ID."), argv[0]);
ef416fc2 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 */
fa73b229 241 {
242 char email[1024]; /* EMail address */
243
244
245 snprintf(email, sizeof(email), "mailto:%s@%s", cupsUser(),
757d2cad 246 httpGetHostname(NULL, buffer, sizeof(buffer)));
7594b224 247 num_options = cupsAddOption("notify-recipient-uri", email,
fa73b229 248 num_options, &options);
249 }
250
251 silent = 1;
ef416fc2 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 {
fa73b229 263 _cupsLangPrintf(stderr,
264 _("%s: Error - expected copies after "
0837b7e8 265 "\"-n\" option."), argv[0]);
ef416fc2 266 return (1);
267 }
268
269 num_copies = atoi(argv[i]);
270 }
271
272 sprintf(buffer, "%d", num_copies);
0837b7e8
MS
273 num_options = cupsAddOption("copies", buffer, num_options,
274 &options);
ef416fc2 275 break;
276
277 case 'o' : /* Option */
278 if (argv[i][2] != '\0')
0837b7e8
MS
279 num_options = cupsParseOptions(argv[i] + 2, num_options,
280 &options);
ef416fc2 281 else
282 {
283 i ++;
284
285 if (i >= argc)
286 {
fa73b229 287 _cupsLangPrintf(stderr,
84315f46 288 _("%s: Error - expected option=value after "
0837b7e8 289 "\"-o\" option."), argv[0]);
ef416fc2 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 {
fa73b229 307 _cupsLangPrintf(stderr,
308 _("%s: Error - expected priority after "
0837b7e8 309 "\"-%c\" option."), argv[0], argv[i][1]);
ef416fc2 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 {
fa73b229 329 _cupsLangPrintf(stderr,
330 _("%s: Error - priority must be between 1 and "
0837b7e8 331 "100."), argv[0]);
ef416fc2 332 return (1);
333 }
334
335 sprintf(buffer, "%d", priority);
0837b7e8
MS
336 num_options = cupsAddOption("job-priority", buffer, num_options,
337 &options);
ef416fc2 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 {
fa73b229 353 _cupsLangPrintf(stderr,
354 _("%s: Error - expected title after "
0837b7e8 355 "\"-t\" option."), argv[0]);
ef416fc2 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 {
fa73b229 370 _cupsLangPrintf(stderr,
371 _("%s: Error - expected mode list after "
0837b7e8 372 "\"-y\" option."), argv[0]);
ef416fc2 373 return (1);
374 }
375 }
376
fa73b229 377 _cupsLangPrintf(stderr,
0837b7e8 378 _("%s: Warning - mode option ignored."), argv[0]);
ef416fc2 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 {
fa73b229 390 _cupsLangPrintf(stderr,
391 _("%s: Error - expected hold name after "
0837b7e8 392 "\"-H\" option."), argv[0]);
ef416fc2 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"))
52f6f666
MS
407 {
408 num_options = cupsAddOption("job-hold-until", "no-hold",
409 num_options, &options);
ef416fc2 410 num_options = cupsAddOption("job-priority", "100",
411 num_options, &options);
52f6f666 412 }
ef416fc2 413 else if (!strcmp(val, "restart"))
414 {
415 if (job_id < 1)
416 {
fa73b229 417 _cupsLangPrintf(stderr,
0837b7e8
MS
418 _("%s: Need job ID (\"-i jobid\") before "
419 "\"-H restart\"."), argv[0]);
ef416fc2 420 return (1);
421 }
422
fa73b229 423 if (restart_job(argv[0], job_id))
ef416fc2 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 {
fa73b229 440 _cupsLangPrintf(stderr,
441 _("%s: Error - expected page list after "
0837b7e8 442 "\"-P\" option."), argv[0]);
ef416fc2 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 {
fa73b229 460 _cupsLangPrintf(stderr,
461 _("%s: Error - expected character set after "
0837b7e8 462 "\"-S\" option."), argv[0]);
ef416fc2 463 return (1);
464 }
465 }
466
fa73b229 467 _cupsLangPrintf(stderr,
0837b7e8 468 _("%s: Warning - character set option ignored."),
fa73b229 469 argv[0]);
ef416fc2 470 break;
471
472 case 'T' : /* Content-Type */
473 if (!argv[i][2])
474 {
475 i ++;
476
477 if (i >= argc)
478 {
fa73b229 479 _cupsLangPrintf(stderr,
480 _("%s: Error - expected content type after "
0837b7e8 481 "\"-T\" option."), argv[0]);
ef416fc2 482 return (1);
483 }
484 }
485
fa73b229 486 _cupsLangPrintf(stderr,
0837b7e8 487 _("%s: Warning - content type option ignored."),
fa73b229 488 argv[0]);
489 break;
490
491 case '-' : /* Stop processing options */
492 end_options = 1;
ef416fc2 493 break;
494
495 default :
0837b7e8 496 _cupsLangPrintf(stderr, _("%s: Error - unknown option \"%c\"."),
fa73b229 497 argv[0], argv[i][1]);
ef416fc2 498 return (1);
499 }
500 else if (!strcmp(argv[i], "-"))
501 {
502 if (num_files || job_id)
503 {
fa73b229 504 _cupsLangPrintf(stderr,
505 _("%s: Error - cannot print from stdin if files or a "
0837b7e8 506 "job ID are provided."), argv[0]);
ef416fc2 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 {
0837b7e8 520 _cupsLangPrintf(stderr, _("%s: Error - unable to access \"%s\" - %s"),
fa73b229 521 argv[0], argv[i], strerror(errno));
ef416fc2 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
0837b7e8 537 _cupsLangPrintf(stderr, _("%s: Error - too many files - \"%s\"."),
fa73b229 538 argv[0], argv[i]);
ef416fc2 539
540 /*
541 * See if we are altering an existing job...
542 */
543
544 if (job_id)
fa73b229 545 return (set_job_attrs(argv[0], job_id, num_options, options));
ef416fc2 546
547 /*
548 * See if we have any files to print; if not, print from stdin...
549 */
550
551 if (printer == NULL)
552 {
a4924f6c 553 if ((dest = cupsGetNamedDest(NULL, NULL, NULL)) != NULL)
ef416fc2 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
a4924f6c 582 if (printer && !cupsGetNamedDest(NULL, printer, NULL))
fa73b229 583 _cupsLangPrintf(stderr,
584 _("%s: Error - %s environment variable names "
0837b7e8
MS
585 "non-existent destination \"%s\"."), argv[0], val,
586 printer);
ef416fc2 587 else if (cupsLastError() == IPP_NOT_FOUND)
fa73b229 588 _cupsLangPrintf(stderr,
0837b7e8 589 _("%s: Error - no default destination available."),
fa73b229 590 argv[0]);
ef416fc2 591 else
0837b7e8 592 _cupsLangPrintf(stderr, _("%s: Error - scheduler not responding."),
fa73b229 593 argv[0]);
ef416fc2 594
595 return (1);
596 }
597
598 if (num_files > 0)
599 job_id = cupsPrintFiles(printer, num_files, files, title, num_options, options);
3d052e43
MS
600 else if ((job_id = cupsCreateJob(CUPS_HTTP_DEFAULT, printer,
601 title ? title : "(stdin)",
602 num_options, options)) > 0)
ef416fc2 603 {
3d052e43
MS
604 http_status_t status; /* Write status */
605 const char *format; /* Document format */
606 ssize_t bytes; /* Bytes read */
ef416fc2 607
ef416fc2 608
3d052e43
MS
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);
ef416fc2 617
3d052e43
MS
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)
ef416fc2 623 {
0837b7e8 624 _cupsLangPrintf(stderr, _("%s: Error - unable to queue from stdin - %s."),
3d052e43 625 argv[0], httpStatus(status));
ef416fc2 626 return (1);
627 }
628
3d052e43
MS
629 if (cupsFinishDocument(CUPS_HTTP_DEFAULT, printer) != IPP_OK)
630 job_id = 0;
ef416fc2 631 }
632
633 if (job_id < 1)
634 {
0837b7e8 635 _cupsLangPrintf(stderr, "%s: %s", argv[0], cupsLastErrorString());
ef416fc2 636 return (1);
637 }
638 else if (!silent)
0837b7e8 639 _cupsLangPrintf(stdout, _("request id is %s-%d (%d file(s))"),
ef416fc2 640 printer, job_id, num_files);
641
642 return (0);
643}
644
645
646/*
647 * 'restart_job()' - Restart a job.
648 */
649
650int /* O - Exit status */
fa73b229 651restart_job(const char *command, /* I - Command name */
652 int job_id) /* I - Job ID */
ef416fc2 653{
fa73b229 654 ipp_t *request; /* IPP request */
ef416fc2 655 char uri[HTTP_MAX_URI]; /* URI for job */
656
657
fa73b229 658 request = ippNewRequest(IPP_RESTART_JOB);
ef416fc2 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
3d052e43 668 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/jobs"));
ef416fc2 669
fa73b229 670 if (cupsLastError() > IPP_OK_CONFLICT)
ef416fc2 671 {
0837b7e8 672 _cupsLangPrintf(stderr, "%s: %s", command, cupsLastErrorString());
ef416fc2 673 return (1);
674 }
675
676 return (0);
677}
678
679
680/*
681 * 'set_job_attrs()' - Set job attributes.
682 */
683
684int /* O - Exit status */
fa73b229 685set_job_attrs(const char *command, /* I - Command name */
686 int job_id, /* I - Job ID */
ef416fc2 687 int num_options,/* I - Number of options */
688 cups_option_t *options) /* I - Options */
689{
fa73b229 690 ipp_t *request; /* IPP request */
ef416fc2 691 char uri[HTTP_MAX_URI]; /* URI for job */
692
693
694 if (num_options == 0)
695 return (0);
696
fa73b229 697 request = ippNewRequest(IPP_SET_JOB_ATTRIBUTES);
ef416fc2 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
3d052e43 709 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/jobs"));
ef416fc2 710
fa73b229 711 if (cupsLastError() > IPP_OK_CONFLICT)
ef416fc2 712 {
0837b7e8 713 _cupsLangPrintf(stderr, "%s: %s", command, cupsLastErrorString());
ef416fc2 714 return (1);
715 }
716
717 return (0);
718}
719
720
ef416fc2 721/*
3d052e43 722 * End of "$Id: lp.c 7170 2008-01-04 02:21:30Z mike $".
ef416fc2 723 */