]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/trace/lib_tracebits.c
ncurses 6.2 - patch 20200829
[ncurses.git] / ncurses / trace / lib_tracebits.c
index 6dbb2f793ad0d1bbea81ace51c09edc9d110c0a7..14e5d90bd0972230c6a51d399f2db5fe263ec9d8 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
+ * Copyright 2019,2020 Thomas E. Dickey                                     *
+ * Copyright 1998-2012,2015 Free Software Foundation, Inc.                  *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
 /****************************************************************************
  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
+ *     and: Thomas E. Dickey                        1996-on                 *
  ****************************************************************************/
 
 #include <curses.priv.h>
-#include <term.h>              /* cur_term */
 
-MODULE_ID("$Id: lib_tracebits.c,v 1.5 2000/02/13 01:01:55 tom Exp $")
-
-#if defined(SVR4_TERMIO) && !defined(_POSIX_SOURCE)
-#define _POSIX_SOURCE
-#endif
+MODULE_ID("$Id: lib_tracebits.c,v 1.28 2020/02/02 23:34:34 tom Exp $")
 
 #if HAVE_SYS_TERMIO_H
 #include <sys/termio.h>                /* needed for ISC */
@@ -52,137 +49,169 @@ MODULE_ID("$Id: lib_tracebits.c,v 1.5 2000/02/13 01:01:55 tom Exp $")
 #ifndef TOSTOP
 #define TOSTOP 0
 #endif
+
 #ifndef IEXTEN
 #define IEXTEN 0
 #endif
 
+#ifndef ONLCR
+#define ONLCR 0
+#endif
+
+#ifndef OCRNL
+#define OCRNL 0
+#endif
+
+#ifndef ONOCR
+#define ONOCR 0
+#endif
+
+#ifndef ONLRET
+#define ONLRET 0
+#endif
+
 #ifdef TRACE
 
 typedef struct {
     unsigned int val;
-    const char *name;
+    const char name[8];
 } BITNAMES;
 
+#define TRACE_BUF_SIZE(num) (_nc_globals.tracebuf_ptr[num].size)
+
 static void
 lookup_bits(char *buf, const BITNAMES * table, const char *label, unsigned int val)
 {
     const BITNAMES *sp;
 
-    (void) strcat(buf, label);
-    (void) strcat(buf, ": {");
-    for (sp = table; sp->name; sp++)
+    _nc_STRCAT(buf, label, TRACE_BUF_SIZE(0));
+    _nc_STRCAT(buf, ": {", TRACE_BUF_SIZE(0));
+    for (sp = table; sp->name[0]; sp++)
        if (sp->val != 0
            && (val & sp->val) == sp->val) {
-           (void) strcat(buf, sp->name);
-           (void) strcat(buf, ", ");
+           _nc_STRCAT(buf, sp->name, TRACE_BUF_SIZE(0));
+           _nc_STRCAT(buf, ", ", TRACE_BUF_SIZE(0));
        }
     if (buf[strlen(buf) - 2] == ',')
        buf[strlen(buf) - 2] = '\0';
-    (void) strcat(buf, "} ");
+    _nc_STRCAT(buf, "} ", TRACE_BUF_SIZE(0));
 }
 
