]> git.cameronkatri.com Git - mandoc.git/blobdiff - term.c
More fixes (hard-escapes, etc.).
[mandoc.git] / term.c
diff --git a/term.c b/term.c
index 5fd868dda15b75911c2d814f99de9d6ec8456e88..66847ea245afaddd05e6a1b817be88ecefc26881 100644 (file)
--- a/term.c
+++ b/term.c
@@ -1,4 +1,4 @@
-/* $Id: term.c,v 1.21 2009/02/25 15:12:26 kristaps Exp $ */
+/* $Id: term.c,v 1.29 2009/02/28 20:13:06 kristaps Exp $ */
 /*
  * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
  *
  * PERFORMANCE OF THIS SOFTWARE.
  */
 #include <assert.h>
+#include <ctype.h>
+#include <err.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "term.h"
 
-#define        INDENT            4
+#define        INDENT            6
 
 /*
  * Performs actions on nodes of the abstract syntax tree.  Both pre- and
@@ -105,6 +108,7 @@ static      void              name##_post(DECL_ARGS)
 DECL_PRE(name); \
 DECL_POST(name);
 
+DECL_PREPOST(termp__t);
 DECL_PREPOST(termp_aq);
 DECL_PREPOST(termp_bd);
 DECL_PREPOST(termp_bq);
@@ -147,7 +151,9 @@ DECL_PRE(termp_nx);
 DECL_PRE(termp_ox);
 DECL_PRE(termp_pa);
 DECL_PRE(termp_pp);
+DECL_PRE(termp_rs);
 DECL_PRE(termp_rv);
+DECL_PRE(termp_sm);
 DECL_PRE(termp_st);
 DECL_PRE(termp_sx);
 DECL_PRE(termp_sy);
@@ -156,6 +162,7 @@ DECL_PRE(termp_ux);
 DECL_PRE(termp_va);
 DECL_PRE(termp_xr);
 
+DECL_POST(termp___);
 DECL_POST(termp_bl);
 
 const  struct termact __termacts[MDOC_MAX] = {
@@ -167,7 +174,7 @@ const       struct termact __termacts[MDOC_MAX] = {
        { termp_ss_pre, termp_ss_post }, /* Ss */ 
        { termp_pp_pre, NULL }, /* Pp */ 
        { termp_d1_pre, termp_d1_post }, /* D1 */
-       { NULL, NULL }, /* Dl */
+       { termp_d1_pre, termp_d1_post }, /* Dl */
        { termp_bd_pre, termp_bd_post }, /* Bd */
        { NULL, NULL }, /* Ed */
        { NULL, termp_bl_post }, /* Bl */
