xref: /illumos-gate/usr/src/cmd/print/lpget/lpget.c (revision 2a8bcb4e)
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 #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <stdarg.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <syslog.h>
337c478bd9Sstevel@tonic-gate #include <locale.h>
347c478bd9Sstevel@tonic-gate #ifndef SUNOS_4
357c478bd9Sstevel@tonic-gate #include <libintl.h>
367c478bd9Sstevel@tonic-gate #endif
377c478bd9Sstevel@tonic-gate 
38355b4669Sjacobs #include <ns.h>
39355b4669Sjacobs #include <list.h>
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate extern char *optarg;
427c478bd9Sstevel@tonic-gate extern int optind, opterr, optopt;
437c478bd9Sstevel@tonic-gate extern char *getenv(const char *);
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static void
Usage(char * name)477c478bd9Sstevel@tonic-gate Usage(char *name)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
507c478bd9Sstevel@tonic-gate 		gettext("Usage: %s [-k key] [list|(printer) ...]\n"),
517c478bd9Sstevel@tonic-gate 		name);
527c478bd9Sstevel@tonic-gate 	exit(1);
537c478bd9Sstevel@tonic-gate }
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static int
display_kvp(char * key,char * value)567c478bd9Sstevel@tonic-gate display_kvp(char *key, char *value)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	int rc = -1;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if (value != NULL) {
617c478bd9Sstevel@tonic-gate 		rc = 0;
627c478bd9Sstevel@tonic-gate 		(void) printf("\n\t%s=%s", key, value);
637c478bd9Sstevel@tonic-gate 	} else
647c478bd9Sstevel@tonic-gate 		(void) printf(gettext("\n\t%s - undefined"), key);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	return (rc);
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate static int
display_value(ns_printer_t * printer,char * name,char ** keys)717c478bd9Sstevel@tonic-gate display_value(ns_printer_t *printer, char *name, char **keys)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	int rc = -1;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if (printer != NULL) {
767c478bd9Sstevel@tonic-gate 		rc = 0;
777c478bd9Sstevel@tonic-gate 		(void) printf("%s:", name);
787c478bd9Sstevel@tonic-gate 		if (keys != NULL) {
797c478bd9Sstevel@tonic-gate 			while (*keys != NULL) {
807c478bd9Sstevel@tonic-gate 				char *string = ns_get_value_string(*keys,
817c478bd9Sstevel@tonic-gate 							printer);
827c478bd9Sstevel@tonic-gate 				rc += display_kvp(*keys, string);
837c478bd9Sstevel@tonic-gate 				keys++;
847c478bd9Sstevel@tonic-gate 			}
857c478bd9Sstevel@tonic-gate 		} else {
867c478bd9Sstevel@tonic-gate 			ns_kvp_t **list = printer->attributes;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 			for (list = printer->attributes;
897c478bd9Sstevel@tonic-gate 			    (list != NULL && *list != NULL); list++) {
907c478bd9Sstevel@tonic-gate 				char *string;
917c478bd9Sstevel@tonic-gate 				if (((*list)->key[0] == '\t') ||
927c478bd9Sstevel@tonic-gate 				    ((*list)->key[0] == ' '))
937c478bd9Sstevel@tonic-gate 					continue;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 				string = ns_get_value_string((*list)->key,
967c478bd9Sstevel@tonic-gate 							    printer);
977c478bd9Sstevel@tonic-gate 				rc += display_kvp((*list)->key, string);
987c478bd9Sstevel@tonic-gate 			}
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 		(void) printf("\n");
1017c478bd9Sstevel@tonic-gate 	} else
1027c478bd9Sstevel@tonic-gate 		(void) printf(gettext("%s: Not Found\n"), name);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	return (rc);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate  *  main() calls the appropriate routine to parse the command line arguments
1107c478bd9Sstevel@tonic-gate  *	and then calls the local remove routine, followed by the remote remove
1117c478bd9Sstevel@tonic-gate  *	routine to remove jobs.
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate int
main(int ac,char * av[])1147c478bd9Sstevel@tonic-gate main(int ac, char *av[])
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	char *program;
1177c478bd9Sstevel@tonic-gate 	int c;
1187c478bd9Sstevel@tonic-gate 	char **keys = NULL;
1197c478bd9Sstevel@tonic-gate 	char *ns = NULL;
1207c478bd9Sstevel@tonic-gate 	int exit_code = 0;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)
1257c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1267c478bd9Sstevel@tonic-gate #endif
1277c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if ((program = strrchr(av[0], '/')) == NULL)
1307c478bd9Sstevel@tonic-gate 		program = av[0];
1317c478bd9Sstevel@tonic-gate 	else
1327c478bd9Sstevel@tonic-gate 		program++;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	openlog(program, LOG_PID, LOG_LPR);
1357c478bd9Sstevel@tonic-gate 	while ((c = getopt(ac, av, "k:t:n:")) != EOF)
1367c478bd9Sstevel@tonic-gate 		switch (c) {
1377c478bd9Sstevel@tonic-gate 		case 'k':
1387c478bd9Sstevel@tonic-gate 		case 't':
1397c478bd9Sstevel@tonic-gate 			keys = (char **)list_append((void **)keys,
1407c478bd9Sstevel@tonic-gate 						    (void *)optarg);
1417c478bd9Sstevel@tonic-gate 			break;
1427c478bd9Sstevel@tonic-gate 		case 'n':
1437c478bd9Sstevel@tonic-gate 			ns = optarg;
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		default:
1467c478bd9Sstevel@tonic-gate 			Usage(program);
1477c478bd9Sstevel@tonic-gate 		}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (optind >= ac)
1507c478bd9Sstevel@tonic-gate 		Usage(program);
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	ns = normalize_ns_name(ns);
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	while (optind < ac) {
1557c478bd9Sstevel@tonic-gate 		char *name = av[optind++];
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		if (strcmp(name, "list") == 0) {
1587c478bd9Sstevel@tonic-gate 			ns_printer_t **printers = ns_printer_get_list(ns);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 			while (printers != NULL && *printers != NULL) {
1617c478bd9Sstevel@tonic-gate 				exit_code += display_value(*printers,
1627c478bd9Sstevel@tonic-gate 						(*printers)->name, keys);
1637c478bd9Sstevel@tonic-gate 				printers++;
1647c478bd9Sstevel@tonic-gate 			}
1657c478bd9Sstevel@tonic-gate 		} else
1667c478bd9Sstevel@tonic-gate 			exit_code = display_value(ns_printer_get_name(name, ns),
1677c478bd9Sstevel@tonic-gate 						name, keys);
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 	return (exit_code);
1727c478bd9Sstevel@tonic-gate }
173