1/****************************************************************************
2 * Copyright 2018-2022,2024 Thomas E. Dickey                                *
3 * Copyright 1998-2016,2017 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 *     and: Juergen Pfeifer                                                 *
35 ****************************************************************************/
36
37/*
38**	lib_data.c
39**
40**	Common data that may/may not be allocated, but is referenced globally
41**
42*/
43
44#include <curses.priv.h>
45
46MODULE_ID("$Id: lib_data.c,v 1.89 2024/02/24 18:11:38 tom Exp $")
47
48/*
49 * OS/2's native linker complains if we don't initialize public data when
50 * constructing a dll (reported by J.J.G.Ripoll).
51 */
52#if USE_REENTRANT
53NCURSES_EXPORT(WINDOW *)
54NCURSES_PUBLIC_VAR(stdscr) (void)
55{
56    return CURRENT_SCREEN ? StdScreen(CURRENT_SCREEN) : 0;
57}
58NCURSES_EXPORT(WINDOW *)
59NCURSES_PUBLIC_VAR(curscr) (void)
60{
61    return CURRENT_SCREEN ? CurScreen(CURRENT_SCREEN) : 0;
62}
63NCURSES_EXPORT(WINDOW *)
64NCURSES_PUBLIC_VAR(newscr) (void)
65{
66    return CURRENT_SCREEN ? NewScreen(CURRENT_SCREEN) : 0;
67}
68#else
69NCURSES_EXPORT_VAR(WINDOW *) stdscr = 0;
70NCURSES_EXPORT_VAR(WINDOW *) curscr = 0;
71NCURSES_EXPORT_VAR(WINDOW *) newscr = 0;
72#endif
73
74NCURSES_EXPORT_VAR(SCREEN *) _nc_screen_chain = 0;
75
76/*
77 * The variable 'SP' will be defined as a function on systems that cannot link
78 * data-only modules, since it is used in a lot of places within ncurses and we
79 * cannot guarantee that any application will use any particular function.  We
80 * put the WINDOW variables in this module, because it appears that any
81 * application that uses them will also use 'SP'.
82 *
83 * This module intentionally does not reference other ncurses modules, to avoid
84 * module coupling that increases the size of the executable.
85 */
86#if BROKEN_LINKER
87static SCREEN *my_screen;
88
89NCURSES_EXPORT(SCREEN *)
90_nc_screen(void)
91{
92    return my_screen;
93}
94
95NCURSES_EXPORT(int)
96_nc_alloc_screen(void)
97{
98    my_screen = _nc_alloc_screen_sp();
99    T(("_nc_alloc_screen_sp %p", my_screen));
100    return (my_screen != 0);
101}
102
103NCURSES_EXPORT(void)
104_nc_set_screen(SCREEN *sp)
105{
106    my_screen = sp;
107}
108
109#else
110
111NCURSES_EXPORT_VAR(SCREEN *) SP = NULL; /* Some linkers require initialized data... */
112#endif
113/* *INDENT-OFF* */
114#define CHARS_0s { '\0' }
115
116#define TGETENT_0 { 0L, FALSE, NULL, NULL, NULL }
117#define TGETENT_0s { TGETENT_0, TGETENT_0, TGETENT_0, TGETENT_0 }
118
119NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals = {
120    0,				/* have_sigtstp */
121    0,				/* have_sigwinch */
122    0,				/* cleanup_nested */
123
124    FALSE,			/* init_signals */
125    FALSE,			/* init_screen */
126
127    NULL,			/* comp_sourcename */
128    NULL,			/* comp_termtype */
129
130    FALSE,			/* have_tic_directory */
131    FALSE,			/* keep_tic_directory */
132    0,				/* tic_directory */
133
134    NULL,			/* dbi_list */
135    0,				/* dbi_size */
136
137    NULL,			/* first_name */
138    NULL,			/* keyname_table */
139    0,				/* init_keyname */
140
141    0,				/* slk_format */
142
143    2048,			/* getstr_limit */
144
145    NULL,			/* safeprint_buf */
146    0,				/* safeprint_used */
147
148    TGETENT_0s,			/* tgetent_cache */
149    0,				/* tgetent_index */
150    0,				/* tgetent_sequence */
151    0,				/* terminal_count */
152
153    0,				/* dbd_blob */
154    0,				/* dbd_list */
155    0,				/* dbd_size */
156    0,				/* dbd_time */
157    { { 0, 0 } },		/* dbd_vars */
158
159#if HAVE_TSEARCH
160    NULL,			/* cached_tparm */
161    0,				/* count_tparm */
162#endif /* HAVE_TSEARCH */
163
164#ifdef USE_TERM_DRIVER
165    0,				/* term_driver */
166#endif
167
168#ifndef USE_SP_WINDOWLIST
169    0,				/* _nc_windowlist */
170#endif
171
172#if USE_HOME_TERMINFO
173    NULL,			/* home_terminfo */
174#endif
175
176#if !USE_SAFE_SPRINTF
177    0,				/* safeprint_cols */
178    0,				/* safeprint_rows */
179#endif
180
181#ifdef USE_PTHREADS
182    PTHREAD_MUTEX_INITIALIZER,	/* mutex_curses */
183    PTHREAD_MUTEX_INITIALIZER,	/* mutex_prescreen */
184    PTHREAD_MUTEX_INITIALIZER,	/* mutex_screen */
185    PTHREAD_MUTEX_INITIALIZER,	/* mutex_update */
186    PTHREAD_MUTEX_INITIALIZER,	/* mutex_tst_tracef */
187    PTHREAD_MUTEX_INITIALIZER,	/* mutex_tracef */
188    0,				/* nested_tracef */
189    0,				/* use_pthreads */
190#if USE_PTHREADS_EINTR
191    0,				/* read_thread */
192#endif
193#endif
194#if USE_WIDEC_SUPPORT
195    CHARS_0s,			/* key_name */
196#endif
197#ifdef TRACE
198    FALSE,			/* trace_opened */
199    0,				/* trace_level */
200    NULL,			/* trace_fp */
201    -1,				/* trace_fd */
202
203    NULL,			/* tracearg_buf */
204    0,				/* tracearg_used */
205
206    NULL,			/* tracebuf_ptr */
207    0,				/* tracebuf_used */
208
209    CHARS_0s,			/* tracechr_buf */
210
211    NULL,			/* tracedmp_buf */
212    0,				/* tracedmp_used */
213
214    NULL,			/* tracetry_buf */
215    0,				/* tracetry_used */
216
217    { CHARS_0s, CHARS_0s },	/* traceatr_color_buf */
218    0,				/* traceatr_color_sel */
219    -1,				/* traceatr_color_last */
220#if !defined(USE_PTHREADS) && USE_REENTRANT
221    0,				/* nested_tracef */
222#endif
223#endif /* TRACE */
224#if NO_LEAKS
225    FALSE,			/* leak_checking */
226#endif
227};
228
229#define STACK_FRAME_0	{ { 0 }, 0 }
230#define STACK_FRAME_0s	{ STACK_FRAME_0 }
231#define NUM_VARS_0s	{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }
232
233#define RIPOFF_0	{ 0,0,0 }
234#define RIPOFF_0s	{ RIPOFF_0 }
235
236NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen = {
237    NULL,			/* allocated */
238    TRUE,			/* use_env */
239    FALSE,			/* filter_mode */
240    A_NORMAL,			/* previous_attr */
241    {				/* tparm_state */
242	NULL,			/* tparam_base */
243
244	STACK_FRAME_0s,		/* stack */
245	0,			/* stack_ptr */
246
247	NULL,			/* out_buff */
248	0,			/* out_size */
249	0,			/* out_used */
250
251	NULL,			/* fmt_buff */
252	0,			/* fmt_size */
253
254	NUM_VARS_0s,		/* static_vars */
255#ifdef TRACE
256	NULL,			/* tname */
257#endif
258    },
259    NULL,			/* saved_tty */
260    FALSE,			/* use_tioctl */
261    0,				/* _outch */
262#ifndef USE_SP_RIPOFF
263    RIPOFF_0s,			/* ripoff */
264    NULL,			/* rsp */
265#endif
266#if NCURSES_NO_PADDING
267    FALSE,			/* flag to set if padding disabled  */
268#endif
269#if BROKEN_LINKER || USE_REENTRANT
270    NULL,			/* real_acs_map */
271    0,				/* LINES */
272    0,				/* COLS */
273    8,				/* TABSIZE */
274    1000,			/* ESCDELAY */
275    0,				/* cur_term */
276#endif
277#ifdef TRACE
278#if BROKEN_LINKER || USE_REENTRANT
279    0L,				/* _outchars */
280    NULL,			/* _tputs_trace */
281#endif
282#endif
283};
284/* *INDENT-ON* */
285
286/*
287 * wgetch() and other functions with a WINDOW* parameter may use a SCREEN*
288 * internally, and it is useful to allow those to be invoked without switching
289 * SCREEN's, e.g., for multi-threaded applications.
290 */
291NCURSES_EXPORT(SCREEN *)
292_nc_screen_of(WINDOW *win)
293{
294    SCREEN *sp = 0;
295
296    if (win != 0) {
297	sp = WINDOW_EXT(win, screen);
298    }
299    return (sp);
300}
301
302/******************************************************************************/
303#ifdef USE_PTHREADS
304static void
305init_global_mutexes(void)
306{
307    static bool initialized = FALSE;
308
309    if (!initialized) {
310	initialized = TRUE;
311	_nc_mutex_init(&_nc_globals.mutex_curses);
312	_nc_mutex_init(&_nc_globals.mutex_prescreen);
313	_nc_mutex_init(&_nc_globals.mutex_screen);
314	_nc_mutex_init(&_nc_globals.mutex_update);
315	_nc_mutex_init(&_nc_globals.mutex_tst_tracef);
316	_nc_mutex_init(&_nc_globals.mutex_tracef);
317    }
318}
319
320NCURSES_EXPORT(void)
321_nc_init_pthreads(void)
322{
323    if (_nc_use_pthreads)
324	return;
325# if USE_WEAK_SYMBOLS
326    if ((pthread_mutex_init) == 0)
327	return;
328    if ((pthread_mutex_lock) == 0)
329	return;
330    if ((pthread_mutex_unlock) == 0)
331	return;
332    if ((pthread_mutex_trylock) == 0)
333	return;
334    if ((pthread_mutexattr_settype) == 0)
335	return;
336# endif
337    _nc_use_pthreads = 1;
338    init_global_mutexes();
339}
340
341/*
342 * Use recursive mutexes if we have them - they're part of Unix98.
343 * For the cases where we do not, _nc_mutex_trylock() is used to avoid a
344 * deadlock, at the expense of memory leaks and unexpected failures that
345 * may not be handled by typical clients.
346 *
347 * FIXME - need configure check for PTHREAD_MUTEX_RECURSIVE, define it to
348 * PTHREAD_MUTEX_NORMAL if not supported.
349 */
350NCURSES_EXPORT(void)
351_nc_mutex_init(pthread_mutex_t * obj)
352{
353    pthread_mutexattr_t recattr;
354
355    if (_nc_use_pthreads) {
356	pthread_mutexattr_init(&recattr);
357	pthread_mutexattr_settype(&recattr, PTHREAD_MUTEX_RECURSIVE);
358	pthread_mutex_init(obj, &recattr);
359    }
360}
361
362NCURSES_EXPORT(int)
363_nc_mutex_lock(pthread_mutex_t * obj)
364{
365    int rc = 0;
366    if (_nc_use_pthreads != 0)
367	rc = pthread_mutex_lock(obj);
368    return rc;
369}
370
371NCURSES_EXPORT(int)
372_nc_mutex_trylock(pthread_mutex_t * obj)
373{
374    int rc = 0;
375    if (_nc_use_pthreads != 0)
376	rc = pthread_mutex_trylock(obj);
377    return rc;
378}
379
380NCURSES_EXPORT(int)
381_nc_mutex_unlock(pthread_mutex_t * obj)
382{
383    int rc = 0;
384    if (_nc_use_pthreads != 0)
385	rc = pthread_mutex_unlock(obj);
386    return rc;
387}
388#endif /* USE_PTHREADS */
389
390#if defined(USE_PTHREADS) || USE_PTHREADS_EINTR
391#if USE_WEAK_SYMBOLS
392/*
393 * NB: sigprocmask(2) is global but pthread_sigmask(3p)
394 * only for the calling thread.
395 */
396NCURSES_EXPORT(int)
397_nc_sigprocmask(int how, const sigset_t * newmask, sigset_t * oldmask)
398{
399    if ((pthread_sigmask))
400	return pthread_sigmask(how, newmask, oldmask);
401    else
402	return (sigprocmask) (how, newmask, oldmask);
403}
404#endif
405#endif /* USE_PTHREADS */
406