]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
refactor peer_st_flags() and kstatus_to_text() to use common
authorDave Hart <hart@ntp.org>
Sun, 2 Jan 2011 09:41:39 +0000 (09:41 +0000)
committerDave Hart <hart@ntp.org>
Sun, 2 Jan 2011 09:41:39 +0000 (09:41 +0000)
  data-driven loop instead of repeated macro expansions.

bk: 4d204853adS9ECVdNbaDREfNZ4Rq3g

include/lib_strbuf.h
include/ntp.h
include/ntp_stdlib.h
include/ntp_types.h
libntp/lib_strbuf.c
libntp/statestr.c
ntpd/ntp_control.c
ntpq/ntpq-subs.c

index 5aa0eb0a6d72d4e4f00b8d829399cb2d4893a678..aa4b7f968beec53f488dcdd8fcb042a1cb4bc702 100644 (file)
@@ -7,22 +7,22 @@
 /*
  * Sizes of things
  */
-#define        LIB_NUMBUFS     200
-#define        LIB_BUFLENGTH   80
+#define LIB_NUMBUF     16
+typedef char libbufstr[128];
+extern libbufstr lib_stringbuf[LIB_NUMBUF];
+extern int lib_nextbuf;
+extern int lib_inited;
+
+#define        LIB_BUFLENGTH   (sizeof(lib_stringbuf) / COUNTOF(lib_stringbuf))
 
 /*
  * Macro to get a pointer to the next buffer
  */
-#define        LIB_GETBUF(buf) \
-       do { \
-               if (!lib_inited) \
-                       init_lib(); \
-               buf = &lib_stringbuf[lib_nextbuf][0]; \
-               if (++lib_nextbuf >= LIB_NUMBUFS) \
-                       lib_nextbuf = 0; \
-               memset(buf, 0, LIB_BUFLENGTH); \
-       } while (0)
-
-extern char lib_stringbuf[LIB_NUMBUFS][LIB_BUFLENGTH];
-extern int lib_nextbuf;
-extern int lib_inited;
+#define        LIB_GETBUF(bufp)                                        \
+       do {                                                    \
+               if (!lib_inited)                                \
+                       init_lib();                             \
+               (bufp) = &lib_stringbuf[lib_nextbuf++][0];      \
+               lib_nextbuf %= COUNTOF(lib_stringbuf);          \
+               memset((bufp), '\0', sizeof(lib_stringbuf[0])); \
+       } while (FALSE)
index 0771c2524fb06dc3c5ceb72b8861857b51d51ed2..20a9c84e81de52b830c5a7d534d56d762ce6de82 100644 (file)
@@ -89,12 +89,6 @@ typedef char s_char;
 #  undef SO_SNDBUF
 # endif
 #endif
-#ifndef TRUE
-# define TRUE 1
-#endif /* TRUE */
-#ifndef FALSE
-# define FALSE 0
-#endif /* FALSE */
 
 /*
  * NTP protocol parameters.  See section 3.2.6 of the specification.
index deaf3908f4833b83956cf783f83fea8a6a840c22..e05389346047f5fe85b0ff42b9fb859c6c481d23 100644 (file)
@@ -108,12 +108,10 @@ extern    char *  inttoa          (long);
 extern char *  mfptoa          (u_long, u_long, short);
 extern char *  mfptoms         (u_long, u_long, short);
 extern const char * modetoa    (int);
-extern  const char * eventstr  (int);
-extern  const char * ceventstr (int);
+extern const char * eventstr   (int);
+extern const char * ceventstr  (int);
+extern const char * k_st_flags (u_int32);
 extern char *  statustoa       (int, int);
-extern  const char * sysstatstr (int);
-extern  const char * peerstatstr (int);
-extern  const char * clockstatstr (int);
 extern sockaddr_u * netof      (sockaddr_u *);
 extern char *  numtoa          (u_int32);
 extern char *  numtohost       (u_int32);
index 66a70c8add31f1af45e67489c351d33c1542f809..9c47a03f45c7a1a09321e4db44ad357c129807f9 100644 (file)
 
 #include "ntp_machine.h"
 
+
+#ifndef TRUE
+# define       TRUE    1
+#endif
+#ifndef FALSE
+# define       FALSE   0
+#endif
+
 /*
  * This is another naming conflict.
  * On NetBSD for MAC the macro "mac" is defined as 1
index e0becbcbc4051fccd63c5758637b0a5234592064..a482fe08a074a738e6edfcedb7f363db36b3850b 100644 (file)
@@ -13,7 +13,7 @@
 /*
  * Storage declarations
  */
