1 /****************************************************************************
2 * Copyright 2020 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
43 MODULE_ID("$Id: lib_cur_term.c,v 1.42 2020/02/02 23:34:34 tom Exp $")
44
45 #undef CUR
46 #define CUR TerminalType(termp).
47
48 #if USE_REENTRANT
49
NCURSES_EXPORT(TERMINAL *)50 NCURSES_EXPORT(TERMINAL *)
51 NCURSES_SP_NAME(_nc_get_cur_term) (NCURSES_SP_DCL0)
52 {
53 return ((0 != TerminalOf(SP_PARM)) ? TerminalOf(SP_PARM) : CurTerm);
54 }
55
56 #if NCURSES_SP_FUNCS
57
58 NCURSES_EXPORT(TERMINAL *)
_nc_get_cur_term(void)59 _nc_get_cur_term(void)
60 {
61 return NCURSES_SP_NAME(_nc_get_cur_term) (CURRENT_SCREEN);
62 }
63 #endif
64
65 NCURSES_EXPORT(TERMINAL *)
NCURSES_PUBLIC_VAR(cur_term)66 NCURSES_PUBLIC_VAR(cur_term) (void)
67 {
68 #if NCURSES_SP_FUNCS
69 return NCURSES_SP_NAME(_nc_get_cur_term) (CURRENT_SCREEN);
70 #else
71 return NCURSES_SP_NAME(_nc_get_cur_term) (NCURSES_SP_ARG);
72 #endif
73 }
74
75 #else
76 NCURSES_EXPORT_VAR(TERMINAL *) cur_term = 0;
77 #endif
78
79 NCURSES_EXPORT(TERMINAL *)
NCURSES_SP_NAME(set_curterm)80 NCURSES_SP_NAME(set_curterm) (NCURSES_SP_DCLx TERMINAL *termp)
81 {
82 TERMINAL *oldterm;
83
84 T((T_CALLED("set_curterm(%p)"), (void *) termp));
85
86 _nc_lock_global(curses);
87 oldterm = cur_term;
88 if (SP_PARM)
89 SP_PARM->_term = termp;
90 #if USE_REENTRANT
91 CurTerm = termp;
92 #else
93 cur_term = termp;
94 #endif
95 if (termp != 0) {
96 #ifdef USE_TERM_DRIVER
97 TERMINAL_CONTROL_BLOCK *TCB = (TERMINAL_CONTROL_BLOCK *) termp;
98 ospeed = (NCURSES_OSPEED) _nc_ospeed(termp->_baudrate);
99 if (TCB->drv &&
100 TCB->drv->isTerminfo &&
101 TerminalType(termp).Strings) {
102 PC = (char) ((pad_char != NULL) ? pad_char[0] : 0);
103 }
104 TCB->csp = SP_PARM;
105 #else
106 ospeed = (NCURSES_OSPEED) _nc_ospeed(termp->_baudrate);
107 if (TerminalType(termp).Strings) {
108 PC = (char) ((pad_char != NULL) ? pad_char[0] : 0);
109 }
110 #endif
111 #if !USE_REENTRANT
112 save_ttytype(termp);
113 #endif
114 }
115 _nc_unlock_global(curses);
116
117 T((T_RETURN("%p"), (void *) oldterm));
118 return (oldterm);
119 }
120
121 #if NCURSES_SP_FUNCS
122 NCURSES_EXPORT(TERMINAL *)
set_curterm(TERMINAL * termp)123 set_curterm(TERMINAL *termp)
124 {
125 return NCURSES_SP_NAME(set_curterm) (CURRENT_SCREEN, termp);
126 }
127 #endif
128
129 NCURSES_EXPORT(int)
NCURSES_SP_NAME(del_curterm)130 NCURSES_SP_NAME(del_curterm) (NCURSES_SP_DCLx TERMINAL *termp)
131 {
132 int rc = ERR;
133
134 T((T_CALLED("del_curterm(%p, %p)"), (void *) SP_PARM, (void *) termp));
135
136 if (termp != 0) {
137 #ifdef USE_TERM_DRIVER
138 TERMINAL_CONTROL_BLOCK *TCB = (TERMINAL_CONTROL_BLOCK *) termp;
139 #endif
140 TERMINAL *cur = (
141 #if USE_REENTRANT
142 NCURSES_SP_NAME(_nc_get_cur_term) (NCURSES_SP_ARG)
143 #else
144 cur_term
145 #endif
146 );
147
148 #if NCURSES_EXT_NUMBERS
149 _nc_free_termtype(&termp->type);
150 #endif
151 _nc_free_termtype2(&TerminalType(termp));
152 if (termp == cur)
153 NCURSES_SP_NAME(set_curterm) (NCURSES_SP_ARGx 0);
154
155 FreeIfNeeded(termp->_termname);
156 #if USE_HOME_TERMINFO
157 if (_nc_globals.home_terminfo != 0) {
158 FreeAndNull(_nc_globals.home_terminfo);
159 }
160 #endif
161 #ifdef USE_TERM_DRIVER
162 if (TCB->drv)
163 TCB->drv->td_release(TCB);
164 #endif
165 #if NO_LEAKS
166 /* discard memory used in tgetent's cache for this terminal */
167 _nc_tgetent_leak(termp);
168 #endif
169 free(termp);
170
171 rc = OK;
172 }
173 returnCode(rc);
174 }
175
176 #if NCURSES_SP_FUNCS
177 NCURSES_EXPORT(int)
del_curterm(TERMINAL * termp)178 del_curterm(TERMINAL *termp)
179 {
180 int rc;
181
182 _nc_lock_global(curses);
183 rc = NCURSES_SP_NAME(del_curterm) (CURRENT_SCREEN, termp);
184 _nc_unlock_global(curses);
185
186 return (rc);
187 }
188 #endif
189