| 1 | /*** error.c ****************************************************************** |
| 2 | ** |
| 3 | ** This file is part of BibTool. |
| 4 | ** It is distributed under the GNU General Public License. |
| 5 | ** See the file COPYING for details. |
| 6 | ** |
| 7 | ** (c) 1996-2020 Gerd Neugebauer |
| 8 | ** |
| 9 | ** Net: [email protected] |
| 10 | ** |
| 11 | ** This program is free software; you can redistribute it and/or modify |
| 12 | ** it under the terms of the GNU General Public License as published by |
| 13 | ** the Free Software Foundation; either version 2, or (at your option) |
| 14 | ** any later version. |
| 15 | ** |
| 16 | ** This program is distributed in the hope that it will be useful, |
| 17 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | ** GNU General Public License for more details. |
| 20 | ** |
| 21 | ** You should have received a copy of the GNU General Public License |
| 22 | ** along with this program; if not, write to the Free Software |
| 23 | ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | ** |
| 25 | **----------------------------------------------------------------------------- |
| 26 | ** Description: |
| 27 | ** To ensure a consistent appearence of error messages \BibTool{} |
| 28 | ** provides one generic error reporting routine. This routine is |
| 29 | ** controlled by several arguments to allow maximum flexibility. |
| 30 | ** |
| 31 | ** Usually it is awkward to fill out all those arguments. To |
| 32 | ** avoid this trouble the header file |error.h| provides some |
| 33 | ** macros which cover the most common situation and hide |
| 34 | ** unneccesary details. |
| 35 | ** |
| 36 | ******************************************************************************/ |
| 37 | |
| 38 | #include <bibtool/general.h> |
| 39 | #include <bibtool/error.h> |
| 40 | |
| 41 | /*****************************************************************************/ |
| 42 | /* Internal Programs */ |
| 43 | /*===========================================================================*/ |
| 44 | |
| 45 | /*****************************************************************************/ |
| 46 | /* External Programs */ |
| 47 | /*===========================================================================*/ |
| 48 | |
| 49 | extern bool rsc_quiet; |
| 50 | |
| 51 | /*---------------------------------------------------------------------------*/ |
| 52 | |
| 53 | char *err_format = "*** BibTool: %s"; |
| 54 | String err_point = (String)"."; |
| 55 | String err_oom = (String)"Out of memory for "; |
| 56 | |
| 57 | FILE * err_file = NULL; |
| 58 | |
| 59 | /*----------------------------------------------------------------------------- |
| 60 | ** Function: init_error() |
| 61 | ** Type: void |
| 62 | ** Purpose: |
| 63 | ** Initialize the error reporting. |
| 64 | ** Arguments: |
| 65 | ** file the output file to write error messages to |
| 66 | ** Returns: nothing |
| 67 | **___________________________________________________ */ |
| 68 | void init_error(file) /* */ |
| 69 | FILE * file; /* */ |
| 70 | { /* */ |
| 71 | err_file = file; /* */ |
| 72 | } /*------------------------*/ |
| 73 | |
| 74 | #define ErrNL (void)fputc('\n',err_file) |
| 75 | #define ErrChar(C) (void)fputc(C,err_file) |
| 76 | #define ErrS(S) (void)fputs(S,err_file) |
| 77 | |
| 78 | /*----------------------------------------------------------------------------- |
| 79 | ** Function: err_location() |
| 80 | ** Type: void |
| 81 | ** Purpose: Print the error location to the error stream. |
| 82 | ** |
| 83 | ** Arguments: |
| 84 | ** lineno the line number |
| 85 | ** fname the file name |
| 86 | ** s1 the optional postfix string |
| 87 | ** Returns: nothing |
| 88 | **___________________________________________________ */ |
| 89 | void err_location(lineno,fname,s1) /* */ |
| 90 | int lineno; /* */ |
| 91 | String fname; /* */ |
| 92 | char* s1; /* */ |
| 93 | { /* */ |
| 94 | (void)fprintf(err_file, /* */ |
| 95 | " (line %d in %s)", /* */ |
| 96 | lineno, /* */ |
| 97 | *fname ? (char*)fname : "<STDIN>");/* */ |
| 98 | if ( s1 ) { ErrS(s1); } /* */ |
| 99 | } /*------------------------*/ |
| 100 | |
| 101 | /*----------------------------------------------------------------------------- |
| 102 | ** Function: error() |
| 103 | ** Purpose: This is the generic error printing routine. |
| 104 | ** It prints an error message together with an optional filename, |
| 105 | ** the line number, the erroneous line and a pointer to the |
| 106 | ** problematic position. |
| 107 | ** |
| 108 | ** All parts of an error message are optional and can be |
| 109 | ** suppressed under certain conditions. The error type |
| 110 | ** determines which parts are actually shown. It is a |
| 111 | ** boolean combination of the following flags which are |
| 112 | ** defined in |error.h|: |
| 113 | ** \begin{description} |
| 114 | ** \item[ERR\_ERROR] If this bit is set then the error |
| 115 | ** message is marked as ``error''. The flag |ERR_WARNING| |
| 116 | ** is ignored in this case. This kind of messages can not |
| 117 | ** be suppressed. |
| 118 | ** \item[ERR\_WARNING] If this bit is set and |
| 119 | ** |ERR_ERROR| is not set then the error message is |
| 120 | ** marked as ``warning''. |ERR_WARNING| is ignored |
| 121 | ** in this case. |
| 122 | ** \item[ERR\_POINT] If this bit is set then the line |line| is |
| 123 | ** shown together with a pointer to the byte pointed to |
| 124 | ** by |err_pos|. Otherwise the line is not shown. |
| 125 | ** \item[ERR\_FILE] If this bit is set then the name of |
| 126 | ** the file |file_name| and the line number |lineno| are |
| 127 | ** shown. Otherwise the file name and the line number are |
| 128 | ** suppressed. |
| 129 | ** \item[ERR\_EXIT] If this bit is set then the error |
| 130 | ** routine calls |exit(-1)| at the end. This implicitly |
| 131 | ** sets the |ERR_ERROR| bit as well. |
| 132 | ** \end{description} |
| 133 | ** |
| 134 | ** The error message itself can be split in up to three |
| 135 | ** strings |s1|, |s2|, and |s3|. Those strings are |
| 136 | ** concatenated. They can also be |NULL| in which case |
| 137 | ** they are ignored. |
| 138 | ** |
| 139 | ** The error message is written to the stream determined |
| 140 | ** by the variable |err_file|. This variable refers to |
| 141 | ** the |stderr| stream initially but can be redirected to |
| 142 | ** any other destination. |
| 143 | ** |
| 144 | ** Arguments: |
| 145 | ** type Error type: boolean combination of the error bits as |
| 146 | ** defined in |error.h|. |
| 147 | ** s1 1$^\mathrm{st}$ error message or |NULL|. |
| 148 | ** s2 2$^\mathrm{nd}$ error message or |NULL|. |
| 149 | ** s3 3$^\mathrm{rd}$ error message or |NULL|. |
| 150 | ** line Current line when error occured (for reading errors). |
| 151 | ** err_pos Error position in line |line|. |
| 152 | ** line_no The line number where the error occurred. |
| 153 | ** fname The file name where the error occurred. |
| 154 | ** Returns: nothing |
| 155 | **___________________________________________________ */ |
| 156 | void error(type, s1, s2, s3, line, err_pos, line_no, fname)/* */ |
| 157 | int type; /* defined in error.h */ |
| 158 | String s1; /* 1st error message */ |
| 159 | String s2; /* 2nd error message */ |
| 160 | String s3; /* 3rd error message */ |
| 161 | String line; /* line_no string. */ |
| 162 | String err_pos; /* error position in line */ |
| 163 | int line_no; /* line number */ |
| 164 | Symbol fname; /* file name */ |
| 165 | { /* */ |
| 166 | if ( (type&ERR_WARNING) /* anything less than an */ |
| 167 | && rsc_quiet ) return; /* error is ignored. */ |
| 168 | /* */ |
| 169 | if (!(type&ERR_NO_NL)) { ErrNL; } /* */ |
| 170 | if ( (type&ERR_POINT) && line != NULL ) /* */ |
| 171 | { ErrS((char*)line); /* print the error line. */ |
| 172 | if (line[strlen((char*)line)-1] != '\n') ErrNL;/* */ |
| 173 | for ( ; line < err_pos; ++line ) ErrChar('_'); /* and a pointer to the */ |
| 174 | ErrChar('^'); ErrNL; /* error position. */ |
| 175 | } /* */ |
| 176 | /* */ |
| 177 | ErrS("*** BibTool"); /* */ |
| 178 | if ( type&ERR_ERROR ) ErrS(" ERROR" ); /* */ |
| 179 | else if ( type&ERR_WARN ) ErrS(" WARNING"); /* */ |
| 180 | if (type&ERR_FILE) /* */ |
| 181 | { err_location(line_no,fname, NULL); } /* */ |
| 182 | ErrS(": "); /* */ |
| 183 | if ( s1 ) { ErrS((char*)s1); } /* */ |
| 184 | if ( s2 ) { ErrS((char*)s2); } /* */ |
| 185 | if ( s3 ) { ErrS((char*)s3); } /* */ |
| 186 | if (!(type&ERR_NO_NL)) { ErrNL; } /* */ |
| 187 | if ( type&ERR_EXIT ) exit(-1); /* */ |
| 188 | } /*------------------------*/ |