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