-char lib_stringbuf[LIB_NUMBUFS][LIB_BUFLENGTH];
+libbufstr lib_stringbuf[LIB_NUMBUF];
 int lib_nextbuf;
 int ipv4_works;
 int ipv6_works;
index 71dc7cad686bb5f212a3a741aa2c0ee822d61b52..e8513cd8fd11de1b23bae873eb0f2ea42bdc45b0 100644 (file)
 #include "ntp_refclock.h"
 #include "ntp_control.h"
 #include "ntp_string.h"
+#ifdef KERNEL_PLL
+# include "ntp_syscall.h"
+#endif
+
 
 /*
  * Structure for turning various constants into a readable string.
@@ -148,6 +152,18 @@ static const struct codestring peer_codes[] = {
        { -1,                           "" }
 };
 
+/*
+ * Peer status bits
+ */
+static const struct codestring peer_st_bits[] = {
+       { CTL_PST_CONFIG,               "conf" },
+       { CTL_PST_AUTHENABLE,           "authenb" },
+       { CTL_PST_AUTHENTIC,            "auth" },
+       { CTL_PST_REACH,                "reach" },
+       { CTL_PST_BCAST,                "bcast" },
+       /* not used with getcode(), no terminating entry needed */
+};
+
 #ifdef AUTOKEY
 /*
  * Crypto events (cryp)
@@ -173,6 +189,63 @@ static const struct codestring crypto_codes[] = {
 };
 #endif /* AUTOKEY */
 
+#ifdef KERNEL_PLL
+/*
+ * kernel discipline status bits
+ */
+static const struct codestring k_st_bits[] = {
+# ifdef STA_PLL
+       { STA_PLL,                      "pll" },
+# endif
+# ifdef STA_PPSFREQ
+       { STA_PPSFREQ,                  "ppsfreq" },
+# endif
+# ifdef STA_PPSTIME
+       { STA_PPSTIME,                  "ppstime" },
+# endif
+# ifdef STA_FLL
+       { STA_FLL,                      "fll" },
+# endif
+# ifdef STA_INS
+       { STA_INS,                      "ins" },
+# endif
+# ifdef STA_DEL
+       { STA_DEL,                      "del" },
+# endif
+# ifdef STA_UNSYNC
+       { STA_UNSYNC,                   "unsync" },
+# endif
+# ifdef STA_FREQHOLD
+       { STA_FREQHOLD,                 "freqhold" },
+# endif
+# ifdef STA_PPSSIGNAL
+       { STA_PPSSIGNAL,                "ppssignal" },
+# endif
+# ifdef STA_PPSJITTER
+       { STA_PPSJITTER,                "ppsjitter" },
+# endif
+# ifdef STA_PPSWANDER
+       { STA_PPSWANDER,                "ppswander" },
+# endif
+# ifdef STA_PPSERROR
+       { STA_PPSERROR,                 "ppserror" },
+# endif
+# ifdef STA_CLOCKERR
+       { STA_CLOCKERR,                 "clockerr" },
+# endif
+# ifdef STA_NANO
+       { STA_NANO,                     "nano" },
+# endif
+# ifdef STA_MODE
+       { STA_MODE,                     "mode=fll" },
+# endif
+# ifdef STA_CLK
+       { STA_CLK,                      "src=B" },
+# endif
+       /* not used with getcode(), no terminating entry needed */
+};
+#endif /* KERNEL_PLL */
+
 /* Forwards */
 static const char *    getcode(int, const struct codestring *);
 static const char *    getevents(int);
@@ -224,43 +297,84 @@ getevents(
 }
 
 
+/*
+ * decode_bitflags()
+ *
+ * returns a human-readable string with a keyword from tab for each bit
+ * set in bits, separating multiple entries with text of sep2.
+ */
 static const char *
