xref: /titanic_52/usr/src/cmd/print/bsd-sysv-commands/lpr.c (revision b6c3f7863936abeae522e48a13887dddeb691a45)
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 = "text/plain";
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 	if (optind != ac) {
214 		/* get the mime type of the file data */
215 #ifdef MAGIC_MIME
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 #else
223 		if (is_postscript(av[optind]) == 1)
224 			document_format = "application/postscript";
225 #endif
226 	}
227 
228 	papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1);
229 	papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
230 				"document-format", document_format);
231 	papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
232 				"job-sheets", "standard");
233 
234 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
235 					encryption, NULL);
236 	if (status != PAPI_OK) {
237 		fprintf(stderr, gettext(
238 			"Failed to contact service for %s: %s\n"), printer,
239 			verbose_papi_message(svc, status));
240 		exit(1);
241 	}
242 
243 	if (validate == 1)	/* validate the request can be processed */
244 		status = papiJobValidate(svc, printer, list,
245 					NULL, &av[optind], &job);
246 	else if (optind == ac)	/* no file list, use stdin */
247 		status = jobSubmitSTDIN(svc, printer, list, &job);
248 	else if (copy == 0)	/* reference the files in the job, default */
249 		status = papiJobSubmitByReference(svc, printer, list,
250 					NULL, &av[optind], &job);
251 	else			/* copy the files before return, -c */
252 		status = papiJobSubmit(svc, printer, list,
253 					NULL, &av[optind], &job);
254 
255 	papiAttributeListFree(list);
256 
257 	if (status != PAPI_OK) {
258 		fprintf(stderr, gettext("%s: %s\n"), printer,
259 			verbose_papi_message(svc, status));
260 		papiJobFree(job);
261 		papiServiceDestroy(svc);
262 		exit(1);
263 	}
264 
265 	if (dump != 0) {
266 		list = papiJobGetAttributeList(job);
267 		printf("job attributes:\n");
268 		papiAttributeListPrint(stdout, list, "\t");
269 		printf("\n");
270 	}
271 
272 	papiJobFree(job);
273 	papiServiceDestroy(svc);
274 
275 	return (exit_code);
276 }
277