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