lib_tracebits.c revision 262629
150276Speter/****************************************************************************
2262629Sdelphij * Copyright (c) 1998-2008,2011 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
3050276Speter *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
3150276Speter *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32166124Srafan *     and: Thomas E. Dickey                        1996-on                 *
3350276Speter ****************************************************************************/
3450276Speter
3550276Speter#include <curses.priv.h>
3650276Speter
37262629SdelphijMODULE_ID("$Id: lib_tracebits.c,v 1.19 2011/01/09 00:23:03 tom Exp $")
3850276Speter
3966963Speter#if SVR4_TERMIO && !defined(_POSIX_SOURCE)
4050276Speter#define _POSIX_SOURCE
4150276Speter#endif
4250276Speter
4350276Speter#if HAVE_SYS_TERMIO_H
4462449Speter#include <sys/termio.h>		/* needed for ISC */
4550276Speter#endif
4650276Speter
4750276Speter#ifdef __EMX__
4850276Speter#include <io.h>
4950276Speter#endif
5050276Speter
5150276Speter/* may be undefined if we're using termio.h */
5250276Speter#ifndef TOSTOP
5350276Speter#define TOSTOP 0
5450276Speter#endif
55174993Srafan
5650276Speter#ifndef IEXTEN
5750276Speter#define IEXTEN 0
5850276Speter#endif
5950276Speter
60174993Srafan#ifndef ONLCR
61174993Srafan#define ONLCR 0
62174993Srafan#endif
63174993Srafan
64174993Srafan#ifndef OCRNL
65174993Srafan#define OCRNL 0
66174993Srafan#endif
67174993Srafan
68174993Srafan#ifndef ONOCR
69174993Srafan#define ONOCR 0
70174993Srafan#endif
71174993Srafan
72174993Srafan#ifndef ONLRET
73174993Srafan#define ONLRET 0
74174993Srafan#endif
75174993Srafan
7650276Speter#ifdef TRACE
7750276Speter
7862449Spetertypedef struct {
7962449Speter    unsigned int val;
8062449Speter    const char *name;
8162449Speter} BITNAMES;
8250276Speter
8362449Speterstatic void
8462449Speterlookup_bits(char *buf, const BITNAMES * table, const char *label, unsigned int val)
8550276Speter{
8662449Speter    const BITNAMES *sp;
8750276Speter
8862449Speter    (void) strcat(buf, label);
8962449Speter    (void) strcat(buf, ": {");
9062449Speter    for (sp = table; sp->name; sp++)
9162449Speter	if (sp->val != 0
9262449Speter	    && (val & sp->val) == sp->val) {
9362449Speter	    (void) strcat(buf, sp->name);
9462449Speter	    (void) strcat(buf, ", ");
9562449Speter	}
9662449Speter    if (buf[strlen(buf) - 2] == ',')
9762449Speter	buf[strlen(buf) - 2] = '\0';
9862449Speter    (void) strcat(buf, "} ");
9950276Speter}
10050276Speter
10176726SpeterNCURSES_EXPORT(char *)
10298503Speter_nc_trace_ttymode(TTY * tty)
10350276Speter/* describe the state of the terminal control bits exactly */
10450276Speter{
10562449Speter    char *buf;
10650276Speter
10750276Speter#ifdef TERMIOS
10862449Speter    static const BITNAMES iflags[] =
10950276Speter    {
11062449Speter	{BRKINT, "BRKINT"},
11162449Speter	{IGNBRK, "IGNBRK"},
11262449Speter	{IGNPAR, "IGNPAR"},
11362449Speter	{PARMRK, "PARMRK"},
11462449Speter	{INPCK, "INPCK"},
11562449Speter	{ISTRIP, "ISTRIP"},
11662449Speter	{INLCR, "INLCR"},
11762449Speter	{IGNCR, "IGNC"},
11862449Speter	{ICRNL, "ICRNL"},
11962449Speter	{IXON, "IXON"},
12062449Speter	{IXOFF, "IXOFF"},
12162449Speter	{0, NULL}
12250276Speter#define ALLIN	(BRKINT|IGNBRK|IGNPAR|PARMRK|INPCK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF)
12362449Speter    }, oflags[] =
12450276Speter    {
12562449Speter	{OPOST, "OPOST"},
126166124Srafan	{OFLAGS_TABS, "XTABS"},
127174993Srafan	{ONLCR, "ONLCR"},
128174993Srafan	{OCRNL, "OCRNL"},
129174993Srafan	{ONOCR, "ONOCR"},
130174993Srafan	{ONLRET, "ONLRET"},
13162449Speter	{0, NULL}
132174993Srafan#define ALLOUT	(OPOST|OFLAGS_TABS|ONLCR|OCRNL|ONOCR|ONLRET)
13362449Speter    }, cflags[] =
13450276Speter    {
13562449Speter	{CLOCAL, "CLOCAL"},
13662449Speter	{CREAD, "CREAD"},
13762449Speter	{CSTOPB, "CSTOPB"},
13850276Speter#if !defined(CS5) || !defined(CS8)
13962449Speter	{CSIZE, "CSIZE"},
14050276Speter#endif
14162449Speter	{HUPCL, "HUPCL"},
14262449Speter	{PARENB, "PARENB"},
14362449Speter	{PARODD | PARENB, "PARODD"},	/* concession to readability */
14462449Speter	{0, NULL}
14550276Speter#define ALLCTRL	(CLOCAL|CREAD|CSIZE|CSTOPB|HUPCL|PARENB|PARODD)
14662449Speter    }, lflags[] =
14750276Speter    {
14862449Speter	{ECHO, "ECHO"},
14962449Speter	{ECHOE | ECHO, "ECHOE"},	/* concession to readability */
15062449Speter	{ECHOK | ECHO, "ECHOK"},	/* concession to readability */
15162449Speter	{ECHONL, "ECHONL"},
15262449Speter	{ICANON, "ICANON"},
15362449Speter	{ISIG, "ISIG"},
15462449Speter	{NOFLSH, "NOFLSH"},
15562449Speter	{TOSTOP, "TOSTOP"},
15662449Speter	{IEXTEN, "IEXTEN"},
15762449Speter	{0, NULL}
15850276Speter#define ALLLOCAL	(ECHO|ECHONL|ICANON|ISIG|NOFLSH|TOSTOP|IEXTEN)
15950276Speter    };
16050276Speter
16150276Speter    buf = _nc_trace_buf(0,
16276726Speter			8 + sizeof(iflags) +
16376726Speter			8 + sizeof(oflags) +
16476726Speter			8 + sizeof(cflags) +
16576726Speter			8 + sizeof(lflags) +
16676726Speter			8);
167184989Srafan    if (buf != 0) {
16850276Speter
169184989Srafan	if (tty->c_iflag & ALLIN)
170184989Srafan	    lookup_bits(buf, iflags, "iflags", tty->c_iflag);
17150276Speter
172184989Srafan	if (tty->c_oflag & ALLOUT)
173184989Srafan	    lookup_bits(buf, oflags, "oflags", tty->c_oflag);
17450276Speter
175184989Srafan	if (tty->c_cflag & ALLCTRL)
176184989Srafan	    lookup_bits(buf, cflags, "cflags", tty->c_cflag);
17750276Speter
17850276Speter#if defined(CS5) && defined(CS8)
179184989Srafan	{
180184989Srafan	    static struct {
181184989Srafan		int value;
182184989Srafan		const char *name;
183184989Srafan	    } csizes[] = {
184184989Srafan#define CS_DATA(name) { name, #name " " }
185184989Srafan		CS_DATA(CS5),
18666963Speter#ifdef CS6
187184989Srafan		    CS_DATA(CS6),
18850276Speter#endif
18966963Speter#ifdef CS7
190184989Srafan		    CS_DATA(CS7),
19150276Speter#endif
192184989Srafan		    CS_DATA(CS8),
193184989Srafan	    };
194184989Srafan	    const char *result = "CSIZE? ";
195184989Srafan	    int value = (tty->c_cflag & CSIZE);
196184989Srafan	    unsigned n;
19766963Speter
198184989Srafan	    if (value != 0) {
199184989Srafan		for (n = 0; n < SIZEOF(csizes); n++) {
200184989Srafan		    if (csizes[n].value == value) {
201184989Srafan			result = csizes[n].name;
202184989Srafan			break;
203184989Srafan		    }
20466963Speter		}
20566963Speter	    }
206184989Srafan	    strcat(buf, result);
20766963Speter	}
20850276Speter#endif
20950276Speter
210184989Srafan	if (tty->c_lflag & ALLLOCAL)
211184989Srafan	    lookup_bits(buf, lflags, "lflags", tty->c_lflag);
212184989Srafan    }
21350276Speter#else
21450276Speter    /* reference: ttcompat(4M) on SunOS 4.1 */
21550276Speter#ifndef EVENP
21650276Speter#define EVENP 0
21750276Speter#endif
21850276Speter#ifndef LCASE
21950276Speter#define LCASE 0
22050276Speter#endif
22150276Speter#ifndef LLITOUT
22250276Speter#define LLITOUT 0
22350276Speter#endif
22450276Speter#ifndef ODDP
22550276Speter#define ODDP 0
22650276Speter#endif
22750276Speter#ifndef TANDEM
22850276Speter#define TANDEM 0
22950276Speter#endif
23050276Speter
23162449Speter    static const BITNAMES cflags[] =
23250276Speter    {
23362449Speter	{CBREAK, "CBREAK"},
23462449Speter	{CRMOD, "CRMOD"},
23562449Speter	{ECHO, "ECHO"},
23662449Speter	{EVENP, "EVENP"},
23762449Speter	{LCASE, "LCASE"},
23862449Speter	{LLITOUT, "LLITOUT"},
23962449Speter	{ODDP, "ODDP"},
24062449Speter	{RAW, "RAW"},
24162449Speter	{TANDEM, "TANDEM"},
24262449Speter	{XTABS, "XTABS"},
24362449Speter	{0, NULL}
24450276Speter#define ALLCTRL	(CBREAK|CRMOD|ECHO|EVENP|LCASE|LLITOUT|ODDP|RAW|TANDEM|XTABS)
24550276Speter    };
24650276Speter
24750276Speter    buf = _nc_trace_buf(0,
24876726Speter			8 + sizeof(cflags));
249184989Srafan    if (buf != 0) {
250184989Srafan	if (tty->sg_flags & ALLCTRL) {
251184989Srafan	    lookup_bits(buf, cflags, "cflags", tty->sg_flags);
252184989Srafan	}
25350276Speter    }
25450276Speter#endif
25562449Speter    return (buf);
25650276Speter}
25798503Speter
25898503SpeterNCURSES_EXPORT(char *)
25998503Speter_nc_tracebits(void)
26098503Speter{
26198503Speter    return _nc_trace_ttymode(&(cur_term->Nttyb));
26298503Speter}
26350276Speter#else
264262629SdelphijEMPTY_MODULE(_nc_empty_lib_tracebits)
26550276Speter#endif /* TRACE */
266