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