<commit_before>/**
 * Copyright (c) 2011 <NAME>, All Rights Reserved
 * License: 3-Clause BSD
 * Revision: 07/31/2011 11:12:43 AM
 */

#include "xvalid.h"

static struct stat stat_info;


static void xvalid_usage(void)
{
	printf("XValid v%s - validate XML documents\n", XVALID_VERSION_STRING);
	printf("Usage: xval [options] XML_FILE ...\n");
	printf("\n");
	printf("where validation are:\n");
	printf("  --dtd FILE      validate against external DTD file\n");
	printf("  --xsd FILE      validate against external XSD schema file\n");
	printf("\n");
	printf("where general options are:\n");
	printf("  --handler FILE  use external error handler (.so|.dll|.lua)\n");
}

static int xvalid__embed_lua(xvalid_ctx_ptr ctx)
{
	assert(ctx != NULL);
	assert(ctx->L == NULL);

	ctx->L = luaL_newstate();
	if (ctx->L == NULL) return 1;

	/* stop the GC during Lua library (all std) initialization and function
	 * registrations
	 */
	lua_gc(ctx->L, LUA_GCSTOP, 0);
	luaL_openlibs(ctx->L);

	/* TODO register functions here */

	lua_gc(ctx->L, LUA_GCRESTART, 0);

	return 0;
}

/* TODO implement */
static int xvalid__dtd_validate(xvalid_ctx_ptr ctx)
{
	int rv = 0;

	assert(ctx != NULL);
	assert(ctx->dtd_file != NULL);
	assert(ctx->current_file != NULL);

	return 0;
}

static int xvalid__xsd_validate(xvalid_ctx_ptr ctx)
{
	int rv = 0;
	int ret = 0;
	xmlSchemaValidCtxtPtr xsd_valid_ctx = NULL;
	xmlParserInputBufferPtr input = NULL;

	assert(ctx != NULL);
	assert(ctx->schema != NULL);
	assert(ctx->current_file != NULL);

	input = xmlParserInputBufferCreateFilename(ctx->current_file,
				XML_CHAR_ENCODING_NONE);
	if (input == NULL)
	{
		fprintf(stderr, "[ERROR] unable to open %s for XSD validation\n",
				ctx->current_file);
		rv = 1;
		goto done;
	}

	/* TODO memoize XSD validation context */
	xsd_valid_ctx = xmlSchemaNewValidCtxt(ctx->schema);
	if (xsd_valid_ctx == NULL)
	{
		fprintf(stderr, "[ERROR] unable to create an XSD validation context\n");
		rv = 1;
		goto done;
	}
	if ((ret = xmlSchemaValidateStream(
					xsd_valid_ctx,
					input,
					XML_CHAR_ENCODING_NONE,
					ctx->handlers,
					NULL)))
	{

		if (ret > 0)
		{
			fprintf(stderr, "%s fails XSD validation\n", ctx->current_file);
		}
		else
		{
			fprintf(stdout, "%s XSD validation caused an internal error\n",
					ctx->current_file);
		}
	}
	else if (ret == 0)
	{
		fprintf(stdout, "Successfully XSD validated %s\n", ctx->current_file);
	}

	/* cleanup */
	xmlSchemaFreeValidCtxt(xsd_valid_ctx);
	xsd_valid_ctx = NULL;
	/* TODO why does this SEGV? */
	xmlFreeParserInputBuffer(input);
	input = NULL;

done:
	return rv;
}