-peer_st_flags(u_char pst)
+decode_bitflags(
+       int                             bits,
+       const char *                    sep2,
+       const struct codestring *       tab,
+       size_t                          tab_ct
+       )
 {
-       static const char       toosmall[] = "buffer too small!";
-       static const char       csp[] = ", ";
-       const char *            sep;
-       char *                  buf;
-       char *                  pch;
-       char *                  lim;
-       int                     rc;
+       const char *    sep;
+       char *          buf;
+       char *          pch;
+       char *          lim;
+       size_t          b;
+       int             rc;
 
        LIB_GETBUF(buf);
        pch = buf;
        lim = buf + LIB_BUFLENGTH;
        sep = "";
 
-#define EXPAND_PEERST_BIT(b, text)                             \
-do {                                                           \
-       if ((b) & pst) {                                        \
-               rc = snprintf(pch, (lim - pch), "%s" text, sep);\
-               if (rc < 0)                                     \
-                       return toosmall;                        \
-               pch += (u_int)rc;                               \
-               if (pch >= lim)                                 \
-                       return toosmall;                        \
-               sep = csp;                                      \
-       }                                                       \
-} while (0)
-
-       EXPAND_PEERST_BIT(CTL_PST_CONFIG,       "conf");
-       EXPAND_PEERST_BIT(CTL_PST_AUTHENABLE,   "authenb");
-       EXPAND_PEERST_BIT(CTL_PST_AUTHENTIC,    "auth");
-       EXPAND_PEERST_BIT(CTL_PST_REACH,        "reach");
-       EXPAND_PEERST_BIT(CTL_PST_BCAST,        "bcast");
+       for (b = 0; b < tab_ct; b++) {
+               if (tab[b].code & bits) {
+                       rc = snprintf(pch, (lim - pch), "%s%s", sep,
+                                     tab[b].string);
+                       if (rc < 0)
+                               goto toosmall;
+                       pch += (u_int)rc;
+                       if (pch >= lim)
+                               goto toosmall;
+                       sep = sep2;
+               }
+       }
 
        return buf;
+
+    toosmall:
+       snprintf(buf, LIB_BUFLENGTH,
+                "decode_bitflags(%s) can't decode 0x%x in %d bytes",
+                (tab == peer_st_bits)
+                    ? "peer_st"
+                    : 
+#ifdef KERNEL_PLL
+                      (tab == k_st_bits)
+                          ? "kern_st"
+                          :
+#endif
+                            "",
+                bits, (int)LIB_BUFLENGTH);
+
+       return buf;
+}
+
+
+static const char *
+peer_st_flags(
+       u_char pst
+       )
+{
+       return decode_bitflags(pst, ", ", peer_st_bits,
+                              COUNTOF(peer_st_bits));
+}
+
+
+#ifdef KERNEL_PLL
+const char *
+k_st_flags(
+       u_int32 st
+       )
+{
+       return decode_bitflags(st, " ", k_st_bits, COUNTOF(k_st_bits));
 }
