xref: /illumos-gate/usr/src/cmd/print/bsd-sysv-commands/lpr.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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 2017 Gary Mills
24  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  *
27  */
28 
29 /* $Id: lpr.c 146 2006-03-24 00:26:54Z njacobs $ */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <locale.h>
36 #include <libintl.h>
37 #include <papi.h>
38 #include "common.h"
39 
40 #ifdef HAVE_LIBMAGIC	/* for mimetype auto-detection */
41 #include <magic.h>
42 #endif /* HAVE_LIBMAGIC */
43 
44 static void
45 usage(char *program)
46 {
47 	char *name;
48 
49 	if ((name = strrchr(program, '/')) == NULL)
50 		name = program;
51 	else
52 		name++;
53 
54 	fprintf(stdout,
55 		gettext("Usage: %s [-P printer] [-# copies] [-C class] "
56 				"[-J job] [-T title] "
57 				"[-p [-i indent] [-w width]] "
58 				"[-1|-2|-3|-4 font] [-m] [-h] [-s] "
59 				"[-filter_option] [file ..]\n"), name);
60 	exit(1);
61 }
62 
63 int
64 main(int ac, char *av[])
65 {
66 	papi_status_t status;
67 	papi_service_t svc = NULL;
68 	papi_attribute_t **list = NULL;
69 	papi_job_t job = NULL;
70 	int exit_code = 0;
71 	char *printer = NULL;
72 	char prefetch[3];
73 	int prefetch_len = sizeof (prefetch);
74 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
75 	int dump = 0;
76 	int validate = 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 			break;
172 		case 's':
173 			copy = 0;
174 			break;
175 		case 'V':	/* validate */
176 			validate = 1;
177 			break;
178 		case '1':
179 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
180 				"rfc-1179-font-r", optarg);
181 			break;
182 		case '2':
183 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
184 				"rfc-1179-font-i", optarg);
185 			break;
186 		case '3':
187 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
188 				"rfc-1179-font-b", optarg);
189 			break;
190 		case '4':
191 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
192 				"rfc-1179-font-s", optarg);
193 			break;
194 		default:
195 			usage(av[0]);
196 		}
197 
198 	if ((printer == NULL) &&
199 	    ((printer = getenv("PRINTER")) == NULL) &&
200 	    ((printer = getenv("LPDEST")) == NULL))
201 		printer = DEFAULT_DEST;
202 
203 	if (((optind + 1) == ac) && (strcmp(av[optind], "-") == 0))
204 		optind = ac;
205 
206 	if (optind != ac) {
207 		/* get the mime type of the file data */
208 #ifdef MAGIC_MIME
209 		magic_t ms;
210 
211 		if ((ms = magic_open(MAGIC_MIME)) != NULL) {
212 			document_format = magic_file(ms, av[optind]);
213 			magic_close(ms);
214 		}
215 #else
216 		if (is_postscript(av[optind]) == 1)
217 			document_format = "application/postscript";
218 #endif
219 	} else {
220 		if (is_postscript_stream(0, prefetch, &prefetch_len) == 1)
221 			document_format = "application/postscript";
222 	}
223 
224 	papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1);
225 	papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
226 				"document-format", document_format);
227 	papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
228 				"job-sheets", "standard");
229 
230 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
231 					encryption, NULL);
232 	if (status != PAPI_OK) {
233 		fprintf(stderr, gettext(
234 			"Failed to contact service for %s: %s\n"), printer,
235 			verbose_papi_message(svc, status));
236 		exit(1);
237 	}
238 
239 	if (validate == 1)	/* validate the request can be processed */
240 		status = papiJobValidate(svc, printer, list,
241 					NULL, &av[optind], &job);
242 	else if (optind == ac)	/* no file list, use stdin */
243 		status = jobSubmitSTDIN(svc, printer, prefetch, prefetch_len,
244 					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