ncurses 6.2 - patch 20210306
[ncurses.git] / ncurses / tinfo / lib_data.c
1 /****************************************************************************
2  * Copyright 2018,2020 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
46 MODULE_ID("$Id: lib_data.c,v 1.81 2020/06/13 22:01:14 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
53 NCURSES_EXPORT(WINDOW *)
54 NCURSES_PUBLIC_VAR(stdscr) (void)
55 {
56     return CURRENT_SCREEN ? StdScreen(CURRENT_SCREEN) : 0;
57 }
58 NCURSES_EXPORT(WINDOW *)
59 NCURSES_PUBLIC_VAR(curscr) (void)
60 {
61     return CURRENT_SCREEN ? CurScreen(CURRENT_SCREEN) : 0;
62 }
63 NCURSES_EXPORT(WINDOW *)
64 NCURSES_PUBLIC_VAR(newscr) (void)
65 {
66     return CURRENT_SCREEN ? NewScreen(CURRENT_SCREEN) : 0;
67 }
68 #else
69 NCURSES_EXPORT_VAR(WINDOW *) stdscr = 0;
70 NCURSES_EXPORT_VAR(WINDOW *) curscr = 0;
71 NCURSES_EXPORT_VAR(WINDOW *) newscr = 0;
72 #endif
73
74 NCURSES_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
87 static SCREEN *my_screen;
88
89 NCURSES_EXPORT(SCREEN *)
90 _nc_screen(void)
91 {
92     return my_screen;
93 }
94
95 NCURSES_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
103 NCURSES_EXPORT(void)
104 _nc_set_screen(SCREEN *sp)
105 {
106     my_screen = sp;
107 }
108
109 #else
110
111 NCURSES_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
119 NCURSES_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
152     0,                          /* dbd_blob */
153     0,                          /* dbd_list */
154     0,                          /* dbd_size */
155     0,                          /* dbd_time */
156     { { 0, 0 } },               /* dbd_vars */
157
158 #if HAVE_TSEARCH
159     NULL,                       /* cached_tparm */
160     0,                          /* count_tparm */
161 #endif /* HAVE_TSEARCH */
162
163 #ifdef USE_TERM_DRIVER
164     0,                          /* term_driver */
165 #endif
166
167 #ifndef USE_SP_WINDOWLIST
168     0,                          /* _nc_windowlist */
169 #endif
170
171 #if USE_HOME_TERMINFO
172     NULL,                       /* home_terminfo */
173 #endif
174
175 #if !USE_SAFE_SPRINTF
176     0,                          /* safeprint_cols */
177     0,                          /* safeprint_rows */
178 #endif
179
180 #ifdef USE_PTHREADS
181     PTHREAD_MUTEX_INITIALIZER,  /* mutex_curses */
182     PTHREAD_MUTEX_INITIALIZER,  /* mutex_prescreen */
183     PTHREAD_MUTEX_INITIALIZER,  /* mutex_screen */
184     PTHREAD_MUTEX_INITIALIZER,  /* mutex_update */
185     PTHREAD_MUTEX_INITIALIZER,  /* mutex_tst_tracef */
186     PTHREAD_MUTEX_INITIALIZER,  /* mutex_tracef */
187     0,                          /* nested_tracef */
188     0,                          /* use_pthreads */
189 #if USE_PTHREADS_EINTR
190     0,                          /* read_thread */
191 #endif
192 #endif
193 #if USE_WIDEC_SUPPORT
194     CHARS_0s,                   /* key_name */
195 #endif
196 #ifdef TRACE
197     FALSE,                      /* trace_opened */
198     CHARS_0s,                   /* trace_fname */
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
236 NCURSES_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,            /* dynamic_var */
255         NUM_VARS_0s,            /* static_vars */
256 #ifdef TRACE
257         NULL,                   /* tname */
258 #endif
259     },
260     NULL,                       /* saved_tty */
261     FALSE,                      /* use_tioctl */
262     0,                          /* _outch */
263 #ifndef USE_SP_RIPOFF
264     RIPOFF_0s,                  /* ripoff */
265     NULL,                       /* rsp */
266 #endif
267 #if NCURSES_NO_PADDING
268     FALSE,                      /* flag to set if padding disabled  */
269 #endif
270 #if BROKEN_LINKER || USE_REENTRANT
271     NULL,                       /* real_acs_map */
272     0,                          /* LINES */
273     0,                          /* COLS */
274     8,                          /* TABSIZE */
275     1000,                       /* ESCDELAY */
276     0,                          /* cur_term */
277 #endif
278 #ifdef TRACE
279 #if BROKEN_LINKER || USE_REENTRANT
280     0L,                         /* _outchars */
281     NULL,                       /* _tputs_trace */
282 #endif
283 #endif
284 };
285 /* *INDENT-ON* */
286
287 /*
288  * wgetch() and other functions with a WINDOW* parameter may use a SCREEN*
289  * internally, and it is useful to allow those to be invoked without switching
290  * SCREEN's, e.g., for multi-threaded applications.
291  */
292 NCURSES_EXPORT(SCREEN *)
293 _nc_screen_of(WINDOW *win)
294 {
295     SCREEN *sp = 0;
296
297     if (win != 0) {
298         sp = WINDOW_EXT(win, screen);
299     }
300     return (sp);
301 }
302
303 /******************************************************************************/
304 #ifdef USE_PTHREADS
305 static void
306 init_global_mutexes(void)
307 {
308     static bool initialized = FALSE;
309
310     if (!initialized) {
311         initialized = TRUE;
312         _nc_mutex_init(&_nc_globals.mutex_curses);
313         _nc_mutex_init(&_nc_globals.mutex_prescreen);
314         _nc_mutex_init(&_nc_globals.mutex_screen);
315         _nc_mutex_init(&_nc_globals.mutex_update);
316         _nc_mutex_init(&_nc_globals.mutex_tst_tracef);
317         _nc_mutex_init(&_nc_globals.mutex_tracef);
318     }
319 }
320
321 NCURSES_EXPORT(void)
322 _nc_init_pthreads(void)
323 {
324     if (_nc_use_pthreads)
325         return;
326 # if USE_WEAK_SYMBOLS
327     if ((pthread_mutex_init) == 0)
328         return;
329     if ((pthread_mutex_lock) == 0)
330         return;
331     if ((pthread_mutex_unlock) == 0)
332         return;
333     if ((pthread_mutex_trylock) == 0)
334         return;
335     if ((pthread_mutexattr_settype) == 0)
336         return;
337 # endif
338     _nc_use_pthreads = 1;
339     init_global_mutexes();
340 }
341
342 /*
343  * Use recursive mutexes if we have them - they're part of Unix98.
344  * For the cases where we do not, _nc_mutex_trylock() is used to avoid a
345  * deadlock, at the expense of memory leaks and unexpected failures that
346  * may not be handled by typical clients.
347  *
348  * FIXME - need configure check for PTHREAD_MUTEX_RECURSIVE, define it to
349  * PTHREAD_MUTEX_NORMAL if not supported.
350  */
351 NCURSES_EXPORT(void)
352 _nc_mutex_init(pthread_mutex_t * obj)
353 {
354     pthread_mutexattr_t recattr;
355
356     if (_nc_use_pthreads) {
357         pthread_mutexattr_init(&recattr);
358         pthread_mutexattr_settype(&recattr, PTHREAD_MUTEX_RECURSIVE);
359         pthread_mutex_init(obj, &recattr);
360     }
361 }
362
363 NCURSES_EXPORT(int)
364 _nc_mutex_lock(pthread_mutex_t * obj)
365 {
366     if (_nc_use_pthreads == 0)
367         return 0;
368     return pthread_mutex_lock(obj);
369 }
370
371 NCURSES_EXPORT(int)
372 _nc_mutex_trylock(pthread_mutex_t * obj)
373 {
374     if (_nc_use_pthreads == 0)
375         return 0;
376     return pthread_mutex_trylock(obj);
377 }
378
379 NCURSES_EXPORT(int)
380 _nc_mutex_unlock(pthread_mutex_t * obj)
381 {
382     if (_nc_use_pthreads == 0)
383         return 0;
384     return pthread_mutex_unlock(obj);
385 }
386 #endif /* USE_PTHREADS */
387
388 #if defined(USE_PTHREADS) || USE_PTHREADS_EINTR
389 #if USE_WEAK_SYMBOLS
390 /*
391  * NB: sigprocmask(2) is global but pthread_sigmask(3p)
392  * only for the calling thread.
393  */
394 NCURSES_EXPORT(int)
395 _nc_sigprocmask(int how, const sigset_t * newmask, sigset_t * oldmask)
396 {
397     if ((pthread_sigmask))
398         return pthread_sigmask(how, newmask, oldmask);
399     else
400         return (sigprocmask) (how, newmask, oldmask);
401 }
402 #endif
403 #endif /* USE_PTHREADS */