2 * "lp" command for CUPS.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
14 #include <cups/cups-private.h>
21 int restart_job(const char *command
, int job_id
);
22 int set_job_attrs(const char *command
, int job_id
, int num_options
,
23 cups_option_t
*options
);
27 * 'main()' - Parse options and send files for printing.
31 main(int argc
, /* I - Number of command-line arguments */
32 char *argv
[]) /* I - Command-line arguments */
34 int i
, j
; /* Looping vars */
35 int job_id
; /* Job ID */
36 char *printer
, /* Printer name */
37 *instance
, /* Instance name */
38 *opt
, /* Option pointer */
39 *val
, /* Option value */
40 *title
; /* Job title */
41 int priority
; /* Job priority (1-100) */
42 int num_copies
; /* Number of copies per file */
43 int num_files
; /* Number of files to print */
44 const char *files
[1000]; /* Files to print */
45 cups_dest_t
*dest
; /* Selected destination */
46 int num_options
; /* Number of options */
47 cups_option_t
*options
; /* Options */
48 int end_options
; /* No more options? */
49 int silent
; /* Silent or verbose output? */
50 char buffer
[8192]; /* Copy buffer */
55 * Solaris does some rather strange things to re-queue remote print
56 * jobs. On bootup, the "lp" command is run as "printd" to re-spool
57 * any remote jobs in /var/spool/print. Since CUPS doesn't need this
58 * nonsense, we just need to add the necessary check here to prevent
59 * lp from causing boot problems...
62 if ((val
= strrchr(argv
[0], '/')) != NULL
)
67 if (!strcmp(val
, "printd"))
83 for (i
= 1; i
< argc
; i
++)
85 if (argv
[i
][0] == '-' && argv
[i
][1] && !end_options
)
87 for (opt
= argv
[i
] + 1; *opt
; opt
++)
91 case 'E' : /* Encrypt */
93 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED
);
95 _cupsLangPrintf(stderr
, _("%s: Sorry, no encryption support."), argv
[0]);
99 case 'U' : /* Username */
102 cupsSetUser(opt
+ 1);
103 opt
+= strlen(opt
) - 1;
110 _cupsLangPrintf(stderr
, _("%s: Error - expected username after \"-U\" option."), argv
[0]);
114 cupsSetUser(argv
[i
]);
118 case 'c' : /* Copy to spool dir (always enabled) */
121 case 'd' : /* Destination printer or class */
125 opt
+= strlen(opt
) - 1;
133 _cupsLangPrintf(stderr
, _("%s: Error - expected destination after \"-d\" option."), argv
[0]);
140 if ((instance
= strrchr(printer
, '/')) != NULL
)
143 if ((dest
= cupsGetNamedDest(CUPS_HTTP_DEFAULT
, printer
,
146 for (j
= 0; j
< dest
->num_options
; j
++)
147 if (cupsGetOption(dest
->options
[j
].name
, num_options
,
149 num_options
= cupsAddOption(dest
->options
[j
].name
,
150 dest
->options
[j
].value
,
151 num_options
, &options
);
153 else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
154 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
156 _cupsLangPrintf(stderr
,
157 _("%s: Error - add '/version=1.1' to server "
163 case 'f' : /* Form */
166 opt
+= strlen(opt
) - 1;
174 _cupsLangPrintf(stderr
, _("%s: Error - expected form after \"-f\" option."), argv
[0]);
179 _cupsLangPrintf(stderr
, _("%s: Warning - form option ignored."), argv
[0]);
182 case 'h' : /* Destination host */
185 cupsSetServer(opt
+ 1);
186 opt
+= strlen(opt
) - 1;
194 _cupsLangPrintf(stderr
, _("%s: Error - expected hostname after \"-h\" option."), argv
[0]);
198 cupsSetServer(argv
[i
]);
202 case 'i' : /* Change job */
206 opt
+= strlen(opt
) - 1;
214 _cupsLangPrintf(stderr
, _("%s: Expected job ID after \"-i\" option."), argv
[0]);
223 _cupsLangPrintf(stderr
, _("%s: Error - cannot print files and alter jobs simultaneously."), argv
[0]);
227 if (strrchr(val
, '-') != NULL
)
228 job_id
= atoi(strrchr(val
, '-') + 1);
234 _cupsLangPrintf(stderr
, _("%s: Error - bad job ID."), argv
[0]);
239 case 'm' : /* Send email when job is done */
241 case 'p' : /* Notify on completion */
243 case 'w' : /* Write to console or email */
245 char email
[1024]; /* EMail address */
248 snprintf(email
, sizeof(email
), "mailto:%s@%s", cupsUser(), httpGetHostname(NULL
, buffer
, sizeof(buffer
)));
249 num_options
= cupsAddOption("notify-recipient-uri", email
, num_options
, &options
);
255 case 'n' : /* Number of copies */
258 num_copies
= atoi(opt
+ 1);
259 opt
+= strlen(opt
) - 1;
267 _cupsLangPrintf(stderr
, _("%s: Error - expected copies after \"-n\" option."), argv
[0]);
271 num_copies
= atoi(argv
[i
]);
276 _cupsLangPrintf(stderr
, _("%s: Error - copies must be 1 or more."), argv
[0]);
280 sprintf(buffer
, "%d", num_copies
);
281 num_options
= cupsAddOption("copies", buffer
, num_options
,
285 case 'o' : /* Option */
288 num_options
= cupsParseOptions(opt
+ 1, num_options
, &options
);
289 opt
+= strlen(opt
) - 1;
297 _cupsLangPrintf(stderr
, _("%s: Error - expected option=value after \"-o\" option."), argv
[0]);
301 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
306 case 'p' : /* Queue priority */
308 case 'q' : /* Queue priority */
311 priority
= atoi(opt
+ 1);
312 opt
+= strlen(opt
) - 1;
318 _cupsLangPrintf(stderr
, _("%s: Error - expected priority after \"-%c\" option."), argv
[0], *opt
);
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
, _("%s: Error - priority must be between 1 and 100."), argv
[0]);
342 sprintf(buffer
, "%d", priority
);
343 num_options
= cupsAddOption("job-priority", buffer
, num_options
,
347 case 's' : /* Silent */
351 case 't' : /* Title */
355 opt
+= strlen(opt
) - 1;
363 _cupsLangPrintf(stderr
, _("%s: Error - expected title after \"-t\" option."), argv
[0]);
371 case 'y' : /* mode-list */
374 opt
+= strlen(opt
) - 1;
382 _cupsLangPrintf(stderr
, _("%s: Error - expected mode list after \"-y\" option."), argv
[0]);
387 _cupsLangPrintf(stderr
, _("%s: Warning - mode option ignored."), argv
[0]);
390 case 'H' : /* Hold job */
394 opt
+= strlen(opt
) - 1;
402 _cupsLangPrintf(stderr
, _("%s: Error - expected hold name after \"-H\" option."), argv
[0]);
409 if (!strcmp(val
, "hold"))
410 num_options
= cupsAddOption("job-hold-until", "indefinite", num_options
, &options
);
411 else if (!strcmp(val
, "resume") || !strcmp(val
, "release"))
412 num_options
= cupsAddOption("job-hold-until", "no-hold", num_options
, &options
);
413 else if (!strcmp(val
, "immediate"))
415 num_options
= cupsAddOption("job-hold-until", "no-hold", num_options
, &options
);
416 num_options
= cupsAddOption("job-priority", "100", num_options
, &options
);
418 else if (!strcmp(val
, "restart"))
422 _cupsLangPrintf(stderr
, _("%s: Need job ID (\"-i jobid\") before \"-H restart\"."), argv
[0]);
426 if (restart_job(argv
[0], job_id
))
430 num_options
= cupsAddOption("job-hold-until", val
, num_options
, &options
);
433 case 'P' : /* Page list */
437 opt
+= strlen(opt
) - 1;
445 _cupsLangPrintf(stderr
, _("%s: Error - expected page list after \"-P\" option."), argv
[0]);
452 num_options
= cupsAddOption("page-ranges", val
, num_options
, &options
);
455 case 'S' : /* character set */
458 opt
+= strlen(opt
) - 1;
466 _cupsLangPrintf(stderr
, _("%s: Error - expected character set after \"-S\" option."), argv
[0]);
471 _cupsLangPrintf(stderr
, _("%s: Warning - character set option ignored."), argv
[0]);
474 case 'T' : /* Content-Type */
477 opt
+= strlen(opt
) - 1;
485 _cupsLangPrintf(stderr
, _("%s: Error - expected content type after \"-T\" option."), argv
[0]);
490 _cupsLangPrintf(stderr
, _("%s: Warning - content type option ignored."), argv
[0]);
493 case '-' : /* Stop processing options */
496 _cupsLangPrintf(stderr
, _("%s: Error - unknown option \"%s\"."), argv
[0], argv
[i
]);
504 _cupsLangPrintf(stderr
, _("%s: Error - unknown option \"%c\"."), argv
[0], *opt
);
509 else if (!strcmp(argv
[i
], "-"))
511 if (num_files
|| job_id
)
513 _cupsLangPrintf(stderr
,
514 _("%s: Error - cannot print from stdin if files or a "
515 "job ID are provided."), argv
[0]);
521 else if (num_files
< 1000 && job_id
== 0)
527 if (access(argv
[i
], R_OK
) != 0)
529 _cupsLangPrintf(stderr
, _("%s: Error - unable to access \"%s\" - %s"), argv
[0], argv
[i
], strerror(errno
));
533 files
[num_files
] = argv
[i
];
538 if ((title
= strrchr(argv
[i
], '/')) != NULL
)
546 _cupsLangPrintf(stderr
, _("%s: Error - too many files - \"%s\"."), argv
[0], argv
[i
]);
551 * See if we are altering an existing job...
555 return (set_job_attrs(argv
[0], job_id
, num_options
, options
));
558 * See if we have any files to print; if not, print from stdin...
563 if ((dest
= cupsGetNamedDest(NULL
, NULL
, NULL
)) != NULL
)
565 printer
= dest
->name
;
567 for (j
= 0; j
< dest
->num_options
; j
++)
568 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
569 num_options
= cupsAddOption(dest
->options
[j
].name
,
570 dest
->options
[j
].value
,
571 num_options
, &options
);
573 else if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
574 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
576 _cupsLangPrintf(stderr
,
577 _("%s: Error - add '/version=1.1' to server "
585 if (!cupsGetNamedDest(NULL
, NULL
, NULL
) && cupsLastError() == IPP_STATUS_ERROR_NOT_FOUND
)
586 _cupsLangPrintf(stderr
, _("%s: Error - %s"), argv
[0], cupsLastErrorString());
588 _cupsLangPrintf(stderr
, _("%s: Error - scheduler not responding."), argv
[0]);
594 job_id
= cupsPrintFiles(printer
, num_files
, files
, title
, num_options
, options
);
595 else if ((job_id
= cupsCreateJob(CUPS_HTTP_DEFAULT
, printer
,
596 title
? title
: "(stdin)",
597 num_options
, options
)) > 0)
599 http_status_t status
; /* Write status */
600 const char *format
; /* Document format */
601 ssize_t bytes
; /* Bytes read */
603 if (cupsGetOption("raw", num_options
, options
))
604 format
= CUPS_FORMAT_RAW
;
605 else if ((format
= cupsGetOption("document-format", num_options
,
607 format
= CUPS_FORMAT_AUTO
;
609 status
= cupsStartDocument(CUPS_HTTP_DEFAULT
, printer
, job_id
, NULL
,
612 while (status
== HTTP_CONTINUE
&&
613 (bytes
= read(0, buffer
, sizeof(buffer
))) > 0)
614 status
= cupsWriteRequestData(CUPS_HTTP_DEFAULT
, buffer
, (size_t)bytes
);
616 if (status
!= HTTP_CONTINUE
)
618 _cupsLangPrintf(stderr
, _("%s: Error - unable to queue from stdin - %s."),
619 argv
[0], httpStatus(status
));
620 cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
);
621 cupsCancelJob2(CUPS_HTTP_DEFAULT
, printer
, job_id
, 0);
625 if (cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
) != IPP_OK
)
627 _cupsLangPrintf(stderr
, "%s: %s", argv
[0], cupsLastErrorString());
628 cupsCancelJob2(CUPS_HTTP_DEFAULT
, printer
, job_id
, 0);
635 _cupsLangPrintf(stderr
, "%s: %s", argv
[0], cupsLastErrorString());
639 _cupsLangPrintf(stdout
, _("request id is %s-%d (%d file(s))"),
640 printer
, job_id
, num_files
);
647 * 'restart_job()' - Restart a job.
650 int /* O - Exit status */
651 restart_job(const char *command
, /* I - Command name */
652 int job_id
) /* I - Job ID */
654 ipp_t
*request
; /* IPP request */
655 char uri
[HTTP_MAX_URI
]; /* URI for job */
658 request
= ippNewRequest(IPP_RESTART_JOB
);
660 sprintf(uri
, "ipp://localhost/jobs/%d", job_id
);
662 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
663 "job-uri", NULL
, uri
);
665 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
666 "requesting-user-name", NULL
, cupsUser());
668 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT
, request
, "/jobs"));
670 if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
671 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
673 _cupsLangPrintf(stderr
,
674 _("%s: Error - add '/version=1.1' to server "
678 else if (cupsLastError() > IPP_OK_CONFLICT
)
680 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());
689 * 'set_job_attrs()' - Set job attributes.
692 int /* O - Exit status */
693 set_job_attrs(const char *command
, /* I - Command name */
694 int job_id
, /* I - Job ID */
695 int num_options
,/* I - Number of options */
696 cups_option_t
*options
) /* I - Options */
698 ipp_t
*request
; /* IPP request */
699 char uri
[HTTP_MAX_URI
]; /* URI for job */
702 if (num_options
== 0)
705 request
= ippNewRequest(IPP_SET_JOB_ATTRIBUTES
);
707 sprintf(uri
, "ipp://localhost/jobs/%d", job_id
);
709 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
710 "job-uri", NULL
, uri
);
712 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
713 "requesting-user-name", NULL
, cupsUser());
715 cupsEncodeOptions(request
, num_options
, options
);
717 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT
, request
, "/jobs"));
719 if (cupsLastError() == IPP_STATUS_ERROR_BAD_REQUEST
||
720 cupsLastError() == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED
)
722 _cupsLangPrintf(stderr
,
723 _("%s: Error - add '/version=1.1' to server "
727 else if (cupsLastError() > IPP_OK_CONFLICT
)
729 _cupsLangPrintf(stderr
, "%s: %s", command
, cupsLastErrorString());