xref: /titanic_50/usr/src/cmd/print/lpget/lpget.c (revision f7209cf2d2e114162cab4b5252c05118a6b7e54a)
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 2006 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 <misc.h>
42 #include <list.h>
43 
44 extern char *optarg;
45 extern int optind, opterr, optopt;
46 extern char *getenv(const char *);
47 
48 
49 static void
50 Usage(char *name)
51 {
52 	(void) fprintf(stderr,
53 		gettext("Usage: %s [-k key] [list|(printer) ...]\n"),
54 		name);
55 	exit(1);
56 }
57 
58 static int
59 display_kvp(char *key, char *value)
60 {
61 	int rc = -1;
62 
63 	if (value != NULL) {
64 		rc = 0;
65 		(void) printf("\n\t%s=%s", key, value);
66 	} else
67 		(void) printf(gettext("\n\t%s - undefined"), key);
68 
69 	return (rc);
70 }
71 
72 
73 static int
74 display_value(ns_printer_t *printer, char *name, char **keys)
75 {
76 	int rc = -1;
77 
78 	if (printer != NULL) {
79 		rc = 0;
80 		(void) printf("%s:", name);
81 		if (keys != NULL) {
82 			while (*keys != NULL) {
83 				char *string = ns_get_value_string(*keys,
84 							printer);
85 				rc += display_kvp(*keys, string);
86 				keys++;
87 			}
88 		} else {
89 			ns_kvp_t **list = printer->attributes;
90 
91 			for (list = printer->attributes;
92 			    (list != NULL && *list != NULL); list++) {
93 				char *string;
94 				if (((*list)->key[0] == '\t') ||
95 				    ((*list)->key[0] == ' '))
96 					continue;
97 
98 				string = ns_get_value_string((*list)->key,
99 							    printer);
100 				rc += display_kvp((*list)->key, string);
101 			}
102 		}
103 		(void) printf("\n");
104 	} else
105 		(void) printf(gettext("%s: Not Found\n"), name);
106 
107 	return (rc);
108 }
109 
110 
111 /*
112  *  main() calls the appropriate routine to parse the command line arguments
113  *	and then calls the local remove routine, followed by the remote remove
114  *	routine to remove jobs.
115  */
116 int
117 main(int ac, char *av[])
118 {
119 	char *program;
120 	int c;
121 	char **keys = NULL;
122 	char *ns = NULL;
123 	int exit_code = 0;
124 
125 	(void) setlocale(LC_ALL, "");
126 
127 #if	!defined(TEXT_DOMAIN)
128 #define	TEXT_DOMAIN "SYS_TEST"
129 #endif
130 	(void) textdomain(TEXT_DOMAIN);
131 
132 	if ((program = strrchr(av[0], '/')) == NULL)
133 		program = av[0];
134 	else
135 		program++;
136 
137 	openlog(program, LOG_PID, LOG_LPR);
138 	while ((c = getopt(ac, av, "k:t:n:")) != EOF)
139 		switch (c) {
140 		case 'k':
141 		case 't':
142 			keys = (char **)list_append((void **)keys,
143 						    (void *)optarg);
144 			break;
145 		case 'n':
146 			ns = optarg;
147 			break;
148 		default:
149 			Usage(program);
150 		}
151 
152 	if (optind >= ac)
153 		Usage(program);
154 
155 	ns = normalize_ns_name(ns);
156 
157 	while (optind < ac) {
158 		char *name = av[optind++];
159 
160 		if (strcmp(name, "list") == 0) {
161 			ns_printer_t **printers = ns_printer_get_list(ns);
162 
163 			while (printers != NULL && *printers != NULL) {
164 				exit_code += display_value(*printers,
165 						(*printers)->name, keys);
166 				printers++;
167 			}
168 		} else
169 			exit_code = display_value(ns_printer_get_name(name, ns),
170 						name, keys);
171 
172 
173 	}
174 	return (exit_code);
175 }
176