17c478bd9Sstevel@tonic-gate /*
2159d09a2SMark Phalan * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
37c478bd9Sstevel@tonic-gate * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate
77c478bd9Sstevel@tonic-gate /*
87c478bd9Sstevel@tonic-gate * prof_tree.c --- these routines maintain the parse tree of the
97c478bd9Sstevel@tonic-gate * config file.
10*55fea89dSDan Cross *
117c478bd9Sstevel@tonic-gate * All of the details of how the tree is stored is abstracted away in
127c478bd9Sstevel@tonic-gate * this file; all of the other profile routines build, access, and
137c478bd9Sstevel@tonic-gate * modify the tree via the accessor functions found in this file.
147c478bd9Sstevel@tonic-gate *
157c478bd9Sstevel@tonic-gate * Each node may represent either a relation or a section header.
16*55fea89dSDan Cross *
177c478bd9Sstevel@tonic-gate * A section header must have its value field set to 0, and may a one
187c478bd9Sstevel@tonic-gate * or more child nodes, pointed to by first_child.
19*55fea89dSDan Cross *
207c478bd9Sstevel@tonic-gate * A relation has as its value a pointer to allocated memory
217c478bd9Sstevel@tonic-gate * containing a string. Its first_child pointer must be null.
227c478bd9Sstevel@tonic-gate *
237c478bd9Sstevel@tonic-gate */
247c478bd9Sstevel@tonic-gate
25505d05c7Sgtb
26505d05c7Sgtb #include "prof_int.h"
27505d05c7Sgtb
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #ifdef HAVE_STDLIB_H
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #endif
337c478bd9Sstevel@tonic-gate #include <errno.h>
347c478bd9Sstevel@tonic-gate #include <ctype.h>
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate struct profile_node {
377c478bd9Sstevel@tonic-gate errcode_t magic;
387c478bd9Sstevel@tonic-gate char *name;
397c478bd9Sstevel@tonic-gate char *value;
407c478bd9Sstevel@tonic-gate int group_level;
417c478bd9Sstevel@tonic-gate int final:1; /* Indicate don't search next file */
42505d05c7Sgtb int deleted:1;
437c478bd9Sstevel@tonic-gate struct profile_node *first_child;
447c478bd9Sstevel@tonic-gate struct profile_node *parent;
457c478bd9Sstevel@tonic-gate struct profile_node *next, *prev;
467c478bd9Sstevel@tonic-gate };
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate #define CHECK_MAGIC(node) \
497c478bd9Sstevel@tonic-gate if ((node)->magic != PROF_MAGIC_NODE) \
507c478bd9Sstevel@tonic-gate return PROF_MAGIC_NODE;
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate * Free a node, and any children
547c478bd9Sstevel@tonic-gate */
profile_free_node(struct profile_node * node)55505d05c7Sgtb void profile_free_node(struct profile_node *node)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate struct profile_node *child, *next;
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate if (node->magic != PROF_MAGIC_NODE)
607c478bd9Sstevel@tonic-gate return;
61*55fea89dSDan Cross
627c478bd9Sstevel@tonic-gate if (node->name)
637c478bd9Sstevel@tonic-gate free(node->name);
647c478bd9Sstevel@tonic-gate if (node->value)
657c478bd9Sstevel@tonic-gate free(node->value);
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate for (child=node->first_child; child; child = next) {
687c478bd9Sstevel@tonic-gate next = child->next;
697c478bd9Sstevel@tonic-gate profile_free_node(child);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate node->magic = 0;
72*55fea89dSDan Cross
737c478bd9Sstevel@tonic-gate free(node);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
76505d05c7Sgtb #ifndef HAVE_STRDUP
77505d05c7Sgtb #undef strdup
78505d05c7Sgtb #define strdup MYstrdup
MYstrdup(const char * s)79505d05c7Sgtb static char *MYstrdup (const char *s)
80505d05c7Sgtb {
81505d05c7Sgtb size_t sz = strlen(s) + 1;
82505d05c7Sgtb char *p = malloc(sz);
83505d05c7Sgtb if (p != 0)
84505d05c7Sgtb memcpy(p, s, sz);
85505d05c7Sgtb return p;
86505d05c7Sgtb }
87505d05c7Sgtb #endif
88505d05c7Sgtb
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate * Create a node
917c478bd9Sstevel@tonic-gate */
profile_create_node(const char * name,const char * value,struct profile_node ** ret_node)92505d05c7Sgtb errcode_t profile_create_node(const char *name, const char *value,
93505d05c7Sgtb struct profile_node **ret_node)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate struct profile_node *new;
967c478bd9Sstevel@tonic-gate
97159d09a2SMark Phalan new = malloc(sizeof(struct profile_node));
987c478bd9Sstevel@tonic-gate if (!new)
997c478bd9Sstevel@tonic-gate return ENOMEM;
1007c478bd9Sstevel@tonic-gate memset(new, 0, sizeof(struct profile_node));
101159d09a2SMark Phalan new->name = strdup(name);
1027c478bd9Sstevel@tonic-gate if (new->name == 0) {
103505d05c7Sgtb profile_free_node(new);
104505d05c7Sgtb return ENOMEM;
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate if (value) {
107159d09a2SMark Phalan new->value = strdup(value);
1087c478bd9Sstevel@tonic-gate if (new->value == 0) {
109505d05c7Sgtb profile_free_node(new);
110505d05c7Sgtb return ENOMEM;
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate new->magic = PROF_MAGIC_NODE;
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate *ret_node = new;
1167c478bd9Sstevel@tonic-gate return 0;
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate * This function verifies that all of the representation invarients of
1217c478bd9Sstevel@tonic-gate * the profile are true. If not, we have a programming bug somewhere,
1227c478bd9Sstevel@tonic-gate * probably in this file.
1237c478bd9Sstevel@tonic-gate */
profile_verify_node(struct profile_node * node)124505d05c7Sgtb errcode_t profile_verify_node(struct profile_node *node)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate struct profile_node *p, *last;
1277c478bd9Sstevel@tonic-gate errcode_t retval;
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate CHECK_MAGIC(node);
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate if (node->value && node->first_child)
1327c478bd9Sstevel@tonic-gate return PROF_SECTION_WITH_VALUE;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate last = 0;
1357c478bd9Sstevel@tonic-gate for (p = node->first_child; p; last = p, p = p->next) {
1367c478bd9Sstevel@tonic-gate if (p->prev != last)
1377c478bd9Sstevel@tonic-gate return PROF_BAD_LINK_LIST;
1387c478bd9Sstevel@tonic-gate if (last && (last->next != p))
1397c478bd9Sstevel@tonic-gate return PROF_BAD_LINK_LIST;
1407c478bd9Sstevel@tonic-gate if (node->group_level+1 != p->group_level)
1417c478bd9Sstevel@tonic-gate return PROF_BAD_GROUP_LVL;
1427c478bd9Sstevel@tonic-gate if (p->parent != node)
1437c478bd9Sstevel@tonic-gate return PROF_BAD_PARENT_PTR;
1447c478bd9Sstevel@tonic-gate retval = profile_verify_node(p);
1457c478bd9Sstevel@tonic-gate if (retval)
1467c478bd9Sstevel@tonic-gate return retval;
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate return 0;
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate /*
1527c478bd9Sstevel@tonic-gate * Add a node to a particular section
1537c478bd9Sstevel@tonic-gate */
profile_add_node(struct profile_node * section,const char * name,const char * value,struct profile_node ** ret_node)154505d05c7Sgtb errcode_t profile_add_node(struct profile_node *section, const char *name,
155505d05c7Sgtb const char *value, struct profile_node **ret_node)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate errcode_t retval;
1587c478bd9Sstevel@tonic-gate struct profile_node *p, *last, *new;
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate CHECK_MAGIC(section);
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate if (section->value)
1637c478bd9Sstevel@tonic-gate return PROF_ADD_NOT_SECTION;
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate * Find the place to insert the new node. We look for the
167*55fea89dSDan Cross * place *after* the last match of the node name, since
1687c478bd9Sstevel@tonic-gate * order matters.
1697c478bd9Sstevel@tonic-gate */
1707c478bd9Sstevel@tonic-gate for (p=section->first_child, last = 0; p; last = p, p = p->next) {
171505d05c7Sgtb int cmp;
1727c478bd9Sstevel@tonic-gate cmp = strcmp(p->name, name);
1737c478bd9Sstevel@tonic-gate if (cmp > 0)
1747c478bd9Sstevel@tonic-gate break;
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate retval = profile_create_node(name, value, &new);
1777c478bd9Sstevel@tonic-gate if (retval)
1787c478bd9Sstevel@tonic-gate return retval;
1797c478bd9Sstevel@tonic-gate new->group_level = section->group_level+1;
180505d05c7Sgtb new->deleted = 0;
1817c478bd9Sstevel@tonic-gate new->parent = section;
1827c478bd9Sstevel@tonic-gate new->prev = last;
1837c478bd9Sstevel@tonic-gate new->next = p;
1847c478bd9Sstevel@tonic-gate if (p)
1857c478bd9Sstevel@tonic-gate p->prev = new;
1867c478bd9Sstevel@tonic-gate if (last)
1877c478bd9Sstevel@tonic-gate last->next = new;
1887c478bd9Sstevel@tonic-gate else
1897c478bd9Sstevel@tonic-gate section->first_child = new;
1907c478bd9Sstevel@tonic-gate if (ret_node)
1917c478bd9Sstevel@tonic-gate *ret_node = new;
1927c478bd9Sstevel@tonic-gate return 0;
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate * Set the final flag on a particular node.
1977c478bd9Sstevel@tonic-gate */
profile_make_node_final(struct profile_node * node)198505d05c7Sgtb errcode_t profile_make_node_final(struct profile_node *node)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate CHECK_MAGIC(node);
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate node->final = 1;
2037c478bd9Sstevel@tonic-gate return 0;
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate * Check the final flag on a node
2087c478bd9Sstevel@tonic-gate */
profile_is_node_final(struct profile_node * node)209505d05c7Sgtb int profile_is_node_final(struct profile_node *node)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate return (node->final != 0);
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate
2147c478bd9Sstevel@tonic-gate /*
2157c478bd9Sstevel@tonic-gate * Return the name of a node. (Note: this is for internal functions
2167c478bd9Sstevel@tonic-gate * only; if the name needs to be returned from an exported function,
2177c478bd9Sstevel@tonic-gate * strdup it first!)
2187c478bd9Sstevel@tonic-gate */
profile_get_node_name(struct profile_node * node)219505d05c7Sgtb const char *profile_get_node_name(struct profile_node *node)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate return node->name;
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate * Return the value of a node. (Note: this is for internal functions
2267c478bd9Sstevel@tonic-gate * only; if the name needs to be returned from an exported function,
2277c478bd9Sstevel@tonic-gate * strdup it first!)
2287c478bd9Sstevel@tonic-gate */
profile_get_node_value(struct profile_node * node)229505d05c7Sgtb const char *profile_get_node_value(struct profile_node *node)
2307c478bd9Sstevel@tonic-gate {
2317c478bd9Sstevel@tonic-gate return node->value;
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate * Iterate through the section, returning the nodes which match
2367c478bd9Sstevel@tonic-gate * the given name. If name is NULL, then interate through all the
2377c478bd9Sstevel@tonic-gate * nodes in the section. If section_flag is non-zero, only return the
2387c478bd9Sstevel@tonic-gate * section which matches the name; don't return relations. If value
2397c478bd9Sstevel@tonic-gate * is non-NULL, then only return relations which match the requested
2407c478bd9Sstevel@tonic-gate * value. (The value argument is ignored if section_flag is non-zero.)
241*55fea89dSDan Cross *
2427c478bd9Sstevel@tonic-gate * The first time this routine is called, the state pointer must be
2437c478bd9Sstevel@tonic-gate * null. When this profile_find_node_relation() returns, if the state
2447c478bd9Sstevel@tonic-gate * pointer is non-NULL, then this routine should be called again.
2457c478bd9Sstevel@tonic-gate * (This won't happen if section_flag is non-zero, obviously.)
2467c478bd9Sstevel@tonic-gate *
2477c478bd9Sstevel@tonic-gate */
profile_find_node(struct profile_node * section,const char * name,const char * value,int section_flag,void ** state,struct profile_node ** node)248505d05c7Sgtb errcode_t profile_find_node(struct profile_node *section, const char *name,
249505d05c7Sgtb const char *value, int section_flag, void **state,
250505d05c7Sgtb struct profile_node **node)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate struct profile_node *p;
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate CHECK_MAGIC(section);
2557c478bd9Sstevel@tonic-gate p = *state;
2567c478bd9Sstevel@tonic-gate if (p) {
2577c478bd9Sstevel@tonic-gate CHECK_MAGIC(p);
2587c478bd9Sstevel@tonic-gate } else
2597c478bd9Sstevel@tonic-gate p = section->first_child;
260*55fea89dSDan Cross
2617c478bd9Sstevel@tonic-gate for (; p; p = p->next) {
2627c478bd9Sstevel@tonic-gate if (name && (strcmp(p->name, name)))
2637c478bd9Sstevel@tonic-gate continue;
2647c478bd9Sstevel@tonic-gate if (section_flag) {
2657c478bd9Sstevel@tonic-gate if (p->value)
2667c478bd9Sstevel@tonic-gate continue;
2677c478bd9Sstevel@tonic-gate } else {
2687c478bd9Sstevel@tonic-gate if (!p->value)
2697c478bd9Sstevel@tonic-gate continue;
2707c478bd9Sstevel@tonic-gate if (value && (strcmp(p->value, value)))
2717c478bd9Sstevel@tonic-gate continue;
2727c478bd9Sstevel@tonic-gate }
273505d05c7Sgtb if (p->deleted)
274505d05c7Sgtb continue;
2757c478bd9Sstevel@tonic-gate /* A match! */
2767c478bd9Sstevel@tonic-gate if (node)
2777c478bd9Sstevel@tonic-gate *node = p;
2787c478bd9Sstevel@tonic-gate break;
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate if (p == 0) {
2817c478bd9Sstevel@tonic-gate *state = 0;
2827c478bd9Sstevel@tonic-gate return section_flag ? PROF_NO_SECTION : PROF_NO_RELATION;
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate /*
2857c478bd9Sstevel@tonic-gate * OK, we've found one match; now let's try to find another
2867c478bd9Sstevel@tonic-gate * one. This way, if we return a non-zero state pointer,
2877c478bd9Sstevel@tonic-gate * there's guaranteed to be another match that's returned.
2887c478bd9Sstevel@tonic-gate */
2897c478bd9Sstevel@tonic-gate for (p = p->next; p; p = p->next) {
2907c478bd9Sstevel@tonic-gate if (name && (strcmp(p->name, name)))
2917c478bd9Sstevel@tonic-gate continue;
2927c478bd9Sstevel@tonic-gate if (section_flag) {
2937c478bd9Sstevel@tonic-gate if (p->value)
2947c478bd9Sstevel@tonic-gate continue;
2957c478bd9Sstevel@tonic-gate } else {
2967c478bd9Sstevel@tonic-gate if (!p->value)
2977c478bd9Sstevel@tonic-gate continue;
2987c478bd9Sstevel@tonic-gate if (value && (strcmp(p->value, value)))
2997c478bd9Sstevel@tonic-gate continue;
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate /* A match! */
3027c478bd9Sstevel@tonic-gate break;
3037c478bd9Sstevel@tonic-gate }
3047c478bd9Sstevel@tonic-gate *state = p;
3057c478bd9Sstevel@tonic-gate return 0;
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate * Iterate through the section, returning the relations which match
3117c478bd9Sstevel@tonic-gate * the given name. If name is NULL, then interate through all the
3127c478bd9Sstevel@tonic-gate * relations in the section. The first time this routine is called,
3137c478bd9Sstevel@tonic-gate * the state pointer must be null. When this profile_find_node_relation()
3147c478bd9Sstevel@tonic-gate * returns, if the state pointer is non-NULL, then this routine should
3157c478bd9Sstevel@tonic-gate * be called again.
3167c478bd9Sstevel@tonic-gate *
3177c478bd9Sstevel@tonic-gate * The returned character string in value points to the stored
3187c478bd9Sstevel@tonic-gate * character string in the parse string. Before this string value is
3197c478bd9Sstevel@tonic-gate * returned to a calling application (profile_find_node_relation is not an
3207c478bd9Sstevel@tonic-gate * exported interface), it should be strdup()'ed.
3217c478bd9Sstevel@tonic-gate */
profile_find_node_relation(struct profile_node * section,const char * name,void ** state,char ** ret_name,char ** value)322505d05c7Sgtb errcode_t profile_find_node_relation(struct profile_node *section,
323505d05c7Sgtb const char *name, void **state,
324505d05c7Sgtb char **ret_name, char **value)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate struct profile_node *p;
3277c478bd9Sstevel@tonic-gate errcode_t retval;
3287c478bd9Sstevel@tonic-gate
3297c478bd9Sstevel@tonic-gate retval = profile_find_node(section, name, 0, 0, state, &p);
3307c478bd9Sstevel@tonic-gate if (retval)
3317c478bd9Sstevel@tonic-gate return retval;
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate if (p) {
3347c478bd9Sstevel@tonic-gate if (value)
3357c478bd9Sstevel@tonic-gate *value = p->value;
3367c478bd9Sstevel@tonic-gate if (ret_name)
3377c478bd9Sstevel@tonic-gate *ret_name = p->name;
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate return 0;
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate /*
3437c478bd9Sstevel@tonic-gate * Iterate through the section, returning the subsections which match
3447c478bd9Sstevel@tonic-gate * the given name. If name is NULL, then interate through all the
3457c478bd9Sstevel@tonic-gate * subsections in the section. The first time this routine is called,
3467c478bd9Sstevel@tonic-gate * the state pointer must be null. When this profile_find_node_subsection()
3477c478bd9Sstevel@tonic-gate * returns, if the state pointer is non-NULL, then this routine should
3487c478bd9Sstevel@tonic-gate * be called again.
3497c478bd9Sstevel@tonic-gate *
3507c478bd9Sstevel@tonic-gate * This is (plus accessor functions for the name and value given a
3517c478bd9Sstevel@tonic-gate * profile node) makes this function mostly syntactic sugar for
352*55fea89dSDan Cross * profile_find_node.
3537c478bd9Sstevel@tonic-gate */
profile_find_node_subsection(struct profile_node * section,const char * name,void ** state,char ** ret_name,struct profile_node ** subsection)354505d05c7Sgtb errcode_t profile_find_node_subsection(struct profile_node *section,
355505d05c7Sgtb const char *name, void **state,
356505d05c7Sgtb char **ret_name,
357505d05c7Sgtb struct profile_node **subsection)
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate struct profile_node *p;
3607c478bd9Sstevel@tonic-gate errcode_t retval;
3617c478bd9Sstevel@tonic-gate
362159d09a2SMark Phalan /* Solaris Kerberos */
3637c478bd9Sstevel@tonic-gate if (section == (struct profile_node *)NULL)
3647c478bd9Sstevel@tonic-gate return (PROF_NO_PROFILE);
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate retval = profile_find_node(section, name, 0, 1, state, &p);
3677c478bd9Sstevel@tonic-gate if (retval)
3687c478bd9Sstevel@tonic-gate return retval;
3697c478bd9Sstevel@tonic-gate
3707c478bd9Sstevel@tonic-gate if (p) {
3717c478bd9Sstevel@tonic-gate if (subsection)
3727c478bd9Sstevel@tonic-gate *subsection = p;
3737c478bd9Sstevel@tonic-gate if (ret_name)
3747c478bd9Sstevel@tonic-gate *ret_name = p->name;
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate return 0;
3777c478bd9Sstevel@tonic-gate }
3787c478bd9Sstevel@tonic-gate
3797c478bd9Sstevel@tonic-gate /*
3807c478bd9Sstevel@tonic-gate * This function returns the parent of a particular node.
3817c478bd9Sstevel@tonic-gate */
profile_get_node_parent(struct profile_node * section,struct profile_node ** parent)382505d05c7Sgtb errcode_t profile_get_node_parent(struct profile_node *section,
383505d05c7Sgtb struct profile_node **parent)
3847c478bd9Sstevel@tonic-gate {
3857c478bd9Sstevel@tonic-gate *parent = section->parent;
3867c478bd9Sstevel@tonic-gate return 0;
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate * This is a general-purpose iterator for returning all nodes that
391*55fea89dSDan Cross * match the specified name array.
3927c478bd9Sstevel@tonic-gate */
3937c478bd9Sstevel@tonic-gate struct profile_iterator {
3947c478bd9Sstevel@tonic-gate prf_magic_t magic;
3957c478bd9Sstevel@tonic-gate profile_t profile;
3967c478bd9Sstevel@tonic-gate int flags;
397505d05c7Sgtb const char *const *names;
3987c478bd9Sstevel@tonic-gate const char *name;
3997c478bd9Sstevel@tonic-gate prf_file_t file;
4007c478bd9Sstevel@tonic-gate int file_serial;
4017c478bd9Sstevel@tonic-gate int done_idx;
4027c478bd9Sstevel@tonic-gate struct profile_node *node;
4037c478bd9Sstevel@tonic-gate int num;
4047c478bd9Sstevel@tonic-gate };
4057c478bd9Sstevel@tonic-gate
profile_node_iterator_create(profile_t profile,const char * const * names,int flags,void ** ret_iter)406505d05c7Sgtb errcode_t profile_node_iterator_create(profile_t profile,
407505d05c7Sgtb const char *const *names, int flags,
408505d05c7Sgtb void **ret_iter)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate struct profile_iterator *iter;
4117c478bd9Sstevel@tonic-gate int done_idx = 0;
4127c478bd9Sstevel@tonic-gate
4137c478bd9Sstevel@tonic-gate if (profile == 0)
4147c478bd9Sstevel@tonic-gate return PROF_NO_PROFILE;
4157c478bd9Sstevel@tonic-gate if (profile->magic != PROF_MAGIC_PROFILE)
4167c478bd9Sstevel@tonic-gate return PROF_MAGIC_PROFILE;
4177c478bd9Sstevel@tonic-gate if (!names)
4187c478bd9Sstevel@tonic-gate return PROF_BAD_NAMESET;
4197c478bd9Sstevel@tonic-gate if (!(flags & PROFILE_ITER_LIST_SECTION)) {
4207c478bd9Sstevel@tonic-gate if (!names[0])
4217c478bd9Sstevel@tonic-gate return PROF_BAD_NAMESET;
4227c478bd9Sstevel@tonic-gate done_idx = 1;
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate
425159d09a2SMark Phalan if ((iter = malloc(sizeof(struct profile_iterator))) == NULL)
4267c478bd9Sstevel@tonic-gate return ENOMEM;
4277c478bd9Sstevel@tonic-gate
4287c478bd9Sstevel@tonic-gate iter->magic = PROF_MAGIC_ITERATOR;
4297c478bd9Sstevel@tonic-gate iter->profile = profile;
4307c478bd9Sstevel@tonic-gate iter->names = names;
4317c478bd9Sstevel@tonic-gate iter->flags = flags;
4327c478bd9Sstevel@tonic-gate iter->file = profile->first_file;
4337c478bd9Sstevel@tonic-gate iter->done_idx = done_idx;
4347c478bd9Sstevel@tonic-gate iter->node = 0;
4357c478bd9Sstevel@tonic-gate iter->num = 0;
4367c478bd9Sstevel@tonic-gate *ret_iter = iter;
4377c478bd9Sstevel@tonic-gate return 0;
4387c478bd9Sstevel@tonic-gate }
4397c478bd9Sstevel@tonic-gate
profile_node_iterator_free(void ** iter_p)440505d05c7Sgtb void profile_node_iterator_free(void **iter_p)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate struct profile_iterator *iter;
4437c478bd9Sstevel@tonic-gate
4447c478bd9Sstevel@tonic-gate if (!iter_p)
4457c478bd9Sstevel@tonic-gate return;
4467c478bd9Sstevel@tonic-gate iter = *iter_p;
4477c478bd9Sstevel@tonic-gate if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
4487c478bd9Sstevel@tonic-gate return;
4497c478bd9Sstevel@tonic-gate free(iter);
4507c478bd9Sstevel@tonic-gate *iter_p = 0;
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate
4537c478bd9Sstevel@tonic-gate /*
4547c478bd9Sstevel@tonic-gate * Note: the returned character strings in ret_name and ret_value
4557c478bd9Sstevel@tonic-gate * points to the stored character string in the parse string. Before
4567c478bd9Sstevel@tonic-gate * this string value is returned to a calling application
4577c478bd9Sstevel@tonic-gate * (profile_node_iterator is not an exported interface), it should be
4587c478bd9Sstevel@tonic-gate * strdup()'ed.
4597c478bd9Sstevel@tonic-gate */
profile_node_iterator(void ** iter_p,struct profile_node ** ret_node,char ** ret_name,char ** ret_value)460505d05c7Sgtb errcode_t profile_node_iterator(void **iter_p, struct profile_node **ret_node,
461505d05c7Sgtb char **ret_name, char **ret_value)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate struct profile_iterator *iter = *iter_p;
4647c478bd9Sstevel@tonic-gate struct profile_node *section, *p;
465505d05c7Sgtb const char *const *cpp;
4667c478bd9Sstevel@tonic-gate errcode_t retval;
4677c478bd9Sstevel@tonic-gate int skip_num = 0;
4687c478bd9Sstevel@tonic-gate
4697c478bd9Sstevel@tonic-gate if (!iter || iter->magic != PROF_MAGIC_ITERATOR)
4707c478bd9Sstevel@tonic-gate return PROF_MAGIC_ITERATOR;
471505d05c7Sgtb if (iter->file && iter->file->magic != PROF_MAGIC_FILE)
472505d05c7Sgtb return PROF_MAGIC_FILE;
473505d05c7Sgtb if (iter->file && iter->file->data->magic != PROF_MAGIC_FILE_DATA)
474505d05c7Sgtb return PROF_MAGIC_FILE_DATA;
4757c478bd9Sstevel@tonic-gate /*
4767c478bd9Sstevel@tonic-gate * If the file has changed, then the node pointer is invalid,
4777c478bd9Sstevel@tonic-gate * so we'll have search the file again looking for it.
4787c478bd9Sstevel@tonic-gate */
479505d05c7Sgtb if (iter->file) {
480505d05c7Sgtb retval = k5_mutex_lock(&iter->file->data->lock);
481505d05c7Sgtb if (retval)
482505d05c7Sgtb return retval;
483505d05c7Sgtb }
484505d05c7Sgtb if (iter->node && (iter->file->data->upd_serial != iter->file_serial)) {
4857c478bd9Sstevel@tonic-gate iter->flags &= ~PROFILE_ITER_FINAL_SEEN;
4867c478bd9Sstevel@tonic-gate skip_num = iter->num;
4877c478bd9Sstevel@tonic-gate iter->node = 0;
4887c478bd9Sstevel@tonic-gate }
489505d05c7Sgtb if (iter->node && iter->node->magic != PROF_MAGIC_NODE) {
490505d05c7Sgtb if (iter->file)
491505d05c7Sgtb k5_mutex_unlock(&iter->file->data->lock);
492505d05c7Sgtb return PROF_MAGIC_NODE;
493505d05c7Sgtb }
4947c478bd9Sstevel@tonic-gate get_new_file:
4957c478bd9Sstevel@tonic-gate if (iter->node == 0) {
4967c478bd9Sstevel@tonic-gate if (iter->file == 0 ||
4977c478bd9Sstevel@tonic-gate (iter->flags & PROFILE_ITER_FINAL_SEEN)) {
498505d05c7Sgtb if (iter->file)
499505d05c7Sgtb k5_mutex_unlock(&iter->file->data->lock);
5007c478bd9Sstevel@tonic-gate profile_node_iterator_free(iter_p);
5017c478bd9Sstevel@tonic-gate if (ret_node)
5027c478bd9Sstevel@tonic-gate *ret_node = 0;
5037c478bd9Sstevel@tonic-gate if (ret_name)
5047c478bd9Sstevel@tonic-gate *ret_name = 0;
5057c478bd9Sstevel@tonic-gate if (ret_value)
5067c478bd9Sstevel@tonic-gate *ret_value =0;
5077c478bd9Sstevel@tonic-gate return 0;
5087c478bd9Sstevel@tonic-gate }
509505d05c7Sgtb k5_mutex_unlock(&iter->file->data->lock);
5107c478bd9Sstevel@tonic-gate if ((retval = profile_update_file(iter->file))) {
511505d05c7Sgtb if (retval == ENOENT || retval == EACCES) {
512505d05c7Sgtb /* XXX memory leak? */
513505d05c7Sgtb iter->file = iter->file->next;
514505d05c7Sgtb if (iter->file) {
515505d05c7Sgtb retval = k5_mutex_lock(&iter->file->data->lock);
516505d05c7Sgtb if (retval) {
517505d05c7Sgtb profile_node_iterator_free(iter_p);
518505d05c7Sgtb return retval;
519505d05c7Sgtb }
520505d05c7Sgtb }
521505d05c7Sgtb skip_num = 0;
522505d05c7Sgtb retval = 0;
523505d05c7Sgtb goto get_new_file;
524505d05c7Sgtb } else {
5257c478bd9Sstevel@tonic-gate profile_node_iterator_free(iter_p);
5267c478bd9Sstevel@tonic-gate return retval;
527505d05c7Sgtb }
528505d05c7Sgtb }
529505d05c7Sgtb retval = k5_mutex_lock(&iter->file->data->lock);
530505d05c7Sgtb if (retval) {
531505d05c7Sgtb profile_node_iterator_free(iter_p);
532505d05c7Sgtb return retval;
5337c478bd9Sstevel@tonic-gate }
534505d05c7Sgtb iter->file_serial = iter->file->data->upd_serial;
5357c478bd9Sstevel@tonic-gate /*
5367c478bd9Sstevel@tonic-gate * Find the section to list if we are a LIST_SECTION,
5377c478bd9Sstevel@tonic-gate * or find the containing section if not.
5387c478bd9Sstevel@tonic-gate */
539505d05c7Sgtb section = iter->file->data->root;
540159d09a2SMark Phalan assert(section != NULL);
5417c478bd9Sstevel@tonic-gate for (cpp = iter->names; cpp[iter->done_idx]; cpp++) {
542505d05c7Sgtb for (p=section->first_child; p; p = p->next) {
5437c478bd9Sstevel@tonic-gate if (!strcmp(p->name, *cpp) && !p->value)
5447c478bd9Sstevel@tonic-gate break;
545505d05c7Sgtb }
5467c478bd9Sstevel@tonic-gate if (!p) {
5477c478bd9Sstevel@tonic-gate section = 0;
5487c478bd9Sstevel@tonic-gate break;
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate section = p;
5517c478bd9Sstevel@tonic-gate if (p->final)
5527c478bd9Sstevel@tonic-gate iter->flags |= PROFILE_ITER_FINAL_SEEN;
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate if (!section) {
555505d05c7Sgtb k5_mutex_unlock(&iter->file->data->lock);
5567c478bd9Sstevel@tonic-gate iter->file = iter->file->next;
557505d05c7Sgtb if (iter->file) {
558505d05c7Sgtb retval = k5_mutex_lock(&iter->file->data->lock);
559505d05c7Sgtb if (retval) {
560505d05c7Sgtb profile_node_iterator_free(iter_p);
561505d05c7Sgtb return retval;
562505d05c7Sgtb }
563505d05c7Sgtb }
5647c478bd9Sstevel@tonic-gate skip_num = 0;
5657c478bd9Sstevel@tonic-gate goto get_new_file;
5667c478bd9Sstevel@tonic-gate }
5677c478bd9Sstevel@tonic-gate iter->name = *cpp;
5687c478bd9Sstevel@tonic-gate iter->node = section->first_child;
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate /*
5717c478bd9Sstevel@tonic-gate * OK, now we know iter->node is set up correctly. Let's do
5727c478bd9Sstevel@tonic-gate * the search.
5737c478bd9Sstevel@tonic-gate */
5747c478bd9Sstevel@tonic-gate for (p = iter->node; p; p = p->next) {
5757c478bd9Sstevel@tonic-gate if (iter->name && strcmp(p->name, iter->name))
5767c478bd9Sstevel@tonic-gate continue;
5777c478bd9Sstevel@tonic-gate if ((iter->flags & PROFILE_ITER_SECTIONS_ONLY) &&
5787c478bd9Sstevel@tonic-gate p->value)
5797c478bd9Sstevel@tonic-gate continue;
5807c478bd9Sstevel@tonic-gate if ((iter->flags & PROFILE_ITER_RELATIONS_ONLY) &&
5817c478bd9Sstevel@tonic-gate !p->value)
5827c478bd9Sstevel@tonic-gate continue;
5837c478bd9Sstevel@tonic-gate if (skip_num > 0) {
5847c478bd9Sstevel@tonic-gate skip_num--;
5857c478bd9Sstevel@tonic-gate continue;
5867c478bd9Sstevel@tonic-gate }
587159d09a2SMark Phalan if (p->deleted)
588159d09a2SMark Phalan continue;
5897c478bd9Sstevel@tonic-gate break;
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate iter->num++;
5927c478bd9Sstevel@tonic-gate if (!p) {
593505d05c7Sgtb k5_mutex_unlock(&iter->file->data->lock);
5947c478bd9Sstevel@tonic-gate iter->file = iter->file->next;
595505d05c7Sgtb if (iter->file) {
596505d05c7Sgtb retval = k5_mutex_lock(&iter->file->data->lock);
597505d05c7Sgtb if (retval) {
598505d05c7Sgtb profile_node_iterator_free(iter_p);
599505d05c7Sgtb return retval;
600505d05c7Sgtb }
601505d05c7Sgtb }
6027c478bd9Sstevel@tonic-gate iter->node = 0;
6037c478bd9Sstevel@tonic-gate skip_num = 0;
6047c478bd9Sstevel@tonic-gate goto get_new_file;
6057c478bd9Sstevel@tonic-gate }
606505d05c7Sgtb k5_mutex_unlock(&iter->file->data->lock);
6077c478bd9Sstevel@tonic-gate if ((iter->node = p->next) == NULL)
6087c478bd9Sstevel@tonic-gate iter->file = iter->file->next;
6097c478bd9Sstevel@tonic-gate if (ret_node)
6107c478bd9Sstevel@tonic-gate *ret_node = p;
6117c478bd9Sstevel@tonic-gate if (ret_name)
6127c478bd9Sstevel@tonic-gate *ret_name = p->name;
6137c478bd9Sstevel@tonic-gate if (ret_value)
6147c478bd9Sstevel@tonic-gate *ret_value = p->value;
6157c478bd9Sstevel@tonic-gate return 0;
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate
618*55fea89dSDan Cross /*
6197c478bd9Sstevel@tonic-gate * Remove a particular node.
620*55fea89dSDan Cross *
6217c478bd9Sstevel@tonic-gate * TYT, 2/25/99
6227c478bd9Sstevel@tonic-gate */
profile_remove_node(struct profile_node * node)623505d05c7Sgtb errcode_t profile_remove_node(struct profile_node *node)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate CHECK_MAGIC(node);
6267c478bd9Sstevel@tonic-gate
6277c478bd9Sstevel@tonic-gate if (node->parent == 0)
6287c478bd9Sstevel@tonic-gate return PROF_EINVAL; /* Can't remove the root! */
629*55fea89dSDan Cross
630505d05c7Sgtb node->deleted = 1;
6317c478bd9Sstevel@tonic-gate
6327c478bd9Sstevel@tonic-gate return 0;
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate
6357c478bd9Sstevel@tonic-gate /*
6367c478bd9Sstevel@tonic-gate * Set the value of a specific node containing a relation.
6377c478bd9Sstevel@tonic-gate *
6387c478bd9Sstevel@tonic-gate * TYT, 2/25/99
6397c478bd9Sstevel@tonic-gate */
profile_set_relation_value(struct profile_node * node,const char * new_value)640505d05c7Sgtb errcode_t profile_set_relation_value(struct profile_node *node,
641505d05c7Sgtb const char *new_value)
6427c478bd9Sstevel@tonic-gate {
6437c478bd9Sstevel@tonic-gate char *cp;
644*55fea89dSDan Cross
6457c478bd9Sstevel@tonic-gate CHECK_MAGIC(node);
6467c478bd9Sstevel@tonic-gate
6477c478bd9Sstevel@tonic-gate if (!node->value)
6487c478bd9Sstevel@tonic-gate return PROF_SET_SECTION_VALUE;
6497c478bd9Sstevel@tonic-gate
650159d09a2SMark Phalan cp = malloc(strlen(new_value)+1);
6517c478bd9Sstevel@tonic-gate if (!cp)
6527c478bd9Sstevel@tonic-gate return ENOMEM;
6537c478bd9Sstevel@tonic-gate
6547c478bd9Sstevel@tonic-gate strcpy(cp, new_value);
6557c478bd9Sstevel@tonic-gate free(node->value);
6567c478bd9Sstevel@tonic-gate node->value = cp;
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate return 0;
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate * Rename a specific node
6637c478bd9Sstevel@tonic-gate *
6647c478bd9Sstevel@tonic-gate * TYT 2/25/99
6657c478bd9Sstevel@tonic-gate */
profile_rename_node(struct profile_node * node,const char * new_name)666505d05c7Sgtb errcode_t profile_rename_node(struct profile_node *node, const char *new_name)
6677c478bd9Sstevel@tonic-gate {
6687c478bd9Sstevel@tonic-gate char *new_string;
6697c478bd9Sstevel@tonic-gate struct profile_node *p, *last;
6707c478bd9Sstevel@tonic-gate
6717c478bd9Sstevel@tonic-gate CHECK_MAGIC(node);
6727c478bd9Sstevel@tonic-gate
6737c478bd9Sstevel@tonic-gate if (strcmp(new_name, node->name) == 0)
6747c478bd9Sstevel@tonic-gate return 0; /* It's the same name, return */
6757c478bd9Sstevel@tonic-gate
6767c478bd9Sstevel@tonic-gate /*
6777c478bd9Sstevel@tonic-gate * Make sure we can allocate memory for the new name, first!
6787c478bd9Sstevel@tonic-gate */
679159d09a2SMark Phalan new_string = malloc(strlen(new_name)+1);
6807c478bd9Sstevel@tonic-gate if (!new_string)
6817c478bd9Sstevel@tonic-gate return ENOMEM;
6827c478bd9Sstevel@tonic-gate strcpy(new_string, new_name);
6837c478bd9Sstevel@tonic-gate
6847c478bd9Sstevel@tonic-gate /*
6857c478bd9Sstevel@tonic-gate * Find the place to where the new node should go. We look
6867c478bd9Sstevel@tonic-gate * for the place *after* the last match of the node name,
6877c478bd9Sstevel@tonic-gate * since order matters.
6887c478bd9Sstevel@tonic-gate */
6897c478bd9Sstevel@tonic-gate for (p=node->parent->first_child, last = 0; p; last = p, p = p->next) {
6907c478bd9Sstevel@tonic-gate if (strcmp(p->name, new_name) > 0)
6917c478bd9Sstevel@tonic-gate break;
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate
6947c478bd9Sstevel@tonic-gate /*
6957c478bd9Sstevel@tonic-gate * If we need to move the node, do it now.
6967c478bd9Sstevel@tonic-gate */
6977c478bd9Sstevel@tonic-gate if ((p != node) && (last != node)) {
6987c478bd9Sstevel@tonic-gate /*
6997c478bd9Sstevel@tonic-gate * OK, let's detach the node
7007c478bd9Sstevel@tonic-gate */
7017c478bd9Sstevel@tonic-gate if (node->prev)
7027c478bd9Sstevel@tonic-gate node->prev->next = node->next;
7037c478bd9Sstevel@tonic-gate else
7047c478bd9Sstevel@tonic-gate node->parent->first_child = node->next;
7057c478bd9Sstevel@tonic-gate if (node->next)
7067c478bd9Sstevel@tonic-gate node->next->prev = node->prev;
7077c478bd9Sstevel@tonic-gate
7087c478bd9Sstevel@tonic-gate /*
7097c478bd9Sstevel@tonic-gate * Now let's reattach it in the right place.
7107c478bd9Sstevel@tonic-gate */
7117c478bd9Sstevel@tonic-gate if (p)
7127c478bd9Sstevel@tonic-gate p->prev = node;
7137c478bd9Sstevel@tonic-gate if (last)
7147c478bd9Sstevel@tonic-gate last->next = node;
7157c478bd9Sstevel@tonic-gate else
7167c478bd9Sstevel@tonic-gate node->parent->first_child = node;
7177c478bd9Sstevel@tonic-gate node->next = p;
7187c478bd9Sstevel@tonic-gate node->prev = last;
7197c478bd9Sstevel@tonic-gate }
7207c478bd9Sstevel@tonic-gate
7217c478bd9Sstevel@tonic-gate free(node->name);
7227c478bd9Sstevel@tonic-gate node->name = new_string;
7237c478bd9Sstevel@tonic-gate return 0;
7247c478bd9Sstevel@tonic-gate }
725