Use good m_ functions, because it smell like a flower, version 2.
[apps/madmutt.git] / makedoc.c
index 54f3549..6451c48 100644 (file)
--- a/makedoc.c
+++ b/makedoc.c
  **
  **/
 
-#if HAVE_CONFIG_H
-# include "config.h"
-#endif
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-
-#include <errno.h>
-
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
+#include <lib-lib/lib-lib.h>
 
 #ifdef HAVE_GETOPT_H
 # include <getopt.h>
 #endif
 
-#ifndef HAVE_STRERROR
-#ifndef STDC_HEADERS
-extern int sys_nerr;
-extern char *sys_errlist[];
-#endif
-
-#define strerror(x) ((x) > 0 && (x) < sys_nerr) ? sys_errlist[(x)] : 0
-#endif /* !HAVE_STRERROR */
-
-extern int optind;
-
-#define BUFFSIZE 2048
-
-#define STRLEN(s) (s ? strlen(s) : 0)
-
 typedef struct {
   short seen;
   char *name;
@@ -66,7 +38,7 @@ static var_t *outbuf = NULL;
 
 static int var_cmp (const void *a, const void *b)
 {
-  return (strcmp (((var_t *) a)->name, ((var_t *) b)->name));
+  return (m_strcmp (((var_t *) a)->name, ((var_t *) b)->name));
 }
 
 enum output_formats_t {
@@ -82,12 +54,15 @@ enum output_formats_t {
 #define D_INIT          (1 << 6)
 #define D_DL            (1 << 7)
 #define D_DT            (1 << 8)
+#define D_DD            (1 << 9)
+#define D_PA            (1 << 10)
 
 enum {
   SP_START_EM,
   SP_START_BF,
   SP_START_TT,
   SP_END_FT,
+  SP_END_PAR,
   SP_NEWLINE,
   SP_NEWPAR,
   SP_STR,
@@ -96,13 +71,14 @@ enum {
   SP_START_DL,
   SP_DT,
   SP_DD,
+  SP_END_DD,
   SP_END_DL,
+  SP_END_SECT,
   SP_REFER
 };
 
 enum output_formats_t OutputFormat = F_NONE;
 char *Progname;
-short Debug = 0;
 
 static char *get_token (char *, size_t, char *);
 static char *skip_ws (char *);
@@ -114,9 +90,9 @@ static int print_it (int, char *, int);
 static void print_confline (const char *, int, const char *);
 static void handle_confline (char *);
 static void makedoc (FILE *, FILE *);
-static void pretty_default (char *, size_t, const char *, int);
 static int sgml_fputc (int);
 static int sgml_fputs (const char *);
+static int sgml_id_fputs (const char *);
 static void add_var (const char *);
 static int add_s (const char *);
 static int add_c (int);
@@ -142,9 +118,6 @@ int main (int argc, char *argv[])
     case 's':
       OutputFormat = F_SGML;
       break;
-    case 'd':
-      Debug++;
-      break;
     default:
       {
         fprintf (stderr, "%s: bad command line parameter.\n", Progname);
@@ -177,7 +150,7 @@ int main (int argc, char *argv[])
   }
 
   if (f != stdin)
-    fclose (f);
+    m_fclose(&f);
 
   exit (1);
 }
@@ -186,13 +159,13 @@ static void add_var (const char *name)
 {
   outbuf = realloc (outbuf, (++outcount) * sizeof (var_t));
   outbuf[outcount - 1].seen = 0;
-  outbuf[outcount - 1].name = strdup (name);
+  outbuf[outcount - 1].name = strdup(name);
   outbuf[outcount - 1].descr = NULL;
 }
 
 static int add_s (const char *s)
 {
-  size_t lnew = STRLEN (s), lold = STRLEN (outbuf[outcount - 1].descr);
+  size_t lnew = m_strlen(s), lold = m_strlen(outbuf[outcount - 1].descr);
 
   if (lnew == 0)
     return (0);
@@ -202,7 +175,7 @@ static int add_s (const char *s)
   }
 
   if (lold == 0)
-    outbuf[outcount - 1].descr = strdup (s);
+    outbuf[outcount - 1].descr = strdup(s);
   else {
     outbuf[outcount - 1].descr =
       realloc (outbuf[outcount - 1].descr, lold + lnew + 1);
@@ -222,8 +195,8 @@ static int add_c (int c)
 
 static void makedoc (FILE * in, FILE * out)
 {
-  char buffer[BUFFSIZE];
-  char token[BUFFSIZE];
+  char buffer[BUFSIZ];
+  char token[BUFSIZ];
   char *p;
   int active = 0;
   int line = 0;
@@ -242,20 +215,15 @@ static void makedoc (FILE * in, FILE * out)
     if (!(p = get_token (token, sizeof (token), buffer)))
       continue;
 
-    if (Debug) {
-      fprintf (stderr, "%s: line %d.  first token: \"%s\".\n",
-               Progname, line, token);
-    }
-
-    if (!strcmp (token, "/*++*/"))
+    if (!m_strcmp (token, "/*++*/"))
       active = 1;
-    else if (!strcmp (token, "/*--*/")) {
+    else if (!m_strcmp (token, "/*--*/")) {
       docstat = flush_doc (docstat);
       active = 0;
     }
-    else if (active && (!strcmp (token, "/**") || !strcmp (token, "**")))
+    else if (active && (!m_strcmp (token, "/**") || !m_strcmp (token, "**")))
       docstat = handle_docline (p, docstat);
-    else if (active && !strcmp (token, "{")) {
+    else if (active && !m_strcmp (token, "{")) {
       docstat = flush_doc (docstat);
       handle_confline (p);
     }
@@ -291,37 +259,20 @@ static char *get_token (char *d, size_t l, char *s)
 {
   char *t;
   short is_quoted = 0;
-  char *dd = d;
-
-  if (Debug)
-    fprintf (stderr, "%s: get_token called for `%s'.\n", Progname, s);
 
   s = skip_ws (s);
 
-  if (Debug > 1)
-    fprintf (stderr, "%s: argumet after skip_ws():  `%s'.\n", Progname, s);
-
   if (!*s) {
-    if (Debug)
-      fprintf (stderr, "%s: no more tokens on this line.\n", Progname);
     return NULL;
   }
 
   if (strchr (single_char_tokens, *s)) {
-    if (Debug) {
-      fprintf (stderr, "%s: found single character token `%c'.\n",
-               Progname, *s);
-    }
     d[0] = *s++;
     d[1] = 0;
     return s;
   }
 
   if (*s == '"') {
-    if (Debug) {
-      fprintf (stderr, "%s: found quote character.\n", Progname);
-    }
-
     s++;
     is_quoted = 1;
   }
@@ -364,12 +315,6 @@ static char *get_token (char *d, size_t l, char *s)
 
   *d = '\0';
 
-  if (Debug) {
-    fprintf (stderr, "%s: Got %stoken: `%s'.\n",
-             Progname, is_quoted ? "quoted " : "", dd);
-    fprintf (stderr, "%s: Remainder: `%s'.\n", Progname, t);
-  }
-
   return t;
 }
 
@@ -397,35 +342,35 @@ enum {
   DT_RX,
   DT_MAGIC,
   DT_SYN,
-  DT_ADDR
+  DT_ADDR,
+  DT_SYS
 };
 
 struct {
-  char *machine;
-  char *human;
+  const char *machine;
+  const char *human;
 } types[] = {
-  {
-  "DT_NONE", "-none-"}, {
-  "DT_BOOL", "boolean"}, {
-  "DT_NUM", "number"}, {
-  "DT_STR", "string"}, {
-  "DT_PATH", "path"}, {
-  "DT_QUAD", "quadoption"}, {
-  "DT_SORT", "sort order"}, {
-  "DT_RX", "regular expression"}, {
-  "DT_MAGIC", "folder magic"}, {
-  "DT_SYN", NULL}, {
-  "DT_ADDR", "e-mail address"}, {
-  NULL, NULL}
+  {"DT_NONE",  "-none-"},
+  {"DT_BOOL",  "boolean"},
+  {"DT_NUM",   "number"},
+  {"DT_STR",   "string"},
+  {"DT_PATH",  "path"},
+  {"DT_QUAD",  "quadoption"},
+  {"DT_SORT",  "sort order"},
+  {"DT_RX",    "regular expression"},
+  {"DT_MAGIC", "folder magic"},
+  {"DT_SYN",   NULL},
+  {"DT_ADDR",  "e-mail address"},
+  {"DT_SYS",   "system property"},
+  {NULL, NULL}
 };
 
-
 static int buff2type (const char *s)
 {
   int type;
 
   for (type = DT_NONE; types[type].machine; type++)
-    if (!strcmp (types[type].machine, s))
+    if (!m_strcmp (types[type].machine, s))
       return type;
 
   return DT_NONE;
@@ -437,12 +382,11 @@ static const char *type2human (int type)
 }
 static void handle_confline (char *s)
 {
-  char varname[BUFFSIZE];
-  char buff[BUFFSIZE];
-  char tmp[BUFFSIZE];
+  char varname[BUFSIZ];
+  char buff[BUFSIZ];
   int type;
 
-  char val[BUFFSIZE];
+  char val[BUFSIZ];
 
   /* xxx - put this into an actual state machine? */
 
@@ -464,9 +408,7 @@ static void handle_confline (char *s)
   if (!(s = get_token (buff, sizeof (buff), s)))
     return;
 
-  if (!strcmp (buff, "|")) {
-    if (Debug)
-      fprintf (stderr, "%s: Expecting <subtype> <comma>.\n", Progname);
+  if (!m_strcmp (buff, "|")) {
     /* ignore subtype and comma */
     if (!(s = get_token (buff, sizeof (buff), s)))
       return;
@@ -479,14 +421,14 @@ static void handle_confline (char *s)
   while (1) {
     if (!(s = get_token (buff, sizeof (buff), s)))
       return;
-    if (!strcmp (buff, ","))
+    if (!m_strcmp (buff, ","))
       break;
   }
 
   /* option name or UL &address */
   if (!(s = get_token (buff, sizeof (buff), s)))
     return;
-  if (!strcmp (buff, "UL"))
+  if (!m_strcmp (buff, "UL"))
     if (!(s = get_token (buff, sizeof (buff), s)))
       return;
 
@@ -494,108 +436,43 @@ static void handle_confline (char *s)
   if (!(s = get_token (buff, sizeof (buff), s)))
     return;
 
-  if (Debug)
-    fprintf (stderr, "%s: Expecting default value.\n", Progname);
-
   /* <default value> or UL <default value> */
   if (!(s = get_token (buff, sizeof (buff), s)))
     return;
-  if (!strcmp (buff, "UL")) {
-    if (Debug)
-      fprintf (stderr, "%s: Skipping UL.\n", Progname);
+  if (!m_strcmp (buff, "UL")) {
     if (!(s = get_token (buff, sizeof (buff), s)))
       return;
   }
 
-  memset (tmp, 0, sizeof (tmp));
+  memset(val, 0, sizeof(val));
 
   do {
-    if (!strcmp (buff, "}"))
+    if (!m_strcmp (buff, "}"))
       break;
 
-    strncpy (tmp + STRLEN (tmp), buff, sizeof (tmp) - STRLEN (tmp));
+    m_strcat(val, sizeof(val), buff);
   }
   while ((s = get_token (buff, sizeof (buff), s)));
 
-  pretty_default (val, sizeof (val), tmp, type);
   add_var (varname);
   print_confline (varname, type, val);
 }
 
-static void pretty_default (char *t, size_t l, const char *s, int type)
-{
-  memset (t, 0, l);
-  l--;
-
-  switch (type) {
-  case DT_QUAD:
-    {
-      if (!strcasecmp (s, "M_YES"))
-        strncpy (t, "yes", l);
-      else if (!strcasecmp (s, "M_NO"))
-        strncpy (t, "no", l);
-      else if (!strcasecmp (s, "M_ASKYES"))
-        strncpy (t, "ask-yes", l);
-      else if (!strcasecmp (s, "M_ASKNO"))
-        strncpy (t, "ask-no", l);
-      break;
-    }
-  case DT_BOOL:
-    {
-      if (atoi (s))
-        strncpy (t, "yes", l);
-      else
-        strncpy (t, "no", l);
-      break;
-    }
-  case DT_SORT:
-    {
-      /* heuristic! */
-      strncpy (t, s + 5, l);
-      for (; *t; t++)
-        *t = tolower ((unsigned char) *t);
-      break;
-    }
-  case DT_MAGIC:
-    {
-      /* heuristic! */
-      strncpy (t, s + 2, l);
-      for (; *t; t++)
-        *t = tolower ((unsigned char) *t);
-      break;
-    }
-  case DT_STR:
-  case DT_RX:
-  case DT_ADDR:
-  case DT_PATH:
-    {
-      if (!strcmp (s, "0"))
-        break;
-      /* fallthrough */
-    }
-  default:
-    {
-      strncpy (t, s, l);
-      break;
-    }
-  }
-}
-
 static void char_to_escape (char *dest, unsigned int c)
 {
   switch (c) {
   case '\r':
-    strcpy (dest, "\\r");
-    break;                      /* __STRCPY_CHECKED__ */
+    m_strcpy(dest, "\\r");
+    break;
   case '\n':
-    strcpy (dest, "\\n");
-    break;                      /* __STRCPY_CHECKED__ */
+    m_strcpy(dest, "\\n");
+    break;
   case '\t':
-    strcpy (dest, "\\t");
-    break;                      /* __STRCPY_CHECKED__ */
+    m_strcpy(dest, "\\t");
+    break;
   case '\f':
-    strcpy (dest, "\\f");
-    break;                      /* __STRCPY_CHECKED__ */
+    m_strcpy(dest, "\\f");
+    break;
   default:
     sprintf (dest, "\\%03o", c);
     break;
@@ -662,24 +539,8 @@ static int sgml_fputc (int c)
     return add_s ("&lt;");
   case '>':
     return add_s ("&gt;");
-  case '$':
-    return add_s ("&dollar;");
-  case '_':
-    return add_s ("&lowbar;");
-  case '%':
-    return add_s ("&percnt;");
   case '&':
     return add_s ("&amp;");
-  case '\\':
-    return add_s ("&bsol;");
-  case '"':
-    return add_s ("&dquot;");
-  case '[':
-    return add_s ("&lsqb;");
-  case ']':
-    return add_s ("&rsqb;");
-  case '~':
-    return add_s ("&tilde;");
   default:
     return add_c (c);
   }
@@ -694,6 +555,21 @@ static int sgml_fputs (const char *s)
   return 0;
 }
 
+/* reduce CDATA to ID */
+static int sgml_id_fputs (const char *s) {
+ char id;
+
+ for (; *s; s++) {
+   if (*s == '_')
+     id = '-';
+   else
+     id = *s;
+   if (sgml_fputc ((unsigned int) id) == EOF)
+     return EOF;
+ }
+ return 0;
+}
+
 static void print_confline (const char *varname, int type, const char *val)
 {
   if (type == DT_SYN)
@@ -703,11 +579,18 @@ static void print_confline (const char *varname, int type, const char *val)
     /* configuration file */
   case F_CONF:
     {
+      if (type == DT_SYS) {
+        add_s ("\n# set ?");
+        add_s (varname);
+        add_s (" prints ");
+        add_s (val);
+        break;
+      }
       if (type == DT_STR || type == DT_RX || type == DT_ADDR
           || type == DT_PATH) {
         add_s ("\n# set ");
         add_s (varname);
-        add_s ("\"");
+        add_s ("=\"");
         conf_print_strval (val);
         add_s ("\"");
       }
@@ -753,7 +636,7 @@ static void print_confline (const char *varname, int type, const char *val)
         add_s ("\\(rq\n");
       }
       else {
-        add_s ("Default: ");
+        add_s (type == DT_SYS ? "Value: " : "Default: ");
         add_s (val);
         add_c ('\n');
       }
@@ -765,26 +648,26 @@ static void print_confline (const char *varname, int type, const char *val)
     /* SGML based manual */
   case F_SGML:
     {
-      add_s ("\n<sect1>");
+      add_s ("\n<madmutt-doc:vardef name=\"");
       sgml_fputs (varname);
-      add_s ("<label id=\"");
-      add_s (varname);
-      add_s ("\">");
-      add_s ("\n<p>\nType: <tt>");
+      add_s ("\">\n<para>Type: <literal>");
       add_s (type2human (type));
-      add_s ("</tt>\n\n");
+      add_s ("</literal></para>\n");
 
       if (type == DT_STR || type == DT_RX || type == DT_ADDR
           || type == DT_PATH) {
-        add_s ("<p>\nDefault: <tt>&dquot;");
+        add_s ("<para>\nDefault: <literal>&quot;");
         sgml_print_strval (val);
-        add_s ("&dquot;</tt>");
+        add_s ("&quot;</literal>");
       }
       else {
-        add_s ("<p>\nDefault: <tt>");
+        add_s ("<para>\n"); 
+        add_s (type == DT_SYS ? "Value: " : "Default: ");
+        add_s ("<literal>");
         add_s (val);
-        add_s ("</tt>");
+        add_s ("</literal>");
       }
+      add_s ("</para>\n");
       break;
     }
     /* make gcc happy */
@@ -835,6 +718,9 @@ static int flush_doc (int docstat)
     exit (1);
   }
 
+  if (docstat & (D_PA))
+    docstat = print_it (SP_END_PAR, NULL, docstat);
+
   if (docstat & (D_TAB))
     docstat = print_it (SP_END_TAB, NULL, docstat);
 
@@ -844,6 +730,8 @@ static int flush_doc (int docstat)
   if (docstat & (D_EM | D_BF | D_TT))
     docstat = print_it (SP_END_FT, NULL, docstat);
 
+  docstat = print_it (SP_END_SECT, NULL, docstat);
+
   docstat = print_it (SP_NEWLINE, NULL, 0);
 
   fd_recurse--;
@@ -947,7 +835,7 @@ static int print_it (int special, char *str, int docstat)
           if (docstat & D_DT) {
             int i;
 
-            for (i = STRLEN (str); i < 8; i++)
+            for (i = m_strlen(str); i < 8; i++)
               add_c (' ');
             docstat &= ~D_DT;
             docstat |= D_NL;
@@ -1055,11 +943,11 @@ static int print_it (int special, char *str, int docstat)
                 add_s ("\\(rq");
               else if (*str == '\\')
                 add_s ("\\\\");
-              else if (!strncmp (str, "``", 2)) {
+              else if (!m_strncmp (str, "``", 2)) {
                 add_s ("\\(lq");
                 str++;
               }
-              else if (!strncmp (str, "''", 2)) {
+              else if (!m_strncmp (str, "''", 2)) {
                 add_s ("\\(rq");
                 str++;
               }
@@ -1080,31 +968,31 @@ static int print_it (int special, char *str, int docstat)
       case SP_END_FT:
         {
           if (docstat & D_EM)
-            add_s ("</em>");
+            add_s ("</emphasis>");
           if (docstat & D_BF)
-            add_s ("</bf>");
+            add_s ("</emphasis>");
           if (docstat & D_TT)
-            add_s ("</tt>");
+            add_s ("</literal>");
           docstat &= ~(D_EM | D_BF | D_TT);
           break;
         }
       case SP_START_BF:
         {
-          add_s ("<bf>");
+          add_s ("<emphasis role=\"bold\">");
           docstat |= D_BF;
           docstat &= ~(D_EM | D_TT);
           break;
         }
       case SP_START_EM:
         {
-          add_s ("<em>");
+          add_s ("<emphasis>");
           docstat |= D_EM;
           docstat &= ~(D_BF | D_TT);
           break;
         }
       case SP_START_TT:
         {
-          add_s ("<tt>");
+          add_s ("<literal>");
           docstat |= D_TT;
           docstat &= ~(D_EM | D_BF);
           break;
@@ -1128,44 +1016,65 @@ static int print_it (int special, char *str, int docstat)
 
           if (!(onl & D_NL))
             add_s ("\n");
-          add_s ("\n<p>\n");
+          if (docstat & D_PA)
+            add_s ("</para>\n");
+          add_s ("<para>\n");
 
           docstat |= D_NP;
+          docstat |= D_PA;
           break;
         }
       case SP_START_TAB:
         {
-          add_s ("\n<tscreen><verb>\n");
+          add_s ("\n<screen>\n");
           docstat |= D_TAB | D_NL;
           break;
         }
       case SP_END_TAB:
         {
-          add_s ("\n</verb></tscreen>");
+          add_s ("\n</screen>");
           docstat &= ~D_TAB;
           docstat |= D_NL;
           break;
         }
       case SP_START_DL:
         {
-          add_s ("\n<descrip>\n");
+          add_s ("\n<variablelist>\n");
           docstat |= D_DL;
           break;
         }
       case SP_DT:
         {
-          add_s ("<tag>");
+          add_s ("<varlistentry><term>");
           break;
         }
       case SP_DD:
         {
-          add_s ("</tag>");
+          add_s ("</term>\n<listitem><para>\n");
+          docstat |= D_DD;
           break;
         }
       case SP_END_DL:
         {
-          add_s ("</descrip>\n");
-          docstat &= ~D_DL;
+          add_s ("</para></listitem></varlistentry></variablelist>\n");
+          docstat &= ~(D_DL|D_DD);
+          break;
+        }
+      case SP_END_PAR:
+        {
+          add_s ("</para>\n");
+          docstat &= ~D_PA;
+          break;
+        }
+      case SP_END_DD:
+        {
+          add_s ("</para></listitem></varlistentry>\n");
+          docstat &= ~D_DD;
+          break;
+        }
+      case SP_END_SECT:
+        {
+          add_s ("</madmutt-doc:vardef>\n");
           break;
         }
       case SP_STR:
@@ -1187,7 +1096,7 @@ static int print_it (int special, char *str, int docstat)
   return docstat;
 }
 
-void print_ref (int output_dollar, const char *ref)
+static void print_ref (int output_dollar, const char *ref)
 {
   switch (OutputFormat) {
   case F_CONF:
@@ -1198,13 +1107,13 @@ void print_ref (int output_dollar, const char *ref)
     break;
 
   case F_SGML:
-    add_s ("<ref id=\"");
-    add_s (ref);
-    add_s ("\" name=\"");
+    add_s ("<link linkend=\"");
+    sgml_id_fputs (ref);
+    add_s ("\">\n");
     if (output_dollar)
-      add_s ("&dollar;");
+      add_s ("$");
     sgml_fputs (ref);
-    add_s ("\">");
+    add_s ("</link>");
     break;
 
   default:
@@ -1225,62 +1134,63 @@ static int commit_buff (char *buff, char **d, int docstat)
 
 static int handle_docline (char *l, int docstat)
 {
-  char buff[BUFFSIZE];
+  char buff[BUFSIZ];
   char *s, *d;
 
   l = skip_ws (l);
 
-  if (Debug)
-    fprintf (stderr, "%s: handle_docline `%s'\n", Progname, l);
-
-  if (!strncmp (l, ".pp", 3))
+  if (!m_strncmp (l, ".pp", 3))
     return print_it (SP_NEWPAR, NULL, docstat);
-  else if (!strncmp (l, ".ts", 3))
+  else if (!m_strncmp (l, ".ts", 3))
     return print_it (SP_START_TAB, NULL, docstat);
-  else if (!strncmp (l, ".te", 3))
+  else if (!m_strncmp (l, ".te", 3))
     return print_it (SP_END_TAB, NULL, docstat);
-  else if (!strncmp (l, ".dl", 3))
+  else if (!m_strncmp (l, ".dl", 3))
     return print_it (SP_START_DL, NULL, docstat);
-  else if (!strncmp (l, ".de", 3))
+  else if (!m_strncmp (l, ".de", 3))
     return print_it (SP_END_DL, NULL, docstat);
-  else if (!strncmp (l, ". ", 2))
+  else if (!m_strncmp (l, ". ", 2))
     *l = ' ';
 
   for (s = l, d = buff; *s; s++) {
-    if (!strncmp (s, "\\(as", 4)) {
+    if (!m_strncmp (s, "\\(as", 4)) {
       *d++ = '*';
       s += 3;
     }
-    else if (!strncmp (s, "\\(rs", 4)) {
+    else if (!m_strncmp (s, "\\(rs", 4)) {
       *d++ = '\\';
       s += 3;
     }
-    else if (!strncmp (s, "\\fI", 3)) {
+    else if (!m_strncmp (s, "\\fI", 3)) {
       docstat = commit_buff (buff, &d, docstat);
       docstat = print_it (SP_START_EM, NULL, docstat);
       s += 2;
     }
-    else if (!strncmp (s, "\\fB", 3)) {
+    else if (!m_strncmp (s, "\\fB", 3)) {
       docstat = commit_buff (buff, &d, docstat);
       docstat = print_it (SP_START_BF, NULL, docstat);
       s += 2;
     }
-    else if (!strncmp (s, "\\fT", 3)) {
+    else if (!m_strncmp (s, "\\fT", 3)) {
       docstat = commit_buff (buff, &d, docstat);
       docstat = print_it (SP_START_TT, NULL, docstat);
       s += 2;
     }
-    else if (!strncmp (s, "\\fP", 3)) {
+    else if (!m_strncmp (s, "\\fP", 3)) {
       docstat = commit_buff (buff, &d, docstat);
       docstat = print_it (SP_END_FT, NULL, docstat);
       s += 2;
     }
-    else if (!strncmp (s, ".dt", 3)) {
+    else if (!m_strncmp (s, ".dt", 3)) {
+      if (docstat & D_DD) {
+        docstat = commit_buff (buff, &d, docstat);
+        docstat = print_it (SP_END_DD, NULL, docstat);
+      }
       docstat = commit_buff (buff, &d, docstat);
       docstat = print_it (SP_DT, NULL, docstat);
       s += 3;
     }
-    else if (!strncmp (s, ".dd", 3)) {
+    else if (!m_strncmp (s, ".dd", 3)) {
       docstat = commit_buff (buff, &d, docstat);
       docstat = print_it (SP_DD, NULL, docstat);
       s += 3;