17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5355b4669Sjacobs * Common Development and Distribution License (the "License").
6355b4669Sjacobs * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22355b4669Sjacobs * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate
35355b4669Sjacobs #include <ns.h>
36355b4669Sjacobs #include <list.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate * Commonly Used routines...
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate * FUNCTION:
447c478bd9Sstevel@tonic-gate * kvp_create(const char *key, const void *value)
457c478bd9Sstevel@tonic-gate * INPUT(S):
467c478bd9Sstevel@tonic-gate * const char *key
477c478bd9Sstevel@tonic-gate * - key for key/value pair
487c478bd9Sstevel@tonic-gate * const void *value
497c478bd9Sstevel@tonic-gate * - value for key/value pair
507c478bd9Sstevel@tonic-gate * OUTPUT(S):
517c478bd9Sstevel@tonic-gate * ns_kvp_t * (return value)
527c478bd9Sstevel@tonic-gate * - pointer to structure containing the key/value pair
537c478bd9Sstevel@tonic-gate * DESCRIPTION:
547c478bd9Sstevel@tonic-gate */
557c478bd9Sstevel@tonic-gate ns_kvp_t *
ns_kvp_create(const char * key,const char * value)567c478bd9Sstevel@tonic-gate ns_kvp_create(const char *key, const char *value)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate ns_kvp_t *kvp;
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate if ((kvp = calloc(1, sizeof (*kvp))) != NULL) {
617c478bd9Sstevel@tonic-gate kvp->key = strdup(key);
627c478bd9Sstevel@tonic-gate kvp->value = (char *)value;
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate return (kvp);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate
67*ef2333d1SToomas Soome int
ns_kvp_destroy(void * arg,va_list arg1 __unused)68*ef2333d1SToomas Soome ns_kvp_destroy(void *arg, va_list arg1 __unused)
697c478bd9Sstevel@tonic-gate {
70*ef2333d1SToomas Soome ns_kvp_t *kvp = arg;
71*ef2333d1SToomas Soome
727c478bd9Sstevel@tonic-gate if (kvp != NULL) {
737c478bd9Sstevel@tonic-gate if (kvp->key != NULL)
747c478bd9Sstevel@tonic-gate free(kvp->key);
757c478bd9Sstevel@tonic-gate if (kvp->value != NULL)
767c478bd9Sstevel@tonic-gate free(kvp->value);
777c478bd9Sstevel@tonic-gate free(kvp);
787c478bd9Sstevel@tonic-gate }
79*ef2333d1SToomas Soome return (0);
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate * FUNCTION:
877c478bd9Sstevel@tonic-gate * ns_kvp_match_key(const ns_kvp_t *kvp, const char *key)
887c478bd9Sstevel@tonic-gate * INPUT(S):
897c478bd9Sstevel@tonic-gate * const ns_kvp_t *kvp
907c478bd9Sstevel@tonic-gate * - key/value pair to check
917c478bd9Sstevel@tonic-gate * const char *key
927c478bd9Sstevel@tonic-gate * - key for matching
937c478bd9Sstevel@tonic-gate * OUTPUT(S):
947c478bd9Sstevel@tonic-gate * int (return value)
957c478bd9Sstevel@tonic-gate * - 0 if matched
967c478bd9Sstevel@tonic-gate * DESCRIPTION:
977c478bd9Sstevel@tonic-gate */
987c478bd9Sstevel@tonic-gate static int
ns_kvp_match_key(const ns_kvp_t * kvp,char * key)997c478bd9Sstevel@tonic-gate ns_kvp_match_key(const ns_kvp_t *kvp, char *key)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate if ((kvp != NULL) && (kvp->key != NULL) && (key != NULL))
1027c478bd9Sstevel@tonic-gate return (strcmp(kvp->key, key));
1037c478bd9Sstevel@tonic-gate return (-1);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate * FUNCTION:
1097c478bd9Sstevel@tonic-gate * ns_r_get_value(const char *key, const ns_printer_t *printer)
1107c478bd9Sstevel@tonic-gate * INPUT(S):
1117c478bd9Sstevel@tonic-gate * const char *key
1127c478bd9Sstevel@tonic-gate * - key for matching
1137c478bd9Sstevel@tonic-gate * const ns_printer_t *printer
1147c478bd9Sstevel@tonic-gate * - printer to glean this from
1157c478bd9Sstevel@tonic-gate * OUTPUT(S):
1167c478bd9Sstevel@tonic-gate * char * (return value)
1177c478bd9Sstevel@tonic-gate * - NULL, if not matched
1187c478bd9Sstevel@tonic-gate * DESCRIPTION:
1197c478bd9Sstevel@tonic-gate */
1207c478bd9Sstevel@tonic-gate static void *
ns_r_get_value(const char * key,const ns_printer_t * printer,int level)1217c478bd9Sstevel@tonic-gate ns_r_get_value(const char *key, const ns_printer_t *printer, int level)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate ns_kvp_t *kvp, **attrs;
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate if ((key == NULL) || (printer == NULL) ||
1267c478bd9Sstevel@tonic-gate (printer->attributes == NULL))
1277c478bd9Sstevel@tonic-gate return (NULL);
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate if (level++ == 16)
1307c478bd9Sstevel@tonic-gate return (NULL);
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate /* find it right here */
1337c478bd9Sstevel@tonic-gate if ((kvp = list_locate((void **)printer->attributes,
134*ef2333d1SToomas Soome (COMP_T)ns_kvp_match_key, (void *)key)) != NULL) {
1357c478bd9Sstevel@tonic-gate void *value = string_to_value(key, kvp->value);
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate /* fill in an empty printer for a bsdaddr */
1387c478bd9Sstevel@tonic-gate if (strcmp(key, NS_KEY_BSDADDR) == 0) {
1397c478bd9Sstevel@tonic-gate ns_bsd_addr_t *addr = value;
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate if (addr->printer == NULL)
1427c478bd9Sstevel@tonic-gate addr->printer = strdup(printer->name);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate return (value);
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /* find it in a child */
1487c478bd9Sstevel@tonic-gate for (attrs = printer->attributes; attrs != NULL && *attrs != NULL;
1497c478bd9Sstevel@tonic-gate attrs++) {
1507c478bd9Sstevel@tonic-gate void *value = NULL;
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate if ((strcmp((*attrs)->key, NS_KEY_ALL) == 0) ||
1537c478bd9Sstevel@tonic-gate (strcmp((*attrs)->key, NS_KEY_GROUP) == 0)) {
1547c478bd9Sstevel@tonic-gate char **printers;
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate for (printers = string_to_value((*attrs)->key,
157*ef2333d1SToomas Soome (*attrs)->value);
1587c478bd9Sstevel@tonic-gate printers != NULL && *printers != NULL; printers++) {
1597c478bd9Sstevel@tonic-gate ns_printer_t *printer =
160*ef2333d1SToomas Soome ns_printer_get_name(*printers, NULL);
1617c478bd9Sstevel@tonic-gate
162*ef2333d1SToomas Soome value = ns_r_get_value(key, printer, level);
163*ef2333d1SToomas Soome if (value != NULL)
1647c478bd9Sstevel@tonic-gate return (value);
1657c478bd9Sstevel@tonic-gate ns_printer_destroy(printer);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate } else if (strcmp((*attrs)->key, NS_KEY_LIST) == 0) {
1687c478bd9Sstevel@tonic-gate ns_printer_t **printers;
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate for (printers = string_to_value((*attrs)->key,
171*ef2333d1SToomas Soome (*attrs)->value);
1727c478bd9Sstevel@tonic-gate printers != NULL && *printers != NULL; printers++) {
173*ef2333d1SToomas Soome value = ns_r_get_value(key, *printers, level);
174*ef2333d1SToomas Soome if (value != NULL)
1757c478bd9Sstevel@tonic-gate return (value);
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate } else if (strcmp((*attrs)->key, NS_KEY_USE) == 0) {
1787c478bd9Sstevel@tonic-gate char *string = NULL;
1797c478bd9Sstevel@tonic-gate ns_printer_t *printer =
180*ef2333d1SToomas Soome ns_printer_get_name((*attrs)->value, NULL);
181*ef2333d1SToomas Soome value = ns_r_get_value(key, printer, level);
182*ef2333d1SToomas Soome if (value != NULL)
1837c478bd9Sstevel@tonic-gate string = value_to_string(string, value);
1847c478bd9Sstevel@tonic-gate if (string != NULL)
1857c478bd9Sstevel@tonic-gate value = string_to_value(key, string);
1867c478bd9Sstevel@tonic-gate ns_printer_destroy(printer);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate if (value != NULL)
1907c478bd9Sstevel@tonic-gate return (value);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate return (NULL);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate * ns_get_value() gets the value of the passed in attribute from the passed
1997c478bd9Sstevel@tonic-gate * in printer structure. The value is returned in a converted format.
2007c478bd9Sstevel@tonic-gate */
2017c478bd9Sstevel@tonic-gate void *
ns_get_value(const char * key,const ns_printer_t * printer)2027c478bd9Sstevel@tonic-gate ns_get_value(const char *key, const ns_printer_t *printer)
2037c478bd9Sstevel@tonic-gate {
2047c478bd9Sstevel@tonic-gate return (ns_r_get_value(key, printer, 0));
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate
2077c478bd9Sstevel@tonic-gate
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate * ns_get_value_string() gets the value of the key passed in from the
2107c478bd9Sstevel@tonic-gate * printer structure passed in. The results is an ascii string.
2117c478bd9Sstevel@tonic-gate */
2127c478bd9Sstevel@tonic-gate char *
ns_get_value_string(const char * key,const ns_printer_t * printer)2137c478bd9Sstevel@tonic-gate ns_get_value_string(const char *key, const ns_printer_t *printer)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate return ((char *)value_to_string(key, ns_get_value(key, printer)));
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate * ns_set_value() sets the passed in kvp in the passed in printer structure,
2217c478bd9Sstevel@tonic-gate * This is done by converting the value to a string first.
2227c478bd9Sstevel@tonic-gate */
2237c478bd9Sstevel@tonic-gate int
ns_set_value(const char * key,const void * value,ns_printer_t * printer)2247c478bd9Sstevel@tonic-gate ns_set_value(const char *key, const void *value, ns_printer_t *printer)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate return (ns_set_value_from_string(key,
227*ef2333d1SToomas Soome value_to_string(key, (void *)value), printer));
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate * ns_set_value_from_string() sets the passed in kvp in the passed in printer
2337c478bd9Sstevel@tonic-gate * structure.
2347c478bd9Sstevel@tonic-gate */
2357c478bd9Sstevel@tonic-gate int
ns_set_value_from_string(const char * key,const char * string,ns_printer_t * printer)2367c478bd9Sstevel@tonic-gate ns_set_value_from_string(const char *key, const char *string,
237*ef2333d1SToomas Soome ns_printer_t *printer)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate if (printer == NULL)
2407c478bd9Sstevel@tonic-gate return (-1);
2417c478bd9Sstevel@tonic-gate
242*ef2333d1SToomas Soome if (key == NULL) {
243*ef2333d1SToomas Soome list_iterate((void **)printer->attributes, ns_kvp_destroy);
244*ef2333d1SToomas Soome } else {
2457c478bd9Sstevel@tonic-gate ns_kvp_t *kvp;
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate if (((kvp = list_locate((void **)printer->attributes,
248*ef2333d1SToomas Soome (COMP_T)ns_kvp_match_key, (void *)key)) == NULL) &&
2497c478bd9Sstevel@tonic-gate ((kvp = calloc(1, sizeof (*kvp))) != NULL)) {
2507c478bd9Sstevel@tonic-gate kvp->key = strdup(key);
2517c478bd9Sstevel@tonic-gate printer->attributes = (ns_kvp_t **)
252*ef2333d1SToomas Soome list_append((void **)printer->attributes, kvp);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate if (string != NULL)
2557c478bd9Sstevel@tonic-gate kvp->value = strdup(string);
2567c478bd9Sstevel@tonic-gate else
2577c478bd9Sstevel@tonic-gate kvp->value = NULL;
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate return (0);
2617c478bd9Sstevel@tonic-gate }
262