-char *
-_nc_tracebits(void)
+NCURSES_EXPORT(char *)
+_nc_trace_ttymode(TTY * tty)
 /* describe the state of the terminal control bits exactly */
 {
     char *buf;
 
 #ifdef TERMIOS
+#define DATA(name)        { name, { #name } }
+#define DATA2(name,name2) { name, { #name2 } }
+#define DATAX()           { 0,    { "" } }
     static const BITNAMES iflags[] =
     {
-       {BRKINT, "BRKINT"},
-       {IGNBRK, "IGNBRK"},
-       {IGNPAR, "IGNPAR"},
-       {PARMRK, "PARMRK"},
-       {INPCK, "INPCK"},
-       {ISTRIP, "ISTRIP"},
-       {INLCR, "INLCR"},
-       {IGNCR, "IGNC"},
-       {ICRNL, "ICRNL"},
-       {IXON, "IXON"},
-       {IXOFF, "IXOFF"},
-       {0, NULL}
+       DATA(BRKINT),
+       DATA(IGNBRK),
+       DATA(IGNPAR),
+       DATA(PARMRK),
+       DATA(INPCK),
+       DATA(ISTRIP),
+       DATA(INLCR),
+       DATA(IGNCR),
+       DATA(ICRNL),
+       DATA(IXON),
+       DATA(IXOFF),
+       DATAX()
 #define ALLIN  (BRKINT|IGNBRK|IGNPAR|PARMRK|INPCK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF)
     }, oflags[] =
     {
-       {OPOST, "OPOST"},
-       {0, NULL}
-#define ALLOUT (OPOST)
+       DATA(OPOST),
+       DATA2(OFLAGS_TABS, XTABS),
+       DATA(ONLCR),
+       DATA(OCRNL),
+       DATA(ONOCR),
+       DATA(ONLRET),
+       DATAX()
+#define ALLOUT (OPOST|OFLAGS_TABS|ONLCR|OCRNL|ONOCR|ONLRET|OFLAGS_TABS)
     }, cflags[] =
     {
-       {CLOCAL, "CLOCAL"},
-       {CREAD, "CREAD"},
-       {CSTOPB, "CSTOPB"},
+       DATA(CLOCAL),
+       DATA(CREAD),
+       DATA(CSTOPB),
 #if !defined(CS5) || !defined(CS8)
-       {CSIZE, "CSIZE"},
+       DATA(CSIZE),
 #endif
-       {HUPCL, "HUPCL"},
-       {PARENB, "PARENB"},
-       {PARODD | PARENB, "PARODD"},    /* concession to readability */
-       {0, NULL}
+       DATA(HUPCL),
+       DATA(PARENB),
+       DATA2(PARODD | PARENB, PARODD),
+       DATAX()
 #define ALLCTRL        (CLOCAL|CREAD|CSIZE|CSTOPB|HUPCL|PARENB|PARODD)
     }, lflags[] =
     {
-       {ECHO, "ECHO"},
-       {ECHOE | ECHO, "ECHOE"},        /* concession to readability */
-       {ECHOK | ECHO, "ECHOK"},        /* concession to readability */
-       {ECHONL, "ECHONL"},
-       {ICANON, "ICANON"},
-       {ISIG, "ISIG"},
-       {NOFLSH, "NOFLSH"},
-       {TOSTOP, "TOSTOP"},
-       {IEXTEN, "IEXTEN"},
-       {0, NULL}
+       DATA(ECHO),
+       DATA2(ECHOE | ECHO, ECHOE),
+       DATA2(ECHOK | ECHO, ECHOK),
+       DATA(ECHONL),
+       DATA(ICANON),
+       DATA(ISIG),
+       DATA(NOFLSH),
+       DATA(TOSTOP),
+       DATA(IEXTEN),
+       DATAX()
 #define ALLLOCAL       (ECHO|ECHONL|ICANON|ISIG|NOFLSH|TOSTOP|IEXTEN)
     };
 
     buf = _nc_trace_buf(0,
-       8 + sizeof(iflags) +
-       8 + sizeof(oflags) +
-       8 + sizeof(cflags) +
-       8 + sizeof(lflags) +
-       8);
+                       8 + sizeof(iflags) +
+                       8 + sizeof(oflags) +
+                       8 + sizeof(cflags) +
+                       8 + sizeof(lflags) +
+                       8);
+    if (buf != 0) {
 
-    if (cur_term->Nttyb.c_iflag & ALLIN)
-       lookup_bits(buf, iflags, "iflags", cur_term->Nttyb.c_iflag);
+       if (tty->c_iflag & ALLIN)
+           lookup_bits(buf, iflags, "iflags", tty->c_iflag);
 
-    if (cur_term->Nttyb.c_oflag & ALLOUT)
-       lookup_bits(buf, oflags, "oflags", cur_term->Nttyb.c_oflag);
+       if (tty->c_oflag & ALLOUT)
+           lookup_bits(buf, oflags, "oflags", tty->c_oflag);
 
-    if (cur_term->Nttyb.c_cflag & ALLCTRL)
-       lookup_bits(buf, cflags, "cflags", cur_term->Nttyb.c_cflag);
+       if (tty->c_cflag & ALLCTRL)
+           lookup_bits(buf, cflags, "cflags", tty->c_cflag);
 
 #if defined(CS5) && defined(CS8)
-    switch (cur_term->Nttyb.c_cflag & CSIZE) {
-#if defined(CS5) && (CS5 != 0)
-    case CS5:
-       strcat(buf, "CS5 ");
-       break;
-#endif
-#if defined(CS6) && (CS6 != 0)
-    case CS6:
-       strcat(buf, "CS6 ");
-       break;
-#endif
-#if defined(CS7) && (CS7 != 0)
-    case CS7:
-       strcat(buf, "CS7 ");
-       break;
-#endif
-#if defined(CS8) && (CS8 != 0)
-    case CS8:
-       strcat(buf, "CS8 ");
-       break;
-#endif
-    default:
-       strcat(buf, "CSIZE? ");
-       break;
-    }
+       {
+           static const struct {
+               int value;
+               const char name[5];
+           } csizes[] = {
+#define CS_DATA(name) { name, { #name " " } }
+               CS_DATA(CS5),
+#ifdef CS6
+                   CS_DATA(CS6),
+#endif
+#ifdef CS7
+                   CS_DATA(CS7),
 #endif
+                   CS_DATA(CS8),
+           };
+           const char *result = "CSIZE? ";
+           int value = (int) (tty->c_cflag & CSIZE);
+           unsigned n;
 
-    if (cur_term->Nttyb.c_lflag & ALLLOCAL)
-       lookup_bits(buf, lflags, "lflags", cur_term->Nttyb.c_lflag);
+           if (value != 0) {
+               for (n = 0; n < SIZEOF(csizes); n++) {
+                   if (csizes[n].value == value) {
+                       result = csizes[n].name;
+                       break;
+                   }
+               }
+           }
+           _nc_STRCAT(buf, result, TRACE_BUF_SIZE(0));
+       }
+#endif
 
+       if (tty->c_lflag & ALLLOCAL)
+           lookup_bits(buf, lflags, "lflags", tty->c_lflag);
+    }
 #else
     /* reference: ttcompat(4M) on SunOS 4.1 */
 #ifndef EVENP
