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