xref: /illumos-gate/usr/src/cmd/print/lpget/lpget.c (revision b07ce584f4e28873b8927d7f83d9d3275a0f3ed2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <stdarg.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <syslog.h>
35 #include <locale.h>
36 #ifndef SUNOS_4
37 #include <libintl.h>
38 #endif
39 
40 #include <ns.h>
41 #include <list.h>
42 
43 extern char *optarg;
44 extern int optind, opterr, optopt;
45 extern char *getenv(const char *);
46 
47 
48 static void
49 Usage(char *name)
50 {
51 	(void) fprintf(stderr,
52 		gettext("Usage: %s [-k key] [list|(printer) ...]\n"),
53 		name);
54 	exit(1);
55 }
56 
57 static int
58 display_kvp(char *key, char *value)
59 {
60 	int rc = -1;
61 
62 	if (value != NULL) {
63 		rc = 0;
64 		(void) printf("\n\t%s=%s", key, value);
65 	} else
66 		(void) printf(gettext("\n\t%s - undefined"), key);
67 
68 	return (rc);
69 }
70 
71 
72 static int
73 display_value(ns_printer_t *printer, char *name, char **keys)
74 {
75 	int rc = -1;
76 
77 	if (printer != NULL) {
78 		rc = 0;
79 		(void) printf("%s:", name);
80 		if (keys != NULL) {
81 			while (*keys != NULL) {
82 				char *string = ns_get_value_string(*keys,
83 							printer);
84 				rc += display_kvp(*keys, string);
85 				keys++;
86 			}
87 		} else {
88 			ns_kvp_t **list = printer->attributes;
89 
90 			for (list = printer->attributes;
91 			    (list != NULL && *list != NULL); list++) {
92 				char *string;
93 				if (((*list)->key[0] == '\t') ||
94 				    ((*list)->key[0] == ' '))
95 					continue;
96 
97 				string = ns_get_value_string((*list)->key,
98 							    printer);
99 				rc += display_kvp((*list)->key, string);
100 			}
101 		}
102 		(void) printf("\n");
103 	} else
104 		(void) printf(gettext("%s: Not Found\n"), name);
105 
106 	return (rc);
107 }
108 
109 
110 /*
111  *  main() calls the appropriate routine to parse the command line arguments
112  *	and then calls the local remove routine, followed by the remote remove
113  *	routine to remove jobs.
114  */
115 int
116 main(int ac, char *av[])
117 {
118 	char *program;
119 	int c;
120 	char **keys = NULL;
121 	char *ns = NULL;
122 	int exit_code = 0;
123 
124 	(void) setlocale(LC_ALL, "");
125 
126 #if	!defined(TEXT_DOMAIN)
127 #define	TEXT_DOMAIN "SYS_TEST"
128 #endif
129 	(void) textdomain(TEXT_DOMAIN);
130 
131 	if ((program = strrchr(av[0], '/')) == NULL)
132 		program = av[0];
133 	else
134 		program++;
135 
136 	openlog(program, LOG_PID, LOG_LPR);
137 	while ((c = getopt(ac, av, "k:t:n:")) != EOF)
138 		switch (c) {
139 		case 'k':
140 		case 't':
141 			keys = (char **)list_append((void **)keys,
142 						    (void *)optarg);
143 			break;
144 		case 'n':
145 			ns = optarg;
146 			break;
147 		default:
148 			Usage(program);
149 		}
150 
151 	if (optind >= ac)
152 		Usage(program);
153 
154 	ns = normalize_ns_name(ns);
155 
156 	while (optind < ac) {
157 		char *name = av[optind++];
158 
159 		if (strcmp(name, "list") == 0) {
160 			ns_printer_t **printers = ns_printer_get_list(ns);
161 
162 			while (printers != NULL && *printers != NULL) {
163 				exit_code += display_value(*printers,
164 						(*printers)->name, keys);
165 				printers++;
166 			}
167 		} else
168 			exit_code = display_value(ns_printer_get_name(name, ns),
169 						name, keys);
170 
171 
172 	}
173 	return (exit_code);
174 }
175