12264Sjacobs /*
22264Sjacobs  * CDDL HEADER START
32264Sjacobs  *
42264Sjacobs  * The contents of this file are subject to the terms of the
52264Sjacobs  * Common Development and Distribution License (the "License").
62264Sjacobs  * You may not use this file except in compliance with the License.
72264Sjacobs  *
82264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92264Sjacobs  * or http://www.opensolaris.org/os/licensing.
102264Sjacobs  * See the License for the specific language governing permissions
112264Sjacobs  * and limitations under the License.
122264Sjacobs  *
132264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
142264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
162264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
172264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
182264Sjacobs  *
192264Sjacobs  * CDDL HEADER END
202264Sjacobs  */
212264Sjacobs 
222264Sjacobs /*
232264Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
242264Sjacobs  * Use is subject to license terms.
252264Sjacobs  *
262264Sjacobs  */
272264Sjacobs 
282264Sjacobs /* $Id: lpr.c 146 2006-03-24 00:26:54Z njacobs $ */
292264Sjacobs 
302264Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
312264Sjacobs 
322264Sjacobs #include <stdio.h>
332264Sjacobs #include <stdlib.h>
342264Sjacobs #include <unistd.h>
352264Sjacobs #include <string.h>
362264Sjacobs #include <locale.h>
372264Sjacobs #include <libintl.h>
382264Sjacobs #include <papi.h>
392264Sjacobs #include "common.h"
402264Sjacobs 
412264Sjacobs #ifdef HAVE_LIBMAGIC	/* for mimetype auto-detection */
422264Sjacobs #include <magic.h>
432264Sjacobs #endif /* HAVE_LIBMAGIC */
442264Sjacobs 
452264Sjacobs static void
462264Sjacobs usage(char *program)
472264Sjacobs {
482264Sjacobs 	char *name;
492264Sjacobs 
502264Sjacobs 	if ((name = strrchr(program, '/')) == NULL)
512264Sjacobs 		name = program;
522264Sjacobs 	else
532264Sjacobs 		name++;
542264Sjacobs 
552264Sjacobs 	fprintf(stdout,
562264Sjacobs 		gettext("Usage: %s [-P printer] [-# copies] [-C class] "
572264Sjacobs 				"[-J job] [-T title] "
582264Sjacobs 				"[-p [-i indent] [-w width]] "
592264Sjacobs 				"[-1|-2|-3|-4 font] [-m] [-h] [-s] "
602264Sjacobs 				"[-filter_option] [file ..]\n"), name);
612264Sjacobs 	exit(1);
622264Sjacobs }
632264Sjacobs 
642264Sjacobs int
652264Sjacobs main(int ac, char *av[])
662264Sjacobs {
672264Sjacobs 	papi_status_t status;
682264Sjacobs 	papi_service_t svc = NULL;
692264Sjacobs 	papi_attribute_t **list = NULL;
702264Sjacobs 	papi_job_t job = NULL;
712264Sjacobs 	int exit_code = 0;
722264Sjacobs 	char *printer = NULL;
732264Sjacobs 	papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
742264Sjacobs 	int dump = 0;
752264Sjacobs 	int validate = 0;
762264Sjacobs 	int remove = 0;
772264Sjacobs 	int copy = 1;	/* default is to copy the data */
78*3125Sjacobs 	char *document_format = "text/plain";
792264Sjacobs 	int c;
802264Sjacobs 
812264Sjacobs 	(void) setlocale(LC_ALL, "");
822264Sjacobs 	(void) textdomain("SUNW_OST_OSCMD");
832264Sjacobs 
842264Sjacobs 	while ((c = getopt(ac, av,
852264Sjacobs 			"EP:#:C:DVJ:T:w:i:hplrstdgvcfmn1:2:3:4:")) != EOF)
862264Sjacobs 		switch (c) {
872264Sjacobs 		case 'E':
882264Sjacobs 			encryption = PAPI_ENCRYPT_REQUIRED;
892264Sjacobs 			break;
902264Sjacobs 		case 'P':
912264Sjacobs 			printer = optarg;
922264Sjacobs 			break;
932264Sjacobs 		case '#':
942264Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
952264Sjacobs 					"copies", atoi(optarg));
962264Sjacobs 			break;
972264Sjacobs 		case 'C':
982264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
992264Sjacobs 					"rfc-1179-class", optarg);
1002264Sjacobs 			break;
1012264Sjacobs 		case 'D':
1022264Sjacobs 			dump = 1;
1032264Sjacobs 			break;
1042264Sjacobs 		case 'J':
1052264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1062264Sjacobs 					"job-name", optarg);
1072264Sjacobs 			break;
1082264Sjacobs 		case 'T':
1092264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1102264Sjacobs 					"pr-title", optarg);
1112264Sjacobs 			break;
1122264Sjacobs 		case 'p':
1132264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1142264Sjacobs 				"document-format", "application/x-pr");
1152264Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
1162264Sjacobs 					"pr-filter", 1);
1172264Sjacobs 			break;
1182264Sjacobs 		case 'i':
1192264Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
1202264Sjacobs 					"pr-indent", atoi(optarg));
1212264Sjacobs 			break;
1222264Sjacobs 		case 'w':
1232264Sjacobs 			papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
1242264Sjacobs 					"pr-width", atoi(optarg));
1252264Sjacobs 			break;
1262264Sjacobs 		case 'h':
1272264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1282264Sjacobs 					"job-sheets", "none");
1292264Sjacobs 			break;
1302264Sjacobs 		case 'l':
1312264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1322264Sjacobs 				"document-format", "application/octet-stream");
1332264Sjacobs 			break;
1342264Sjacobs 		case 'o':
1352264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1362264Sjacobs 				"document-format", "application/postscript");
1372264Sjacobs 			break;
1382264Sjacobs 		case 'c':
1392264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1402264Sjacobs 				"document-format", "application/x-cif");
1412264Sjacobs 			break;
1422264Sjacobs 		case 'd':
1432264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1442264Sjacobs 				"document-format", "application/x-dvi");
1452264Sjacobs 			break;
1462264Sjacobs 		case 'f':
1472264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1482264Sjacobs 				"document-format", "application/x-fortran");
1492264Sjacobs 			break;
1502264Sjacobs 		case 'g':
1512264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1522264Sjacobs 				"document-format", "application/x-plot");
1532264Sjacobs 			break;
1542264Sjacobs 		case 'n':
1552264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1562264Sjacobs 				"document-format", "application/x-ditroff");
1572264Sjacobs 			break;
1582264Sjacobs 		case 't':
1592264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1602264Sjacobs 				"document-format", "application/x-troff");
1612264Sjacobs 			break;
1622264Sjacobs 		case 'v':
1632264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1642264Sjacobs 				"document-format", "application/x-raster");
1652264Sjacobs 			break;
1662264Sjacobs 		case 'm':
1672264Sjacobs 			papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
1682264Sjacobs 				"rfc-1179-mail", 1);
1692264Sjacobs 			break;
1702264Sjacobs 		case 'r':
1712264Sjacobs 			remove = 1;
1722264Sjacobs 			break;
1732264Sjacobs 		case 's':
1742264Sjacobs 			copy = 0;
1752264Sjacobs 			break;
1762264Sjacobs 		case 'V':	/* validate */
1772264Sjacobs 			validate = 1;
1782264Sjacobs 			break;
1792264Sjacobs 		case '1':
1802264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1812264Sjacobs 				"rfc-1179-font-r", optarg);
1822264Sjacobs 			break;
1832264Sjacobs 		case '2':
1842264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1852264Sjacobs 				"rfc-1179-font-i", optarg);
1862264Sjacobs 			break;
1872264Sjacobs 		case '3':
1882264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1892264Sjacobs 				"rfc-1179-font-b", optarg);
1902264Sjacobs 			break;
1912264Sjacobs 		case '4':
1922264Sjacobs 			papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
1932264Sjacobs 				"rfc-1179-font-s", optarg);
1942264Sjacobs 			break;
1952264Sjacobs 		default:
1962264Sjacobs 			usage(av[0]);
1972264Sjacobs 		}
1982264Sjacobs 
1992264Sjacobs 	if ((remove != 0) && (copy == 0)) {
2002264Sjacobs 		fprintf(stderr, gettext(
2012264Sjacobs 			"-r and -s may not be used together\n"));
2022264Sjacobs 		exit(1);
2032264Sjacobs 	}
2042264Sjacobs 
2052264Sjacobs 	if ((printer == NULL) &&
2062264Sjacobs 	    ((printer = getenv("PRINTER")) == NULL) &&
2072264Sjacobs 	    ((printer = getenv("LPDEST")) == NULL))
2082264Sjacobs 		printer = DEFAULT_DEST;
2092264Sjacobs 
2102660Sjacobs 	if (((optind + 1) == ac) && (strcmp(av[optind], "-") == 0))
2112660Sjacobs 		optind = ac;
2122660Sjacobs 
2132264Sjacobs 	if (optind != ac) {
2142264Sjacobs 		/* get the mime type of the file data */
215*3125Sjacobs #ifdef MAGIC_MIME
2162264Sjacobs 		magic_t ms;
2172264Sjacobs 
2182264Sjacobs 		if ((ms = magic_open(MAGIC_MIME)) != NULL) {
2192264Sjacobs 			document_format = magic_file(ms, av[optind]);
2202264Sjacobs 			magic_close(ms);
2212264Sjacobs 		}
222*3125Sjacobs #else
223*3125Sjacobs 		if (is_postscript(av[optind]) == 1)
224*3125Sjacobs 			document_format = "application/postscript";
225*3125Sjacobs #endif
2262264Sjacobs 	}
2272264Sjacobs 
2282264Sjacobs 	papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1);
2292264Sjacobs 	papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
2302264Sjacobs 				"document-format", document_format);
2312264Sjacobs 	papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
2322264Sjacobs 				"job-sheets", "standard");
2332264Sjacobs 
2342264Sjacobs 	status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
2352264Sjacobs 					encryption, NULL);
2362264Sjacobs 	if (status != PAPI_OK) {
2372264Sjacobs 		fprintf(stderr, gettext(
2382264Sjacobs 			"Failed to contact service for %s: %s\n"), printer,
2392264Sjacobs 			verbose_papi_message(svc, status));
2402264Sjacobs 		exit(1);
2412264Sjacobs 	}
2422264Sjacobs 
2432264Sjacobs 	if (validate == 1)	/* validate the request can be processed */
2442264Sjacobs 		status = papiJobValidate(svc, printer, list,
2452264Sjacobs 					NULL, &av[optind], &job);
2462264Sjacobs 	else if (optind == ac)	/* no file list, use stdin */
2472264Sjacobs 		status = jobSubmitSTDIN(svc, printer, list, &job);
2482264Sjacobs 	else if (copy == 0)	/* reference the files in the job, default */
2492264Sjacobs 		status = papiJobSubmitByReference(svc, printer, list,
2502264Sjacobs 					NULL, &av[optind], &job);
2512264Sjacobs 	else			/* copy the files before return, -c */
2522264Sjacobs 		status = papiJobSubmit(svc, printer, list,
2532264Sjacobs 					NULL, &av[optind], &job);
2542264Sjacobs 
2552264Sjacobs 	papiAttributeListFree(list);
2562264Sjacobs 
2572264Sjacobs 	if (status != PAPI_OK) {
2582264Sjacobs 		fprintf(stderr, gettext("%s: %s\n"), printer,
2592264Sjacobs 			verbose_papi_message(svc, status));
2602264Sjacobs 		papiJobFree(job);
2612264Sjacobs 		papiServiceDestroy(svc);
2622264Sjacobs 		exit(1);
2632264Sjacobs 	}
2642264Sjacobs 
2652264Sjacobs 	if (dump != 0) {
2662264Sjacobs 		list = papiJobGetAttributeList(job);
2672264Sjacobs 		printf("job attributes:\n");
2682264Sjacobs 		papiAttributeListPrint(stdout, list, "\t");
2692264Sjacobs 		printf("\n");
2702264Sjacobs 	}
2712264Sjacobs 
2722264Sjacobs 	papiJobFree(job);
2732264Sjacobs 	papiServiceDestroy(svc);
2742264Sjacobs 
2752264Sjacobs 	return (exit_code);
2762264Sjacobs }
277