blob: b817f49bd78a78c76c48526f4c2ea90ce5b1019d [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2022,2024 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
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 *
Steve Kondikae271bc2015-11-15 02:50:53 +010034 * and: Juergen Pfeifer *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053035 ****************************************************************************/
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
micky3879b9f5e72025-07-08 18:04:53 -040046MODULE_ID("$Id: lib_data.c,v 1.89 2024/02/24 18:11:38 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053047
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{
Steve Kondikae271bc2015-11-15 02:50:53 +010056 return CURRENT_SCREEN ? StdScreen(CURRENT_SCREEN) : 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053057}
58NCURSES_EXPORT(WINDOW *)
59NCURSES_PUBLIC_VAR(curscr) (void)
60{
Steve Kondikae271bc2015-11-15 02:50:53 +010061 return CURRENT_SCREEN ? CurScreen(CURRENT_SCREEN) : 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053062}
63NCURSES_EXPORT(WINDOW *)
64NCURSES_PUBLIC_VAR(newscr) (void)
65{
Steve Kondikae271bc2015-11-15 02:50:53 +010066 return CURRENT_SCREEN ? NewScreen(CURRENT_SCREEN) : 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053067}
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{
micky3879b9f5e72025-07-08 18:04:53 -040098 my_screen = _nc_alloc_screen_sp();
99 T(("_nc_alloc_screen_sp %p", my_screen));
100 return (my_screen != 0);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530101}
102
103NCURSES_EXPORT(void)
104_nc_set_screen(SCREEN *sp)
105{
106 my_screen = sp;
107}
108
109#else
Steve Kondikae271bc2015-11-15 02:50:53 +0100110
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530111NCURSES_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 = {
Steve Kondikae271bc2015-11-15 02:50:53 +0100120 0, /* have_sigtstp */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530121 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 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100132 0, /* tic_directory */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530133
134 NULL, /* dbi_list */
135 0, /* dbi_size */
136
137 NULL, /* first_name */
138 NULL, /* keyname_table */
Steve Kondikae271bc2015-11-15 02:50:53 +0100139 0, /* init_keyname */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530140
141 0, /* slk_format */
142
micky3879b9f5e72025-07-08 18:04:53 -0400143 2048, /* getstr_limit */
144
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530145 NULL, /* safeprint_buf */
146 0, /* safeprint_used */
147
148 TGETENT_0s, /* tgetent_cache */
149 0, /* tgetent_index */
150 0, /* tgetent_sequence */
micky3879b9f5e72025-07-08 18:04:53 -0400151 0, /* terminal_count */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530152
Steve Kondikae271bc2015-11-15 02:50:53 +0100153 0, /* dbd_blob */
154 0, /* dbd_list */
155 0, /* dbd_size */
156 0, /* dbd_time */
157 { { 0, 0 } }, /* dbd_vars */
158
micky3879b9f5e72025-07-08 18:04:53 -0400159#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
Steve Kondikae271bc2015-11-15 02:50:53 +0100168#ifndef USE_SP_WINDOWLIST
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530169 0, /* _nc_windowlist */
Steve Kondikae271bc2015-11-15 02:50:53 +0100170#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530171
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
micky3879b9f5e72025-07-08 18:04:53 -0400181#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 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100192#endif
micky3879b9f5e72025-07-08 18:04:53 -0400193#endif
194#if USE_WIDEC_SUPPORT
195 CHARS_0s, /* key_name */
196#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530197#ifdef TRACE
micky3879b9f5e72025-07-08 18:04:53 -0400198 FALSE, /* trace_opened */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530199 0, /* trace_level */
200 NULL, /* trace_fp */
micky3879b9f5e72025-07-08 18:04:53 -0400201 -1, /* trace_fd */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530202
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 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100220#if !defined(USE_PTHREADS) && USE_REENTRANT
221 0, /* nested_tracef */
222#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530223#endif /* TRACE */
micky3879b9f5e72025-07-08 18:04:53 -0400224#if NO_LEAKS
225 FALSE, /* leak_checking */
Steve Kondikae271bc2015-11-15 02:50:53 +0100226#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530227};
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 = {
micky3879b9f5e72025-07-08 18:04:53 -0400237 NULL, /* allocated */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530238 TRUE, /* use_env */
239 FALSE, /* filter_mode */
240 A_NORMAL, /* previous_attr */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530241 { /* tparm_state */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530242 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
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530254 NUM_VARS_0s, /* static_vars */
micky3879b9f5e72025-07-08 18:04:53 -0400255#ifdef TRACE
256 NULL, /* tname */
257#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530258 },
259 NULL, /* saved_tty */
micky3879b9f5e72025-07-08 18:04:53 -0400260 FALSE, /* use_tioctl */
261 0, /* _outch */
262#ifndef USE_SP_RIPOFF
263 RIPOFF_0s, /* ripoff */
264 NULL, /* rsp */
265#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530266#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 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100273 8, /* TABSIZE */
274 1000, /* ESCDELAY */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530275 0, /* cur_term */
micky3879b9f5e72025-07-08 18:04:53 -0400276#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530277#ifdef TRACE
micky3879b9f5e72025-07-08 18:04:53 -0400278#if BROKEN_LINKER || USE_REENTRANT
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530279 0L, /* _outchars */
280 NULL, /* _tputs_trace */
281#endif
282#endif
283};
284/* *INDENT-ON* */
285
Steve Kondikae271bc2015-11-15 02:50:53 +0100286/*
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
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530302/******************************************************************************/
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);
micky3879b9f5e72025-07-08 18:04:53 -0400312 _nc_mutex_init(&_nc_globals.mutex_prescreen);
313 _nc_mutex_init(&_nc_globals.mutex_screen);
314 _nc_mutex_init(&_nc_globals.mutex_update);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530315 _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{
micky3879b9f5e72025-07-08 18:04:53 -0400365 int rc = 0;
366 if (_nc_use_pthreads != 0)
367 rc = pthread_mutex_lock(obj);
368 return rc;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530369}
370
371NCURSES_EXPORT(int)
372_nc_mutex_trylock(pthread_mutex_t * obj)
373{
micky3879b9f5e72025-07-08 18:04:53 -0400374 int rc = 0;
375 if (_nc_use_pthreads != 0)
376 rc = pthread_mutex_trylock(obj);
377 return rc;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530378}
379
380NCURSES_EXPORT(int)
381_nc_mutex_unlock(pthread_mutex_t * obj)
382{
micky3879b9f5e72025-07-08 18:04:53 -0400383 int rc = 0;
384 if (_nc_use_pthreads != 0)
385 rc = pthread_mutex_unlock(obj);
386 return rc;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530387}
Steve Kondikae271bc2015-11-15 02:50:53 +0100388#endif /* USE_PTHREADS */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530389
Steve Kondikae271bc2015-11-15 02:50:53 +0100390#if defined(USE_PTHREADS) || USE_PTHREADS_EINTR
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530391#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
micky3879b9f5e72025-07-08 18:04:53 -0400402 return (sigprocmask) (how, newmask, oldmask);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530403}
404#endif
405#endif /* USE_PTHREADS */