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