2 * "$Id: lp.c 11101 2013-07-08 11:20:33Z msweet $"
4 * "lp" command for CUPS.
6 * Copyright 2007-2013 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
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/".
17 * main() - Parse options and send files for printing.
18 * restart_job() - Restart a job.
19 * set_job_attrs() - Set job attributes.
23 * Include necessary headers...
26 #include <cups/cups-private.h>
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
);
39 * 'main()' - Parse options and send files for printing.
43 main(int argc
, /* I - Number of command-line arguments */
44 char *argv
[]) /* I - Command-line arguments */
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 */
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...
73 if ((val
= strrchr(argv
[0], '/')) != NULL
)
78 if (!strcmp(val
, "printd"))
94 for (i
= 1; i
< argc
; i
++)
95 if (argv
[i
][0] == '-' && argv
[i
][1] && !end_options
)
98 case 'E' : /* Encrypt */
100 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
102 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."),
104 #endif /* HAVE_SSL */
107 case 'U' : /* Username */
108 if (argv
[i
][2] != '\0')
109 cupsSetUser(argv
[i
] + 2);
115 _cupsLangPrintf(stderr
,
116 _("%s: Error - expected username after \"-U\" "
117 "option."), argv
[0]);
121 cupsSetUser(argv
[i
]);
125 case 'c' : /* Copy to spool dir (always enabled) */
128 case 'd' : /* Destination printer or class */
129 if (argv
[i
][2] != '\0')
130 printer
= argv
[i
] + 2;
137 _cupsLangPrintf(stderr
,
138 _("%s: Error - expected destination after "
139 "\"-d\" option."), argv
[0]);
146 if ((instance
= strrchr(printer
, '/')) != NULL
)
149 if ((dest
= cupsGetNamedDest(CUPS_HTTP_DEFAULT
, printer
,
152 for (j
= 0; j
< dest
->num_options
; j
++)
153 if (cupsGetOption(dest
->options
[j
].name
, num_options
,
155 num_options
= cupsAddOption(dest
->options
[j
].name
,
156 dest
->options
[j
].value
,
157 num_options
, &options
);
159 else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
160 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
162 _cupsLangPrintf(stderr
,
163 _("%s: Error - add '/version=1.1' to server "
169 case 'f' : /* Form */
176 _cupsLangPrintf(stderr
,
177 _("%s: Error - expected form after \"-f\" "
184 _cupsLangPrintf(stderr
, _("%s: Warning - form option ignored."),
188 case 'h' : /* Destination host */
189 if (argv
[i
][2] != '\0')
190 cupsSetServer(argv
[i
] + 2);
197 _cupsLangPrintf(stderr
,
198 _("%s: Error - expected hostname after "
199 "\"-h\" option."), argv
[0]);
203 cupsSetServer(argv
[i
]);
207 case 'i' : /* Change job */
216 _cupsLangPrintf(stderr
,
217 _("%s: Expected job ID after \"-i\" option."),
227 _cupsLangPrintf(stderr
,
228 _("%s: Error - cannot print files and alter "
229 "jobs simultaneously."), argv
[0]);
233 if (strrchr(val
, '-') != NULL
)
234 job_id
= atoi(strrchr(val
, '-') + 1);
240 _cupsLangPrintf(stderr
, _("%s: Error - bad job ID."), argv
[0]);
245 case 'm' : /* Send email when job is done */
247 case 'p' : /* Notify on completion */
249 case 'w' : /* Write to console or email */
251 char email
[1024]; /* EMail address */
254 snprintf(email
, sizeof(email
), "mailto:%s@%s", cupsUser(),
255 httpGetHostname(NULL
, buffer
, sizeof(buffer
)));
256 num_options
= cupsAddOption("notify-recipient-uri", email
,
257 num_options
, &options
);
263 case 'n' : /* Number of copies */
264 if (argv
[i
][2] != '\0')
265 num_copies
= atoi(argv
[i
] + 2);
272 _cupsLangPrintf(stderr
,
273 _("%s: Error - expected copies after "
274 "\"-n\" option."), argv
[0]);
278 num_copies
= atoi(argv
[i
]);
281 sprintf(buffer
, "%d", num_copies
);
282 num_options
= cupsAddOption("copies", buffer
, num_options
,
286 case 'o' : /* Option */
287 if (argv
[i
][2] != '\0')
288 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
,
296 _cupsLangPrintf(stderr
,
297 _("%s: Error - expected option=value after "
298 "\"-o\" option."), argv
[0]);
302 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
307 case 'p' : /* Queue priority */
309 case 'q' : /* Queue priority */
310 if (argv
[i
][2] != '\0')
311 priority
= atoi(argv
[i
] + 2);
316 _cupsLangPrintf(stderr
,
317 _("%s: Error - expected priority after "
318 "\"-%c\" option."), argv
[0], argv
[i
][1]);
324 priority
= atoi(argv
[i
]);
328 * For 100% Solaris compatibility, need to add:
330 * priority = 99 * (39 - priority) / 39 + 1;
332 * However, to keep CUPS lp the same across all platforms
333 * we will break compatibility this far...
336 if (priority
< 1 || priority
> 100)
338 _cupsLangPrintf(stderr
,
339 _("%s: Error - priority must be between 1 and "
344 sprintf(buffer
, "%d", priority
);
345 num_options
= cupsAddOption("job-priority", buffer
, num_options
,
349 case 's' : /* Silent */
353 case 't' : /* Title */
354 if (argv
[i
][2] != '\0')
362 _cupsLangPrintf(stderr
,
363 _("%s: Error - expected title after "
364 "\"-t\" option."), argv
[0]);
372 case 'y' : /* mode-list */
379 _cupsLangPrintf(stderr
,
380 _("%s: Error - expected mode list after "
381 "\"-y\" option."), argv
[0]);
386 _cupsLangPrintf(stderr
,
387 _("%s: Warning - mode option ignored."), argv
[0]);
390 case 'H' : /* Hold job */
399 _cupsLangPrintf(stderr
,
400 _("%s: Error - expected hold name after "
401 "\"-H\" option."), argv
[0]);
408 if (!strcmp(val
, "hold"))
409 num_options
= cupsAddOption("job-hold-until", "indefinite",
410 num_options
, &options
);
411 else if (!strcmp(val
, "resume") ||
412 !strcmp(val
, "release"))
413 num_options
= cupsAddOption("job-hold-until", "no-hold",
414 num_options
, &options
);
415 else if (!strcmp(val
, "immediate"))
417 num_options
= cupsAddOption("job-hold-until", "no-hold",
418 num_options
, &options
);
419 num_options
= cupsAddOption("job-priority", "100",
420 num_options
, &options
);
422 else if (!strcmp(val
, "restart"))
426 _cupsLangPrintf(stderr
,
427 _("%s: Need job ID (\"-i jobid\") before "
428 "\"-H restart\"."), argv
[0]);
432 if (restart_job(argv
[0], job_id
))
436 num_options
= cupsAddOption("job-hold-until", val
,
437 num_options
, &options
);
440 case 'P' : /* Page list */
449 _cupsLangPrintf(stderr
,
450 _("%s: Error - expected page list after "
451 "\"-P\" option."), argv
[0]);
458 num_options
= cupsAddOption("page-ranges", val
, num_options
,
462 case 'S' : /* character set */
469 _cupsLangPrintf(stderr
,
470 _("%s: Error - expected character set after "
471 "\"-S\" option."), argv
[0]);
476 _cupsLangPrintf(stderr
,
477 _("%s: Warning - character set option ignored."),
481 case 'T' : /* Content-Type */
488 _cupsLangPrintf(stderr
,
489 _("%s: Error - expected content type after "
490 "\"-T\" option."), argv
[0]);
495 _cupsLangPrintf(stderr
,
496 _("%s: Warning - content type option ignored."),
500 case '-' : /* Stop processing options */
503 _cupsLangPrintf(stderr
, _("%s: Error - unknown option \"%s\"."),
512 _cupsLangPrintf(stderr
, _("%s: Error - unknown option \"%c\"."),
513 argv
[0], argv
[i
][1]);
516 else if (!strcmp(argv
[i
], "-"))
518 if (num_files
|| job_id
)
520 _cupsLangPrintf(stderr
,
521 _("%s: Error - cannot print from stdin if files or a "
522 "job ID are provided."), argv
[0]);
528 else if (num_files
< 1000 && job_id
== 0)
534 if (access(argv
[i
], R_OK
) != 0)
536 _cupsLangPrintf(stderr
, _("%s: Error - unable to access \"%s\" - %s"),
537 argv
[0], argv
[i
], strerror(errno
));
541 files
[num_files
] = argv
[i
];
546 if ((title
= strrchr(argv
[i
], '/')) != NULL
)
553 _cupsLangPrintf(stderr
, _("%s: Error - too many files - \"%s\"."),
557 * See if we are altering an existing job...
561 return (set_job_attrs(argv
[0], job_id
, num_options
, options
));
564 * See if we have any files to print; if not, print from stdin...
569 if ((dest
= cupsGetNamedDest(NULL
, NULL
, NULL
)) != NULL
)
571 printer
= dest
->name
;
573 for (j
= 0; j
< dest
->num_options
; j
++)
574 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
575 num_options
= cupsAddOption(dest
->options
[j
].name
,
576 dest
->options
[j
].value
,
577 num_options
, &options
);
579 else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
580 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
582 _cupsLangPrintf(stderr
,
583 _("%s: Error - add '/version=1.1' to server "
593 if ((printer
= getenv("LPDEST")) == NULL
)
595 if ((printer
= getenv("PRINTER")) != NULL
)
597 if (!strcmp(printer
, "lp"))
606 if (printer
&& !cupsGetNamedDest(NULL
, printer
, NULL
))
607 _cupsLangPrintf(stderr
,
608 _("%s: Error - %s environment variable names "
609 "non-existent destination \"%s\"."), argv
[0], val
,
611 else if (cupsLastError() == IPP_NOT_FOUND
)
612 _cupsLangPrintf(stderr
,
613 _("%s: Error - no default destination available."),
616 _cupsLangPrintf(stderr
, _("%s: Error - scheduler not responding."),
623 job_id
= cupsPrintFiles(printer
, num_files
, files
, title
, num_options
, options
);
624 else if ((job_id
= cupsCreateJob(CUPS_HTTP_DEFAULT
, printer
,
625 title
? title
: "(stdin)",
626 num_options
, options
)) > 0)
628 http_status_t status
; /* Write status */
629 const char *format
; /* Document format */
630 ssize_t bytes
; /* Bytes read */
632 if (cupsGetOption("raw", num_options
, options
))
633 format
= CUPS_FORMAT_RAW
;
634 else if ((format
= cupsGetOption("document-format", num_options
,
636 format
= CUPS_FORMAT_AUTO
;
638 status
= cupsStartDocument(CUPS_HTTP_DEFAULT
, printer
, job_id
, NULL
,
641 while (status
== HTTP_CONTINUE
&&
642 (bytes
= read(0, buffer
, sizeof(buffer
))) > 0)
643 status
= cupsWriteRequestData(CUPS_HTTP_DEFAULT
, buffer
, bytes
);
645 if (status
!= HTTP_CONTINUE
)
647 _cupsLangPrintf(stderr
, _("%s: Error - unable to queue from stdin - %s."),
648 argv
[0], httpStatus(status
));
649 cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
);
650 cupsCancelJob2(CUPS_HTTP_DEFAULT
, printer
, job_id
, 0);
654 if (cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
) != IPP_OK
)
656 _cupsLangPrintf(stderr
, "%s: %s", argv
[0], cupsLastErrorString());
657 cupsCancelJob2(CUPS_HTTP_DEFAULT
, printer
, job_id
, 0);
664 _cupsLangPrintf(stderr
, "%s: %s", argv
[0], cupsLastErrorString());
668 _cupsLangPrintf(stdout
, _("request id is %s-%d (%d file(s))"),
669 printer
, job_id
, num_files
);
676 * 'restart_job()' - Restart a job.
679 int /* O - Exit status */
680 restart_job(const char *command
, /* I - Command name */
681 int job_id
) /* I - Job ID */
683 ipp_t
*request
; /* IPP request */
684 char uri
[HTTP_MAX_URI
]; /* URI for job */
687 request
= ippNewRequest(IPP_RESTART_JOB
);
689 sprintf(uri
, "ipp://localhost/jobs/%d", job_id
);
691 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
692 "job-uri", NULL
, uri
);
694 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
695 "requesting-user-name", NULL
, cupsUser());
697 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT
, request
, "/jobs"));
699 if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
700 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
702 _cupsLangPrintf(stderr
,
703 _("%s: Error - add '/version=1.1' to server "
707 else if (cupsLastError() > IPP_OK_CONFLICT
)
709 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
718 * 'set_job_attrs()' - Set job attributes.
721 int /* O - Exit status */
722 set_job_attrs(const char *command
, /* I - Command name */
723 int job_id
, /* I - Job ID */
724 int num_options
,/* I - Number of options */
725 cups_option_t
*options
) /* I - Options */
727 ipp_t
*request
; /* IPP request */
728 char uri
[HTTP_MAX_URI
]; /* URI for job */
731 if (num_options
== 0)
734 request
= ippNewRequest(IPP_SET_JOB_ATTRIBUTES
);
736 sprintf(uri
, "ipp://localhost/jobs/%d", job_id
);
738 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
739 "job-uri", NULL
, uri
);
741 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
742 "requesting-user-name", NULL
, cupsUser());
744 cupsEncodeOptions(request
, num_options
, options
);
746 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT
, request
, "/jobs"));
748 if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
749 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
751 _cupsLangPrintf(stderr
,
752 _("%s: Error - add '/version=1.1' to server "
756 else if (cupsLastError() > IPP_OK_CONFLICT
)
758 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
767 * End of "$Id: lp.c 11101 2013-07-08 11:20:33Z msweet $".