xref: /titanic_52/usr/src/cmd/print/bsd-sysv-commands/lp.c (revision 324104aba6acecc130e7091bab730d52f37d5a9c)
1355b4669Sjacobs /*
2355b4669Sjacobs  * CDDL HEADER START
3355b4669Sjacobs  *
4355b4669Sjacobs  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
7355b4669Sjacobs  *
8355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs  * See the License for the specific language governing permissions
11355b4669Sjacobs  * and limitations under the License.
12355b4669Sjacobs  *
13355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs  *
19355b4669Sjacobs  * CDDL HEADER END
20355b4669Sjacobs  */
21355b4669Sjacobs 
22355b4669Sjacobs /*
23*324104abSsonam gupta - Sun Microsystems - Bangalore India  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24355b4669Sjacobs  *
25355b4669Sjacobs  */
26355b4669Sjacobs 
27179184d3Sjacobs /* $Id: lp.c 179 2006-07-17 18:24:07Z njacobs $ */
28355b4669Sjacobs 
29355b4669Sjacobs #include <stdio.h>
30355b4669Sjacobs #include <stdlib.h>
31355b4669Sjacobs #include <unistd.h>
32355b4669Sjacobs #include <string.h>
33355b4669Sjacobs #include <locale.h>
34355b4669Sjacobs #include <libintl.h>
35355b4669Sjacobs #include <papi.h>
36355b4669Sjacobs #include "common.h"
37355b4669Sjacobs 
38355b4669Sjacobs #ifdef HAVE_LIBMAGIC	/* for mimetype auto-detection */
39355b4669Sjacobs #include <magic.h>
40355b4669Sjacobs #endif /* HAVE_LIBMAGIC */
41355b4669Sjacobs 
42355b4669Sjacobs static void
43355b4669Sjacobs usage(char *program)
44355b4669Sjacobs {
45355b4669Sjacobs 	char *name;
46355b4669Sjacobs 
47355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
48355b4669Sjacobs 		name = program;
49355b4669Sjacobs 	else
50355b4669Sjacobs 		name++;
51355b4669Sjacobs 
52355b4669Sjacobs 	fprintf(stdout,
53355b4669Sjacobs 	    gettext("Usage: %s [-c] [-m] [-p] [-s] [-w] [-d destination]  "
54355b4669Sjacobs 	    "[-f form-name] [-H special-handling] [-n number] "
55355b4669Sjacobs 	    "[-o option] [-P page-list] [-q priority-level]  "
56355b4669Sjacobs 	    "[-S character-set | print-wheel]  [-t title] [-v] "
57355b4669Sjacobs 	    "[-T content-type [-r]] [-y mode-list] [file...]\n"),
58355b4669Sjacobs 	    name);
59355b4669Sjacobs 	exit(1);
60355b4669Sjacobs }
61355b4669Sjacobs 
62355b4669Sjacobs int
63355b4669Sjacobs main(int ac, char *av[])
64355b4669Sjacobs {
65355b4669Sjacobs 	papi_status_t status;
66355b4669Sjacobs 	papi_service_t svc = NULL;
67355b4669Sjacobs 	papi_attribute_t **list = NULL;
68355b4669Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
69355b4669Sjacobs 	papi_job_t job = NULL;
70c1ecd8b9Sjacobs 	char prefetch[3];
71c1ecd8b9Sjacobs 	int prefetch_len = sizeof (prefetch);
72355b4669Sjacobs 	char *printer = NULL;
73355b4669Sjacobs 	char b = PAPI_TRUE;
74355b4669Sjacobs 	int copy = 0;
75355b4669Sjacobs 	int silent = 0;
76355b4669Sjacobs 	int dump = 0;
77355b4669Sjacobs 	int validate = 0;
78355b4669Sjacobs 	int modify = -1;
79355b4669Sjacobs 	int c;
80355b4669Sjacobs 
81355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
82355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
83355b4669Sjacobs 
84355b4669Sjacobs 	while ((c = getopt(ac, av, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF)
85355b4669Sjacobs 		switch (c) {
86355b4669Sjacobs 		case 'H':	/* handling */
87355b4669Sjacobs 			if (strcasecmp(optarg, "hold") == 0)
88355b4669Sjacobs 				papiAttributeListAddString(&list,
89355b4669Sjacobs 				    PAPI_ATTR_EXCL,
90355b4669Sjacobs 				    "job-hold-until", "indefinite");
9143b9c050Sjacobs 			else if (strcasecmp(optarg, "immediate") == 0)
92355b4669Sjacobs 				papiAttributeListAddString(&list,
93355b4669Sjacobs 				    PAPI_ATTR_EXCL,
94355b4669Sjacobs 				    "job-hold-until", "no-hold");
95355b4669Sjacobs 			else
96355b4669Sjacobs 				papiAttributeListAddString(&list,
97355b4669Sjacobs 				    PAPI_ATTR_EXCL,
98355b4669Sjacobs 				    "job-hold-until", optarg);
99355b4669Sjacobs 			break;
10043b9c050Sjacobs 		case 'P': {	/* page list */
10143b9c050Sjacobs 			char buf[BUFSIZ];
10243b9c050Sjacobs 
10343b9c050Sjacobs 			snprintf(buf, sizeof (buf), "page-ranges=%s", optarg);
10443b9c050Sjacobs 			papiAttributeListFromString(&list,
10543b9c050Sjacobs 			    PAPI_ATTR_EXCL, buf);
10643b9c050Sjacobs 			}
107355b4669Sjacobs 			break;
108355b4669Sjacobs 		case 'S':	/* charset */
109355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
110355b4669Sjacobs 			    "lp-charset", optarg);
111355b4669Sjacobs 			break;
112355b4669Sjacobs 		case 'T':	/* type */
113355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
114355b4669Sjacobs 			    "document-format",
115355b4669Sjacobs 			    lp_type_to_mime_type(optarg));
116355b4669Sjacobs 			break;
117355b4669Sjacobs 		case 'D':	/* dump */
118355b4669Sjacobs 			dump = 1;
119355b4669Sjacobs 			break;
120355b4669Sjacobs 		case 'c':	/* copy */
121355b4669Sjacobs 			copy = 1;
122355b4669Sjacobs 			break;
123355b4669Sjacobs 		case 'd':	/* destination */
124355b4669Sjacobs 			printer = optarg;
125355b4669Sjacobs 			break;
126355b4669Sjacobs 		case 'f':	/* form */
127355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1280a44ef6dSjacobs 			    "form", optarg);
129355b4669Sjacobs 			break;
130355b4669Sjacobs 		case 'i':	/* modify job */
131355b4669Sjacobs 			if ((get_printer_id(optarg, &printer, &modify) < 0) ||
132355b4669Sjacobs 			    (modify < 0)) {
133355b4669Sjacobs 				fprintf(stderr,
134355b4669Sjacobs 				    gettext("invalid request id: %s\n"),
135355b4669Sjacobs 				    optarg);
136355b4669Sjacobs 				exit(1);
137355b4669Sjacobs 			}
138355b4669Sjacobs 			break;
139355b4669Sjacobs 		case 'm':	/* mail when complete */
140355b4669Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
141355b4669Sjacobs 			    "rfc-1179-mail", 1);
142355b4669Sjacobs 			break;
143355b4669Sjacobs 		case 'n':	/* copies */
144355b4669Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
145355b4669Sjacobs 			    "copies", atoi(optarg));
146355b4669Sjacobs 			break;
147355b4669Sjacobs 		case 'o':	/* lp "options" */
148355b4669Sjacobs 			papiAttributeListFromString(&list,
149355b4669Sjacobs 			    PAPI_ATTR_REPLACE, optarg);
150355b4669Sjacobs 			break;
151355b4669Sjacobs 		case 'p':	/* Solaris - notification */
152355b4669Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
153355b4669Sjacobs 			    "rfc-1179-mail", 1);
154355b4669Sjacobs 			break;
155355b4669Sjacobs 		case 'q': {	/* priority */
156355b4669Sjacobs 			int i = atoi(optarg);
157355b4669Sjacobs 
15843b9c050Sjacobs 			i = 100 - (i * 2.5);
159355b4669Sjacobs 			if ((i < 1) || (i > 100)) {
160355b4669Sjacobs 				fprintf(stderr, gettext(
161355b4669Sjacobs 				    "priority must be between 0 and 39.\n"));
162355b4669Sjacobs 				exit(1);
163355b4669Sjacobs 			}
164355b4669Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
16543b9c050Sjacobs 			    "job-priority", i);
166355b4669Sjacobs 			}
167355b4669Sjacobs 			break;
168355b4669Sjacobs 		case 'r':	/* "raw" mode */
169355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
170355b4669Sjacobs 			    "document-format",
171355b4669Sjacobs 			    "application/octet-stream");
172355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_APPEND,
173355b4669Sjacobs 			    "stty", "raw");
174355b4669Sjacobs 			break;
175355b4669Sjacobs 		case 's':	/* suppress message */
176355b4669Sjacobs 			silent = 1;
177355b4669Sjacobs 			break;
178355b4669Sjacobs 		case 't':	/* title */
179355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
180355b4669Sjacobs 			    "job-name", optarg);
181355b4669Sjacobs 			break;
182355b4669Sjacobs 		case 'V':	/* validate */
183355b4669Sjacobs 			validate = 1;
184355b4669Sjacobs 			break;
185355b4669Sjacobs 		case 'w':
186355b4669Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
187355b4669Sjacobs 			    "rfc-1179-mail", 1);
188355b4669Sjacobs 			break;
189355b4669Sjacobs 		case 'y':	/* lp "modes" */
190355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_APPEND,
191355b4669Sjacobs 			    "lp-modes", optarg);
192355b4669Sjacobs 			break;
193355b4669Sjacobs 		case 'E':
194355b4669Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
195355b4669Sjacobs 			break;
196355b4669Sjacobs 		default:
197355b4669Sjacobs 			usage(av[0]);
198355b4669Sjacobs 		}
199355b4669Sjacobs 
200355b4669Sjacobs 	/* convert "banner", "nobanner" to "job-sheet" */
201355b4669Sjacobs 	if (papiAttributeListGetBoolean(list, NULL, "banner", &b) == PAPI_OK) {
202355b4669Sjacobs 		(void) papiAttributeListDelete(&list, "banner");
203355b4669Sjacobs 		if (b == PAPI_FALSE)
204355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
205355b4669Sjacobs 			    "job-sheets", "none");
206355b4669Sjacobs 	}
207355b4669Sjacobs 
208355b4669Sjacobs 	if ((printer == NULL) &&
209355b4669Sjacobs 	    ((printer = getenv("PRINTER")) == NULL) &&
210355b4669Sjacobs 	    ((printer = getenv("LPDEST")) == NULL))
211355b4669Sjacobs 		printer = DEFAULT_DEST;
212355b4669Sjacobs 
213e2738c5eSjacobs 	if (((optind + 1) == ac) && (strcmp(av[optind], "-") == 0))
214e2738c5eSjacobs 		optind = ac;
215e2738c5eSjacobs 
216355b4669Sjacobs 	if (modify == -1) {
2170a44ef6dSjacobs 		char *document_format = "text/plain";
218355b4669Sjacobs 
219355b4669Sjacobs 		if (optind != ac) {
220355b4669Sjacobs 			/* get the mime type of the file data */
2210a44ef6dSjacobs #ifdef MAGIC_MIME
222355b4669Sjacobs 			magic_t ms = NULL;
223355b4669Sjacobs 
224355b4669Sjacobs 			if ((ms = magic_open(MAGIC_MIME)) != NULL) {
225355b4669Sjacobs 				document_format = magic_file(ms, av[optind]);
226355b4669Sjacobs 				magic_close(ms);
227355b4669Sjacobs 			}
2280a44ef6dSjacobs #else
2290a44ef6dSjacobs 			if (is_postscript(av[optind]) == 1)
2300a44ef6dSjacobs 				document_format = "application/postscript";
231355b4669Sjacobs #endif
232c1ecd8b9Sjacobs 		} else {
233c1ecd8b9Sjacobs 			if (is_postscript_stream(0, prefetch, &prefetch_len)
234c1ecd8b9Sjacobs 			    == 1)
235c1ecd8b9Sjacobs 				document_format = "application/postscript";
2360a44ef6dSjacobs 		}
237355b4669Sjacobs 
238355b4669Sjacobs 		papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1);
239355b4669Sjacobs 		papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
240355b4669Sjacobs 		    "document-format", document_format);
241355b4669Sjacobs 		papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
242355b4669Sjacobs 		    "job-sheets", "standard");
243355b4669Sjacobs 	}
244355b4669Sjacobs 
245355b4669Sjacobs 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
246355b4669Sjacobs 	    encryption, NULL);
247355b4669Sjacobs 	if (status != PAPI_OK) {
248355b4669Sjacobs 		fprintf(stderr, gettext(
249355b4669Sjacobs 		    "Failed to contact service for %s: %s\n"), printer,
250355b4669Sjacobs 		    verbose_papi_message(svc, status));
251355b4669Sjacobs 		exit(1);
252355b4669Sjacobs 	}
253355b4669Sjacobs 
2540a44ef6dSjacobs 	if (dump != 0) {
2550a44ef6dSjacobs 		printf("requesting attributes:\n");
2560a44ef6dSjacobs 		papiAttributeListPrint(stdout, list, "\t");
2570a44ef6dSjacobs 		printf("\n");
2580a44ef6dSjacobs 	}
2590a44ef6dSjacobs 
260355b4669Sjacobs 	if (modify != -1)
261355b4669Sjacobs 		status = papiJobModify(svc, printer, modify, list, &job);
262355b4669Sjacobs 	else if (optind == ac)	/* no file list, use stdin */
263c1ecd8b9Sjacobs 		status = jobSubmitSTDIN(svc, printer, prefetch, prefetch_len,
264c1ecd8b9Sjacobs 		    list, &job);
265355b4669Sjacobs 	else if (validate == 1)	/* validate the request can be processed */
266355b4669Sjacobs 		status = papiJobValidate(svc, printer, list,
267355b4669Sjacobs 		    NULL, &av[optind], &job);
268355b4669Sjacobs 	else if (copy == 0)	/* reference the files in the job, default */
269355b4669Sjacobs 		status = papiJobSubmitByReference(svc, printer, list,
270355b4669Sjacobs 		    NULL, &av[optind], &job);
271355b4669Sjacobs 	else			/* copy the files before return, -c */
272355b4669Sjacobs 		status = papiJobSubmit(svc, printer, list,
273355b4669Sjacobs 		    NULL, &av[optind], &job);
274355b4669Sjacobs 
275355b4669Sjacobs 	papiAttributeListFree(list);
276355b4669Sjacobs 
277355b4669Sjacobs 	if (status != PAPI_OK) {
278355b4669Sjacobs 		fprintf(stderr, gettext("%s: %s\n"), printer,
279355b4669Sjacobs 		    verbose_papi_message(svc, status));
280355b4669Sjacobs 		papiJobFree(job);
281355b4669Sjacobs 		papiServiceDestroy(svc);
282355b4669Sjacobs 		exit(1);
283355b4669Sjacobs 	}
284355b4669Sjacobs 
285355b4669Sjacobs 	if (((silent == 0) || (dump != 0)) &&
286355b4669Sjacobs 	    ((list = papiJobGetAttributeList(job)) != NULL)) {
287*324104abSsonam gupta - Sun Microsystems - Bangalore India 		int32_t id = -1;
288355b4669Sjacobs 
28935a603adSsonam gupta - Sun Microsystems - Bangalore India 		if (printer == NULL)
290179184d3Sjacobs 			papiAttributeListGetString(list, NULL,
291179184d3Sjacobs 			    "printer-name", &printer);
29235a603adSsonam gupta - Sun Microsystems - Bangalore India 
293*324104abSsonam gupta - Sun Microsystems - Bangalore India 		papiAttributeListGetInteger(list, NULL,
294*324104abSsonam gupta - Sun Microsystems - Bangalore India 		    "job-id-requested", &id);
295*324104abSsonam gupta - Sun Microsystems - Bangalore India 		if (id == -1) {
296355b4669Sjacobs 			papiAttributeListGetInteger(list, NULL, "job-id", &id);
297*324104abSsonam gupta - Sun Microsystems - Bangalore India 		}
298*324104abSsonam gupta - Sun Microsystems - Bangalore India 
299179184d3Sjacobs 		printf(gettext("request id is %s-%d "), printer, id);
300355b4669Sjacobs 		if (ac != optind)
301355b4669Sjacobs 			printf("(%d file(s))\n", ac - optind);
302355b4669Sjacobs 		else
303355b4669Sjacobs 			printf("(standard input)\n");
304355b4669Sjacobs 
305355b4669Sjacobs 		if (dump != 0) {
306355b4669Sjacobs 			printf("job attributes:\n");
307355b4669Sjacobs 			papiAttributeListPrint(stdout, list, "\t");
308355b4669Sjacobs 			printf("\n");
309355b4669Sjacobs 		}
310355b4669Sjacobs 	}
311355b4669Sjacobs 
312355b4669Sjacobs 	papiJobFree(job);
313355b4669Sjacobs 	papiServiceDestroy(svc);
314355b4669Sjacobs 
315355b4669Sjacobs 	return (0);
316355b4669Sjacobs }
317