]>
git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lp.c
2 * "$Id: lp.c 7170 2008-01-04 02:21:30Z mike $"
4 * "lp" command for CUPS.
6 * Copyright 2007-2010 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
,
103 _("%s: Sorry, no encryption support compiled in\n"),
105 #endif /* HAVE_SSL */
108 case 'U' : /* Username */
109 if (argv
[i
][2] != '\0')
110 cupsSetUser(argv
[i
] + 2);
116 _cupsLangPrintf(stderr
,
117 _("%s: Error - expected username after "
123 cupsSetUser(argv
[i
]);
127 case 'c' : /* Copy to spool dir (always enabled) */
130 case 'd' : /* Destination printer or class */
131 if (argv
[i
][2] != '\0')
132 printer
= argv
[i
] + 2;
139 _cupsLangPrintf(stderr
,
140 _("%s: Error - expected destination after "
149 if ((instance
= strrchr(printer
, '/')) != NULL
)
152 if ((dest
= cupsGetNamedDest(NULL
, printer
, instance
)) != NULL
)
154 for (j
= 0; j
< dest
->num_options
; j
++)
155 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
156 num_options
= cupsAddOption(dest
->options
[j
].name
,
157 dest
->options
[j
].value
,
158 num_options
, &options
);
162 case 'f' : /* Form */
169 _cupsLangPrintf(stderr
,
170 _("%s: Error - expected form after \'-f\' "
177 _cupsLangPrintf(stderr
, _("%s: Warning - form option ignored\n"),
181 case 'h' : /* Destination host */
182 if (argv
[i
][2] != '\0')
183 cupsSetServer(argv
[i
] + 2);
190 _cupsLangPrintf(stderr
,
191 _("%s: Error - expected hostname after "
197 cupsSetServer(argv
[i
]);
201 case 'i' : /* Change job */
210 _cupsLangPrintf(stderr
,
211 _("%s: Expected job ID after \'-i\' option\n"),
221 _cupsLangPrintf(stderr
,
222 _("%s: Error - cannot print files and alter "
223 "jobs simultaneously\n"),
228 if (strrchr(val
, '-') != NULL
)
229 job_id
= atoi(strrchr(val
, '-') + 1);
235 _cupsLangPrintf(stderr
, _("%s: Error - bad job ID\n"), argv
[0]);
240 case 'm' : /* Send email when job is done */
242 case 'p' : /* Notify on completion */
244 case 'w' : /* Write to console or email */
246 char email
[1024]; /* EMail address */
249 snprintf(email
, sizeof(email
), "mailto:%s@%s", cupsUser(),
250 httpGetHostname(NULL
, buffer
, sizeof(buffer
)));
251 num_options
= cupsAddOption("notify-recipient-uri", email
,
252 num_options
, &options
);
258 case 'n' : /* Number of copies */
259 if (argv
[i
][2] != '\0')
260 num_copies
= atoi(argv
[i
] + 2);
267 _cupsLangPrintf(stderr
,
268 _("%s: Error - expected copies after "
274 num_copies
= atoi(argv
[i
]);
277 sprintf(buffer
, "%d", num_copies
);
278 num_options
= cupsAddOption("copies", buffer
, num_options
, &options
);
281 case 'o' : /* Option */
282 if (argv
[i
][2] != '\0')
283 num_options
= cupsParseOptions(argv
[i
] + 2, num_options
, &options
);
290 _cupsLangPrintf(stderr
,
291 _("%s: Error - expected option string after "
297 num_options
= cupsParseOptions(argv
[i
], num_options
, &options
);
302 case 'p' : /* Queue priority */
304 case 'q' : /* Queue priority */
305 if (argv
[i
][2] != '\0')
306 priority
= atoi(argv
[i
] + 2);
311 _cupsLangPrintf(stderr
,
312 _("%s: Error - expected priority after "
314 argv
[0], argv
[i
][1]);
320 priority
= atoi(argv
[i
]);
324 * For 100% Solaris compatibility, need to add:
326 * priority = 99 * (39 - priority) / 39 + 1;
328 * However, to keep CUPS lp the same across all platforms
329 * we will break compatibility this far...
332 if (priority
< 1 || priority
> 100)
334 _cupsLangPrintf(stderr
,
335 _("%s: Error - priority must be between 1 and "
341 sprintf(buffer
, "%d", priority
);
342 num_options
= cupsAddOption("job-priority", buffer
, num_options
, &options
);
345 case 's' : /* Silent */
349 case 't' : /* Title */
350 if (argv
[i
][2] != '\0')
358 _cupsLangPrintf(stderr
,
359 _("%s: Error - expected title after "
369 case 'y' : /* mode-list */
376 _cupsLangPrintf(stderr
,
377 _("%s: Error - expected mode list after "
384 _cupsLangPrintf(stderr
,
385 _("%s: Warning - mode option ignored\n"),
389 case 'H' : /* Hold job */
398 _cupsLangPrintf(stderr
,
399 _("%s: Error - expected hold name after "
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 "
433 if (restart_job(argv
[0], job_id
))
437 num_options
= cupsAddOption("job-hold-until", val
,
438 num_options
, &options
);
441 case 'P' : /* Page list */
450 _cupsLangPrintf(stderr
,
451 _("%s: Error - expected page list after "
460 num_options
= cupsAddOption("page-ranges", val
, num_options
,
464 case 'S' : /* character set */
471 _cupsLangPrintf(stderr
,
472 _("%s: Error - expected character set after "
479 _cupsLangPrintf(stderr
,
480 _("%s: Warning - character set option ignored\n"),
484 case 'T' : /* Content-Type */
491 _cupsLangPrintf(stderr
,
492 _("%s: Error - expected content type after "
499 _cupsLangPrintf(stderr
,
500 _("%s: Warning - content type option ignored\n"),
504 case '-' : /* Stop processing options */
509 _cupsLangPrintf(stderr
, _("%s: Error - unknown option \'%c\'\n"),
510 argv
[0], argv
[i
][1]);
513 else if (!strcmp(argv
[i
], "-"))
515 if (num_files
|| job_id
)
517 _cupsLangPrintf(stderr
,
518 _("%s: Error - cannot print from stdin if files or a "
519 "job ID are provided\n"),
526 else if (num_files
< 1000 && job_id
== 0)
532 if (access(argv
[i
], R_OK
) != 0)
534 _cupsLangPrintf(stderr
, _("%s: Error - unable to access \"%s\" - %s\n"),
535 argv
[0], argv
[i
], strerror(errno
));
539 files
[num_files
] = argv
[i
];
544 if ((title
= strrchr(argv
[i
], '/')) != NULL
)
551 _cupsLangPrintf(stderr
, _("%s: Error - too many files - \"%s\"\n"),
555 * See if we are altering an existing job...
559 return (set_job_attrs(argv
[0], job_id
, num_options
, options
));
562 * See if we have any files to print; if not, print from stdin...
567 if ((dest
= cupsGetNamedDest(NULL
, NULL
, NULL
)) != NULL
)
569 printer
= dest
->name
;
571 for (j
= 0; j
< dest
->num_options
; j
++)
572 if (cupsGetOption(dest
->options
[j
].name
, num_options
, options
) == NULL
)
573 num_options
= cupsAddOption(dest
->options
[j
].name
,
574 dest
->options
[j
].value
,
575 num_options
, &options
);
583 if ((printer
= getenv("LPDEST")) == NULL
)
585 if ((printer
= getenv("PRINTER")) != NULL
)
587 if (!strcmp(printer
, "lp"))
596 if (printer
&& !cupsGetNamedDest(NULL
, printer
, NULL
))
597 _cupsLangPrintf(stderr
,
598 _("%s: Error - %s environment variable names "
599 "non-existent destination \"%s\"\n"),
600 argv
[0], val
, printer
);
601 else if (cupsLastError() == IPP_NOT_FOUND
)
602 _cupsLangPrintf(stderr
,
603 _("%s: Error - no default destination available.\n"),
606 _cupsLangPrintf(stderr
,
607 _("%s: Error - scheduler not responding\n"),
614 job_id
= cupsPrintFiles(printer
, num_files
, files
, title
, num_options
, options
);
615 else if ((job_id
= cupsCreateJob(CUPS_HTTP_DEFAULT
, printer
,
616 title
? title
: "(stdin)",
617 num_options
, options
)) > 0)
619 http_status_t status
; /* Write status */
620 const char *format
; /* Document format */
621 ssize_t bytes
; /* Bytes read */
624 if (cupsGetOption("raw", num_options
, options
))
625 format
= CUPS_FORMAT_RAW
;
626 else if ((format
= cupsGetOption("document-format", num_options
,
628 format
= CUPS_FORMAT_AUTO
;
630 status
= cupsStartDocument(CUPS_HTTP_DEFAULT
, printer
, job_id
, NULL
,
633 while (status
== HTTP_CONTINUE
&&
634 (bytes
= read(0, buffer
, sizeof(buffer
))) > 0)
635 status
= cupsWriteRequestData(CUPS_HTTP_DEFAULT
, buffer
, bytes
);
637 if (status
!= HTTP_CONTINUE
)
639 _cupsLangPrintf(stderr
,
640 _("%s: Error - unable to queue from stdin - %s\n"),
641 argv
[0], httpStatus(status
));
645 if (cupsFinishDocument(CUPS_HTTP_DEFAULT
, printer
) != IPP_OK
)
651 _cupsLangPrintf(stderr
, "%s: %s\n", argv
[0], cupsLastErrorString());
655 _cupsLangPrintf(stdout
, _("request id is %s-%d (%d file(s))\n"),
656 printer
, job_id
, num_files
);
663 * 'restart_job()' - Restart a job.
666 int /* O - Exit status */
667 restart_job(const char *command
, /* I - Command name */
668 int job_id
) /* I - Job ID */
670 ipp_t
*request
; /* IPP request */
671 char uri
[HTTP_MAX_URI
]; /* URI for job */
674 request
= ippNewRequest(IPP_RESTART_JOB
);
676 sprintf(uri
, "ipp://localhost/jobs/%d", job_id
);
678 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
679 "job-uri", NULL
, uri
);
681 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
682 "requesting-user-name", NULL
, cupsUser());
684 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT
, request
, "/jobs"));
686 if (cupsLastError() > IPP_OK_CONFLICT
)
688 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
697 * 'set_job_attrs()' - Set job attributes.
700 int /* O - Exit status */
701 set_job_attrs(const char *command
, /* I - Command name */
702 int job_id
, /* I - Job ID */
703 int num_options
,/* I - Number of options */
704 cups_option_t
*options
) /* I - Options */
706 ipp_t
*request
; /* IPP request */
707 char uri
[HTTP_MAX_URI
]; /* URI for job */
710 if (num_options
== 0)
713 request
= ippNewRequest(IPP_SET_JOB_ATTRIBUTES
);
715 sprintf(uri
, "ipp://localhost/jobs/%d", job_id
);
717 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
,
718 "job-uri", NULL
, uri
);
720 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
,
721 "requesting-user-name", NULL
, cupsUser());
723 cupsEncodeOptions(request
, num_options
, options
);
725 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT
, request
, "/jobs"));
727 if (cupsLastError() > IPP_OK_CONFLICT
)
729 _cupsLangPrintf(stderr
, "%s: %s\n", command
, cupsLastErrorString());
738 * End of "$Id: lp.c 7170 2008-01-04 02:21:30Z mike $".