]> git.cameronkatri.com Git - mandoc.git/blobdiff - mdoc_validate.c
Simplify: write HTTP 303 redirects with relative locations.
[mandoc.git] / mdoc_validate.c
index be9b6f4ff3478198032a010e0d2adc0b120dc16a..d53187542e2ec265caeaa010c5b66e339db71b08 100644 (file)
@@ -1,4 +1,4 @@
-/*     $Id: mdoc_validate.c,v 1.313 2017/01/10 12:53:07 schwarze Exp $ */
+/*     $Id: mdoc_validate.c,v 1.319 2017/03/06 17:26:04 schwarze Exp $ */
 /*
  * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
  * Copyright (c) 2010-2017 Ingo Schwarze <schwarze@openbsd.org>
@@ -51,6 +51,7 @@ enum  check_ineq {
 
 typedef        void    (*v_post)(POST_ARGS);
 
+static int      build_list(struct roff_man *, int);
 static void     check_text(struct roff_man *, int, int, char *);
 static void     check_argv(struct roff_man *,
                        struct roff_node *, struct mdoc_argv *);
@@ -95,6 +96,7 @@ static        void     post_par(POST_ARGS);
 static void     post_prevpar(POST_ARGS);
 static void     post_root(POST_ARGS);
 static void     post_rs(POST_ARGS);
+static void     post_rv(POST_ARGS);
 static void     post_sh(POST_ARGS);
 static void     post_sh_head(POST_ARGS);
 static void     post_sh_name(POST_ARGS);
@@ -104,6 +106,7 @@ static      void     post_sm(POST_ARGS);
 static void     post_st(POST_ARGS);
 static void     post_std(POST_ARGS);
 static void     post_xr(POST_ARGS);
+static void     post_xx(POST_ARGS);
 
 static v_post mdoc_valids[MDOC_MAX] = {
        NULL,           /* Ap */
