Added a libpwmd manual page.
[pwmd.git] / src / xml.c
blob5600e988ba7177537c793f69c9074c044bccf53a
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2007 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <err.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <ctype.h>
28 #include <glib.h>
29 #include <gcrypt.h>
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include "pwmd_error.h"
36 #include "xml.h"
38 gboolean valid_xml_element(const xmlChar *element)
40 const xmlChar *p;
42 if (!element || !*element || isdigit(*element))
43 return FALSE;
45 for (p = element; *p; p++) {
46 if (!isalnum(*p))
47 return FALSE;
50 if (xmlStrncasecmp(element, (xmlChar *)"?xml", 4) == 0)
51 return FALSE;
53 return TRUE;
56 gboolean new_account(xmlDocPtr doc, gchar *name)
58 xmlNodePtr root = xmlDocGetRootElement(doc);
59 xmlAttrPtr a;
60 xmlNodePtr n;
62 if (!name || !root)
63 return FALSE;
65 n = xmlNewNode(NULL, (xmlChar *)"account");
66 n = xmlAddChild(root, n);
67 a = xmlNewProp(n, (xmlChar *)"name", (xmlChar *)name);
68 return TRUE;
71 gchar *new_document()
73 gchar *buf;
74 const char *line =
75 "<?xml version=\"1.0\"?>\n"
76 "<!DOCTYPE accounts [\n"
77 "<!ELEMENT accounts (account*)>\n"
78 "<!ATTLIST account name CDATA #REQUIRED>\n"
79 "]>\n"
80 "<accounts/>";
82 if ((buf = gcry_malloc(strlen(line) + 1)) == NULL)
83 return NULL;
85 strcpy(buf, line);
86 return buf;
89 gboolean list_accounts(xmlTextReaderPtr reader, gchar **dst, gint *pwmd_errno)
91 xmlNodePtr n;
92 gchar *line = NULL;
93 gchar **str = NULL;
94 gint i = 0;
97 * "fast forward" to the first account.
99 while (xmlTextReaderRead(reader) == 1) {
100 if ((n = xmlTextReaderCurrentNode(reader)) == NULL) {
101 *pwmd_errno = EPWMD_LIBXML_ERROR;
102 return FALSE;
105 if (xmlTextReaderDepth(reader) == 1 &&
106 xmlStrcmp(n->name, (xmlChar *)"account") == 0 &&
107 xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
108 xmlChar *p = xmlTextReaderGetAttribute(reader, (xmlChar *)"name");
110 if (!p)
111 continue;
113 str = g_realloc(str, (i + 2) * sizeof(gchar *));
114 str[i++] = g_strdup((gchar *)p);
115 str[i] = NULL;
116 xmlFree(p);
120 if (!i || !str) {
121 *pwmd_errno = EPWMD_EMPTY_ELEMENT;
122 return FALSE;
125 line = g_strjoinv("\n", str);
126 g_strfreev(str);
127 *dst = line;
128 return TRUE;
131 gboolean find_account(xmlTextReaderPtr reader, gchar *name)
133 xmlNodePtr n;
135 while (xmlTextReaderRead(reader) == 1) {
136 if ((n = xmlTextReaderCurrentNode(reader)) == NULL)
137 return FALSE;
139 if (xmlTextReaderDepth(reader) == 1 &&
140 xmlStrcmp(n->name, (xmlChar *)"account") == 0 &&
141 xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
142 xmlChar *p = xmlTextReaderGetAttribute(reader, (xmlChar *)"name");
144 // Shouldn't happen (DTD).
145 if (!p)
146 continue;
148 if (xmlStrcmp(p, (xmlChar *)name) == 0) {
149 xmlFree(p);
150 return TRUE;
153 xmlFree(p);
157 return FALSE;
160 gboolean find_element(xmlTextReaderPtr reader, gchar *e, gint more)
162 xmlNodePtr n;
164 while (xmlTextReaderRead(reader) == 1) {
165 if ((n = xmlTextReaderCurrentNode(reader)) == NULL)
166 return FALSE;
169 * Stop processing after the closing account element to prevent
170 * finding element names that may exist in another account.
172 if (xmlTextReaderDepth(reader) == 1 &&
173 xmlStrcmp(n->name, (xmlChar *)"account") == 0 &&
174 xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT)
175 return FALSE;
177 if (xmlStrcmp(n->name, (xmlChar *)e) == 0 &&
178 xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT)
179 return TRUE;
182 return FALSE;
185 xmlNodePtr find_node(xmlNodePtr root, xmlChar *name)
187 xmlNodePtr n;
189 for (n = root->children; n; n = n->next) {
190 if (g_ascii_strcasecmp((gchar *)name, (gchar *)n->name) == 0)
191 return n;
194 return NULL;
197 gint open_xml(const gchar *data, gsize size, xmlDocPtr *doc,
198 xmlTextReaderPtr *reader)
200 if ((*doc = xmlReadMemory(data, size, NULL, "UTF-8",
201 XML_PARSE_NOBLANKS)) == NULL)
202 return 1;
204 if ((*reader = xmlReaderWalker(*doc)) == NULL)
205 return 2;
207 return 0;