ncurses 4.2
[ncurses.git] / ncurses / lib_raw.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  *      raw.c
37  *
38  *      Routines:
39  *              raw()
40  *              cbreak()
41  *              noraw()
42  *              nocbreak()
43  *              qiflush()
44  *              noqiflush()
45  *              intrflush()
46  *
47  */
48
49 #include <curses.priv.h>
50 #include <term.h>       /* cur_term */
51
52 MODULE_ID("$Id: lib_raw.c,v 1.25 1998/02/11 12:13:59 tom Exp $")
53
54 #if defined(SVR4_TERMIO) && !defined(_POSIX_SOURCE)
55 #define _POSIX_SOURCE
56 #endif
57
58 #if HAVE_SYS_TERMIO_H
59 #include <sys/termio.h> /* needed for ISC */
60 #endif
61
62 #ifdef __EMX__
63 #include <io.h>
64 #include <fcntl.h>
65 #endif
66
67 /* may be undefined if we're using termio.h */
68 #ifndef TOSTOP
69 #define TOSTOP 0
70 #endif
71 #ifndef IEXTEN
72 #define IEXTEN 0
73 #endif
74
75 #define COOKED_INPUT    (IXON|BRKINT|PARMRK)
76
77 #ifdef TRACE
78
79 typedef struct {unsigned int val; const char *name;} BITNAMES;
80
81 static void lookup_bits(char *buf, const BITNAMES *table, const char *label, unsigned int val)
82 {
83         const BITNAMES *sp;
84
85         (void) strcat(buf, label);
86         (void) strcat(buf, ": {");
87         for (sp = table; sp->name; sp++)
88                 if (sp->val != 0
89                 && (val & sp->val) == sp->val)
90                 {
91                         (void) strcat(buf, sp->name);
92                         (void) strcat(buf, ", ");
93                 }
94         if (buf[strlen(buf) - 2] == ',')
95                 buf[strlen(buf) - 2] = '\0';
96         (void) strcat(buf,"} ");
97 }
98
99 char *_tracebits(void)
100 /* describe the state of the terminal control bits exactly */
101 {
102 char    *buf;
103 static const    BITNAMES
104
105 #ifdef TERMIOS
106 iflags[] =
107     {
108         {BRKINT,        "BRKINT"},
109         {IGNBRK,        "IGNBRK"},
110         {IGNPAR,        "IGNPAR"},
111         {PARMRK,        "PARMRK"},
112         {INPCK,         "INPCK"},
113         {ISTRIP,        "ISTRIP"},
114         {INLCR,         "INLCR"},
115         {IGNCR,         "IGNC"},
116         {ICRNL,         "ICRNL"},
117         {IXON,          "IXON"},
118         {IXOFF,         "IXOFF"},
119         {0,             NULL}
120 #define ALLIN   (BRKINT|IGNBRK|IGNPAR|PARMRK|INPCK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF)
121     },
122 oflags[] =
123     {
124         {OPOST,         "OPOST"},
125         {0,             NULL}
126 #define ALLOUT  (OPOST)
127     },
128 cflags[] =
129     {
130         {CLOCAL,        "CLOCAL"},
131         {CREAD,         "CREAD"},
132         {CSTOPB,        "CSTOPB"},
133 #if !defined(CS5) || !defined(CS8)
134         {CSIZE,         "CSIZE"},
135 #endif
136         {HUPCL,         "HUPCL"},
137         {PARENB,        "PARENB"},
138         {PARODD|PARENB, "PARODD"},      /* concession to readability */
139         {0,             NULL}
140 #define ALLCTRL (CLOCAL|CREAD|CSIZE|CSTOPB|HUPCL|PARENB|PARODD)
141     },
142 lflags[] =
143     {
144         {ECHO,          "ECHO"},
145         {ECHOE|ECHO,    "ECHOE"},       /* concession to readability */
146         {ECHOK|ECHO,    "ECHOK"},       /* concession to readability */
147         {ECHONL,        "ECHONL"},
148         {ICANON,        "ICANON"},
149         {ISIG,          "ISIG"},
150         {NOFLSH,        "NOFLSH"},
151         {TOSTOP,        "TOSTOP"},
152         {IEXTEN,        "IEXTEN"},
153         {0,             NULL}
154 #define ALLLOCAL        (ECHO|ECHONL|ICANON|ISIG|NOFLSH|TOSTOP|IEXTEN)
155     };
156
157
158     buf = _nc_trace_buf(0,
159         8 + sizeof(iflags) +
160         8 + sizeof(oflags) +
161         8 + sizeof(cflags) +
162         8 + sizeof(lflags) +
163         8);
164
165     if (cur_term->Nttyb.c_iflag & ALLIN)
166         lookup_bits(buf, iflags, "iflags", cur_term->Nttyb.c_iflag);
167
168     if (cur_term->Nttyb.c_oflag & ALLOUT)
169         lookup_bits(buf, oflags, "oflags", cur_term->Nttyb.c_oflag);
170
171     if (cur_term->Nttyb.c_cflag & ALLCTRL)
172         lookup_bits(buf, cflags, "cflags", cur_term->Nttyb.c_cflag);
173
174 #if defined(CS5) && defined(CS8)
175     switch (cur_term->Nttyb.c_cflag & CSIZE) {
176     case CS5:   strcat(buf, "CS5 ");    break;
177     case CS6:   strcat(buf, "CS6 ");    break;
178     case CS7:   strcat(buf, "CS7 ");    break;
179     case CS8:   strcat(buf, "CS8 ");    break;
180     default:    strcat(buf, "CSIZE? "); break;
181     }
182 #endif
183
184     if (cur_term->Nttyb.c_lflag & ALLLOCAL)
185         lookup_bits(buf, lflags, "lflags", cur_term->Nttyb.c_lflag);
186
187 #else
188     /* reference: ttcompat(4M) on SunOS 4.1 */
189 #ifndef EVENP
190 #define EVENP 0
191 #endif
192 #ifndef LCASE
193 #define LCASE 0
194 #endif
195 #ifndef LLITOUT
196 #define LLITOUT 0
197 #endif
198 #ifndef ODDP
199 #define ODDP 0
200 #endif
201 #ifndef TANDEM
202 #define TANDEM 0
203 #endif
204
205 cflags[] =
206     {
207         {CBREAK,        "CBREAK"},
208         {CRMOD,         "CRMOD"},
209         {ECHO,          "ECHO"},
210         {EVENP,         "EVENP"},
211         {LCASE,         "LCASE"},
212         {LLITOUT,       "LLITOUT"},
213         {ODDP,          "ODDP"},
214         {RAW,           "RAW"},
215         {TANDEM,        "TANDEM"},
216         {XTABS,         "XTABS"},
217         {0,             NULL}
218 #define ALLCTRL (CBREAK|CRMOD|ECHO|EVENP|LCASE|LLITOUT|ODDP|RAW|TANDEM|XTABS)
219     };
220
221     buf = _nc_trace_buf(0,
222         8 + sizeof(cflags));
223
224     if (cur_term->Nttyb.sg_flags & ALLCTRL)
225     {
226         lookup_bits(buf, cflags, "cflags", cur_term->Nttyb.sg_flags);
227     }
228
229 #endif
230     return(buf);
231 }
232
233 #define BEFORE(N)       if (_nc_tracing&TRACE_BITS) _tracef("%s before bits: %s", N, _tracebits())
234 #define AFTER(N)        if (_nc_tracing&TRACE_BITS) _tracef("%s after bits: %s", N, _tracebits())
235 #else
236 #define BEFORE(s)
237 #define AFTER(s)
238 #endif /* TRACE */
239
240 int raw(void)
241 {
242         T((T_CALLED("raw()")));
243         if (SP != 0 && cur_term != 0) {
244
245                 SP->_raw = TRUE;
246                 SP->_cbreak = TRUE;
247
248 #ifdef __EMX__
249                 setmode(SP->_ifd, O_BINARY);
250 #endif
251
252 #ifdef TERMIOS
253                 BEFORE("raw");
254                 cur_term->Nttyb.c_lflag &= ~(ICANON|ISIG);
255                 cur_term->Nttyb.c_iflag &= ~(COOKED_INPUT);
256                 cur_term->Nttyb.c_cc[VMIN] = 1;
257                 cur_term->Nttyb.c_cc[VTIME] = 0;
258                 AFTER("raw");
259 #else
260                 cur_term->Nttyb.sg_flags |= RAW;
261 #endif
262                 returnCode(_nc_set_curterm(&cur_term->Nttyb));
263         }
264         returnCode(ERR);
265 }
266
267 int cbreak(void)
268 {
269         T((T_CALLED("cbreak()")));
270
271         SP->_cbreak = TRUE;
272
273 #ifdef __EMX__
274         setmode(SP->_ifd, O_BINARY);
275 #endif
276
277 #ifdef TERMIOS
278         BEFORE("cbreak");
279         cur_term->Nttyb.c_lflag &= ~ICANON;
280         cur_term->Nttyb.c_iflag &= ~ICRNL;
281         cur_term->Nttyb.c_lflag |= ISIG;
282         cur_term->Nttyb.c_cc[VMIN] = 1;
283         cur_term->Nttyb.c_cc[VTIME] = 0;
284         AFTER("cbreak");
285 #else
286         cur_term->Nttyb.sg_flags |= CBREAK;
287 #endif
288         returnCode(_nc_set_curterm( &cur_term->Nttyb));
289 }
290
291 int qiflush(void)
292 {
293         T((T_CALLED("qiflush()")));
294
295         /*
296          * Note: this implementation may be wrong.  See the comment under
297          * intrflush().
298          */
299
300 #ifdef TERMIOS
301         BEFORE("qiflush");
302         cur_term->Nttyb.c_lflag &= ~(NOFLSH);
303         AFTER("qiflush");
304         returnCode(_nc_set_curterm( &cur_term->Nttyb));
305 #else
306         returnCode(ERR);
307 #endif
308 }
309
310
311 int noraw(void)
312 {
313         T((T_CALLED("noraw()")));
314
315         SP->_raw = FALSE;
316         SP->_cbreak = FALSE;
317
318 #ifdef __EMX__
319         setmode(SP->_ifd, O_TEXT);
320 #endif
321
322 #ifdef TERMIOS
323         BEFORE("noraw");
324         cur_term->Nttyb.c_lflag |= ISIG|ICANON;
325         cur_term->Nttyb.c_iflag |= COOKED_INPUT;
326         AFTER("noraw");
327 #else
328         cur_term->Nttyb.sg_flags &= ~(RAW|CBREAK);
329 #endif
330         returnCode(_nc_set_curterm( &cur_term->Nttyb));
331 }
332
333
334 int nocbreak(void)
335 {
336         T((T_CALLED("nocbreak()")));
337
338         SP->_cbreak = FALSE;
339
340 #ifdef __EMX__
341         setmode(SP->_ifd, O_TEXT);
342 #endif
343
344 #ifdef TERMIOS
345         BEFORE("nocbreak");
346         cur_term->Nttyb.c_lflag |= ICANON;
347         cur_term->Nttyb.c_iflag |= ICRNL;
348         AFTER("nocbreak");
349 #else
350         cur_term->Nttyb.sg_flags &= ~CBREAK;
351 #endif
352         returnCode(_nc_set_curterm( &cur_term->Nttyb));
353 }
354
355 int noqiflush(void)
356 {
357         T((T_CALLED("noqiflush()")));
358
359         /*
360          * Note: this implementation may be wrong.  See the comment under
361          * intrflush().
362          */
363
364 #ifdef TERMIOS
365         BEFORE("noqiflush");
366         cur_term->Nttyb.c_lflag |= NOFLSH;
367         AFTER("noqiflush");
368         returnCode(_nc_set_curterm( &cur_term->Nttyb));
369 #else
370         returnCode(ERR);
371 #endif
372 }
373
374 int intrflush(WINDOW *win GCC_UNUSED, bool flag)
375 {
376         T((T_CALLED("intrflush(%d)"), flag));
377
378         /*
379          * This call does the same thing as the qiflush()/noqiflush()
380          * pair.  We know for certain that SVr3 intrflush() tweaks the
381          * NOFLSH bit; on the other hand, the match (in the SVr4 man
382          * pages) between the language describing NOFLSH in termio(7)
383          * and the language describing qiflush()/noqiflush() in
384          * curs_inopts(3x) is too exact to be coincidence.
385          */
386
387 #ifdef TERMIOS
388         BEFORE("intrflush");
389         if (flag)
390                 cur_term->Nttyb.c_lflag &= ~(NOFLSH);
391         else
392                 cur_term->Nttyb.c_lflag |= (NOFLSH);
393         AFTER("intrflush");
394         returnCode(_nc_set_curterm( &cur_term->Nttyb));
395 #else
396         returnCode(ERR);
397 #endif
398 }