xref: /freebsd/contrib/ncurses/ncurses/tinfo/lib_cur_term.c (revision 21817992b3314c908ab50f0bb88d2ee750b9c4ac)
1 /****************************************************************************
2 ,* Copyright 2020-2021,2022 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.49 2022/05/28 17:56:55 tom Exp $")
45 
46 #undef CUR
47 #define CUR TerminalType(termp).
48 
49 #if USE_REENTRANT
50 
NCURSES_EXPORT(TERMINAL *)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 *)
_nc_get_cur_term(void)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 *)
NCURSES_PUBLIC_VAR(cur_term)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 *)
NCURSES_SP_NAME(set_curterm)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 *)
set_curterm(TERMINAL * termp)124 set_curterm(TERMINAL *termp)
125 {
126     return NCURSES_SP_NAME(set_curterm) (CURRENT_SCREEN, termp);
127 }
128 #endif
129 
130 NCURSES_EXPORT(int)
NCURSES_SP_NAME(del_curterm)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 #if NCURSES_EXT_COLORS
151 	_nc_free_termtype1(&termp->type);
152 #else
153 	_nc_free_termtype2(&termp->type);
154 #endif
155 #endif
156 	_nc_free_termtype2(&TerminalType(termp));
157 	if (termp == cur)
158 	    NCURSES_SP_NAME(set_curterm) (NCURSES_SP_ARGx 0);
159 
160 	FreeIfNeeded(termp->_termname);
161 #if USE_HOME_TERMINFO
162 	if (_nc_globals.home_terminfo != 0) {
163 	    FreeAndNull(_nc_globals.home_terminfo);
164 	}
165 #endif
166 #ifdef USE_TERM_DRIVER
167 	if (TCB->drv)
168 	    TCB->drv->td_release(TCB);
169 #endif
170 #if NO_LEAKS
171 	/* discard memory used in tgetent's cache for this terminal */
172 	_nc_tgetent_leak(termp);
173 #endif
174 	if (--_nc_globals.terminal_count == 0) {
175 	    _nc_free_tparm(termp);
176 	}
177 
178 	free(termp->tparm_state.fmt_buff);
179 	free(termp->tparm_state.out_buff);
180 	free(termp);
181 
182 	rc = OK;
183     }
184 
185     returnCode(rc);
186 }
187 
188 #if NCURSES_SP_FUNCS
189 NCURSES_EXPORT(int)
del_curterm(TERMINAL * termp)190 del_curterm(TERMINAL *termp)
191 {
192     int rc;
193 
194     _nc_lock_global(curses);
195     rc = NCURSES_SP_NAME(del_curterm) (CURRENT_SCREEN, termp);
196     _nc_unlock_global(curses);
197 
198     return (rc);
199 }
200 #endif
201