@@ -142,7 +145,7 @@ static      v_post mdoc_valids[MDOC_MAX] = {
        NULL,           /* Op */
        post_obsolete,  /* Ot */
        post_defaults,  /* Pa */
-       post_std,       /* Rv */
+       post_rv,        /* Rv */
        post_st,        /* St */
        NULL,           /* Va */
        NULL,           /* Vt */
@@ -166,7 +169,7 @@ static      v_post mdoc_valids[MDOC_MAX] = {
        post_bf,        /* Bf */
        NULL,           /* Bo */
        NULL,           /* Bq */
-       NULL,           /* Bsx */
+       post_xx,        /* Bsx */
        post_bx,        /* Bx */
        post_obsolete,  /* Db */
        NULL,           /* Dc */
@@ -176,12 +179,12 @@ static    v_post mdoc_valids[MDOC_MAX] = {
        NULL,           /* Ef */
        NULL,           /* Em */
        NULL,           /* Eo */
-       NULL,           /* Fx */
+       post_xx,        /* Fx */
        NULL,           /* Ms */
        NULL,           /* No */
        post_ns,        /* Ns */
-       NULL,           /* Nx */
-       NULL,           /* Ox */
+       post_xx,        /* Nx */
+       post_xx,        /* Ox */
        NULL,           /* Pc */
        NULL,           /* Pf */
        NULL,           /* Po */
@@ -199,7 +202,7 @@ static      v_post mdoc_valids[MDOC_MAX] = {
        post_hyph,      /* Sx */
        NULL,           /* Sy */
        NULL,           /* Tn */
-       NULL,           /* Ux */
+       post_xx,        /* Ux */
        NULL,           /* Xc */
        NULL,           /* Xo */
        post_fo,        /* Fo */
@@ -222,7 +225,7 @@ static      v_post mdoc_valids[MDOC_MAX] = {
        NULL,           /* %C */
        post_es,        /* Es */
        post_en,        /* En */
-       NULL,           /* Dx */
+       post_xx,        /* Dx */
        NULL,           /* %Q */
        post_par,       /* br */
        post_par,       /* sp */
@@ -317,9 +320,9 @@ mdoc_node_validate(struct roff_man *mdoc)
                 */
 
                if (n->child != NULL)
-                       n->child->flags &= ~MDOC_DELIMC;
+                       n->child->flags &= ~NODE_DELIMC;
                if (n->last != NULL)
-                       n->last->flags &= ~MDOC_DELIMO;
+                       n->last->flags &= ~NODE_DELIMO;
 
                /* Call the macro's postprocessor. */
 
@@ -620,6 +623,10 @@ post_bd(POST_ARGS)
        }
 }
 
+/*
+ * Stand-alone line macros.
+ */
+
 static void
 post_an_norm(POST_ARGS)
 {
@@ -647,6 +654,158 @@ post_an_norm(POST_ARGS)
                abort();
 }
 
+static void
+post_eoln(POST_ARGS)
+{
+       struct roff_node        *n;
+
+       n = mdoc->last;
+       if (n->child != NULL)
+               mandoc_vmsg(MANDOCERR_ARG_SKIP, mdoc->parse,
+                   n->line, n->pos, "%s %s",
+                   mdoc_macronames[n->tok], n->child->string);
+
+       while (n->child != NULL)
+               roff_node_delete(mdoc, n->child);
+
+       roff_word_alloc(mdoc, n->line, n->pos, n->tok == MDOC_Bt ?
+           "is currently in beta test." : "currently under development.");
+       mdoc->last->flags |= NODE_EOS | NODE_NOSRC;
+       mdoc->last = n;
+}
+
+static int
+build_list(struct roff_man *mdoc, int tok)
+{
+       struct roff_node        *n;
+       int                      ic;
+
+       n = mdoc->last->next;
+       for (ic = 1;; ic++) {
+               roff_elem_alloc(mdoc, n->line, n->pos, tok);
+               mdoc->last->flags |= NODE_NOSRC;
+               mdoc_node_relink(mdoc, n);
+               n = mdoc->last = mdoc->last->parent;
+               mdoc->next = ROFF_NEXT_SIBLING;
+               if (n->next == NULL)
+                       return ic;
+               if (ic > 1 || n->next->next != NULL) {
+                       roff_word_alloc(mdoc, n->line, n->pos, ",");
+                       mdoc->last->flags |= NODE_DELIMC | NODE_NOSRC;
+               }
+               n = mdoc->last->next;
+               if (n->next == NULL) {
+                       roff_word_alloc(mdoc, n->line, n->pos, "and");
+                       mdoc->last->flags |= NODE_NOSRC;
+               }
+       }
+}
+
+static void
+post_ex(POST_ARGS)
+{
+       struct roff_node        *n;
+       int                      ic;
+
+       post_std(mdoc);
+
+       n = mdoc->last;
+       mdoc->next = ROFF_NEXT_CHILD;
+       roff_word_alloc(mdoc, n->line, n->pos, "The");
+       mdoc->last->flags |= NODE_NOSRC;
+
+       if (mdoc->last->next != NULL)
+               ic = build_list(mdoc, MDOC_Nm);
+       else if (mdoc->meta.name != NULL) {
+               roff_elem_alloc(mdoc, n->line, n->pos, MDOC_Nm);
+               mdoc->last->flags |= NODE_NOSRC;
+               roff_word_alloc(mdoc, n->line, n->pos, mdoc->meta.name);
+               mdoc->last->flags |= NODE_NOSRC;
+               mdoc->last = mdoc->last->parent;
+               mdoc->next = ROFF_NEXT_SIBLING;
+               ic = 1;
+       } else {
+               mandoc_msg(MANDOCERR_EX_NONAME, mdoc->parse,
+                   n->line, n->pos, "Ex");
+               ic = 0;
+       }
+
+       roff_word_alloc(mdoc, n->line, n->pos,
+           ic > 1 ? "utilities exit\\~0" : "utility exits\\~0");
+       mdoc->last->flags |= NODE_NOSRC;
+       roff_word_alloc(mdoc, n->line, n->pos,
+           "on success, and\\~>0 if an error occurs.");
+       mdoc->last->flags |= NODE_EOS | NODE_NOSRC;
+       mdoc->last = n;
+}
+
+static void
+post_lb(POST_ARGS)
+{
+       struct roff_node        *n;
+       const char              *p;
+
+       n = mdoc->last;
+       assert(n->child->type == ROFFT_TEXT);
+       mdoc->next = ROFF_NEXT_CHILD;
+
+       if ((p = mdoc_a2lib(n->child->string)) != NULL) {
+               n->child->flags |= NODE_NOPRT;
+               roff_word_alloc(mdoc, n->line, n->pos, p);
+               mdoc->last->flags = NODE_NOSRC;
+               mdoc->last = n;
+               return;
+       }
+
+       roff_word_alloc(mdoc, n->line, n->pos, "library");
+       mdoc->last->flags = NODE_NOSRC;
+       roff_word_alloc(mdoc, n->line, n->pos, "\\(Lq");
+       mdoc->last->flags = NODE_DELIMO | NODE_NOSRC;
+       mdoc->last = mdoc->last->next;
+       roff_word_alloc(mdoc, n->line, n->pos, "\\(Rq");
+       mdoc->last->flags = NODE_DELIMC | NODE_NOSRC;
+       mdoc->last = n;
+}
+
+static void
+post_rv(POST_ARGS)
+{
+       struct roff_node        *n;
+       int                      ic;
+
+       post_std(mdoc);
+
+       n = mdoc->last;
+       mdoc->next = ROFF_NEXT_CHILD;
+       if (n->child != NULL) {
+               roff_word_alloc(mdoc, n->line, n->pos, "The");
+               mdoc->last->flags |= NODE_NOSRC;
+               ic = build_list(mdoc, MDOC_Fn);
+               roff_word_alloc(mdoc, n->line, n->pos,
+                   ic > 1 ? "functions return" : "function returns");
+               mdoc->last->flags |= NODE_NOSRC;
+               roff_word_alloc(mdoc, n->line, n->pos,
+                   "the value\\~0 if successful;");
+       } else
+               roff_word_alloc(mdoc, n->line, n->pos, "Upon successful "
+                   "completion, the value\\~0 is returned;");
+       mdoc->last->flags |= NODE_NOSRC;
+
+       roff_word_alloc(mdoc, n->line, n->pos, "otherwise "
+           "the value\\~\\-1 is returned and the global variable");
+       mdoc->last->flags |= NODE_NOSRC;
+       roff_elem_alloc(mdoc, n->line, n->pos, MDOC_Va);
+       mdoc->last->flags |= NODE_NOSRC;
+       roff_word_alloc(mdoc, n->line, n->pos, "errno");
+       mdoc->last->flags |= NODE_NOSRC;
+       mdoc->last = mdoc->last->parent;
+       mdoc->next = ROFF_NEXT_SIBLING;
+       roff_word_alloc(mdoc, n->line, n->pos,
+           "is set to indicate the error.");
+       mdoc->last->flags |= NODE_EOS | NODE_NOSRC;
+       mdoc->last = n;
+}
+
 static void
 post_std(POST_ARGS)
 {
@@ -661,6 +820,30 @@ post_std(POST_ARGS)
            n->line, n->pos, mdoc_macronames[n->tok]);
 }
 
+static void
+post_st(POST_ARGS)
+{
+       struct roff_node         *n, *nch;
+       const char               *p;
+
+       n = mdoc->last;
+       nch = n->child;
+       assert(nch->type == ROFFT_TEXT);
+
+       if ((p = mdoc_a2st(nch->string)) == NULL) {
+               mandoc_vmsg(MANDOCERR_ST_BAD, mdoc->parse,
+                   nch->line, nch->pos, "St %s", nch->string);
+               roff_node_delete(mdoc, n);
+               return;
+       }
+
+       nch->flags |= NODE_NOPRT;
+       mdoc->next = ROFF_NEXT_CHILD;
+       roff_word_alloc(mdoc, nch->line, nch->pos, p);
+       mdoc->last->flags |= NODE_NOSRC;
+       mdoc->last= n;
+}
+
 static void
 post_obsolete(POST_ARGS)
 {
@@ -672,6 +855,10 @@ post_obsolete(POST_ARGS)
                    n->line, n->pos, mdoc_macronames[n->tok]);
 }
 
+/*
+ * Block macros.
+ */
+
 static void
 post_bf(POST_ARGS)
 {
@@ -737,39 +924,6 @@ post_bf(POST_ARGS)
                    "Bf %s", np->child->string);
 }
 
