]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_newterm.c
9a4919bf5292480e2c61f87df1d2d19d78062036
[ncurses.git] / ncurses / base / lib_newterm.c
1 /****************************************************************************
2  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33
34
35
36 /*
37 **      lib_newterm.c
38 **
39 **      The newterm() function.
40 **
41 */
42
43 #include <curses.priv.h>
44
45 #if defined(SVR4_TERMIO) && !defined(_POSIX_SOURCE)
46 #define _POSIX_SOURCE
47 #endif
48
49 #include <term.h>       /* clear_screen, cup & friends, cur_term */
50
51 MODULE_ID("$Id: lib_newterm.c,v 1.41 1999/07/24 20:07:48 tom Exp $")
52
53 #ifndef ONLCR           /* Allows compilation under the QNX 4.2 OS */
54 #define ONLCR 0
55 #endif
56
57 /*
58  * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
59  * restored during the curses session.  The library simulates echo in software.
60  * (The behavior is unspecified if the application enables hardware echo).
61  *
62  * The newterm function also initializes terminal settings, and since initscr
63  * is supposed to behave as if it calls newterm, we do it here.
64  */
65 static inline int _nc_initscr(void)
66 {
67         /* for extended XPG4 conformance requires cbreak() at this point */
68         /* (SVr4 curses does this anyway) */
69         cbreak();
70
71 #ifdef TERMIOS
72         cur_term->Nttyb.c_lflag &= ~(ECHO|ECHONL);
73         cur_term->Nttyb.c_iflag &= ~(ICRNL|INLCR|IGNCR);
74         cur_term->Nttyb.c_oflag &= ~(ONLCR);
75 #else
76         cur_term->Nttyb.sg_flags &= ~(ECHO|CRMOD);
77 #endif
78         return _nc_set_tty_mode(&cur_term->Nttyb);
79 }
80
81 /*
82  * filter() has to be called before either initscr() or newterm(), so there is
83  * apparently no way to make this flag apply to some terminals and not others,
84  * aside from possibly delaying a filter() call until some terminals have been
85  * initialized.
86  */
87 static int filter_mode = FALSE;
88
89 void filter(void)
90 {
91     filter_mode = TRUE;
92 }
93
94 SCREEN * newterm(NCURSES_CONST char *name, FILE *ofp, FILE *ifp)
95 {
96 int     errret;
97 int     slk_format = _nc_slk_format;
98 SCREEN* current;
99 #ifdef TRACE
100 int t = _nc_getenv_num("NCURSES_TRACE");
101
102         if (t >= 0)
103                trace(t);
104 #endif
105
106         T((T_CALLED("newterm(\"%s\",%p,%p)"), name, ofp, ifp));
107
108         /* this loads the capability entry, then sets LINES and COLS */
109         if (setupterm(name, fileno(ofp), &errret) == ERR)
110                 return 0;
111
112         /* implement filter mode */
113         if (filter_mode) {
114                 LINES = 1;
115
116                 if (init_tabs != -1)
117                         TABSIZE = init_tabs;
118                 else
119                         TABSIZE = 8;
120
121                 T(("TABSIZE = %d", TABSIZE));
122
123                 clear_screen = 0;
124                 cursor_down = parm_down_cursor = 0;
125                 cursor_address = 0;
126                 cursor_up = parm_up_cursor = 0;
127                 row_address = 0;
128
129                 cursor_home = carriage_return;
130         }
131
132         /* If we must simulate soft labels, grab off the line to be used.
133            We assume that we must simulate, if it is none of the standard
134            formats (4-4  or 3-2-3) for which there may be some hardware
135            support. */
136         if (num_labels <= 0 || !SLK_STDFMT(slk_format))
137             if (slk_format)
138               {
139                 if (ERR==_nc_ripoffline(-SLK_LINES(slk_format),
140                                         _nc_slk_initialize))
141                   return 0;
142               }
143         /* this actually allocates the screen structure, and saves the
144          * original terminal settings.
145          */
146         current = SP;
147         _nc_set_screen(0);
148         if (_nc_setupscreen(LINES, COLS, ofp) == ERR) {
149                 _nc_set_screen(current);
150                 return 0;
151         }
152
153         /* if the terminal type has real soft labels, set those up */
154         if (slk_format && num_labels > 0 && SLK_STDFMT(slk_format))
155             _nc_slk_initialize(stdscr, COLS);
156
157         SP->_ifd        = fileno(ifp);
158         SP->_checkfd    = fileno(ifp);
159         typeahead(fileno(ifp));
160 #ifdef TERMIOS
161         SP->_use_meta   = ((cur_term->Ottyb.c_cflag & CSIZE) == CS8 &&
162                             !(cur_term->Ottyb.c_iflag & ISTRIP));
163 #else
164         SP->_use_meta   = FALSE;
165 #endif
166         SP->_endwin     = FALSE;
167
168         /* Check whether we can optimize scrolling under dumb terminals in case
169          * we do not have any of these capabilities, scrolling optimization
170          * will be useless.
171          */
172         SP->_scrolling = ((scroll_forward && scroll_reverse) ||
173                           ((parm_rindex || parm_insert_line || insert_line) &&
174                            (parm_index  || parm_delete_line || delete_line)));
175
176         baudrate();     /* sets a field in the SP structure */
177
178         SP->_keytry = 0;
179
180         /*
181          * Check for mismatched graphic-rendition capabilities.  Most SVr4
182          * terminfo trees contain entries that have rmul or rmso equated to
183          * sgr0 (Solaris curses copes with those entries).  We do this only for
184          * curses, since many termcap applications assume that smso/rmso and
185          * smul/rmul are paired, and will not function properly if we remove
186          * rmso or rmul.  Curses applications shouldn't be looking at this
187          * detail.
188          */
189 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
190         SP->_use_rmso = SGR0_TEST(exit_standout_mode);
191         SP->_use_rmul = SGR0_TEST(exit_underline_mode);
192
193         /* compute movement costs so we can do better move optimization */
194         _nc_mvcur_init();
195
196         /* initialize terminal to a sane state */
197         _nc_screen_init();
198
199         /* Initialize the terminal line settings. */
200         _nc_initscr();
201
202         _nc_signal_handler(TRUE);
203
204         T((T_RETURN("%p"), SP));
205         return(SP);
206 }