]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tinfo/lib_data.c
ncurses 6.2 - patch 20200627
[ncurses.git] / ncurses / tinfo / lib_data.c
index ba37e5dc8c49b34b594bf33f3bf4e4d6227b2a7c..39274f4e7c39c923688edf9f63fefc0009ad4cc7 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
+ * Copyright 2018,2020 Thomas E. Dickey                                     *
+ * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -30,6 +31,7 @@
  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
  *     and: Thomas E. Dickey                        1996-on                 *
+ *     and: Juergen Pfeifer                                                 *
  ****************************************************************************/
 
 /*
@@ -41,7 +43,7 @@
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: lib_data.c,v 1.43 2008/03/29 21:16:49 tom Exp $")
+MODULE_ID("$Id: lib_data.c,v 1.81 2020/06/13 22:01:14 tom Exp $")
 
 /*
  * OS/2's native linker complains if we don't initialize public data when
@@ -51,17 +53,17 @@ MODULE_ID("$Id: lib_data.c,v 1.43 2008/03/29 21:16:49 tom Exp $")
 NCURSES_EXPORT(WINDOW *)
 NCURSES_PUBLIC_VAR(stdscr) (void)
 {
-    return SP ? SP->_stdscr : 0;
+    return CURRENT_SCREEN ? StdScreen(CURRENT_SCREEN) : 0;
 }
 NCURSES_EXPORT(WINDOW *)
 NCURSES_PUBLIC_VAR(curscr) (void)
 {
-    return SP ? SP->_curscr : 0;
+    return CURRENT_SCREEN ? CurScreen(CURRENT_SCREEN) : 0;
 }
 NCURSES_EXPORT(WINDOW *)
 NCURSES_PUBLIC_VAR(newscr) (void)
 {
-    return SP ? SP->_newscr : 0;
+    return CURRENT_SCREEN ? NewScreen(CURRENT_SCREEN) : 0;
 }
 #else
 NCURSES_EXPORT_VAR(WINDOW *) stdscr = 0;
@@ -93,7 +95,9 @@ _nc_screen(void)
 NCURSES_EXPORT(int)
 _nc_alloc_screen(void)
 {
-    return ((my_screen = typeCalloc(SCREEN, 1)) != 0);
+    my_screen = _nc_alloc_screen_sp();
+    T(("_nc_alloc_screen_sp %p", my_screen));
+    return (my_screen != 0);
 }
 
 NCURSES_EXPORT(void)
@@ -103,6 +107,7 @@ _nc_set_screen(SCREEN *sp)
 }
 
 #else
+
 NCURSES_EXPORT_VAR(SCREEN *) SP = NULL; /* Some linkers require initialized data... */
 #endif
 /* *INDENT-OFF* */
@@ -112,6 +117,7 @@ NCURSES_EXPORT_VAR(SCREEN *) SP = NULL; /* Some linkers require initialized data
 #define TGETENT_0s { TGETENT_0, TGETENT_0, TGETENT_0, TGETENT_0 }
 
 NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals = {
+    0,                         /* have_sigtstp */
     0,                         /* have_sigwinch */
     0,                         /* cleanup_nested */
 
