xref: /titanic_53/usr/src/cmd/print/bsd-sysv-commands/lpr.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
1*355b4669Sjacobs /*
2*355b4669Sjacobs  * CDDL HEADER START
3*355b4669Sjacobs  *
4*355b4669Sjacobs  * The contents of this file are subject to the terms of the
5*355b4669Sjacobs  * Common Development and Distribution License (the "License").
6*355b4669Sjacobs  * You may not use this file except in compliance with the License.
7*355b4669Sjacobs  *
8*355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10*355b4669Sjacobs  * See the License for the specific language governing permissions
11*355b4669Sjacobs  * and limitations under the License.
12*355b4669Sjacobs  *
13*355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14*355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16*355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17*355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18*355b4669Sjacobs  *
19*355b4669Sjacobs  * CDDL HEADER END
20*355b4669Sjacobs  */
21*355b4669Sjacobs 
22*355b4669Sjacobs /*
23*355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*355b4669Sjacobs  * Use is subject to license terms.
25*355b4669Sjacobs  *
26*355b4669Sjacobs  */
27*355b4669Sjacobs 
28*355b4669Sjacobs /* $Id: lpr.c 146 2006-03-24 00:26:54Z njacobs $ */
29*355b4669Sjacobs 
30*355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*355b4669Sjacobs 
32*355b4669Sjacobs #include <stdio.h>
33*355b4669Sjacobs #include <stdlib.h>
34*355b4669Sjacobs #include <unistd.h>
35*355b4669Sjacobs #include <string.h>
36*355b4669Sjacobs #include <locale.h>
37*355b4669Sjacobs #include <libintl.h>
38*355b4669Sjacobs #include <papi.h>
39*355b4669Sjacobs #include "common.h"
40*355b4669Sjacobs 
41*355b4669Sjacobs #ifdef HAVE_LIBMAGIC	/* for mimetype auto-detection */
42*355b4669Sjacobs #include <magic.h>
43*355b4669Sjacobs #endif /* HAVE_LIBMAGIC */
44*355b4669Sjacobs 
45*355b4669Sjacobs static void
46*355b4669Sjacobs usage(char *program)
47*355b4669Sjacobs {
48*355b4669Sjacobs 	char *name;
49*355b4669Sjacobs 
50*355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
51*355b4669Sjacobs 		name = program;
52*355b4669Sjacobs 	else
53*355b4669Sjacobs 		name++;
54*355b4669Sjacobs 
55*355b4669Sjacobs 	fprintf(stdout,
56*355b4669Sjacobs 		gettext("Usage: %s [-P printer] [-# copies] [-C class] "
57*355b4669Sjacobs 				"[-J job] [-T title] "
58*355b4669Sjacobs 				"[-p [-i indent] [-w width]] "
59*355b4669Sjacobs 				"[-1|-2|-3|-4 font] [-m] [-h] [-s] "
60*355b4669Sjacobs 				"[-filter_option] [file ..]\n"), name);
61*355b4669Sjacobs 	exit(1);
62*355b4669Sjacobs }
63*355b4669Sjacobs 
64*355b4669Sjacobs int
65*355b4669Sjacobs main(int ac, char *av[])
66*355b4669Sjacobs {
67*355b4669Sjacobs 	papi_status_t status;
68*355b4669Sjacobs 	papi_service_t svc = NULL;
69*355b4669Sjacobs 	papi_attribute_t **list = NULL;
70*355b4669Sjacobs 	papi_job_t job = NULL;
71*355b4669Sjacobs 	int exit_code = 0;
72*355b4669Sjacobs 	char *printer = NULL;
73*355b4669Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
74*355b4669Sjacobs 	int dump = 0;
75*355b4669Sjacobs 	int validate = 0;
76*355b4669Sjacobs 	int remove = 0;
77*355b4669Sjacobs 	int copy = 1;	/* default is to copy the data */
78*355b4669Sjacobs 	char *document_format = "application/octet-stream";
79*355b4669Sjacobs 	int c;
80*355b4669Sjacobs 
81*355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
82*355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
83*355b4669Sjacobs 
84*355b4669Sjacobs 	while ((c = getopt(ac, av,
85*355b4669Sjacobs 			"EP:#:C:DVJ:T:w:i:hplrstdgvcfmn1:2:3:4:")) != EOF)
86*355b4669Sjacobs 		switch (c) {
87*355b4669Sjacobs 		case 'E':
88*355b4669Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
89*355b4669Sjacobs 			break;
90*355b4669Sjacobs 		case 'P':
91*355b4669Sjacobs 			printer = optarg;
92*355b4669Sjacobs 			break;
93*355b4669Sjacobs 		case '#':
94*355b4669Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
95*355b4669Sjacobs 					"copies", atoi(optarg));
96*355b4669Sjacobs 			break;
97*355b4669Sjacobs 		case 'C':
98*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
99*355b4669Sjacobs 					"rfc-1179-class", optarg);
100*355b4669Sjacobs 			break;
101*355b4669Sjacobs 		case 'D':
102*355b4669Sjacobs 			dump = 1;
103*355b4669Sjacobs 			break;
104*355b4669Sjacobs 		case 'J':
105*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
106*355b4669Sjacobs 					"job-name", optarg);
107*355b4669Sjacobs 			break;
108*355b4669Sjacobs 		case 'T':
109*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
110*355b4669Sjacobs 					"pr-title", optarg);
111*355b4669Sjacobs 			break;
112*355b4669Sjacobs 		case 'p':
113*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
114*355b4669Sjacobs 				"document-format", "application/x-pr");
115*355b4669Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
116*355b4669Sjacobs 					"pr-filter", 1);
117*355b4669Sjacobs 			break;
118*355b4669Sjacobs 		case 'i':
119*355b4669Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
120*355b4669Sjacobs 					"pr-indent", atoi(optarg));
121*355b4669Sjacobs 			break;
122*355b4669Sjacobs 		case 'w':
123*355b4669Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
124*355b4669Sjacobs 					"pr-width", atoi(optarg));
125*355b4669Sjacobs 			break;
126*355b4669Sjacobs 		case 'h':
127*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
128*355b4669Sjacobs 					"job-sheets", "none");
129*355b4669Sjacobs 			break;
130*355b4669Sjacobs 		case 'l':
131*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
132*355b4669Sjacobs 				"document-format", "application/octet-stream");
133*355b4669Sjacobs 			break;
134*355b4669Sjacobs 		case 'o':
135*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
136*355b4669Sjacobs 				"document-format", "application/postscript");
137*355b4669Sjacobs 			break;
138*355b4669Sjacobs 		case 'c':
139*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
140*355b4669Sjacobs 				"document-format", "application/x-cif");
141*355b4669Sjacobs 			break;
142*355b4669Sjacobs 		case 'd':
143*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
144*355b4669Sjacobs 				"document-format", "application/x-dvi");
145*355b4669Sjacobs 			break;
146*355b4669Sjacobs 		case 'f':
147*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
148*355b4669Sjacobs 				"document-format", "application/x-fortran");
149*355b4669Sjacobs 			break;
150*355b4669Sjacobs 		case 'g':
151*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
152*355b4669Sjacobs 				"document-format", "application/x-plot");
153*355b4669Sjacobs 			break;
154*355b4669Sjacobs 		case 'n':
155*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
156*355b4669Sjacobs 				"document-format", "application/x-ditroff");
157*355b4669Sjacobs 			break;
158*355b4669Sjacobs 		case 't':
159*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
160*355b4669Sjacobs 				"document-format", "application/x-troff");
161*355b4669Sjacobs 			break;
162*355b4669Sjacobs 		case 'v':
163*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
164*355b4669Sjacobs 				"document-format", "application/x-raster");
165*355b4669Sjacobs 			break;
166*355b4669Sjacobs 		case 'm':
167*355b4669Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
168*355b4669Sjacobs 				"rfc-1179-mail", 1);
169*355b4669Sjacobs 			break;
170*355b4669Sjacobs 		case 'r':
171*355b4669Sjacobs 			remove = 1;
172*355b4669Sjacobs 			break;
173*355b4669Sjacobs 		case 's':
174*355b4669Sjacobs 			copy = 0;
175*355b4669Sjacobs 			break;
176*355b4669Sjacobs 		case 'V':	/* validate */
177*355b4669Sjacobs 			validate = 1;
178*355b4669Sjacobs 			break;
179*355b4669Sjacobs 		case '1':
180*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
181*355b4669Sjacobs 				"rfc-1179-font-r", optarg);
182*355b4669Sjacobs 			break;
183*355b4669Sjacobs 		case '2':
184*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
185*355b4669Sjacobs 				"rfc-1179-font-i", optarg);
186*355b4669Sjacobs 			break;
187*355b4669Sjacobs 		case '3':
188*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
189*355b4669Sjacobs 				"rfc-1179-font-b", optarg);
190*355b4669Sjacobs 			break;
191*355b4669Sjacobs 		case '4':
192*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
193*355b4669Sjacobs 				"rfc-1179-font-s", optarg);
194*355b4669Sjacobs 			break;
195*355b4669Sjacobs 		default:
196*355b4669Sjacobs 			usage(av[0]);
197*355b4669Sjacobs 		}
198*355b4669Sjacobs 
199*355b4669Sjacobs 	if ((remove != 0) && (copy == 0)) {
200*355b4669Sjacobs 		fprintf(stderr, gettext(
201*355b4669Sjacobs 			"-r and -s may not be used together\n"));
202*355b4669Sjacobs 		exit(1);
203*355b4669Sjacobs 	}
204*355b4669Sjacobs 
205*355b4669Sjacobs 	if ((printer == NULL) &&
206*355b4669Sjacobs 	    ((printer = getenv("PRINTER")) == NULL) &&
207*355b4669Sjacobs 	    ((printer = getenv("LPDEST")) == NULL))
208*355b4669Sjacobs 		printer = DEFAULT_DEST;
209*355b4669Sjacobs 
210*355b4669Sjacobs #ifdef MAGIC_MIME
211*355b4669Sjacobs 	if (optind != ac) {
212*355b4669Sjacobs 		/* get the mime type of the file data */
213*355b4669Sjacobs 		magic_t ms;
214*355b4669Sjacobs 
215*355b4669Sjacobs 		if ((ms = magic_open(MAGIC_MIME)) != NULL) {
216*355b4669Sjacobs 			document_format = magic_file(ms, av[optind]);
217*355b4669Sjacobs 			magic_close(ms);
218*355b4669Sjacobs 		}
219*355b4669Sjacobs 	}
220*355b4669Sjacobs #endif
221*355b4669Sjacobs 
222*355b4669Sjacobs 	papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1);
223*355b4669Sjacobs 	papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
224*355b4669Sjacobs 				"document-format", document_format);
225*355b4669Sjacobs 	papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
226*355b4669Sjacobs 				"job-sheets", "standard");
227*355b4669Sjacobs 
228*355b4669Sjacobs 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
229*355b4669Sjacobs 					encryption, NULL);
230*355b4669Sjacobs 	if (status != PAPI_OK) {
231*355b4669Sjacobs 		fprintf(stderr, gettext(
232*355b4669Sjacobs 			"Failed to contact service for %s: %s\n"), printer,
233*355b4669Sjacobs 			verbose_papi_message(svc, status));
234*355b4669Sjacobs 		exit(1);
235*355b4669Sjacobs 	}
236*355b4669Sjacobs 
237*355b4669Sjacobs 	if (validate == 1)	/* validate the request can be processed */
238*355b4669Sjacobs 		status = papiJobValidate(svc, printer, list,
239*355b4669Sjacobs 					NULL, &av[optind], &job);
240*355b4669Sjacobs 	else if (optind == ac)	/* no file list, use stdin */
241*355b4669Sjacobs 		status = jobSubmitSTDIN(svc, printer, list, &job);
242*355b4669Sjacobs 	else if (copy == 0)	/* reference the files in the job, default */
243*355b4669Sjacobs 		status = papiJobSubmitByReference(svc, printer, list,
244*355b4669Sjacobs 					NULL, &av[optind], &job);
245*355b4669Sjacobs 	else			/* copy the files before return, -c */
246*355b4669Sjacobs 		status = papiJobSubmit(svc, printer, list,
247*355b4669Sjacobs 					NULL, &av[optind], &job);
248*355b4669Sjacobs 
249*355b4669Sjacobs 	papiAttributeListFree(list);
250*355b4669Sjacobs 
251*355b4669Sjacobs 	if (status != PAPI_OK) {
252*355b4669Sjacobs 		fprintf(stderr, gettext("%s: %s\n"), printer,
253*355b4669Sjacobs 			verbose_papi_message(svc, status));
254*355b4669Sjacobs 		papiJobFree(job);
255*355b4669Sjacobs 		papiServiceDestroy(svc);
256*355b4669Sjacobs 		exit(1);
257*355b4669Sjacobs 	}
258*355b4669Sjacobs 
259*355b4669Sjacobs 	if (dump != 0) {
260*355b4669Sjacobs 		list = papiJobGetAttributeList(job);
261*355b4669Sjacobs 		printf("job attributes:\n");
262*355b4669Sjacobs 		papiAttributeListPrint(stdout, list, "\t");
263*355b4669Sjacobs 		printf("\n");
264*355b4669Sjacobs 	}
265*355b4669Sjacobs 
266*355b4669Sjacobs 	papiJobFree(job);
267*355b4669Sjacobs 	papiServiceDestroy(svc);
268*355b4669Sjacobs 
269*355b4669Sjacobs 	return (exit_code);
270*355b4669Sjacobs }
271