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