summaryrefslogtreecommitdiffstats
path: root/__root__/ccs-flatten/xmlconf.c
blob: 7fb616371f92d18239a13d28595e85d6cda52763 (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
/*
  Copyright Red Hat, Inc. 2004

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2, or (at your option) any
  later version.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  MA 02110-1301 USA
*/
#include <stdio.h>
#include <assert.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <string.h>
#include "xmlconf.h"

static xmlDocPtr conf_doc = NULL;
static const char *conffile = "/etc/cluster/cluster.conf";

/**
   Execute an XPath query, returning the first match.  Multiple matches are
   ignored.  Please be advised that this is quite inefficient.

   @param doc           Loaded XML document to search
   @param ctx           Predefined XML XPath context
   @param query         Query to execute.
   @return              newly allocated pointer to value or NULL if not found.
 */
char *
xpath_get_one(xmlDocPtr __attribute__ ((unused)) doc, xmlXPathContextPtr ctx, char *query)
{
    char *val = NULL, *ret = NULL;
    xmlXPathObjectPtr obj;
    xmlNodePtr node;
    size_t size = 0;
    int nnv = 0;

    obj = xmlXPathEvalExpression((unsigned char *)query, ctx);
    if (!obj)
        return NULL;
    if (!obj->nodesetval)
        goto out;
    if (obj->nodesetval->nodeNr <= 0)
        goto out;

    node = obj->nodesetval->nodeTab[0];
    if (!node)
        goto out;

    if (((node->type == XML_ATTRIBUTE_NODE) && strstr(query, "@*")) ||
        ((node->type == XML_ELEMENT_NODE) && strstr(query, "child::*"))) {
        if (node->children && node->children->content)
            size = strlen((char *)node->children->content) + strlen((char *)node->name) + 2;
        else
            size = strlen((char *)node->name) + 2;
        nnv = 1;
    } else {
        if (node->children && node->children->content) {
            size = strlen((char *)node->children->content) + 1;
        } else {
            goto out;
        }
    }

    val = (char *)malloc(size);
    if (!val)
        goto out;
    memset(val, 0, size);
    if (nnv) {
        sprintf(val, "%s=%s", node->name, (node->children && node->children->content) ?
                (char *)node->children->content : "");
    } else {
        sprintf(val, "%s", (node->children && node->children->content) ? node->children->content :
                node->name);
    }

    ret = val;
  out:
    xmlXPathFreeObject(obj);

    return ret;
}

int
conf_open(void)
{
    xmlInitParser();
    conf_doc = xmlParseFile(conffile);
    if (!conf_doc)
        return -1;
    return 0;
}

xmlDocPtr
conf_get_doc(void)
{
    return conf_doc;
}

int
conf_close(void)
{
    xmlFreeDoc(conf_doc);
    conf_doc = NULL;
    return 0;
}

void
conf_setconfig(char *path)
{
    conffile = path;
}

int
conf_get(char *path, char **value)
{
    char *foo;
    xmlXPathContextPtr ctx;

    ctx = xmlXPathNewContext(conf_doc);
    foo = xpath_get_one(conf_doc, ctx, path);
    xmlXPathFreeContext(ctx);

    if (foo) {
        *value = foo;
        return 0;
    }
    return 1;
}