150276Speter/****************************************************************************
2184989Srafan * Copyright (c) 1998-2003,2008 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
3050276Speter *  Author: Thomas E. Dickey <dickey@clark.net> 1997                        *
3150276Speter ****************************************************************************/
3250276Speter/*
3350276Speter * Module that "owns" the 'cur_term' variable:
3450276Speter *
3550276Speter *	TERMINAL *set_curterm(TERMINAL *)
3650276Speter *	int del_curterm(TERMINAL *)
3750276Speter */
3850276Speter
3950276Speter#include <curses.priv.h>
4076726Speter#include <term_entry.h>		/* TTY, cur_term */
4176726Speter#include <termcap.h>		/* ospeed */
4250276Speter
43184989SrafanMODULE_ID("$Id: lib_cur_term.c,v 1.18 2008/08/16 19:22:55 tom Exp $")
4450276Speter
45184989Srafan#undef CUR
46184989Srafan#define CUR termp->type.
47184989Srafan
48184989Srafan#if BROKEN_LINKER || USE_REENTRANT
49184989SrafanNCURSES_EXPORT(TERMINAL *)
50184989SrafanNCURSES_PUBLIC_VAR(cur_term) (void)
51184989Srafan{
52184989Srafan    return (SP != 0 && SP->_term != 0) ? SP->_term : _nc_prescreen._cur_term;
53184989Srafan}
54184989Srafan#else
5576726SpeterNCURSES_EXPORT_VAR(TERMINAL *) cur_term = 0;
56184989Srafan#endif
5750276Speter
5876726SpeterNCURSES_EXPORT(TERMINAL *)
5976726Speterset_curterm(TERMINAL * termp)
6050276Speter{
61184989Srafan    TERMINAL *oldterm;
6250276Speter
63166124Srafan    T((T_CALLED("set_curterm(%p)"), termp));
64166124Srafan
65184989Srafan    _nc_lock_global(curses);
66184989Srafan    oldterm = cur_term;
67184989Srafan    if (SP)
68184989Srafan	SP->_term = termp;
69184989Srafan#if BROKEN_LINKER || USE_REENTRANT
70184989Srafan    _nc_prescreen._cur_term = termp;
71184989Srafan#else
72184989Srafan    cur_term = termp;
73184989Srafan#endif
74184989Srafan    if (termp != 0) {
75184989Srafan	ospeed = _nc_ospeed(termp->_baudrate);
76184989Srafan	if (termp->type.Strings) {
77184989Srafan	    PC = (char) ((pad_char != NULL) ? pad_char[0] : 0);
78184989Srafan	}
7976726Speter    }
80184989Srafan    _nc_unlock_global(curses);
81184989Srafan
82166124Srafan    T((T_RETURN("%p"), oldterm));
83166124Srafan    return (oldterm);
8450276Speter}
8550276Speter
8676726SpeterNCURSES_EXPORT(int)
8776726Speterdel_curterm(TERMINAL * termp)
8850276Speter{
89184989Srafan    int rc = ERR;
90184989Srafan
9176726Speter    T((T_CALLED("del_curterm(%p)"), termp));
9250276Speter
93184989Srafan    _nc_lock_global(curses);
9476726Speter    if (termp != 0) {
9576726Speter	_nc_free_termtype(&(termp->type));
96166124Srafan	FreeIfNeeded(termp->_termname);
9776726Speter	free(termp);
9876726Speter	if (termp == cur_term)
99184989Srafan	    set_curterm(0);
100184989Srafan	rc = OK;
10176726Speter    }
102184989Srafan    _nc_unlock_global(curses);
103184989Srafan
104184989Srafan    returnCode(rc);
10550276Speter}
106