xref: /freebsd/contrib/ncurses/ncurses/tinfo/comp_error.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /****************************************************************************
2  * Copyright 2019-2020,2023 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.44 2023/06/15 20:27:02 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     if (name == NULL) {
64 	free(SourceName);
65 	SourceName = NULL;
66     } else if (SourceName == NULL) {
67 	SourceName = strdup(name);
68     } else if (strcmp(name, SourceName)) {
69 	free(SourceName);
70 	SourceName = strdup(name);
71     }
72 }
73 
74 NCURSES_EXPORT(void)
75 _nc_set_type(const char *const name)
76 {
77 #define MY_SIZE (size_t) MAX_NAME_SIZE
78     if (TermType == 0)
79 	TermType = typeMalloc(char, MY_SIZE + 1);
80     if (TermType != 0) {
81 	TermType[0] = '\0';
82 	if (name) {
83 	    _nc_STRNCAT(TermType, name, MY_SIZE, MY_SIZE);
84 	}
85     }
86 }
87 
88 NCURSES_EXPORT(void)
89 _nc_get_type(char *name)
90 {
91 #if NO_LEAKS
92     if (name == 0 && TermType != 0) {
93 	FreeAndNull(TermType);
94 	return;
95     }
96 #endif
97     if (name != 0)
98 	_nc_STRCPY(name, TermType != 0 ? TermType : "", MAX_NAME_SIZE);
99 }
100 
101 static NCURSES_INLINE void
102 where_is_problem(void)
103 {
104     fprintf(stderr, "\"%s\"", SourceName ? SourceName : "?");
105     if (_nc_curr_line > 0)
106 	fprintf(stderr, ", line %d", _nc_curr_line);
107     if (_nc_curr_col > 0)
108 	fprintf(stderr, ", col %d", _nc_curr_col);
109     if (TermType != 0 && TermType[0] != '\0')
110 	fprintf(stderr, ", terminal '%s'", TermType);
111     fputc(':', stderr);
112     fputc(' ', stderr);
113 }
114 
115 NCURSES_EXPORT(void)
116 _nc_warning(const char *const fmt, ...)
117 {
118     va_list argp;
119 
120     if (_nc_suppress_warnings)
121 	return;
122 
123     where_is_problem();
124     va_start(argp, fmt);
125     vfprintf(stderr, fmt, argp);
126     fprintf(stderr, "\n");
127     va_end(argp);
128 }
129 
130 NCURSES_EXPORT(void)
131 _nc_err_abort(const char *const fmt, ...)
132 {
133     va_list argp;
134 
135     where_is_problem();
136     va_start(argp, fmt);
137     vfprintf(stderr, fmt, argp);
138     fprintf(stderr, "\n");
139     va_end(argp);
140     exit(EXIT_FAILURE);
141 }
142 
143 NCURSES_EXPORT(void)
144 _nc_syserr_abort(const char *const fmt, ...)
145 {
146     va_list argp;
147 
148     where_is_problem();
149     va_start(argp, fmt);
150     vfprintf(stderr, fmt, argp);
151     fprintf(stderr, "\n");
152     va_end(argp);
153 
154 #if defined(TRACE) || !defined(NDEBUG)
155     /* If we're debugging, try to show where the problem occurred - this
156      * will dump core.
157      */
158     if (_nc_env_access())
159 	abort();
160 #endif
161     /* Dumping core in production code is not a good idea.
162      */
163     exit(EXIT_FAILURE);
164 }
165 
166 #if NO_LEAKS
167 NCURSES_EXPORT(void)
168 _nc_comp_error_leaks(void)
169 {
170     FreeAndNull(SourceName);
171     FreeAndNull(TermType);
172 }
173 #endif
174