The daemon code is no longer needed. We'll use only the server and
[pwmd.git] / src / xml.c
blobc221a9c0f293dad85a1a13cab173cee9d0471b47
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006 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>
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
30 #include "xml.h"
32 void write_dtd(FILE *fp)
34 fprintf(fp, "%s",
35 "<?xml version=\"1.0\"?>\n"
36 "<!DOCTYPE accounts [\n"
37 "!ELEMENT accounts (account*)>\n"
38 "!ELEMENT account (name, username?, password?, smtp?, pop?, imap?)>\n"
39 "!ELEMENT name (#PCDATA)>\n"
40 "!ELEMENT username (#PCDATA)>\n"
41 "!ELEMENT password (#PCDATA)>\n"
42 "!ELEMENT smtp (host, port, ssl)>\n"
43 "!ELEMENT pop (host, port, ssl)>\n"
44 "!ELEMENT imap (host, port, ssl)>\n"
45 "!ELEMENT host (#PCDATA)>\n"
46 "!ELEMENT port (#PCDATA)>\n"
47 "!ELEMENT ssl (#PCDATA)>\n"
48 "]>\n"
52 int find_account(xmlTextReaderPtr reader, xmlChar *name)
54 int type;
55 xmlNodePtr n;
57 while (xmlTextReaderRead(reader) == 1) {
58 if ((n = xmlTextReaderCurrentNode(reader)) == NULL)
59 return 1;
62 * "name" is the element that holds the account name.
64 if (xmlTextReaderDepth(reader) == 1 &&
65 xmlStrcmp(n->name, (xmlChar *)"name") &&
66 xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
67 do {
68 if (xmlTextReaderNext(reader) != 1)
69 return 1;
70 type = xmlTextReaderNodeType(reader);
71 } while (type != -1 && type != XML_READER_TYPE_TEXT);
73 if ((n = xmlTextReaderCurrentNode(reader)) == NULL)
74 return 1;
76 if (xmlStrcmp(n->content, name) == 0)
77 return 0;
81 return 1;
84 int find_element(xmlTextReaderPtr reader, xmlChar *e, int more)
86 int type;
87 xmlNodePtr n;
89 while (xmlTextReaderRead(reader) == 1) {
90 if ((n = xmlTextReaderCurrentNode(reader)) == NULL)
91 return 1;
93 if (xmlStrcmp(n->name, e) == 0 &&
94 xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
95 if (more)
96 return 0;
99 * FIXME account/service without details (account/service/host).
101 do {
102 if (xmlTextReaderNext(reader) != 1)
103 return 1;
104 type = xmlTextReaderNodeType(reader);
105 } while (type != -1 && type != XML_READER_TYPE_TEXT);
106 return (type == XML_READER_TYPE_TEXT) ? 0 : 1;
110 return 1;
113 xmlNodePtr find_node(xmlNodePtr root, xmlChar *name)
115 xmlNodePtr n;
117 for (n = root->children; n; n = n->next) {
118 if (strcmp((char *)name, (char *)n->name) == 0)
119 return n;
122 return NULL;
125 xmlNodePtr create_doc(xmlDocPtr *doc, const char *root)
127 xmlDocPtr d = xmlNewDoc((xmlChar *)"1.0");
128 xmlNodePtr n = xmlNewNode(NULL, (xmlChar *)root);
130 *doc = d;
131 xmlDocSetRootElement(d, n);
132 return n;
135 int open_xml(const char *data, int size, xmlDocPtr *doc, xmlNodePtr *root,
136 xmlTextReaderPtr *reader)
138 if (size >= 0) {
139 if ((*doc = xmlReadMemory(data, size, NULL, NULL, 0)) == NULL)
140 return 1;
142 else {
143 if ((*doc = xmlReadFile(data, NULL, 0)) == NULL)
144 return 1;
147 *root = xmlDocGetRootElement(*doc);
149 if ((*reader = xmlReaderWalker(*doc)) == NULL)
150 return 2;
152 return 0;