configure: enable -Wsuggest-attribute=format
[lttng-tools.git] / tests / utils / xml-utils / validate_xml.cpp
1 /*
2 * Copyright (C) 2014 Jonathan Rajotte <jonathan.r.julien@gmail.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
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
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>
28 #include <common/macros.h>
29
30 #include <common/macros.h>
31
32 struct validation_ctx {
33 xmlSchemaParserCtxtPtr parser_ctx;
34 xmlSchemaPtr schema;
35 xmlSchemaValidCtxtPtr schema_validation_ctx;
36 };
37
38 enum command_err_code {
39 CMD_SUCCESS = 0,
40 CMD_ERROR
41 };
42
43 static ATTR_FORMAT_PRINTF(2, 3)
44 void 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
63 static
64 void 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
82 static
83 int 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
117 end:
118 if (ret) {
119 fini_validation_ctx(ctx);
120 }
121 return ret;
122 }
123
124 static int validate_xml(const char *xml_file_path, struct validation_ctx *ctx)
125 {
126 int ret;
127 xmlDocPtr doc = NULL;
128
129 LTTNG_ASSERT(xml_file_path);
130 LTTNG_ASSERT(ctx);
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;
148 end:
149 return ret;
150
151
152 }
153 int main(int argc, char **argv, char *env[])
154 {
155 int ret;
156 struct validation_ctx ctx = { 0 };
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
189 end:
190 return ret;
191 }
This page took 0.033506 seconds and 5 git commands to generate.