+#endif /* KERNEL_PLL */
 
 
 /*
index 2040053b4979ee264db22fd082e3683badbd1a44..98b44233ae96f0f6443e962c5ff4ba91844144ed 100644 (file)
@@ -70,9 +70,6 @@ static        void    ctl_putadr      (const char *, u_int32,
                                 sockaddr_u *);
 static void    ctl_putid       (const char *, char *);
 static void    ctl_putarray    (const char *, double *, int);
-#ifdef KERNEL_PLL
-static void    kstatus_to_text (u_int32, char *, size_t);
-#endif
 static void    ctl_putsys      (int);
 static void    ctl_putpeer     (int, struct peer *);
 static void    ctl_putfs       (const char *, tstamp_t);
@@ -1622,133 +1619,6 @@ ctl_putarray(
        ctl_putdata(buffer, (unsigned)(cp - buffer), 0);
 }
 
-/*
- * kstatus_to_text - convert ntp_adjtime() status bits to text
- */
-#ifdef KERNEL_PLL
-void
-kstatus_to_text(
-       u_int32 status,
-       char *  str,
-       size_t  str_sz
-       )
-{
-       char *  pch;
-       const char *lim;
-
-       pch = str;
-       lim = pch + str_sz;
-
-# define       XLATE_KST_BIT(bitval, text)                     \
-do {                                                           \
-       if (((bitval) & status) &&                              \
-           (pch + sizeof(text) <= lim)) {                      \
-               memcpy(pch, (text), sizeof(text));              \
-               pch += (u_int)(sizeof(text) - 1);               \
-       }                                                       \
-} while (0)
-
-# ifdef STA_PLL
-       {
-               const char spll[] =             "pll ";
-               XLATE_KST_BIT(STA_PLL, spll);
-       }
-# endif
-# ifdef STA_PPSFREQ
-       {
-               const char sppsfreq[] =         "ppsfreq ";
-               XLATE_KST_BIT(STA_PPSFREQ, sppsfreq);
-       }
-# endif
-# ifdef STA_PPSTIME
-       {
-               const char sppstime[] =         "ppstime ";
-               XLATE_KST_BIT(STA_PPSTIME, sppstime);
-       }
-# endif
-# ifdef STA_FLL
-       {
-               const char sfll[] =             "fll ";
-               XLATE_KST_BIT(STA_FLL, sfll);
-       }
-# endif
-# ifdef STA_INS
-       {
-               const char sins[] =             "ins ";
-               XLATE_KST_BIT(STA_INS, sins);
-       }
-# endif
-# ifdef STA_DEL
-       {
-               const char sdel[] =             "del ";
-               XLATE_KST_BIT(STA_DEL, sdel);
-       }
-# endif
-# ifdef STA_UNSYNC
-       {
-               const char sunsync[] =          "unsync ";
-               XLATE_KST_BIT(STA_UNSYNC, sunsync);
-       }
-# endif
-# ifdef STA_FREQHOLD
-       {
-               const char sfreqhold[] =        "freqhold ";
-               XLATE_KST_BIT(STA_FREQHOLD, sfreqhold);
-       }
-# endif
-# ifdef STA_PPSSIGNAL
-       {
-               const char sppssignal[] =       "ppssignal ";
-               XLATE_KST_BIT(STA_PPSSIGNAL, sppssignal);
-       }
-# endif
-# ifdef STA_PPSJITTER
-       {
-               const char sppsjitter[] =       "ppsjitter ";
-               XLATE_KST_BIT(STA_PPSJITTER, sppsjitter);
-       }
-# endif
-# ifdef STA_PPSWANDER
-       {
-               const char sppswander[] =       "ppswander ";
-               XLATE_KST_BIT(STA_PPSWANDER, sppswander);
-       }
-# endif
-# ifdef STA_PPSERROR
-       {
-               const char sppserror[] =        "ppserror ";
-               XLATE_KST_BIT(STA_PPSERROR, sppserror);
-       }
-# endif
-# ifdef STA_CLOCKERR
-       {
-               const char sclockerr[] =        "clockerr ";
-               XLATE_KST_BIT(STA_CLOCKERR, sclockerr);
-       }
-# endif
-# ifdef STA_NANO
-       {
-               const char snano[] =            "nano ";
-               XLATE_KST_BIT(STA_NANO, snano);
-       }
-# endif
-# ifdef STA_MODE
-       {
-               const char smodefll[] =         "mode=fll ";
-               XLATE_KST_BIT(STA_MODE, smodefll);
-       }
-# endif
-# ifdef STA_CLK
-       {
-               const char ssrcb[] =            "src=B ";
-               XLATE_KST_BIT(STA_CLK, ssrcb);
-       }
-# endif
-       if (pch > str && ' ' == pch[-1])
-               pch[-1] = '\0';
-}
-#endif /* KERNEL_PLL */
-
 
 /*
  * ctl_putsys - output a system variable
@@ -2178,11 +2048,11 @@ ctl_putsys(
 
        case CS_K_STFLAGS:
 #ifndef KERNEL_PLL
-               str[0] = '\0';
+               ss = "";
 #else
-               kstatus_to_text(ntx.status, str, sizeof(str));
+               ss = k_st_flags(ntx.status);
 #endif
-               ctl_putstr(sys_var[varid].text, str, strlen(str));
+               ctl_putstr(sys_var[varid].text, ss, strlen(ss));
                break;
 
        case CS_K_TIMECONST:
index 34029094de4950160c51941f7f859df570b2097b..00a0a0665c5febea4ac231d17a2f7a7334bbb878 100644 (file)
@@ -3183,6 +3183,8 @@ collect_display_vdc(
         * the retrieved values.
         */
        while (nextvar(&rsize, &rdata, &tag, &val)) {
+               if (NULL == val)
+                       continue;
                for (pvdc = table; pvdc->tag != NULL; pvdc++) {
                        len = strlen(pvdc->tag);
                        if (strncmp(tag, pvdc->tag, len))