@@ -123,16 +129,19 @@ NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals = {
 
     FALSE,                     /* have_tic_directory */
     FALSE,                     /* keep_tic_directory */
-    TERMINFO,                  /* tic_directory */
+    0,                         /* tic_directory */
 
     NULL,                      /* dbi_list */
     0,                         /* dbi_size */
 
     NULL,                      /* first_name */
     NULL,                      /* keyname_table */
+    0,                         /* init_keyname */
 
     0,                         /* slk_format */
 
+    2048,                      /* getstr_limit */
+
     NULL,                      /* safeprint_buf */
     0,                         /* safeprint_used */
 
@@ -140,7 +149,24 @@ NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals = {
     0,                         /* tgetent_index */
     0,                         /* tgetent_sequence */
 
+    0,                         /* dbd_blob */
+    0,                         /* dbd_list */
+    0,                         /* dbd_size */
+    0,                         /* dbd_time */
+    { { 0, 0 } },              /* dbd_vars */
+
+#if HAVE_TSEARCH
+    NULL,                      /* cached_tparm */
+    0,                         /* count_tparm */
+#endif /* HAVE_TSEARCH */
+
+#ifdef USE_TERM_DRIVER
+    0,                         /* term_driver */
+#endif
+
+#ifndef USE_SP_WINDOWLIST
     0,                         /* _nc_windowlist */
+#endif
 
 #if USE_HOME_TERMINFO
     NULL,                      /* home_terminfo */
@@ -151,11 +177,28 @@ NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals = {
     0,                         /* safeprint_rows */
 #endif
 
+#ifdef USE_PTHREADS
+    PTHREAD_MUTEX_INITIALIZER, /* mutex_curses */
+    PTHREAD_MUTEX_INITIALIZER, /* mutex_prescreen */
+    PTHREAD_MUTEX_INITIALIZER, /* mutex_screen */
+    PTHREAD_MUTEX_INITIALIZER, /* mutex_update */
+    PTHREAD_MUTEX_INITIALIZER, /* mutex_tst_tracef */
+    PTHREAD_MUTEX_INITIALIZER, /* mutex_tracef */
+    0,                         /* nested_tracef */
+    0,                         /* use_pthreads */
+#if USE_PTHREADS_EINTR
+    0,                         /* read_thread */
+#endif
+#endif
+#if USE_WIDEC_SUPPORT
+    CHARS_0s,                  /* key_name */
+#endif
 #ifdef TRACE
-    FALSE,                     /* init_trace */
+    FALSE,                     /* trace_opened */
     CHARS_0s,                  /* trace_fname */
     0,                         /* trace_level */
     NULL,                      /* trace_fp */
+    -1,                                /* trace_fd */
 
     NULL,                      /* tracearg_buf */
     0,                         /* tracearg_used */
@@ -168,25 +211,19 @@ NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals = {
     NULL,                      /* tracedmp_buf */
     0,                         /* tracedmp_used */
 
-    CHARS_0s,                  /* tracemse_buf */
-
     NULL,                      /* tracetry_buf */
     0,                         /* tracetry_used */
 
     { CHARS_0s, CHARS_0s },    /* traceatr_color_buf */
     0,                         /* traceatr_color_sel */
     -1,                                /* traceatr_color_last */
-
-#endif /* TRACE */
-#ifdef USE_PTHREADS
-    PTHREAD_MUTEX_INITIALIZER, /* mutex_set_SP */
-    PTHREAD_MUTEX_INITIALIZER, /* mutex_use_screen */
-    PTHREAD_MUTEX_INITIALIZER, /* mutex_use_window */
-    PTHREAD_MUTEX_INITIALIZER, /* mutex_windowlist */
-    PTHREAD_MUTEX_INITIALIZER, /* mutex_tst_tracef */
-    PTHREAD_MUTEX_INITIALIZER, /* mutex_tracef */
+#if !defined(USE_PTHREADS) && USE_REENTRANT
     0,                         /* nested_tracef */
 #endif
+#endif /* TRACE */
+#if NO_LEAKS
+    FALSE,                     /* leak_checking */
+#endif
 };
 
 #define STACK_FRAME_0  { { 0 }, 0 }
@@ -197,15 +234,11 @@ NCURSES_EXPORT_VAR(NCURSES_GLOBALS) _nc_globals = {
 #define RIPOFF_0s      { RIPOFF_0 }
 
 NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen = {
+    NULL,                      /* allocated */
     TRUE,                      /* use_env */
     FALSE,                     /* filter_mode */
     A_NORMAL,                  /* previous_attr */
-    RIPOFF_0s,                 /* ripoff */
-    NULL,                      /* rsp */
     {                          /* tparm_state */
-#ifdef TRACE
-       NULL,                   /* tname */
-#endif
        NULL,                   /* tparam_base */
 
        STACK_FRAME_0s,         /* stack */
@@ -220,13 +253,30 @@ NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen = {
 
        NUM_VARS_0s,            /* dynamic_var */
        NUM_VARS_0s,            /* static_vars */
+#ifdef TRACE
+       NULL,                   /* tname */
+#endif
     },
     NULL,                      /* saved_tty */
+    FALSE,                     /* use_tioctl */
+    0,                         /* _outch */
+#ifndef USE_SP_RIPOFF
+    RIPOFF_0s,                 /* ripoff */
+    NULL,                      /* rsp */
+#endif
+#if NCURSES_NO_PADDING
+    FALSE,                     /* flag to set if padding disabled  */
+#endif
 #if BROKEN_LINKER || USE_REENTRANT
     NULL,                      /* real_acs_map */
     0,                         /* LINES */
     0,                         /* COLS */
+    8,                         /* TABSIZE */
+    1000,                      /* ESCDELAY */
+    0,                         /* cur_term */
+#endif
 #ifdef TRACE
+#if BROKEN_LINKER || USE_REENTRANT
     0L,                                /* _outchars */
     NULL,                      /* _tputs_trace */
 #endif
@@ -234,6 +284,22 @@ NCURSES_EXPORT_VAR(NCURSES_PRESCREEN) _nc_prescreen = {
 };
 /* *INDENT-ON* */
 
+/*
+ * wgetch() and other functions with a WINDOW* parameter may use a SCREEN*
+ * internally, and it is useful to allow those to be invoked without switching
+ * SCREEN's, e.g., for multi-threaded applications.
+ */
+NCURSES_EXPORT(SCREEN *)
+_nc_screen_of(WINDOW *win)
+{
+    SCREEN *sp = 0;
+
+    if (win != 0) {
+       sp = WINDOW_EXT(win, screen);
+    }
+    return (sp);
+}
+
 /******************************************************************************/
 #ifdef USE_PTHREADS
 static void
@@ -243,15 +309,36 @@ init_global_mutexes(void)
 
     if (!initialized) {
        initialized = TRUE;
-       _nc_mutex_init(&_nc_globals.mutex_set_SP);
-       _nc_mutex_init(&_nc_globals.mutex_use_screen);
-       _nc_mutex_init(&_nc_globals.mutex_use_window);
-       _nc_mutex_init(&_nc_globals.mutex_windowlist);
+       _nc_mutex_init(&_nc_globals.mutex_curses);
+       _nc_mutex_init(&_nc_globals.mutex_prescreen);
+       _nc_mutex_init(&_nc_globals.mutex_screen);
+       _nc_mutex_init(&_nc_globals.mutex_update);
        _nc_mutex_init(&_nc_globals.mutex_tst_tracef);
        _nc_mutex_init(&_nc_globals.mutex_tracef);
     }
 }
 
+NCURSES_EXPORT(void)
+_nc_init_pthreads(void)
+{
+    if (_nc_use_pthreads)
+       return;
+# if USE_WEAK_SYMBOLS
+    if ((pthread_mutex_init) == 0)
+       return;
+    if ((pthread_mutex_lock) == 0)
+       return;
+    if ((pthread_mutex_unlock) == 0)
+       return;
+    if ((pthread_mutex_trylock) == 0)
+       return;
+    if ((pthread_mutexattr_settype) == 0)
+       return;
+# endif
+    _nc_use_pthreads = 1;
+    init_global_mutexes();
+}
+
 /*
  * Use recursive mutexes if we have them - they're part of Unix98.
  * For the cases where we do not, _nc_mutex_trylock() is used to avoid a
@@ -266,29 +353,51 @@ _nc_mutex_init(pthread_mutex_t * obj)
 {
     pthread_mutexattr_t recattr;
 
-    memset(&recattr, 0, sizeof(recattr));
-    pthread_mutexattr_settype(&recattr, PTHREAD_MUTEX_RECURSIVE);
-    pthread_mutex_init(obj, &recattr);
+    if (_nc_use_pthreads) {
+       pthread_mutexattr_init(&recattr);
+       pthread_mutexattr_settype(&recattr, PTHREAD_MUTEX_RECURSIVE);
+       pthread_mutex_init(obj, &recattr);
+    }
 }
 
 NCURSES_EXPORT(int)
 _nc_mutex_lock(pthread_mutex_t * obj)
 {
-    init_global_mutexes();
+    if (_nc_use_pthreads == 0)
+       return 0;
     return pthread_mutex_lock(obj);
 }
 
 NCURSES_EXPORT(int)
 _nc_mutex_trylock(pthread_mutex_t * obj)
 {
-    init_global_mutexes();
+    if (_nc_use_pthreads == 0)
+       return 0;
     return pthread_mutex_trylock(obj);
 }
 
 NCURSES_EXPORT(int)
 _nc_mutex_unlock(pthread_mutex_t * obj)
 {
-    init_global_mutexes();
+    if (_nc_use_pthreads == 0)
+       return 0;
     return pthread_mutex_unlock(obj);
 }
 #endif /* USE_PTHREADS */
+
+#if defined(USE_PTHREADS) || USE_PTHREADS_EINTR
+#if USE_WEAK_SYMBOLS
+/*
+ * NB: sigprocmask(2) is global but pthread_sigmask(3p)
+ * only for the calling thread.
+ */
+NCURSES_EXPORT(int)
+_nc_sigprocmask(int how, const sigset_t * newmask, sigset_t * oldmask)
+{
+    if ((pthread_sigmask))
+       return pthread_sigmask(how, newmask, oldmask);
+    else
+       return (sigprocmask) (how, newmask, oldmask);
+}
+#endif
+#endif /* USE_PTHREADS */