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#include <tic.h>		/* VALID_STRING */
43
44MODULE_ID("$Id: lib_cur_term.c,v 1.43 2020/10/24 18:54:32 tom Exp $")
45
46#undef CUR
47#define CUR TerminalType(termp).
48
49#if USE_REENTRANT
50
51NCURSES_EXPORT(TERMINAL *)
52NCURSES_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
59NCURSES_EXPORT(TERMINAL *)
60_nc_get_cur_term(void)
61{
62    return NCURSES_SP_NAME(_nc_get_cur_term) (CURRENT_SCREEN);
63}
64#endif
65
66NCURSES_EXPORT(TERMINAL *)
67NCURSES_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
77NCURSES_EXPORT_VAR(TERMINAL *) cur_term = 0;
78#endif
79
80NCURSES_EXPORT(TERMINAL *)
81NCURSES_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
123NCURSES_EXPORT(TERMINAL *)
124set_curterm(TERMINAL *termp)
125{
126    return NCURSES_SP_NAME(set_curterm) (CURRENT_SCREEN, termp);
127}
128#endif
129
130NCURSES_EXPORT(int)
131NCURSES_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);
171
172	rc = OK;
173    }
174    returnCode(rc);
175}
176
177#if NCURSES_SP_FUNCS
178NCURSES_EXPORT(int)
179del_curterm(TERMINAL *termp)
180{
181    int rc;
182
183    _nc_lock_global(curses);
184    rc = NCURSES_SP_NAME(del_curterm) (CURRENT_SCREEN, termp);
185    _nc_unlock_global(curses);
186
187    return (rc);
188}
189#endif
190