-static void
-post_lb(POST_ARGS)
-{
-       struct roff_node        *n;
-       const char              *stdlibname;
-       char                    *libname;
-
-       n = mdoc->last->child;
-       assert(n->type == ROFFT_TEXT);
-
-       if (NULL == (stdlibname = mdoc_a2lib(n->string)))
-               mandoc_asprintf(&libname,
-                   "library \\(Lq%s\\(Rq", n->string);
-       else
-               libname = mandoc_strdup(stdlibname);
-
-       free(n->string);
-       n->string = libname;
-}
-
-static void
-post_eoln(POST_ARGS)
-{
-       const struct roff_node *n;
-
-       n = mdoc->last;
-       if (n->child != NULL)
-               mandoc_vmsg(MANDOCERR_ARG_SKIP,
-                   mdoc->parse, n->line, n->pos,
-                   "%s %s", mdoc_macronames[n->tok],
-                   n->child->string);
-}
-
 static void
 post_fname(POST_ARGS)
 {
@@ -859,6 +1013,16 @@ post_nm(POST_ARGS)
            (mdoc->lastsec == SEC_NAME && n->child == NULL))
                mandoc_msg(MANDOCERR_NM_NONAME, mdoc->parse,
                    n->line, n->pos, "Nm");
+
+       if ((n->type != ROFFT_ELEM && n->type != ROFFT_HEAD) ||
+           (n->child != NULL && n->child->type == ROFFT_TEXT) ||
+           mdoc->meta.name == NULL)
+               return;
+
+       mdoc->next = ROFF_NEXT_CHILD;
+       roff_word_alloc(mdoc, n->line, n->pos, mdoc->meta.name);
+       mdoc->last->flags |= NODE_NOSRC;
+       mdoc->last = n;
 }
 
 static void
