1/****************************************************************************
2 * Copyright (c) 1998-2011,2012 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
44MODULE_ID("$Id: comp_error.c,v 1.36 2012/02/22 22:34:31 tom Exp $")
45
46NCURSES_EXPORT_VAR(bool) _nc_suppress_warnings = FALSE;
47NCURSES_EXPORT_VAR(int) _nc_curr_line = 0; /* current line # in input */
48NCURSES_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
53NCURSES_EXPORT(const char *)
54_nc_get_source(void)
55{
56    return SourceName;
57}
58
59NCURSES_EXPORT(void)
60_nc_set_source(const char *const name)
61{
62    FreeIfNeeded(SourceName);
63    SourceName = strdup(name);
64}
65
66NCURSES_EXPORT(void)
67_nc_set_type(const char *const name)
68{
69    if (TermType == 0)
70	TermType = typeMalloc(char, MAX_NAME_SIZE + 1);
71    if (TermType != 0) {
72	TermType[0] = '\0';
73	if (name)
74	    strncat(TermType, name, (size_t) MAX_NAME_SIZE);
75    }
76}
77
78NCURSES_EXPORT(void)
79_nc_get_type(char *name)
80{
81#if NO_LEAKS
82    if (name == 0 && TermType != 0) {
83	FreeAndNull(TermType);
84	return;
85    }
86#endif
87    if (name != 0)
88	_nc_STRCPY(name, TermType != 0 ? TermType : "", MAX_NAME_SIZE);
89}
90
91static NCURSES_INLINE void
92where_is_problem(void)
93{
94    fprintf(stderr, "\"%s\"", SourceName ? SourceName : "?");
95    if (_nc_curr_line >= 0)
96	fprintf(stderr, ", line %d", _nc_curr_line);
97    if (_nc_curr_col >= 0)
98	fprintf(stderr, ", col %d", _nc_curr_col);
99    if (TermType != 0 && TermType[0] != '\0')
100	fprintf(stderr, ", terminal '%s'", TermType);
101    fputc(':', stderr);
102    fputc(' ', stderr);
103}
104
105NCURSES_EXPORT(void)
106_nc_warning(const char *const fmt,...)
107{
108    va_list argp;
109
110    if (_nc_suppress_warnings)
111	return;
112
113    where_is_problem();
114    va_start(argp, fmt);
115    vfprintf(stderr, fmt, argp);
116    fprintf(stderr, "\n");
117    va_end(argp);
118}
119
120NCURSES_EXPORT(void)
121_nc_err_abort(const char *const fmt,...)
122{
123    va_list argp;
124
125    where_is_problem();
126    va_start(argp, fmt);
127    vfprintf(stderr, fmt, argp);
128    fprintf(stderr, "\n");
129    va_end(argp);
130    exit(EXIT_FAILURE);
131}
132
133NCURSES_EXPORT(void)
134_nc_syserr_abort(const char *const fmt,...)
135{
136    va_list argp;
137
138    where_is_problem();
139    va_start(argp, fmt);
140    vfprintf(stderr, fmt, argp);
141    fprintf(stderr, "\n");
142    va_end(argp);
143
144    /* If we're debugging, try to show where the problem occurred - this
145     * will dump core.
146     */
147#if defined(TRACE) || !defined(NDEBUG)
148    abort();
149#else
150    /* Dumping core in production code is not a good idea.
151     */
152    exit(EXIT_FAILURE);
153#endif
154}
155
156#if NO_LEAKS
157NCURSES_EXPORT(void)
158_nc_comp_error_leaks(void)
159{
160    FreeAndNull(SourceName);
161    FreeAndNull(TermType);
162}
163#endif
164