Annotation of src/usr.bin/mandoc/mdoc_action.c, Revision 1.1
1.1 ! kristaps 1: /* $Id: mdoc_action.c,v 1.6 2009/04/06 08:53:12 kristaps Exp $ */
! 2: /*
! 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
! 4: *
! 5: * Permission to use, copy, modify, and distribute this software for any
! 6: * purpose with or without fee is hereby granted, provided that the
! 7: * above copyright notice and this permission notice appear in all
! 8: * copies.
! 9: *
! 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
! 11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
! 12: * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
! 13: * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
! 14: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
! 15: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
! 16: * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
! 17: * PERFORMANCE OF THIS SOFTWARE.
! 18: */
! 19: #include <sys/utsname.h>
! 20:
! 21: #include <assert.h>
! 22: #include <errno.h>
! 23: #include <stdio.h>
! 24: #include <stdlib.h>
! 25: #include <string.h>
! 26:
! 27: #include "libmdoc.h"
! 28:
! 29: enum mwarn {
! 30: WBADSEC,
! 31: WNOWIDTH,
! 32: WBADDATE
! 33: };
! 34:
! 35: enum merr {
! 36: ETOOLONG,
! 37: EMALLOC,
! 38: ENUMFMT
! 39: };
! 40:
! 41: #define PRE_ARGS struct mdoc *m, const struct mdoc_node *n
! 42: #define POST_ARGS struct mdoc *m
! 43:
! 44: struct actions {
! 45: int (*pre)(PRE_ARGS);
! 46: int (*post)(POST_ARGS);
! 47: };
! 48:
! 49: static int pwarn(struct mdoc *, int, int, enum mwarn);
! 50: static int perr(struct mdoc *, int, int, enum merr);
! 51: static int concat(struct mdoc *, const struct mdoc_node *,
! 52: char *, size_t);
! 53:
! 54: static int post_ar(POST_ARGS);
! 55: static int post_bl(POST_ARGS);
! 56: static int post_bl_width(POST_ARGS);
! 57: static int post_bl_tagwidth(POST_ARGS);
! 58: static int post_dd(POST_ARGS);
! 59: static int post_display(POST_ARGS);
! 60: static int post_dt(POST_ARGS);
! 61: static int post_nm(POST_ARGS);
! 62: static int post_os(POST_ARGS);
! 63: static int post_prol(POST_ARGS);
! 64: static int post_sh(POST_ARGS);
! 65: static int post_std(POST_ARGS);
! 66:
! 67: static int pre_bd(PRE_ARGS);
! 68: static int pre_dl(PRE_ARGS);
! 69:
! 70: #define vwarn(m, t) pwarn((m), (m)->last->line, (m)->last->pos, (t))
! 71: #define verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t))
! 72: #define nerr(m, n, t) perr((m), (n)->line, (n)->pos, (t))
! 73:
! 74: const struct actions mdoc_actions[MDOC_MAX] = {
! 75: { NULL, NULL }, /* \" */
! 76: { NULL, post_dd }, /* Dd */
! 77: { NULL, post_dt }, /* Dt */
! 78: { NULL, post_os }, /* Os */
! 79: { NULL, post_sh }, /* Sh */
! 80: { NULL, NULL }, /* Ss */
! 81: { NULL, NULL }, /* Pp */
! 82: { NULL, NULL }, /* D1 */
! 83: { pre_dl, post_display }, /* Dl */
! 84: { pre_bd, post_display }, /* Bd */
! 85: { NULL, NULL }, /* Ed */
! 86: { NULL, post_bl }, /* Bl */
! 87: { NULL, NULL }, /* El */
! 88: { NULL, NULL }, /* It */
! 89: { NULL, NULL }, /* Ad */
! 90: { NULL, NULL }, /* An */
! 91: { NULL, post_ar }, /* Ar */
! 92: { NULL, NULL }, /* Cd */
! 93: { NULL, NULL }, /* Cm */
! 94: { NULL, NULL }, /* Dv */
! 95: { NULL, NULL }, /* Er */
! 96: { NULL, NULL }, /* Ev */
! 97: { NULL, post_std }, /* Ex */
! 98: { NULL, NULL }, /* Fa */
! 99: { NULL, NULL }, /* Fd */
! 100: { NULL, NULL }, /* Fl */
! 101: { NULL, NULL }, /* Fn */
! 102: { NULL, NULL }, /* Ft */
! 103: { NULL, NULL }, /* Ic */
! 104: { NULL, NULL }, /* In */
! 105: { NULL, NULL }, /* Li */
! 106: { NULL, NULL }, /* Nd */
! 107: { NULL, post_nm }, /* Nm */
! 108: { NULL, NULL }, /* Op */
! 109: { NULL, NULL }, /* Ot */
! 110: { NULL, NULL }, /* Pa */
! 111: { NULL, post_std }, /* Rv */
! 112: { NULL, NULL }, /* St */
! 113: { NULL, NULL }, /* Va */
! 114: { NULL, NULL }, /* Vt */
! 115: { NULL, NULL }, /* Xr */
! 116: { NULL, NULL }, /* %A */
! 117: { NULL, NULL }, /* %B */
! 118: { NULL, NULL }, /* %D */
! 119: { NULL, NULL }, /* %I */
! 120: { NULL, NULL }, /* %J */
! 121: { NULL, NULL }, /* %N */
! 122: { NULL, NULL }, /* %O */
! 123: { NULL, NULL }, /* %P */
! 124: { NULL, NULL }, /* %R */
! 125: { NULL, NULL }, /* %T */
! 126: { NULL, NULL }, /* %V */
! 127: { NULL, NULL }, /* Ac */
! 128: { NULL, NULL }, /* Ao */
! 129: { NULL, NULL }, /* Aq */
! 130: { NULL, NULL }, /* At */
! 131: { NULL, NULL }, /* Bc */
! 132: { NULL, NULL }, /* Bf */
! 133: { NULL, NULL }, /* Bo */
! 134: { NULL, NULL }, /* Bq */
! 135: { NULL, NULL }, /* Bsx */
! 136: { NULL, NULL }, /* Bx */
! 137: { NULL, NULL }, /* Db */
! 138: { NULL, NULL }, /* Dc */
! 139: { NULL, NULL }, /* Do */
! 140: { NULL, NULL }, /* Dq */
! 141: { NULL, NULL }, /* Ec */
! 142: { NULL, NULL }, /* Ef */
! 143: { NULL, NULL }, /* Em */
! 144: { NULL, NULL }, /* Eo */
! 145: { NULL, NULL }, /* Fx */
! 146: { NULL, NULL }, /* Ms */
! 147: { NULL, NULL }, /* No */
! 148: { NULL, NULL }, /* Ns */
! 149: { NULL, NULL }, /* Nx */
! 150: { NULL, NULL }, /* Ox */
! 151: { NULL, NULL }, /* Pc */
! 152: { NULL, NULL }, /* Pf */
! 153: { NULL, NULL }, /* Po */
! 154: { NULL, NULL }, /* Pq */
! 155: { NULL, NULL }, /* Qc */
! 156: { NULL, NULL }, /* Ql */
! 157: { NULL, NULL }, /* Qo */
! 158: { NULL, NULL }, /* Qq */
! 159: { NULL, NULL }, /* Re */
! 160: { NULL, NULL }, /* Rs */
! 161: { NULL, NULL }, /* Sc */
! 162: { NULL, NULL }, /* So */
! 163: { NULL, NULL }, /* Sq */
! 164: { NULL, NULL }, /* Sm */
! 165: { NULL, NULL }, /* Sx */
! 166: { NULL, NULL }, /* Sy */
! 167: { NULL, NULL }, /* Tn */
! 168: { NULL, NULL }, /* Ux */
! 169: { NULL, NULL }, /* Xc */
! 170: { NULL, NULL }, /* Xo */
! 171: { NULL, NULL }, /* Fo */
! 172: { NULL, NULL }, /* Fc */
! 173: { NULL, NULL }, /* Oo */
! 174: { NULL, NULL }, /* Oc */
! 175: { NULL, NULL }, /* Bk */
! 176: { NULL, NULL }, /* Ek */
! 177: { NULL, NULL }, /* Bt */
! 178: { NULL, NULL }, /* Hf */
! 179: { NULL, NULL }, /* Fr */
! 180: { NULL, NULL }, /* Ud */
! 181: { NULL, NULL }, /* Lb */
! 182: { NULL, NULL }, /* Ap */
! 183: { NULL, NULL }, /* Lp */
! 184: { NULL, NULL }, /* Lk */
! 185: { NULL, NULL }, /* Mt */
! 186: { NULL, NULL }, /* Brq */
! 187: { NULL, NULL }, /* Bro */
! 188: { NULL, NULL }, /* Brc */
! 189: { NULL, NULL }, /* %C */
! 190: { NULL, NULL }, /* Es */
! 191: { NULL, NULL }, /* En */
! 192: { NULL, NULL }, /* Dx */
! 193: { NULL, NULL }, /* %Q */
! 194: };
! 195:
! 196:
! 197: int
! 198: mdoc_action_pre(struct mdoc *m, const struct mdoc_node *n)
! 199: {
! 200:
! 201: switch (n->type) {
! 202: case (MDOC_ROOT):
! 203: /* FALLTHROUGH */
! 204: case (MDOC_TEXT):
! 205: return(1);
! 206: default:
! 207: break;
! 208: }
! 209:
! 210: if (NULL == mdoc_actions[n->tok].pre)
! 211: return(1);
! 212: return((*mdoc_actions[n->tok].pre)(m, n));
! 213: }
! 214:
! 215:
! 216: int
! 217: mdoc_action_post(struct mdoc *m)
! 218: {
! 219:
! 220: if (MDOC_ACTED & m->last->flags)
! 221: return(1);
! 222: m->last->flags |= MDOC_ACTED;
! 223:
! 224: switch (m->last->type) {
! 225: case (MDOC_TEXT):
! 226: /* FALLTHROUGH */
! 227: case (MDOC_ROOT):
! 228: return(1);
! 229: default:
! 230: break;
! 231: }
! 232:
! 233: if (NULL == mdoc_actions[m->last->tok].post)
! 234: return(1);
! 235: return((*mdoc_actions[m->last->tok].post)(m));
! 236: }
! 237:
! 238:
! 239: static int
! 240: concat(struct mdoc *m, const struct mdoc_node *n,
! 241: char *buf, size_t sz)
! 242: {
! 243:
! 244: for ( ; n; n = n->next) {
! 245: assert(MDOC_TEXT == n->type);
! 246: if (strlcat(buf, n->string, sz) >= sz)
! 247: return(nerr(m, n, ETOOLONG));
! 248: if (NULL == n->next)
! 249: continue;
! 250: if (strlcat(buf, " ", sz) >= sz)
! 251: return(nerr(m, n, ETOOLONG));
! 252: }
! 253:
! 254: return(1);
! 255: }
! 256:
! 257:
! 258: static int
! 259: perr(struct mdoc *m, int line, int pos, enum merr type)
! 260: {
! 261: char *p;
! 262:
! 263: p = NULL;
! 264: switch (type) {
! 265: case (ENUMFMT):
! 266: p = "bad number format";
! 267: break;
! 268: case (ETOOLONG):
! 269: p = "argument text too long";
! 270: break;
! 271: case (EMALLOC):
! 272: p = "memory exhausted";
! 273: break;
! 274: }
! 275: assert(p);
! 276: return(mdoc_perr(m, line, pos, p));
! 277: }
! 278:
! 279:
! 280: static int
! 281: pwarn(struct mdoc *m, int line, int pos, enum mwarn type)
! 282: {
! 283: char *p;
! 284: int c;
! 285:
! 286: p = NULL;
! 287: c = WARN_SYNTAX;
! 288: switch (type) {
! 289: case (WBADSEC):
! 290: p = "inappropriate document section in manual section";
! 291: c = WARN_COMPAT;
! 292: break;
! 293: case (WNOWIDTH):
! 294: p = "cannot determine default width";
! 295: break;
! 296: case (WBADDATE):
! 297: p = "malformed date syntax";
! 298: break;
! 299: }
! 300: assert(p);
! 301: return(mdoc_pwarn(m, line, pos, c, p));
! 302: }
! 303:
! 304:
! 305: static int
! 306: post_std(POST_ARGS)
! 307: {
! 308:
! 309: /*
! 310: * If '-std' is invoked without an argument, fill it in with our
! 311: * name (if it's been set).
! 312: */
! 313:
! 314: if (NULL == m->last->args)
! 315: return(1);
! 316: if (m->last->args->argv[0].sz)
! 317: return(1);
! 318:
! 319: assert(m->meta.name);
! 320:
! 321: m->last->args->argv[0].value = calloc(1, sizeof(char *));
! 322: if (NULL == m->last->args->argv[0].value)
! 323: return(verr(m, EMALLOC));
! 324:
! 325: m->last->args->argv[0].sz = 1;
! 326: m->last->args->argv[0].value[0] = strdup(m->meta.name);
! 327: if (NULL == m->last->args->argv[0].value[0])
! 328: return(verr(m, EMALLOC));
! 329:
! 330: return(1);
! 331: }
! 332:
! 333:
! 334: static int
! 335: post_nm(POST_ARGS)
! 336: {
! 337: char buf[64];
! 338:
! 339: if (m->meta.name)
! 340: return(1);
! 341:
! 342: buf[0] = 0;
! 343: if ( ! concat(m, m->last->child, buf, sizeof(buf)))
! 344: return(0);
! 345: if (NULL == (m->meta.name = strdup(buf)))
! 346: return(verr(m, EMALLOC));
! 347: return(1);
! 348: }
! 349:
! 350:
! 351: static int
! 352: post_sh(POST_ARGS)
! 353: {
! 354: enum mdoc_sec sec;
! 355: char buf[64];
! 356:
! 357: /*
! 358: * We keep track of the current section /and/ the "named"
! 359: * section, which is one of the conventional ones, in order to
! 360: * check ordering.
! 361: */
! 362:
! 363: if (MDOC_HEAD != m->last->type)
! 364: return(1);
! 365:
! 366: buf[0] = 0;
! 367: if ( ! concat(m, m->last->child, buf, sizeof(buf)))
! 368: return(0);
! 369: if (SEC_CUSTOM != (sec = mdoc_atosec(buf)))
! 370: m->lastnamed = sec;
! 371:
! 372: switch ((m->lastsec = sec)) {
! 373: case (SEC_RETURN_VALUES):
! 374: /* FALLTHROUGH */
! 375: case (SEC_ERRORS):
! 376: switch (m->meta.msec) {
! 377: case (2):
! 378: /* FALLTHROUGH */
! 379: case (3):
! 380: /* FALLTHROUGH */
! 381: case (9):
! 382: break;
! 383: default:
! 384: return(vwarn(m, WBADSEC));
! 385: }
! 386: break;
! 387: default:
! 388: break;
! 389: }
! 390: return(1);
! 391: }
! 392:
! 393:
! 394: static int
! 395: post_dt(POST_ARGS)
! 396: {
! 397: struct mdoc_node *n;
! 398: const char *cp;
! 399: char *ep;
! 400: long lval;
! 401:
! 402: if (m->meta.title)
! 403: free(m->meta.title);
! 404: if (m->meta.vol)
! 405: free(m->meta.vol);
! 406: if (m->meta.arch)
! 407: free(m->meta.arch);
! 408:
! 409: m->meta.title = m->meta.vol = m->meta.arch = NULL;
! 410: m->meta.msec = 0;
! 411:
! 412: /* Handles: `.Dt'
! 413: * --> title = unknown, volume = local, msec = 0, arch = NULL
! 414: */
! 415:
! 416: if (NULL == (n = m->last->child)) {
! 417: if (NULL == (m->meta.title = strdup("unknown")))
! 418: return(verr(m, EMALLOC));
! 419: if (NULL == (m->meta.vol = strdup("local")))
! 420: return(verr(m, EMALLOC));
! 421: return(post_prol(m));
! 422: }
! 423:
! 424: /* Handles: `.Dt TITLE'
! 425: * --> title = TITLE, volume = local, msec = 0, arch = NULL
! 426: */
! 427:
! 428: if (NULL == (m->meta.title = strdup(n->string)))
! 429: return(verr(m, EMALLOC));
! 430:
! 431: if (NULL == (n = n->next)) {
! 432: if (NULL == (m->meta.vol = strdup("local")))
! 433: return(verr(m, EMALLOC));
! 434: return(post_prol(m));
! 435: }
! 436:
! 437: /* Handles: `.Dt TITLE SEC'
! 438: * --> title = TITLE, volume = SEC is msec ?
! 439: * format(msec) : SEC,
! 440: * msec = SEC is msec ? atoi(msec) : 0,
! 441: * arch = NULL
! 442: */
! 443:
! 444: cp = mdoc_a2msec(n->string);
! 445: if (cp) {
! 446: if (NULL == (m->meta.vol = strdup(cp)))
! 447: return(verr(m, EMALLOC));
! 448: errno = 0;
! 449: lval = strtol(n->string, &ep, 10);
! 450: if (n->string[0] != '\0' && *ep == '\0')
! 451: m->meta.msec = (int)lval;
! 452: } else if (NULL == (m->meta.vol = strdup(n->string)))
! 453: return(verr(m, EMALLOC));
! 454:
! 455: if (NULL == (n = n->next))
! 456: return(post_prol(m));
! 457:
! 458: /* Handles: `.Dt TITLE SEC VOL'
! 459: * --> title = TITLE, volume = VOL is vol ?
! 460: * format(VOL) :
! 461: * VOL is arch ? format(arch) :
! 462: * VOL
! 463: */
! 464:
! 465: cp = mdoc_a2vol(n->string);
! 466: if (cp) {
! 467: free(m->meta.vol);
! 468: if (NULL == (m->meta.vol = strdup(cp)))
! 469: return(verr(m, EMALLOC));
! 470: n = n->next;
! 471: } else {
! 472: cp = mdoc_a2arch(n->string);
! 473: if (NULL == cp) {
! 474: free(m->meta.vol);
! 475: if (NULL == (m->meta.vol = strdup(n->string)))
! 476: return(verr(m, EMALLOC));
! 477: } else if (NULL == (m->meta.arch = strdup(cp)))
! 478: return(verr(m, EMALLOC));
! 479: }
! 480:
! 481: /* Ignore any subsequent parameters... */
! 482:
! 483: return(post_prol(m));
! 484: }
! 485:
! 486:
! 487: static int
! 488: post_os(POST_ARGS)
! 489: {
! 490: char buf[64];
! 491: struct utsname utsname;
! 492:
! 493: if (m->meta.os)
! 494: free(m->meta.os);
! 495:
! 496: buf[0] = 0;
! 497: if ( ! concat(m, m->last->child, buf, sizeof(buf)))
! 498: return(0);
! 499:
! 500: if (0 == buf[0]) {
! 501: if (-1 == uname(&utsname))
! 502: return(mdoc_err(m, "utsname"));
! 503: if (strlcat(buf, utsname.sysname, 64) >= 64)
! 504: return(verr(m, ETOOLONG));
! 505: if (strlcat(buf, " ", 64) >= 64)
! 506: return(verr(m, ETOOLONG));
! 507: if (strlcat(buf, utsname.release, 64) >= 64)
! 508: return(verr(m, ETOOLONG));
! 509: }
! 510:
! 511: if (NULL == (m->meta.os = strdup(buf)))
! 512: return(verr(m, EMALLOC));
! 513: m->lastnamed = m->lastsec = SEC_BODY;
! 514:
! 515: return(post_prol(m));
! 516: }
! 517:
! 518:
! 519: /*
! 520: * Calculate the -width for a `Bl -tag' list if it hasn't been provided.
! 521: * Uses the first head macro.
! 522: */
! 523: static int
! 524: post_bl_tagwidth(struct mdoc *m)
! 525: {
! 526: struct mdoc_node *n;
! 527: int sz;
! 528: char buf[32];
! 529:
! 530: /*
! 531: * Use the text width, if a text node, or the default macro
! 532: * width if a macro.
! 533: */
! 534:
! 535: if ((n = m->last->body->child)) {
! 536: assert(MDOC_BLOCK == n->type);
! 537: assert(MDOC_It == n->tok);
! 538: n = n->head->child;
! 539: }
! 540:
! 541: sz = 10; /* Default size. */
! 542:
! 543: if (n) {
! 544: if (MDOC_TEXT != n->type) {
! 545: if (0 == (sz = (int)mdoc_macro2len(n->tok)))
! 546: if ( ! vwarn(m, WNOWIDTH))
! 547: return(0);
! 548: } else
! 549: sz = (int)strlen(n->string) + 1;
! 550: }
! 551:
! 552: if (-1 == snprintf(buf, sizeof(buf), "%dn", sz))
! 553: return(verr(m, ENUMFMT));
! 554:
! 555: /*
! 556: * We have to dynamically add this to the macro's argument list.
! 557: * We're guaranteed that a MDOC_Width doesn't already exist.
! 558: */
! 559:
! 560: n = m->last;
! 561: assert(n->args);
! 562: sz = (int)(n->args->argc)++;
! 563:
! 564: n->args->argv = realloc(n->args->argv,
! 565: n->args->argc * sizeof(struct mdoc_argv));
! 566:
! 567: if (NULL == n->args->argv)
! 568: return(verr(m, EMALLOC));
! 569:
! 570: n->args->argv[sz].arg = MDOC_Width;
! 571: n->args->argv[sz].line = m->last->line;
! 572: n->args->argv[sz].pos = m->last->pos;
! 573: n->args->argv[sz].sz = 1;
! 574: n->args->argv[sz].value = calloc(1, sizeof(char *));
! 575:
! 576: if (NULL == n->args->argv[sz].value)
! 577: return(verr(m, EMALLOC));
! 578: if (NULL == (n->args->argv[sz].value[0] = strdup(buf)))
! 579: return(verr(m, EMALLOC));
! 580:
! 581: return(1);
! 582: }
! 583:
! 584:
! 585: static int
! 586: post_bl_width(struct mdoc *m)
! 587: {
! 588: size_t width;
! 589: int i, tok;
! 590: char buf[32];
! 591: char *p;
! 592:
! 593: if (NULL == m->last->args)
! 594: return(1);
! 595:
! 596: for (i = 0; i < (int)m->last->args->argc; i++)
! 597: if (MDOC_Width == m->last->args->argv[i].arg)
! 598: break;
! 599:
! 600: if (i == (int)m->last->args->argc)
! 601: return(1);
! 602: p = m->last->args->argv[i].value[0];
! 603:
! 604: /*
! 605: * If the value to -width is a macro, then we re-write it to be
! 606: * the macro's width as set in share/tmac/mdoc/doc-common.
! 607: */
! 608:
! 609: if (0 == strcmp(p, "Ds"))
! 610: width = 8;
! 611: else if (MDOC_MAX == (tok = mdoc_hash_find(m->htab, p)))
! 612: return(1);
! 613: else if (0 == (width = mdoc_macro2len(tok)))
! 614: return(vwarn(m, WNOWIDTH));
! 615:
! 616: /* The value already exists: free and reallocate it. */
! 617:
! 618: if (-1 == snprintf(buf, sizeof(buf), "%zun", width))
! 619: return(verr(m, ENUMFMT));
! 620:
! 621: free(m->last->args->argv[i].value[0]);
! 622: m->last->args->argv[i].value[0] = strdup(buf);
! 623: if (NULL == m->last->args->argv[i].value[0])
! 624: return(verr(m, EMALLOC));
! 625:
! 626: return(1);
! 627: }
! 628:
! 629:
! 630: static int
! 631: post_bl(POST_ARGS)
! 632: {
! 633: int i, r, len;
! 634:
! 635: if (MDOC_BLOCK != m->last->type)
! 636: return(1);
! 637:
! 638: /*
! 639: * These are fairly complicated, so we've broken them into two
! 640: * functions. post_bl_tagwidth() is called when a -tag is
! 641: * specified, but no -width (it must be guessed). The second
! 642: * when a -width is specified (macro indicators must be
! 643: * rewritten into real lengths).
! 644: */
! 645:
! 646: len = (int)(m->last->args ? m->last->args->argc : 0);
! 647:
! 648: for (r = i = 0; i < len; i++) {
! 649: if (MDOC_Tag == m->last->args->argv[i].arg)
! 650: r |= 1 << 0;
! 651: if (MDOC_Width == m->last->args->argv[i].arg)
! 652: r |= 1 << 1;
! 653: }
! 654:
! 655: if (r & (1 << 0) && ! (r & (1 << 1))) {
! 656: if ( ! post_bl_tagwidth(m))
! 657: return(0);
! 658: } else if (r & (1 << 1))
! 659: if ( ! post_bl_width(m))
! 660: return(0);
! 661:
! 662: return(1);
! 663: }
! 664:
! 665:
! 666: static int
! 667: post_ar(POST_ARGS)
! 668: {
! 669: struct mdoc_node *n;
! 670:
! 671: if (m->last->child)
! 672: return(1);
! 673:
! 674: n = m->last;
! 675: m->next = MDOC_NEXT_CHILD;
! 676: if ( ! mdoc_word_alloc(m, m->last->line,
! 677: m->last->pos, "file"))
! 678: return(0);
! 679: m->next = MDOC_NEXT_SIBLING;
! 680: if ( ! mdoc_word_alloc(m, m->last->line,
! 681: m->last->pos, "..."))
! 682: return(0);
! 683:
! 684: m->last = n;
! 685: m->next = MDOC_NEXT_SIBLING;
! 686: return(1);
! 687: }
! 688:
! 689:
! 690: static int
! 691: post_dd(POST_ARGS)
! 692: {
! 693: char buf[64];
! 694:
! 695: buf[0] = 0;
! 696: if ( ! concat(m, m->last->child, buf, sizeof(buf)))
! 697: return(0);
! 698:
! 699: if (0 == (m->meta.date = mdoc_atotime(buf))) {
! 700: if ( ! vwarn(m, WBADDATE))
! 701: return(0);
! 702: m->meta.date = time(NULL);
! 703: }
! 704:
! 705: return(post_prol(m));
! 706: }
! 707:
! 708:
! 709: static int
! 710: post_prol(POST_ARGS)
! 711: {
! 712: struct mdoc_node *n;
! 713:
! 714: /*
! 715: * The end document shouldn't have the prologue macros as part
! 716: * of the syntax tree (they encompass only meta-data).
! 717: */
! 718:
! 719: if (m->last->parent->child == m->last)
! 720: m->last->parent->child = m->last->prev;
! 721: if (m->last->prev)
! 722: m->last->prev->next = NULL;
! 723:
! 724: n = m->last;
! 725: assert(NULL == m->last->next);
! 726:
! 727: if (m->last->prev) {
! 728: m->last = m->last->prev;
! 729: m->next = MDOC_NEXT_SIBLING;
! 730: } else {
! 731: m->last = m->last->parent;
! 732: m->next = MDOC_NEXT_CHILD;
! 733: }
! 734:
! 735: mdoc_node_freelist(n);
! 736: return(1);
! 737: }
! 738:
! 739:
! 740: static int
! 741: pre_dl(PRE_ARGS)
! 742: {
! 743:
! 744: if (MDOC_BODY != n->type)
! 745: return(1);
! 746: m->flags |= MDOC_LITERAL;
! 747: return(1);
! 748: }
! 749:
! 750:
! 751: static int
! 752: pre_bd(PRE_ARGS)
! 753: {
! 754: int i;
! 755:
! 756: if (MDOC_BODY != n->type)
! 757: return(1);
! 758:
! 759: /* Enter literal context if `Bd -literal' or * -unfilled'. */
! 760:
! 761: for (n = n->parent, i = 0; i < (int)n->args->argc; i++)
! 762: if (MDOC_Literal == n->args->argv[i].arg)
! 763: break;
! 764: else if (MDOC_Unfilled == n->args->argv[i].arg)
! 765: break;
! 766:
! 767: if (i < (int)n->args->argc)
! 768: m->flags |= MDOC_LITERAL;
! 769:
! 770: return(1);
! 771: }
! 772:
! 773:
! 774: static int
! 775: post_display(POST_ARGS)
! 776: {
! 777:
! 778: if (MDOC_BODY == m->last->type)
! 779: m->flags &= ~MDOC_LITERAL;
! 780: return(1);
! 781: }
! 782:
! 783: