]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_data.c
ncurses 5.7 - patch 20090606
[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  ****************************************************************************/
34
35 /*
36 **      lib_data.c
37 **
38 **      Common data that may/may not be allocated, but is referenced globally
39 **
40 */
41
42 #include <curses.priv.h>
43
44 MODULE_ID("$Id: lib_data.c,v 1.54 2009/06/06 18:46:31 tom Exp $")
45
46 /*
47  * OS/2's native linker complains if we don't initialize public data when
48  * constructing a dll (reported by J.J.G.Ripoll).
49  */
50 #if USE_REENTRANT
51 NCURSES_EXPORT(WINDOW *)
52 NCURSES_PUBLIC_VAR(stdscr) (void)
53 {
54     return CURRENT_SCREEN ? StdScreen(CURRENT_SCREEN) : 0;
55 }
56 NCURSES_EXPORT(WINDOW *)
57 NCURSES_PUBLIC_VAR(curscr) (void)
58 {
59     return CURRENT_SCREEN ? CurScreen(CURRENT_SCREEN) : 0;
60 }
61 NCURSES_EXPORT(WINDOW *)
62 NCURSES_PUBLIC_VAR(newscr) (void)
63 {
64     return CURRENT_SCREEN ? NewScreen(CURRENT_SCREEN) : 0;
65 }
66 #else
67 NCURSES_EXPORT_VAR(WINDOW *) stdscr = 0;
68 NCURSES_EXPORT_VAR(WINDOW *) curscr = 0;
69 NCURSES_EXPORT_VAR(WINDOW *) newscr = 0;
70 #endif
71
72 NCURSES_EXPORT_VAR(SCREEN *) _nc_screen_chain = 0;
73
74 /*
75  * The variable 'SP' will be defined as a function on systems that cannot link
76  * data-only modules, since it is used in a lot of places within ncurses and we
77  * cannot guarantee that any application will use any particular function.  We
78  * put the WINDOW variables in this module, because it appears that any
79  * application that uses them will also use 'SP'.
80  *
81  * This module intentionally does not reference other ncurses modules, to avoid
82  * module coupling that increases the size of the executable.
83  */
84 #if BROKEN_LINKER
85 static SCREEN *my_screen;
86
87 NCURSES_EXPORT(SCREEN *)
88 _nc_screen(void)
89 {
90     return my_screen;
91 }
92
93 NCURSES_EXPORT(int)
94 _nc_alloc_screen(void)
95 {
96     return ((my_screen = _nc_alloc_screen_sp()) != 0);
97 }
98
99 NCURSES_EXPORT(void)
100 _nc_set_screen(SCREEN *sp)
101 {
102     my_screen = sp;
103 }
104
105 #else
106
107 NCURSES_EXPORT_VAR(SCREEN *) SP = NULL; /* Some linkers require initialized data... */
108 #endif
109 /* *INDENT-OFF* */
110 #define CHARS_0s { '\0' }
111
112 #define TGETENT_0 { 0L, FALSE, NULL, NULL, NULL }
113 #define TGETENT_0s { TGETENT_0, TGETENT_0, TGETENT_0, TGETENT_0 }
114
115 NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals = {
116     0,                          /* have_sigwinch */
117     0,                          /* cleanup_nested */
118
119     FALSE,                      /* init_signals */
120     FALSE,                      /* init_screen */
121
122     NULL,                       /* comp_sourcename */
123     NULL,                       /* comp_termtype */
124
125     FALSE,                      /* have_tic_directory */
126     FALSE,                      /* keep_tic_directory */
127     TERMINFO,                   /* tic_directory */
128
129     NULL,                       /* dbi_list */
130     0,                          /* dbi_size */
131
132     NULL,                       /* first_name */
133     NULL,                       /* keyname_table */
134
135     0,                          /* slk_format */
136
137     NULL,                       /* safeprint_buf */
138     0,                          /* safeprint_used */
139
140     TGETENT_0s,                 /* tgetent_cache */
141     0,                          /* tgetent_index */
142     0,                          /* tgetent_sequence */
143
144 #ifndef USE_SP_WINDOWLIST
145     0,                          /* _nc_windowlist */
146 #endif
147
148 #if USE_HOME_TERMINFO
149     NULL,                       /* home_terminfo */
150 #endif
151
152 #if !USE_SAFE_SPRINTF
153     0,                          /* safeprint_cols */
154     0,                          /* safeprint_rows */
155 #endif
156
157 #ifdef TRACE
158     FALSE,                      /* init_trace */
159     CHARS_0s,                   /* trace_fname */
160     0,                          /* trace_level */
161     NULL,                       /* trace_fp */
162
163     NULL,                       /* tracearg_buf */
164     0,                          /* tracearg_used */
165
166     NULL,                       /* tracebuf_ptr */
167     0,                          /* tracebuf_used */
168
169     CHARS_0s,                   /* tracechr_buf */
170
171     NULL,                       /* tracedmp_buf */
172     0,                          /* tracedmp_used */
173
174     NULL,                       /* tracetry_buf */
175     0,                          /* tracetry_used */
176
177     { CHARS_0s, CHARS_0s },     /* traceatr_color_buf */
178     0,                          /* traceatr_color_sel */
179     -1,                         /* traceatr_color_last */
180 #if !defined(USE_PTHREADS) && USE_REENTRANT
181     0,                          /* nested_tracef */
182 #endif
183 #endif /* TRACE */
184 #ifdef USE_PTHREADS
185     PTHREAD_MUTEX_INITIALIZER,  /* mutex_curses */
186     PTHREAD_MUTEX_INITIALIZER,  /* mutex_tst_tracef */
187     PTHREAD_MUTEX_INITIALIZER,  /* mutex_tracef */
188     0,                          /* nested_tracef */
189     0,                          /* use_pthreads */
190 #endif
191 };
192
193 #define STACK_FRAME_0   { { 0 }, 0 }
194 #define STACK_FRAME_0s  { STACK_FRAME_0 }
195 #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 }
196
197 #define RIPOFF_0        { 0,0,0 }
198 #define RIPOFF_0s       { RIPOFF_0 }
199
200 NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen = {
201     TRUE,                       /* use_env */
202     FALSE,                      /* filter_mode */
203     A_NORMAL,                   /* previous_attr */
204 #ifndef USE_SP_RIPOFF
205     RIPOFF_0s,                  /* ripoff */
206     NULL,                       /* rsp */
207 #endif
208     {                           /* tparm_state */
209 #ifdef TRACE
210         NULL,                   /* tname */
211 #endif
212         NULL,                   /* tparam_base */
213
214         STACK_FRAME_0s,         /* stack */
215         0,                      /* stack_ptr */
216
217         NULL,                   /* out_buff */
218         0,                      /* out_size */
219         0,                      /* out_used */
220
221         NULL,                   /* fmt_buff */
222         0,                      /* fmt_size */
223
224         NUM_VARS_0s,            /* dynamic_var */
225         NUM_VARS_0s,            /* static_vars */
226     },
227     NULL,                       /* saved_tty */
228 #if NCURSES_NO_PADDING
229     FALSE,                      /* flag to set if padding disabled  */
230 #endif
231 #if BROKEN_LINKER || USE_REENTRANT
232     NULL,                       /* real_acs_map */
233     0,                          /* LINES */
234     0,                          /* COLS */
235     8,                          /* TABSIZE */
236     1000,                       /* ESCDELAY */
237     0,                          /* cur_term */
238 #ifdef TRACE
239     0L,                         /* _outchars */
240     NULL,                       /* _tputs_trace */
241 #endif
242 #endif
243 };
244 /* *INDENT-ON* */
245
246 /*
247  * wgetch() and other functions with a WINDOW* parameter may use a SCREEN*
248  * internally, and it is useful to allow those to be invoked without switching
249  * SCREEN's, e.g., for multi-threaded applications.
250  */
251 NCURSES_EXPORT(SCREEN *)
252 _nc_screen_of(WINDOW *win)
253 {
254     SCREEN *sp = 0;
255
256     if (win != 0) {
257         WINDOWLIST *wp = (WINDOWLIST *) win;
258         sp = wp->screen;
259     }
260     return (sp);
261 }
262
263 /******************************************************************************/
264 #ifdef USE_PTHREADS
265 static void
266 init_global_mutexes(void)
267 {
268     static bool initialized = FALSE;
269
270     if (!initialized) {
271         initialized = TRUE;
272         _nc_mutex_init(&_nc_globals.mutex_curses);
273         _nc_mutex_init(&_nc_globals.mutex_tst_tracef);
274         _nc_mutex_init(&_nc_globals.mutex_tracef);
275     }
276 }
277
278 NCURSES_EXPORT(void)
279 _nc_init_pthreads(void)
280 {
281     if (_nc_use_pthreads)
282         return;
283 # if USE_WEAK_SYMBOLS
284     if ((pthread_mutex_init) == 0)
285         return;
286     if ((pthread_mutex_lock) == 0)
287         return;
288     if ((pthread_mutex_unlock) == 0)
289         return;
290     if ((pthread_mutex_trylock) == 0)
291         return;
292     if ((pthread_mutexattr_settype) == 0)
293         return;
294 # endif
295     _nc_use_pthreads = 1;
296     init_global_mutexes();
297 }
298
299 /*
300  * Use recursive mutexes if we have them - they're part of Unix98.
301  * For the cases where we do not, _nc_mutex_trylock() is used to avoid a
302  * deadlock, at the expense of memory leaks and unexpected failures that
303  * may not be handled by typical clients.
304  *
305  * FIXME - need configure check for PTHREAD_MUTEX_RECURSIVE, define it to
306  * PTHREAD_MUTEX_NORMAL if not supported.
307  */
308 NCURSES_EXPORT(void)
309 _nc_mutex_init(pthread_mutex_t * obj)
310 {
311     pthread_mutexattr_t recattr;
312
313     if (_nc_use_pthreads) {
314         pthread_mutexattr_init(&recattr);
315         pthread_mutexattr_settype(&recattr, PTHREAD_MUTEX_RECURSIVE);
316         pthread_mutex_init(obj, &recattr);
317     }
318 }
319
320 NCURSES_EXPORT(int)
321 _nc_mutex_lock(pthread_mutex_t * obj)
322 {
323     if (_nc_use_pthreads == 0)
324         return 0;
325     return pthread_mutex_lock(obj);
326 }
327
328 NCURSES_EXPORT(int)
329 _nc_mutex_trylock(pthread_mutex_t * obj)
330 {
331     if (_nc_use_pthreads == 0)
332         return 0;
333     return pthread_mutex_trylock(obj);
334 }
335
336 NCURSES_EXPORT(int)
337 _nc_mutex_unlock(pthread_mutex_t * obj)
338 {
339     if (_nc_use_pthreads == 0)
340         return 0;
341     return pthread_mutex_unlock(obj);
342 }
343
344 #if USE_WEAK_SYMBOLS
345 /*
346  * NB: sigprocmask(2) is global but pthread_sigmask(3p)
347  * only for the calling thread.
348  */
349 NCURSES_EXPORT(int)
350 _nc_sigprocmask(int how, const sigset_t * newmask, sigset_t * oldmask)
351 {
352     if ((pthread_sigmask))
353         return pthread_sigmask(how, newmask, oldmask);
354     else
355         return sigprocmask(how, newmask, oldmask);
356 }
357 #endif
358 #endif /* USE_PTHREADS */