/*======================================================================= Some functions for printing verbose error messages and keeping track of informational messages of different verbosity levels. Copyright 1993 (C) David Hinds - All Rights Reserved =======================================================================*/ #include #include #include #include #include #include #define ERRORS_C #include "utypes.h" #include "errors.h" #include "xalloc.h" static const char ident_c[] = "@(#) errors.c version 1.3 (3/15/93)"; /*=====================================================================*/ /* Error handling variables */ static void (*lock_err)(void) = NULL, (*unlock_err)(void) = NULL; static char *cmdname = NULL; static char *info_prefix = ""; static int_t info_limit = 1; /*=====================================================================*/ # ifdef OFF void init_err(char *cmd) { char *slash; slash = strrchr(cmd, '/'); if (slash == NULL) cmdname = cmd; else cmdname = slash+1; } /* init_err */ void init_lock(void (*lock)(void), void (*unlock)(void)) { lock_err = lock; unlock_err = unlock; } /* init_lock */ /*=====================================================================*/ void set_info(char *pre, int_t level) { info_prefix = pre; info_limit = level; } int_t get_info(void) { return info_limit; } void inform(int_t level, char *fmt, ...) { va_list args; va_start(args, fmt); if (level <= info_limit) { if (lock_err != NULL) lock_err(); printf("%s", info_prefix); vprintf(fmt, args); fflush(stdout); if (unlock_err != NULL) unlock_err(); } va_end(args); } /* inform */ void warning(char *fmt, ...) { va_list args; va_start(args, fmt); if (lock_err != NULL) lock_err(); fflush(stdout); if (cmdname != NULL) fprintf(stderr, "%s: ", cmdname); vfprintf(stderr, fmt, args); fflush(stderr); if (unlock_err != NULL) unlock_err(); va_end(args); } /* warning */ # endif void error(char *fmt, ...) { va_list args; va_start(args, fmt); if (lock_err != NULL) lock_err(); fflush(stdout); if (cmdname != NULL) fprintf(stderr, "%s: ", cmdname); vfprintf(stderr, fmt, args); fflush(stderr); if (unlock_err != NULL) unlock_err(); va_end(args); exit(EXIT_FAILURE); } /* error */ # ifdef OFF /*======================================================================= Function to display an annotated system error message. It should be called as fatal(__FILE__, __LINE__, #s) where 's' is expression that yielded the error. A call to init_lock() defines functions for fatal() to use to lock stderr while generating the messages, to make output from multiprocessing jobs interpretable. =======================================================================*/ void fatal(char *a, int_t b, char *c) { if (lock_err != NULL) lock_err(); fflush(stdout); if (cmdname != NULL) fprintf(stderr, "%s: ", cmdname); fprintf(stderr, "%s line %d, %s:\n", a, b, c); if (cmdname != NULL) fprintf(stderr, "%s: ", cmdname); perror((char *)NULL); fflush(stderr); if (unlock_err != NULL) unlock_err(); exit(EXIT_FAILURE); } /* fatal */ /*======================================================================= Handler for I/O formatting errors =======================================================================*/ void ioerror(char *a, int_t b, char *c) { if (lock_err != NULL) lock_err(); fflush(stdout); if (cmdname != NULL) fprintf(stderr, "%s: ", cmdname); fprintf(stderr, "%s line %d, %s:\n", a, b, c); if (cmdname != NULL) fprintf(stderr, "%s: ", cmdname); fprintf(stderr, "I/O error: wrong number of elements transferred\n"); fflush(stderr); if (unlock_err != NULL) unlock_err(); exit(EXIT_FAILURE); } /* fatal */ # endif