/*-
 *  Copyright (c) 2001-2003  Peter Pentchev
 *  All rights reserved.
 * 
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 *  SUCH DAMAGE.
 */

#include <errno.h>
#include <stdio.h>
#include <string.h>

#include "unquote.h"
#include "uq_err.h"

static char	cvs_id[] __UNUS =
"$Ringlet: c/misc/unquote/uq_err.c,v 1.6 2003/07/07 08:55:06 roam Exp $";

/* error message strings, one per uq_err_t value */
static const char *errs[UQ_ERR_LAST] = {
	"no error",
	"out of memory",
	"bad command-line arguments",
	"invalid character in the quoted string",
	"exec() error",
	"INTERNAL ERROR"
};

/* boolean 'need errno/strerror()' flags, one per uq_err_t value */
static int errsn[UQ_ERR_LAST] = {
	0, 0, 0,	/* NONE, NOMEM, CMDLINE */
	0,		/* BADCHAR */
	1,		/* EXEC */
	0		/* INT */
};

/* temporary string for storing error messages */
static char	errbuf[512];

/*
 * Function:
 * 	uq_strerror			- build an error message string
 * Inputs:
 * 	code				- error code
 * Returns:
 * 	pointer to static error message string
 * Modifies:
 * 	errbuf
 */

char *
uq_strerror(uq_err_t code) {
	
	if (code > UQ_ERR_INT)
		code = UQ_ERR_INT;

	if (errsn[code])
		snprintf(errbuf, sizeof(errbuf), "%s: %s", errs[code],
				strerror(errno));
	else
		snprintf(errbuf, sizeof(errbuf), "%s", errs[code]);
	
	return (errbuf);
}

/*
 * Function:
 * 	uq_prerror			- print error message and context
 * Inputs:
 * 	s				- context string to be printed
 * 	code				- error code
 * Returns:
 * 	code
 * Modifies:
 * 	nothing; writes to stderr
 */

uq_err_t
uq_prerror(const char *s, uq_err_t code) {
	
	if (code > UQ_ERR_INT)
		code = UQ_ERR_INT;
	
	fprintf(stderr, "unquote error: %s: %s\n", s, uq_strerror(code));
	return (code);
}


syntax highlighted by Code2HTML, v. 0.9.1