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