xref: /titanic_41/usr/src/cmd/print/bsd-sysv-commands/lpr.c (revision c1ecd8b9404ee0d96d93f02e82c441b9bb149a3d)
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 /*
23ebae6f6eSps29005  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: lpr.c 146 2006-03-24 00:26:54Z njacobs $ */
29355b4669Sjacobs 
30355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31355b4669Sjacobs 
32355b4669Sjacobs #include <stdio.h>
33355b4669Sjacobs #include <stdlib.h>
34355b4669Sjacobs #include <unistd.h>
35355b4669Sjacobs #include <string.h>
36355b4669Sjacobs #include <locale.h>
37355b4669Sjacobs #include <libintl.h>
38355b4669Sjacobs #include <papi.h>
39355b4669Sjacobs #include "common.h"
40355b4669Sjacobs 
41355b4669Sjacobs #ifdef HAVE_LIBMAGIC	/* for mimetype auto-detection */
42355b4669Sjacobs #include <magic.h>
43355b4669Sjacobs #endif /* HAVE_LIBMAGIC */
44355b4669Sjacobs 
45355b4669Sjacobs static void
usage(char * program)46355b4669Sjacobs usage(char *program)
47355b4669Sjacobs {
48355b4669Sjacobs 	char *name;
49355b4669Sjacobs 
50355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
51355b4669Sjacobs 		name = program;
52355b4669Sjacobs 	else
53355b4669Sjacobs 		name++;
54355b4669Sjacobs 
55355b4669Sjacobs 	fprintf(stdout,
56355b4669Sjacobs 		gettext("Usage: %s [-P printer] [-# copies] [-C class] "
57355b4669Sjacobs 				"[-J job] [-T title] "
58355b4669Sjacobs 				"[-p [-i indent] [-w width]] "
59355b4669Sjacobs 				"[-1|-2|-3|-4 font] [-m] [-h] [-s] "
60355b4669Sjacobs 				"[-filter_option] [file ..]\n"), name);
61355b4669Sjacobs 	exit(1);
62355b4669Sjacobs }
63355b4669Sjacobs 
64355b4669Sjacobs int
main(int ac,char * av[])65355b4669Sjacobs main(int ac, char *av[])
66355b4669Sjacobs {
67355b4669Sjacobs 	papi_status_t status;
68355b4669Sjacobs 	papi_service_t svc = NULL;
69355b4669Sjacobs 	papi_attribute_t **list = NULL;
70355b4669Sjacobs 	papi_job_t job = NULL;
71355b4669Sjacobs 	int exit_code = 0;
72355b4669Sjacobs 	char *printer = NULL;
73*c1ecd8b9Sjacobs 	char prefetch[3];
74*c1ecd8b9Sjacobs 	int prefetch_len = sizeof (prefetch);
75355b4669Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
76355b4669Sjacobs 	int dump = 0;
77355b4669Sjacobs 	int validate = 0;
78355b4669Sjacobs 	int remove = 0;
79355b4669Sjacobs 	int copy = 1;	/* default is to copy the data */
800a44ef6dSjacobs 	char *document_format = "text/plain";
81355b4669Sjacobs 	int c;
82355b4669Sjacobs 
83355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
84355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
85355b4669Sjacobs 
86355b4669Sjacobs 	while ((c = getopt(ac, av,
87355b4669Sjacobs 			"EP:#:C:DVJ:T:w:i:hplrstdgvcfmn1:2:3:4:")) != EOF)
88355b4669Sjacobs 		switch (c) {
89355b4669Sjacobs 		case 'E':
90355b4669Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
91355b4669Sjacobs 			break;
92355b4669Sjacobs 		case 'P':
93355b4669Sjacobs 			printer = optarg;
94355b4669Sjacobs 			break;
95355b4669Sjacobs 		case '#':
96355b4669Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
97355b4669Sjacobs 					"copies", atoi(optarg));
98355b4669Sjacobs 			break;
99355b4669Sjacobs 		case 'C':
100355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
101355b4669Sjacobs 					"rfc-1179-class", optarg);
102355b4669Sjacobs 			break;
103355b4669Sjacobs 		case 'D':
104355b4669Sjacobs 			dump = 1;
105355b4669Sjacobs 			break;
106355b4669Sjacobs 		case 'J':
107355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
108355b4669Sjacobs 					"job-name", optarg);
109355b4669Sjacobs 			break;
110355b4669Sjacobs 		case 'T':
111355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
112355b4669Sjacobs 					"pr-title", optarg);
113355b4669Sjacobs 			break;
114355b4669Sjacobs 		case 'p':
115355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
116355b4669Sjacobs 				"document-format", "application/x-pr");
117355b4669Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
118355b4669Sjacobs 					"pr-filter", 1);
119355b4669Sjacobs 			break;
120355b4669Sjacobs 		case 'i':
121355b4669Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
122355b4669Sjacobs 					"pr-indent", atoi(optarg));
123355b4669Sjacobs 			break;
124355b4669Sjacobs 		case 'w':
125355b4669Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
126355b4669Sjacobs 					"pr-width", atoi(optarg));
127355b4669Sjacobs 			break;
128355b4669Sjacobs 		case 'h':
129355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
130355b4669Sjacobs 					"job-sheets", "none");
131355b4669Sjacobs 			break;
132355b4669Sjacobs 		case 'l':
133355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
134355b4669Sjacobs 				"document-format", "application/octet-stream");
135355b4669Sjacobs 			break;
136355b4669Sjacobs 		case 'o':
137355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
138355b4669Sjacobs 				"document-format", "application/postscript");
139355b4669Sjacobs 			break;
140355b4669Sjacobs 		case 'c':
141355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
142355b4669Sjacobs 				"document-format", "application/x-cif");
143355b4669Sjacobs 			break;
144355b4669Sjacobs 		case 'd':
145355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
146355b4669Sjacobs 				"document-format", "application/x-dvi");
147355b4669Sjacobs 			break;
148355b4669Sjacobs 		case 'f':
149355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
150355b4669Sjacobs 				"document-format", "application/x-fortran");
151355b4669Sjacobs 			break;
152355b4669Sjacobs 		case 'g':
153355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
154355b4669Sjacobs 				"document-format", "application/x-plot");
155355b4669Sjacobs 			break;
156355b4669Sjacobs 		case 'n':
157355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
158355b4669Sjacobs 				"document-format", "application/x-ditroff");
159355b4669Sjacobs 			break;
160355b4669Sjacobs 		case 't':
161355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
162355b4669Sjacobs 				"document-format", "application/x-troff");
163355b4669Sjacobs 			break;
164355b4669Sjacobs 		case 'v':
165355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
166355b4669Sjacobs 				"document-format", "application/x-raster");
167355b4669Sjacobs 			break;
168355b4669Sjacobs 		case 'm':
169355b4669Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
170355b4669Sjacobs 				"rfc-1179-mail", 1);
171355b4669Sjacobs 			break;
172355b4669Sjacobs 		case 'r':
173355b4669Sjacobs 			remove = 1;
174355b4669Sjacobs 			break;
175355b4669Sjacobs 		case 's':
176355b4669Sjacobs 			copy = 0;
177355b4669Sjacobs 			break;
178355b4669Sjacobs 		case 'V':	/* validate */
179355b4669Sjacobs 			validate = 1;
180355b4669Sjacobs 			break;
181355b4669Sjacobs 		case '1':
182355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
183355b4669Sjacobs 				"rfc-1179-font-r", optarg);
184355b4669Sjacobs 			break;
185355b4669Sjacobs 		case '2':
186355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
187355b4669Sjacobs 				"rfc-1179-font-i", optarg);
188355b4669Sjacobs 			break;
189355b4669Sjacobs 		case '3':
190355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
191355b4669Sjacobs 				"rfc-1179-font-b", optarg);
192355b4669Sjacobs 			break;
193355b4669Sjacobs 		case '4':
194355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
195355b4669Sjacobs 				"rfc-1179-font-s", optarg);
196355b4669Sjacobs 			break;
197355b4669Sjacobs 		default:
198355b4669Sjacobs 			usage(av[0]);
199355b4669Sjacobs 		}
200355b4669Sjacobs 
201355b4669Sjacobs 	if ((printer == NULL) &&
202355b4669Sjacobs 	    ((printer = getenv("PRINTER")) == NULL) &&
203355b4669Sjacobs 	    ((printer = getenv("LPDEST")) == NULL))
204355b4669Sjacobs 		printer = DEFAULT_DEST;
205355b4669Sjacobs 
206e2738c5eSjacobs 	if (((optind + 1) == ac) && (strcmp(av[optind], "-") == 0))
207e2738c5eSjacobs 		optind = ac;
208e2738c5eSjacobs 
209355b4669Sjacobs 	if (optind != ac) {
210355b4669Sjacobs 		/* get the mime type of the file data */
2110a44ef6dSjacobs #ifdef MAGIC_MIME
212355b4669Sjacobs 		magic_t ms;
213355b4669Sjacobs 
214355b4669Sjacobs 		if ((ms = magic_open(MAGIC_MIME)) != NULL) {
215355b4669Sjacobs 			document_format = magic_file(ms, av[optind]);
216355b4669Sjacobs 			magic_close(ms);
217355b4669Sjacobs 		}
2180a44ef6dSjacobs #else
2190a44ef6dSjacobs 		if (is_postscript(av[optind]) == 1)
2200a44ef6dSjacobs 			document_format = "application/postscript";
221355b4669Sjacobs #endif
222*c1ecd8b9Sjacobs 	} else {
223*c1ecd8b9Sjacobs 		if (is_postscript_stream(0, prefetch, &prefetch_len) == 1)
224*c1ecd8b9Sjacobs 			document_format = "application/postscript";
2250a44ef6dSjacobs 	}
226355b4669Sjacobs 
227355b4669Sjacobs 	papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1);
228355b4669Sjacobs 	papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
229355b4669Sjacobs 				"document-format", document_format);
230355b4669Sjacobs 	papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
231355b4669Sjacobs 				"job-sheets", "standard");
232355b4669Sjacobs 
233355b4669Sjacobs 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
234355b4669Sjacobs 					encryption, NULL);
235355b4669Sjacobs 	if (status != PAPI_OK) {
236355b4669Sjacobs 		fprintf(stderr, gettext(
237355b4669Sjacobs 			"Failed to contact service for %s: %s\n"), printer,
238355b4669Sjacobs 			verbose_papi_message(svc, status));
239355b4669Sjacobs 		exit(1);
240355b4669Sjacobs 	}
241355b4669Sjacobs 
242355b4669Sjacobs 	if (validate == 1)	/* validate the request can be processed */
243355b4669Sjacobs 		status = papiJobValidate(svc, printer, list,
244355b4669Sjacobs 					NULL, &av[optind], &job);
245355b4669Sjacobs 	else if (optind == ac)	/* no file list, use stdin */
246*c1ecd8b9Sjacobs 		status = jobSubmitSTDIN(svc, printer, prefetch, prefetch_len,
247*c1ecd8b9Sjacobs 					list, &job);
248355b4669Sjacobs 	else if (copy == 0)	/* reference the files in the job, default */
249355b4669Sjacobs 		status = papiJobSubmitByReference(svc, printer, list,
250355b4669Sjacobs 					NULL, &av[optind], &job);
251355b4669Sjacobs 	else			/* copy the files before return, -c */
252355b4669Sjacobs 		status = papiJobSubmit(svc, printer, list,
253355b4669Sjacobs 					NULL, &av[optind], &job);
254355b4669Sjacobs 
255355b4669Sjacobs 	papiAttributeListFree(list);
256355b4669Sjacobs 
257355b4669Sjacobs 	if (status != PAPI_OK) {
258355b4669Sjacobs 		fprintf(stderr, gettext("%s: %s\n"), printer,
259355b4669Sjacobs 			verbose_papi_message(svc, status));
260355b4669Sjacobs 		papiJobFree(job);
261355b4669Sjacobs 		papiServiceDestroy(svc);
262355b4669Sjacobs 		exit(1);
263355b4669Sjacobs 	}
264355b4669Sjacobs 
265355b4669Sjacobs 	if (dump != 0) {
266355b4669Sjacobs 		list = papiJobGetAttributeList(job);
267355b4669Sjacobs 		printf("job attributes:\n");
268355b4669Sjacobs 		papiAttributeListPrint(stdout, list, "\t");
269355b4669Sjacobs 		printf("\n");
270355b4669Sjacobs 	}
271355b4669Sjacobs 
272355b4669Sjacobs 	papiJobFree(job);
273355b4669Sjacobs 	papiServiceDestroy(svc);
274355b4669Sjacobs 
275355b4669Sjacobs 	return (exit_code);
276355b4669Sjacobs }
277