xref: /titanic_44/usr/src/cmd/print/bsd-sysv-commands/lp.c (revision e429788e241121c1f81089f762558027000ea25f)
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: lp.c 179 2006-07-17 18:24:07Z 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 [-c] [-m] [-p] [-s] [-w] [-d destination]  "
57 			"[-f form-name] [-H special-handling] [-n number] "
58 			"[-o option] [-P page-list] [-q priority-level]  "
59 			"[-S character-set | print-wheel]  [-t title] [-v] "
60 			"[-T content-type [-r]] [-y mode-list] [file...]\n"),
61 		name);
62 	exit(1);
63 }
64 
65 static struct {
66 	char *mime_type;
67 	char *lp_type;
68 } type_map[] = {
69 	{ "text/plain", "simple" },
70 	{ "application/octet-stream", "raw" },
71 	{ "application/octet-stream", "any" },
72 	{ "application/postscript", "postscript" },
73 	{ "application/postscript", "ps" },
74 	{ "application/x-cif", "cif" },
75 	{ "application/x-dvi", "dvi" },
76 	{ "application/x-plot", "plot" },
77 	{ "application/x-ditroff", "troff" },
78 	{ "application/x-troff", "otroff" },
79 	{ "application/x-pr", "pr" },
80 	{ "application/x-fortran", "fortran" },
81 	{ "application/x-raster", "raster" },
82 	{ NULL, NULL}
83 };
84 
85 static char *
86 lp_type_to_mime_type(char *lp_type)
87 {
88 	int i;
89 
90 	if (lp_type == NULL)
91 		return ("application/octet-stream");
92 
93 	for (i = 0; type_map[i].lp_type != NULL; i++)
94 		if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
95 			return (type_map[i].mime_type);
96 
97 	return (lp_type);
98 }
99 
100 int
101 main(int ac, char *av[])
102 {
103 	papi_status_t status;
104 	papi_service_t svc = NULL;
105 	papi_attribute_t **list = NULL;
106 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
107 	papi_job_t job = NULL;
108 	char *printer = NULL;
109 	char b = PAPI_TRUE;
110 	int copy = 0;
111 	int silent = 0;
112 	int dump = 0;
113 	int validate = 0;
114 	int modify = -1;
115 	int c;
116 
117 	(void) setlocale(LC_ALL, "");
118 	(void) textdomain("SUNW_OST_OSCMD");
119 
120 	while ((c = getopt(ac, av, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF)
121 		switch (c) {
122 		case 'H':	/* handling */
123 			if (strcasecmp(optarg, "hold") == 0)
124 				papiAttributeListAddString(&list,
125 					PAPI_ATTR_EXCL,
126 					"job-hold-until", "indefinite");
127 			else if (strcasecmp(optarg, "release") == 0)
128 				papiAttributeListAddString(&list,
129 					PAPI_ATTR_EXCL,
130 					"job-hold-until", "no-hold");
131 			else if (strcasecmp(optarg, "immediate") == 0)
132 				papiAttributeListAddInteger(&list,
133 					PAPI_ATTR_EXCL,
134 					"job-priority", 100);
135 			else
136 				papiAttributeListAddString(&list,
137 					PAPI_ATTR_EXCL,
138 					"job-hold-until", optarg);
139 			break;
140 		case 'P':	/* page list */
141 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
142 					"page-ranges", optarg);
143 			break;
144 		case 'S':	/* charset */
145 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
146 					"lp-charset", optarg);
147 			break;
148 		case 'T':	/* type */
149 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
150 					"document-format",
151 					lp_type_to_mime_type(optarg));
152 			break;
153 		case 'D':	/* dump */
154 			dump = 1;
155 			break;
156 		case 'c':	/* copy */
157 			copy = 1;
158 			break;
159 		case 'd':	/* destination */
160 			printer = optarg;
161 			break;
162 		case 'f':	/* form */
163 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
164 					"media", optarg);
165 			break;
166 		case 'i':	/* modify job */
167 			if ((get_printer_id(optarg, &printer, &modify) < 0) ||
168 			    (modify < 0)) {
169 				fprintf(stderr,
170 					gettext("invalid request id: %s\n"),
171 					optarg);
172 				exit(1);
173 			}
174 			break;
175 		case 'm':	/* mail when complete */
176 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
177 				"rfc-1179-mail", 1);
178 			break;
179 		case 'n':	/* copies */
180 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
181 					"copies", atoi(optarg));
182 			break;
183 		case 'o':	/* lp "options" */
184 			papiAttributeListFromString(&list,
185 					PAPI_ATTR_REPLACE, optarg);
186 			break;
187 		case 'p':	/* Solaris - notification */
188 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
189 				"rfc-1179-mail", 1);
190 			break;
191 		case 'q': {	/* priority */
192 			int i = atoi(optarg);
193 
194 			i = 99 * (39 - i) / 39 + 1;
195 			if ((i < 1) || (i > 100)) {
196 				fprintf(stderr, gettext(
197 				    "priority must be between 0 and 39.\n"));
198 				exit(1);
199 			}
200 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
201 					"priority", i);
202 			}
203 			break;
204 		case 'r':	/* "raw" mode */
205 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
206 					"document-format",
207 					"application/octet-stream");
208 			papiAttributeListAddString(&list, PAPI_ATTR_APPEND,
209 					"stty", "raw");
210 			break;
211 		case 's':	/* suppress message */
212 			silent = 1;
213 			break;
214 		case 't':	/* title */
215 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
216 					"job-name", optarg);
217 			break;
218 		case 'V':	/* validate */
219 			validate = 1;
220 			break;
221 		case 'w':
222 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
223 				"rfc-1179-mail", 1);
224 			break;
225 		case 'y':	/* lp "modes" */
226 			papiAttributeListAddString(&list, PAPI_ATTR_APPEND,
227 					"lp-modes", optarg);
228 			break;
229 		case 'E':
230 			encryption = PAPI_ENCRYPT_REQUIRED;
231 			break;
232 		default:
233 			usage(av[0]);
234 		}
235 
236 	/* convert "banner", "nobanner" to "job-sheet" */
237 	if (papiAttributeListGetBoolean(list, NULL, "banner", &b) == PAPI_OK) {
238 		(void) papiAttributeListDelete(&list, "banner");
239 		if (b == PAPI_FALSE)
240 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
241 						"job-sheets", "none");
242 	}
243 
244 	if ((printer == NULL) &&
245 	    ((printer = getenv("PRINTER")) == NULL) &&
246 	    ((printer = getenv("LPDEST")) == NULL))
247 		printer = DEFAULT_DEST;
248 
249 	if (modify == -1) {
250 		char *document_format = "application/octet-stream";
251 
252 #ifdef MAGIC_MIME
253 		if (optind != ac) {
254 			/* get the mime type of the file data */
255 			magic_t ms = NULL;
256 
257 			if ((ms = magic_open(MAGIC_MIME)) != NULL) {
258 				document_format = magic_file(ms, av[optind]);
259 				magic_close(ms);
260 			}
261 		}
262 #endif
263 
264 		papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1);
265 		papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
266 				"document-format", document_format);
267 		papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
268 				"job-sheets", "standard");
269 	}
270 
271 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
272 					encryption, NULL);
273 	if (status != PAPI_OK) {
274 		fprintf(stderr, gettext(
275 			"Failed to contact service for %s: %s\n"), printer,
276 			verbose_papi_message(svc, status));
277 		exit(1);
278 	}
279 
280 	if (modify != -1)
281 		status = papiJobModify(svc, printer, modify, list, &job);
282 	else if (optind == ac)	/* no file list, use stdin */
283 		status = jobSubmitSTDIN(svc, printer, list, &job);
284 	else if (validate == 1)	/* validate the request can be processed */
285 		status = papiJobValidate(svc, printer, list,
286 					NULL, &av[optind], &job);
287 	else if (copy == 0)	/* reference the files in the job, default */
288 		status = papiJobSubmitByReference(svc, printer, list,
289 					NULL, &av[optind], &job);
290 	else			/* copy the files before return, -c */
291 		status = papiJobSubmit(svc, printer, list,
292 					NULL, &av[optind], &job);
293 
294 	papiAttributeListFree(list);
295 
296 	if (status != PAPI_OK) {
297 		fprintf(stderr, gettext("%s: %s\n"), printer,
298 			verbose_papi_message(svc, status));
299 		papiJobFree(job);
300 		papiServiceDestroy(svc);
301 		exit(1);
302 	}
303 
304 	if (((silent == 0) || (dump != 0)) &&
305 	    ((list = papiJobGetAttributeList(job)) != NULL)) {
306 		int32_t id = 0;
307 
308 		papiAttributeListGetString(list, NULL,
309 					"printer-name", &printer);
310 		papiAttributeListGetInteger(list, NULL, "job-id", &id);
311 		printf(gettext("request id is %s-%d "), printer, id);
312 		if (ac != optind)
313 			printf("(%d file(s))\n", ac - optind);
314 		else
315 			printf("(standard input)\n");
316 
317 		if (dump != 0) {
318 			printf("job attributes:\n");
319 			papiAttributeListPrint(stdout, list, "\t");
320 			printf("\n");
321 		}
322 	}
323 
324 	papiJobFree(job);
325 	papiServiceDestroy(svc);
326 
327 	return (0);
328 }
329