xref: /titanic_51/usr/src/cmd/print/bsd-sysv-commands/lp.c (revision 179184d3e9bc6ab146407a62a8461338687b1908)
1355b4669Sjacobs /*
2355b4669Sjacobs  * CDDL HEADER START
3355b4669Sjacobs  *
4355b4669Sjacobs  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
7355b4669Sjacobs  *
8355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs  * See the License for the specific language governing permissions
11355b4669Sjacobs  * and limitations under the License.
12355b4669Sjacobs  *
13355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs  *
19355b4669Sjacobs  * CDDL HEADER END
20355b4669Sjacobs  */
21355b4669Sjacobs 
22355b4669Sjacobs /*
23355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28*179184d3Sjacobs /* $Id: lp.c 179 2006-07-17 18:24:07Z njacobs $ */
29355b4669Sjacobs 
30355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31355b4669Sjacobs 
32355b4669Sjacobs #include <stdio.h>
33355b4669Sjacobs #include <stdlib.h>
34355b4669Sjacobs #include <unistd.h>
35355b4669Sjacobs #include <string.h>
36355b4669Sjacobs #include <locale.h>
37355b4669Sjacobs #include <libintl.h>
38355b4669Sjacobs #include <papi.h>
39355b4669Sjacobs #include "common.h"
40355b4669Sjacobs 
41355b4669Sjacobs #ifdef HAVE_LIBMAGIC	/* for mimetype auto-detection */
42355b4669Sjacobs #include <magic.h>
43355b4669Sjacobs #endif /* HAVE_LIBMAGIC */
44355b4669Sjacobs 
45355b4669Sjacobs static void
46355b4669Sjacobs usage(char *program)
47355b4669Sjacobs {
48355b4669Sjacobs 	char *name;
49355b4669Sjacobs 
50355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
51355b4669Sjacobs 		name = program;
52355b4669Sjacobs 	else
53355b4669Sjacobs 		name++;
54355b4669Sjacobs 
55355b4669Sjacobs 	fprintf(stdout,
56355b4669Sjacobs 		gettext("Usage: %s [-c] [-m] [-p] [-s] [-w] [-d destination]  "
57355b4669Sjacobs 			"[-f form-name] [-H special-handling] [-n number] "
58355b4669Sjacobs 			"[-o option] [-P page-list] [-q priority-level]  "
59355b4669Sjacobs 			"[-S character-set | print-wheel]  [-t title] [-v] "
60355b4669Sjacobs 			"[-T content-type [-r]] [-y mode-list] [file...]\n"),
61355b4669Sjacobs 		name);
62355b4669Sjacobs 	exit(1);
63355b4669Sjacobs }
64355b4669Sjacobs 
65355b4669Sjacobs static struct {
66355b4669Sjacobs 	char *mime_type;
67355b4669Sjacobs 	char *lp_type;
68355b4669Sjacobs } type_map[] = {
69355b4669Sjacobs 	{ "text/plain", "simple" },
70355b4669Sjacobs 	{ "application/octet-stream", "raw" },
71355b4669Sjacobs 	{ "application/octet-stream", "any" },
72355b4669Sjacobs 	{ "application/postscript", "postscript" },
73355b4669Sjacobs 	{ "application/postscript", "ps" },
74355b4669Sjacobs 	{ "application/x-cif", "cif" },
75355b4669Sjacobs 	{ "application/x-dvi", "dvi" },
76355b4669Sjacobs 	{ "application/x-plot", "plot" },
77355b4669Sjacobs 	{ "application/x-ditroff", "troff" },
78355b4669Sjacobs 	{ "application/x-troff", "otroff" },
79355b4669Sjacobs 	{ "application/x-pr", "pr" },
80355b4669Sjacobs 	{ "application/x-fortran", "fortran" },
81355b4669Sjacobs 	{ "application/x-raster", "raster" },
82355b4669Sjacobs 	{ NULL, NULL}
83355b4669Sjacobs };
84355b4669Sjacobs 
85355b4669Sjacobs static char *
86355b4669Sjacobs lp_type_to_mime_type(char *lp_type)
87355b4669Sjacobs {
88355b4669Sjacobs 	int i;
89355b4669Sjacobs 
90355b4669Sjacobs 	if (lp_type == NULL)
91355b4669Sjacobs 		return ("application/octet-stream");
92355b4669Sjacobs 
93355b4669Sjacobs 	for (i = 0; type_map[i].lp_type != NULL; i++)
94355b4669Sjacobs 		if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
95355b4669Sjacobs 			return (type_map[i].mime_type);
96355b4669Sjacobs 
97355b4669Sjacobs 	return (lp_type);
98355b4669Sjacobs }
99355b4669Sjacobs 
100355b4669Sjacobs int
101355b4669Sjacobs main(int ac, char *av[])
102355b4669Sjacobs {
103355b4669Sjacobs 	papi_status_t status;
104355b4669Sjacobs 	papi_service_t svc = NULL;
105355b4669Sjacobs 	papi_attribute_t **list = NULL;
106355b4669Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
107355b4669Sjacobs 	papi_job_t job = NULL;
108355b4669Sjacobs 	char *printer = NULL;
109355b4669Sjacobs 	char b = PAPI_TRUE;
110355b4669Sjacobs 	int copy = 0;
111355b4669Sjacobs 	int silent = 0;
112355b4669Sjacobs 	int dump = 0;
113355b4669Sjacobs 	int validate = 0;
114355b4669Sjacobs 	int modify = -1;
115355b4669Sjacobs 	int c;
116355b4669Sjacobs 
117355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
118355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
119355b4669Sjacobs 
120355b4669Sjacobs 	while ((c = getopt(ac, av, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF)
121355b4669Sjacobs 		switch (c) {
122355b4669Sjacobs 		case 'H':	/* handling */
123355b4669Sjacobs 			if (strcasecmp(optarg, "hold") == 0)
124355b4669Sjacobs 				papiAttributeListAddString(&list,
125355b4669Sjacobs 					PAPI_ATTR_EXCL,
126355b4669Sjacobs 					"job-hold-until", "indefinite");
127355b4669Sjacobs 			else if (strcasecmp(optarg, "release") == 0)
128355b4669Sjacobs 				papiAttributeListAddString(&list,
129355b4669Sjacobs 					PAPI_ATTR_EXCL,
130355b4669Sjacobs 					"job-hold-until", "no-hold");
131355b4669Sjacobs 			else if (strcasecmp(optarg, "immediate") == 0)
132355b4669Sjacobs 				papiAttributeListAddInteger(&list,
133355b4669Sjacobs 					PAPI_ATTR_EXCL,
134355b4669Sjacobs 					"job-priority", 100);
135355b4669Sjacobs 			else
136355b4669Sjacobs 				papiAttributeListAddString(&list,
137355b4669Sjacobs 					PAPI_ATTR_EXCL,
138355b4669Sjacobs 					"job-hold-until", optarg);
139355b4669Sjacobs 			break;
140355b4669Sjacobs 		case 'P':	/* page list */
141355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
142355b4669Sjacobs 					"page-ranges", optarg);
143355b4669Sjacobs 			break;
144355b4669Sjacobs 		case 'S':	/* charset */
145355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
146355b4669Sjacobs 					"lp-charset", optarg);
147355b4669Sjacobs 			break;
148355b4669Sjacobs 		case 'T':	/* type */
149355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
150355b4669Sjacobs 					"document-format",
151355b4669Sjacobs 					lp_type_to_mime_type(optarg));
152355b4669Sjacobs 			break;
153355b4669Sjacobs 		case 'D':	/* dump */
154355b4669Sjacobs 			dump = 1;
155355b4669Sjacobs 			break;
156355b4669Sjacobs 		case 'c':	/* copy */
157355b4669Sjacobs 			copy = 1;
158355b4669Sjacobs 			break;
159355b4669Sjacobs 		case 'd':	/* destination */
160355b4669Sjacobs 			printer = optarg;
161355b4669Sjacobs 			break;
162355b4669Sjacobs 		case 'f':	/* form */
163355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
164355b4669Sjacobs 					"media", optarg);
165355b4669Sjacobs 			break;
166355b4669Sjacobs 		case 'i':	/* modify job */
167355b4669Sjacobs 			if ((get_printer_id(optarg, &printer, &modify) < 0) ||
168355b4669Sjacobs 			    (modify < 0)) {
169355b4669Sjacobs 				fprintf(stderr,
170355b4669Sjacobs 					gettext("invalid request id: %s\n"),
171355b4669Sjacobs 					optarg);
172355b4669Sjacobs 				exit(1);
173355b4669Sjacobs 			}
174355b4669Sjacobs 			break;
175355b4669Sjacobs 		case 'm':	/* mail when complete */
176355b4669Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
177355b4669Sjacobs 				"rfc-1179-mail", 1);
178355b4669Sjacobs 			break;
179355b4669Sjacobs 		case 'n':	/* copies */
180355b4669Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
181355b4669Sjacobs 					"copies", atoi(optarg));
182355b4669Sjacobs 			break;
183355b4669Sjacobs 		case 'o':	/* lp "options" */
184355b4669Sjacobs 			papiAttributeListFromString(&list,
185355b4669Sjacobs 					PAPI_ATTR_REPLACE, optarg);
186355b4669Sjacobs 			break;
187355b4669Sjacobs 		case 'p':	/* Solaris - notification */
188355b4669Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
189355b4669Sjacobs 				"rfc-1179-mail", 1);
190355b4669Sjacobs 			break;
191355b4669Sjacobs 		case 'q': {	/* priority */
192355b4669Sjacobs 			int i = atoi(optarg);
193355b4669Sjacobs 
194355b4669Sjacobs 			i = 99 * (39 - i) / 39 + 1;
195355b4669Sjacobs 			if ((i < 1) || (i > 100)) {
196355b4669Sjacobs 				fprintf(stderr, gettext(
197355b4669Sjacobs 				    "priority must be between 0 and 39.\n"));
198355b4669Sjacobs 				exit(1);
199355b4669Sjacobs 			}
200355b4669Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
201355b4669Sjacobs 					"priority", i);
202355b4669Sjacobs 			}
203355b4669Sjacobs 			break;
204355b4669Sjacobs 		case 'r':	/* "raw" mode */
205355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
206355b4669Sjacobs 					"document-format",
207355b4669Sjacobs 					"application/octet-stream");
208355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_APPEND,
209355b4669Sjacobs 					"stty", "raw");
210355b4669Sjacobs 			break;
211355b4669Sjacobs 		case 's':	/* suppress message */
212355b4669Sjacobs 			silent = 1;
213355b4669Sjacobs 			break;
214355b4669Sjacobs 		case 't':	/* title */
215355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
216355b4669Sjacobs 					"job-name", optarg);
217355b4669Sjacobs 			break;
218355b4669Sjacobs 		case 'V':	/* validate */
219355b4669Sjacobs 			validate = 1;
220355b4669Sjacobs 			break;
221355b4669Sjacobs 		case 'w':
222355b4669Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
223355b4669Sjacobs 				"rfc-1179-mail", 1);
224355b4669Sjacobs 			break;
225355b4669Sjacobs 		case 'y':	/* lp "modes" */
226355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_APPEND,
227355b4669Sjacobs 					"lp-modes", optarg);
228355b4669Sjacobs 			break;
229355b4669Sjacobs 		case 'E':
230355b4669Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
231355b4669Sjacobs 			break;
232355b4669Sjacobs 		default:
233355b4669Sjacobs 			usage(av[0]);
234355b4669Sjacobs 		}
235355b4669Sjacobs 
236355b4669Sjacobs 	/* convert "banner", "nobanner" to "job-sheet" */
237355b4669Sjacobs 	if (papiAttributeListGetBoolean(list, NULL, "banner", &b) == PAPI_OK) {
238355b4669Sjacobs 		(void) papiAttributeListDelete(&list, "banner");
239355b4669Sjacobs 		if (b == PAPI_FALSE)
240355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
241355b4669Sjacobs 						"job-sheets", "none");
242355b4669Sjacobs 	}
243355b4669Sjacobs 
244355b4669Sjacobs 	if ((printer == NULL) &&
245355b4669Sjacobs 	    ((printer = getenv("PRINTER")) == NULL) &&
246355b4669Sjacobs 	    ((printer = getenv("LPDEST")) == NULL))
247355b4669Sjacobs 		printer = DEFAULT_DEST;
248355b4669Sjacobs 
249355b4669Sjacobs 	if (modify == -1) {
250355b4669Sjacobs 		char *document_format = "application/octet-stream";
251355b4669Sjacobs 
252355b4669Sjacobs #ifdef MAGIC_MIME
253355b4669Sjacobs 		if (optind != ac) {
254355b4669Sjacobs 			/* get the mime type of the file data */
255355b4669Sjacobs 			magic_t ms = NULL;
256355b4669Sjacobs 
257355b4669Sjacobs 			if ((ms = magic_open(MAGIC_MIME)) != NULL) {
258355b4669Sjacobs 				document_format = magic_file(ms, av[optind]);
259355b4669Sjacobs 				magic_close(ms);
260355b4669Sjacobs 			}
261355b4669Sjacobs 		}
262355b4669Sjacobs #endif
263355b4669Sjacobs 
264355b4669Sjacobs 		papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1);
265355b4669Sjacobs 		papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
266355b4669Sjacobs 				"document-format", document_format);
267355b4669Sjacobs 		papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
268355b4669Sjacobs 				"job-sheets", "standard");
269355b4669Sjacobs 	}
270355b4669Sjacobs 
271355b4669Sjacobs 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
272355b4669Sjacobs 					encryption, NULL);
273355b4669Sjacobs 	if (status != PAPI_OK) {
274355b4669Sjacobs 		fprintf(stderr, gettext(
275355b4669Sjacobs 			"Failed to contact service for %s: %s\n"), printer,
276355b4669Sjacobs 			verbose_papi_message(svc, status));
277355b4669Sjacobs 		exit(1);
278355b4669Sjacobs 	}
279355b4669Sjacobs 
280355b4669Sjacobs 	if (modify != -1)
281355b4669Sjacobs 		status = papiJobModify(svc, printer, modify, list, &job);
282355b4669Sjacobs 	else if (optind == ac)	/* no file list, use stdin */
283355b4669Sjacobs 		status = jobSubmitSTDIN(svc, printer, list, &job);
284355b4669Sjacobs 	else if (validate == 1)	/* validate the request can be processed */
285355b4669Sjacobs 		status = papiJobValidate(svc, printer, list,
286355b4669Sjacobs 					NULL, &av[optind], &job);
287355b4669Sjacobs 	else if (copy == 0)	/* reference the files in the job, default */
288355b4669Sjacobs 		status = papiJobSubmitByReference(svc, printer, list,
289355b4669Sjacobs 					NULL, &av[optind], &job);
290355b4669Sjacobs 	else			/* copy the files before return, -c */
291355b4669Sjacobs 		status = papiJobSubmit(svc, printer, list,
292355b4669Sjacobs 					NULL, &av[optind], &job);
293355b4669Sjacobs 
294355b4669Sjacobs 	papiAttributeListFree(list);
295355b4669Sjacobs 
296355b4669Sjacobs 	if (status != PAPI_OK) {
297355b4669Sjacobs 		fprintf(stderr, gettext("%s: %s\n"), printer,
298355b4669Sjacobs 			verbose_papi_message(svc, status));
299355b4669Sjacobs 		papiJobFree(job);
300355b4669Sjacobs 		papiServiceDestroy(svc);
301355b4669Sjacobs 		exit(1);
302355b4669Sjacobs 	}
303355b4669Sjacobs 
304355b4669Sjacobs 	if (((silent == 0) || (dump != 0)) &&
305355b4669Sjacobs 	    ((list = papiJobGetAttributeList(job)) != NULL)) {
306355b4669Sjacobs 		int32_t id = 0;
307355b4669Sjacobs 
308*179184d3Sjacobs 		papiAttributeListGetString(list, NULL,
309*179184d3Sjacobs 					"printer-name", &printer);
310355b4669Sjacobs 		papiAttributeListGetInteger(list, NULL, "job-id", &id);
311*179184d3Sjacobs 		printf(gettext("request id is %s-%d "), printer, id);
312355b4669Sjacobs 		if (ac != optind)
313355b4669Sjacobs 			printf("(%d file(s))\n", ac - optind);
314355b4669Sjacobs 		else
315355b4669Sjacobs 			printf("(standard input)\n");
316355b4669Sjacobs 
317355b4669Sjacobs 		if (dump != 0) {
318355b4669Sjacobs 			printf("job attributes:\n");
319355b4669Sjacobs 			papiAttributeListPrint(stdout, list, "\t");
320355b4669Sjacobs 			printf("\n");
321355b4669Sjacobs 		}
322355b4669Sjacobs 	}
323355b4669Sjacobs 
324355b4669Sjacobs 	papiJobFree(job);
325355b4669Sjacobs 	papiServiceDestroy(svc);
326355b4669Sjacobs 
327355b4669Sjacobs 	return (0);
328355b4669Sjacobs }
329