@@ -200,17 +207,17 @@ const     struct termact __termacts[MDOC_MAX] = {
        { termp_va_pre, NULL }, /* Va */
        { termp_vt_pre, termp_vt_post }, /* Vt */ 
        { termp_xr_pre, NULL }, /* Xr */
-       { NULL, NULL }, /* %A */
-       { NULL, NULL }, /* %B */
-       { NULL, NULL }, /* %D */
-       { NULL, NULL }, /* %I */
-       { NULL, NULL }, /* %J */
-       { NULL, NULL }, /* %N */
-       { NULL, NULL }, /* %O */
-       { NULL, NULL }, /* %P */
-       { NULL, NULL }, /* %R */
-       { NULL, NULL }, /* %T */
-       { NULL, NULL }, /* %V */
+       { NULL, termp____post }, /* %A */
+       { NULL, termp____post }, /* %B */
+       { NULL, termp____post }, /* %D */
+       { NULL, termp____post }, /* %I */
+       { NULL, termp____post }, /* %J */
+       { NULL, termp____post }, /* %N */
+       { NULL, termp____post }, /* %O */
+       { NULL, termp____post }, /* %P */
+       { NULL, termp____post }, /* %R */
+       { termp__t_pre, termp__t_post }, /* %T */
+       { NULL, termp____post }, /* %V */
        { NULL, NULL }, /* Ac */
        { termp_aq_pre, termp_aq_post }, /* Ao */
        { termp_aq_pre, termp_aq_post }, /* Aq */
@@ -244,11 +251,11 @@ const     struct termact __termacts[MDOC_MAX] = {
        { termp_qq_pre, termp_qq_post }, /* Qo */
        { termp_qq_pre, termp_qq_post }, /* Qq */
        { NULL, NULL }, /* Re */
-       { NULL, NULL }, /* Rs */
+       { termp_rs_pre, NULL }, /* Rs */
        { NULL, NULL }, /* Sc */
        { termp_sq_pre, termp_sq_post }, /* So */
        { termp_sq_pre, termp_sq_post }, /* Sq */
-       { NULL, NULL }, /* Sm */
+       { termp_sm_pre, NULL }, /* Sm */
        { termp_sx_pre, NULL }, /* Sx */
        { termp_sy_pre, NULL }, /* Sy */
        { NULL, NULL }, /* Tn */
@@ -273,10 +280,30 @@ const struct termact *termacts = __termacts;
 static size_t
 arg_width(const struct mdoc_arg *arg)
 {
+       size_t           v;
+       int              i, len;
 
-       /* TODO */
        assert(*arg->value);
-       return(strlen(*arg->value));
+       if (0 == strcmp(*arg->value, "indent"))
+               return(INDENT);
+       if (0 == strcmp(*arg->value, "indent-two"))
+               return(INDENT * 2);
+
+       len = (int)strlen(*arg->value);
+       assert(len > 0);
+
+       for (i = 0; i < len - 1; i++) 
+               if ( ! isdigit((int)(*arg->value)[i]))
+                       break;
+
+       if (i == len - 1) {
+               if ('n' == (*arg->value)[len - 1]) {
+                       v = (size_t)atoi(*arg->value);
+                       return(v);
+               }
+
+       }
+       return(strlen(*arg->value) + 2);
 }
 
 
@@ -290,7 +317,6 @@ arg_offset(const struct mdoc_arg *arg)
                return(INDENT);
        if (0 == strcmp(*arg->value, "indent-two"))
                return(INDENT * 2);
-
        return(strlen(*arg->value));
 }
 
@@ -348,8 +374,8 @@ termp_it_pre(DECL_ARGS)
 {
        const struct mdoc_node *n, *it;
        const struct mdoc_block *bl;
-       char             buf[7];
-       int              i;
+       char             buf[7], *tp;
+       int              i, type;
        size_t           width, offset;
 
        switch (node->type) {
@@ -369,86 +395,145 @@ termp_it_pre(DECL_ARGS)
        bl = &n->data.block;
 
        if (MDOC_BLOCK == node->type) {
-               if (arg_hasattr(MDOC_Compact, bl->argc, bl->argv))
-                       newln(p);
-               else
-                       vspace(p);
+               newln(p);
+               if ( ! arg_hasattr(MDOC_Compact, bl->argc, bl->argv))
+                       if (node->prev || n->prev)
+                               vspace(p);
                return(1);
        }
 
+       /* Get our list type. */
+
+       for (type = -1, i = 0; i < (int)bl->argc; i++) 
+               switch (bl->argv[i].arg) {
+               case (MDOC_Bullet):
+                       /* FALLTHROUGH */
+               case (MDOC_Dash):
+                       /* FALLTHROUGH */
+               case (MDOC_Enum):
+                       /* FALLTHROUGH */
+               case (MDOC_Hyphen):
+                       /* FALLTHROUGH */
+               case (MDOC_Tag):
+                       /* FALLTHROUGH */
+               case (MDOC_Ohang):
+                       type = bl->argv[i].arg;
+                       i = (int)bl->argc;
+                       break;
+               default:
+                       errx(1, "list type not supported");
+                       /* NOTREACHED */
+               }
+
+       assert(-1 != type);
+
+       /* Save our existing (inherited) margin and offset. */
+
        pair->offset = p->offset;
        pair->rmargin = p->rmargin;
 
-       /* FIXME: auto-size. */
+       /* Get list width and offset. */
+
        i = arg_getattr(MDOC_Width, bl->argc, bl->argv);
-       width = i >= 0 ? arg_width(&bl->argv[i]) : 10;
+       width = i >= 0 ? arg_width(&bl->argv[i]) : 0;
 
        i = arg_getattr(MDOC_Offset, bl->argc, bl->argv);
        offset = i >= 0 ? arg_offset(&bl->argv[i]) : 0;
 
-       assert(MDOC_HEAD == node->type || 
-                       MDOC_BODY == node->type);
-
-       if (arg_hasattr(MDOC_Tag, bl->argc, bl->argv)) {
-               p->flags |= TERMP_NOSPACE;
-               if (MDOC_BODY == node->type) {
-                       p->flags |= TERMP_NOLPAD;
-                       p->offset += width;
-               } else  {
-                       p->flags |= TERMP_NOBREAK;
-                       p->rmargin = p->offset + offset + width;
-               }
+       /* Override the width. */
 
-       } else if (arg_hasattr(MDOC_Ohang, bl->argc, bl->argv)) {
-               p->flags |= TERMP_NOSPACE;
+       switch (type) {
+       case (MDOC_Bullet):
+               /* FALLTHROUGH */
+       case (MDOC_Dash):
+               /* FALLTHROUGH */
+       case (MDOC_Enum):
+               /* FALLTHROUGH */
+       case (MDOC_Hyphen):
+               width = width > 6 ? width : 6;
+               break;
+       case (MDOC_Tag):
+               /* FIXME: auto-size. */
+               if (0 == width)
+                       errx(1, "need non-zero -width");
+               break;
+       default:
+               break;
+       }
 
-       } else if (arg_hasattr(MDOC_Diag, bl->argc, bl->argv)) {
-               /* TODO. */
+       /* Word-wrap control. */
 
-       } else if (arg_hasattr(MDOC_Hang, bl->argc, bl->argv)) {
-               /* TODO. */
+       p->flags |= TERMP_NOSPACE;
 
-       } else if (arg_hasattr(MDOC_Bullet, bl->argc, bl->argv)) {
-               p->flags |= TERMP_NOSPACE;
-               if (MDOC_BODY == node->type) {
-                       p->flags |= TERMP_NOLPAD;
-                       p->offset += 6;
-               } else {
-                       word(p, "\\[bu]");
+       switch (type) {
+       case (MDOC_Bullet):
+               /* FALLTHROUGH */
+       case (MDOC_Dash):
+               /* FALLTHROUGH */
+       case (MDOC_Enum):
+               /* FALLTHROUGH */
+       case (MDOC_Hyphen):
+               /* FALLTHROUGH */
+       case (MDOC_Tag):
+               if (MDOC_HEAD == node->type)
                        p->flags |= TERMP_NOBREAK;
-                       p->rmargin = p->offset + offset + 6;
-               }
-
-       } else if (arg_hasattr(MDOC_Enum, bl->argc, bl->argv)) {
-               p->flags |= TERMP_NOSPACE;
-               if (MDOC_BODY == node->type) {
+               else if (MDOC_BODY == node->type)
                        p->flags |= TERMP_NOLPAD;
-                       p->offset += 6;
-               } else {
+               break;
+       default:
+               break;
+       }
+
+       /* 
+        * Get a token to use as the HEAD lead-in.  If NULL, we use the
+        * HEAD child. 
+        */
+
+       tp = NULL;
+
+       if (MDOC_HEAD == node->type) {
+               if (arg_hasattr(MDOC_Bullet, bl->argc, bl->argv))
+                       tp = "\\[bu]";
+               if (arg_hasattr(MDOC_Dash, bl->argc, bl->argv))
+                       tp = "\\-";
+               if (arg_hasattr(MDOC_Enum, bl->argc, bl->argv)) {
                        (pair->ppair->ppair->count)++;
                        (void)snprintf(buf, sizeof(buf), "%d.", 
                                        pair->ppair->ppair->count);
-                       word(p, buf);
-                       p->flags |= TERMP_NOBREAK;
-                       p->rmargin = p->offset + offset + 6;
+                       tp = buf;
                }
+               if (arg_hasattr(MDOC_Hyphen, bl->argc, bl->argv))
+                       tp = "\\-";
+       }
 
-       } else if (arg_hasattr(MDOC_Dash, bl->argc, bl->argv) ||
-                       arg_hasattr(MDOC_Hyphen, bl->argc, bl->argv)) {
-               p->flags |= TERMP_NOSPACE;
-               if (MDOC_BODY == node->type) {
-                       p->flags |= TERMP_NOLPAD;
-                       p->offset += 6;
-                       return(1);
-               } else {
-                       word(p, "\\-");
-                       p->flags |= TERMP_NOBREAK;
-                       p->rmargin = p->offset + offset + 6;
-               }
-       } 
+       /* Margin control. */
 
        p->offset += offset;
-       return(1);
+
+       switch (type) {
+       case (MDOC_Bullet):
+               /* FALLTHROUGH */
+       case (MDOC_Dash):
+               /* FALLTHROUGH */
+       case (MDOC_Enum):
+               /* FALLTHROUGH */
+       case (MDOC_Hyphen):
+               /* FALLTHROUGH */
+       case (MDOC_Tag):
+               if (MDOC_HEAD == node->type)
+                       p->rmargin = p->offset + width;
+               else if (MDOC_BODY == node->type) 
+                       p->offset += width;
+               break;
+       default:
+               break;
+       }
+
+       if (NULL == tp)
+               return(1);
+
+       word(p, tp);
+       return(0);
 }
 
 
@@ -456,41 +541,20 @@ termp_it_pre(DECL_ARGS)
 static void
 termp_it_post(DECL_ARGS)
 {
-       const struct mdoc_node *n, *it;
-       const struct mdoc_block *bl;
 
        if (MDOC_BODY != node->type && MDOC_HEAD != node->type)
                return;
 
-       it = node->parent;
-       n = it->parent->parent;
-       bl = &n->data.block;
-
-       if (arg_hasattr(MDOC_Tag, bl->argc, bl->argv) ||
-                       arg_hasattr(MDOC_Bullet, bl->argc, bl->argv) ||
-                       arg_hasattr(MDOC_Dash, bl->argc, bl->argv) ||
-                       arg_hasattr(MDOC_Enum, bl->argc, bl->argv) ||
-                       arg_hasattr(MDOC_Hyphen, bl->argc, bl->argv)) {
-               flushln(p);
-               if (MDOC_HEAD == node->type) {
-                       p->rmargin = pair->rmargin;
-                       p->flags &= ~TERMP_NOBREAK;
-               } else
-                       p->flags &= ~TERMP_NOLPAD;
-
-       } else if (arg_hasattr(MDOC_Ohang, bl->argc, bl->argv)) {
-               flushln(p);
-
-       } else if (arg_hasattr(MDOC_Inset, bl->argc, bl->argv)) {
-               if (MDOC_BODY == node->type)
-                       flushln(p);
-
-       } else if (arg_hasattr(MDOC_Item, bl->argc, bl->argv)) {
-               if (MDOC_BODY == node->type)
-                       flushln(p);
-       }
+       flushln(p);
 
        p->offset = pair->offset;
+       p->rmargin = pair->rmargin;
+
+       if (MDOC_HEAD == node->type) {
+               p->flags &= ~TERMP_NOBREAK;
+               p->flags &= ~TERMP_NORPAD;
+       } else if (MDOC_BODY == node->type)
+               p->flags &= ~TERMP_NOLPAD;
 }
 
 
@@ -499,6 +563,9 @@ static int
 termp_nm_pre(DECL_ARGS)
 {
 
+       if (SEC_SYNOPSIS == node->sec)
+               newln(p);
+
        TERMPAIR_SETFLAG(pair, ttypes[TTYPE_PROG]);
        if (NULL == node->child)
                word(p, meta->name);
@@ -523,9 +590,14 @@ static int
 termp_ar_pre(DECL_ARGS)
 {
 
-       TERMPAIR_SETFLAG(pair, ttypes[TTYPE_CMD_ARG]);
-       if (NULL == node->child)
-               word(p, "...");
+       if (node->child) {
+               TERMPAIR_SETFLAG(pair, ttypes[TTYPE_CMD_ARG]);
+               return(1);
+       }
+       p->flags |= ttypes[TTYPE_CMD_ARG];
+       word(p, "file");
+       word(p, "...");
+       p->flags &= ~ttypes[TTYPE_CMD_ARG];
        return(1);
 }
 
@@ -565,6 +637,17 @@ termp_st_pre(DECL_ARGS)
 }
 
 
+/* ARGSUSED */
+static int
+termp_rs_pre(DECL_ARGS)
+{
+
+       if (MDOC_BLOCK == node->type)
+               vspace(p);
+       return(1);
+}
+
+
 /* ARGSUSED */
 static int
 termp_rv_pre(DECL_ARGS)
@@ -1112,7 +1195,7 @@ termp_sq_pre(DECL_ARGS)
 
        if (MDOC_BODY != node->type)
                return(1);
-       word(p, "\'");
+       word(p, "`");
        p->flags |= TERMP_NOSPACE;
        return(1);
 }
@@ -1269,7 +1352,7 @@ termp_bq_pre(DECL_ARGS)
 
        if (MDOC_BODY != node->type)
                return(1);
-       word(p, "[");
+       word(p, "\\[");
        p->flags |= TERMP_NOSPACE;
        return(1);
 }
@@ -1293,7 +1376,7 @@ termp_pq_pre(DECL_ARGS)
 
        if (MDOC_BODY != node->type)
                return(1);
-       word(p, "(");
+       word(p, "\\&(");
        p->flags |= TERMP_NOSPACE;
        return(1);
 }
