1/****************************************************************************
2 * Copyright (c) 1998-2002,2005 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.29 2005/08/20 19:22:36 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
50static const char *sourcename;
51static char *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    sourcename = name;
63}
64
65NCURSES_EXPORT(void)
66_nc_set_type(const char *const name)
67{
68    if (termtype == 0)
69	termtype = typeMalloc(char, MAX_NAME_SIZE + 1);
70    if (termtype != 0) {
71	termtype[0] = '\0';
72	if (name)
73	    strncat(termtype, name, MAX_NAME_SIZE);
74    }
75}
76
77NCURSES_EXPORT(void)
78_nc_get_type(char *name)
79{
80#if NO_LEAKS
81    if (name == 0 && termtype != 0) {
82	FreeAndNull(termtype);
83	return;
84    }
85#endif
86    if (name != 0)
87	strcpy(name, termtype != 0 ? termtype : "");
88}
89
90static inline void
91where_is_problem(void)
92{
93    fprintf(stderr, "\"%s\"", sourcename);
94    if (_nc_curr_line >= 0)
95	fprintf(stderr, ", line %d", _nc_curr_line);
96    if (_nc_curr_col >= 0)
97	fprintf(stderr, ", col %d", _nc_curr_col);
98    if (termtype != 0 && termtype[0] != '\0')
99	fprintf(stderr, ", terminal '%s'", termtype);
100    fputc(':', stderr);
101    fputc(' ', stderr);
102}
103
104NCURSES_EXPORT(void)
105_nc_warning(const char *const fmt,...)
106{
107    va_list argp;
108
109    if (_nc_suppress_warnings)
110	return;
111
112    where_is_problem();
113    va_start(argp, fmt);
114    vfprintf(stderr, fmt, argp);
115    fprintf(stderr, "\n");
116    va_end(argp);
117}
118
119NCURSES_EXPORT(void)
120_nc_err_abort(const char *const fmt,...)
121{
122    va_list argp;
123
124    where_is_problem();
125    va_start(argp, fmt);
126    vfprintf(stderr, fmt, argp);
127    fprintf(stderr, "\n");
128    va_end(argp);
129    exit(EXIT_FAILURE);
130}
131
132NCURSES_EXPORT(void)
133_nc_syserr_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
143    /* If we're debugging, try to show where the problem occurred - this
144     * will dump core.
145     */
146#if defined(TRACE) || !defined(NDEBUG)
147    abort();
148#else
149    /* Dumping core in production code is not a good idea.
150     */
151    exit(EXIT_FAILURE);
152#endif
153}
154