1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
/**************************************************************************************************
$Header: /pub/cvsroot/yencode/lib/error.c,v 1.13 2002/03/19 06:52:16 bboy Exp $
Copyright (C) 2002 Don Moore <bboy@bboy.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at Your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**************************************************************************************************/
#include "misc.h"
/* Short program name - set with set_progname(). */
char *short_progname = PACKAGE;
/* Program name (argv[0] verbatim) - set with set_progname(). */
char *progname = PACKAGE;
/* Full program name - set with set_progname(). */
char *long_progname = PACKAGE;
/* Should verbose messages be output? */
int err_verbose = 0;
/* Should debug messages be output? */
int err_debug = 0;
/* Should these functions be COMPLETELY silent? */
int err_quiet = 0;
/* Function to call on exit */
static void (*_err_exit_func)(int) = NULL;
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ERR_SET_EXIT
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void
err_set_exit(void (*ef)(int))
{
_err_exit_func = ef;
}
/*--- err_set_exit() ----------------------------------------------------------------------------*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SET_PROGRAM_NAME
Sets the global variables `_err_progname' and `_err_long_progname'.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void
set_progname(const char *name)
{
char *c, resolved_path[PATH_MAX];
if (!name)
return;
progname = xstrdup(name);
if ((c = strrchr(name, '/')))
short_progname = xstrdup(c+1);
else
short_progname = xstrdup(name);
if (!(realpath(name, resolved_path)))
long_progname = xstrdup(name);
else
long_progname = xstrdup(resolved_path);
}
/*--- set_progname() ----------------------------------------------------------------------------*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
_ERR_OUT
Outputs an error message.
Returns -1 for convenience if msg_type is err_type_warning or err_type_error, else returns 0.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
int
_err_out(
_err_errtype_t err_type, /* Message type; "warning", "error", etc. */
int append_strerror, /* Should strerror(errno) be appended to the message? */
int is_fatal, /* Should this error cause an abort()? */
int output_help, /* Append the "try --help" message? */
const char *sourcefile, /* Source file name (i.e. __FILE__) */
unsigned int lineno, /* Source file line number (i.e. __LINE__) */
const char *fmt, ... /* The user supplied message to output. */
)
{
#if HAVE_VASPRINTF
unsigned char *msg = NULL;
#else
unsigned char msg[BUFSIZ] = "\0";
#endif
va_list ap;
/* If this is a debug or verbose msg_type and the user doesn't want to see it, don't output. */
if ((err_type == err_type_verbose) && !err_verbose && !err_debug) // (debug implies verbose)
return (0);
if ((err_type == err_type_debug) && !err_debug)
return (0);
if (!err_quiet)
{
va_start(ap, fmt);
#if HAVE_VASPRINTF
vasprintf(&msg, fmt, ap);
#else
vsnprintf(msg, sizeof(msg), fmt, ap);
#endif
va_end(ap);
// Start with "progname: " if this is an error
if (err_type == err_type_error)
{
fputs(progname, stderr); // "progname: "
fputs(": ", stderr);
}
if (err_type == err_type_debug)
fputs("[DEBUG] ", stderr);
fputs(msg, stderr); // message
if (append_strerror) // strerror(errno) if requested
{
fputs(": ", stderr);
if (!errno)
fputs("None/unknown error", stderr);
else
fputs(strerror(errno), stderr);
}
fputc('\n', stderr); // done
// Output help string if requested
if (output_help)
fprintf(stderr, "Try `%s --help' for more information.\n", progname);
#if HAVE_VASPRINTF
free(msgbuf);
#endif
}
if (is_fatal) // Exit if requested
{
if (_err_exit_func)
_err_exit_func(0); // (send 0 so this could also be a signal handler)
exit(EXIT_FAILURE);
}
return (err_type == err_type_warning || err_type == err_type_error) ? -1 : 0;
}
/*--- _err_out() --------------------------------------------------------------------------------*/
/* vi:set ts=3: */
|