1/**************************************************************************** 2 * Copyright 2018,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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 32 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 33 * and: Thomas E. Dickey 1996-on * 34 * and: Juergen Pfeifer 2009 * 35 ****************************************************************************/ 36 37/* 38** lib_newterm.c 39** 40** The newterm() function. 41** 42*/ 43 44#include <curses.priv.h> 45 46#ifndef CUR 47#define CUR SP_TERMTYPE 48#endif 49 50#include <tic.h> 51 52MODULE_ID("$Id: lib_newterm.c,v 1.103 2020/09/05 21:34:04 tom Exp $") 53 54#ifdef USE_TERM_DRIVER 55#define NumLabels InfoOf(SP_PARM).numlabels 56#else 57#define NumLabels num_labels 58#endif 59 60#ifndef ONLCR /* Allows compilation under the QNX 4.2 OS */ 61#define ONLCR 0 62#endif 63 64/* 65 * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not 66 * restored during the curses session. The library simulates echo in software. 67 * (The behavior is unspecified if the application enables hardware echo). 68 * 69 * The newterm function also initializes terminal settings, and since initscr 70 * is supposed to behave as if it calls newterm, we do it here. 71 */ 72static NCURSES_INLINE int 73_nc_initscr(NCURSES_SP_DCL0) 74{ 75 int result = ERR; 76 TERMINAL *term = TerminalOf(SP_PARM); 77 78 /* for extended XPG4 conformance requires cbreak() at this point */ 79 /* (SVr4 curses does this anyway) */ 80 T((T_CALLED("_nc_initscr(%p) ->term %p"), (void *) SP_PARM, (void *) term)); 81 if (NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG) == OK) { 82 TTY buf; 83 84 buf = term->Nttyb; 85#ifdef TERMIOS 86 buf.c_lflag &= (unsigned) ~(ECHO | ECHONL); 87 buf.c_iflag &= (unsigned) ~(ICRNL | INLCR | IGNCR); 88 buf.c_oflag &= (unsigned) ~(ONLCR); 89#elif HAVE_SGTTY_H 90 buf.sg_flags &= ~(ECHO | CRMOD); 91#elif defined(EXP_WIN32_DRIVER) 92 buf.dwFlagIn = CONMODE_IN_DEFAULT; 93 buf.dwFlagOut = CONMODE_OUT_DEFAULT | VT_FLAG_OUT; 94 if (WINCONSOLE.isTermInfoConsole) { 95 buf.dwFlagIn |= VT_FLAG_IN; 96 } 97#else 98 memset(&buf, 0, sizeof(buf)); 99#endif 100 result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf); 101 if (result == OK) 102 term->Nttyb = buf; 103 } 104 returnCode(result); 105} 106 107/* 108 * filter() has to be called before either initscr() or newterm(), so there is 109 * apparently no way to make this flag apply to some terminals and not others, 110 * aside from possibly delaying a filter() call until some terminals have been 111 * initialized. 112 */ 113NCURSES_EXPORT(void) 114NCURSES_SP_NAME(filter) (NCURSES_SP_DCL0) 115{ 116 START_TRACE(); 117 T((T_CALLED("filter(%p)"), (void *) SP_PARM)); 118#if NCURSES_SP_FUNCS 119 if (IsPreScreen(SP_PARM)) { 120 SP_PARM->_filtered = TRUE; 121 } 122#else 123 _nc_prescreen.filter_mode = TRUE; 124#endif 125 returnVoid; 126} 127 128#if NCURSES_SP_FUNCS 129NCURSES_EXPORT(void) 130filter(void) 131{ 132 START_TRACE(); 133 T((T_CALLED("filter()"))); 134 _nc_prescreen.filter_mode = TRUE; 135 returnVoid; 136} 137#endif 138 139#if NCURSES_EXT_FUNCS 140/* 141 * An extension, allowing the application to open a new screen without 142 * requiring it to also be filtered. 143 */ 144NCURSES_EXPORT(void) 145NCURSES_SP_NAME(nofilter) (NCURSES_SP_DCL0) 146{ 147 START_TRACE(); 148 T((T_CALLED("nofilter(%p)"), (void *) SP_PARM)); 149#if NCURSES_SP_FUNCS 150 if (IsPreScreen(SP_PARM)) { 151 SP_PARM->_filtered = FALSE; 152 } 153#else 154 _nc_prescreen.filter_mode = FALSE; 155#endif 156 returnVoid; 157} 158 159#if NCURSES_SP_FUNCS 160NCURSES_EXPORT(void) 161nofilter(void) 162{ 163 START_TRACE(); 164 T((T_CALLED("nofilter()"))); 165 _nc_prescreen.filter_mode = FALSE; 166 returnVoid; 167} 168#endif 169#endif /* NCURSES_EXT_FUNCS */ 170 171NCURSES_EXPORT(SCREEN *) 172NCURSES_SP_NAME(newterm) (NCURSES_SP_DCLx 173 const char *name, 174 FILE *ofp, 175 FILE *ifp) 176{ 177 int errret; 178 SCREEN *result = 0; 179 SCREEN *current; 180 TERMINAL *its_term; 181 FILE *_ofp = ofp ? ofp : stdout; 182 FILE *_ifp = ifp ? ifp : stdin; 183 TERMINAL *new_term = 0; 184 185 START_TRACE(); 186 T((T_CALLED("newterm(%p, \"%s\", %p,%p)"), 187 (void *) SP_PARM, 188 (name ? name : ""), 189 (void *) ofp, 190 (void *) ifp)); 191 192#if NCURSES_SP_FUNCS 193 assert(SP_PARM != 0); 194 if (SP_PARM == 0) 195 returnSP(SP_PARM); 196#endif 197 198 _nc_init_pthreads(); 199 _nc_lock_global(curses); 200 201 current = CURRENT_SCREEN; 202 its_term = (current ? current->_term : 0); 203 204#if defined(EXP_WIN32_DRIVER) 205 _setmode(fileno(_ifp), _O_BINARY); 206 _setmode(fileno(_ofp), _O_BINARY); 207#endif 208 209 INIT_TERM_DRIVER(); 210 /* this loads the capability entry, then sets LINES and COLS */ 211 if ( 212 TINFO_SETUP_TERM(&new_term, name, 213 fileno(_ofp), &errret, FALSE) != ERR) { 214 int slk_format; 215 int filter_mode; 216 217 _nc_set_screen(0); 218#ifdef USE_TERM_DRIVER 219 assert(new_term != 0); 220#endif 221 222#if NCURSES_SP_FUNCS 223 slk_format = SP_PARM->slk_format; 224 filter_mode = SP_PARM->_filtered; 225#else 226 slk_format = _nc_globals.slk_format; 227 filter_mode = _nc_prescreen.filter_mode; 228#endif 229 230 /* 231 * This actually allocates the screen structure, and saves the original 232 * terminal settings. 233 */ 234 if (NCURSES_SP_NAME(_nc_setupscreen) ( 235#if NCURSES_SP_FUNCS 236 &SP_PARM, 237#endif 238 *(ptrLines(SP_PARM)), 239 *(ptrCols(SP_PARM)), 240 _ofp, 241 filter_mode, 242 slk_format) == ERR) { 243 _nc_set_screen(current); 244 result = 0; 245 } else { 246 int value; 247 int cols; 248 249#ifdef USE_TERM_DRIVER 250 TERMINAL_CONTROL_BLOCK *TCB; 251#elif !NCURSES_SP_FUNCS 252 _nc_set_screen(CURRENT_SCREEN); 253#endif 254 assert(SP_PARM != 0); 255 cols = *(ptrCols(SP_PARM)); 256#ifdef USE_TERM_DRIVER 257 _nc_set_screen(SP_PARM); 258 TCB = (TERMINAL_CONTROL_BLOCK *) new_term; 259 TCB->csp = SP_PARM; 260#endif 261 /* 262 * In setupterm() we did a set_curterm(), but it was before we set 263 * CURRENT_SCREEN. So the "current" screen's terminal pointer was 264 * overwritten with a different terminal. Later, in 265 * _nc_setupscreen(), we set CURRENT_SCREEN and the terminal 266 * pointer in the new screen. 267 * 268 * Restore the terminal-pointer for the pre-existing screen, if 269 * any. 270 */ 271 if (current) 272 current->_term = its_term; 273 274#ifdef USE_TERM_DRIVER 275 SP_PARM->_term = new_term; 276#else 277 new_term = SP_PARM->_term; 278#endif 279 280 /* allow user to set maximum escape delay from the environment */ 281 if ((value = _nc_getenv_num("ESCDELAY")) >= 0) { 282#if NCURSES_EXT_FUNCS 283 NCURSES_SP_NAME(set_escdelay) (NCURSES_SP_ARGx value); 284#else 285 ESCDELAY = value; 286#endif 287 } 288 289 /* if the terminal type has real soft labels, set those up */ 290 if (slk_format && NumLabels > 0 && SLK_STDFMT(slk_format)) 291 _nc_slk_initialize(StdScreen(SP_PARM), cols); 292 293 SP_PARM->_ifd = fileno(_ifp); 294 NCURSES_SP_NAME(typeahead) (NCURSES_SP_ARGx fileno(_ifp)); 295#ifdef TERMIOS 296 SP_PARM->_use_meta = ((new_term->Ottyb.c_cflag & CSIZE) == CS8 && 297 !(new_term->Ottyb.c_iflag & ISTRIP)) || 298 USE_KLIBC_KBD; 299#else 300 SP_PARM->_use_meta = FALSE; 301#endif 302 SP_PARM->_endwin = ewInitial; 303#ifndef USE_TERM_DRIVER 304 /* 305 * Check whether we can optimize scrolling under dumb terminals in 306 * case we do not have any of these capabilities, scrolling 307 * optimization will be useless. 308 */ 309 SP_PARM->_scrolling = ((scroll_forward && scroll_reverse) || 310 ((parm_rindex || 311 parm_insert_line || 312 insert_line) && 313 (parm_index || 314 parm_delete_line || 315 delete_line))); 316#endif 317 318 NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG); /* sets a field in the screen structure */ 319 320 SP_PARM->_keytry = 0; 321 322 /* compute movement costs so we can do better move optimization */ 323#ifdef USE_TERM_DRIVER 324 TCBOf(SP_PARM)->drv->td_scinit(SP_PARM); 325#else /* ! USE_TERM_DRIVER */ 326 /* 327 * Check for mismatched graphic-rendition capabilities. Most SVr4 328 * terminfo trees contain entries that have rmul or rmso equated to 329 * sgr0 (Solaris curses copes with those entries). We do this only 330 * for curses, since many termcap applications assume that 331 * smso/rmso and smul/rmul are paired, and will not function 332 * properly if we remove rmso or rmul. Curses applications 333 * shouldn't be looking at this detail. 334 */ 335#define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode)) 336 SP_PARM->_use_rmso = SGR0_TEST(exit_standout_mode); 337 SP_PARM->_use_rmul = SGR0_TEST(exit_underline_mode); 338#if USE_ITALIC 339 SP_PARM->_use_ritm = SGR0_TEST(exit_italics_mode); 340#endif 341 342 /* compute movement costs so we can do better move optimization */ 343 _nc_mvcur_init(); 344 345 /* initialize terminal to a sane state */ 346 _nc_screen_init(); 347#endif /* USE_TERM_DRIVER */ 348 349 /* Initialize the terminal line settings. */ 350 _nc_initscr(NCURSES_SP_ARG); 351 352 _nc_signal_handler(TRUE); 353 result = SP_PARM; 354 } 355 } 356 _nc_unlock_global(curses); 357 returnSP(result); 358} 359 360#if NCURSES_SP_FUNCS 361NCURSES_EXPORT(SCREEN *) 362newterm(const char *name, FILE *ofp, FILE *ifp) 363{ 364 SCREEN *rc; 365 _nc_lock_global(prescreen); 366 START_TRACE(); 367 rc = NCURSES_SP_NAME(newterm) (CURRENT_SCREEN_PRE, name, ofp, ifp); 368 _nc_forget_prescr(); 369 _nc_unlock_global(prescreen); 370 return rc; 371} 372#endif 373