/*-
 *  Copyright (c) 2002, 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 "urelay.h"
#include "ur_err.h"

__RINGID("$Ringlet: c/net/urelay/ur_err.c,v 1.3 2003/07/16 10:21:03 roam Exp $");

static const char *errs[UR_ERR_LAST] = {
	"no error",
	"out of memory",
	"bad command-line arguments",
	"read() error",
	"write() error",
	"select() error",
	"no more to read",
	"fcntl(F_GETFL) error",
	"fcntl(F_SETFL) error",
	"swallow string not matched",
	"INTERNAL ERROR"
};

static int errsn[UR_ERR_LAST] = {
	0, 0, 0,	/* NONE, NOMEM, CMDLINE */
	1, 1, 1, 0,	/* READ, WRITE, SELECT, NOREAD */
	1, 1,		/* FCNTL_GET, FCNTL_SET */
	0,		/* DIFF */
	0		/* INT */
};

static char	errbuf[512];

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

char *
ur_strerror(ur_err_t code) {
	
	if (code > UR_ERR_INT)
		code = UR_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:
 * 	ur_prerror			- print error message and context
 * Inputs:
 * 	s				- context string to be printed
 * 	code				- error code
 * Returns:
 * 	code
 * Modifies:
 * 	nothing; writes to stderr
 */

ur_err_t
ur_prerror(const char *s, ur_err_t code) {
	
	if (code > UR_ERR_INT)
		code = UR_ERR_INT;
	
	fprintf(stderr, "urelay error: %s: %s\n", s, ur_strerror(code));
	return (code);
}



syntax highlighted by Code2HTML, v. 0.9.1