@@ -871,6 +1035,10 @@ post_nd(POST_ARGS)
        if (n->type != ROFFT_BODY)
                return;
 
+       if (n->sec != SEC_NAME)
+               mandoc_msg(MANDOCERR_ND_LATE, mdoc->parse,
+                   n->line, n->pos, "Nd");
+
        if (n->child == NULL)
                mandoc_msg(MANDOCERR_ND_EMPTY, mdoc->parse,
                    n->line, n->pos, "Nd");
@@ -964,17 +1132,11 @@ post_defaults(POST_ARGS)
 static void
 post_at(POST_ARGS)
 {
-       struct roff_node        *n;
-       const char              *std_att;
-       char                    *att;
+       struct roff_node        *n, *nch;
+       const char              *att;
 
        n = mdoc->last;
-       if (n->child == NULL) {
-               mdoc->next = ROFF_NEXT_CHILD;
-               roff_word_alloc(mdoc, n->line, n->pos, "AT&T UNIX");
-               mdoc->last = n;
-               return;
-       }
+       nch = n->child;
 
        /*
         * If we have a child, look it up in the standard keys.  If a
@@ -982,17 +1144,19 @@ post_at(POST_ARGS)
         * prefix "AT&T UNIX " to the existing data.
         */
 
-       n = n->child;
-       assert(n->type == ROFFT_TEXT);
-       if ((std_att = mdoc_a2att(n->string)) == NULL) {
+       att = NULL;
+       if (nch != NULL && ((att = mdoc_a2att(nch->string)) == NULL))
                mandoc_vmsg(MANDOCERR_AT_BAD, mdoc->parse,
-                   n->line, n->pos, "At %s", n->string);
-               mandoc_asprintf(&att, "AT&T UNIX %s", n->string);
-       } else
-               att = mandoc_strdup(std_att);
+                   nch->line, nch->pos, "At %s", nch->string);
 
-       free(n->string);
-       n->string = att;
+       mdoc->next = ROFF_NEXT_CHILD;
+       if (att != NULL) {
+               roff_word_alloc(mdoc, nch->line, nch->pos, att);
+               nch->flags |= NODE_NOPRT;
+       } else
+               roff_word_alloc(mdoc, n->line, n->pos, "AT&T UNIX");
+       mdoc->last->flags |= NODE_NOSRC;
+       mdoc->last = n;
 }
 
 static void
@@ -1030,6 +1194,41 @@ post_es(POST_ARGS)
        mdoc->last_es = mdoc->last;
 }
 
+static void
+post_xx(POST_ARGS)
+{
+       struct roff_node        *n;
+       const char              *os;
+
+       n = mdoc->last;
+       switch (n->tok) {
+       case MDOC_Bsx:
+               os = "BSD/OS";
+               break;
+       case MDOC_Dx:
+               os = "DragonFly";
+               break;
+       case MDOC_Fx:
+               os = "FreeBSD";
+               break;
+       case MDOC_Nx:
+               os = "NetBSD";
+               break;
+       case MDOC_Ox:
+               os = "OpenBSD";
+               break;
+       case MDOC_Ux:
+               os = "UNIX";
+               break;
+       default:
+               abort();
+       }
+       mdoc->next = ROFF_NEXT_CHILD;
+       roff_word_alloc(mdoc, n->line, n->pos, os);
+       mdoc->last->flags |= NODE_NOSRC;
+       mdoc->last = n;
+}
+
 static void
 post_it(POST_ARGS)
 {
@@ -1416,27 +1615,6 @@ post_root(POST_ARGS)
                    n->line, n->pos, mdoc_macronames[n->tok]);
 }
 
