summaryrefslogtreecommitdiff
path: root/usr/src/lib/print/libprint/common/ns_cmn_kvp.c
blob: 9a36ddceac262012d3d2d7c90cb94862470dfd67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*LINTLIBRARY*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdarg.h>
#include <string.h>

#include <ns.h>
#include <list.h>

/*
 *	Commonly Used routines...
 */

/*
 * FUNCTION:
 *	kvp_create(const char *key, const void *value)
 * INPUT(S):
 *	const char *key
 *		- key for key/value pair
 *	const void *value
 *		- value for key/value pair
 * OUTPUT(S):
 *	ns_kvp_t * (return value)
 *		- pointer to structure containing the key/value pair
 * DESCRIPTION:
 */
ns_kvp_t *
ns_kvp_create(const char *key, const char *value)
{
	ns_kvp_t *kvp;

	if ((kvp = calloc(1, sizeof (*kvp))) != NULL) {
		kvp->key = strdup(key);
		kvp->value = (char *)value;
	}
	return (kvp);
}

int
ns_kvp_destroy(void *arg, va_list arg1 __unused)
{
	ns_kvp_t *kvp = arg;

	if (kvp != NULL) {
		if (kvp->key != NULL)
			free(kvp->key);
		if (kvp->value != NULL)
			free(kvp->value);
		free(kvp);
	}
	return (0);
}




/*
 * FUNCTION:
 *	ns_kvp_match_key(const ns_kvp_t *kvp, const char *key)
 * INPUT(S):
 *	const ns_kvp_t *kvp
 *		- key/value pair to check
 *	const char *key
 *		- key for matching
 * OUTPUT(S):
 *	int (return value)
 *		- 0 if matched
 * DESCRIPTION:
 */
static int
ns_kvp_match_key(const ns_kvp_t *kvp, char *key)
{
	if ((kvp != NULL) && (kvp->key != NULL) && (key != NULL))
		return (strcmp(kvp->key, key));
	return (-1);
}


/*
 * FUNCTION:
 *	ns_r_get_value(const char *key, const ns_printer_t *printer)
 * INPUT(S):
 *	const char *key
 *		- key for matching
 *	const ns_printer_t *printer
 *		- printer to glean this from
 * OUTPUT(S):
 *	char * (return value)
 *		- NULL, if not matched
 * DESCRIPTION:
 */
static void *
ns_r_get_value(const char *key, const ns_printer_t *printer, int level)
{
	ns_kvp_t *kvp, **attrs;

	if ((key == NULL) || (printer == NULL) ||
	    (printer->attributes == NULL))
		return (NULL);

	if (level++ == 16)
		return (NULL);

	/* find it right here */
	if ((kvp = list_locate((void **)printer->attributes,
	    (COMP_T)ns_kvp_match_key, (void *)key)) != NULL) {
		void *value = string_to_value(key, kvp->value);

		/* fill in an empty printer for a bsdaddr */
		if (strcmp(key, NS_KEY_BSDADDR) == 0) {
			ns_bsd_addr_t *addr = value;

			if (addr->printer == NULL)
				addr->printer = strdup(printer->name);
		}
		return (value);
	}

	/* find it in a child */
	for (attrs = printer->attributes; attrs != NULL && *attrs != NULL;
	    attrs++) {
		void *value = NULL;

		if ((strcmp((*attrs)->key, NS_KEY_ALL) == 0) ||
		    (strcmp((*attrs)->key, NS_KEY_GROUP) == 0)) {
			char **printers;

			for (printers = string_to_value((*attrs)->key,
			    (*attrs)->value);
			    printers != NULL && *printers != NULL; printers++) {
				ns_printer_t *printer =
				    ns_printer_get_name(*printers, NULL);

				value = ns_r_get_value(key, printer, level);
				if (value != NULL)
					return (value);
				ns_printer_destroy(printer);
			}
		} else if (strcmp((*attrs)->key, NS_KEY_LIST) == 0) {
			ns_printer_t **printers;

			for (printers = string_to_value((*attrs)->key,
			    (*attrs)->value);
			    printers != NULL && *printers != NULL; printers++) {
				value = ns_r_get_value(key, *printers, level);
				if (value != NULL)
					return (value);
			}
		} else if (strcmp((*attrs)->key, NS_KEY_USE) == 0) {
			char *string = NULL;
			ns_printer_t *printer =
			    ns_printer_get_name((*attrs)->value, NULL);
			value = ns_r_get_value(key, printer, level);
			if (value != NULL)
				string = value_to_string(string, value);
			if (string != NULL)
				value = string_to_value(key, string);
			ns_printer_destroy(printer);
		}

		if (value != NULL)
			return (value);
	}

	return (NULL);
}


/*
 * ns_get_value() gets the value of the passed in attribute from the passed
 * in printer structure.  The value is returned in a converted format.
 */
void *
ns_get_value(const char *key, const ns_printer_t *printer)
{
	return (ns_r_get_value(key, printer, 0));
}


/*
 * ns_get_value_string() gets the value of the key passed in from the
 * printer structure passed in.  The results is an ascii string.
 */
char *
ns_get_value_string(const char *key, const ns_printer_t *printer)
{
	return ((char *)value_to_string(key, ns_get_value(key, printer)));
}


/*
 * ns_set_value() sets the passed in kvp in the passed in printer structure,
 * This is done by converting the value to a string first.
 */
int
ns_set_value(const char *key, const void *value, ns_printer_t *printer)
{
	return (ns_set_value_from_string(key,
	    value_to_string(key, (void *)value), printer));
}


/*
 * ns_set_value_from_string() sets the passed in kvp in the passed in printer
 * structure.
 */
int
ns_set_value_from_string(const char *key, const char *string,
    ns_printer_t *printer)
{
	if (printer == NULL)
		return (-1);

	if (key == NULL) {
		list_iterate((void **)printer->attributes, ns_kvp_destroy);
	} else {
		ns_kvp_t *kvp;

		if (((kvp = list_locate((void **)printer->attributes,
		    (COMP_T)ns_kvp_match_key, (void *)key)) == NULL) &&
		    ((kvp = calloc(1, sizeof (*kvp))) != NULL)) {
			kvp->key = strdup(key);
			printer->attributes = (ns_kvp_t **)
			    list_append((void **)printer->attributes, kvp);
		}
		if (string != NULL)
			kvp->value = strdup(string);
		else
			kvp->value = NULL;
	}

	return (0);
}