1 /****************************************************************************
2 * Copyright (c) 1998-2017,2018 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
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 * and: Juergen Pfeifer *
34 ****************************************************************************/
39 ** Common data that may/may not be allocated, but is referenced globally
43 #include <curses.priv.h>
45 MODULE_ID("$Id: lib_data.c,v 1.78 2018/03/01 18:09:21 tom Exp $")
48 * OS/2's native linker complains if we don't initialize public data when
49 * constructing a dll (reported by J.J.G.Ripoll).
52 NCURSES_EXPORT(WINDOW *)
53 NCURSES_PUBLIC_VAR(stdscr) (void)
55 return CURRENT_SCREEN ? StdScreen(CURRENT_SCREEN) : 0;
57 NCURSES_EXPORT(WINDOW *)
58 NCURSES_PUBLIC_VAR(curscr) (void)
60 return CURRENT_SCREEN ? CurScreen(CURRENT_SCREEN) : 0;
62 NCURSES_EXPORT(WINDOW *)
63 NCURSES_PUBLIC_VAR(newscr) (void)
65 return CURRENT_SCREEN ? NewScreen(CURRENT_SCREEN) : 0;
68 NCURSES_EXPORT_VAR(WINDOW *) stdscr = 0;
69 NCURSES_EXPORT_VAR(WINDOW *) curscr = 0;
70 NCURSES_EXPORT_VAR(WINDOW *) newscr = 0;
73 NCURSES_EXPORT_VAR(SCREEN *) _nc_screen_chain = 0;
76 * The variable 'SP' will be defined as a function on systems that cannot link
77 * data-only modules, since it is used in a lot of places within ncurses and we
78 * cannot guarantee that any application will use any particular function. We
79 * put the WINDOW variables in this module, because it appears that any
80 * application that uses them will also use 'SP'.
82 * This module intentionally does not reference other ncurses modules, to avoid
83 * module coupling that increases the size of the executable.
86 static SCREEN *my_screen;
88 NCURSES_EXPORT(SCREEN *)
95 _nc_alloc_screen(void)
97 my_screen = _nc_alloc_screen_sp();
98 T(("_nc_alloc_screen_sp %p", my_screen));
99 return (my_screen != 0);
103 _nc_set_screen(SCREEN *sp)
110 NCURSES_EXPORT_VAR(SCREEN *) SP = NULL; /* Some linkers require initialized data... */
113 #define CHARS_0s { '\0' }
115 #define TGETENT_0 { 0L, FALSE, NULL, NULL, NULL }
116 #define TGETENT_0s { TGETENT_0, TGETENT_0, TGETENT_0, TGETENT_0 }
118 NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals = {
119 0, /* have_sigtstp */
120 0, /* have_sigwinch */
121 0, /* cleanup_nested */
123 FALSE, /* init_signals */
124 FALSE, /* init_screen */
126 NULL, /* comp_sourcename */
127 NULL, /* comp_termtype */
129 FALSE, /* have_tic_directory */
130 FALSE, /* keep_tic_directory */
131 0, /* tic_directory */
136 NULL, /* first_name */
137 NULL, /* keyname_table */
138 0, /* init_keyname */
142 NULL, /* safeprint_buf */
143 0, /* safeprint_used */
145 TGETENT_0s, /* tgetent_cache */
146 0, /* tgetent_index */
147 0, /* tgetent_sequence */
153 { { 0, 0 } }, /* dbd_vars */
155 #ifdef USE_TERM_DRIVER
159 #ifndef USE_SP_WINDOWLIST
160 0, /* _nc_windowlist */
163 #if USE_HOME_TERMINFO
164 NULL, /* home_terminfo */
167 #if !USE_SAFE_SPRINTF
168 0, /* safeprint_cols */
169 0, /* safeprint_rows */
173 PTHREAD_MUTEX_INITIALIZER, /* mutex_curses */
174 PTHREAD_MUTEX_INITIALIZER, /* mutex_prescreen */
175 PTHREAD_MUTEX_INITIALIZER, /* mutex_screen */
176 PTHREAD_MUTEX_INITIALIZER, /* mutex_update */
177 PTHREAD_MUTEX_INITIALIZER, /* mutex_tst_tracef */
178 PTHREAD_MUTEX_INITIALIZER, /* mutex_tracef */
179 0, /* nested_tracef */
180 0, /* use_pthreads */
181 #if USE_PTHREADS_EINTR
185 #if USE_WIDEC_SUPPORT
186 CHARS_0s, /* key_name */
189 FALSE, /* trace_opened */
190 CHARS_0s, /* trace_fname */
195 NULL, /* tracearg_buf */
196 0, /* tracearg_used */
198 NULL, /* tracebuf_ptr */
199 0, /* tracebuf_used */
201 CHARS_0s, /* tracechr_buf */
203 NULL, /* tracedmp_buf */
204 0, /* tracedmp_used */
206 NULL, /* tracetry_buf */
207 0, /* tracetry_used */
209 { CHARS_0s, CHARS_0s }, /* traceatr_color_buf */
210 0, /* traceatr_color_sel */
211 -1, /* traceatr_color_last */
212 #if !defined(USE_PTHREADS) && USE_REENTRANT
213 0, /* nested_tracef */
217 FALSE, /* leak_checking */
221 #define STACK_FRAME_0 { { 0 }, 0 }
222 #define STACK_FRAME_0s { STACK_FRAME_0 }
223 #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 }
225 #define RIPOFF_0 { 0,0,0 }
226 #define RIPOFF_0s { RIPOFF_0 }
228 NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen = {
229 NULL, /* allocated */
231 FALSE, /* filter_mode */
232 A_NORMAL, /* previous_attr */
234 NULL, /* tparam_base */
236 STACK_FRAME_0s, /* stack */
246 NUM_VARS_0s, /* dynamic_var */
247 NUM_VARS_0s, /* static_vars */
252 NULL, /* saved_tty */
253 FALSE, /* use_tioctl */
255 #ifndef USE_SP_RIPOFF
256 RIPOFF_0s, /* ripoff */
259 #if NCURSES_NO_PADDING
260 FALSE, /* flag to set if padding disabled */
262 #if BROKEN_LINKER || USE_REENTRANT
263 NULL, /* real_acs_map */
271 #if BROKEN_LINKER || USE_REENTRANT
273 NULL, /* _tputs_trace */
280 * wgetch() and other functions with a WINDOW* parameter may use a SCREEN*
281 * internally, and it is useful to allow those to be invoked without switching
282 * SCREEN's, e.g., for multi-threaded applications.
284 NCURSES_EXPORT(SCREEN *)
285 _nc_screen_of(WINDOW *win)
290 sp = WINDOW_EXT(win, screen);
295 /******************************************************************************/
298 init_global_mutexes(void)
300 static bool initialized = FALSE;
304 _nc_mutex_init(&_nc_globals.mutex_curses);
305 _nc_mutex_init(&_nc_globals.mutex_prescreen);
306 _nc_mutex_init(&_nc_globals.mutex_screen);
307 _nc_mutex_init(&_nc_globals.mutex_update);
308 _nc_mutex_init(&_nc_globals.mutex_tst_tracef);
309 _nc_mutex_init(&_nc_globals.mutex_tracef);
314 _nc_init_pthreads(void)
316 if (_nc_use_pthreads)
318 # if USE_WEAK_SYMBOLS
319 if ((pthread_mutex_init) == 0)
321 if ((pthread_mutex_lock) == 0)
323 if ((pthread_mutex_unlock) == 0)
325 if ((pthread_mutex_trylock) == 0)
327 if ((pthread_mutexattr_settype) == 0)
330 _nc_use_pthreads = 1;
331 init_global_mutexes();
335 * Use recursive mutexes if we have them - they're part of Unix98.
336 * For the cases where we do not, _nc_mutex_trylock() is used to avoid a
337 * deadlock, at the expense of memory leaks and unexpected failures that
338 * may not be handled by typical clients.
340 * FIXME - need configure check for PTHREAD_MUTEX_RECURSIVE, define it to
341 * PTHREAD_MUTEX_NORMAL if not supported.
344 _nc_mutex_init(pthread_mutex_t * obj)
346 pthread_mutexattr_t recattr;
348 if (_nc_use_pthreads) {
349 pthread_mutexattr_init(&recattr);
350 pthread_mutexattr_settype(&recattr, PTHREAD_MUTEX_RECURSIVE);
351 pthread_mutex_init(obj, &recattr);
356 _nc_mutex_lock(pthread_mutex_t * obj)
358 if (_nc_use_pthreads == 0)
360 return pthread_mutex_lock(obj);
364 _nc_mutex_trylock(pthread_mutex_t * obj)
366 if (_nc_use_pthreads == 0)
368 return pthread_mutex_trylock(obj);
372 _nc_mutex_unlock(pthread_mutex_t * obj)
374 if (_nc_use_pthreads == 0)
376 return pthread_mutex_unlock(obj);
378 #endif /* USE_PTHREADS */
380 #if defined(USE_PTHREADS) || USE_PTHREADS_EINTR
383 * NB: sigprocmask(2) is global but pthread_sigmask(3p)
384 * only for the calling thread.
387 _nc_sigprocmask(int how, const sigset_t * newmask, sigset_t * oldmask)
389 if ((pthread_sigmask))
390 return pthread_sigmask(how, newmask, oldmask);
392 return (sigprocmask) (how, newmask, oldmask);
395 #endif /* USE_PTHREADS */