-static void
-post_st(POST_ARGS)
-{
-       struct roff_node         *n, *nch;
-       const char               *p;
-
-       n = mdoc->last;
-       nch = n->child;
-
-       assert(nch->type == ROFFT_TEXT);
-
-       if ((p = mdoc_a2st(nch->string)) == NULL) {
-               mandoc_vmsg(MANDOCERR_ST_BAD, mdoc->parse,
-                   nch->line, nch->pos, "St %s", nch->string);
-               roff_node_delete(mdoc, n);
-       } else {
-               free(nch->string);
-               nch->string = mandoc_strdup(p);
-       }
-}
-
 static void
 post_rs(POST_ARGS)
 {
@@ -1555,7 +1733,7 @@ static void
 post_ns(POST_ARGS)
 {
 
-       if (mdoc->last->flags & MDOC_LINE)
+       if (mdoc->last->flags & NODE_LINE)
                mandoc_msg(MANDOCERR_NS_SKIP, mdoc->parse,
                    mdoc->last->line, mdoc->last->pos, NULL);
 }
@@ -2076,7 +2254,36 @@ post_dt(POST_ARGS)
 static void
 post_bx(POST_ARGS)
 {
-       struct roff_node        *n;
+       struct roff_node        *n, *nch;
+
+       n = mdoc->last;
+       nch = n->child;
+
+       if (nch != NULL) {
+               mdoc->last = nch;
+               nch = nch->next;
+               mdoc->next = ROFF_NEXT_SIBLING;
+               roff_elem_alloc(mdoc, n->line, n->pos, MDOC_Ns);
+               mdoc->last->flags |= NODE_NOSRC;
+               mdoc->next = ROFF_NEXT_SIBLING;
+       } else
+               mdoc->next = ROFF_NEXT_CHILD;
+       roff_word_alloc(mdoc, n->line, n->pos, "BSD");
+       mdoc->last->flags |= NODE_NOSRC;
+
+       if (nch == NULL) {
+               mdoc->last = n;
+               return;
+       }
+
+       roff_elem_alloc(mdoc, n->line, n->pos, MDOC_Ns);
+       mdoc->last->flags |= NODE_NOSRC;
+       mdoc->next = ROFF_NEXT_SIBLING;
+       roff_word_alloc(mdoc, n->line, n->pos, "-");
+       mdoc->last->flags |= NODE_NOSRC;
+       roff_elem_alloc(mdoc, n->line, n->pos, MDOC_Ns);
+       mdoc->last->flags |= NODE_NOSRC;
+       mdoc->last = n;
 
        /*
         * Make `Bx's second argument always start with an uppercase
@@ -2084,8 +2291,7 @@ post_bx(POST_ARGS)
         * uppercase blindly.
         */
 
-       if ((n = mdoc->last->child) != NULL && (n = n->next) != NULL)
-               *n->string = (char)toupper((unsigned char)*n->string);
+       *nch->string = (char)toupper((unsigned char)*nch->string);
 }
 
 static void
@@ -2143,32 +2349,6 @@ post_os(POST_ARGS)
 #endif /*!OSNAME*/
 }
 
-/*
- * If no argument is provided,
- * fill in the name of the current manual page.
- */
-static void
-post_ex(POST_ARGS)
-{
-       struct roff_node *n;
-
-       post_std(mdoc);
-
-       n = mdoc->last;
-       if (n->child != NULL)
-               return;
-
-       if (mdoc->meta.name == NULL) {
-               mandoc_msg(MANDOCERR_EX_NONAME, mdoc->parse,
-                   n->line, n->pos, "Ex");
-               return;
-       }
-
-       mdoc->next = ROFF_NEXT_CHILD;
-       roff_word_alloc(mdoc, n->line, n->pos, mdoc->meta.name);
-       mdoc->last = n;
-}
-
 enum roff_sec
 mdoc_a2sec(const char *p)
 {