xref: /freebsd/contrib/ncurses/ncurses/tinfo/comp_error.c (revision 1c4ee7dfb8affed302171232b0f612e6bcba3c10)
1 /****************************************************************************
2  * Copyright 2019,2020 Thomas E. Dickey                                     *
3  * Copyright 1998-2012,2016 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  *     and: Thomas E. Dickey                        1996-on                 *
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("$Id: comp_error.c,v 1.40 2020/02/02 23:34:34 tom Exp $")
46 
47 NCURSES_EXPORT_VAR(bool) _nc_suppress_warnings = FALSE;
48 NCURSES_EXPORT_VAR(int) _nc_curr_line = 0; /* current line # in input */
49 NCURSES_EXPORT_VAR(int) _nc_curr_col = 0; /* current column # in input */
50 
51 #define SourceName	_nc_globals.comp_sourcename
52 #define TermType	_nc_globals.comp_termtype
53 
54 NCURSES_EXPORT(const char *)
55 _nc_get_source(void)
56 {
57     return SourceName;
58 }
59 
60 NCURSES_EXPORT(void)
61 _nc_set_source(const char *const name)
62 {
63     FreeIfNeeded(SourceName);
64     SourceName = strdup(name);
65 }
66 
67 NCURSES_EXPORT(void)
68 _nc_set_type(const char *const name)
69 {
70 #define MY_SIZE (size_t) MAX_NAME_SIZE
71     if (TermType == 0)
72 	TermType = typeMalloc(char, MY_SIZE + 1);
73     if (TermType != 0) {
74 	TermType[0] = '\0';
75 	if (name) {
76 	    _nc_STRNCAT(TermType, name, MY_SIZE, MY_SIZE);
77 	}
78     }
79 }
80 
81 NCURSES_EXPORT(void)
82 _nc_get_type(char *name)
83 {
84 #if NO_LEAKS
85     if (name == 0 && TermType != 0) {
86 	FreeAndNull(TermType);
87 	return;
88     }
89 #endif
90     if (name != 0)
91 	_nc_STRCPY(name, TermType != 0 ? TermType : "", MAX_NAME_SIZE);
92 }
93 
94 static NCURSES_INLINE void
95 where_is_problem(void)
96 {
97     fprintf(stderr, "\"%s\"", SourceName ? SourceName : "?");
98     if (_nc_curr_line >= 0)
99 	fprintf(stderr, ", line %d", _nc_curr_line);
100     if (_nc_curr_col >= 0)
101 	fprintf(stderr, ", col %d", _nc_curr_col);
102     if (TermType != 0 && TermType[0] != '\0')
103 	fprintf(stderr, ", terminal '%s'", TermType);
104     fputc(':', stderr);
105     fputc(' ', stderr);
106 }
107 
108 NCURSES_EXPORT(void)
109 _nc_warning(const char *const fmt, ...)
110 {
111     va_list argp;
112 
113     if (_nc_suppress_warnings)
114 	return;
115 
116     where_is_problem();
117     va_start(argp, fmt);
118     vfprintf(stderr, fmt, argp);
119     fprintf(stderr, "\n");
120     va_end(argp);
121 }
122 
123 NCURSES_EXPORT(void)
124 _nc_err_abort(const char *const fmt, ...)
125 {
126     va_list argp;
127 
128     where_is_problem();
129     va_start(argp, fmt);
130     vfprintf(stderr, fmt, argp);
131     fprintf(stderr, "\n");
132     va_end(argp);
133     exit(EXIT_FAILURE);
134 }
135 
136 NCURSES_EXPORT(void)
137 _nc_syserr_abort(const char *const fmt, ...)
138 {
139     va_list argp;
140 
141     where_is_problem();
142     va_start(argp, fmt);
143     vfprintf(stderr, fmt, argp);
144     fprintf(stderr, "\n");
145     va_end(argp);
146 
147 #if defined(TRACE) || !defined(NDEBUG)
148     /* If we're debugging, try to show where the problem occurred - this
149      * will dump core.
150      */
151 #ifndef USE_ROOT_ENVIRON
152     if (getuid() != ROOT_UID)
153 #endif
154 	abort();
155 #endif
156     /* Dumping core in production code is not a good idea.
157      */
158     exit(EXIT_FAILURE);
159 }
160 
161 #if NO_LEAKS
162 NCURSES_EXPORT(void)
163 _nc_comp_error_leaks(void)
164 {
165     FreeAndNull(SourceName);
166     FreeAndNull(TermType);
167 }
168 #endif
169