@@ -203,34 +232,36 @@ _nc_tracebits(void)
 
     static const BITNAMES cflags[] =
     {
-       {CBREAK, "CBREAK"},
-       {CRMOD, "CRMOD"},
-       {ECHO, "ECHO"},
-       {EVENP, "EVENP"},
-       {LCASE, "LCASE"},
-       {LLITOUT, "LLITOUT"},
-       {ODDP, "ODDP"},
-       {RAW, "RAW"},
-       {TANDEM, "TANDEM"},
-       {XTABS, "XTABS"},
-       {0, NULL}
+       DATA(CBREAK),
+       DATA(CRMOD),
+       DATA(ECHO),
+       DATA(EVENP),
+       DATA(LCASE),
+       DATA(LLITOUT),
+       DATA(ODDP),
+       DATA(RAW),
+       DATA(TANDEM),
+       DATA(XTABS),
+       DATAX()
 #define ALLCTRL        (CBREAK|CRMOD|ECHO|EVENP|LCASE|LLITOUT|ODDP|RAW|TANDEM|XTABS)
     };
 
     buf = _nc_trace_buf(0,
-       8 + sizeof(cflags));
-
-    if (cur_term->Nttyb.sg_flags & ALLCTRL) {
-       lookup_bits(buf, cflags, "cflags", cur_term->Nttyb.sg_flags);
+                       8 + sizeof(cflags));
+    if (buf != 0) {
+       if (tty->sg_flags & ALLCTRL) {
+           lookup_bits(buf, cflags, "cflags", tty->sg_flags);
+       }
     }
 #endif
     return (buf);
 }
-#else
-char *
+
+NCURSES_EXPORT(char *)
 _nc_tracebits(void)
 {
-    static char tmp[] = "";
-    return tmp;
+    return _nc_trace_ttymode(&(cur_term->Nttyb));
 }
+#else
+EMPTY_MODULE(_nc_empty_lib_tracebits)
 #endif /* TRACE */