+ return(1);
+}
+
+
+static int
+post_dt(struct mdoc *mdoc)
+{
+ struct mdoc_node *n;
+ const char *cp;
+ char *ep;
+ long lval;
+
+ if (mdoc->meta.title)
+ free(mdoc->meta.title);
+ if (mdoc->meta.vol)
+ free(mdoc->meta.vol);
+ if (mdoc->meta.arch)
+ free(mdoc->meta.arch);
+
+ mdoc->meta.title = mdoc->meta.vol = mdoc->meta.arch = NULL;
+ mdoc->meta.msec = 0;
+
+ /* Handles: `.Dt'
+ * --> title = unknown, volume = local, msec = 0, arch = NULL
+ */
+
+ if (NULL == (n = mdoc->last->child)) {
+ mdoc->meta.title = xstrdup("unknown");
+ mdoc->meta.vol = xstrdup("local");
+ mdoc_msg(mdoc, "title: %s", mdoc->meta.title);
+ mdoc_msg(mdoc, "volume: %s", mdoc->meta.vol);
+ mdoc_msg(mdoc, "arch: <unset>");
+ mdoc_msg(mdoc, "msec: <unset>");
+ return(post_prologue(mdoc));
+ }
+
+ /* Handles: `.Dt TITLE'
+ * --> title = TITLE, volume = local, msec = 0, arch = NULL
+ */
+
+ mdoc->meta.title = xstrdup(n->string);
+ mdoc_msg(mdoc, "title: %s", mdoc->meta.title);
+
+ if (NULL == (n = n->next)) {
+ mdoc->meta.vol = xstrdup("local");
+ mdoc_msg(mdoc, "volume: %s", mdoc->meta.vol);
+ mdoc_msg(mdoc, "arch: <unset>");
+ mdoc_msg(mdoc, "msec: %d", mdoc->meta.msec);
+ return(post_prologue(mdoc));
+ }
+
+ /* Handles: `.Dt TITLE SEC'
+ * --> title = TITLE, volume = SEC is msec ?
+ * format(msec) : SEC,
+ * msec = SEC is msec ? atoi(msec) : 0,
+ * arch = NULL
+ */
+
+ if ((cp = mdoc_a2msec(n->string))) {
+ mdoc->meta.vol = xstrdup(cp);
+ errno = 0;
+ lval = strtol(n->string, &ep, 10);
+ if (n->string[0] != '\0' && *ep == '\0')
+ mdoc->meta.msec = (int)lval;
+ } else
+ mdoc->meta.vol = xstrdup(n->string);
+
+ if (NULL == (n = n->next)) {
+ mdoc_msg(mdoc, "volume: %s", mdoc->meta.vol);
+ mdoc_msg(mdoc, "arch: <unset>");
+ mdoc_msg(mdoc, "msec: %d", mdoc->meta.msec);
+ return(post_prologue(mdoc));
+ }
+
+ /* Handles: `.Dt TITLE SEC VOL'
+ * --> title = TITLE, volume = VOL is vol ?
+ * format(VOL) :
+ * VOL is arch ? format(arch) :
+ * VOL
+ */
+
+ if ((cp = mdoc_a2vol(n->string))) {
+ free(mdoc->meta.vol);
+ mdoc->meta.vol = xstrdup(cp);
+ n = n->next;
+ } else {
+ cp = mdoc_a2arch(n->string);
+ if (NULL == cp) {
+ free(mdoc->meta.vol);
+ mdoc->meta.vol = xstrdup(n->string);
+ } else
+ mdoc->meta.arch = xstrdup(cp);
+ }
+
+ mdoc_msg(mdoc, "volume: %s", mdoc->meta.vol);
+ mdoc_msg(mdoc, "arch: %s", mdoc->meta.arch ?
+ mdoc->meta.arch : "<unset>");
+ mdoc_msg(mdoc, "msec: %d", mdoc->meta.msec);
+
+ /* Ignore any subsequent parameters... */
+
+ return(post_prologue(mdoc));
+}
+
+
+static int
+post_os(struct mdoc *mdoc)
+{
+ char buf[64];
+ struct utsname utsname;
+
+ if (mdoc->meta.os)
+ free(mdoc->meta.os);
+
+ (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf));
+
+ if (0 == buf[0]) {
+ if (-1 == uname(&utsname))
+ return(mdoc_err(mdoc, "utsname"));
+ (void)xstrlcpy(buf, utsname.sysname, sizeof(buf));
+ (void)xstrlcat(buf, " ", sizeof(buf));
+ (void)xstrlcat(buf, utsname.release, sizeof(buf));
+ }
+
+ mdoc->meta.os = xstrdup(buf);
+ mdoc_msg(mdoc, "system: %s", mdoc->meta.os);
+
+ mdoc->lastnamed = mdoc->lastsec = SEC_BODY;
+
+ return(post_prologue(mdoc));
+}
+
+
+static int
+post_bl_tagwidth(struct mdoc *mdoc)
+{
+ struct mdoc_node *n;
+ int sz;
+ char buf[32];
+
+ /*
+ * If -tag has been specified and -width has not been, then try
+ * to intuit our width from the first body element.
+ */
+
+ if (NULL == (n = mdoc->last->body->child))
+ return(1);
+
+ /*
+ * Use the text width, if a text node, or the default macro
+ * width if a macro.
+ */
+
+ if ((n = n->head->child)) {
+ if (MDOC_TEXT != n->type) {
+ if (0 == (sz = (int)mdoc_macro2len(n->tok)))
+ sz = -1;
+ } else
+ sz = (int)strlen(n->string) + 1;
+ } else
+ sz = -1;
+
+ if (-1 == sz) {
+ if ( ! mwarn(mdoc, WNOWIDTH))
+ return(0);
+ sz = 10;
+ }
+
+ (void)snprintf(buf, sizeof(buf), "%dn", sz);
+
+ /*
+ * We have to dynamically add this to the macro's argument list.
+ * We're guaranteed that a MDOC_Width doesn't already exist.
+ */
+
+ if (NULL == mdoc->last->args) {
+ mdoc->last->args = xcalloc
+ (1, sizeof(struct mdoc_arg));
+ mdoc->last->args->refcnt = 1;
+ }
+
+ n = mdoc->last;
+ sz = (int)n->args->argc;
+
+ (n->args->argc)++;
+
+ n->args->argv = xrealloc(n->args->argv,
+ n->args->argc * sizeof(struct mdoc_arg));
+
+ n->args->argv[sz - 1].arg = MDOC_Width;
+ n->args->argv[sz - 1].line = mdoc->last->line;
+ n->args->argv[sz - 1].pos = mdoc->last->pos;
+ n->args->argv[sz - 1].sz = 1;
+ n->args->argv[sz - 1].value = xcalloc(1, sizeof(char *));
+ n->args->argv[sz - 1].value[0] = xstrdup(buf);
+
+ mdoc_msg(mdoc, "adding %s argument: %s",
+ mdoc_argnames[MDOC_Width], buf);
+
+ return(1);
+}
+
+
+static int
+post_bl_width(struct mdoc *m)
+{
+ size_t width;
+ int i, tok;
+ char buf[32];
+ char *p;
+
+ if (NULL == m->last->args)
+ return(merr(m, ENOWIDTH));
+
+ for (i = 0; i < (int)m->last->args->argc; i++)
+ if (MDOC_Width == m->last->args->argv[i].arg)
+ break;
+
+ if (i == (int)m->last->args->argc)
+ return(merr(m, ENOWIDTH));
+
+ p = m->last->args->argv[i].value[0];
+
+ /*
+ * If the value to -width is a macro, then we re-write it to be
+ * the macro's width as set in share/tmac/mdoc/doc-common.
+ */
+
+ if (xstrcmp(p, "Ds"))
+ width = 8;
+ else if (MDOC_MAX == (tok = mdoc_tokhash_find(m->htab, p)))
+ return(1);
+ else if (0 == (width = mdoc_macro2len(tok)))
+ return(mwarn(m, WNOWIDTH));
+
+ mdoc_msg(m, "re-writing %s argument: %s -> %zun",
+ mdoc_argnames[MDOC_Width], p, width);
+
+ /* The value already exists: free and reallocate it. */
+
+ (void)snprintf(buf, sizeof(buf), "%zun", width);
+
+ free(m->last->args->argv[i].value[0]);
+ m->last->args->argv[i].value[0] = xstrdup(buf);
+
+ return(1);
+}
+
+
+static int
+post_bl(struct mdoc *mdoc)
+{
+ int i, r, len;
+
+ if (MDOC_BLOCK != mdoc->last->type)
+ return(1);