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