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