xref: /titanic_51/usr/src/cmd/print/lpget/lpget.c (revision c1ecd8b9404ee0d96d93f02e82c441b9bb149a3d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * 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.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*c1ecd8b9Sjacobs  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23355b4669Sjacobs  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <stdarg.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <syslog.h>
357c478bd9Sstevel@tonic-gate #include <locale.h>
367c478bd9Sstevel@tonic-gate #ifndef SUNOS_4
377c478bd9Sstevel@tonic-gate #include <libintl.h>
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate 
40355b4669Sjacobs #include <ns.h>
41355b4669Sjacobs #include <list.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate extern char *optarg;
447c478bd9Sstevel@tonic-gate extern int optind, opterr, optopt;
457c478bd9Sstevel@tonic-gate extern char *getenv(const char *);
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static void
497c478bd9Sstevel@tonic-gate Usage(char *name)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
527c478bd9Sstevel@tonic-gate 		gettext("Usage: %s [-k key] [list|(printer) ...]\n"),
537c478bd9Sstevel@tonic-gate 		name);
547c478bd9Sstevel@tonic-gate 	exit(1);
557c478bd9Sstevel@tonic-gate }
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static int
587c478bd9Sstevel@tonic-gate display_kvp(char *key, char *value)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	int rc = -1;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	if (value != NULL) {
637c478bd9Sstevel@tonic-gate 		rc = 0;
647c478bd9Sstevel@tonic-gate 		(void) printf("\n\t%s=%s", key, value);
657c478bd9Sstevel@tonic-gate 	} else
667c478bd9Sstevel@tonic-gate 		(void) printf(gettext("\n\t%s - undefined"), key);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	return (rc);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static int
737c478bd9Sstevel@tonic-gate display_value(ns_printer_t *printer, char *name, char **keys)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	int rc = -1;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	if (printer != NULL) {
787c478bd9Sstevel@tonic-gate 		rc = 0;
797c478bd9Sstevel@tonic-gate 		(void) printf("%s:", name);
807c478bd9Sstevel@tonic-gate 		if (keys != NULL) {
817c478bd9Sstevel@tonic-gate 			while (*keys != NULL) {
827c478bd9Sstevel@tonic-gate 				char *string = ns_get_value_string(*keys,
837c478bd9Sstevel@tonic-gate 							printer);
847c478bd9Sstevel@tonic-gate 				rc += display_kvp(*keys, string);
857c478bd9Sstevel@tonic-gate 				keys++;
867c478bd9Sstevel@tonic-gate 			}
877c478bd9Sstevel@tonic-gate 		} else {
887c478bd9Sstevel@tonic-gate 			ns_kvp_t **list = printer->attributes;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 			for (list = printer->attributes;
917c478bd9Sstevel@tonic-gate 			    (list != NULL && *list != NULL); list++) {
927c478bd9Sstevel@tonic-gate 				char *string;
937c478bd9Sstevel@tonic-gate 				if (((*list)->key[0] == '\t') ||
947c478bd9Sstevel@tonic-gate 				    ((*list)->key[0] == ' '))
957c478bd9Sstevel@tonic-gate 					continue;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 				string = ns_get_value_string((*list)->key,
987c478bd9Sstevel@tonic-gate 							    printer);
997c478bd9Sstevel@tonic-gate 				rc += display_kvp((*list)->key, string);
1007c478bd9Sstevel@tonic-gate 			}
1017c478bd9Sstevel@tonic-gate 		}
1027c478bd9Sstevel@tonic-gate 		(void) printf("\n");
1037c478bd9Sstevel@tonic-gate 	} else
1047c478bd9Sstevel@tonic-gate 		(void) printf(gettext("%s: Not Found\n"), name);
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	return (rc);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  *  main() calls the appropriate routine to parse the command line arguments
1127c478bd9Sstevel@tonic-gate  *	and then calls the local remove routine, followed by the remote remove
1137c478bd9Sstevel@tonic-gate  *	routine to remove jobs.
1147c478bd9Sstevel@tonic-gate  */
1157c478bd9Sstevel@tonic-gate int
1167c478bd9Sstevel@tonic-gate main(int ac, char *av[])
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	char *program;
1197c478bd9Sstevel@tonic-gate 	int c;
1207c478bd9Sstevel@tonic-gate 	char **keys = NULL;
1217c478bd9Sstevel@tonic-gate 	char *ns = NULL;
1227c478bd9Sstevel@tonic-gate 	int exit_code = 0;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)
1277c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1287c478bd9Sstevel@tonic-gate #endif
1297c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if ((program = strrchr(av[0], '/')) == NULL)
1327c478bd9Sstevel@tonic-gate 		program = av[0];
1337c478bd9Sstevel@tonic-gate 	else
1347c478bd9Sstevel@tonic-gate 		program++;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	openlog(program, LOG_PID, LOG_LPR);
1377c478bd9Sstevel@tonic-gate 	while ((c = getopt(ac, av, "k:t:n:")) != EOF)
1387c478bd9Sstevel@tonic-gate 		switch (c) {
1397c478bd9Sstevel@tonic-gate 		case 'k':
1407c478bd9Sstevel@tonic-gate 		case 't':
1417c478bd9Sstevel@tonic-gate 			keys = (char **)list_append((void **)keys,
1427c478bd9Sstevel@tonic-gate 						    (void *)optarg);
1437c478bd9Sstevel@tonic-gate 			break;
1447c478bd9Sstevel@tonic-gate 		case 'n':
1457c478bd9Sstevel@tonic-gate 			ns = optarg;
1467c478bd9Sstevel@tonic-gate 			break;
1477c478bd9Sstevel@tonic-gate 		default:
1487c478bd9Sstevel@tonic-gate 			Usage(program);
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (optind >= ac)
1527c478bd9Sstevel@tonic-gate 		Usage(program);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	ns = normalize_ns_name(ns);
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	while (optind < ac) {
1577c478bd9Sstevel@tonic-gate 		char *name = av[optind++];
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 		if (strcmp(name, "list") == 0) {
1607c478bd9Sstevel@tonic-gate 			ns_printer_t **printers = ns_printer_get_list(ns);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 			while (printers != NULL && *printers != NULL) {
1637c478bd9Sstevel@tonic-gate 				exit_code += display_value(*printers,
1647c478bd9Sstevel@tonic-gate 						(*printers)->name, keys);
1657c478bd9Sstevel@tonic-gate 				printers++;
1667c478bd9Sstevel@tonic-gate 			}
1677c478bd9Sstevel@tonic-gate 		} else
1687c478bd9Sstevel@tonic-gate 			exit_code = display_value(ns_printer_get_name(name, ns),
1697c478bd9Sstevel@tonic-gate 						name, keys);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	return (exit_code);
1747c478bd9Sstevel@tonic-gate }
175