Prepare for '-Wunused-parameter'
[lttng-tools.git] / tests / utils / xml-utils / validate_xml.cpp
CommitLineData
68270f0f 1/*
4b2b86f2 2 * Copyright (C) 2014 EfficiOS Inc.
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)
f46376a1
MJ
44void xml_error_handler(void *ctx __attribute__((unused)),
45 const char *format, ...)
68270f0f
JRJ
46{
47 char *err_msg;
48 va_list args;
49 int ret;
50
51 va_start(args, format);
52 ret = vasprintf(&err_msg, format, args);
53 va_end(args);
54 if (ret == -1) {
55 fprintf(stderr, "ERR: %s\n",
56 "String allocation failed in xml error handle");
57 return;
58 }
59
60 fprintf(stderr, "XML Error: %s\n", err_msg);
61 free(err_msg);
62}
63
64static
65void fini_validation_ctx(
66 struct validation_ctx *ctx)
67{
68 if (ctx->parser_ctx) {
69 xmlSchemaFreeParserCtxt(ctx->parser_ctx);
70 }
71
72 if (ctx->schema) {
73 xmlSchemaFree(ctx->schema);
74 }
75
76 if (ctx->schema_validation_ctx) {
77 xmlSchemaFreeValidCtxt(ctx->schema_validation_ctx);
78 }
79
80 memset(ctx, 0, sizeof(struct validation_ctx));
81}
82
83static
84int init_validation_ctx(
85 struct validation_ctx *ctx, char *xsd_path)
86{
87 int ret;
88
89 if (!xsd_path) {
90 ret = -LTTNG_ERR_NOMEM;
91 goto end;
92 }
93
94 ctx->parser_ctx = xmlSchemaNewParserCtxt(xsd_path);
95 if (!ctx->parser_ctx) {
96 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
97 goto end;
98 }
99 xmlSchemaSetParserErrors(ctx->parser_ctx, xml_error_handler,
100 xml_error_handler, NULL);
101
102 ctx->schema = xmlSchemaParse(ctx->parser_ctx);
103 if (!ctx->schema) {
104 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
105 goto end;
106 }
107
108 ctx->schema_validation_ctx = xmlSchemaNewValidCtxt(ctx->schema);
109 if (!ctx->schema_validation_ctx) {
110 ret = -LTTNG_ERR_LOAD_INVALID_CONFIG;
111 goto end;
112 }
113
114 xmlSchemaSetValidErrors(ctx->schema_validation_ctx, xml_error_handler,
115 xml_error_handler, NULL);
116 ret = 0;
117
118end:
119 if (ret) {
120 fini_validation_ctx(ctx);
121 }
122 return ret;
123}
124
125static int validate_xml(const char *xml_file_path, struct validation_ctx *ctx)
126{
127 int ret;
128 xmlDocPtr doc = NULL;
129
a0377dfe
FD
130 LTTNG_ASSERT(xml_file_path);
131 LTTNG_ASSERT(ctx);
68270f0f
JRJ
132
133 /* Open the document */
134 doc = xmlParseFile(xml_file_path);
135 if (!doc) {
136 ret = LTTNG_ERR_MI_IO_FAIL;
137 goto end;
138 }
139
140 /* Validate against the validation ctx (xsd) */
141 ret = xmlSchemaValidateDoc(ctx->schema_validation_ctx, doc);
142 if (ret) {
143 fprintf(stderr, "ERR: %s\n", "XML is not valid againt provided XSD");
144 ret = CMD_ERROR;
145 goto end;
146 }
147
148 ret = CMD_SUCCESS;
149end:
150 return ret;
151
152
153}
f46376a1 154int main(int argc, char **argv)
68270f0f
JRJ
155{
156 int ret;
a90d21b3 157 struct validation_ctx ctx = { 0 };
68270f0f
JRJ
158
159 /* Check if we have all argument */
160 if (argc < 3) {
161 fprintf(stderr, "ERR: %s\n", "Missing arguments");
162 ret = CMD_ERROR;
163 goto end;
164 }
165
166 /* Check if xsd file exist */
167 ret = access(argv[1], F_OK);
168 if (ret < 0) {
169 fprintf(stderr, "ERR: %s\n", "Xsd path not valid");
170 goto end;
171 }
172
173 /* Check if xml to validate exist */
174 ret = access(argv[2], F_OK);
175 if (ret < 0) {
176 fprintf(stderr, "ERR: %s\n", "XML path not valid");
177 goto end;
178 }
179
180 /* initialize the validation ctx */
181 ret = init_validation_ctx(&ctx, argv[1]);
182 if (ret) {
183 goto end;
184 }
185
186 ret = validate_xml(argv[2], &ctx);
187
188 fini_validation_ctx(&ctx);
189
190end:
191 return ret;
192}
This page took 0.047497 seconds and 4 git commands to generate.