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