/* TODO implement both DTD validation */
static int xvalid_validate_xml_file(xvalid_ctx_ptr ctx, const char *filename)
{
	int rv = 0;
	xmlSchemaParserCtxtPtr xsd_ctx = NULL;

	assert(ctx != NULL);
	assert(filename != NULL);

	ctx->current_file = filename;

	/* validate with given XSD schema file if given */
	if (ctx->schema_file != NULL)
	{
		/* compile and memoize XSD schema if not already present */
		if (ctx->schema == NULL)
		{
			xsd_ctx = xmlSchemaNewParserCtxt(ctx->schema_file);
			if (xsd_ctx == NULL)
			{<commit_msg>Don't free stack variables<commit_after>/**
 * Copyright (c) 2011 <NAME>, All Rights Reserved
 * License: 3-Clause BSD
 * Revision: 08/13/2011 10:17:42 PM
 */

#include "xvalid.h"

static struct stat stat_info;


static void xvalid_usage(void)
{
	printf("XValid v%s - validate XML documents\n", XVALID_VERSION_STRING);
	printf("Usage: xval [options] XML_FILE ...\n");
	printf("\n");
	printf("where validation are:\n");
	printf("  --dtd FILE      validate against external DTD file\n");
	printf("  --xsd FILE      validate against external XSD schema file\n");
	printf("\n");
	printf("where general options are:\n");
	printf("  --handler FILE  use external error handler (.so|.dll|.lua)\n");
}

static int xvalid__embed_lua(xvalid_ctx_ptr ctx)
{
	assert(ctx != NULL);
	assert(ctx->L == NULL);

	ctx->L = luaL_newstate();
	if (ctx->L == NULL) return 1;

	/* stop the GC during Lua library (all std) initialization and function
	 * registrations
	 */
	lua_gc(ctx->L, LUA_GCSTOP, 0);
	luaL_openlibs(ctx->L);

	/* TODO register functions here */

	lua_gc(ctx->L, LUA_GCRESTART, 0);

	return 0;
}

/* TODO implement */
static int xvalid__dtd_validate(xvalid_ctx_ptr ctx)
{
	int rv = 0;

	assert(ctx != NULL);
	assert(ctx->dtd_file != NULL);
	assert(ctx->current_file != NULL);

	return 0;
}

static int xvalid__xsd_validate(xvalid_ctx_ptr ctx)
{
	int rv = 0;
	int ret = 0;
	xmlSchemaValidCtxtPtr xsd_valid_ctx = NULL;
	xmlParserInputBufferPtr input = NULL;

	assert(ctx != NULL);
	assert(ctx->schema != NULL);
	assert(ctx->current_file != NULL);

	input = xmlParserInputBufferCreateFilename(ctx->current_file,
				XML_CHAR_ENCODING_NONE);
	if (input == NULL)
	{
		fprintf(stderr, "[ERROR] unable to open %s for XSD validation\n",
				ctx->current_file);
		rv = 1;
		goto done;
	}

	/* TODO memoize XSD validation context */
	xsd_valid_ctx = xmlSchemaNewValidCtxt(ctx->schema);
	if (xsd_valid_ctx == NULL)
	{
		fprintf(stderr, "[ERROR] unable to create an XSD validation context\n");
		rv = 1;
		goto done;
	}
	if ((ret = xmlSchemaValidateStream(
					xsd_valid_ctx,
					input,
					XML_CHAR_ENCODING_NONE,
					ctx->handlers,
					NULL)))
	{

		if (ret > 0)
		{
			fprintf(stderr, "%s fails XSD validation\n", ctx->current_file);
		}
		else
		{
			fprintf(stdout, "%s XSD validation caused an internal error\n",
					ctx->current_file);
		}
	}
	else if (ret == 0)
	{
		fprintf(stdout, "Successfully XSD validated %s\n", ctx->current_file);
	}

	/* cleanup */
	xmlSchemaFreeValidCtxt(xsd_valid_ctx);
	xsd_valid_ctx = NULL;

done:
	return rv;
}

/* TODO implement both DTD validation */
static int xvalid_validate_xml_file(xvalid_ctx_ptr ctx, const char *filename)
{
	int rv = 0;
	xmlSchemaParserCtxtPtr xsd_ctx = NULL;

	assert(ctx != NULL);
	assert(filename != NULL);

	ctx->current_file = filename;

	/* validate with given XSD schema file if given */
	if (ctx->schema_file != NULL)
	{
		/* compile and memoize XSD schema if not already present */
		if (ctx->schema == NULL)
		{
			xsd_ctx = xmlSchemaNewParserCtxt(ctx->schema_file);
			if (xsd_ctx == NULL)
			{
{'fraction_non_alphanumeric': 0.13761319534282018, 'fraction_numerical': 0.01034928848641656, 'mean_word_length': 3.87007874015748, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 19}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3162510', 'n_tokens_mistral': 2414, 'n_tokens_neox': 2179, 'n_words': 585}