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