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