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