/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* prof_tree.c --- these routines maintain the parse tree of the
* config file.
*
* All of the details of how the tree is stored is abstracted away in
* this file; all of the other profile routines build, access, and
* modify the tree via the accessor functions found in this file.
*
* Each node may represent either a relation or a section header.
*
* A section header must have its value field set to 0, and may a one
* or more child nodes, pointed to by first_child.
*
* A relation has as its value a pointer to allocated memory
* containing a string. Its first_child pointer must be null.
*
*/
#include "prof_int.h"
#include <stdio.h>
#include <string.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <errno.h>
#include <ctype.h>
struct profile_node {
char *name;
char *value;
int group_level;
};
return PROF_MAGIC_NODE;
/*
* Free a node, and any children
*/
{
return;
}
}
#ifndef HAVE_STRDUP
static char *MYstrdup (const char *s)
{
if (p != 0)
return p;
}
#endif
/*
* Create a node
*/
struct profile_node **ret_node)
{
if (!new)
return ENOMEM;
return ENOMEM;
}
if (value) {
return ENOMEM;
}
}
return 0;
}
/*
* This function verifies that all of the representation invarients of
* the profile are true. If not, we have a programming bug somewhere,
* probably in this file.
*/
{
return PROF_SECTION_WITH_VALUE;
last = 0;
return PROF_BAD_LINK_LIST;
return PROF_BAD_LINK_LIST;
return PROF_BAD_GROUP_LVL;
return PROF_BAD_PARENT_PTR;
retval = profile_verify_node(p);
if (retval)
return retval;
}
return 0;
}
/*
* Add a node to a particular section
*/
{
return PROF_ADD_NOT_SECTION;
/*
* Find the place to insert the new node. We look for the
* place *after* the last match of the node name, since
* order matters.
*/
int cmp;
if (cmp > 0)
break;
}
if (retval)
return retval;
if (p)
if (last)
else
if (ret_node)
return 0;
}
/*
* Set the final flag on a particular node.
*/
{
return 0;
}
/*
* Check the final flag on a node
*/
{
}
/*
* Return the name of a node. (Note: this is for internal functions
* only; if the name needs to be returned from an exported function,
* strdup it first!)
*/
{
}
/*
* Return the value of a node. (Note: this is for internal functions
* only; if the name needs to be returned from an exported function,
* strdup it first!)
*/
{
}
/*
* Iterate through the section, returning the nodes which match
* the given name. If name is NULL, then interate through all the
* nodes in the section. If section_flag is non-zero, only return the
* section which matches the name; don't return relations. If value
* is non-NULL, then only return relations which match the requested
* value. (The value argument is ignored if section_flag is non-zero.)
*
* The first time this routine is called, the state pointer must be
* null. When this profile_find_node_relation() returns, if the state
* pointer is non-NULL, then this routine should be called again.
* (This won't happen if section_flag is non-zero, obviously.)
*
*/
struct profile_node **node)
{
struct profile_node *p;
p = *state;
if (p) {
CHECK_MAGIC(p);
} else
p = section->first_child;
for (; p; p = p->next) {
continue;
if (section_flag) {
if (p->value)
continue;
} else {
if (!p->value)
continue;
continue;
}
if (p->deleted)
continue;
/* A match! */
if (node)
*node = p;
break;
}
if (p == 0) {
*state = 0;
}
/*
* OK, we've found one match; now let's try to find another
* one. This way, if we return a non-zero state pointer,
* there's guaranteed to be another match that's returned.
*/
continue;
if (section_flag) {
if (p->value)
continue;
} else {
if (!p->value)
continue;
continue;
}
/* A match! */
break;
}
*state = p;
return 0;
}
/*
* Iterate through the section, returning the relations which match
* the given name. If name is NULL, then interate through all the
* relations in the section. The first time this routine is called,
* the state pointer must be null. When this profile_find_node_relation()
* returns, if the state pointer is non-NULL, then this routine should
* be called again.
*
* The returned character string in value points to the stored
* character string in the parse string. Before this string value is
* returned to a calling application (profile_find_node_relation is not an
* exported interface), it should be strdup()'ed.
*/
{
struct profile_node *p;
if (retval)
return retval;
if (p) {
if (value)
if (ret_name)
}
return 0;
}
/*
* Iterate through the section, returning the subsections which match
* the given name. If name is NULL, then interate through all the
* subsections in the section. The first time this routine is called,
* the state pointer must be null. When this profile_find_node_subsection()
* returns, if the state pointer is non-NULL, then this routine should
* be called again.
*
* This is (plus accessor functions for the name and value given a
* profile node) makes this function mostly syntactic sugar for
* profile_find_node.
*/
char **ret_name,
struct profile_node **subsection)
{
struct profile_node *p;
/* Solaris Kerberos */
return (PROF_NO_PROFILE);
if (retval)
return retval;
if (p) {
if (subsection)
*subsection = p;
if (ret_name)
}
return 0;
}
/*
* This function returns the parent of a particular node.
*/
struct profile_node **parent)
{
return 0;
}
/*
* This is a general-purpose iterator for returning all nodes that
* match the specified name array.
*/
struct profile_iterator {
int flags;
const char *const *names;
const char *name;
int file_serial;
int done_idx;
int num;
};
void **ret_iter)
{
int done_idx = 0;
if (profile == 0)
return PROF_NO_PROFILE;
return PROF_MAGIC_PROFILE;
if (!names)
return PROF_BAD_NAMESET;
if (!(flags & PROFILE_ITER_LIST_SECTION)) {
if (!names[0])
return PROF_BAD_NAMESET;
done_idx = 1;
}
return ENOMEM;
return 0;
}
{
if (!iter_p)
return;
return;
*iter_p = 0;
}
/*
* Note: the returned character strings in ret_name and ret_value
* points to the stored character string in the parse string. Before
* this string value is returned to a calling application
* (profile_node_iterator is not an exported interface), it should be
* strdup()'ed.
*/
{
const char *const *cpp;
int skip_num = 0;
return PROF_MAGIC_ITERATOR;
return PROF_MAGIC_FILE;
return PROF_MAGIC_FILE_DATA;
/*
* If the file has changed, then the node pointer is invalid,
* so we'll have search the file again looking for it.
*/
if (retval)
return retval;
}
}
return PROF_MAGIC_NODE;
}
if (ret_node)
*ret_node = 0;
if (ret_name)
*ret_name = 0;
if (ret_value)
*ret_value =0;
return 0;
}
/* XXX memory leak? */
if (retval) {
return retval;
}
}
skip_num = 0;
retval = 0;
goto get_new_file;
} else {
return retval;
}
}
if (retval) {
return retval;
}
/*
* Find the section to list if we are a LIST_SECTION,
* or find the containing section if not.
*/
break;
}
if (!p) {
section = 0;
break;
}
section = p;
if (p->final)
}
if (!section) {
if (retval) {
return retval;
}
}
skip_num = 0;
goto get_new_file;
}
}
/*
* OK, now we know iter->node is set up correctly. Let's do
* the search.
*/
continue;
p->value)
continue;
!p->value)
continue;
if (skip_num > 0) {
skip_num--;
continue;
}
if (p->deleted)
continue;
break;
}
if (!p) {
if (retval) {
return retval;
}
}
skip_num = 0;
goto get_new_file;
}
if (ret_node)
*ret_node = p;
if (ret_name)
if (ret_value)
return 0;
}
/*
* Remove a particular node.
*
* TYT, 2/25/99
*/
{
return PROF_EINVAL; /* Can't remove the root! */
return 0;
}
/*
* Set the value of a specific node containing a relation.
*
* TYT, 2/25/99
*/
const char *new_value)
{
char *cp;
return PROF_SET_SECTION_VALUE;
if (!cp)
return ENOMEM;
return 0;
}
/*
* Rename a specific node
*
* TYT 2/25/99
*/
{
char *new_string;
return 0; /* It's the same name, return */
/*
* Make sure we can allocate memory for the new name, first!
*/
if (!new_string)
return ENOMEM;
/*
* Find the place to where the new node should go. We look
* for the place *after* the last match of the node name,
* since order matters.
*/
break;
}
/*
* If we need to move the node, do it now.
*/
/*
* OK, let's detach the node
*/
else
/*
* Now let's reattach it in the right place.
*/
if (p)
if (last)
else
}
return 0;
}