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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
/** @file
* @brief HID report descriptor - XML sink
*
* Copyright (C) 2009-2010 Nikolai Kondrashov
*
* This file is part of hidrd.
*
* Hidrd 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 of the License, or
* (at your option) any later version.
*
* Hidrd 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 hidrd; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @author Nikolai Kondrashov <spbnick@gmail.com>
*
* @(#) $Id: snk.c 438 2010-05-24 12:40:53Z spb_nick $
*/
#include "hidrd/cfg.h"
#include "hidrd/fmt/xml/cfg.h"
#include "hidrd/fmt/xml/prop.h"
#include "hidrd/fmt/xml/snk.h"
#include "snk/group.h"
#include "snk/item.h"
#include "../xml.h"
static bool
hidrd_xml_snk_init(hidrd_snk *snk, char **perr, bool format, const char *schema)
{
bool result = false;
hidrd_xml_snk_inst *xml_snk = (hidrd_xml_snk_inst *)snk;
char *own_schema = NULL;
hidrd_xml_snk_state *state = NULL;
xmlDocPtr doc = NULL;
xmlNodePtr root = NULL;
xmlNsPtr ns;
XML_ERR_FUNC_BACKUP_DECL;
if (perr != NULL)
*perr = strdup("");
XML_ERR_FUNC_SET(perr);
/* Copy schema file path */
own_schema = strdup(schema);
if (own_schema == NULL)
XML_ERR_CLNP("failed to allocate memory for the schema file path");
/* Create item state table stack */
state = malloc(sizeof(*state));
if (state == NULL)
XML_ERR_CLNP("failed to allocate memory for the state table");
state->prev = NULL;
state->usage_page = HIDRD_USAGE_PAGE_UNDEFINED;
/* Create the document */
doc = xmlNewDoc(BAD_CAST "1.0");
if (doc == NULL)
goto cleanup;
/* Create root node */
root = xmlNewNode(NULL, BAD_CAST "descriptor");
if (root == NULL)
goto cleanup;
/* Add and assign our namespace */
ns = xmlNewNs(root, BAD_CAST HIDRD_XML_PROP_NS, NULL);
if (ns == NULL)
goto cleanup;
xmlSetNs(root, ns);
/* Add XML schema instance namespace */
ns = xmlNewNs(root, BAD_CAST HIDRD_XML_PROP_NS_XSI, BAD_CAST "xsi");
if (ns == NULL)
goto cleanup;
/* Add xsi:schemaLocation attribute */
if (xmlSetNsProp(root, ns,
BAD_CAST "schemaLocation",
BAD_CAST HIDRD_XML_PROP_XSI_SCHEMA_LOCATION) == NULL)
goto cleanup;
/* Set root element */
xmlDocSetRootElement(doc, root);
/* Initialize the sink */
xml_snk->schema = own_schema;
xml_snk->format = format;
xml_snk->state = state;
xml_snk->doc = doc;
xml_snk->prnt = root;
xml_snk->err = strdup("");
own_schema = NULL;
state = NULL;
doc = NULL;
root = NULL;
result = true;
cleanup:
xmlFreeNode(root);
if (doc != NULL)
xmlFreeDoc(doc);
free(state);
free(own_schema);
XML_ERR_FUNC_RESTORE;
return result;
}
static bool
hidrd_xml_snk_initv(hidrd_snk *snk, char **perr, va_list ap)
{
bool format = (va_arg(ap, int) != 0);
const char *schema = va_arg(ap, const char *);
return hidrd_xml_snk_init(snk, perr, format, schema);
}
#ifdef HIDRD_WITH_OPT
static const hidrd_opt_spec hidrd_xml_snk_opts_spec[] = {
{.name = "format",
.type = HIDRD_OPT_TYPE_BOOLEAN,
.req = false,
.dflt = {
.boolean = true
},
.desc = "format XML output"},
{.name = "schema",
.type = HIDRD_OPT_TYPE_STRING,
.req = false,
.dflt = {
#ifdef NDEBUG
.string = ""
#else
.string = HIDRD_XML_SCHEMA_PATH
#endif
},
.desc = "path to a schema file for output validation"},
{.name = NULL}
};
static bool
hidrd_xml_snk_init_opts(hidrd_snk *snk, char **perr, const hidrd_opt *list)
{
return hidrd_xml_snk_init(
snk, perr,
hidrd_opt_list_get_boolean(list, "format"),
hidrd_opt_list_get_string(list, "schema"));
}
#endif /* HIDRD_WITH_OPT */
static bool
hidrd_xml_snk_valid(const hidrd_snk *snk)
{
const hidrd_xml_snk_inst *xml_snk = (const hidrd_xml_snk_inst *)snk;
return snk->type->size >= sizeof(hidrd_xml_snk_inst) &&
xml_snk->state != NULL &&
xml_snk->doc != NULL &&
xml_snk->prnt != NULL;
}
static char *
hidrd_xml_snk_errmsg(const hidrd_snk *snk)
{
const hidrd_xml_snk_inst *xml_snk =
(const hidrd_xml_snk_inst *)snk;
return strdup(xml_snk->err);
}
static bool
hidrd_xml_snk_flush(hidrd_snk *snk)
{
bool result = false;
hidrd_xml_snk_inst *xml_snk = (hidrd_xml_snk_inst *)snk;
bool valid;
xmlBufferPtr xml_buf = NULL;
xmlOutputBufferPtr xml_out_buf = NULL;
void *new_buf;
size_t new_size;
XML_ERR_FUNC_BACKUP_DECL;
free(xml_snk->err);
xml_snk->err = strdup("");
XML_ERR_FUNC_SET(&xml_snk->err);
/* Break any unfinished groups */
if (!xml_snk_group_break_branch(snk))
goto cleanup;
/* Validate the document, if the schema is specified */
if (*xml_snk->schema != '\0' &&
(!xml_validate(&valid, xml_snk->doc, xml_snk->schema) || !valid))
goto cleanup;
/* Create an XML buffer */
xml_buf = xmlBufferCreate();
if (xml_buf == NULL)
goto cleanup;
/* Create an XML output buffer from the generic buffer */
xml_out_buf = xmlOutputBufferCreateBuffer(xml_buf, NULL);
if (xml_out_buf == NULL)
goto cleanup;
/* Format XML from the document */
if (xmlSaveFormatFileTo(xml_out_buf, xml_snk->doc,
NULL, xml_snk->format) < 0)
goto cleanup;
/* xml_out_buf is closed by xmlSaveFormatFileTo */
xml_out_buf = NULL;
/* Retrieve resulting size */
new_size = xmlBufferLength(xml_buf);
/* If we have a location for the buffer pointer */
if (snk->pbuf != NULL)
{
/* Retension and update the buffer */
new_buf = realloc(*snk->pbuf, new_size);
if (new_size > 0 && new_buf == NULL)
XML_ERR_CLNP("failed to retension the output buffer");
memcpy(new_buf, xmlBufferContent(xml_buf), new_size);
/* Update the buffer pointer */
*snk->pbuf = new_buf;
}
/* Output size */
if (snk->psize != NULL)
*snk->psize = new_size;
result = true;
cleanup:
if (xml_out_buf != NULL)
xmlOutputBufferClose(xml_out_buf);
if (xml_buf != NULL)
xmlBufferFree(xml_buf);
XML_ERR_FUNC_RESTORE;
return result;
}
static void
hidrd_xml_snk_clnp(hidrd_snk *snk)
{
hidrd_xml_snk_inst *xml_snk = (hidrd_xml_snk_inst *)snk;
hidrd_xml_snk_state *state;
hidrd_xml_snk_state *prev_state;
/* Free the error message */
free(xml_snk->err);
xml_snk->err = NULL;
/* Free the document, if there is any */
if (xml_snk->doc != NULL)
{
xmlFreeDoc(xml_snk->doc);
xml_snk->doc = NULL;
}
/* Free the state stack, if there is any */
for (state = xml_snk->state; state != NULL; state = prev_state)
{
prev_state = state->prev;
free(state);
}
xml_snk->state = NULL;
/* Free the schema file path */
free(xml_snk->schema);
xml_snk->schema = NULL;
}
static bool
hidrd_xml_snk_put(hidrd_snk *snk, const hidrd_item *item)
{
bool result;
hidrd_xml_snk_inst *xml_snk = (hidrd_xml_snk_inst *)snk;
XML_ERR_FUNC_BACKUP_DECL;
free(xml_snk->err);
xml_snk->err = strdup("");
XML_ERR_FUNC_SET(&xml_snk->err);
result = xml_snk_item_basic(xml_snk, item);
XML_ERR_FUNC_RESTORE;
return result;
}
const hidrd_snk_type hidrd_xml_snk = {
.size = sizeof(hidrd_xml_snk_inst),
.initv = hidrd_xml_snk_initv,
#ifdef HIDRD_WITH_OPT
.init_opts = hidrd_xml_snk_init_opts,
.opts_spec = hidrd_xml_snk_opts_spec,
#endif
.valid = hidrd_xml_snk_valid,
.errmsg = hidrd_xml_snk_errmsg,
.put = hidrd_xml_snk_put,
.flush = hidrd_xml_snk_flush,
.clnp = hidrd_xml_snk_clnp,
};
|