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