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