xref: /titanic_51/usr/src/cmd/print/bsd-sysv-commands/lp.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
1*355b4669Sjacobs /*
2*355b4669Sjacobs  * CDDL HEADER START
3*355b4669Sjacobs  *
4*355b4669Sjacobs  * The contents of this file are subject to the terms of the
5*355b4669Sjacobs  * Common Development and Distribution License (the "License").
6*355b4669Sjacobs  * You may not use this file except in compliance with the License.
7*355b4669Sjacobs  *
8*355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10*355b4669Sjacobs  * See the License for the specific language governing permissions
11*355b4669Sjacobs  * and limitations under the License.
12*355b4669Sjacobs  *
13*355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14*355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16*355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17*355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18*355b4669Sjacobs  *
19*355b4669Sjacobs  * CDDL HEADER END
20*355b4669Sjacobs  */
21*355b4669Sjacobs 
22*355b4669Sjacobs /*
23*355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*355b4669Sjacobs  * Use is subject to license terms.
25*355b4669Sjacobs  *
26*355b4669Sjacobs  */
27*355b4669Sjacobs 
28*355b4669Sjacobs /* $Id: lp.c 169 2006-05-20 05:58:14Z njacobs $ */
29*355b4669Sjacobs 
30*355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*355b4669Sjacobs 
32*355b4669Sjacobs #include <stdio.h>
33*355b4669Sjacobs #include <stdlib.h>
34*355b4669Sjacobs #include <unistd.h>
35*355b4669Sjacobs #include <string.h>
36*355b4669Sjacobs #include <locale.h>
37*355b4669Sjacobs #include <libintl.h>
38*355b4669Sjacobs #include <papi.h>
39*355b4669Sjacobs #include "common.h"
40*355b4669Sjacobs 
41*355b4669Sjacobs #ifdef HAVE_LIBMAGIC	/* for mimetype auto-detection */
42*355b4669Sjacobs #include <magic.h>
43*355b4669Sjacobs #endif /* HAVE_LIBMAGIC */
44*355b4669Sjacobs 
45*355b4669Sjacobs static void
46*355b4669Sjacobs usage(char *program)
47*355b4669Sjacobs {
48*355b4669Sjacobs 	char *name;
49*355b4669Sjacobs 
50*355b4669Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
51*355b4669Sjacobs 		name = program;
52*355b4669Sjacobs 	else
53*355b4669Sjacobs 		name++;
54*355b4669Sjacobs 
55*355b4669Sjacobs 	fprintf(stdout,
56*355b4669Sjacobs 		gettext("Usage: %s [-c] [-m] [-p] [-s] [-w] [-d destination]  "
57*355b4669Sjacobs 			"[-f form-name] [-H special-handling] [-n number] "
58*355b4669Sjacobs 			"[-o option] [-P page-list] [-q priority-level]  "
59*355b4669Sjacobs 			"[-S character-set | print-wheel]  [-t title] [-v] "
60*355b4669Sjacobs 			"[-T content-type [-r]] [-y mode-list] [file...]\n"),
61*355b4669Sjacobs 		name);
62*355b4669Sjacobs 	exit(1);
63*355b4669Sjacobs }
64*355b4669Sjacobs 
65*355b4669Sjacobs static struct {
66*355b4669Sjacobs 	char *mime_type;
67*355b4669Sjacobs 	char *lp_type;
68*355b4669Sjacobs } type_map[] = {
69*355b4669Sjacobs 	{ "text/plain", "simple" },
70*355b4669Sjacobs 	{ "application/octet-stream", "raw" },
71*355b4669Sjacobs 	{ "application/octet-stream", "any" },
72*355b4669Sjacobs 	{ "application/postscript", "postscript" },
73*355b4669Sjacobs 	{ "application/postscript", "ps" },
74*355b4669Sjacobs 	{ "application/x-cif", "cif" },
75*355b4669Sjacobs 	{ "application/x-dvi", "dvi" },
76*355b4669Sjacobs 	{ "application/x-plot", "plot" },
77*355b4669Sjacobs 	{ "application/x-ditroff", "troff" },
78*355b4669Sjacobs 	{ "application/x-troff", "otroff" },
79*355b4669Sjacobs 	{ "application/x-pr", "pr" },
80*355b4669Sjacobs 	{ "application/x-fortran", "fortran" },
81*355b4669Sjacobs 	{ "application/x-raster", "raster" },
82*355b4669Sjacobs 	{ NULL, NULL}
83*355b4669Sjacobs };
84*355b4669Sjacobs 
85*355b4669Sjacobs static char *
86*355b4669Sjacobs lp_type_to_mime_type(char *lp_type)
87*355b4669Sjacobs {
88*355b4669Sjacobs 	int i;
89*355b4669Sjacobs 
90*355b4669Sjacobs 	if (lp_type == NULL)
91*355b4669Sjacobs 		return ("application/octet-stream");
92*355b4669Sjacobs 
93*355b4669Sjacobs 	for (i = 0; type_map[i].lp_type != NULL; i++)
94*355b4669Sjacobs 		if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
95*355b4669Sjacobs 			return (type_map[i].mime_type);
96*355b4669Sjacobs 
97*355b4669Sjacobs 	return (lp_type);
98*355b4669Sjacobs }
99*355b4669Sjacobs 
100*355b4669Sjacobs int
101*355b4669Sjacobs main(int ac, char *av[])
102*355b4669Sjacobs {
103*355b4669Sjacobs 	papi_status_t status;
104*355b4669Sjacobs 	papi_service_t svc = NULL;
105*355b4669Sjacobs 	papi_attribute_t **list = NULL;
106*355b4669Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
107*355b4669Sjacobs 	papi_job_t job = NULL;
108*355b4669Sjacobs 	char *printer = NULL;
109*355b4669Sjacobs 	char b = PAPI_TRUE;
110*355b4669Sjacobs 	int copy = 0;
111*355b4669Sjacobs 	int silent = 0;
112*355b4669Sjacobs 	int dump = 0;
113*355b4669Sjacobs 	int validate = 0;
114*355b4669Sjacobs 	int modify = -1;
115*355b4669Sjacobs 	int c;
116*355b4669Sjacobs 
117*355b4669Sjacobs 	(void) setlocale(LC_ALL, "");
118*355b4669Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
119*355b4669Sjacobs 
120*355b4669Sjacobs 	while ((c = getopt(ac, av, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF)
121*355b4669Sjacobs 		switch (c) {
122*355b4669Sjacobs 		case 'H':	/* handling */
123*355b4669Sjacobs 			if (strcasecmp(optarg, "hold") == 0)
124*355b4669Sjacobs 				papiAttributeListAddString(&list,
125*355b4669Sjacobs 					PAPI_ATTR_EXCL,
126*355b4669Sjacobs 					"job-hold-until", "indefinite");
127*355b4669Sjacobs 			else if (strcasecmp(optarg, "release") == 0)
128*355b4669Sjacobs 				papiAttributeListAddString(&list,
129*355b4669Sjacobs 					PAPI_ATTR_EXCL,
130*355b4669Sjacobs 					"job-hold-until", "no-hold");
131*355b4669Sjacobs 			else if (strcasecmp(optarg, "immediate") == 0)
132*355b4669Sjacobs 				papiAttributeListAddInteger(&list,
133*355b4669Sjacobs 					PAPI_ATTR_EXCL,
134*355b4669Sjacobs 					"job-priority", 100);
135*355b4669Sjacobs 			else
136*355b4669Sjacobs 				papiAttributeListAddString(&list,
137*355b4669Sjacobs 					PAPI_ATTR_EXCL,
138*355b4669Sjacobs 					"job-hold-until", optarg);
139*355b4669Sjacobs 			break;
140*355b4669Sjacobs 		case 'P':	/* page list */
141*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
142*355b4669Sjacobs 					"page-ranges", optarg);
143*355b4669Sjacobs 			break;
144*355b4669Sjacobs 		case 'S':	/* charset */
145*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
146*355b4669Sjacobs 					"lp-charset", optarg);
147*355b4669Sjacobs 			break;
148*355b4669Sjacobs 		case 'T':	/* type */
149*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
150*355b4669Sjacobs 					"document-format",
151*355b4669Sjacobs 					lp_type_to_mime_type(optarg));
152*355b4669Sjacobs 			break;
153*355b4669Sjacobs 		case 'D':	/* dump */
154*355b4669Sjacobs 			dump = 1;
155*355b4669Sjacobs 			break;
156*355b4669Sjacobs 		case 'c':	/* copy */
157*355b4669Sjacobs 			copy = 1;
158*355b4669Sjacobs 			break;
159*355b4669Sjacobs 		case 'd':	/* destination */
160*355b4669Sjacobs 			printer = optarg;
161*355b4669Sjacobs 			break;
162*355b4669Sjacobs 		case 'f':	/* form */
163*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
164*355b4669Sjacobs 					"media", optarg);
165*355b4669Sjacobs 			break;
166*355b4669Sjacobs 		case 'i':	/* modify job */
167*355b4669Sjacobs 			if ((get_printer_id(optarg, &printer, &modify) < 0) ||
168*355b4669Sjacobs 			    (modify < 0)) {
169*355b4669Sjacobs 				fprintf(stderr,
170*355b4669Sjacobs 					gettext("invalid request id: %s\n"),
171*355b4669Sjacobs 					optarg);
172*355b4669Sjacobs 				exit(1);
173*355b4669Sjacobs 			}
174*355b4669Sjacobs 			break;
175*355b4669Sjacobs 		case 'm':	/* mail when complete */
176*355b4669Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
177*355b4669Sjacobs 				"rfc-1179-mail", 1);
178*355b4669Sjacobs 			break;
179*355b4669Sjacobs 		case 'n':	/* copies */
180*355b4669Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
181*355b4669Sjacobs 					"copies", atoi(optarg));
182*355b4669Sjacobs 			break;
183*355b4669Sjacobs 		case 'o':	/* lp "options" */
184*355b4669Sjacobs 			papiAttributeListFromString(&list,
185*355b4669Sjacobs 					PAPI_ATTR_REPLACE, optarg);
186*355b4669Sjacobs 			break;
187*355b4669Sjacobs 		case 'p':	/* Solaris - notification */
188*355b4669Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
189*355b4669Sjacobs 				"rfc-1179-mail", 1);
190*355b4669Sjacobs 			break;
191*355b4669Sjacobs 		case 'q': {	/* priority */
192*355b4669Sjacobs 			int i = atoi(optarg);
193*355b4669Sjacobs 
194*355b4669Sjacobs 			i = 99 * (39 - i) / 39 + 1;
195*355b4669Sjacobs 			if ((i < 1) || (i > 100)) {
196*355b4669Sjacobs 				fprintf(stderr, gettext(
197*355b4669Sjacobs 				    "priority must be between 0 and 39.\n"));
198*355b4669Sjacobs 				exit(1);
199*355b4669Sjacobs 			}
200*355b4669Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
201*355b4669Sjacobs 					"priority", i);
202*355b4669Sjacobs 			}
203*355b4669Sjacobs 			break;
204*355b4669Sjacobs 		case 'r':	/* "raw" mode */
205*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
206*355b4669Sjacobs 					"document-format",
207*355b4669Sjacobs 					"application/octet-stream");
208*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_APPEND,
209*355b4669Sjacobs 					"stty", "raw");
210*355b4669Sjacobs 			break;
211*355b4669Sjacobs 		case 's':	/* suppress message */
212*355b4669Sjacobs 			silent = 1;
213*355b4669Sjacobs 			break;
214*355b4669Sjacobs 		case 't':	/* title */
215*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
216*355b4669Sjacobs 					"job-name", optarg);
217*355b4669Sjacobs 			break;
218*355b4669Sjacobs 		case 'V':	/* validate */
219*355b4669Sjacobs 			validate = 1;
220*355b4669Sjacobs 			break;
221*355b4669Sjacobs 		case 'w':
222*355b4669Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
223*355b4669Sjacobs 				"rfc-1179-mail", 1);
224*355b4669Sjacobs 			break;
225*355b4669Sjacobs 		case 'y':	/* lp "modes" */
226*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_APPEND,
227*355b4669Sjacobs 					"lp-modes", optarg);
228*355b4669Sjacobs 			break;
229*355b4669Sjacobs 		case 'E':
230*355b4669Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
231*355b4669Sjacobs 			break;
232*355b4669Sjacobs 		default:
233*355b4669Sjacobs 			usage(av[0]);
234*355b4669Sjacobs 		}
235*355b4669Sjacobs 
236*355b4669Sjacobs 	/* convert "banner", "nobanner" to "job-sheet" */
237*355b4669Sjacobs 	if (papiAttributeListGetBoolean(list, NULL, "banner", &b) == PAPI_OK) {
238*355b4669Sjacobs 		(void) papiAttributeListDelete(&list, "banner");
239*355b4669Sjacobs 		if (b == PAPI_FALSE)
240*355b4669Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
241*355b4669Sjacobs 						"job-sheets", "none");
242*355b4669Sjacobs 	}
243*355b4669Sjacobs 
244*355b4669Sjacobs 	if ((printer == NULL) &&
245*355b4669Sjacobs 	    ((printer = getenv("PRINTER")) == NULL) &&
246*355b4669Sjacobs 	    ((printer = getenv("LPDEST")) == NULL))
247*355b4669Sjacobs 		printer = DEFAULT_DEST;
248*355b4669Sjacobs 
249*355b4669Sjacobs 	if (modify == -1) {
250*355b4669Sjacobs 		char *document_format = "application/octet-stream";
251*355b4669Sjacobs 
252*355b4669Sjacobs #ifdef MAGIC_MIME
253*355b4669Sjacobs 		if (optind != ac) {
254*355b4669Sjacobs 			/* get the mime type of the file data */
255*355b4669Sjacobs 			magic_t ms = NULL;
256*355b4669Sjacobs 
257*355b4669Sjacobs 			if ((ms = magic_open(MAGIC_MIME)) != NULL) {
258*355b4669Sjacobs 				document_format = magic_file(ms, av[optind]);
259*355b4669Sjacobs 				magic_close(ms);
260*355b4669Sjacobs 			}
261*355b4669Sjacobs 		}
262*355b4669Sjacobs #endif
263*355b4669Sjacobs 
264*355b4669Sjacobs 		papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1);
265*355b4669Sjacobs 		papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
266*355b4669Sjacobs 				"document-format", document_format);
267*355b4669Sjacobs 		papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
268*355b4669Sjacobs 				"job-sheets", "standard");
269*355b4669Sjacobs 	}
270*355b4669Sjacobs 
271*355b4669Sjacobs 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
272*355b4669Sjacobs 					encryption, NULL);
273*355b4669Sjacobs 	if (status != PAPI_OK) {
274*355b4669Sjacobs 		fprintf(stderr, gettext(
275*355b4669Sjacobs 			"Failed to contact service for %s: %s\n"), printer,
276*355b4669Sjacobs 			verbose_papi_message(svc, status));
277*355b4669Sjacobs 		exit(1);
278*355b4669Sjacobs 	}
279*355b4669Sjacobs 
280*355b4669Sjacobs 	if (modify != -1)
281*355b4669Sjacobs 		status = papiJobModify(svc, printer, modify, list, &job);
282*355b4669Sjacobs 	else if (optind == ac)	/* no file list, use stdin */
283*355b4669Sjacobs 		status = jobSubmitSTDIN(svc, printer, list, &job);
284*355b4669Sjacobs 	else if (validate == 1)	/* validate the request can be processed */
285*355b4669Sjacobs 		status = papiJobValidate(svc, printer, list,
286*355b4669Sjacobs 					NULL, &av[optind], &job);
287*355b4669Sjacobs 	else if (copy == 0)	/* reference the files in the job, default */
288*355b4669Sjacobs 		status = papiJobSubmitByReference(svc, printer, list,
289*355b4669Sjacobs 					NULL, &av[optind], &job);
290*355b4669Sjacobs 	else			/* copy the files before return, -c */
291*355b4669Sjacobs 		status = papiJobSubmit(svc, printer, list,
292*355b4669Sjacobs 					NULL, &av[optind], &job);
293*355b4669Sjacobs 
294*355b4669Sjacobs 	papiAttributeListFree(list);
295*355b4669Sjacobs 
296*355b4669Sjacobs 	if (status != PAPI_OK) {
297*355b4669Sjacobs 		fprintf(stderr, gettext("%s: %s\n"), printer,
298*355b4669Sjacobs 			verbose_papi_message(svc, status));
299*355b4669Sjacobs 		papiJobFree(job);
300*355b4669Sjacobs 		papiServiceDestroy(svc);
301*355b4669Sjacobs 		exit(1);
302*355b4669Sjacobs 	}
303*355b4669Sjacobs 
304*355b4669Sjacobs 	if (((silent == 0) || (dump != 0)) &&
305*355b4669Sjacobs 	    ((list = papiJobGetAttributeList(job)) != NULL)) {
306*355b4669Sjacobs 		char *dest = "unknown";
307*355b4669Sjacobs 		int32_t id = 0;
308*355b4669Sjacobs 
309*355b4669Sjacobs 		papiAttributeListGetString(list, NULL, "printer-name", &dest);
310*355b4669Sjacobs 		papiAttributeListGetInteger(list, NULL, "job-id", &id);
311*355b4669Sjacobs 		printf(gettext("request id is %s-%d "), dest, id);
312*355b4669Sjacobs 		if (ac != optind)
313*355b4669Sjacobs 			printf("(%d file(s))\n", ac - optind);
314*355b4669Sjacobs 		else
315*355b4669Sjacobs 			printf("(standard input)\n");
316*355b4669Sjacobs 
317*355b4669Sjacobs 		if (dump != 0) {
318*355b4669Sjacobs 			printf("job attributes:\n");
319*355b4669Sjacobs 			papiAttributeListPrint(stdout, list, "\t");
320*355b4669Sjacobs 			printf("\n");
321*355b4669Sjacobs 		}
322*355b4669Sjacobs 	}
323*355b4669Sjacobs 
324*355b4669Sjacobs 	papiJobFree(job);
325*355b4669Sjacobs 	papiServiceDestroy(svc);
326*355b4669Sjacobs 
327*355b4669Sjacobs 	return (0);
328*355b4669Sjacobs }
329