xref: /openbsd-src/lib/libcurses/tinfo/comp_error.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: comp_error.c,v 1.5 2003/03/18 16:55:54 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /*
37  *	comp_error.c -- Error message routines
38  *
39  */
40 
41 #include <curses.priv.h>
42 
43 #include <tic.h>
44 
45 MODULE_ID("$From: comp_error.c,v 1.21 2000/12/10 02:55:07 tom Exp $")
46 
47 NCURSES_EXPORT_VAR(bool) _nc_suppress_warnings = FALSE;
48 NCURSES_EXPORT_VAR(int)
49 _nc_curr_line = 0;		/* current line # in input */
50 NCURSES_EXPORT_VAR(int)
51 _nc_curr_col = 0;		/* current column # in input */
52 
53      static const char *sourcename;
54      static char termtype[MAX_NAME_SIZE + 1];
55 
56 NCURSES_EXPORT(void)
57 _nc_set_source(const char *const name)
58 {
59     sourcename = name;
60 }
61 
62 NCURSES_EXPORT(void)
63 _nc_set_type(const char *const name)
64 {
65     if (name)
66 	strlcpy(termtype, name, sizeof(termtype));
67     else
68     	termtype[0] = '\0';
69 }
70 
71 NCURSES_EXPORT(void)
72 _nc_get_type(char *name)
73 {
74     strlcpy(name, termtype, MAX_NAME_SIZE + 1);
75 }
76 
77 static inline void
78 where_is_problem(void)
79 {
80     fprintf(stderr, "\"%s\"", sourcename);
81     if (_nc_curr_line >= 0)
82 	fprintf(stderr, ", line %d", _nc_curr_line);
83     if (_nc_curr_col >= 0)
84 	fprintf(stderr, ", col %d", _nc_curr_col);
85     if (termtype[0])
86 	fprintf(stderr, ", terminal '%s'", termtype);
87     fputc(':', stderr);
88     fputc(' ', stderr);
89 }
90 
91 NCURSES_EXPORT(void)
92 _nc_warning(const char *const fmt,...)
93 {
94     va_list argp;
95 
96     if (_nc_suppress_warnings)
97 	return;
98 
99     where_is_problem();
100     va_start(argp, fmt);
101     vfprintf(stderr, fmt, argp);
102     fprintf(stderr, "\n");
103     va_end(argp);
104 }
105 
106 NCURSES_EXPORT(void)
107 _nc_err_abort(const char *const fmt,...)
108 {
109     va_list argp;
110 
111     where_is_problem();
112     va_start(argp, fmt);
113     vfprintf(stderr, fmt, argp);
114     fprintf(stderr, "\n");
115     va_end(argp);
116     exit(EXIT_FAILURE);
117 }
118 
119 NCURSES_EXPORT(void)
120 _nc_syserr_abort(const char *const fmt,...)
121 {
122     va_list argp;
123 
124     where_is_problem();
125     va_start(argp, fmt);
126     vfprintf(stderr, fmt, argp);
127     fprintf(stderr, "\n");
128     va_end(argp);
129 
130     /* If we're debugging, try to show where the problem occurred - this
131      * will dump core.
132      */
133 #if defined(TRACE) || !defined(NDEBUG)
134     abort();
135 #else
136     /* Dumping core in production code is not a good idea.
137      */
138     exit(EXIT_FAILURE);
139 #endif
140 }
141