configure: enable -Wsuggest-attribute=format
[lttng-tools.git] / tests / utils / xml-utils / validate_xml.cpp
CommitLineData
68270f0f 1/*
9d16b343 2 * Copyright (C) 2014 Jonathan Rajotte <jonathan.r.julien@gmail.com>
68270f0f 3 *
9d16b343 4 * SPDX-License-Identifier: LGPL-2.1-only
68270f0f 5 *
68270f0f
JRJ
6 */
7
8 /*
9 * This script validate and xml from an xsd.
10 * argv[1] Path of the xsd
11 * argv[2] Path to the XML to be validated
12 */
13
68270f0f
JRJ
14#include <ctype.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <inttypes.h>
19#include <dirent.h>
20#include <unistd.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23
24#include <libxml/xmlschemas.h>
25#include <libxml/parser.h>
26
27#include <lttng/lttng-error.h>
a0377dfe 28#include <common/macros.h>
68270f0f 29
d22ad5f8
SM
30#include <common/macros.h>
31
68270f0f
JRJ
32struct validation_ctx {
33 xmlSchemaParserCtxtPtr parser_ctx;
34 xmlSchemaPtr schema;
35 xmlSchemaValidCtxtPtr schema_validation_ctx;
36};
37
38enum command_err_code {
39 CMD_SUCCESS = 0,
40 CMD_ERROR
41};
42
d22ad5f8 43static ATTR_FORMAT_PRINTF(2, 3)
68270f0f
JRJ
44void xml_error_handler(void *ctx, const char *format, ...)
45{
46 char *err_msg;
47 va_list args;
48 int ret;
49
50 va_start(args, format);
51 ret = vasprintf(&err_msg, format, args);
52 va_end(args);
53 if (ret == -1) {
54 fprintf(stderr, "ERR: %s\n",
55 "String allocation failed in xml error handle");
56 return;
57 }
58
59 fprintf(stderr, "XML Error: %s\n", err_msg);
60 free(err_msg);
61}
62
63static
64void fini_validation_ctx(
65 struct validation_ctx *ctx)
66{
67 if (ctx->parser_ctx) {
68 xmlSchemaFreeParserCtxt(ctx->parser_ctx);
69 }
70
71 if (ctx->schema) {
72 xmlSchemaFree(ctx->schema);
73 }
74
75 if (ctx->schema_validation_ctx) {
76 xmlSchemaFreeValidCtxt(ctx->schema_validation_ctx);
77 }
78
79 memset(ctx, 0, sizeof(struct validation_ctx));
80}
81
82static
83int init_validation_ctx(
84 struct validation_ctx *ctx, char *xsd_path)
85{
86 int ret;
87
88 if (!xsd_path) {
89 ret = -LTTNG_ERR_NOMEM;
90 goto end;
91 }
92
93 ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path);
94 if (!ctx->parser_ctx) {
95 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
96 goto end;
97 }
98 xmlSchemaSetParserErrors(ctx->parser_ctx, xml_error_handler,
99 xml_error_handler, NULL);
100
101 ctx->schema = xmlSchemaParse(ctx->parser_ctx);
102 if (!ctx->schema) {
103 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
104 goto end;
105 }
106
107 ctx->schema_validation_ctx = xmlSchemaNewValidCtxt(ctx->schema);
108 if (!ctx->schema_validation_ctx) {
109 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
110 goto end;
111 }
112
113 xmlSchemaSetValidErrors(ctx->schema_validation_ctx, xml_error_handler,
114 xml_error_handler, NULL);
115 ret = 0;
116
117end:
118 if (ret) {
119 fini_validation_ctx(ctx);
120 }
121 return ret;
122}
123
124static int validate_xml(const char *xml_file_path, struct validation_ctx *ctx)
125{
126 int ret;
127 xmlDocPtr doc = NULL;
128
a0377dfe
FD
129 LTTNG_ASSERT(xml_file_path);
130 LTTNG_ASSERT(ctx);
68270f0f
JRJ
131
132 /* Open the document */
133 doc = xmlParseFile(xml_file_path);
134 if (!doc) {
135 ret = LTTNG_ERR_MI_IO_FAIL;
136 goto end;
137 }
138
139 /* Validate against the validation ctx (xsd) */
140 ret = xmlSchemaValidateDoc(ctx->schema_validation_ctx, doc);
141 if (ret) {
142 fprintf(stderr, "ERR: %s\n", "XML is not valid againt provided XSD");
143 ret = CMD_ERROR;
144 goto end;
145 }
146
147 ret = CMD_SUCCESS;
148end:
149 return ret;
150
151
152}
153int main(int argc, char **argv, char *env[])
154{
155 int ret;
a90d21b3 156 struct validation_ctx ctx = { 0 };
68270f0f
JRJ
157
158 /* Check if we have all argument */
159 if (argc < 3) {
160 fprintf(stderr, "ERR: %s\n", "Missing arguments");
161 ret = CMD_ERROR;
162 goto end;
163 }
164
165 /* Check if xsd file exist */
166 ret = access(argv[1], F_OK);
167 if (ret < 0) {
168 fprintf(stderr, "ERR: %s\n", "Xsd path not valid");
169 goto end;
170 }
171
172 /* Check if xml to validate exist */
173 ret = access(argv[2], F_OK);
174 if (ret < 0) {
175 fprintf(stderr, "ERR: %s\n", "XML path not valid");
176 goto end;
177 }
178
179 /* initialize the validation ctx */
180 ret = init_validation_ctx(&ctx, argv[1]);
181 if (ret) {
182 goto end;
183 }
184
185 ret = validate_xml(argv[2], &ctx);
186
187 fini_validation_ctx(&ctx);
188
189end:
190 return ret;
191}
This page took 0.043774 seconds and 4 git commands to generate.