]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_cur_term.c
ncurses 6.3 - patch 20211106
[ncurses.git] / ncurses / tinfo / lib_cur_term.c
1 /****************************************************************************
2  * Copyright 2020,2021 Thomas E. Dickey                                     *
3  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /****************************************************************************
31  *  Author: Thomas E. Dickey <dickey@clark.net> 1997                        *
32  ****************************************************************************/
33 /*
34  * Module that "owns" the 'cur_term' variable:
35  *
36  *      TERMINAL *set_curterm(TERMINAL *)
37  *      int del_curterm(TERMINAL *)
38  */
39
40 #include <curses.priv.h>
41 #include <termcap.h>            /* ospeed */
42 #include <tic.h>                /* VALID_STRING */
43
44 MODULE_ID("$Id: lib_cur_term.c,v 1.44 2021/11/06 19:04:21 tom Exp $")
45
46 #undef CUR
47 #define CUR TerminalType(termp).
48
49 #if USE_REENTRANT
50
51 NCURSES_EXPORT(TERMINAL *)
52 NCURSES_SP_NAME(_nc_get_cur_term) (NCURSES_SP_DCL0)
53 {
54     return ((0 != TerminalOf(SP_PARM)) ? TerminalOf(SP_PARM) : CurTerm);
55 }
56
57 #if NCURSES_SP_FUNCS
58
59 NCURSES_EXPORT(TERMINAL *)
60 _nc_get_cur_term(void)
61 {
62     return NCURSES_SP_NAME(_nc_get_cur_term) (CURRENT_SCREEN);
63 }
64 #endif
65
66 NCURSES_EXPORT(TERMINAL *)
67 NCURSES_PUBLIC_VAR(cur_term) (void)
68 {
69 #if NCURSES_SP_FUNCS
70     return NCURSES_SP_NAME(_nc_get_cur_term) (CURRENT_SCREEN);
71 #else
72     return NCURSES_SP_NAME(_nc_get_cur_term) (NCURSES_SP_ARG);
73 #endif
74 }
75
76 #else
77 NCURSES_EXPORT_VAR(TERMINAL *) cur_term = 0;
78 #endif
79
80 NCURSES_EXPORT(TERMINAL *)
81 NCURSES_SP_NAME(set_curterm) (NCURSES_SP_DCLx TERMINAL *termp)
82 {
83     TERMINAL *oldterm;
84
85     T((T_CALLED("set_curterm(%p)"), (void *) termp));
86
87     _nc_lock_global(curses);
88     oldterm = cur_term;
89     if (SP_PARM)
90         SP_PARM->_term = termp;
91 #if USE_REENTRANT
92     CurTerm = termp;
93 #else
94     cur_term = termp;
95 #endif
96     if (termp != 0) {
97 #ifdef USE_TERM_DRIVER
98         TERMINAL_CONTROL_BLOCK *TCB = (TERMINAL_CONTROL_BLOCK *) termp;
99         ospeed = (NCURSES_OSPEED) _nc_ospeed(termp->_baudrate);
100         if (TCB->drv &&
101             TCB->drv->isTerminfo &&
102             TerminalType(termp).Strings) {
103             PC = (char) (VALID_STRING(pad_char) ? pad_char[0] : 0);
104         }
105         TCB->csp = SP_PARM;
106 #else
107         ospeed = (NCURSES_OSPEED) _nc_ospeed(termp->_baudrate);
108         if (TerminalType(termp).Strings) {
109             PC = (char) (VALID_STRING(pad_char) ? pad_char[0] : 0);
110         }
111 #endif
112 #if !USE_REENTRANT
113         save_ttytype(termp);
114 #endif
115     }
116     _nc_unlock_global(curses);
117
118     T((T_RETURN("%p"), (void *) oldterm));
119     return (oldterm);
120 }
121
122 #if NCURSES_SP_FUNCS
123 NCURSES_EXPORT(TERMINAL *)
124 set_curterm(TERMINAL *termp)
125 {
126     return NCURSES_SP_NAME(set_curterm) (CURRENT_SCREEN, termp);
127 }
128 #endif
129
130 NCURSES_EXPORT(int)
131 NCURSES_SP_NAME(del_curterm) (NCURSES_SP_DCLx TERMINAL *termp)
132 {
133     int rc = ERR;
134
135     T((T_CALLED("del_curterm(%p, %p)"), (void *) SP_PARM, (void *) termp));
136
137     if (termp != 0) {
138 #ifdef USE_TERM_DRIVER
139         TERMINAL_CONTROL_BLOCK *TCB = (TERMINAL_CONTROL_BLOCK *) termp;
140 #endif
141         TERMINAL *cur = (
142 #if USE_REENTRANT
143                             NCURSES_SP_NAME(_nc_get_cur_term) (NCURSES_SP_ARG)
144 #else
145                             cur_term
146 #endif
147         );
148
149 #if NCURSES_EXT_NUMBERS
150         _nc_free_termtype(&termp->type);
151 #endif
152         _nc_free_termtype2(&TerminalType(termp));
153         if (termp == cur)
154             NCURSES_SP_NAME(set_curterm) (NCURSES_SP_ARGx 0);
155
156         FreeIfNeeded(termp->_termname);
157 #if USE_HOME_TERMINFO
158         if (_nc_globals.home_terminfo != 0) {
159             FreeAndNull(_nc_globals.home_terminfo);
160         }
161 #endif
162 #ifdef USE_TERM_DRIVER
163         if (TCB->drv)
164             TCB->drv->td_release(TCB);
165 #endif
166 #if NO_LEAKS
167         /* discard memory used in tgetent's cache for this terminal */
168         _nc_tgetent_leak(termp);
169 #endif
170         free(termp->tparm_state.fmt_buff);
171         free(termp->tparm_state.out_buff);
172         free(termp);
173
174         rc = OK;
175     }
176
177     returnCode(rc);
178 }
179
180 #if NCURSES_SP_FUNCS
181 NCURSES_EXPORT(int)
182 del_curterm(TERMINAL *termp)
183 {
184     int rc;
185
186     _nc_lock_global(curses);
187     rc = NCURSES_SP_NAME(del_curterm) (CURRENT_SCREEN, termp);
188     _nc_unlock_global(curses);
189
190     return (rc);
191 }
192 #endif