ncurses 6.0 - patch 20170311
[ncurses.git] / ncurses / tinfo / lib_data.c
1 /****************************************************************************
2  * Copyright (c) 1998-2016,2017 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.70 2017/01/14 17:52:32 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     return ((my_screen = _nc_alloc_screen_sp()) != 0);
98 }
99
100 NCURSES_EXPORT(void)
101 _nc_set_screen(SCREEN *sp)
102 {
103     my_screen = sp;
104 }
105
106 #else
107
108 NCURSES_EXPORT_VAR(SCREEN *) SP = NULL; /* Some linkers require initialized data... */
109 #endif
110 /* *INDENT-OFF* */
111 #define CHARS_0s { '\0' }
112
113 #define TGETENT_0 { 0L, FALSE, NULL, NULL, NULL }
114 #define TGETENT_0s { TGETENT_0, TGETENT_0, TGETENT_0, TGETENT_0 }
115
116 NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals = {
117     0,                          /* have_sigtstp */
118     0,                          /* have_sigwinch */
119     0,                          /* cleanup_nested */
120
121     FALSE,                      /* init_signals */
122     FALSE,                      /* init_screen */
123
124     NULL,                       /* comp_sourcename */
125     NULL,                       /* comp_termtype */
126
127     FALSE,                      /* have_tic_directory */
128     FALSE,                      /* keep_tic_directory */
129     0,                          /* tic_directory */
130
131     NULL,                       /* dbi_list */
132     0,                          /* dbi_size */
133
134     NULL,                       /* first_name */
135     NULL,                       /* keyname_table */
136     0,                          /* init_keyname */
137
138     0,                          /* slk_format */
139
140     NULL,                       /* safeprint_buf */
141     0,                          /* safeprint_used */
142
143     TGETENT_0s,                 /* tgetent_cache */
144     0,                          /* tgetent_index */
145     0,                          /* tgetent_sequence */
146
147     0,                          /* dbd_blob */
148     0,                          /* dbd_list */
149     0,                          /* dbd_size */
150     0,                          /* dbd_time */
151     { { 0, 0 } },               /* dbd_vars */
152
153 #ifndef USE_SP_WINDOWLIST
154     0,                          /* _nc_windowlist */
155 #endif
156
157 #if USE_HOME_TERMINFO
158     NULL,                       /* home_terminfo */
159 #endif
160
161 #if !USE_SAFE_SPRINTF
162     0,                          /* safeprint_cols */
163     0,                          /* safeprint_rows */
164 #endif
165
166 #ifdef USE_TERM_DRIVER
167     0,                          /* term_driver */
168 #endif
169
170 #ifdef TRACE
171     FALSE,                      /* trace_opened */
172     CHARS_0s,                   /* trace_fname */
173     0,                          /* trace_level */
174     NULL,                       /* trace_fp */
175     -1,                         /* trace_fd */
176
177     NULL,                       /* tracearg_buf */
178     0,                          /* tracearg_used */
179
180     NULL,                       /* tracebuf_ptr */
181     0,                          /* tracebuf_used */
182
183     CHARS_0s,                   /* tracechr_buf */
184
185     NULL,                       /* tracedmp_buf */
186     0,                          /* tracedmp_used */
187
188     NULL,                       /* tracetry_buf */
189     0,                          /* tracetry_used */
190
191     { CHARS_0s, CHARS_0s },     /* traceatr_color_buf */
192     0,                          /* traceatr_color_sel */
193     -1,                         /* traceatr_color_last */
194 #if !defined(USE_PTHREADS) && USE_REENTRANT
195     0,                          /* nested_tracef */
196 #endif
197 #endif /* TRACE */
198 #ifdef USE_PTHREADS
199     PTHREAD_MUTEX_INITIALIZER,  /* mutex_curses */
200     PTHREAD_MUTEX_INITIALIZER,  /* mutex_tst_tracef */
201     PTHREAD_MUTEX_INITIALIZER,  /* mutex_tracef */
202     0,                          /* nested_tracef */
203     0,                          /* use_pthreads */
204 #endif
205 #if USE_PTHREADS_EINTR
206     0,                          /* read_thread */
207 #endif
208 #if USE_WIDEC_SUPPORT
209     CHARS_0s,                   /* key_name */
210 #endif
211 };
212
213 #define STACK_FRAME_0   { { 0 }, 0 }
214 #define STACK_FRAME_0s  { STACK_FRAME_0 }
215 #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 }
216
217 #define RIPOFF_0        { 0,0,0 }
218 #define RIPOFF_0s       { RIPOFF_0 }
219
220 NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen = {
221     TRUE,                       /* use_env */
222     FALSE,                      /* filter_mode */
223     A_NORMAL,                   /* previous_attr */
224 #ifndef USE_SP_RIPOFF
225     RIPOFF_0s,                  /* ripoff */
226     NULL,                       /* rsp */
227 #endif
228     {                           /* tparm_state */
229 #ifdef TRACE
230         NULL,                   /* tname */
231 #endif
232         NULL,                   /* tparam_base */
233
234         STACK_FRAME_0s,         /* stack */
235         0,                      /* stack_ptr */
236
237         NULL,                   /* out_buff */
238         0,                      /* out_size */
239         0,                      /* out_used */
240
241         NULL,                   /* fmt_buff */
242         0,                      /* fmt_size */
243
244         NUM_VARS_0s,            /* dynamic_var */
245         NUM_VARS_0s,            /* static_vars */
246     },
247     NULL,                       /* saved_tty */
248 #if NCURSES_NO_PADDING
249     FALSE,                      /* flag to set if padding disabled  */
250 #endif
251     0,                          /* _outch */
252 #if BROKEN_LINKER || USE_REENTRANT
253     NULL,                       /* real_acs_map */
254     0,                          /* LINES */
255     0,                          /* COLS */
256     8,                          /* TABSIZE */
257     1000,                       /* ESCDELAY */
258     0,                          /* cur_term */
259 #ifdef TRACE
260     0L,                         /* _outchars */
261     NULL,                       /* _tputs_trace */
262 #endif
263 #endif
264     FALSE,                      /* use_tioctl */
265 };
266 /* *INDENT-ON* */
267
268 /*
269  * wgetch() and other functions with a WINDOW* parameter may use a SCREEN*
270  * internally, and it is useful to allow those to be invoked without switching
271  * SCREEN's, e.g., for multi-threaded applications.
272  */
273 NCURSES_EXPORT(SCREEN *)
274 _nc_screen_of(WINDOW *win)
275 {
276     SCREEN *sp = 0;
277
278     if (win != 0) {
279         sp = WINDOW_EXT(win, screen);
280     }
281     return (sp);
282 }
283
284 /******************************************************************************/
285 #ifdef USE_PTHREADS
286 static void
287 init_global_mutexes(void)
288 {
289     static bool initialized = FALSE;
290
291     if (!initialized) {
292         initialized = TRUE;
293         _nc_mutex_init(&_nc_globals.mutex_curses);
294         _nc_mutex_init(&_nc_globals.mutex_tst_tracef);
295         _nc_mutex_init(&_nc_globals.mutex_tracef);
296     }
297 }
298
299 NCURSES_EXPORT(void)
300 _nc_init_pthreads(void)
301 {
302     if (_nc_use_pthreads)
303         return;
304 # if USE_WEAK_SYMBOLS
305     if ((pthread_mutex_init) == 0)
306         return;
307     if ((pthread_mutex_lock) == 0)
308         return;
309     if ((pthread_mutex_unlock) == 0)
310         return;
311     if ((pthread_mutex_trylock) == 0)
312         return;
313     if ((pthread_mutexattr_settype) == 0)
314         return;
315 # endif
316     _nc_use_pthreads = 1;
317     init_global_mutexes();
318 }
319
320 /*
321  * Use recursive mutexes if we have them - they're part of Unix98.
322  * For the cases where we do not, _nc_mutex_trylock() is used to avoid a
323  * deadlock, at the expense of memory leaks and unexpected failures that
324  * may not be handled by typical clients.
325  *
326  * FIXME - need configure check for PTHREAD_MUTEX_RECURSIVE, define it to
327  * PTHREAD_MUTEX_NORMAL if not supported.
328  */
329 NCURSES_EXPORT(void)
330 _nc_mutex_init(pthread_mutex_t * obj)
331 {
332     pthread_mutexattr_t recattr;
333
334     if (_nc_use_pthreads) {
335         pthread_mutexattr_init(&recattr);
336         pthread_mutexattr_settype(&recattr, PTHREAD_MUTEX_RECURSIVE);
337         pthread_mutex_init(obj, &recattr);
338     }
339 }
340
341 NCURSES_EXPORT(int)
342 _nc_mutex_lock(pthread_mutex_t * obj)
343 {
344     if (_nc_use_pthreads == 0)
345         return 0;
346     return pthread_mutex_lock(obj);
347 }
348
349 NCURSES_EXPORT(int)
350 _nc_mutex_trylock(pthread_mutex_t * obj)
351 {
352     if (_nc_use_pthreads == 0)
353         return 0;
354     return pthread_mutex_trylock(obj);
355 }
356
357 NCURSES_EXPORT(int)
358 _nc_mutex_unlock(pthread_mutex_t * obj)
359 {
360     if (_nc_use_pthreads == 0)
361         return 0;
362     return pthread_mutex_unlock(obj);
363 }
364 #endif /* USE_PTHREADS */
365
366 #if defined(USE_PTHREADS) || USE_PTHREADS_EINTR
367 #if USE_WEAK_SYMBOLS
368 /*
369  * NB: sigprocmask(2) is global but pthread_sigmask(3p)
370  * only for the calling thread.
371  */
372 NCURSES_EXPORT(int)
373 _nc_sigprocmask(int how, const sigset_t * newmask, sigset_t * oldmask)
374 {
375     if ((pthread_sigmask))
376         return pthread_sigmask(how, newmask, oldmask);
377     else
378         return (sigprocmask) (how, newmask, oldmask);
379 }
380 #endif
381 #endif /* USE_PTHREADS */