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