1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert * prof_tree.c --- these routines maintain the parse tree of the
4*7f2fe78bSCy Schubert * config file.
5*7f2fe78bSCy Schubert *
6*7f2fe78bSCy Schubert * All of the details of how the tree is stored is abstracted away in
7*7f2fe78bSCy Schubert * this file; all of the other profile routines build, access, and
8*7f2fe78bSCy Schubert * modify the tree via the accessor functions found in this file.
9*7f2fe78bSCy Schubert *
10*7f2fe78bSCy Schubert * Each node may represent either a relation or a section header.
11*7f2fe78bSCy Schubert *
12*7f2fe78bSCy Schubert * A section header must have its value field be null, and may have one
13*7f2fe78bSCy Schubert * or more child nodes, pointed to by first_child.
14*7f2fe78bSCy Schubert *
15*7f2fe78bSCy Schubert * A relation has as its value a pointer to allocated memory
16*7f2fe78bSCy Schubert * containing a string. Its first_child pointer must be null.
17*7f2fe78bSCy Schubert *
18*7f2fe78bSCy Schubert */
19*7f2fe78bSCy Schubert
20*7f2fe78bSCy Schubert
21*7f2fe78bSCy Schubert #include "prof_int.h"
22*7f2fe78bSCy Schubert
23*7f2fe78bSCy Schubert #include <stdio.h>
24*7f2fe78bSCy Schubert #include <string.h>
25*7f2fe78bSCy Schubert #ifdef HAVE_STDLIB_H
26*7f2fe78bSCy Schubert #include <stdlib.h>
27*7f2fe78bSCy Schubert #endif
28*7f2fe78bSCy Schubert #include <errno.h>
29*7f2fe78bSCy Schubert #include <ctype.h>
30*7f2fe78bSCy Schubert
31*7f2fe78bSCy Schubert struct profile_node {
32*7f2fe78bSCy Schubert errcode_t magic;
33*7f2fe78bSCy Schubert char *name;
34*7f2fe78bSCy Schubert char *value;
35*7f2fe78bSCy Schubert int group_level;
36*7f2fe78bSCy Schubert unsigned int final:1; /* Indicate don't search next file */
37*7f2fe78bSCy Schubert unsigned int deleted:1;
38*7f2fe78bSCy Schubert struct profile_node *first_child;
39*7f2fe78bSCy Schubert struct profile_node *parent;
40*7f2fe78bSCy Schubert struct profile_node *next, *prev;
41*7f2fe78bSCy Schubert };
42*7f2fe78bSCy Schubert
43*7f2fe78bSCy Schubert #define CHECK_MAGIC(node) \
44*7f2fe78bSCy Schubert if ((node)->magic != PROF_MAGIC_NODE) \
45*7f2fe78bSCy Schubert return PROF_MAGIC_NODE;
46*7f2fe78bSCy Schubert
47*7f2fe78bSCy Schubert /*
48*7f2fe78bSCy Schubert * Free a node, and any children
49*7f2fe78bSCy Schubert */
profile_free_node(struct profile_node * node)50*7f2fe78bSCy Schubert void profile_free_node(struct profile_node *node)
51*7f2fe78bSCy Schubert {
52*7f2fe78bSCy Schubert struct profile_node *child, *next;
53*7f2fe78bSCy Schubert
54*7f2fe78bSCy Schubert if (node->magic != PROF_MAGIC_NODE)
55*7f2fe78bSCy Schubert return;
56*7f2fe78bSCy Schubert
57*7f2fe78bSCy Schubert if (node->name)
58*7f2fe78bSCy Schubert free(node->name);
59*7f2fe78bSCy Schubert if (node->value)
60*7f2fe78bSCy Schubert free(node->value);
61*7f2fe78bSCy Schubert
62*7f2fe78bSCy Schubert for (child=node->first_child; child; child = next) {
63*7f2fe78bSCy Schubert next = child->next;
64*7f2fe78bSCy Schubert profile_free_node(child);
65*7f2fe78bSCy Schubert }
66*7f2fe78bSCy Schubert node->magic = 0;
67*7f2fe78bSCy Schubert
68*7f2fe78bSCy Schubert free(node);
69*7f2fe78bSCy Schubert }
70*7f2fe78bSCy Schubert
71*7f2fe78bSCy Schubert #ifndef HAVE_STRDUP
72*7f2fe78bSCy Schubert #undef strdup
73*7f2fe78bSCy Schubert #define strdup MYstrdup
MYstrdup(const char * s)74*7f2fe78bSCy Schubert static char *MYstrdup (const char *s)
75*7f2fe78bSCy Schubert {
76*7f2fe78bSCy Schubert size_t sz = strlen(s) + 1;
77*7f2fe78bSCy Schubert char *p = malloc(sz);
78*7f2fe78bSCy Schubert if (p != 0)
79*7f2fe78bSCy Schubert memcpy(p, s, sz);
80*7f2fe78bSCy Schubert return p;
81*7f2fe78bSCy Schubert }
82*7f2fe78bSCy Schubert #endif
83*7f2fe78bSCy Schubert
84*7f2fe78bSCy Schubert /*
85*7f2fe78bSCy Schubert * Create a node
86*7f2fe78bSCy Schubert */
profile_create_node(const char * name,const char * value,struct profile_node ** ret_node)87*7f2fe78bSCy Schubert errcode_t profile_create_node(const char *name, const char *value,
88*7f2fe78bSCy Schubert struct profile_node **ret_node)
89*7f2fe78bSCy Schubert {
90*7f2fe78bSCy Schubert struct profile_node *new;
91*7f2fe78bSCy Schubert
92*7f2fe78bSCy Schubert new = malloc(sizeof(struct profile_node));
93*7f2fe78bSCy Schubert if (!new)
94*7f2fe78bSCy Schubert return ENOMEM;
95*7f2fe78bSCy Schubert memset(new, 0, sizeof(struct profile_node));
96*7f2fe78bSCy Schubert /* Set magic here so profile_free_node will free memory */
97*7f2fe78bSCy Schubert new->magic = PROF_MAGIC_NODE;
98*7f2fe78bSCy Schubert new->name = strdup(name);
99*7f2fe78bSCy Schubert if (new->name == 0) {
100*7f2fe78bSCy Schubert profile_free_node(new);
101*7f2fe78bSCy Schubert return ENOMEM;
102*7f2fe78bSCy Schubert }
103*7f2fe78bSCy Schubert if (value) {
104*7f2fe78bSCy Schubert new->value = strdup(value);
105*7f2fe78bSCy Schubert if (new->value == 0) {
106*7f2fe78bSCy Schubert profile_free_node(new);
107*7f2fe78bSCy Schubert return ENOMEM;
108*7f2fe78bSCy Schubert }
109*7f2fe78bSCy Schubert }
110*7f2fe78bSCy Schubert
111*7f2fe78bSCy Schubert *ret_node = new;
112*7f2fe78bSCy Schubert return 0;
113*7f2fe78bSCy Schubert }
114*7f2fe78bSCy Schubert
115*7f2fe78bSCy Schubert /*
116*7f2fe78bSCy Schubert * This function verifies that all of the representation invariants of
117*7f2fe78bSCy Schubert * the profile are true. If not, we have a programming bug somewhere,
118*7f2fe78bSCy Schubert * probably in this file.
119*7f2fe78bSCy Schubert */
profile_verify_node(struct profile_node * node)120*7f2fe78bSCy Schubert errcode_t profile_verify_node(struct profile_node *node)
121*7f2fe78bSCy Schubert {
122*7f2fe78bSCy Schubert struct profile_node *p, *last;
123*7f2fe78bSCy Schubert errcode_t retval;
124*7f2fe78bSCy Schubert
125*7f2fe78bSCy Schubert CHECK_MAGIC(node);
126*7f2fe78bSCy Schubert
127*7f2fe78bSCy Schubert if (node->value && node->first_child)
128*7f2fe78bSCy Schubert return PROF_SECTION_WITH_VALUE;
129*7f2fe78bSCy Schubert
130*7f2fe78bSCy Schubert last = 0;
131*7f2fe78bSCy Schubert for (p = node->first_child; p; last = p, p = p->next) {
132*7f2fe78bSCy Schubert if (p->prev != last)
133*7f2fe78bSCy Schubert return PROF_BAD_LINK_LIST;
134*7f2fe78bSCy Schubert if (last && (last->next != p))
135*7f2fe78bSCy Schubert return PROF_BAD_LINK_LIST;
136*7f2fe78bSCy Schubert if (node->group_level+1 != p->group_level)
137*7f2fe78bSCy Schubert return PROF_BAD_GROUP_LVL;
138*7f2fe78bSCy Schubert if (p->parent != node)
139*7f2fe78bSCy Schubert return PROF_BAD_PARENT_PTR;
140*7f2fe78bSCy Schubert retval = profile_verify_node(p);
141*7f2fe78bSCy Schubert if (retval)
142*7f2fe78bSCy Schubert return retval;
143*7f2fe78bSCy Schubert }
144*7f2fe78bSCy Schubert return 0;
145*7f2fe78bSCy Schubert }
146*7f2fe78bSCy Schubert
147*7f2fe78bSCy Schubert /*
148*7f2fe78bSCy Schubert * Add a node to a particular section
149*7f2fe78bSCy Schubert */
profile_add_node(struct profile_node * section,const char * name,const char * value,struct profile_node ** ret_node)150*7f2fe78bSCy Schubert errcode_t profile_add_node(struct profile_node *section, const char *name,
151*7f2fe78bSCy Schubert const char *value, struct profile_node **ret_node)
152*7f2fe78bSCy Schubert {
153*7f2fe78bSCy Schubert errcode_t retval;
154*7f2fe78bSCy Schubert struct profile_node *p, *last, *new;
155*7f2fe78bSCy Schubert
156*7f2fe78bSCy Schubert CHECK_MAGIC(section);
157*7f2fe78bSCy Schubert
158*7f2fe78bSCy Schubert if (section->value)
159*7f2fe78bSCy Schubert return PROF_ADD_NOT_SECTION;
160*7f2fe78bSCy Schubert
161*7f2fe78bSCy Schubert /*
162*7f2fe78bSCy Schubert * Find the place to insert the new node. If we are adding a subsection
163*7f2fe78bSCy Schubert * and already have a subsection with that name, merge them. Otherwise,
164*7f2fe78bSCy Schubert * we look for the place *after* the last match of the node name, since
165*7f2fe78bSCy Schubert * order matters.
166*7f2fe78bSCy Schubert */
167*7f2fe78bSCy Schubert for (p=section->first_child, last = 0; p; last = p, p = p->next) {
168*7f2fe78bSCy Schubert int cmp;
169*7f2fe78bSCy Schubert cmp = strcmp(p->name, name);
170*7f2fe78bSCy Schubert if (cmp > 0) {
171*7f2fe78bSCy Schubert break;
172*7f2fe78bSCy Schubert } else if (value == NULL && cmp == 0 &&
173*7f2fe78bSCy Schubert p->value == NULL && p->deleted != 1) {
174*7f2fe78bSCy Schubert /* Found duplicate subsection, so don't make a new one. */
175*7f2fe78bSCy Schubert *ret_node = p;
176*7f2fe78bSCy Schubert return 0;
177*7f2fe78bSCy Schubert }
178*7f2fe78bSCy Schubert }
179*7f2fe78bSCy Schubert retval = profile_create_node(name, value, &new);
180*7f2fe78bSCy Schubert if (retval)
181*7f2fe78bSCy Schubert return retval;
182*7f2fe78bSCy Schubert new->group_level = section->group_level+1;
183*7f2fe78bSCy Schubert new->deleted = 0;
184*7f2fe78bSCy Schubert new->parent = section;
185*7f2fe78bSCy Schubert new->prev = last;
186*7f2fe78bSCy Schubert new->next = p;
187*7f2fe78bSCy Schubert if (p)
188*7f2fe78bSCy Schubert p->prev = new;
189*7f2fe78bSCy Schubert if (last)
190*7f2fe78bSCy Schubert last->next = new;
191*7f2fe78bSCy Schubert else
192*7f2fe78bSCy Schubert section->first_child = new;
193*7f2fe78bSCy Schubert if (ret_node)
194*7f2fe78bSCy Schubert *ret_node = new;
195*7f2fe78bSCy Schubert return 0;
196*7f2fe78bSCy Schubert }
197*7f2fe78bSCy Schubert
198*7f2fe78bSCy Schubert /*
199*7f2fe78bSCy Schubert * Set the final flag on a particular node.
200*7f2fe78bSCy Schubert */
profile_make_node_final(struct profile_node * node)201*7f2fe78bSCy Schubert errcode_t profile_make_node_final(struct profile_node *node)
202*7f2fe78bSCy Schubert {
203*7f2fe78bSCy Schubert CHECK_MAGIC(node);
204*7f2fe78bSCy Schubert
205*7f2fe78bSCy Schubert node->final = 1;
206*7f2fe78bSCy Schubert return 0;
207*7f2fe78bSCy Schubert }
208*7f2fe78bSCy Schubert
209*7f2fe78bSCy Schubert /*
210*7f2fe78bSCy Schubert * Check the final flag on a node
211*7f2fe78bSCy Schubert */
profile_is_node_final(struct profile_node * node)212*7f2fe78bSCy Schubert int profile_is_node_final(struct profile_node *node)
213*7f2fe78bSCy Schubert {
214*7f2fe78bSCy Schubert return (node->final != 0);
215*7f2fe78bSCy Schubert }
216*7f2fe78bSCy Schubert
217*7f2fe78bSCy Schubert /*
218*7f2fe78bSCy Schubert * Return the name of a node. (Note: this is for internal functions
219*7f2fe78bSCy Schubert * only; if the name needs to be returned from an exported function,
220*7f2fe78bSCy Schubert * strdup it first!)
221*7f2fe78bSCy Schubert */
profile_get_node_name(struct profile_node * node)222*7f2fe78bSCy Schubert const char *profile_get_node_name(struct profile_node *node)
223*7f2fe78bSCy Schubert {
224*7f2fe78bSCy Schubert return node->name;
225*7f2fe78bSCy Schubert }
226*7f2fe78bSCy Schubert
227*7f2fe78bSCy Schubert /*
228*7f2fe78bSCy Schubert * Return the value of a node. (Note: this is for internal functions
229*7f2fe78bSCy Schubert * only; if the name needs to be returned from an exported function,
230*7f2fe78bSCy Schubert * strdup it first!)
231*7f2fe78bSCy Schubert */
profile_get_node_value(struct profile_node * node)232*7f2fe78bSCy Schubert const char *profile_get_node_value(struct profile_node *node)
233*7f2fe78bSCy Schubert {
234*7f2fe78bSCy Schubert return node->value;
235*7f2fe78bSCy Schubert }
236*7f2fe78bSCy Schubert
237*7f2fe78bSCy Schubert /*
238*7f2fe78bSCy Schubert * Iterate through the section, returning the nodes which match
239*7f2fe78bSCy Schubert * the given name. If name is NULL, then iterate through all the
240*7f2fe78bSCy Schubert * nodes in the section. If section_flag is non-zero, only return the
241*7f2fe78bSCy Schubert * section which matches the name; don't return relations. If value
242*7f2fe78bSCy Schubert * is non-NULL, then only return relations which match the requested
243*7f2fe78bSCy Schubert * value. (The value argument is ignored if section_flag is non-zero.)
244*7f2fe78bSCy Schubert *
245*7f2fe78bSCy Schubert * The first time this routine is called, the state pointer must be
246*7f2fe78bSCy Schubert * null. When this profile_find_node_relation() returns, if the state
247*7f2fe78bSCy Schubert * pointer is non-NULL, then this routine should be called again.
248*7f2fe78bSCy Schubert * (This won't happen if section_flag is non-zero, obviously.)
249*7f2fe78bSCy Schubert *
250*7f2fe78bSCy Schubert */
profile_find_node(struct profile_node * section,const char * name,const char * value,int section_flag,void ** state,struct profile_node ** node)251*7f2fe78bSCy Schubert errcode_t profile_find_node(struct profile_node *section, const char *name,
252*7f2fe78bSCy Schubert const char *value, int section_flag, void **state,
253*7f2fe78bSCy Schubert struct profile_node **node)
254*7f2fe78bSCy Schubert {
255*7f2fe78bSCy Schubert struct profile_node *p;
256*7f2fe78bSCy Schubert
257*7f2fe78bSCy Schubert CHECK_MAGIC(section);
258*7f2fe78bSCy Schubert p = *state;
259*7f2fe78bSCy Schubert if (p) {
260*7f2fe78bSCy Schubert CHECK_MAGIC(p);
261*7f2fe78bSCy Schubert } else
262*7f2fe78bSCy Schubert p = section->first_child;
263*7f2fe78bSCy Schubert
264*7f2fe78bSCy Schubert for (; p; p = p->next) {
265*7f2fe78bSCy Schubert if (name && (strcmp(p->name, name)))
266*7f2fe78bSCy Schubert continue;
267*7f2fe78bSCy Schubert if (section_flag) {
268*7f2fe78bSCy Schubert if (p->value)
269*7f2fe78bSCy Schubert continue;
270*7f2fe78bSCy Schubert } else {
271*7f2fe78bSCy Schubert if (!p->value)
272*7f2fe78bSCy Schubert continue;
273*7f2fe78bSCy Schubert if (value && (strcmp(p->value, value)))
274*7f2fe78bSCy Schubert continue;
275*7f2fe78bSCy Schubert }
276*7f2fe78bSCy Schubert if (p->deleted)
277*7f2fe78bSCy Schubert continue;
278*7f2fe78bSCy Schubert /* A match! */
279*7f2fe78bSCy Schubert if (node)
280*7f2fe78bSCy Schubert *node = p;
281*7f2fe78bSCy Schubert break;
282*7f2fe78bSCy Schubert }
283*7f2fe78bSCy Schubert if (p == 0) {
284*7f2fe78bSCy Schubert *state = 0;
285*7f2fe78bSCy Schubert return section_flag ? PROF_NO_SECTION : PROF_NO_RELATION;
286*7f2fe78bSCy Schubert }
287*7f2fe78bSCy Schubert /*
288*7f2fe78bSCy Schubert * OK, we've found one match; now let's try to find another
289*7f2fe78bSCy Schubert * one. This way, if we return a non-zero state pointer,
290*7f2fe78bSCy Schubert * there's guaranteed to be another match that's returned.
291*7f2fe78bSCy Schubert */
292*7f2fe78bSCy Schubert for (p = p->next; p; p = p->next) {
293*7f2fe78bSCy Schubert if (name && (strcmp(p->name, name)))
294*7f2fe78bSCy Schubert continue;
295*7f2fe78bSCy Schubert if (section_flag) {
296*7f2fe78bSCy Schubert if (p->value)
297*7f2fe78bSCy Schubert continue;
298*7f2fe78bSCy Schubert } else {
299*7f2fe78bSCy Schubert if (!p->value)
300*7f2fe78bSCy Schubert continue;
301*7f2fe78bSCy Schubert if (value && (strcmp(p->value, value)))
302*7f2fe78bSCy Schubert continue;
303*7f2fe78bSCy Schubert }
304*7f2fe78bSCy Schubert if (p->deleted)
305*7f2fe78bSCy Schubert continue;
306*7f2fe78bSCy Schubert /* A match! */
307*7f2fe78bSCy Schubert break;
308*7f2fe78bSCy Schubert }
309*7f2fe78bSCy Schubert *state = p;
310*7f2fe78bSCy Schubert return 0;
311*7f2fe78bSCy Schubert }
312*7f2fe78bSCy Schubert
313*7f2fe78bSCy Schubert
314*7f2fe78bSCy Schubert /*
315*7f2fe78bSCy Schubert * Iterate through the section, returning the relations which match
316*7f2fe78bSCy Schubert * the given name. If name is NULL, then iterate through all the
317*7f2fe78bSCy Schubert * relations in the section. The first time this routine is called,
318*7f2fe78bSCy Schubert * the state pointer must be null. When this profile_find_node_relation()
319*7f2fe78bSCy Schubert * returns, if the state pointer is non-NULL, then this routine should
320*7f2fe78bSCy Schubert * be called again.
321*7f2fe78bSCy Schubert *
322*7f2fe78bSCy Schubert * The returned character string in value points to the stored
323*7f2fe78bSCy Schubert * character string in the parse string. Before this string value is
324*7f2fe78bSCy Schubert * returned to a calling application (profile_find_node_relation is not an
325*7f2fe78bSCy Schubert * exported interface), it should be strdup()'ed.
326*7f2fe78bSCy Schubert */
profile_find_node_relation(struct profile_node * section,const char * name,void ** state,char ** ret_name,char ** value)327*7f2fe78bSCy Schubert errcode_t profile_find_node_relation(struct profile_node *section,
328*7f2fe78bSCy Schubert const char *name, void **state,
329*7f2fe78bSCy Schubert char **ret_name, char **value)
330*7f2fe78bSCy Schubert {
331*7f2fe78bSCy Schubert struct profile_node *p;
332*7f2fe78bSCy Schubert errcode_t retval;
333*7f2fe78bSCy Schubert
334*7f2fe78bSCy Schubert retval = profile_find_node(section, name, 0, 0, state, &p);
335*7f2fe78bSCy Schubert if (retval)
336*7f2fe78bSCy Schubert return retval;
337*7f2fe78bSCy Schubert
338*7f2fe78bSCy Schubert if (p) {
339*7f2fe78bSCy Schubert if (value)
340*7f2fe78bSCy Schubert *value = p->value;
341*7f2fe78bSCy Schubert if (ret_name)
342*7f2fe78bSCy Schubert *ret_name = p->name;
343*7f2fe78bSCy Schubert }
344*7f2fe78bSCy Schubert return 0;
345*7f2fe78bSCy Schubert }
346*7f2fe78bSCy Schubert
347*7f2fe78bSCy Schubert /*
348*7f2fe78bSCy Schubert * Iterate through the section, returning the subsections which match
349*7f2fe78bSCy Schubert * the given name. If name is NULL, then iterate through all the
350*7f2fe78bSCy Schubert * subsections in the section. The first time this routine is called,
351*7f2fe78bSCy Schubert * the state pointer must be null. When this profile_find_node_subsection()
352*7f2fe78bSCy Schubert * returns, if the state pointer is non-NULL, then this routine should
353*7f2fe78bSCy Schubert * be called again.
354*7f2fe78bSCy Schubert *
355*7f2fe78bSCy Schubert * This is (plus accessor functions for the name and value given a
356*7f2fe78bSCy Schubert * profile node) makes this function mostly syntactic sugar for
357*7f2fe78bSCy Schubert * profile_find_node.
358*7f2fe78bSCy Schubert */
profile_find_node_subsection(struct profile_node * section,const char * name,void ** state,char ** ret_name,struct profile_node ** subsection)359*7f2fe78bSCy Schubert errcode_t profile_find_node_subsection(struct profile_node *section,
360*7f2fe78bSCy Schubert const char *name, void **state,
361*7f2fe78bSCy Schubert char **ret_name,
362*7f2fe78bSCy Schubert struct profile_node **subsection)
363*7f2fe78bSCy Schubert {
364*7f2fe78bSCy Schubert struct profile_node *p;
365*7f2fe78bSCy Schubert errcode_t retval;
366*7f2fe78bSCy Schubert
367*7f2fe78bSCy Schubert retval = profile_find_node(section, name, 0, 1, state, &p);
368*7f2fe78bSCy Schubert if (retval)
369*7f2fe78bSCy Schubert return retval;
370*7f2fe78bSCy Schubert
371*7f2fe78bSCy Schubert if (p) {
372*7f2fe78bSCy Schubert if (subsection)
373*7f2fe78bSCy Schubert *subsection = p;
374*7f2fe78bSCy Schubert if (ret_name)
375*7f2fe78bSCy Schubert *ret_name = p->name;
376*7f2fe78bSCy Schubert }
377*7f2fe78bSCy Schubert return 0;
378*7f2fe78bSCy Schubert }
379*7f2fe78bSCy Schubert
380*7f2fe78bSCy Schubert /*
381*7f2fe78bSCy Schubert * This function returns the parent of a particular node.
382*7f2fe78bSCy Schubert */
profile_get_node_parent(struct profile_node * section,struct profile_node ** parent)383*7f2fe78bSCy Schubert errcode_t profile_get_node_parent(struct profile_node *section,
384*7f2fe78bSCy Schubert struct profile_node **parent)
385*7f2fe78bSCy Schubert {
386*7f2fe78bSCy Schubert *parent = section->parent;
387*7f2fe78bSCy Schubert return 0;
388*7f2fe78bSCy Schubert }
389*7f2fe78bSCy Schubert
390*7f2fe78bSCy Schubert /*
391*7f2fe78bSCy Schubert * This is a general-purpose iterator for returning all nodes that
392*7f2fe78bSCy Schubert * match the specified name array.
393*7f2fe78bSCy Schubert */
394*7f2fe78bSCy Schubert struct profile_node_iterator {
395*7f2fe78bSCy Schubert prf_magic_t magic;
396*7f2fe78bSCy Schubert int flags;
397*7f2fe78bSCy Schubert const char *const *names;
398*7f2fe78bSCy Schubert const char *name;
399*7f2fe78bSCy Schubert prf_file_t file;
400*7f2fe78bSCy Schubert int file_serial;
401*7f2fe78bSCy Schubert int done_idx;
402*7f2fe78bSCy Schubert struct profile_node *node;
403*7f2fe78bSCy Schubert int num;
404*7f2fe78bSCy Schubert };
405*7f2fe78bSCy Schubert
profile_node_iterator_create(profile_t profile,const char * const * names,int flags,void ** ret_iter)406*7f2fe78bSCy Schubert errcode_t profile_node_iterator_create(profile_t profile,
407*7f2fe78bSCy Schubert const char *const *names, int flags,
408*7f2fe78bSCy Schubert void **ret_iter)
409*7f2fe78bSCy Schubert {
410*7f2fe78bSCy Schubert struct profile_node_iterator *iter;
411*7f2fe78bSCy Schubert int done_idx = 0;
412*7f2fe78bSCy Schubert
413*7f2fe78bSCy Schubert if (profile == 0)
414*7f2fe78bSCy Schubert return PROF_NO_PROFILE;
415*7f2fe78bSCy Schubert if (profile->magic != PROF_MAGIC_PROFILE)
416*7f2fe78bSCy Schubert return PROF_MAGIC_PROFILE;
417*7f2fe78bSCy Schubert if (!names)
418*7f2fe78bSCy Schubert return PROF_BAD_NAMESET;
419*7f2fe78bSCy Schubert if (!(flags & PROFILE_ITER_LIST_SECTION)) {
420*7f2fe78bSCy Schubert if (!names[0])
421*7f2fe78bSCy Schubert return PROF_BAD_NAMESET;
422*7f2fe78bSCy Schubert done_idx = 1;
423*7f2fe78bSCy Schubert }
424*7f2fe78bSCy Schubert
425*7f2fe78bSCy Schubert iter = malloc(sizeof(*iter));
426*7f2fe78bSCy Schubert if (iter == NULL)
427*7f2fe78bSCy Schubert return ENOMEM;
428*7f2fe78bSCy Schubert
429*7f2fe78bSCy Schubert iter->magic = PROF_MAGIC_NODE_ITERATOR;
430*7f2fe78bSCy Schubert iter->names = names;
431*7f2fe78bSCy Schubert iter->flags = flags;
432*7f2fe78bSCy Schubert iter->file = profile->first_file;
433*7f2fe78bSCy Schubert iter->done_idx = done_idx;
434*7f2fe78bSCy Schubert iter->node = 0;
435*7f2fe78bSCy Schubert iter->num = 0;
436*7f2fe78bSCy Schubert *ret_iter = iter;
437*7f2fe78bSCy Schubert return 0;
438*7f2fe78bSCy Schubert }
439*7f2fe78bSCy Schubert
profile_node_iterator_free(void ** iter_p)440*7f2fe78bSCy Schubert void profile_node_iterator_free(void **iter_p)
441*7f2fe78bSCy Schubert {
442*7f2fe78bSCy Schubert struct profile_node_iterator *iter;
443*7f2fe78bSCy Schubert
444*7f2fe78bSCy Schubert if (!iter_p)
445*7f2fe78bSCy Schubert return;
446*7f2fe78bSCy Schubert iter = *iter_p;
447*7f2fe78bSCy Schubert if (!iter || iter->magic != PROF_MAGIC_NODE_ITERATOR)
448*7f2fe78bSCy Schubert return;
449*7f2fe78bSCy Schubert free(iter);
450*7f2fe78bSCy Schubert *iter_p = 0;
451*7f2fe78bSCy Schubert }
452*7f2fe78bSCy Schubert
453*7f2fe78bSCy Schubert /*
454*7f2fe78bSCy Schubert * Note: the returned character strings in ret_name and ret_value
455*7f2fe78bSCy Schubert * points to the stored character string in the parse string. Before
456*7f2fe78bSCy Schubert * this string value is returned to a calling application
457*7f2fe78bSCy Schubert * (profile_node_iterator is not an exported interface), it should be
458*7f2fe78bSCy Schubert * strdup()'ed.
459*7f2fe78bSCy Schubert */
profile_node_iterator(void ** iter_p,struct profile_node ** ret_node,char ** ret_name,char ** ret_value)460*7f2fe78bSCy Schubert errcode_t profile_node_iterator(void **iter_p,
461*7f2fe78bSCy Schubert struct profile_node **ret_node,
462*7f2fe78bSCy Schubert char **ret_name, char **ret_value)
463*7f2fe78bSCy Schubert {
464*7f2fe78bSCy Schubert struct profile_node_iterator *iter = *iter_p;
465*7f2fe78bSCy Schubert struct profile_node *section, *p;
466*7f2fe78bSCy Schubert const char *const *cpp;
467*7f2fe78bSCy Schubert errcode_t retval;
468*7f2fe78bSCy Schubert int skip_num = 0;
469*7f2fe78bSCy Schubert
470*7f2fe78bSCy Schubert if (!iter || iter->magic != PROF_MAGIC_NODE_ITERATOR)
471*7f2fe78bSCy Schubert return PROF_MAGIC_NODE_ITERATOR;
472*7f2fe78bSCy Schubert if (iter->file && iter->file->magic != PROF_MAGIC_FILE)
473*7f2fe78bSCy Schubert return PROF_MAGIC_FILE;
474*7f2fe78bSCy Schubert if (iter->file && iter->file->data->magic != PROF_MAGIC_FILE_DATA)
475*7f2fe78bSCy Schubert return PROF_MAGIC_FILE_DATA;
476*7f2fe78bSCy Schubert /*
477*7f2fe78bSCy Schubert * If the file has changed, then the node pointer is invalid,
478*7f2fe78bSCy Schubert * so we'll have search the file again looking for it.
479*7f2fe78bSCy Schubert */
480*7f2fe78bSCy Schubert if (iter->file)
481*7f2fe78bSCy Schubert k5_mutex_lock(&iter->file->data->lock);
482*7f2fe78bSCy Schubert if (iter->node && (iter->file->data->upd_serial != iter->file_serial)) {
483*7f2fe78bSCy Schubert iter->flags &= ~PROFILE_ITER_FINAL_SEEN;
484*7f2fe78bSCy Schubert skip_num = iter->num;
485*7f2fe78bSCy Schubert iter->node = 0;
486*7f2fe78bSCy Schubert }
487*7f2fe78bSCy Schubert if (iter->node && iter->node->magic != PROF_MAGIC_NODE) {
488*7f2fe78bSCy Schubert if (iter->file)
489*7f2fe78bSCy Schubert k5_mutex_unlock(&iter->file->data->lock);
490*7f2fe78bSCy Schubert return PROF_MAGIC_NODE;
491*7f2fe78bSCy Schubert }
492*7f2fe78bSCy Schubert get_new_file:
493*7f2fe78bSCy Schubert if (iter->node == 0) {
494*7f2fe78bSCy Schubert if (iter->file == 0 ||
495*7f2fe78bSCy Schubert (iter->flags & PROFILE_ITER_FINAL_SEEN)) {
496*7f2fe78bSCy Schubert if (iter->file)
497*7f2fe78bSCy Schubert k5_mutex_unlock(&iter->file->data->lock);
498*7f2fe78bSCy Schubert profile_node_iterator_free(iter_p);
499*7f2fe78bSCy Schubert if (ret_node)
500*7f2fe78bSCy Schubert *ret_node = 0;
501*7f2fe78bSCy Schubert if (ret_name)
502*7f2fe78bSCy Schubert *ret_name = 0;
503*7f2fe78bSCy Schubert if (ret_value)
504*7f2fe78bSCy Schubert *ret_value =0;
505*7f2fe78bSCy Schubert return 0;
506*7f2fe78bSCy Schubert }
507*7f2fe78bSCy Schubert if ((retval = profile_update_file_locked(iter->file, NULL))) {
508*7f2fe78bSCy Schubert k5_mutex_unlock(&iter->file->data->lock);
509*7f2fe78bSCy Schubert if (retval == ENOENT || retval == EACCES) {
510*7f2fe78bSCy Schubert /* XXX memory leak? */
511*7f2fe78bSCy Schubert iter->file = iter->file->next;
512*7f2fe78bSCy Schubert if (iter->file)
513*7f2fe78bSCy Schubert k5_mutex_lock(&iter->file->data->lock);
514*7f2fe78bSCy Schubert skip_num = 0;
515*7f2fe78bSCy Schubert retval = 0;
516*7f2fe78bSCy Schubert goto get_new_file;
517*7f2fe78bSCy Schubert } else {
518*7f2fe78bSCy Schubert profile_node_iterator_free(iter_p);
519*7f2fe78bSCy Schubert return retval;
520*7f2fe78bSCy Schubert }
521*7f2fe78bSCy Schubert }
522*7f2fe78bSCy Schubert iter->file_serial = iter->file->data->upd_serial;
523*7f2fe78bSCy Schubert /*
524*7f2fe78bSCy Schubert * Find the section to list if we are a LIST_SECTION,
525*7f2fe78bSCy Schubert * or find the containing section if not.
526*7f2fe78bSCy Schubert */
527*7f2fe78bSCy Schubert section = iter->file->data->root;
528*7f2fe78bSCy Schubert assert(section != NULL);
529*7f2fe78bSCy Schubert for (cpp = iter->names; cpp[iter->done_idx]; cpp++) {
530*7f2fe78bSCy Schubert for (p=section->first_child; p; p = p->next) {
531*7f2fe78bSCy Schubert if (!strcmp(p->name, *cpp) && !p->value && !p->deleted)
532*7f2fe78bSCy Schubert break;
533*7f2fe78bSCy Schubert }
534*7f2fe78bSCy Schubert if (!p) {
535*7f2fe78bSCy Schubert section = 0;
536*7f2fe78bSCy Schubert break;
537*7f2fe78bSCy Schubert }
538*7f2fe78bSCy Schubert section = p;
539*7f2fe78bSCy Schubert if (p->final)
540*7f2fe78bSCy Schubert iter->flags |= PROFILE_ITER_FINAL_SEEN;
541*7f2fe78bSCy Schubert }
542*7f2fe78bSCy Schubert if (!section) {
543*7f2fe78bSCy Schubert k5_mutex_unlock(&iter->file->data->lock);
544*7f2fe78bSCy Schubert iter->file = iter->file->next;
545*7f2fe78bSCy Schubert if (iter->file)
546*7f2fe78bSCy Schubert k5_mutex_lock(&iter->file->data->lock);
547*7f2fe78bSCy Schubert skip_num = 0;
548*7f2fe78bSCy Schubert goto get_new_file;
549*7f2fe78bSCy Schubert }
550*7f2fe78bSCy Schubert iter->name = *cpp;
551*7f2fe78bSCy Schubert iter->node = section->first_child;
552*7f2fe78bSCy Schubert }
553*7f2fe78bSCy Schubert /*
554*7f2fe78bSCy Schubert * OK, now we know iter->node is set up correctly. Let's do
555*7f2fe78bSCy Schubert * the search.
556*7f2fe78bSCy Schubert */
557*7f2fe78bSCy Schubert for (p = iter->node; p; p = p->next) {
558*7f2fe78bSCy Schubert if (iter->name && strcmp(p->name, iter->name))
559*7f2fe78bSCy Schubert continue;
560*7f2fe78bSCy Schubert if ((iter->flags & PROFILE_ITER_SECTIONS_ONLY) &&
561*7f2fe78bSCy Schubert p->value)
562*7f2fe78bSCy Schubert continue;
563*7f2fe78bSCy Schubert if ((iter->flags & PROFILE_ITER_RELATIONS_ONLY) &&
564*7f2fe78bSCy Schubert !p->value)
565*7f2fe78bSCy Schubert continue;
566*7f2fe78bSCy Schubert if (skip_num > 0) {
567*7f2fe78bSCy Schubert skip_num--;
568*7f2fe78bSCy Schubert continue;
569*7f2fe78bSCy Schubert }
570*7f2fe78bSCy Schubert if (p->deleted)
571*7f2fe78bSCy Schubert continue;
572*7f2fe78bSCy Schubert break;
573*7f2fe78bSCy Schubert }
574*7f2fe78bSCy Schubert iter->num++;
575*7f2fe78bSCy Schubert if (!p) {
576*7f2fe78bSCy Schubert k5_mutex_unlock(&iter->file->data->lock);
577*7f2fe78bSCy Schubert iter->file = iter->file->next;
578*7f2fe78bSCy Schubert if (iter->file)
579*7f2fe78bSCy Schubert k5_mutex_lock(&iter->file->data->lock);
580*7f2fe78bSCy Schubert iter->node = 0;
581*7f2fe78bSCy Schubert skip_num = 0;
582*7f2fe78bSCy Schubert goto get_new_file;
583*7f2fe78bSCy Schubert }
584*7f2fe78bSCy Schubert k5_mutex_unlock(&iter->file->data->lock);
585*7f2fe78bSCy Schubert if ((iter->node = p->next) == NULL)
586*7f2fe78bSCy Schubert iter->file = iter->file->next;
587*7f2fe78bSCy Schubert if (ret_node)
588*7f2fe78bSCy Schubert *ret_node = p;
589*7f2fe78bSCy Schubert if (ret_name)
590*7f2fe78bSCy Schubert *ret_name = p->name;
591*7f2fe78bSCy Schubert if (ret_value)
592*7f2fe78bSCy Schubert *ret_value = p->value;
593*7f2fe78bSCy Schubert return 0;
594*7f2fe78bSCy Schubert }
595*7f2fe78bSCy Schubert
596*7f2fe78bSCy Schubert /*
597*7f2fe78bSCy Schubert * Remove a particular node.
598*7f2fe78bSCy Schubert *
599*7f2fe78bSCy Schubert * TYT, 2/25/99
600*7f2fe78bSCy Schubert */
profile_remove_node(struct profile_node * node)601*7f2fe78bSCy Schubert errcode_t profile_remove_node(struct profile_node *node)
602*7f2fe78bSCy Schubert {
603*7f2fe78bSCy Schubert CHECK_MAGIC(node);
604*7f2fe78bSCy Schubert
605*7f2fe78bSCy Schubert if (node->parent == 0)
606*7f2fe78bSCy Schubert return PROF_EINVAL; /* Can't remove the root! */
607*7f2fe78bSCy Schubert
608*7f2fe78bSCy Schubert node->deleted = 1;
609*7f2fe78bSCy Schubert
610*7f2fe78bSCy Schubert return 0;
611*7f2fe78bSCy Schubert }
612*7f2fe78bSCy Schubert
613*7f2fe78bSCy Schubert /*
614*7f2fe78bSCy Schubert * Set the value of a specific node containing a relation.
615*7f2fe78bSCy Schubert *
616*7f2fe78bSCy Schubert * TYT, 2/25/99
617*7f2fe78bSCy Schubert */
profile_set_relation_value(struct profile_node * node,const char * new_value)618*7f2fe78bSCy Schubert errcode_t profile_set_relation_value(struct profile_node *node,
619*7f2fe78bSCy Schubert const char *new_value)
620*7f2fe78bSCy Schubert {
621*7f2fe78bSCy Schubert char *cp;
622*7f2fe78bSCy Schubert
623*7f2fe78bSCy Schubert CHECK_MAGIC(node);
624*7f2fe78bSCy Schubert
625*7f2fe78bSCy Schubert if (!node->value)
626*7f2fe78bSCy Schubert return PROF_SET_SECTION_VALUE;
627*7f2fe78bSCy Schubert
628*7f2fe78bSCy Schubert cp = strdup(new_value);
629*7f2fe78bSCy Schubert if (!cp)
630*7f2fe78bSCy Schubert return ENOMEM;
631*7f2fe78bSCy Schubert
632*7f2fe78bSCy Schubert free(node->value);
633*7f2fe78bSCy Schubert node->value = cp;
634*7f2fe78bSCy Schubert
635*7f2fe78bSCy Schubert return 0;
636*7f2fe78bSCy Schubert }
637*7f2fe78bSCy Schubert
638*7f2fe78bSCy Schubert /*
639*7f2fe78bSCy Schubert * Rename a specific node
640*7f2fe78bSCy Schubert *
641*7f2fe78bSCy Schubert * TYT 2/25/99
642*7f2fe78bSCy Schubert */
profile_rename_node(struct profile_node * node,const char * new_name)643*7f2fe78bSCy Schubert errcode_t profile_rename_node(struct profile_node *node, const char *new_name)
644*7f2fe78bSCy Schubert {
645*7f2fe78bSCy Schubert char *new_string;
646*7f2fe78bSCy Schubert struct profile_node *p, *last;
647*7f2fe78bSCy Schubert
648*7f2fe78bSCy Schubert CHECK_MAGIC(node);
649*7f2fe78bSCy Schubert
650*7f2fe78bSCy Schubert if (strcmp(new_name, node->name) == 0)
651*7f2fe78bSCy Schubert return 0; /* It's the same name, return */
652*7f2fe78bSCy Schubert
653*7f2fe78bSCy Schubert /*
654*7f2fe78bSCy Schubert * Make sure we can allocate memory for the new name, first!
655*7f2fe78bSCy Schubert */
656*7f2fe78bSCy Schubert new_string = strdup(new_name);
657*7f2fe78bSCy Schubert if (!new_string)
658*7f2fe78bSCy Schubert return ENOMEM;
659*7f2fe78bSCy Schubert
660*7f2fe78bSCy Schubert /*
661*7f2fe78bSCy Schubert * Find the place to where the new node should go. We look
662*7f2fe78bSCy Schubert * for the place *after* the last match of the node name,
663*7f2fe78bSCy Schubert * since order matters.
664*7f2fe78bSCy Schubert */
665*7f2fe78bSCy Schubert for (p=node->parent->first_child, last = 0; p; last = p, p = p->next) {
666*7f2fe78bSCy Schubert if (strcmp(p->name, new_name) > 0)
667*7f2fe78bSCy Schubert break;
668*7f2fe78bSCy Schubert }
669*7f2fe78bSCy Schubert
670*7f2fe78bSCy Schubert /*
671*7f2fe78bSCy Schubert * If we need to move the node, do it now.
672*7f2fe78bSCy Schubert */
673*7f2fe78bSCy Schubert if ((p != node) && (last != node)) {
674*7f2fe78bSCy Schubert /*
675*7f2fe78bSCy Schubert * OK, let's detach the node
676*7f2fe78bSCy Schubert */
677*7f2fe78bSCy Schubert if (node->prev)
678*7f2fe78bSCy Schubert node->prev->next = node->next;
679*7f2fe78bSCy Schubert else
680*7f2fe78bSCy Schubert node->parent->first_child = node->next;
681*7f2fe78bSCy Schubert if (node->next)
682*7f2fe78bSCy Schubert node->next->prev = node->prev;
683*7f2fe78bSCy Schubert
684*7f2fe78bSCy Schubert /*
685*7f2fe78bSCy Schubert * Now let's reattach it in the right place.
686*7f2fe78bSCy Schubert */
687*7f2fe78bSCy Schubert if (p)
688*7f2fe78bSCy Schubert p->prev = node;
689*7f2fe78bSCy Schubert if (last)
690*7f2fe78bSCy Schubert last->next = node;
691*7f2fe78bSCy Schubert else
692*7f2fe78bSCy Schubert node->parent->first_child = node;
693*7f2fe78bSCy Schubert node->next = p;
694*7f2fe78bSCy Schubert node->prev = last;
695*7f2fe78bSCy Schubert }
696*7f2fe78bSCy Schubert
697*7f2fe78bSCy Schubert free(node->name);
698*7f2fe78bSCy Schubert node->name = new_string;
699*7f2fe78bSCy Schubert return 0;
700*7f2fe78bSCy Schubert }
701