@@ -1404,3 +1487,55 @@ termp_ms_pre(DECL_ARGS)
        return(1);
 }
 
+
+
+/* ARGSUSED */
+static int
+termp_sm_pre(DECL_ARGS)
+{
+
+#if notyet
+       assert(node->child);
+       if (0 == strcmp("off", node->child->data.text.string)) {
+               p->flags &= ~TERMP_NONOSPACE;
+               p->flags &= ~TERMP_NOSPACE;
+       } else {
+               p->flags |= TERMP_NONOSPACE;
+               p->flags |= TERMP_NOSPACE;
+       }
+#endif
+
+       return(0);
+}
+
+
+/* ARGSUSED */
+static int
+termp__t_pre(DECL_ARGS)
+{
+
+       word(p, "\"");
+       p->flags |= TERMP_NOSPACE;
+       return(1);
+}
+
+
+/* ARGSUSED */
+static void
+termp__t_post(DECL_ARGS)
+{
+
+       p->flags |= TERMP_NOSPACE;
+       word(p, "\"");
+       word(p, node->next ? "," : ".");
+}
+
+
+/* ARGSUSED */
+static void
+termp____post(DECL_ARGS)
+{
+
+       p->flags |= TERMP_NOSPACE;
+       word(p, node->next ? "," : ".");
+}