]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/lib_initscr.c
3a995d22f662fcb2c3011a9e8c0d5220f69c5087
[ncurses.git] / ncurses / lib_initscr.c
1
2 /***************************************************************************
3 *                            COPYRIGHT NOTICE                              *
4 ****************************************************************************
5 *                ncurses is copyright (C) 1992-1995                        *
6 *                          Zeyd M. Ben-Halim                               *
7 *                          zmbenhal@netcom.com                             *
8 *                          Eric S. Raymond                                 *
9 *                          esr@snark.thyrsus.com                           *
10 *                                                                          *
11 *        Permission is hereby granted to reproduce and distribute ncurses  *
12 *        by any means and for any fee, whether alone or as part of a       *
13 *        larger distribution, in source or in binary form, PROVIDED        *
14 *        this notice is included with any such distribution, and is not    *
15 *        removed from any of its header files. Mention of ncurses in any   *
16 *        applications linked with it is highly appreciated.                *
17 *                                                                          *
18 *        ncurses comes AS IS with no warranty, implied or expressed.       *
19 *                                                                          *
20 ***************************************************************************/
21
22 /*
23 **      lib_initscr.c
24 **
25 **      The routines initscr(), and termname().
26 **
27 */
28
29 #include <curses.priv.h>
30 #include <term.h>       /* cur_term */
31
32 #if HAVE_SYS_TERMIO_H
33 #include <sys/termio.h> /* needed for ISC */
34 #endif
35
36 MODULE_ID("$Id: lib_initscr.c,v 1.18 1997/03/08 14:03:59 tom Exp $")
37
38 #ifndef ONLCR           /* Allows compilation under the QNX 4.2 OS */
39 #define ONLCR 0
40 #endif
41
42 /*
43  * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
44  * restored during the curses session.  The library simulates echo in software.
45  * (The behavior is unspecified if the application enables hardware echo).
46  *
47  * The newterm function also initializes terminal settings.
48  */
49 int _nc_initscr(void)
50 {
51         /* for extended XPG4 conformance requires cbreak() at this point */
52         /* (SVr4 curses does this anyway) */
53         cbreak();
54
55 #ifdef TERMIOS
56         cur_term->Nttyb.c_lflag &= ~(ECHO|ECHONL);
57         cur_term->Nttyb.c_iflag &= ~(ICRNL|INLCR|IGNCR);
58         cur_term->Nttyb.c_oflag &= ~(ONLCR);
59 #else
60         cur_term->Nttyb.sg_flags &= ~(ECHO|CRMOD);
61 #endif
62         if ((SET_TTY(cur_term->Filedes, &cur_term->Nttyb)) == -1)
63                 return ERR;
64         return OK;
65 }
66
67 WINDOW *initscr(void)
68 {
69 static  bool initialized = FALSE;
70 const char *name;
71
72         T((T_CALLED("initscr()")));
73         /* Portable applications must not call initscr() more than once */
74         if (!initialized) {
75                 initialized = TRUE;
76
77                 if ((name = getenv("TERM")) == 0)
78                         name = "unknown";
79                 if (newterm(name, stdout, stdin) == 0) {
80                         fprintf(stderr, "Error opening terminal: %s.\n", name);
81                         exit(EXIT_FAILURE);
82                 }
83
84                 /* allow user to set maximum escape delay from the environment */
85                 if ((name = getenv("ESCDELAY")) != 0)
86                         ESCDELAY = atoi(getenv("ESCDELAY"));
87
88                 /* def_shell_mode - done in newterm/_nc_setupscreen */
89                 def_prog_mode();
90         }
91         returnWin(stdscr);
92 }
93
94 char *termname(void)
95 {
96 char    *term = getenv("TERM");
97 static char     ret[15];
98
99         T(("termname() called"));
100
101         if (term != 0) {
102                 (void) strncpy(ret, term, sizeof(ret) - 1);
103                 term = ret;
104         }
105         return term;
106 }