150276Speter/****************************************************************************
2184989Srafan * Copyright (c) 1998-2007,2008 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>
3662449Speter#include <term.h>		/* cur_term */
3750276Speter
38184989SrafanMODULE_ID("$Id: lib_tracebits.c,v 1.17 2008/08/03 16:09:26 tom Exp $")
3950276Speter
4066963Speter#if SVR4_TERMIO && !defined(_POSIX_SOURCE)
4150276Speter#define _POSIX_SOURCE
4250276Speter#endif
4350276Speter
4450276Speter#if HAVE_SYS_TERMIO_H
4562449Speter#include <sys/termio.h>		/* needed for ISC */
4650276Speter#endif
4750276Speter
4850276Speter#ifdef __EMX__
4950276Speter#include <io.h>
5050276Speter#endif
5150276Speter
5250276Speter/* may be undefined if we're using termio.h */
5350276Speter#ifndef TOSTOP
5450276Speter#define TOSTOP 0
5550276Speter#endif
56174993Srafan
5750276Speter#ifndef IEXTEN
5850276Speter#define IEXTEN 0
5950276Speter#endif
6050276Speter
61174993Srafan#ifndef ONLCR
62174993Srafan#define ONLCR 0
63174993Srafan#endif
64174993Srafan
65174993Srafan#ifndef OCRNL
66174993Srafan#define OCRNL 0
67174993Srafan#endif
68174993Srafan
69174993Srafan#ifndef ONOCR
70174993Srafan#define ONOCR 0
71174993Srafan#endif
72174993Srafan
73174993Srafan#ifndef ONLRET
74174993Srafan#define ONLRET 0
75174993Srafan#endif
76174993Srafan
7750276Speter#ifdef TRACE
7850276Speter
7962449Spetertypedef struct {
8062449Speter    unsigned int val;
8162449Speter    const char *name;
8262449Speter} BITNAMES;
8350276Speter
8462449Speterstatic void
8562449Speterlookup_bits(char *buf, const BITNAMES * table, const char *label, unsigned int val)
8650276Speter{
8762449Speter    const BITNAMES *sp;
8850276Speter
8962449Speter    (void) strcat(buf, label);
9062449Speter    (void) strcat(buf, ": {");
9162449Speter    for (sp = table; sp->name; sp++)
9262449Speter	if (sp->val != 0
9362449Speter	    && (val & sp->val) == sp->val) {
9462449Speter	    (void) strcat(buf, sp->name);
9562449Speter	    (void) strcat(buf, ", ");
9662449Speter	}
9762449Speter    if (buf[strlen(buf) - 2] == ',')
9862449Speter	buf[strlen(buf) - 2] = '\0';
9962449Speter    (void) strcat(buf, "} ");
10050276Speter}
10150276Speter
10276726SpeterNCURSES_EXPORT(char *)
10398503Speter_nc_trace_ttymode(TTY * tty)
10450276Speter/* describe the state of the terminal control bits exactly */
10550276Speter{
10662449Speter    char *buf;
10750276Speter
10850276Speter#ifdef TERMIOS
10962449Speter    static const BITNAMES iflags[] =
11050276Speter    {
11162449Speter	{BRKINT, "BRKINT"},
11262449Speter	{IGNBRK, "IGNBRK"},
11362449Speter	{IGNPAR, "IGNPAR"},
11462449Speter	{PARMRK, "PARMRK"},
11562449Speter	{INPCK, "INPCK"},
11662449Speter	{ISTRIP, "ISTRIP"},
11762449Speter	{INLCR, "INLCR"},
11862449Speter	{IGNCR, "IGNC"},
11962449Speter	{ICRNL, "ICRNL"},
12062449Speter	{IXON, "IXON"},
12162449Speter	{IXOFF, "IXOFF"},
12262449Speter	{0, NULL}
12350276Speter#define ALLIN	(BRKINT|IGNBRK|IGNPAR|PARMRK|INPCK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF)
12462449Speter    }, oflags[] =
12550276Speter    {
12662449Speter	{OPOST, "OPOST"},
127166124Srafan	{OFLAGS_TABS, "XTABS"},
128174993Srafan	{ONLCR, "ONLCR"},
129174993Srafan	{OCRNL, "OCRNL"},
130174993Srafan	{ONOCR, "ONOCR"},
131174993Srafan	{ONLRET, "ONLRET"},
13262449Speter	{0, NULL}
133174993Srafan#define ALLOUT	(OPOST|OFLAGS_TABS|ONLCR|OCRNL|ONOCR|ONLRET)
13462449Speter    }, cflags[] =
13550276Speter    {
13662449Speter	{CLOCAL, "CLOCAL"},
13762449Speter	{CREAD, "CREAD"},
13862449Speter	{CSTOPB, "CSTOPB"},
13950276Speter#if !defined(CS5) || !defined(CS8)
14062449Speter	{CSIZE, "CSIZE"},
14150276Speter#endif
14262449Speter	{HUPCL, "HUPCL"},
14362449Speter	{PARENB, "PARENB"},
14462449Speter	{PARODD | PARENB, "PARODD"},	/* concession to readability */
14562449Speter	{0, NULL}
14650276Speter#define ALLCTRL	(CLOCAL|CREAD|CSIZE|CSTOPB|HUPCL|PARENB|PARODD)
14762449Speter    }, lflags[] =
14850276Speter    {
14962449Speter	{ECHO, "ECHO"},
15062449Speter	{ECHOE | ECHO, "ECHOE"},	/* concession to readability */
15162449Speter	{ECHOK | ECHO, "ECHOK"},	/* concession to readability */
15262449Speter	{ECHONL, "ECHONL"},
15362449Speter	{ICANON, "ICANON"},
15462449Speter	{ISIG, "ISIG"},
15562449Speter	{NOFLSH, "NOFLSH"},
15662449Speter	{TOSTOP, "TOSTOP"},
15762449Speter	{IEXTEN, "IEXTEN"},
15862449Speter	{0, NULL}
15950276Speter#define ALLLOCAL	(ECHO|ECHONL|ICANON|ISIG|NOFLSH|TOSTOP|IEXTEN)
16050276Speter    };
16150276Speter
16250276Speter    buf = _nc_trace_buf(0,
16376726Speter			8 + sizeof(iflags) +
16476726Speter			8 + sizeof(oflags) +
16576726Speter			8 + sizeof(cflags) +
16676726Speter			8 + sizeof(lflags) +
16776726Speter			8);
168184989Srafan    if (buf != 0) {
16950276Speter
170184989Srafan	if (tty->c_iflag & ALLIN)
171184989Srafan	    lookup_bits(buf, iflags, "iflags", tty->c_iflag);
17250276Speter
173184989Srafan	if (tty->c_oflag & ALLOUT)
174184989Srafan	    lookup_bits(buf, oflags, "oflags", tty->c_oflag);
17550276Speter
176184989Srafan	if (tty->c_cflag & ALLCTRL)
177184989Srafan	    lookup_bits(buf, cflags, "cflags", tty->c_cflag);
17850276Speter
17950276Speter#if defined(CS5) && defined(CS8)
180184989Srafan	{
181184989Srafan	    static struct {
182184989Srafan		int value;
183184989Srafan		const char *name;
184184989Srafan	    } csizes[] = {
185184989Srafan#define CS_DATA(name) { name, #name " " }
186184989Srafan		CS_DATA(CS5),
18766963Speter#ifdef CS6
188184989Srafan		    CS_DATA(CS6),
18950276Speter#endif
19066963Speter#ifdef CS7
191184989Srafan		    CS_DATA(CS7),
19250276Speter#endif
193184989Srafan		    CS_DATA(CS8),
194184989Srafan	    };
195184989Srafan	    const char *result = "CSIZE? ";
196184989Srafan	    int value = (tty->c_cflag & CSIZE);
197184989Srafan	    unsigned n;
19866963Speter
199184989Srafan	    if (value != 0) {
200184989Srafan		for (n = 0; n < SIZEOF(csizes); n++) {
201184989Srafan		    if (csizes[n].value == value) {
202184989Srafan			result = csizes[n].name;
203184989Srafan			break;
204184989Srafan		    }
20566963Speter		}
20666963Speter	    }
207184989Srafan	    strcat(buf, result);
20866963Speter	}
20950276Speter#endif
21050276Speter
211184989Srafan	if (tty->c_lflag & ALLLOCAL)
212184989Srafan	    lookup_bits(buf, lflags, "lflags", tty->c_lflag);
213184989Srafan    }
21450276Speter#else
21550276Speter    /* reference: ttcompat(4M) on SunOS 4.1 */
21650276Speter#ifndef EVENP
21750276Speter#define EVENP 0
21850276Speter#endif
21950276Speter#ifndef LCASE
22050276Speter#define LCASE 0
22150276Speter#endif
22250276Speter#ifndef LLITOUT
22350276Speter#define LLITOUT 0
22450276Speter#endif
22550276Speter#ifndef ODDP
22650276Speter#define ODDP 0
22750276Speter#endif
22850276Speter#ifndef TANDEM
22950276Speter#define TANDEM 0
23050276Speter#endif
23150276Speter
23262449Speter    static const BITNAMES cflags[] =
23350276Speter    {
23462449Speter	{CBREAK, "CBREAK"},
23562449Speter	{CRMOD, "CRMOD"},
23662449Speter	{ECHO, "ECHO"},
23762449Speter	{EVENP, "EVENP"},
23862449Speter	{LCASE, "LCASE"},
23962449Speter	{LLITOUT, "LLITOUT"},
24062449Speter	{ODDP, "ODDP"},
24162449Speter	{RAW, "RAW"},
24262449Speter	{TANDEM, "TANDEM"},
24362449Speter	{XTABS, "XTABS"},
24462449Speter	{0, NULL}
24550276Speter#define ALLCTRL	(CBREAK|CRMOD|ECHO|EVENP|LCASE|LLITOUT|ODDP|RAW|TANDEM|XTABS)
24650276Speter    };
24750276Speter
24850276Speter    buf = _nc_trace_buf(0,
24976726Speter			8 + sizeof(cflags));
250184989Srafan    if (buf != 0) {
251184989Srafan	if (tty->sg_flags & ALLCTRL) {
252184989Srafan	    lookup_bits(buf, cflags, "cflags", tty->sg_flags);
253184989Srafan	}
25450276Speter    }
25550276Speter#endif
25662449Speter    return (buf);
25750276Speter}
25898503Speter
25998503SpeterNCURSES_EXPORT(char *)
26098503Speter_nc_tracebits(void)
26198503Speter{
26298503Speter    return _nc_trace_ttymode(&(cur_term->Nttyb));
26398503Speter}
26450276Speter#else
265184989SrafanEMPTY_MODULE(_nc_tracebits)
26650276Speter#endif /* TRACE */
267