10e3d5408SPeter Wemm /****************************************************************************
2aa59d4d4SRong-En Fan  * Copyright 2019,2020 Thomas E. Dickey                                     *
30e3d5408SPeter Wemm  * Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
40e3d5408SPeter Wemm  *                                                                          *
50e3d5408SPeter Wemm  * Permission is hereby granted, free of charge, to any person obtaining a  *
60e3d5408SPeter Wemm  * copy of this software and associated documentation files (the            *
70e3d5408SPeter Wemm  * "Software"), to deal in the Software without restriction, including      *
80e3d5408SPeter Wemm  * without limitation the rights to use, copy, modify, merge, publish,      *
90e3d5408SPeter Wemm  * distribute, distribute with modifications, sublicense, and/or sell       *
100e3d5408SPeter Wemm  * copies of the Software, and to permit persons to whom the Software is    *
110e3d5408SPeter Wemm  * furnished to do so, subject to the following conditions:                 *
120e3d5408SPeter Wemm  *                                                                          *
130e3d5408SPeter Wemm  * The above copyright notice and this permission notice shall be included  *
140e3d5408SPeter Wemm  * in all copies or substantial portions of the Software.                   *
150e3d5408SPeter Wemm  *                                                                          *
160e3d5408SPeter Wemm  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
170e3d5408SPeter Wemm  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
180e3d5408SPeter Wemm  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
190e3d5408SPeter Wemm  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
200e3d5408SPeter Wemm  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
210e3d5408SPeter Wemm  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
220e3d5408SPeter Wemm  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
230e3d5408SPeter Wemm  *                                                                          *
240e3d5408SPeter Wemm  * Except as contained in this notice, the name(s) of the above copyright   *
250e3d5408SPeter Wemm  * holders shall not be used in advertising or otherwise to promote the     *
260e3d5408SPeter Wemm  * sale, use or other dealings in this Software without prior written       *
270e3d5408SPeter Wemm  * authorization.                                                           *
280e3d5408SPeter Wemm  ****************************************************************************/
290e3d5408SPeter Wemm 
300e3d5408SPeter Wemm /****************************************************************************
310e3d5408SPeter Wemm  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
324a1a9510SRong-En Fan  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
330e3d5408SPeter Wemm  *     and: Thomas E. Dickey                        1996-on                 *
340e3d5408SPeter Wemm  ****************************************************************************/
350e3d5408SPeter Wemm 
360e3d5408SPeter Wemm /*
370e3d5408SPeter Wemm **	lib_initscr.c
380e3d5408SPeter Wemm **
390e3d5408SPeter Wemm **	The routines initscr(), and termname().
400e3d5408SPeter Wemm **
410e3d5408SPeter Wemm */
420e3d5408SPeter Wemm 
430e3d5408SPeter Wemm #include <curses.priv.h>
440e3d5408SPeter Wemm 
450e3d5408SPeter Wemm #if HAVE_SYS_TERMIO_H
460e3d5408SPeter Wemm #include <sys/termio.h>		/* needed for ISC */
470e3d5408SPeter Wemm #endif
48aa59d4d4SRong-En Fan 
490e3d5408SPeter Wemm MODULE_ID("$Id: lib_initscr.c,v 1.48 2020/09/07 14:26:48 tom Exp $")
507a69bbfbSPeter Wemm 
NCURSES_EXPORT(WINDOW *)517a69bbfbSPeter Wemm NCURSES_EXPORT(WINDOW *)
520e3d5408SPeter Wemm initscr(void)
53aa59d4d4SRong-En Fan {
54aa59d4d4SRong-En Fan     WINDOW *result;
550e3d5408SPeter Wemm 
5639f2269fSPeter Wemm     START_TRACE();
574a1a9510SRong-En Fan     T((T_CALLED("initscr()")));
580e3d5408SPeter Wemm 
59aa59d4d4SRong-En Fan     _nc_init_pthreads();
60aa59d4d4SRong-En Fan     _nc_lock_global(curses);
610e3d5408SPeter Wemm 
625ca44d1cSRong-En Fan     /* Portable applications must not call initscr() more than once */
635ca44d1cSRong-En Fan     if (!_nc_globals.init_screen) {
640e3d5408SPeter Wemm 	const char *env;
650e3d5408SPeter Wemm 	char *name;
660e3d5408SPeter Wemm 
670e3d5408SPeter Wemm 	_nc_globals.init_screen = TRUE;
684a1a9510SRong-En Fan 
694a1a9510SRong-En Fan 	env = getenv("TERM");
704a1a9510SRong-En Fan 	(void) VALID_TERM_ENV(env, "unknown");
714a1a9510SRong-En Fan 
724a1a9510SRong-En Fan 	if ((name = strdup(env)) == NULL) {
734a1a9510SRong-En Fan 	    fprintf(stderr, "Error opening allocating $TERM.\n");
744a1a9510SRong-En Fan 	    ExitProgram(EXIT_FAILURE);
754a1a9510SRong-En Fan 	}
764a1a9510SRong-En Fan #ifdef __CYGWIN__
774a1a9510SRong-En Fan 	/*
784a1a9510SRong-En Fan 	 * 2002/9/21
794a1a9510SRong-En Fan 	 * Work around a bug in Cygwin.  Full-screen subprocesses run from
804a1a9510SRong-En Fan 	 * bash, in turn spawned from another full-screen process, will dump
814a1a9510SRong-En Fan 	 * core when attempting to write to stdout.  Opening /dev/tty
824a1a9510SRong-En Fan 	 * explicitly seems to fix the problem.
834a1a9510SRong-En Fan 	 */
844a1a9510SRong-En Fan 	if (NC_ISATTY(fileno(stdout))) {
850e3d5408SPeter Wemm 	    FILE *fp = fopen("/dev/tty", "w");
860e3d5408SPeter Wemm 	    if (fp != 0 && NC_ISATTY(fileno(fp))) {
870e3d5408SPeter Wemm 		fclose(stdout);
880e3d5408SPeter Wemm 		dup2(fileno(fp), STDOUT_FILENO);
890e3d5408SPeter Wemm 		stdout = fdopen(STDOUT_FILENO, "w");
900e3d5408SPeter Wemm 	    }
910e3d5408SPeter Wemm 	}
920e3d5408SPeter Wemm #endif
93aa59d4d4SRong-En Fan 	if (newterm(name, stdout, stdin) == 0) {
94aa59d4d4SRong-En Fan 	    fprintf(stderr, "Error opening terminal: %s.\n", name);
95aa59d4d4SRong-En Fan 	    ExitProgram(EXIT_FAILURE);
96aa59d4d4SRong-En Fan 	}
970e3d5408SPeter Wemm 
98 	/* def_shell_mode - done in newterm/_nc_setupscreen */
99 #if NCURSES_SP_FUNCS
100 	NCURSES_SP_NAME(def_prog_mode) (CURRENT_SCREEN);
101 #else
102 	def_prog_mode();
103 #endif
104 	free(name);
105     }
106     result = stdscr;
107     _nc_unlock_global(curses);
108 
109     returnWin(result);
110 }
111