diff options
author | Kristaps Dzonsons <kristaps@bsd.lv> | 2009-09-16 09:41:24 +0000 |
---|---|---|
committer | Kristaps Dzonsons <kristaps@bsd.lv> | 2009-09-16 09:41:24 +0000 |
commit | 464bf99c7bec33718cdc95c963d3e5d3a28b3124 (patch) | |
tree | ef4611149e16f2ceec906cc4980285365d2d48bc | |
parent | ea228aff6a872a947bb23cd1b1405ab1d72c37e6 (diff) | |
download | mandoc-464bf99c7bec33718cdc95c963d3e5d3a28b3124.tar.gz |
Made tree/term/out() functions return void.
Put err() functions back into front-ends (no use making it needlessly complex).
-rw-r--r-- | html.c | 638 | ||||
-rw-r--r-- | main.c | 18 | ||||
-rw-r--r-- | man.3 | 2 | ||||
-rw-r--r-- | man_term.c | 31 | ||||
-rw-r--r-- | mdoc.3 | 2 | ||||
-rw-r--r-- | mdoc_term.c | 95 | ||||
-rw-r--r-- | term.c | 12 | ||||
-rw-r--r-- | term.h | 4 | ||||
-rw-r--r-- | tree.c | 6 |
9 files changed, 713 insertions, 95 deletions
@@ -0,0 +1,638 @@ +/* $Id$ */ +/* + * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#include <assert.h> +#include <err.h> +#include <stdio.h> +#include <stdlib.h> + +#include "mdoc.h" +#include "man.h" + +#define DOCTYPE "-//W3C//DTD HTML 4.01//EN" +#define DTD "http://www.w3.org/TR/html4/strict.dtd" + +enum htmltag { + TAG_HTML, + TAG_HEAD, + TAG_BODY, + TAG_META, + TAG_TITLE, + TAG_DIV, + TAG_H1, + TAG_H2, + TAG_P, + TAG_SPAN, + TAG_LINK, + TAG_MAX +}; + +enum htmlattr { + ATTR_HTTPEQUIV, + ATTR_CONTENT, + ATTR_NAME, + ATTR_REL, + ATTR_HREF, + ATTR_TYPE, + ATTR_MEDIA, + ATTR_CLASS, + ATTR_MAX +}; + +struct htmldata { + char *name; + int flags; +#define HTML_BLOCK (1 << 0) +}; + +static const struct htmldata htmltags[TAG_MAX] = { + {"html", HTML_BLOCK}, /* TAG_HTML */ + {"head", HTML_BLOCK}, /* TAG_HEAD */ + {"body", HTML_BLOCK}, /* TAG_BODY */ + {"meta", HTML_BLOCK}, /* TAG_META */ + {"title", HTML_BLOCK}, /* TAG_TITLE */ + {"div", HTML_BLOCK}, /* TAG_DIV */ + {"h1", 0}, /* TAG_H1 */ + {"h2", 0}, /* TAG_H2 */ + {"p", HTML_BLOCK}, /* TAG_P */ + {"span", 0}, /* TAG_SPAN */ + {"link", HTML_BLOCK}, /* TAG_LINK */ +}; + +static const char *const htmlattrs[ATTR_MAX] = { + "http-equiv", + "content", + "name", + "rel", + "href", + "type", + "media", + "class" +}; + +struct htmlpair { + enum htmlattr key; + char *val; +}; + +struct html { + int flags; +#define HTML_NOSPACE (1 << 0) +}; + +#define MDOC_ARGS const struct mdoc_meta *m, \ + const struct mdoc_node *n, \ + struct html *h +#define MAN_ARGS const struct man_meta *m, \ + const struct man_node *n, \ + struct html *h +struct htmlmdoc { + int (*pre)(MDOC_ARGS); + void (*post)(MDOC_ARGS); +}; + +static void print_gen_doctype(struct html *); +static void print_gen_head(struct html *); +static void print_mdoc(MDOC_ARGS); +static void print_mdoc_head(MDOC_ARGS); +static void print_mdoc_node(MDOC_ARGS); +static void print_man(MAN_ARGS); +static void print_man_head(MAN_ARGS); +static void print_man_body(MAN_ARGS); +static void print_otag(struct html *, enum htmltag, + int, const struct htmlpair *); +static void print_ctag(struct html *, enum htmltag); +static void print_encode(const char *); +static void print_text(struct html *, const char *); +static int mdoc_root_pre(MDOC_ARGS); +static void mdoc_root_post(MDOC_ARGS); + +static int mdoc_nd_pre(MDOC_ARGS); +static int mdoc_nm_pre(MDOC_ARGS); +static void mdoc_nm_post(MDOC_ARGS); +static int mdoc_pp_pre(MDOC_ARGS); +static int mdoc_sh_pre(MDOC_ARGS); +static void mdoc_sh_post(MDOC_ARGS); +static int mdoc_ss_pre(MDOC_ARGS); +static void mdoc_ss_post(MDOC_ARGS); + +static const struct htmlmdoc mdocs[MDOC_MAX] = { + {NULL, NULL}, /* Ap */ + {NULL, NULL}, /* Dd */ + {NULL, NULL}, /* Dt */ + {NULL, NULL}, /* Os */ + {mdoc_sh_pre, mdoc_sh_post }, /* Sh */ + {mdoc_ss_pre, mdoc_ss_post }, /* Ss */ + {mdoc_pp_pre, NULL}, /* Pp */ + {NULL, NULL}, /* D1 */ + {NULL, NULL}, /* Dl */ + {NULL, NULL}, /* Bd */ + {NULL, NULL}, /* Ed */ + {NULL, NULL}, /* Bl */ + {NULL, NULL}, /* El */ + {NULL, NULL}, /* It */ + {NULL, NULL}, /* Ad */ + {NULL, NULL}, /* An */ + {NULL, NULL}, /* Ar */ + {NULL, NULL}, /* Cd */ + {NULL, NULL}, /* Cm */ + {NULL, NULL}, /* Dv */ + {NULL, NULL}, /* Er */ + {NULL, NULL}, /* Ev */ + {NULL, NULL}, /* Ex */ + {NULL, NULL}, /* Fa */ + {NULL, NULL}, /* Fd */ + {NULL, NULL}, /* Fl */ + {NULL, NULL}, /* Fn */ + {NULL, NULL}, /* Ft */ + {NULL, NULL}, /* Ic */ + {NULL, NULL}, /* In */ + {NULL, NULL}, /* Li */ + {mdoc_nd_pre, NULL}, /* Nd */ + {mdoc_nm_pre, mdoc_nm_post}, /* Nm */ + {NULL, NULL}, /* Op */ + {NULL, NULL}, /* Ot */ + {NULL, NULL}, /* Pa */ + {NULL, NULL}, /* Rv */ + {NULL, NULL}, /* St */ + {NULL, NULL}, /* Va */ + {NULL, NULL}, /* Vt */ + {NULL, 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, NULL}, /* Ac */ + {NULL, NULL}, /* Ao */ + {NULL, NULL}, /* Aq */ + {NULL, NULL}, /* At */ + {NULL, NULL}, /* Bc */ + {NULL, NULL}, /* Bf */ + {NULL, NULL}, /* Bo */ + {NULL, NULL}, /* Bq */ + {NULL, NULL}, /* Bsx */ + {NULL, NULL}, /* Bx */ + {NULL, NULL}, /* Db */ + {NULL, NULL}, /* Dc */ + {NULL, NULL}, /* Do */ + {NULL, NULL}, /* Dq */ + {NULL, NULL}, /* Ec */ + {NULL, NULL}, /* Ef */ + {NULL, NULL}, /* Em */ + {NULL, NULL}, /* Eo */ + {NULL, NULL}, /* Fx */ + {NULL, NULL}, /* Ms */ + {NULL, NULL}, /* No */ + {NULL, NULL}, /* Ns */ + {NULL, NULL}, /* Nx */ + {NULL, NULL}, /* Ox */ + {NULL, NULL}, /* Pc */ + {NULL, NULL}, /* Pf */ + {NULL, NULL}, /* Po */ + {NULL, NULL}, /* Pq */ + {NULL, NULL}, /* Qc */ + {NULL, NULL}, /* Ql */ + {NULL, NULL}, /* Qo */ + {NULL, NULL}, /* Qq */ + {NULL, NULL}, /* Re */ + {NULL, NULL}, /* Rs */ + {NULL, NULL}, /* Sc */ + {NULL, NULL}, /* So */ + {NULL, NULL}, /* Sq */ + {NULL, NULL}, /* Sm */ + {NULL, NULL}, /* Sx */ + {NULL, NULL}, /* Sy */ + {NULL, NULL}, /* Tn */ + {NULL, NULL}, /* Ux */ + {NULL, NULL}, /* Xc */ + {NULL, NULL}, /* Xo */ + {NULL, NULL}, /* Fo */ + {NULL, NULL}, /* Fc */ + {NULL, NULL}, /* Oo */ + {NULL, NULL}, /* Oc */ + {NULL, NULL}, /* Bk */ + {NULL, NULL}, /* Ek */ + {NULL, NULL}, /* Bt */ + {NULL, NULL}, /* Hf */ + {NULL, NULL}, /* Fr */ + {NULL, NULL}, /* Ud */ + {NULL, NULL}, /* Lb */ + {NULL, NULL}, /* Lp */ + {NULL, NULL}, /* Lk */ + {NULL, NULL}, /* Mt */ + {NULL, NULL}, /* Brq */ + {NULL, NULL}, /* Bro */ + {NULL, NULL}, /* Brc */ + {NULL, NULL}, /* %C */ + {NULL, NULL}, /* Es */ + {NULL, NULL}, /* En */ + {NULL, NULL}, /* Dx */ + {NULL, NULL}, /* %Q */ + {NULL, NULL}, /* br */ + {NULL, NULL}, /* sp */ +}; + + +int +html_mdoc(void *arg, const struct mdoc *m) +{ + struct html *h; + + h = (struct html *)arg; + + print_gen_doctype(h); + print_otag(h, TAG_HTML, 0, NULL); + print_mdoc(mdoc_meta(m), mdoc_node(m), h); + print_ctag(h, TAG_HTML); + printf("\n"); + return(1); +} + + +int +html_man(void *arg, const struct man *m) +{ + struct html *h; + + h = (struct html *)arg; + + print_gen_doctype(h); + print_otag(h, TAG_HTML, 0, NULL); + print_man(man_meta(m), man_node(m), h); + print_ctag(h, TAG_HTML); + printf("\n"); + return(1); +} + + +void * +html_alloc(void) +{ + + return(calloc(1, sizeof(struct html))); +} + + +void +html_free(void *p) +{ + + free(p); +} + + +static void +print_mdoc(MDOC_ARGS) +{ + + print_otag(h, TAG_HEAD, 0, NULL); + print_mdoc_head(m, n, h); + print_ctag(h, TAG_HEAD); + print_otag(h, TAG_BODY, 0, NULL); + print_mdoc_node(m, n, h); + print_ctag(h, TAG_BODY); +} + + +static void +print_gen_head(struct html *h) +{ + struct htmlpair meta0[2]; + struct htmlpair meta1[2]; + struct htmlpair link[4]; + + meta0[0].key = ATTR_HTTPEQUIV; + meta0[0].val = "Content-Type"; + meta0[1].key = ATTR_CONTENT; + meta0[1].val = "text/html; charest-utf-8"; + + meta1[0].key = ATTR_NAME; + meta1[0].val = "resource-type"; + meta1[1].key = ATTR_CONTENT; + meta1[1].val = "document"; + + link[0].key = ATTR_REL; + link[0].val = "stylesheet"; + link[1].key = ATTR_HREF; + link[1].val = "style.css"; + link[2].key = ATTR_TYPE; + link[2].val = "text/css"; + link[3].key = ATTR_MEDIA; + link[3].val = "all"; + + print_otag(h, TAG_META, 2, meta0); + print_otag(h, TAG_META, 2, meta1); + print_otag(h, TAG_LINK, 4, link); +} + + +static void +print_mdoc_head(MDOC_ARGS) +{ + + print_gen_head(h); + print_otag(h, TAG_TITLE, 0, NULL); + print_encode(m->title); + print_ctag(h, TAG_TITLE); +} + + +static int +mdoc_root_pre(MDOC_ARGS) +{ + struct htmlpair div; + + div.key = ATTR_CLASS; + div.val = "body"; + + print_otag(h, TAG_DIV, 1, &div); + return(1); +} + + +static void +mdoc_root_post(MDOC_ARGS) +{ + + print_ctag(h, TAG_DIV); +} + + +static int +mdoc_ss_pre(MDOC_ARGS) +{ + + if (MDOC_BODY == n->type) + print_otag(h, TAG_P, 0, NULL); + if (MDOC_HEAD == n->type) + print_otag(h, TAG_H2, 0, NULL); + return(1); +} + + +static void +mdoc_ss_post(MDOC_ARGS) +{ + + if (MDOC_BODY == n->type) + print_ctag(h, TAG_P); + if (MDOC_HEAD == n->type) + print_ctag(h, TAG_H2); +} + + +static int +mdoc_pp_pre(MDOC_ARGS) +{ + + print_otag(h, TAG_P, 0, NULL); + return(0); +} + + +static int +mdoc_nd_pre(MDOC_ARGS) +{ + + if (MDOC_BODY == n->type) + print_text(h, "--"); + return(1); +} + + +static int +mdoc_nm_pre(MDOC_ARGS) +{ + struct htmlpair class; + + class.key = ATTR_CLASS; + class.val = "name"; + + print_otag(h, TAG_SPAN, 1, &class); + if (NULL == n->child) + print_text(h, m->name); + + return(1); +} + + +static void +mdoc_nm_post(MDOC_ARGS) +{ + + print_ctag(h, TAG_SPAN); +} + + +static int +mdoc_sh_pre(MDOC_ARGS) +{ + + if (MDOC_BODY == n->type) + print_otag(h, TAG_P, 0, NULL); + if (MDOC_HEAD == n->type) + print_otag(h, TAG_H1, 0, NULL); + return(1); +} + + +static void +mdoc_sh_post(MDOC_ARGS) +{ + + if (MDOC_BODY == n->type) + print_ctag(h, TAG_P); + if (MDOC_HEAD == n->type) + print_ctag(h, TAG_H1); +} + + +static void +print_mdoc_node(MDOC_ARGS) +{ + int child; + + child = 1; + + switch (n->type) { + case (MDOC_ROOT): + child = mdoc_root_pre(m, n, h); + break; + case (MDOC_TEXT): + print_text(h, n->string); + break; + default: + if (mdocs[n->tok].pre) + child = (*mdocs[n->tok].pre)(m, n, h); + break; + } + + if (child && n->child) + print_mdoc_node(m, n->child, h); + + switch (n->type) { + case (MDOC_ROOT): + mdoc_root_post(m, n, h); + break; + case (MDOC_TEXT): + break; + default: + if (mdocs[n->tok].post) + (*mdocs[n->tok].post)(m, n, h); + break; + } + + if (n->next) + print_mdoc_node(m, n->next, h); +} + + +static void +print_man(MAN_ARGS) +{ + + print_otag(h, TAG_HEAD, 0, NULL); + print_man_head(m, n, h); + print_ctag(h, TAG_HEAD); + print_otag(h, TAG_BODY, 0, NULL); + print_man_body(m, n, h); + print_ctag(h, TAG_BODY); +} + + +static void +print_man_head(MAN_ARGS) +{ + + print_gen_head(h); + print_otag(h, TAG_TITLE, 0, NULL); + print_encode(m->title); + print_ctag(h, TAG_TITLE); +} + + +static void +print_man_body(MAN_ARGS) +{ +} + + +static void +print_encode(const char *p) +{ + + printf("%s", p); /* XXX */ +} + + +static void +print_otag(struct html *h, enum htmltag tag, + int sz, const struct htmlpair *p) +{ + int i; + + if ( ! (HTML_NOSPACE & h->flags)) + if ( ! (HTML_BLOCK & htmltags[tag].flags)) + printf(" "); + + printf("<%s", htmltags[tag].name); + for (i = 0; i < sz; i++) { + printf(" %s=\"", htmlattrs[p[i].key]); + assert(p->val); + print_encode(p[i].val); + printf("\""); + } + printf(">"); + + h->flags |= HTML_NOSPACE; + +} + + +/* ARGSUSED */ +static void +print_ctag(struct html *h, enum htmltag tag) +{ + + printf("</%s>", htmltags[tag].name); + if (HTML_BLOCK & htmltags[tag].flags) + h->flags |= HTML_NOSPACE; +} + + +/* ARGSUSED */ +static void +print_gen_doctype(struct html *h) +{ + + printf("<!DOCTYPE HTML PUBLIC \"%s\" \"%s\">\n", DOCTYPE, DTD); +} + + +static void +print_text(struct html *h, const char *p) +{ + + if (*p && 0 == *(p + 1)) + switch (*p) { + case('.'): + /* FALLTHROUGH */ + case(','): + /* FALLTHROUGH */ + case(';'): + /* FALLTHROUGH */ + case(':'): + /* FALLTHROUGH */ + case('?'): + /* FALLTHROUGH */ + case('!'): + /* FALLTHROUGH */ + case(')'): + /* FALLTHROUGH */ + case(']'): + /* FALLTHROUGH */ + case('}'): + h->flags |= HTML_NOSPACE; + default: + break; + } + + if ( ! (h->flags & HTML_NOSPACE)) + printf(" "); + h->flags &= ~HTML_NOSPACE; + + if (p) + print_encode(p); + + if (*p && 0 == *(p + 1)) + switch (*p) { + case('('): + /* FALLTHROUGH */ + case('['): + /* FALLTHROUGH */ + case('{'): + h->flags |= HTML_NOSPACE; + default: + break; + } +} @@ -40,8 +40,8 @@ extern int getsubopt(char **, char * const *, char **); # endif #endif -typedef int (*out_mdoc)(void *, const struct mdoc *); -typedef int (*out_man)(void *, const struct man *); +typedef void (*out_mdoc)(void *, const struct mdoc *); +typedef void (*out_man)(void *, const struct man *); typedef void (*out_free)(void *); struct buf { @@ -86,10 +86,10 @@ struct curparse { }; extern void *ascii_alloc(void); -extern int tree_mdoc(void *, const struct mdoc *); -extern int tree_man(void *, const struct man *); -extern int terminal_mdoc(void *, const struct mdoc *); -extern int terminal_man(void *, const struct man *); +extern void tree_mdoc(void *, const struct mdoc *); +extern void tree_man(void *, const struct man *); +extern void terminal_mdoc(void *, const struct mdoc *); +extern void terminal_man(void *, const struct man *); extern void terminal_free(void *); static int foptions(int *, char *); @@ -450,11 +450,9 @@ fdesc(struct buf *blk, struct buf *ln, struct curparse *curp) /* Execute the out device, if it exists. */ if (man && curp->outman) - if ( ! (*curp->outman)(curp->outdata, man)) - return(-1); + (*curp->outman)(curp->outdata, man); if (mdoc && curp->outmdoc) - if ( ! (*curp->outmdoc)(curp->outdata, mdoc)) - return(-1); + (*curp->outmdoc)(curp->outdata, mdoc); return(1); } @@ -29,7 +29,7 @@ .Nd man macro compiler library .\" SECTION .Sh SYNOPSIS -.Fd #include <man.h> +.Fd #include "man.h" .Vt extern const char * const * man_macronames; .Ft "struct man *" .Fn man_alloc "void *data" "int pflags" "const struct man_cb *cb" @@ -18,6 +18,7 @@ #include <assert.h> #include <ctype.h> +#include <err.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -129,27 +130,24 @@ extern size_t strlcpy(char *, const char *, size_t); extern size_t strlcat(char *, const char *, size_t); #endif -static int print_head(struct termp *, +static void print_head(struct termp *, const struct man_meta *); static void print_body(DECL_ARGS); static void print_node(DECL_ARGS); -static int print_foot(struct termp *, +static void print_foot(struct termp *, const struct man_meta *); static void fmt_block_vspace(struct termp *, const struct man_node *); static int arg_width(const struct man_node *); -int +void man_run(struct termp *p, const struct man *m) { struct mtermp mt; - if ( ! print_head(p, man_meta(m))) - return(0); + print_head(p, man_meta(m)); p->flags |= TERMP_NOSPACE; - assert(man_node(m)); - assert(MAN_ROOT == man_node(m)->type); mt.fl = 0; mt.lmargin = INDENT; @@ -157,10 +155,7 @@ man_run(struct termp *p, const struct man *m) if (man_node(m)->child) print_body(p, &mt, man_node(m)->child, man_meta(m)); - if ( ! print_foot(p, man_meta(m))) - return(0); - - return(1); + print_foot(p, man_meta(m)); } @@ -920,19 +915,19 @@ print_body(DECL_ARGS) } -static int +static void print_foot(struct termp *p, const struct man_meta *meta) { struct tm *tm; char *buf; if (NULL == (buf = malloc(p->rmargin))) - return(0); + err(EXIT_FAILURE, "malloc"); tm = localtime(&meta->date); if (0 == strftime(buf, p->rmargin, "%B %d, %Y", tm)) - buf[0] = 0; + err(EXIT_FAILURE, "strftime"); term_vspace(p); @@ -955,11 +950,10 @@ print_foot(struct termp *p, const struct man_meta *meta) term_flushln(p); free(buf); - return(1); } -static int +static void print_head(struct termp *p, const struct man_meta *meta) { char *buf, *title; @@ -968,9 +962,9 @@ print_head(struct termp *p, const struct man_meta *meta) p->offset = 0; if (NULL == (buf = malloc(p->rmargin))) - return(0); + err(EXIT_FAILURE, "malloc"); if (NULL == (title = malloc(p->rmargin))) - return(0); + err(EXIT_FAILURE, "malloc"); if (meta->vol) (void)strlcpy(buf, meta->vol, p->rmargin); @@ -1008,6 +1002,5 @@ print_head(struct termp *p, const struct man_meta *meta) free(title); free(buf); - return(1); } @@ -29,7 +29,7 @@ .Nd mdoc macro compiler library .\" SECTION .Sh SYNOPSIS -.Fd #include <mdoc.h> +.Fd #include "mdoc.h" .Vt extern const char * const * mdoc_macronames; .Vt extern const char * const * mdoc_argnames; .Ft "struct mdoc *" diff --git a/mdoc_term.c b/mdoc_term.c index 8e2efd6d..e2cf0356 100644 --- a/mdoc_term.c +++ b/mdoc_term.c @@ -113,7 +113,7 @@ static int termp_ud_pre(DECL_ARGS); static int termp_xr_pre(DECL_ARGS); static int termp_xx_pre(DECL_ARGS); -static const struct termact termacts[MDOC_MAX] = { +static const struct termact termacts[MDOC_MAX] = { { termp_ap_pre, NULL }, /* Ap */ { NULL, NULL }, /* Dd */ { NULL, NULL }, /* Dt */ @@ -153,7 +153,7 @@ static const struct termact termacts[MDOC_MAX] = { { termp_rv_pre, NULL }, /* Rv */ { NULL, NULL }, /* St */ { termp_under_pre, NULL }, /* Va */ - { termp_under_pre, termp_vt_post }, /* Vt */ /* FIXME: type name */ + { termp_under_pre, termp_vt_post }, /* Vt */ { termp_xr_pre, NULL }, /* Xr */ { NULL, termp____post }, /* %A */ { NULL, termp____post }, /* %B */ @@ -252,27 +252,24 @@ static void fmt_block_vspace(struct termp *, const struct mdoc_node *, const struct mdoc_node *); static void print_node(DECL_ARGS); -static void print_head(struct termp *, - const struct mdoc_meta *); +static void print_head(DECL_ARGS); static void print_body(DECL_ARGS); -static void print_foot(struct termp *, - const struct mdoc_meta *); +static void print_foot(DECL_ARGS); -int -mdoc_run(struct termp *p, const struct mdoc *m) +void +mdoc_run(struct termp *p, const struct mdoc *mdoc) { - /* - * Main output function. When this is called, assume that the - * tree is properly formed. - */ - print_head(p, mdoc_meta(m)); - assert(mdoc_node(m)); - assert(MDOC_ROOT == mdoc_node(m)->type); - if (mdoc_node(m)->child) - print_body(p, NULL, mdoc_meta(m), mdoc_node(m)->child); - print_foot(p, mdoc_meta(m)); - return(1); + const struct mdoc_node *n; + const struct mdoc_meta *m; + + n = mdoc_node(mdoc); + m = mdoc_meta(mdoc); + + print_head(p, NULL, m, n); + if (n->child) + print_body(p, NULL, m, n->child); + print_foot(p, NULL, m, n); } @@ -281,64 +278,60 @@ print_body(DECL_ARGS) { print_node(p, pair, meta, node); - if ( ! node->next) - return; - print_body(p, pair, meta, node->next); + if (node->next) + print_body(p, pair, meta, node->next); } +/* ARGSUSED */ static void print_node(DECL_ARGS) { - int dochild, bold, under; + int chld, bold, under; struct termpair npair; size_t offset, rmargin; - dochild = 1; + chld = 1; offset = p->offset; rmargin = p->rmargin; bold = p->bold; under = p->under; + bzero(&npair, sizeof(struct termpair)); npair.ppair = pair; - npair.flag = 0; - npair.count = 0; - - /* - * Note on termpair. This allows a pre function to set a termp - * flag that is automatically unset after the body, but before - * the post function. Thus, if a pre uses a termpair flag, it - * must be reapplied in the post for use. - */ if (MDOC_TEXT != node->type) { if (termacts[node->tok].pre) - if ( ! (*termacts[node->tok].pre)(p, &npair, meta, node)) - dochild = 0; - } else /* MDOC_TEXT == node->type */ + chld = (*termacts[node->tok].pre) + (p, &npair, meta, node); + } else term_word(p, node->string); - /* Children. */ - - if (dochild && node->child) + if (chld && node->child) print_body(p, &npair, meta, node->child); + /* + * XXX - if bold/under were to span scopes, this wouldn't be + * possible, but because decoration is always in-scope, we can + * get away with this. + */ + p->bold = bold; p->under = under; - /* Post-processing. */ - if (MDOC_TEXT != node->type) if (termacts[node->tok].post) - (*termacts[node->tok].post)(p, &npair, meta, node); + (*termacts[node->tok].post) + (p, &npair, meta, node); p->offset = offset; p->rmargin = rmargin; } +/* ARGSUSED */ static void -print_foot(struct termp *p, const struct mdoc_meta *meta) +print_foot(DECL_ARGS) { struct tm *tm; char *buf, *os; @@ -352,14 +345,14 @@ print_foot(struct termp *p, const struct mdoc_meta *meta) */ if (NULL == (buf = malloc(p->rmargin))) - err(1, "malloc"); + err(EXIT_FAILURE, "malloc"); if (NULL == (os = malloc(p->rmargin))) - err(1, "malloc"); + err(EXIT_FAILURE, "malloc"); tm = localtime(&meta->date); if (0 == strftime(buf, p->rmargin, "%B %e, %Y", tm)) - err(1, "strftime"); + err(EXIT_FAILURE, "strftime"); (void)strlcpy(os, meta->os, p->rmargin); @@ -396,8 +389,9 @@ print_foot(struct termp *p, const struct mdoc_meta *meta) } +/* ARGSUSED */ static void -print_head(struct termp *p, const struct mdoc_meta *meta) +print_head(DECL_ARGS) { char *buf, *title; @@ -405,9 +399,9 @@ print_head(struct termp *p, const struct mdoc_meta *meta) p->offset = 0; if (NULL == (buf = malloc(p->rmargin))) - err(1, "malloc"); + err(EXIT_FAILURE, "malloc"); if (NULL == (title = malloc(p->rmargin))) - err(1, "malloc"); + err(EXIT_FAILURE, "malloc"); /* * The header is strange. It has three components, which are @@ -431,8 +425,7 @@ print_head(struct termp *p, const struct mdoc_meta *meta) (void)strlcat(buf, ")", p->rmargin); } - (void)snprintf(title, p->rmargin, "%s(%d)", - meta->title, meta->msec); + snprintf(title, p->rmargin, "%s(%d)", meta->title, meta->msec); p->offset = 0; p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2; @@ -24,9 +24,9 @@ #include "man.h" #include "mdoc.h" -extern int man_run(struct termp *, +extern void man_run(struct termp *, const struct man *); -extern int mdoc_run(struct termp *, +extern void mdoc_run(struct termp *, const struct mdoc *); static struct termp *term_alloc(enum termenc); @@ -51,7 +51,7 @@ ascii_alloc(void) } -int +void terminal_man(void *arg, const struct man *man) { struct termp *p; @@ -60,11 +60,11 @@ terminal_man(void *arg, const struct man *man) if (NULL == p->symtab) p->symtab = term_ascii2htab(); - return(man_run(p, man)); + man_run(p, man); } -int +void terminal_mdoc(void *arg, const struct mdoc *mdoc) { struct termp *p; @@ -73,7 +73,7 @@ terminal_mdoc(void *arg, const struct mdoc *mdoc) if (NULL == p->symtab) p->symtab = term_ascii2htab(); - return(mdoc_run(p, mdoc)); + mdoc_run(p, mdoc); } @@ -20,9 +20,7 @@ __BEGIN_DECLS enum termenc { - TERMENC_ASCII, - TERMENC_LATIN1, /* Not implemented. */ - TERMENC_UTF8 /* Not implemented. */ + TERMENC_ASCII }; struct termp { @@ -27,22 +27,20 @@ static void print_man(const struct man_node *, int); /* ARGSUSED */ -int +void tree_mdoc(void *arg, const struct mdoc *mdoc) { print_mdoc(mdoc_node(mdoc), 0); - return(1); } /* ARGSUSED */ -int +void tree_man(void *arg, const struct man *man) { print_man(man_node(man), 0); - return(1); } |