| 1 | /* $Id$ */ |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | * err.c |
|---|
| 5 | * |
|---|
| 6 | * Adapted from OpenBSD libc *err* *warn* code. |
|---|
| 7 | * |
|---|
| 8 | * Copyright (c) 2001-2007 Aaron Turner. |
|---|
| 9 | * |
|---|
| 10 | * Copyright (c) 2000 Dug Song <dugsong@monkey.org> |
|---|
| 11 | * |
|---|
| 12 | * Copyright (c) 1993 |
|---|
| 13 | * The Regents of the University of California. All rights reserved. |
|---|
| 14 | * |
|---|
| 15 | * Redistribution and use in source and binary forms, with or without |
|---|
| 16 | * modification, are permitted provided that the following conditions |
|---|
| 17 | * are met: |
|---|
| 18 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 19 | * notice, this list of conditions and the following disclaimer. |
|---|
| 20 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 21 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 22 | * documentation and/or other materials provided with the distribution. |
|---|
| 23 | * 3. All advertising materials mentioning features or use of this software |
|---|
| 24 | * must display the following acknowledgement: |
|---|
| 25 | * This product includes software developed by the University of |
|---|
| 26 | * California, Berkeley and its contributors. |
|---|
| 27 | * 4. Neither the name of the University nor the names of its contributors |
|---|
| 28 | * may be used to endorse or promote products derived from this software |
|---|
| 29 | * without specific prior written permission. |
|---|
| 30 | * |
|---|
| 31 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
|---|
| 32 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 33 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|---|
| 34 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
|---|
| 35 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 36 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|---|
| 37 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|---|
| 39 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|---|
| 40 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|---|
| 41 | * SUCH DAMAGE. |
|---|
| 42 | */ |
|---|
| 43 | |
|---|
| 44 | #include "config.h" |
|---|
| 45 | #include "defines.h" |
|---|
| 46 | #include "common.h" |
|---|
| 47 | |
|---|
| 48 | #include <stdio.h> |
|---|
| 49 | #include <stdlib.h> |
|---|
| 50 | #include <stdarg.h> |
|---|
| 51 | #include <string.h> |
|---|
| 52 | #include <errno.h> |
|---|
| 53 | |
|---|
| 54 | #ifdef DEBUG |
|---|
| 55 | extern int debug; |
|---|
| 56 | #endif |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * writes a notice message to stderr. Always forces a newline |
|---|
| 60 | */ |
|---|
| 61 | void |
|---|
| 62 | notice(const char *fmt, ...) |
|---|
| 63 | { |
|---|
| 64 | va_list ap; |
|---|
| 65 | |
|---|
| 66 | va_start(ap, fmt); |
|---|
| 67 | if (fmt != NULL) |
|---|
| 68 | (void)vfprintf(stderr, fmt, ap); |
|---|
| 69 | (void)fprintf(stderr, "\n"); |
|---|
| 70 | va_end(ap); |
|---|
| 71 | fflush(NULL); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | /** |
|---|
| 75 | * Inner call to dbgx() which prints the function, line & function along |
|---|
| 76 | * with the message to stderr. Always forces a newline. |
|---|
| 77 | * |
|---|
| 78 | * You don't actually want to call this! use dbgx() instead! |
|---|
| 79 | */ |
|---|
| 80 | #ifdef DEBUG |
|---|
| 81 | void |
|---|
| 82 | _our_verbose_dbgx(int dbg_level, const char *fmt, const char *function, |
|---|
| 83 | const int line, const char *file, ...) |
|---|
| 84 | { |
|---|
| 85 | va_list ap; |
|---|
| 86 | |
|---|
| 87 | if (debug < dbg_level) |
|---|
| 88 | return; |
|---|
| 89 | |
|---|
| 90 | fprintf(stderr, "DEBUG%d in %s:%s() line %d: ", dbg_level, file, |
|---|
| 91 | function, line); |
|---|
| 92 | |
|---|
| 93 | va_start(ap, file); |
|---|
| 94 | |
|---|
| 95 | if (fmt != NULL) |
|---|
| 96 | (void)vfprintf(stderr, fmt, ap); |
|---|
| 97 | (void)fprintf(stderr, "\n"); |
|---|
| 98 | va_end(ap); |
|---|
| 99 | fflush(NULL); |
|---|
| 100 | } |
|---|
| 101 | #endif |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * Inner call to errx() which when in DEBUG mode, prints the function, line & file |
|---|
| 105 | * along with the actual error message to stderr. Alawys forces a newline |
|---|
| 106 | */ |
|---|
| 107 | #ifdef DEBUG |
|---|
| 108 | void |
|---|
| 109 | _our_verbose_errx(int eval, const char *fmt, const char *function, const int line, const char *file, ...) { |
|---|
| 110 | #else |
|---|
| 111 | void |
|---|
| 112 | _our_verbose_errx(int eval, const char *fmt, ...) { |
|---|
| 113 | #endif |
|---|
| 114 | |
|---|
| 115 | va_list ap; |
|---|
| 116 | |
|---|
| 117 | #ifdef DEBUG |
|---|
| 118 | fprintf(stderr, "\nFatal Error in %s:%s() line %d:\n", file, function, line); |
|---|
| 119 | va_start(ap, file); |
|---|
| 120 | #else |
|---|
| 121 | fprintf(stderr, "\nFatal Error: "); |
|---|
| 122 | va_start(ap, fmt); |
|---|
| 123 | #endif |
|---|
| 124 | |
|---|
| 125 | if (fmt != NULL) |
|---|
| 126 | (void)vfprintf(stderr, fmt, ap); |
|---|
| 127 | (void)fprintf(stderr, "\n"); |
|---|
| 128 | va_end(ap); |
|---|
| 129 | exit(eval); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | /** |
|---|
| 133 | * Inner call to warnx() which when in DEBUG mode, prints the function, line & file |
|---|
| 134 | * along with the actual warning to stderr. Alawys forces a newline |
|---|
| 135 | */ |
|---|
| 136 | #ifdef DEBUG |
|---|
| 137 | void |
|---|
| 138 | _our_verbose_warnx(const char *fmt, const char *function, const int line, const char *file, ...) { |
|---|
| 139 | #else |
|---|
| 140 | void |
|---|
| 141 | _our_verbose_warnx(const char *fmt, ...) { |
|---|
| 142 | #endif |
|---|
| 143 | |
|---|
| 144 | va_list ap; |
|---|
| 145 | #ifdef DEBUG |
|---|
| 146 | fprintf(stderr, "Warning in %s:%s() line %d:\n", file, function, line); |
|---|
| 147 | va_start(ap, file); |
|---|
| 148 | #else |
|---|
| 149 | fprintf(stderr, "Warning: "); |
|---|
| 150 | va_start(ap, fmt); |
|---|
| 151 | #endif |
|---|
| 152 | |
|---|
| 153 | if (fmt != NULL) |
|---|
| 154 | (void)vfprintf(stderr, fmt, ap); |
|---|
| 155 | (void)fprintf(stderr, "\n"); |
|---|
| 156 | va_end(ap); |
|---|
| 157 | } |
|---|