150276Speter/****************************************************************************
2178866Srafan * Copyright (c) 1998-2007,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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
3150276Speter *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32166124Srafan *     and: Thomas E. Dickey                        1996-2003               *
3350276Speter ****************************************************************************/
3450276Speter
3550276Speter/*
3650276Speter**	lib_initscr.c
3750276Speter**
3850276Speter**	The routines initscr(), and termname().
3950276Speter**
4050276Speter*/
4150276Speter
4250276Speter#include <curses.priv.h>
4350276Speter
4450276Speter#if HAVE_SYS_TERMIO_H
4576726Speter#include <sys/termio.h>		/* needed for ISC */
4650276Speter#endif
4750276Speter
48184989SrafanMODULE_ID("$Id: lib_initscr.c,v 1.38 2008/08/16 21:20:48 Werner.Fink Exp $")
4950276Speter
5076726SpeterNCURSES_EXPORT(WINDOW *)
5176726Speterinitscr(void)
5250276Speter{
53178866Srafan    WINDOW *result;
54178866Srafan
5576726Speter    NCURSES_CONST char *name;
5650276Speter
57166124Srafan    START_TRACE();
5876726Speter    T((T_CALLED("initscr()")));
59178866Srafan
60184989Srafan    _nc_init_pthreads();
61184989Srafan    _nc_lock_global(curses);
62184989Srafan
6376726Speter    /* Portable applications must not call initscr() more than once */
64174993Srafan    if (!_nc_globals.init_screen) {
65174993Srafan	_nc_globals.init_screen = TRUE;
6650276Speter
6776726Speter	if ((name = getenv("TERM")) == 0
6876726Speter	    || *name == '\0')
6976726Speter	    name = "unknown";
70166124Srafan#ifdef __CYGWIN__
71166124Srafan	/*
72166124Srafan	 * 2002/9/21
73166124Srafan	 * Work around a bug in Cygwin.  Full-screen subprocesses run from
74166124Srafan	 * bash, in turn spawned from another full-screen process, will dump
75166124Srafan	 * core when attempting to write to stdout.  Opening /dev/tty
76166124Srafan	 * explicitly seems to fix the problem.
77166124Srafan	 */
78166124Srafan	if (isatty(fileno(stdout))) {
79166124Srafan	    FILE *fp = fopen("/dev/tty", "w");
80166124Srafan	    if (fp != 0 && isatty(fileno(fp))) {
81166124Srafan		fclose(stdout);
82166124Srafan		dup2(fileno(fp), STDOUT_FILENO);
83166124Srafan		stdout = fdopen(STDOUT_FILENO, "w");
84166124Srafan	    }
85166124Srafan	}
86166124Srafan#endif
8776726Speter	if (newterm(name, stdout, stdin) == 0) {
8876726Speter	    fprintf(stderr, "Error opening terminal: %s.\n", name);
8976726Speter	    exit(EXIT_FAILURE);
9076726Speter	}
9150276Speter
9276726Speter	/* def_shell_mode - done in newterm/_nc_setupscreen */
9376726Speter	def_prog_mode();
9476726Speter    }
95178866Srafan    result = stdscr;
96184989Srafan    _nc_unlock_global(curses);
97178866Srafan
98178866Srafan    returnWin(result);
9950276Speter}
100