1*2264Sjacobs /*
2*2264Sjacobs * CDDL HEADER START
3*2264Sjacobs *
4*2264Sjacobs * The contents of this file are subject to the terms of the
5*2264Sjacobs * Common Development and Distribution License (the "License").
6*2264Sjacobs * You may not use this file except in compliance with the License.
7*2264Sjacobs *
8*2264Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2264Sjacobs * or http://www.opensolaris.org/os/licensing.
10*2264Sjacobs * See the License for the specific language governing permissions
11*2264Sjacobs * and limitations under the License.
12*2264Sjacobs *
13*2264Sjacobs * When distributing Covered Code, include this CDDL HEADER in each
14*2264Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2264Sjacobs * If applicable, add the following below this CDDL HEADER, with the
16*2264Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying
17*2264Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner]
18*2264Sjacobs *
19*2264Sjacobs * CDDL HEADER END
20*2264Sjacobs */
21*2264Sjacobs /*
22*2264Sjacobs * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23*2264Sjacobs * Use is subject to license terms.
24*2264Sjacobs */
25*2264Sjacobs
26*2264Sjacobs #pragma ident "%Z%%M% %I% %E% SMI"
27*2264Sjacobs
28*2264Sjacobs /*LINTLIBRARY*/
29*2264Sjacobs
30*2264Sjacobs #include <stdio.h>
31*2264Sjacobs #include <stdlib.h>
32*2264Sjacobs #include <unistd.h>
33*2264Sjacobs #include <sys/types.h>
34*2264Sjacobs #include <stdarg.h>
35*2264Sjacobs #include <string.h>
36*2264Sjacobs
37*2264Sjacobs #include <ns.h>
38*2264Sjacobs #include <list.h>
39*2264Sjacobs
40*2264Sjacobs /*
41*2264Sjacobs * Commonly Used routines...
42*2264Sjacobs */
43*2264Sjacobs
44*2264Sjacobs /*
45*2264Sjacobs * FUNCTION:
46*2264Sjacobs * kvp_create(const char *key, const void *value)
47*2264Sjacobs * INPUT(S):
48*2264Sjacobs * const char *key
49*2264Sjacobs * - key for key/value pair
50*2264Sjacobs * const void *value
51*2264Sjacobs * - value for key/value pair
52*2264Sjacobs * OUTPUT(S):
53*2264Sjacobs * ns_kvp_t * (return value)
54*2264Sjacobs * - pointer to structure containing the key/value pair
55*2264Sjacobs * DESCRIPTION:
56*2264Sjacobs */
57*2264Sjacobs ns_kvp_t *
ns_kvp_create(const char * key,const char * value)58*2264Sjacobs ns_kvp_create(const char *key, const char *value)
59*2264Sjacobs {
60*2264Sjacobs ns_kvp_t *kvp;
61*2264Sjacobs
62*2264Sjacobs if ((kvp = calloc(1, sizeof (*kvp))) != NULL) {
63*2264Sjacobs kvp->key = strdup(key);
64*2264Sjacobs kvp->value = (char *)value;
65*2264Sjacobs }
66*2264Sjacobs return (kvp);
67*2264Sjacobs }
68*2264Sjacobs
69*2264Sjacobs void
ns_kvp_destroy(ns_kvp_t * kvp)70*2264Sjacobs ns_kvp_destroy(ns_kvp_t *kvp)
71*2264Sjacobs {
72*2264Sjacobs if (kvp != NULL) {
73*2264Sjacobs if (kvp->key != NULL)
74*2264Sjacobs free(kvp->key);
75*2264Sjacobs if (kvp->value != NULL)
76*2264Sjacobs free(kvp->value);
77*2264Sjacobs free(kvp);
78*2264Sjacobs }
79*2264Sjacobs }
80*2264Sjacobs
81*2264Sjacobs
82*2264Sjacobs
83*2264Sjacobs
84*2264Sjacobs /*
85*2264Sjacobs * FUNCTION:
86*2264Sjacobs * ns_kvp_match_key(const ns_kvp_t *kvp, const char *key)
87*2264Sjacobs * INPUT(S):
88*2264Sjacobs * const ns_kvp_t *kvp
89*2264Sjacobs * - key/value pair to check
90*2264Sjacobs * const char *key
91*2264Sjacobs * - key for matching
92*2264Sjacobs * OUTPUT(S):
93*2264Sjacobs * int (return value)
94*2264Sjacobs * - 0 if matched
95*2264Sjacobs * DESCRIPTION:
96*2264Sjacobs */
97*2264Sjacobs static int
ns_kvp_match_key(const ns_kvp_t * kvp,char * key)98*2264Sjacobs ns_kvp_match_key(const ns_kvp_t *kvp, char *key)
99*2264Sjacobs {
100*2264Sjacobs if ((kvp != NULL) && (kvp->key != NULL) && (key != NULL))
101*2264Sjacobs return (strcmp(kvp->key, key));
102*2264Sjacobs return (-1);
103*2264Sjacobs }
104*2264Sjacobs
105*2264Sjacobs
106*2264Sjacobs /*
107*2264Sjacobs * FUNCTION:
108*2264Sjacobs * ns_r_get_value(const char *key, const ns_printer_t *printer)
109*2264Sjacobs * INPUT(S):
110*2264Sjacobs * const char *key
111*2264Sjacobs * - key for matching
112*2264Sjacobs * const ns_printer_t *printer
113*2264Sjacobs * - printer to glean this from
114*2264Sjacobs * OUTPUT(S):
115*2264Sjacobs * char * (return value)
116*2264Sjacobs * - NULL, if not matched
117*2264Sjacobs * DESCRIPTION:
118*2264Sjacobs */
119*2264Sjacobs static void *
ns_r_get_value(const char * key,const ns_printer_t * printer,int level)120*2264Sjacobs ns_r_get_value(const char *key, const ns_printer_t *printer, int level)
121*2264Sjacobs {
122*2264Sjacobs ns_kvp_t *kvp, **attrs;
123*2264Sjacobs
124*2264Sjacobs if ((key == NULL) || (printer == NULL) ||
125*2264Sjacobs (printer->attributes == NULL))
126*2264Sjacobs return (NULL);
127*2264Sjacobs
128*2264Sjacobs if (level++ == 16)
129*2264Sjacobs return (NULL);
130*2264Sjacobs
131*2264Sjacobs /* find it right here */
132*2264Sjacobs if ((kvp = list_locate((void **)printer->attributes,
133*2264Sjacobs (COMP_T)ns_kvp_match_key, (void *)key)) != NULL) {
134*2264Sjacobs void *value = string_to_value(key, kvp->value);
135*2264Sjacobs
136*2264Sjacobs /* fill in an empty printer for a bsdaddr */
137*2264Sjacobs if (strcmp(key, NS_KEY_BSDADDR) == 0) {
138*2264Sjacobs ns_bsd_addr_t *addr = value;
139*2264Sjacobs
140*2264Sjacobs if (addr->printer == NULL)
141*2264Sjacobs addr->printer = strdup(printer->name);
142*2264Sjacobs }
143*2264Sjacobs return (value);
144*2264Sjacobs }
145*2264Sjacobs
146*2264Sjacobs /* find it in a child */
147*2264Sjacobs for (attrs = printer->attributes; attrs != NULL && *attrs != NULL;
148*2264Sjacobs attrs++) {
149*2264Sjacobs void *value = NULL;
150*2264Sjacobs
151*2264Sjacobs if ((strcmp((*attrs)->key, NS_KEY_ALL) == 0) ||
152*2264Sjacobs (strcmp((*attrs)->key, NS_KEY_GROUP) == 0)) {
153*2264Sjacobs char **printers;
154*2264Sjacobs
155*2264Sjacobs for (printers = string_to_value((*attrs)->key,
156*2264Sjacobs (*attrs)->value);
157*2264Sjacobs printers != NULL && *printers != NULL; printers++) {
158*2264Sjacobs ns_printer_t *printer =
159*2264Sjacobs ns_printer_get_name(*printers, NULL);
160*2264Sjacobs
161*2264Sjacobs if ((value = ns_r_get_value(key, printer,
162*2264Sjacobs level)) != NULL)
163*2264Sjacobs return (value);
164*2264Sjacobs ns_printer_destroy(printer);
165*2264Sjacobs }
166*2264Sjacobs } else if (strcmp((*attrs)->key, NS_KEY_LIST) == 0) {
167*2264Sjacobs ns_printer_t **printers;
168*2264Sjacobs
169*2264Sjacobs for (printers = string_to_value((*attrs)->key,
170*2264Sjacobs (*attrs)->value);
171*2264Sjacobs printers != NULL && *printers != NULL; printers++) {
172*2264Sjacobs if ((value = ns_r_get_value(key, *printers,
173*2264Sjacobs level)) != NULL)
174*2264Sjacobs return (value);
175*2264Sjacobs }
176*2264Sjacobs } else if (strcmp((*attrs)->key, NS_KEY_USE) == 0) {
177*2264Sjacobs char *string = NULL;
178*2264Sjacobs ns_printer_t *printer =
179*2264Sjacobs ns_printer_get_name((*attrs)->value, NULL);
180*2264Sjacobs if ((value = ns_r_get_value(key, printer,
181*2264Sjacobs level)) != NULL)
182*2264Sjacobs string = value_to_string(string, value);
183*2264Sjacobs if (string != NULL)
184*2264Sjacobs value = string_to_value(key, string);
185*2264Sjacobs ns_printer_destroy(printer);
186*2264Sjacobs }
187*2264Sjacobs
188*2264Sjacobs if (value != NULL)
189*2264Sjacobs return (value);
190*2264Sjacobs }
191*2264Sjacobs
192*2264Sjacobs return (NULL);
193*2264Sjacobs }
194*2264Sjacobs
195*2264Sjacobs
196*2264Sjacobs /*
197*2264Sjacobs * ns_get_value() gets the value of the passed in attribute from the passed
198*2264Sjacobs * in printer structure. The value is returned in a converted format.
199*2264Sjacobs */
200*2264Sjacobs void *
ns_get_value(const char * key,const ns_printer_t * printer)201*2264Sjacobs ns_get_value(const char *key, const ns_printer_t *printer)
202*2264Sjacobs {
203*2264Sjacobs return (ns_r_get_value(key, printer, 0));
204*2264Sjacobs }
205*2264Sjacobs
206*2264Sjacobs
207*2264Sjacobs /*
208*2264Sjacobs * ns_get_value_string() gets the value of the key passed in from the
209*2264Sjacobs * printer structure passed in. The results is an ascii string.
210*2264Sjacobs */
211*2264Sjacobs char *
ns_get_value_string(const char * key,const ns_printer_t * printer)212*2264Sjacobs ns_get_value_string(const char *key, const ns_printer_t *printer)
213*2264Sjacobs {
214*2264Sjacobs return ((char *)value_to_string(key, ns_get_value(key, printer)));
215*2264Sjacobs }
216*2264Sjacobs
217*2264Sjacobs
218*2264Sjacobs /*
219*2264Sjacobs * ns_set_value() sets the passed in kvp in the passed in printer structure,
220*2264Sjacobs * This is done by converting the value to a string first.
221*2264Sjacobs */
222*2264Sjacobs int
ns_set_value(const char * key,const void * value,ns_printer_t * printer)223*2264Sjacobs ns_set_value(const char *key, const void *value, ns_printer_t *printer)
224*2264Sjacobs {
225*2264Sjacobs return (ns_set_value_from_string(key,
226*2264Sjacobs value_to_string(key, (void *)value), printer));
227*2264Sjacobs }
228*2264Sjacobs
229*2264Sjacobs
230*2264Sjacobs /*
231*2264Sjacobs * ns_set_value_from_string() sets the passed in kvp in the passed in printer
232*2264Sjacobs * structure.
233*2264Sjacobs */
234*2264Sjacobs int
ns_set_value_from_string(const char * key,const char * string,ns_printer_t * printer)235*2264Sjacobs ns_set_value_from_string(const char *key, const char *string,
236*2264Sjacobs ns_printer_t *printer)
237*2264Sjacobs {
238*2264Sjacobs if (printer == NULL)
239*2264Sjacobs return (-1);
240*2264Sjacobs
241*2264Sjacobs if (key == NULL)
242*2264Sjacobs list_iterate((void **)printer->attributes,
243*2264Sjacobs (VFUNC_T)ns_kvp_destroy);
244*2264Sjacobs else {
245*2264Sjacobs ns_kvp_t *kvp;
246*2264Sjacobs
247*2264Sjacobs if (((kvp = list_locate((void **)printer->attributes,
248*2264Sjacobs (COMP_T)ns_kvp_match_key,
249*2264Sjacobs (void *)key)) == NULL) &&
250*2264Sjacobs ((kvp = calloc(1, sizeof (*kvp))) != NULL)) {
251*2264Sjacobs kvp->key = strdup(key);
252*2264Sjacobs printer->attributes = (ns_kvp_t **)
253*2264Sjacobs list_append((void **)printer->attributes, kvp);
254*2264Sjacobs }
255*2264Sjacobs if (string != NULL)
256*2264Sjacobs kvp->value = strdup(string);
257*2264Sjacobs else
258*2264Sjacobs kvp->value = NULL;
259*2264Sjacobs }
260*2264Sjacobs
261*2264Sjacobs return (0);
262*2264Sjacobs }
263