Back to index

tetex-bin  3.0
lib_raw.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00031  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00032  *     and: Thomas E. Dickey 1998 on                                        *
00033  ****************************************************************************/
00034 
00035 /*
00036  *     raw.c
00037  *
00038  *     Routines:
00039  *            raw()
00040  *            cbreak()
00041  *            noraw()
00042  *            nocbreak()
00043  *            qiflush()
00044  *            noqiflush()
00045  *            intrflush()
00046  *
00047  */
00048 
00049 #include <curses.priv.h>
00050 #include <term.h>           /* cur_term */
00051 
00052 MODULE_ID("$Id: lib_raw.c,v 1.13 2002/07/06 22:00:45 tom Exp $")
00053 
00054 #if SVR4_TERMIO && !defined(_POSIX_SOURCE)
00055 #define _POSIX_SOURCE
00056 #endif
00057 
00058 #if HAVE_SYS_TERMIO_H
00059 #include <sys/termio.h>            /* needed for ISC */
00060 #endif
00061 
00062 #ifdef __EMX__
00063 #include <io.h>
00064 #define _nc_setmode(mode) setmode(SP->_ifd, mode)
00065 #else
00066 #define _nc_setmode(mode)   /* nothing */
00067 #endif
00068 
00069 #define COOKED_INPUT (IXON|BRKINT|PARMRK)
00070 
00071 #ifdef TRACE
00072 #define BEFORE(N)    if (_nc_tracing&TRACE_BITS) _tracef("%s before bits: %s", N, _nc_tracebits())
00073 #define AFTER(N)     if (_nc_tracing&TRACE_BITS) _tracef("%s after bits: %s", N, _nc_tracebits())
00074 #else
00075 #define BEFORE(s)
00076 #define AFTER(s)
00077 #endif /* TRACE */
00078 
00079 NCURSES_EXPORT(int)
00080 raw(void)
00081 {
00082     int result = ERR;
00083 
00084     T((T_CALLED("raw()")));
00085 
00086     if (SP != 0 && cur_term != 0) {
00087        TTY buf;
00088 
00089        BEFORE("raw");
00090        _nc_setmode(O_BINARY);
00091 
00092        buf = cur_term->Nttyb;
00093 #ifdef TERMIOS
00094        buf.c_lflag &= ~(ICANON | ISIG | IEXTEN);
00095        buf.c_iflag &= ~(COOKED_INPUT);
00096        buf.c_cc[VMIN] = 1;
00097        buf.c_cc[VTIME] = 0;
00098 #else
00099        buf.sg_flags |= RAW;
00100 #endif
00101        if ((result = _nc_set_tty_mode(&buf)) == OK) {
00102            SP->_raw = TRUE;
00103            SP->_cbreak = 1;
00104            cur_term->Nttyb = buf;
00105        }
00106        AFTER("raw");
00107     }
00108     returnCode(result);
00109 }
00110 
00111 NCURSES_EXPORT(int)
00112 cbreak(void)
00113 {
00114     int result = ERR;
00115 
00116     T((T_CALLED("cbreak()")));
00117 
00118     if (SP != 0 && cur_term != 0) {
00119        TTY buf;
00120 
00121        BEFORE("cbreak");
00122        _nc_setmode(O_BINARY);
00123 
00124        buf = cur_term->Nttyb;
00125 #ifdef TERMIOS
00126        buf.c_lflag &= ~ICANON;
00127        buf.c_iflag &= ~ICRNL;
00128        buf.c_lflag |= ISIG;
00129        buf.c_cc[VMIN] = 1;
00130        buf.c_cc[VTIME] = 0;
00131 #else
00132        buf.sg_flags |= CBREAK;
00133 #endif
00134        if ((result = _nc_set_tty_mode(&buf)) == OK) {
00135            SP->_cbreak = 1;
00136            cur_term->Nttyb = buf;
00137        }
00138        AFTER("cbreak");
00139     }
00140     returnCode(result);
00141 }
00142 
00143 /*
00144  * Note:
00145  * this implementation may be wrong.  See the comment under intrflush().
00146  */
00147 NCURSES_EXPORT(void)
00148 qiflush(void)
00149 {
00150     int result = ERR;
00151 
00152     T((T_CALLED("qiflush()")));
00153 
00154     if (cur_term != 0) {
00155        TTY buf;
00156 
00157        BEFORE("qiflush");
00158        buf = cur_term->Nttyb;
00159 #ifdef TERMIOS
00160        buf.c_lflag &= ~(NOFLSH);
00161        result = _nc_set_tty_mode(&buf);
00162 #else
00163        /* FIXME */
00164 #endif
00165        if (result == OK)
00166            cur_term->Nttyb = buf;
00167        AFTER("qiflush");
00168     }
00169     returnVoid;
00170 }
00171 
00172 NCURSES_EXPORT(int)
00173 noraw(void)
00174 {
00175     int result = ERR;
00176 
00177     T((T_CALLED("noraw()")));
00178 
00179     if (SP != 0 && cur_term != 0) {
00180        TTY buf;
00181 
00182        BEFORE("noraw");
00183        _nc_setmode(O_TEXT);
00184 
00185        buf = cur_term->Nttyb;
00186 #ifdef TERMIOS
00187        buf.c_lflag |= ISIG | ICANON |
00188            (cur_term->Ottyb.c_lflag & IEXTEN);
00189        buf.c_iflag |= COOKED_INPUT;
00190 #else
00191        buf.sg_flags &= ~(RAW | CBREAK);
00192 #endif
00193        if ((result = _nc_set_tty_mode(&buf)) == OK) {
00194            SP->_raw = FALSE;
00195            SP->_cbreak = 0;
00196            cur_term->Nttyb = buf;
00197        }
00198        AFTER("noraw");
00199     }
00200     returnCode(result);
00201 }
00202 
00203 NCURSES_EXPORT(int)
00204 nocbreak(void)
00205 {
00206     int result = ERR;
00207 
00208     T((T_CALLED("nocbreak()")));
00209 
00210     if (SP != 0 && cur_term != 0) {
00211        TTY buf;
00212 
00213        BEFORE("nocbreak");
00214        _nc_setmode(O_TEXT);
00215 
00216        buf = cur_term->Nttyb;
00217 #ifdef TERMIOS
00218        buf.c_lflag |= ICANON;
00219        buf.c_iflag |= ICRNL;
00220 #else
00221        buf.sg_flags &= ~CBREAK;
00222 #endif
00223        if ((result = _nc_set_tty_mode(&buf)) == OK) {
00224            SP->_cbreak = 0;
00225            cur_term->Nttyb = buf;
00226        }
00227        AFTER("nocbreak");
00228     }
00229     returnCode(result);
00230 }
00231 
00232 /*
00233  * Note:
00234  * this implementation may be wrong.  See the comment under intrflush().
00235  */
00236 NCURSES_EXPORT(void)
00237 noqiflush(void)
00238 {
00239     int result = ERR;
00240 
00241     T((T_CALLED("noqiflush()")));
00242 
00243     if (cur_term != 0) {
00244        TTY buf;
00245 
00246        BEFORE("noqiflush");
00247        buf = cur_term->Nttyb;
00248 #ifdef TERMIOS
00249        buf.c_lflag |= NOFLSH;
00250        result = _nc_set_tty_mode(&buf);
00251 #else
00252        /* FIXME */
00253 #endif
00254        if (result == OK) {
00255            cur_term->Nttyb = buf;
00256        }
00257        AFTER("noqiflush");
00258     }
00259     returnVoid;
00260 }
00261 
00262 /*
00263  * This call does the same thing as the qiflush()/noqiflush() pair.  We know
00264  * for certain that SVr3 intrflush() tweaks the NOFLSH bit; on the other hand,
00265  * the match (in the SVr4 man pages) between the language describing NOFLSH in
00266  * termio(7) and the language describing qiflush()/noqiflush() in
00267  * curs_inopts(3x) is too exact to be coincidence.
00268  */
00269 NCURSES_EXPORT(int)
00270 intrflush(WINDOW *win GCC_UNUSED, bool flag)
00271 {
00272     int result = ERR;
00273 
00274     T((T_CALLED("intrflush(%d)"), flag));
00275 
00276     if (cur_term != 0) {
00277        TTY buf;
00278 
00279        BEFORE("intrflush");
00280        buf = cur_term->Nttyb;
00281 #ifdef TERMIOS
00282        if (flag)
00283            buf.c_lflag &= ~(NOFLSH);
00284        else
00285            buf.c_lflag |= (NOFLSH);
00286        result = _nc_set_tty_mode(&buf);
00287 #else
00288        /* FIXME */
00289 #endif
00290        if (result == OK) {
00291            cur_term->Nttyb = buf;
00292        }
00293        AFTER("intrflush");
00294     }
00295     returnCode(result);
00296 }