diff options
author | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2009-12-22 23:58:01 +0000 |
---|---|---|
committer | Ingo Schwarze <schwarze@cvs.openbsd.org> | 2009-12-22 23:58:01 +0000 |
commit | 89c124b1e04e596f4ba06193091daad0e01dc939 (patch) | |
tree | 9ed92a7f590db13da46deb9d8434a83b1802ba08 /usr.bin/mandoc | |
parent | 3577315b7c4436e4b96b8b9f1d54181b4451d77e (diff) |
sync to 1.9.12, mostly portability and refactoring:
correctness/functionality:
- bugfix: do not die when overstep hits the right margin
- new option: -fign-escape
- and various HTML features
portability:
- replace bzero(3) by memset(3), which is ANSI C
- replace err(3)/warn(3) by perror(3)/exit(3), which is ANSI C
- iuse argv[0] instead of __progname
- add time.h to various files for FreeBSD compilation
simplicity:
- do not allocate header/footer data dynamically in *_term.c
- provide and use malloc frontends that error out on failure
for full changelogs, see http://bsd.lv/cgi-bin/cvsweb.cgi/
Diffstat (limited to 'usr.bin/mandoc')
32 files changed, 519 insertions, 467 deletions
diff --git a/usr.bin/mandoc/Makefile b/usr.bin/mandoc/Makefile index 857429bfab4..12eb3d5284c 100644 --- a/usr.bin/mandoc/Makefile +++ b/usr.bin/mandoc/Makefile @@ -1,8 +1,8 @@ -# $OpenBSD: Makefile,v 1.21 2009/10/27 21:40:07 schwarze Exp $ +# $OpenBSD: Makefile,v 1.22 2009/12/22 23:58:00 schwarze Exp $ .include <bsd.own.mk> -VERSION=1.9.11 +VERSION=1.9.12 CFLAGS+=-DVERSION=\"${VERSION}\" CFLAGS+=-W -Wall -Wstrict-prototypes .if ${USE_GCC3:L} != "no" diff --git a/usr.bin/mandoc/arch.c b/usr.bin/mandoc/arch.c index 8ab1b001e7d..53679ea8066 100644 --- a/usr.bin/mandoc/arch.c +++ b/usr.bin/mandoc/arch.c @@ -1,4 +1,4 @@ -/* $Id: arch.c,v 1.2 2009/06/14 23:00:57 schwarze Exp $ */ +/* $Id: arch.c,v 1.3 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -16,6 +16,7 @@ */ #include <stdlib.h> #include <string.h> +#include <time.h> #include "libmdoc.h" diff --git a/usr.bin/mandoc/att.c b/usr.bin/mandoc/att.c index ebc7f8410ce..e95dc09a45b 100644 --- a/usr.bin/mandoc/att.c +++ b/usr.bin/mandoc/att.c @@ -1,4 +1,4 @@ -/* $Id: att.c,v 1.2 2009/06/14 23:00:57 schwarze Exp $ */ +/* $Id: att.c,v 1.3 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -16,6 +16,7 @@ */ #include <stdlib.h> #include <string.h> +#include <time.h> #include "libmdoc.h" diff --git a/usr.bin/mandoc/chars.c b/usr.bin/mandoc/chars.c index ecf1fb30d12..c8f8dd9e616 100644 --- a/usr.bin/mandoc/chars.c +++ b/usr.bin/mandoc/chars.c @@ -1,4 +1,4 @@ -/* $Id: chars.c,v 1.2 2009/10/19 09:56:35 schwarze Exp $ */ +/* $Id: chars.c,v 1.3 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -15,7 +15,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <assert.h> -#include <err.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -89,13 +89,17 @@ chars_init(enum chars type) * (they're in-line re-ordered during lookup). */ - if (NULL == (tab = malloc(sizeof(struct tbl)))) - err(1, "malloc"); - tab->type = type; + tab = malloc(sizeof(struct tbl)); + if (NULL == tab) { + perror(NULL); + exit(EXIT_FAILURE); + } htab = calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **)); - if (NULL == htab) - err(1, "malloc"); + if (NULL == htab) { + perror(NULL); + exit(EXIT_FAILURE); + } for (i = 0; i < LINES_MAX; i++) { hash = (int)lines[i].code[0] - PRINT_LO; @@ -111,6 +115,7 @@ chars_init(enum chars type) } tab->htab = htab; + tab->type = type; return(tab); } diff --git a/usr.bin/mandoc/html.c b/usr.bin/mandoc/html.c index 01cedef7da7..7af37b797b5 100644 --- a/usr.bin/mandoc/html.c +++ b/usr.bin/mandoc/html.c @@ -1,4 +1,4 @@ -/* $Id: html.c,v 1.2 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: html.c,v 1.3 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -17,7 +17,7 @@ #include <sys/types.h> #include <assert.h> -#include <err.h> +#include <ctype.h> #include <stdio.h> #include <stdarg.h> #include <stdint.h> @@ -80,6 +80,7 @@ static const char *const htmlattrs[ATTR_MAX] = { "valign", "target", "id", + "summary", }; void * @@ -94,16 +95,15 @@ html_alloc(char *outopts) toks[2] = "includes"; toks[3] = NULL; - if (NULL == (h = calloc(1, sizeof(struct html)))) - return(NULL); + h = calloc(1, sizeof(struct html)); + if (NULL == h) { + perror(NULL); + exit(EXIT_FAILURE); + } h->tags.head = NULL; h->ords.head = NULL; - - if (NULL == (h->symtab = chars_init(CHARS_HTML))) { - free(h); - return(NULL); - } + h->symtab = chars_init(CHARS_HTML); while (outopts && *outopts) switch (getsubopt(&outopts, UNCONST(toks), &v)) { @@ -348,8 +348,11 @@ print_otag(struct html *h, enum htmltag tag, struct tag *t; if ( ! (HTML_NOSTACK & htmltags[tag].flags)) { - if (NULL == (t = malloc(sizeof(struct tag)))) - err(EXIT_FAILURE, "malloc"); + t = malloc(sizeof(struct tag)); + if (NULL == t) { + perror(NULL); + exit(EXIT_FAILURE); + } t->tag = tag; t->next = h->tags.head; h->tags.head = t; @@ -385,11 +388,11 @@ print_ctag(struct html *h, enum htmltag tag) { printf("</%s>", htmltags[tag].name); - if (HTML_CLRLINE & htmltags[tag].flags) + if (HTML_CLRLINE & htmltags[tag].flags) { h->flags |= HTML_NOSPACE; - if (HTML_CLRLINE & htmltags[tag].flags) h->flags |= HTML_NEWLINE; - else + printf("\n"); + } else h->flags &= ~HTML_NEWLINE; } @@ -643,3 +646,30 @@ bufcat_su(struct html *h, const char *p, const struct roffsu *su) buffmt(h, "%s: %d%s;", p, (int)v, u); } + +void +html_idcat(char *dst, const char *src, int sz) +{ + int ssz; + + assert(sz); + + /* Cf. <http://www.w3.org/TR/html4/types.html#h-6.2>. */ + + for ( ; *dst != '\0' && sz; dst++, sz--) + /* Jump to end. */ ; + + assert(sz > 2); + + /* We can't start with a number (bah). */ + + *dst++ = 'x'; + *dst = '\0'; + sz--; + + for ( ; *src != '\0' && sz > 1; src++) { + ssz = snprintf(dst, (size_t)sz, "%.2x", *src); + sz -= ssz; + dst += ssz; + } +} diff --git a/usr.bin/mandoc/html.h b/usr.bin/mandoc/html.h index 6829ab8cb7e..9a202042a8f 100644 --- a/usr.bin/mandoc/html.h +++ b/usr.bin/mandoc/html.h @@ -1,4 +1,4 @@ -/* $Id: html.h,v 1.2 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: html.h,v 1.3 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -58,6 +58,7 @@ enum htmlattr { ATTR_VALIGN, ATTR_TARGET, ATTR_ID, + ATTR_SUMMARY, ATTR_MAX }; @@ -93,6 +94,9 @@ struct htmlpair { #define PAIR_STYLE_INIT(p, h) \ do { (p)->key = ATTR_STYLE; \ (p)->val = (h)->buf; } while (/* CONSTCOND */ 0) +#define PAIR_SUMMARY_INIT(p, v) \ + do { (p)->key = ATTR_SUMMARY; \ + (p)->val = (v); } while (/* CONSTCOND */ 0) struct html { int flags; @@ -132,6 +136,8 @@ void bufcat_style(struct html *, void bufncat(struct html *, const char *, size_t); void bufinit(struct html *); +void html_idcat(char *, const char *, int); + __END_DECLS #endif /*!HTML_H*/ diff --git a/usr.bin/mandoc/lib.c b/usr.bin/mandoc/lib.c index 3cd2ab98bc9..1f9a2cc7971 100644 --- a/usr.bin/mandoc/lib.c +++ b/usr.bin/mandoc/lib.c @@ -1,4 +1,4 @@ -/* $Id: lib.c,v 1.2 2009/06/14 23:00:57 schwarze Exp $ */ +/* $Id: lib.c,v 1.3 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -16,6 +16,7 @@ */ #include <stdlib.h> #include <string.h> +#include <time.h> #include "libmdoc.h" diff --git a/usr.bin/mandoc/libman.h b/usr.bin/mandoc/libman.h index 7529587b266..5ae5c4082e1 100644 --- a/usr.bin/mandoc/libman.h +++ b/usr.bin/mandoc/libman.h @@ -1,4 +1,4 @@ -/* $Id: libman.h,v 1.10 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: libman.h,v 1.11 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -41,7 +41,6 @@ struct man { enum merr { WNPRINT = 0, - WNMEM, WMSEC, WDATE, WLNSCOPE, diff --git a/usr.bin/mandoc/libmandoc.h b/usr.bin/mandoc/libmandoc.h index 8441ec35d0d..ddc29a88b8f 100644 --- a/usr.bin/mandoc/libmandoc.h +++ b/usr.bin/mandoc/libmandoc.h @@ -1,4 +1,4 @@ -/* $Id: libmandoc.h,v 1.1 2009/07/08 00:04:10 schwarze Exp $ */ +/* $Id: libmandoc.h,v 1.2 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -20,6 +20,10 @@ __BEGIN_DECLS int mandoc_special(const char *); +void *mandoc_calloc(size_t, size_t); +char *mandoc_strdup(const char *); +void *mandoc_malloc(size_t); +void *mandoc_realloc(void *, size_t); __END_DECLS diff --git a/usr.bin/mandoc/libmdoc.h b/usr.bin/mandoc/libmdoc.h index 86cdf99efc9..8c8ab1e1e38 100644 --- a/usr.bin/mandoc/libmdoc.h +++ b/usr.bin/mandoc/libmdoc.h @@ -1,4 +1,4 @@ -/* $Id: libmdoc.h,v 1.23 2009/10/21 19:13:50 schwarze Exp $ */ +/* $Id: libmdoc.h,v 1.24 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -44,7 +44,6 @@ enum merr { ETAILWS = 0, EQUOTPARM, EQUOTTERM, - EMALLOC, EARGVAL, EBODYPROL, EPROLBODY, diff --git a/usr.bin/mandoc/main.c b/usr.bin/mandoc/main.c index f88798c383a..31b5455380a 100644 --- a/usr.bin/mandoc/main.c +++ b/usr.bin/mandoc/main.c @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.18 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: main.c,v 1.19 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -17,7 +17,6 @@ #include <sys/stat.h> #include <assert.h> -#include <err.h> #include <fcntl.h> #include <stdio.h> #include <stdint.h> @@ -95,7 +94,7 @@ static struct mdoc *mdoc_init(struct curparse *); __dead static void version(void); __dead static void usage(void); -extern char *__progname; +static const char *progname; int @@ -105,7 +104,13 @@ main(int argc, char *argv[]) struct buf ln, blk; struct curparse curp; - bzero(&curp, sizeof(struct curparse)); + progname = strrchr(argv[0], '/'); + if (progname == NULL) + progname = argv[0]; + else + ++progname; + + memset(&curp, 0, sizeof(struct curparse)); curp.inttype = INTT_AUTO; curp.outtype = OUTT_ASCII; @@ -144,8 +149,8 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - bzero(&ln, sizeof(struct buf)); - bzero(&blk, sizeof(struct buf)); + memset(&ln, 0, sizeof(struct buf)); + memset(&blk, 0, sizeof(struct buf)); rc = 1; @@ -170,11 +175,9 @@ main(int argc, char *argv[]) argv++; if (*argv && rc) { if (curp.lastman) - if ( ! man_reset(curp.lastman)) - rc = 0; + man_reset(curp.lastman); if (curp.lastmdoc) - if ( ! mdoc_reset(curp.lastmdoc)) - rc = 0; + mdoc_reset(curp.lastmdoc); curp.lastman = NULL; curp.lastmdoc = NULL; } @@ -199,7 +202,7 @@ __dead static void version(void) { - (void)printf("%s %s\n", __progname, VERSION); + (void)printf("%s %s\n", progname, VERSION); exit(EXIT_SUCCESS); } @@ -210,7 +213,7 @@ usage(void) (void)fprintf(stderr, "usage: %s [-V] [-foption...] " "[-mformat] [-Ooption] [-Toutput] " - "[-Werr...]\n", __progname); + "[-Werr...]\n", progname); exit(EXIT_FAILURE); } @@ -219,7 +222,6 @@ static struct man * man_init(struct curparse *curp) { int pflags; - struct man *man; struct man_cb mancb; mancb.man_err = merr; @@ -236,10 +238,7 @@ man_init(struct curparse *curp) if (curp->fflags & NO_IGN_ESCAPE) pflags &= ~MAN_IGN_ESCAPE; - if (NULL == (man = man_alloc(curp, pflags, &mancb))) - warnx("memory exhausted"); - - return(man); + return(man_alloc(curp, pflags, &mancb)); } @@ -247,7 +246,6 @@ static struct mdoc * mdoc_init(struct curparse *curp) { int pflags; - struct mdoc *mdoc; struct mdoc_cb mdoccb; mdoccb.mdoc_err = merr; @@ -266,10 +264,7 @@ mdoc_init(struct curparse *curp) if (curp->fflags & NO_IGN_CHARS) pflags &= ~MDOC_IGN_CHARS; - if (NULL == (mdoc = mdoc_alloc(curp, pflags, &mdoccb))) - warnx("memory exhausted"); - - return(mdoc); + return(mdoc_alloc(curp, pflags, &mdoccb)); } @@ -281,14 +276,14 @@ ffile(struct buf *blk, struct buf *ln, curp->file = file; if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) { - warn("%s", curp->file); + perror(curp->file); return(-1); } c = fdesc(blk, ln, curp); if (-1 == close(curp->fd)) - warn("%s", curp->file); + perror(curp->file); return(c); } @@ -315,15 +310,15 @@ fdesc(struct buf *blk, struct buf *ln, struct curparse *curp) */ if (-1 == fstat(curp->fd, &st)) - warn("%s", curp->file); + perror(curp->file); else if ((size_t)st.st_blksize > sz) sz = st.st_blksize; if (sz > blk->sz) { blk->buf = realloc(blk->buf, sz); if (NULL == blk->buf) { - warn("realloc"); - return(-1); + perror(NULL); + exit(EXIT_FAILURE); } blk->sz = sz; } @@ -332,7 +327,7 @@ fdesc(struct buf *blk, struct buf *ln, struct curparse *curp) for (lnn = pos = comment = 0; ; ) { if (-1 == (ssz = read(curp->fd, blk->buf, sz))) { - warn("%s", curp->file); + perror(curp->file); return(-1); } else if (0 == ssz) break; @@ -344,8 +339,8 @@ fdesc(struct buf *blk, struct buf *ln, struct curparse *curp) ln->sz += 256; /* Step-size. */ ln->buf = realloc(ln->buf, ln->sz); if (NULL == ln->buf) { - warn("realloc"); - return(-1); + perror(NULL); + return(EXIT_FAILURE); } } @@ -408,8 +403,7 @@ fdesc(struct buf *blk, struct buf *ln, struct curparse *curp) /* NOTE a parser may not have been assigned, yet. */ if ( ! (man || mdoc)) { - (void)fprintf(stderr, "%s: not a manual\n", - curp->file); + fprintf(stderr, "%s: Not a manual\n", curp->file); return(0); } @@ -524,7 +518,7 @@ moptions(enum intt *tflags, char *arg) else if (0 == strcmp(arg, "an")) *tflags = INTT_MAN; else { - warnx("bad argument: -m%s", arg); + fprintf(stderr, "%s: Bad argument", arg); return(0); } @@ -545,7 +539,7 @@ toptions(enum outt *tflags, char *arg) else if (0 == strcmp(arg, "html")) *tflags = OUTT_HTML; else { - warnx("bad argument: -T%s", arg); + fprintf(stderr, "%s: Bad argument", arg); return(0); } @@ -557,7 +551,7 @@ static int foptions(int *fflags, char *arg) { char *v, *o; - const char *toks[7]; + const char *toks[8]; toks[0] = "ign-scope"; toks[1] = "no-ign-escape"; @@ -565,7 +559,8 @@ foptions(int *fflags, char *arg) toks[3] = "no-ign-chars"; toks[4] = "ign-errors"; toks[5] = "strict"; - toks[6] = NULL; + toks[6] = "ign-escape"; + toks[7] = NULL; while (*arg) { o = arg; @@ -589,8 +584,11 @@ foptions(int *fflags, char *arg) *fflags |= NO_IGN_ESCAPE | NO_IGN_MACRO | NO_IGN_CHARS; break; + case (6): + *fflags &= ~NO_IGN_ESCAPE; + break; default: - warnx("bad argument: -f%s", o); + fprintf(stderr, "%s: Bad argument", o); return(0); } } @@ -619,7 +617,7 @@ woptions(int *wflags, char *arg) *wflags |= WARN_WERR; break; default: - warnx("bad argument: -W%s", o); + fprintf(stderr, "%s: Bad argument", o); return(0); } } diff --git a/usr.bin/mandoc/man.c b/usr.bin/mandoc/man.c index 5321673fda1..eff62501e24 100644 --- a/usr.bin/mandoc/man.c +++ b/usr.bin/mandoc/man.c @@ -1,4 +1,4 @@ -/* $Id: man.c,v 1.15 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: man.c,v 1.16 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -24,10 +24,10 @@ #include <string.h> #include "libman.h" +#include "libmandoc.h" const char *const __man_merrnames[WERRMAX] = { "invalid character", /* WNPRINT */ - "system: malloc error", /* WNMEM */ "invalid manual section", /* WMSEC */ "invalid date format", /* WDATE */ "scope of prior line violated", /* WLNSCOPE */ @@ -67,7 +67,7 @@ static int man_node_append(struct man *, static int man_ptext(struct man *, int, char *); static int man_pmacro(struct man *, int, char *); static void man_free1(struct man *); -static int man_alloc1(struct man *); +static void man_alloc1(struct man *); static int pstring(struct man *, int, int, const char *, size_t); static int macrowarn(struct man *, int, const char *); @@ -89,12 +89,12 @@ man_meta(const struct man *m) } -int +void man_reset(struct man *man) { man_free1(man); - return(man_alloc1(man)); + man_alloc1(man); } @@ -112,19 +112,16 @@ man_alloc(void *data, int pflags, const struct man_cb *cb) { struct man *p; - if (NULL == (p = calloc(1, sizeof(struct man)))) - return(NULL); + p = mandoc_calloc(1, sizeof(struct man)); - if ( ! man_alloc1(p)) { - free(p); - return(NULL); - } + if (cb) + memcpy(&p->cb, cb, sizeof(struct man_cb)); man_hash_init(); - p->data = data; p->pflags = pflags; - (void)memcpy(&p->cb, cb, sizeof(struct man_cb)); + + man_alloc1(p); return(p); } @@ -167,19 +164,16 @@ man_free1(struct man *man) } -static int +static void man_alloc1(struct man *m) { - bzero(&m->meta, sizeof(struct man_meta)); + memset(&m->meta, 0, sizeof(struct man_meta)); m->flags = 0; - m->last = calloc(1, sizeof(struct man_node)); - if (NULL == m->last) - return(0); + m->last = mandoc_calloc(1, sizeof(struct man_node)); m->first = m->last; m->last->type = MAN_ROOT; m->next = MAN_NEXT_CHILD; - return(1); } @@ -246,10 +240,7 @@ man_node_alloc(int line, int pos, enum man_type type, int tok) { struct man_node *p; - p = calloc(1, sizeof(struct man_node)); - if (NULL == p) - return(NULL); - + p = mandoc_calloc(1, sizeof(struct man_node)); p->line = line; p->pos = pos; p->type = type; @@ -264,8 +255,6 @@ man_elem_alloc(struct man *m, int line, int pos, int tok) struct man_node *p; p = man_node_alloc(line, pos, MAN_ELEM, tok); - if (NULL == p) - return(0); if ( ! man_node_append(m, p)) return(0); m->next = MAN_NEXT_CHILD; @@ -279,8 +268,6 @@ man_head_alloc(struct man *m, int line, int pos, int tok) struct man_node *p; p = man_node_alloc(line, pos, MAN_HEAD, tok); - if (NULL == p) - return(0); if ( ! man_node_append(m, p)) return(0); m->next = MAN_NEXT_CHILD; @@ -294,8 +281,6 @@ man_body_alloc(struct man *m, int line, int pos, int tok) struct man_node *p; p = man_node_alloc(line, pos, MAN_BODY, tok); - if (NULL == p) - return(0); if ( ! man_node_append(m, p)) return(0); m->next = MAN_NEXT_CHILD; @@ -309,8 +294,6 @@ man_block_alloc(struct man *m, int line, int pos, int tok) struct man_node *p; p = man_node_alloc(line, pos, MAN_BLOCK, tok); - if (NULL == p) - return(0); if ( ! man_node_append(m, p)) return(0); m->next = MAN_NEXT_CHILD; @@ -326,15 +309,7 @@ pstring(struct man *m, int line, int pos, size_t sv; n = man_node_alloc(line, pos, MAN_TEXT, -1); - if (NULL == n) - return(0); - - n->string = malloc(len + 1); - if (NULL == n->string) { - free(n); - return(0); - } - + n->string = mandoc_malloc(len + 1); sv = strlcpy(n->string, p, len + 1); /* Prohibit truncation. */ diff --git a/usr.bin/mandoc/man.h b/usr.bin/mandoc/man.h index 31841e55322..4abe9a3d695 100644 --- a/usr.bin/mandoc/man.h +++ b/usr.bin/mandoc/man.h @@ -1,4 +1,4 @@ -/* $Id: man.h,v 1.11 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: man.h,v 1.12 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -105,7 +105,7 @@ struct man; void man_free(struct man *); struct man *man_alloc(void *, int, const struct man_cb *); -int man_reset(struct man *); +void man_reset(struct man *); int man_parseln(struct man *, int, char *buf); int man_endparse(struct man *); diff --git a/usr.bin/mandoc/man_action.c b/usr.bin/mandoc/man_action.c index c9a65b5990e..e45de11fcb2 100644 --- a/usr.bin/mandoc/man_action.c +++ b/usr.bin/mandoc/man_action.c @@ -1,4 +1,4 @@ -/* $Id: man_action.c,v 1.9 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: man_action.c,v 1.10 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -17,11 +17,11 @@ #include <sys/utsname.h> #include <assert.h> -#include <errno.h> #include <stdlib.h> #include <string.h> #include "libman.h" +#include "libmandoc.h" struct actions { int (*post)(struct man *); @@ -138,16 +138,13 @@ post_TH(struct man *m) n = m->last->child; assert(n); - - if (NULL == (m->meta.title = strdup(n->string))) - return(man_nerr(m, n, WNMEM)); + m->meta.title = mandoc_strdup(n->string); /* TITLE ->MSEC<- DATE SOURCE VOL */ n = n->next; assert(n); - errno = 0; lval = strtol(n->string, &ep, 10); if (n->string[0] != '\0' && *ep == '\0') m->meta.msec = (int)lval; @@ -167,14 +164,12 @@ post_TH(struct man *m) /* TITLE MSEC DATE ->SOURCE<- VOL */ if (n && (n = n->next)) - if (NULL == (m->meta.source = strdup(n->string))) - return(man_nerr(m, n, WNMEM)); + m->meta.source = mandoc_strdup(n->string); /* TITLE MSEC DATE SOURCE ->VOL<- */ if (n && (n = n->next)) - if (NULL == (m->meta.vol = strdup(n->string))) - return(man_nerr(m, n, WNMEM)); + m->meta.vol = mandoc_strdup(n->string); /* * The end document shouldn't have the prologue macros as part @@ -205,7 +200,7 @@ man_atotime(const char *p) struct tm tm; char *pp; - bzero(&tm, sizeof(struct tm)); + memset(&tm, 0, sizeof(struct tm)); if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp) return(mktime(&tm)); diff --git a/usr.bin/mandoc/man_html.c b/usr.bin/mandoc/man_html.c index d01bc1619e8..83f0d4b37c0 100644 --- a/usr.bin/mandoc/man_html.c +++ b/usr.bin/mandoc/man_html.c @@ -1,4 +1,4 @@ -/* $Id: man_html.c,v 1.2 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: man_html.c,v 1.3 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -18,7 +18,6 @@ #include <assert.h> #include <ctype.h> -#include <err.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -229,7 +228,7 @@ a2width(const struct man_node *n, struct roffsu *su) static int man_root_pre(MAN_ARGS) { - struct htmlpair tag[2]; + struct htmlpair tag[3]; struct tag *t, *tt; char b[BUFSIZ], title[BUFSIZ]; @@ -243,7 +242,9 @@ man_root_pre(MAN_ARGS) PAIR_CLASS_INIT(&tag[0], "header"); bufcat_style(h, "width", "100%"); PAIR_STYLE_INIT(&tag[1], h); - t = print_otag(h, TAG_TABLE, 2, tag); + PAIR_SUMMARY_INIT(&tag[2], "header"); + + t = print_otag(h, TAG_TABLE, 3, tag); tt = print_otag(h, TAG_TR, 0, NULL); bufinit(h); @@ -277,7 +278,7 @@ man_root_pre(MAN_ARGS) static void man_root_post(MAN_ARGS) { - struct htmlpair tag[2]; + struct htmlpair tag[3]; struct tag *t, *tt; char b[DATESIZ]; @@ -286,7 +287,9 @@ man_root_post(MAN_ARGS) PAIR_CLASS_INIT(&tag[0], "footer"); bufcat_style(h, "width", "100%"); PAIR_STYLE_INIT(&tag[1], h); - t = print_otag(h, TAG_TABLE, 2, tag); + PAIR_SUMMARY_INIT(&tag[2], "footer"); + + t = print_otag(h, TAG_TABLE, 3, tag); tt = print_otag(h, TAG_TR, 0, NULL); bufinit(h); @@ -325,6 +328,9 @@ man_br_pre(MAN_ARGS) bufcat_su(h, "height", &su); PAIR_STYLE_INIT(&tag, h); print_otag(h, TAG_DIV, 1, &tag); + /* So the div isn't empty: */ + print_text(h, "\\~"); + return(0); } diff --git a/usr.bin/mandoc/man_term.c b/usr.bin/mandoc/man_term.c index 3fbdd6bc004..0bb7b1eb7bf 100644 --- a/usr.bin/mandoc/man_term.c +++ b/usr.bin/mandoc/man_term.c @@ -1,4 +1,4 @@ -/* $Id: man_term.c,v 1.19 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: man_term.c,v 1.20 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -18,7 +18,6 @@ #include <assert.h> #include <ctype.h> -#include <err.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -934,25 +933,18 @@ print_man_foot(struct termp *p, const struct man_meta *meta) static void -print_man_head(struct termp *p, const struct man_meta *meta) +print_man_head(struct termp *p, const struct man_meta *m) { - char *buf, *title; + char buf[BUFSIZ], title[BUFSIZ]; p->rmargin = p->maxrmargin; p->offset = 0; + buf[0] = title[0] = '\0'; - if (NULL == (buf = malloc(p->rmargin))) - err(EXIT_FAILURE, "malloc"); - if (NULL == (title = malloc(p->rmargin))) - err(EXIT_FAILURE, "malloc"); + if (m->vol) + strlcpy(buf, m->vol, BUFSIZ); - if (meta->vol) - (void)strlcpy(buf, meta->vol, p->rmargin); - else - *buf = 0; - - (void)snprintf(title, p->rmargin, "%s(%d)", - meta->title, meta->msec); + snprintf(title, BUFSIZ, "%s(%d)", m->title, m->msec); p->offset = 0; p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2; @@ -979,8 +971,4 @@ print_man_head(struct termp *p, const struct man_meta *meta) p->rmargin = p->maxrmargin; p->offset = 0; p->flags &= ~TERMP_NOSPACE; - - free(title); - free(buf); } - diff --git a/usr.bin/mandoc/mandoc.1 b/usr.bin/mandoc/mandoc.1 index c2c326bc96c..b80ba628de2 100644 --- a/usr.bin/mandoc/mandoc.1 +++ b/usr.bin/mandoc/mandoc.1 @@ -1,4 +1,4 @@ -.\" $Id: mandoc.1,v 1.18 2009/10/27 21:40:07 schwarze Exp $ +.\" $Id: mandoc.1,v 1.19 2009/12/22 23:58:00 schwarze Exp $ .\" .\" Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se> .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: October 27 2009 $ +.Dd $Mdocdate: December 22 2009 $ .Dt MANDOC 1 .Os . @@ -228,6 +228,11 @@ When rewinding the scope of a block macro, forces the compiler to ignore scope violations. This can seriously mangle the resulting tree. .Pq mdoc only . +.It Fl f Ns Ar ign-escape +Ignore invalid escape sequences. +This is the default, but the option can be used to override an earlier +.Fl f Ns Ar strict . +. .It Fl f Ns Ar no-ign-escape Don't ignore invalid escape sequences. . diff --git a/usr.bin/mandoc/mandoc.c b/usr.bin/mandoc/mandoc.c index e6452f90737..5d2b2a2c890 100644 --- a/usr.bin/mandoc/mandoc.c +++ b/usr.bin/mandoc/mandoc.c @@ -1,4 +1,4 @@ -/* $Id: mandoc.c,v 1.3 2009/08/22 15:18:11 schwarze Exp $ */ +/* $Id: mandoc.c,v 1.4 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -19,6 +19,8 @@ #include <assert.h> #include <ctype.h> #include <stdlib.h> +#include <stdio.h> +#include <string.h> #include "libmandoc.h" @@ -103,3 +105,61 @@ mandoc_special(const char *p) return(*p == ']' ? c : 0); } + +void * +mandoc_calloc(size_t num, size_t size) +{ + void *ptr; + + ptr = calloc(num, size); + if (NULL == ptr) { + perror(NULL); + exit(EXIT_FAILURE); + } + + return(ptr); +} + + +void * +mandoc_malloc(size_t size) +{ + void *ptr; + + ptr = malloc(size); + if (NULL == ptr) { + perror(NULL); + exit(EXIT_FAILURE); + } + + return(ptr); +} + + +void * +mandoc_realloc(void *ptr, size_t size) +{ + + ptr = realloc(ptr, size); + if (NULL == ptr) { + perror(NULL); + exit(EXIT_FAILURE); + } + + return(ptr); +} + + +char * +mandoc_strdup(const char *ptr) +{ + char *p; + + p = strdup(ptr); + if (NULL == p) { + perror(NULL); + exit(EXIT_FAILURE); + } + + return(p); +} diff --git a/usr.bin/mandoc/mdoc.c b/usr.bin/mandoc/mdoc.c index e2ed95f8c8e..ae69cb2f05c 100644 --- a/usr.bin/mandoc/mdoc.c +++ b/usr.bin/mandoc/mdoc.c @@ -1,4 +1,4 @@ -/* $Id: mdoc.c,v 1.31 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: mdoc.c,v 1.32 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -24,12 +24,12 @@ #include <string.h> #include "libmdoc.h" +#include "libmandoc.h" const char *const __mdoc_merrnames[MERRMAX] = { "trailing whitespace", /* ETAILWS */ "unexpected quoted parameter", /* EQUOTPARM */ "unterminated quoted parameter", /* EQUOTTERM */ - "system: malloc error", /* EMALLOC */ "argument parameter suggested", /* EARGVAL */ "macro disallowed in prologue", /* EBODYPROL */ "macro disallowed in body", /* EPROLBODY */ @@ -137,7 +137,7 @@ const char * const *mdoc_macronames = __mdoc_macronames; const char * const *mdoc_argnames = __mdoc_argnames; static void mdoc_free1(struct mdoc *); -static int mdoc_alloc1(struct mdoc *); +static void mdoc_alloc1(struct mdoc *); static struct mdoc_node *node_alloc(struct mdoc *, int, int, int, enum mdoc_type); static int node_append(struct mdoc *, @@ -190,21 +190,17 @@ mdoc_free1(struct mdoc *mdoc) /* * Allocate all volatile resources (parse tree, meta-data, fields). */ -static int +static void mdoc_alloc1(struct mdoc *mdoc) { - bzero(&mdoc->meta, sizeof(struct mdoc_meta)); + memset(&mdoc->meta, 0, sizeof(struct mdoc_meta)); mdoc->flags = 0; mdoc->lastnamed = mdoc->lastsec = SEC_NONE; - mdoc->last = calloc(1, sizeof(struct mdoc_node)); - if (NULL == mdoc->last) - return(0); - + mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node)); mdoc->first = mdoc->last; mdoc->last->type = MDOC_ROOT; mdoc->next = MDOC_NEXT_CHILD; - return(1); } @@ -214,12 +210,12 @@ mdoc_alloc1(struct mdoc *mdoc) * and the parser is ready for re-invocation on a new tree; however, * cross-parse non-volatile data is kept intact. */ -int +void mdoc_reset(struct mdoc *mdoc) { mdoc_free1(mdoc); - return(mdoc_alloc1(mdoc)); + mdoc_alloc1(mdoc); } @@ -244,21 +240,17 @@ mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb) { struct mdoc *p; - if (NULL == (p = calloc(1, sizeof(struct mdoc)))) - return(NULL); - if (cb) - (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb)); + p = mandoc_calloc(1, sizeof(struct mdoc)); - mdoc_hash_init(); + if (cb) + memcpy(&p->cb, cb, sizeof(struct mdoc_cb)); p->data = data; p->pflags = pflags; - if (mdoc_alloc1(p)) - return(p); - - free(p); - return(NULL); + mdoc_hash_init(); + mdoc_alloc1(p); + return(p); } @@ -434,11 +426,7 @@ node_alloc(struct mdoc *m, int line, { struct mdoc_node *p; - if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) { - (void)mdoc_nerr(m, m->last, EMALLOC); - return(NULL); - } - + p = mandoc_calloc(1, sizeof(struct mdoc_node)); p->sec = m->lastsec; p->line = line; p->pos = pos; @@ -456,8 +444,6 @@ mdoc_tail_alloc(struct mdoc *m, int line, int pos, int tok) struct mdoc_node *p; p = node_alloc(m, line, pos, tok, MDOC_TAIL); - if (NULL == p) - return(0); if ( ! node_append(m, p)) return(0); m->next = MDOC_NEXT_CHILD; @@ -474,8 +460,6 @@ mdoc_head_alloc(struct mdoc *m, int line, int pos, int tok) assert(m->last); p = node_alloc(m, line, pos, tok, MDOC_HEAD); - if (NULL == p) - return(0); if ( ! node_append(m, p)) return(0); m->next = MDOC_NEXT_CHILD; @@ -489,8 +473,6 @@ mdoc_body_alloc(struct mdoc *m, int line, int pos, int tok) struct mdoc_node *p; p = node_alloc(m, line, pos, tok, MDOC_BODY); - if (NULL == p) - return(0); if ( ! node_append(m, p)) return(0); m->next = MDOC_NEXT_CHILD; @@ -505,8 +487,6 @@ mdoc_block_alloc(struct mdoc *m, int line, int pos, struct mdoc_node *p; p = node_alloc(m, line, pos, tok, MDOC_BLOCK); - if (NULL == p) - return(0); p->args = args; if (p->args) (args->refcnt)++; @@ -524,8 +504,6 @@ mdoc_elem_alloc(struct mdoc *m, int line, int pos, struct mdoc_node *p; p = node_alloc(m, line, pos, tok, MDOC_ELEM); - if (NULL == p) - return(0); p->args = args; if (p->args) (args->refcnt)++; @@ -543,15 +521,7 @@ pstring(struct mdoc *m, int line, int pos, const char *p, size_t len) size_t sv; n = node_alloc(m, line, pos, -1, MDOC_TEXT); - if (NULL == n) - return(mdoc_nerr(m, m->last, EMALLOC)); - - n->string = malloc(len + 1); - if (NULL == n->string) { - free(n); - return(mdoc_nerr(m, m->last, EMALLOC)); - } - + n->string = mandoc_malloc(len + 1); sv = strlcpy(n->string, p, len + 1); /* Prohibit truncation. */ diff --git a/usr.bin/mandoc/mdoc.h b/usr.bin/mandoc/mdoc.h index 648f4818ca1..732aa1607a5 100644 --- a/usr.bin/mandoc/mdoc.h +++ b/usr.bin/mandoc/mdoc.h @@ -1,4 +1,4 @@ -/* $Id: mdoc.h,v 1.15 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: mdoc.h,v 1.16 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -292,7 +292,7 @@ struct mdoc; void mdoc_free(struct mdoc *); struct mdoc *mdoc_alloc(void *, int, const struct mdoc_cb *); -int mdoc_reset(struct mdoc *); +void mdoc_reset(struct mdoc *); int mdoc_parseln(struct mdoc *, int, char *buf); const struct mdoc_node *mdoc_node(const struct mdoc *); const struct mdoc_meta *mdoc_meta(const struct mdoc *); diff --git a/usr.bin/mandoc/mdoc_action.c b/usr.bin/mandoc/mdoc_action.c index 2c62096fec9..488faca4e26 100644 --- a/usr.bin/mandoc/mdoc_action.c +++ b/usr.bin/mandoc/mdoc_action.c @@ -1,4 +1,4 @@ -/* $Id: mdoc_action.c,v 1.24 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: mdoc_action.c,v 1.25 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -19,25 +19,27 @@ #endif #include <assert.h> -#include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include "libmdoc.h" +#include "libmandoc.h" #define POST_ARGS struct mdoc *m, struct mdoc_node *n #define PRE_ARGS struct mdoc *m, const struct mdoc_node *n +#define NUMSIZ 32 +#define DATESIZ 32 + struct actions { int (*pre)(PRE_ARGS); int (*post)(POST_ARGS); }; -static int concat(struct mdoc *, - const struct mdoc_node *, - char *, size_t); +static int concat(struct mdoc *, char *, + const struct mdoc_node *, size_t); static inline int order_rs(int); static int post_ar(POST_ARGS); @@ -52,12 +54,12 @@ static int post_dt(POST_ARGS); static int post_lb(POST_ARGS); static int post_nm(POST_ARGS); static int post_os(POST_ARGS); +static int post_pa(POST_ARGS); static int post_prol(POST_ARGS); static int post_rs(POST_ARGS); static int post_sh(POST_ARGS); static int post_st(POST_ARGS); static int post_std(POST_ARGS); -static int post_tilde(POST_ARGS); static int pre_bd(PRE_ARGS); static int pre_bl(PRE_ARGS); @@ -100,7 +102,7 @@ static const struct actions mdoc_actions[MDOC_MAX] = { { NULL, post_nm }, /* Nm */ { NULL, NULL }, /* Op */ { NULL, NULL }, /* Ot */ - { NULL, post_tilde }, /* Pa */ + { NULL, post_pa }, /* Pa */ { NULL, post_std }, /* Rv */ { NULL, post_st }, /* St */ { NULL, NULL }, /* Va */ @@ -250,18 +252,24 @@ mdoc_action_post(struct mdoc *m) } +/* + * Concatenate sibling nodes together. All siblings must be of type + * MDOC_TEXT or an assertion is raised. Concatenation is separated by a + * single whitespace. + */ static int -concat(struct mdoc *m, const struct mdoc_node *n, - char *buf, size_t sz) +concat(struct mdoc *m, char *p, const struct mdoc_node *n, size_t sz) { + assert(sz); + p[0] = '\0'; for ( ; n; n = n->next) { assert(MDOC_TEXT == n->type); - if (strlcat(buf, n->string, sz) >= sz) + if (strlcat(p, n->string, sz) >= sz) return(mdoc_nerr(m, n, ETOOLONG)); if (NULL == n->next) continue; - if (strlcat(buf, " ", sz) >= sz) + if (strlcat(p, " ", sz) >= sz) return(mdoc_nerr(m, n, ETOOLONG)); } @@ -269,6 +277,10 @@ concat(struct mdoc *m, const struct mdoc_node *n, } +/* + * Macros accepting `-std' as an argument have the name of the current + * document (`Nm') filled in as the argument if it's not provided. + */ static int post_std(POST_ARGS) { @@ -283,29 +295,34 @@ post_std(POST_ARGS) if ( ! mdoc_word_alloc(m, n->line, n->pos, m->meta.name)) return(0); m->last = nn; - return(1); } +/* + * The `Nm' macro's first use sets the name of the document. See also + * post_std(), etc. + */ static int post_nm(POST_ARGS) { - char buf[64]; + char buf[BUFSIZ]; if (m->meta.name) return(1); - - buf[0] = 0; - if ( ! concat(m, n->child, buf, sizeof(buf))) + if ( ! concat(m, buf, n->child, BUFSIZ)) return(0); - if (NULL == (m->meta.name = strdup(buf))) - return(mdoc_nerr(m, n, EMALLOC)); - + m->meta.name = mandoc_strdup(buf); return(1); } +/* + * Look up the value of `Lb' for matching predefined strings. If it has + * one, then substitute the current value for the formatted value. Note + * that the lookup may fail (we can provide arbitrary strings). + */ +/* ARGSUSED */ static int post_lb(POST_ARGS) { @@ -315,28 +332,29 @@ post_lb(POST_ARGS) assert(MDOC_TEXT == n->child->type); p = mdoc_a2lib(n->child->string); - if (NULL == p) { - sz = strlen(n->child->string) + - 2 + strlen("\\(lqlibrary\\(rq"); - buf = malloc(sz); - if (NULL == buf) - return(mdoc_nerr(m, n, EMALLOC)); - (void)snprintf(buf, sz, "library \\(lq%s\\(rq", - n->child->string); + + if (p) { free(n->child->string); - n->child->string = buf; + n->child->string = mandoc_strdup(p); return(1); } + sz = strlen(n->child->string) + + 2 + strlen("\\(lqlibrary\\(rq"); + buf = mandoc_malloc(sz); + snprintf(buf, sz, "library \\(lq%s\\(rq", n->child->string); free(n->child->string); - n->child->string = strdup(p); - if (NULL == n->child->string) - return(mdoc_nerr(m, n, EMALLOC)); - + n->child->string = buf; return(1); } +/* + * Substitute the value of `St' for the corresponding formatted string. + * We're guaranteed that this exists (it's been verified during the + * validation phase). + */ +/* ARGSUSED */ static int post_st(POST_ARGS) { @@ -346,14 +364,16 @@ post_st(POST_ARGS) p = mdoc_a2st(n->child->string); assert(p); free(n->child->string); - n->child->string = strdup(p); - if (NULL == n->child->string) - return(mdoc_nerr(m, n, EMALLOC)); - + n->child->string = mandoc_strdup(p); return(1); } +/* + * Look up the standard string in a table. We know that it exists from + * the validation phase, so assert on failure. If a standard key wasn't + * supplied, supply the default ``AT&T UNIX''. + */ static int post_at(POST_ARGS) { @@ -365,44 +385,42 @@ post_at(POST_ARGS) p = mdoc_a2att(n->child->string); assert(p); free(n->child->string); - n->child->string = strdup(p); - if (NULL == n->child->string) - return(mdoc_nerr(m, n, EMALLOC)); + n->child->string = mandoc_strdup(p); return(1); } nn = n; m->next = MDOC_NEXT_CHILD; - if ( ! mdoc_word_alloc(m, nn->line, nn->pos, "AT&T UNIX")) return(0); m->last = nn; - return(1); } +/* + * Mark the current section. The ``named'' section (lastnamed) is set + * whenever the current section isn't a custom section--we use this to + * keep track of section ordering. Also check that the section is + * allowed within the document's manual section. + */ static int post_sh(POST_ARGS) { enum mdoc_sec sec; - char buf[64]; - - /* - * We keep track of the current section /and/ the "named" - * section, which is one of the conventional ones, in order to - * check ordering. - */ + char buf[BUFSIZ]; if (MDOC_HEAD != n->type) return(1); - buf[0] = 0; - if ( ! concat(m, n->child, buf, sizeof(buf))) + if ( ! concat(m, buf, n->child, BUFSIZ)) return(0); - if (SEC_CUSTOM != (sec = mdoc_atosec(buf))) + sec = mdoc_atosec(buf); + if (SEC_CUSTOM != sec) m->lastnamed = sec; + /* Some sections only live in certain manual sections. */ + switch ((m->lastsec = sec)) { case (SEC_RETURN_VALUES): /* FALLTHROUGH */ @@ -425,6 +443,10 @@ post_sh(POST_ARGS) } +/* + * Parse out the contents of `Dt'. See in-line documentation for how we + * handle the various fields of this macro. + */ static int post_dt(POST_ARGS) { @@ -448,10 +470,9 @@ post_dt(POST_ARGS) */ if (NULL == (nn = n->child)) { - if (NULL == (m->meta.title = strdup("unknown"))) - return(mdoc_nerr(m, n, EMALLOC)); - if (NULL == (m->meta.vol = strdup("local"))) - return(mdoc_nerr(m, n, EMALLOC)); + /* XXX: make these macro values. */ + m->meta.title = mandoc_strdup("unknown"); + m->meta.vol = mandoc_strdup("local"); return(post_prol(m, n)); } @@ -459,12 +480,11 @@ post_dt(POST_ARGS) * --> title = TITLE, volume = local, msec = 0, arch = NULL */ - if (NULL == (m->meta.title = strdup(nn->string))) - return(mdoc_nerr(m, n, EMALLOC)); + m->meta.title = mandoc_strdup(nn->string); if (NULL == (nn = nn->next)) { - if (NULL == (m->meta.vol = strdup("local"))) - return(mdoc_nerr(m, n, EMALLOC)); + /* XXX: make this a macro value. */ + m->meta.vol = mandoc_strdup("local"); return(post_prol(m, n)); } @@ -477,14 +497,13 @@ post_dt(POST_ARGS) cp = mdoc_a2msec(nn->string); if (cp) { - if (NULL == (m->meta.vol = strdup(cp))) - return(mdoc_nerr(m, n, EMALLOC)); - errno = 0; + /* FIXME: where is strtonum!? */ + m->meta.vol = mandoc_strdup(cp); lval = strtol(nn->string, &ep, 10); if (nn->string[0] != '\0' && *ep == '\0') m->meta.msec = (int)lval; - } else if (NULL == (m->meta.vol = strdup(nn->string))) - return(mdoc_nerr(m, n, EMALLOC)); + } else + m->meta.vol = mandoc_strdup(nn->string); if (NULL == (nn = nn->next)) return(post_prol(m, n)); @@ -499,104 +518,99 @@ post_dt(POST_ARGS) cp = mdoc_a2vol(nn->string); if (cp) { free(m->meta.vol); - if (NULL == (m->meta.vol = strdup(cp))) - return(mdoc_nerr(m, n, EMALLOC)); + m->meta.vol = mandoc_strdup(cp); } else { cp = mdoc_a2arch(nn->string); if (NULL == cp) { free(m->meta.vol); - if (NULL == (m->meta.vol = strdup(nn->string))) - return(mdoc_nerr(m, n, EMALLOC)); - } else if (NULL == (m->meta.arch = strdup(cp))) - return(mdoc_nerr(m, n, EMALLOC)); + m->meta.vol = mandoc_strdup(nn->string); + } else + m->meta.arch = mandoc_strdup(cp); } /* Ignore any subsequent parameters... */ + /* FIXME: warn about subsequent parameters. */ return(post_prol(m, n)); } +/* + * Set the operating system by way of the `Os' macro. Note that if an + * argument isn't provided and -DOSNAME="\"foo\"" is provided during + * compilation, this value will be used instead of filling in "sysname + * release" from uname(). + */ static int post_os(POST_ARGS) { - char buf[64]; -#ifndef OSNAME + char buf[BUFSIZ]; +#ifndef OSNAME struct utsname utsname; #endif - /* - * Setting OSNAME to be the name of the target operating system, - * e.g., "OpenBSD 4.4", will result in the compile-time constant - * by supplied instead of the value in uname(). - */ - if (m->meta.os) free(m->meta.os); - buf[0] = 0; - if ( ! concat(m, n->child, buf, sizeof(buf))) + if ( ! concat(m, buf, n->child, BUFSIZ)) return(0); - if (0 == buf[0]) { -#ifdef OSNAME - if (strlcat(buf, OSNAME, 64) >= 64) + if ('\0' == buf[0]) { +#ifdef OSNAME + if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) return(mdoc_nerr(m, n, EUTSNAME)); -#else +#else /*!OSNAME */ if (-1 == uname(&utsname)) return(mdoc_nerr(m, n, EUTSNAME)); - if (strlcat(buf, utsname.sysname, 64) >= 64) + if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) return(mdoc_nerr(m, n, ETOOLONG)); - if (strlcat(buf, " ", 64) >= 64) + if (strlcat(buf, " ", 64) >= BUFSIZ) return(mdoc_nerr(m, n, ETOOLONG)); - if (strlcat(buf, utsname.release, 64) >= 64) + if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) return(mdoc_nerr(m, n, ETOOLONG)); -#endif +#endif /*!OSNAME*/ } - if (NULL == (m->meta.os = strdup(buf))) - return(mdoc_nerr(m, n, EMALLOC)); - + m->meta.os = mandoc_strdup(buf); return(post_prol(m, n)); } /* * Calculate the -width for a `Bl -tag' list if it hasn't been provided. - * Uses the first head macro. + * Uses the first head macro. NOTE AGAIN: this is ONLY if the -width + * argument has NOT been provided. See post_bl_width() for converting + * the -width string. */ static int post_bl_tagwidth(POST_ARGS) { - struct mdoc_node *nn; - int sz; - char buf[32]; + struct mdoc_node *nn; + size_t sz; + int i; + char buf[NUMSIZ]; - /* - * Use the text width, if a text node, or the default macro - * width if a macro. - */ + /* Defaults to ten ens. */ + sz = 10; /* XXX: make this a macro value. */ nn = n->body->child; + if (nn) { assert(MDOC_BLOCK == nn->type); assert(MDOC_It == nn->tok); nn = nn->head->child; - } - - sz = 10; /* Default size. */ - - if (nn) { if (MDOC_TEXT != nn->type) { - if (0 == (sz = (int)mdoc_macro2len(nn->tok))) + sz = mdoc_macro2len(nn->tok); + if (sz == 0) { if ( ! mdoc_nwarn(m, n, ENOWIDTH)) return(0); + sz = 10; + } } else - sz = (int)strlen(nn->string) + 1; + sz = strlen(nn->string) + 1; } - if (-1 == snprintf(buf, sizeof(buf), "%dn", sz)) - return(mdoc_nerr(m, n, ENUMFMT)); + snprintf(buf, NUMSIZ, "%zun", sz); /* * We have to dynamically add this to the macro's argument list. @@ -605,35 +619,32 @@ post_bl_tagwidth(POST_ARGS) nn = n; assert(nn->args); - sz = (int)(nn->args->argc)++; + i = (int)(nn->args->argc)++; - nn->args->argv = realloc(nn->args->argv, + nn->args->argv = mandoc_realloc(nn->args->argv, nn->args->argc * sizeof(struct mdoc_argv)); - if (NULL == nn->args->argv) - return(mdoc_nerr(m, n, EMALLOC)); - - nn->args->argv[sz].arg = MDOC_Width; - nn->args->argv[sz].line = n->line; - nn->args->argv[sz].pos = n->pos; - nn->args->argv[sz].sz = 1; - nn->args->argv[sz].value = calloc(1, sizeof(char *)); - - if (NULL == nn->args->argv[sz].value) - return(mdoc_nerr(m, n, EMALLOC)); - if (NULL == (nn->args->argv[sz].value[0] = strdup(buf))) - return(mdoc_nerr(m, n, EMALLOC)); - + nn->args->argv[i].arg = MDOC_Width; + nn->args->argv[i].line = n->line; + nn->args->argv[i].pos = n->pos; + nn->args->argv[i].sz = 1; + nn->args->argv[i].value = mandoc_malloc(sizeof(char *)); + nn->args->argv[i].value[0] = mandoc_strdup(buf); return(1); } +/* + * Calculate the real width of a list from the -width string, which may + * contain a macro (with a known default width), a literal string, or a + * scaling width. + */ static int post_bl_width(POST_ARGS) { size_t width; int i, tok; - char buf[32]; + char buf[NUMSIZ]; char *p; if (NULL == n->args) @@ -653,6 +664,7 @@ post_bl_width(POST_ARGS) */ if (0 == strcmp(p, "Ds")) + /* XXX: make into a macro. */ width = 6; else if (MDOC_MAX == (tok = mdoc_hash_find(p))) return(1); @@ -661,18 +673,17 @@ post_bl_width(POST_ARGS) /* The value already exists: free and reallocate it. */ - if (-1 == snprintf(buf, sizeof(buf), "%zun", width)) - return(mdoc_nerr(m, n, ENUMFMT)); - + snprintf(buf, NUMSIZ, "%zun", width); free(n->args->argv[i].value[0]); - n->args->argv[i].value[0] = strdup(buf); - if (NULL == n->args->argv[i].value[0]) - return(mdoc_nerr(m, n, EMALLOC)); - + n->args->argv[i].value[0] = mandoc_strdup(buf); return(1); } +/* + * Do processing for -column lists, which can have two distinct styles + * of invocation. Merge this two styles into a consistent form. + */ /* ARGSUSED */ static int post_bl_head(POST_ARGS) @@ -690,11 +701,8 @@ post_bl_head(POST_ARGS) if (MDOC_Column == np->args->argv[c].arg) break; - /* Only process -column. */ - if (c == (int)np->args->argc) return(1); - assert(0 == np->args->argv[c].sz); /* @@ -704,7 +712,7 @@ post_bl_head(POST_ARGS) */ np->args->argv[c].sz = (size_t)n->nchild; - np->args->argv[c].value = malloc + np->args->argv[c].value = mandoc_malloc ((size_t)n->nchild * sizeof(char *)); for (i = 0, nn = n->child; nn; i++) { @@ -717,7 +725,6 @@ post_bl_head(POST_ARGS) n->nchild = 0; n->child = NULL; - return(1); } @@ -760,8 +767,12 @@ post_bl(POST_ARGS) } +/* + * The `Pa' macro defaults to a tilde if no value is provided as an + * argument. + */ static int -post_tilde(POST_ARGS) +post_pa(POST_ARGS) { struct mdoc_node *np; @@ -770,15 +781,18 @@ post_tilde(POST_ARGS) np = n; m->next = MDOC_NEXT_CHILD; - + /* XXX: make into macro value. */ if ( ! mdoc_word_alloc(m, n->line, n->pos, "~")) return(0); m->last = np; - return(1); } +/* + * The `Ar' macro defaults to two strings "file ..." if no value is + * provided as an argument. + */ static int post_ar(POST_ARGS) { @@ -789,26 +803,30 @@ post_ar(POST_ARGS) np = n; m->next = MDOC_NEXT_CHILD; + /* XXX: make into macro values. */ if ( ! mdoc_word_alloc(m, n->line, n->pos, "file")) return(0); if ( ! mdoc_word_alloc(m, n->line, n->pos, "...")) return(0); m->last = np; - return(1); } +/* + * Parse the date field in `Dd', primarily through mdoc_atotime(). + * FIXME: push mdoc_atotime() into here. + */ static int post_dd(POST_ARGS) { - char buf[64]; + char buf[DATESIZ]; - buf[0] = 0; - if ( ! concat(m, n->child, buf, sizeof(buf))) + if ( ! concat(m, buf, n->child, DATESIZ)) return(0); - if (0 == (m->meta.date = mdoc_atotime(buf))) { + m->meta.date = mdoc_atotime(buf); + if (0 == m->meta.date) { if ( ! mdoc_nwarn(m, n, EBADDATE)) return(0); m->meta.date = time(NULL); @@ -818,13 +836,16 @@ post_dd(POST_ARGS) } +/* + * Remove prologue macros from the document after they're processed. + * The final document uses mdoc_meta for these values and discards the + * originals. + */ static int post_prol(POST_ARGS) { struct mdoc_node *np; - /* Remove prologue macros from AST. */ - if (n->parent->child == n) n->parent->child = n->prev; if (n->prev) @@ -850,6 +871,9 @@ post_prol(POST_ARGS) } +/* + * Trigger a literal context. + */ static int pre_dl(PRE_ARGS) { @@ -860,6 +884,7 @@ pre_dl(PRE_ARGS) } +/* ARGSUSED */ static int pre_offset(PRE_ARGS) { @@ -878,14 +903,9 @@ pre_offset(PRE_ARGS) break; assert(1 == n->args->refcnt); /* If no value set, length of <string>. */ - n->args->argv[i].value = - calloc(1, sizeof(char *)); - if (NULL == n->args->argv[i].value) - return(mdoc_nerr(m, n, EMALLOC)); n->args->argv[i].sz++; - n->args->argv[i].value[0] = strdup("8n"); - if (NULL == n->args->argv[i].value[0]) - return(mdoc_nerr(m, n, EMALLOC)); + n->args->argv[i].value = mandoc_malloc(sizeof(char *)); + n->args->argv[i].value[0] = mandoc_strdup("8n"); break; } diff --git a/usr.bin/mandoc/mdoc_argv.c b/usr.bin/mandoc/mdoc_argv.c index fbc435bf588..0b4f4c7c918 100644 --- a/usr.bin/mandoc/mdoc_argv.c +++ b/usr.bin/mandoc/mdoc_argv.c @@ -1,4 +1,4 @@ -/* $Id: mdoc_argv.c,v 1.18 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: mdoc_argv.c,v 1.19 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -23,6 +23,7 @@ #include <string.h> #include "libmdoc.h" +#include "libmandoc.h" /* * Routines to parse arguments of macros. Arguments follow the syntax @@ -266,23 +267,12 @@ mdoc_argv(struct mdoc *m, int line, int tok, if ( ! argv(m, line, &tmp, pos, buf)) return(ARGV_ERROR); - if (NULL == (arg = *v)) { - *v = calloc(1, sizeof(struct mdoc_arg)); - if (NULL == *v) { - (void)mdoc_nerr(m, m->last, EMALLOC); - return(ARGV_ERROR); - } - arg = *v; - } + if (NULL == (arg = *v)) + arg = *v = mandoc_calloc(1, sizeof(struct mdoc_arg)); arg->argc++; - arg->argv = realloc(arg->argv, arg->argc * - sizeof(struct mdoc_argv)); - - if (NULL == arg->argv) { - (void)mdoc_nerr(m, m->last, EMALLOC); - return(ARGV_ERROR); - } + arg->argv = mandoc_realloc + (arg->argv, arg->argc * sizeof(struct mdoc_argv)); (void)memcpy(&arg->argv[(int)arg->argc - 1], &tmp, sizeof(struct mdoc_argv)); @@ -673,16 +663,11 @@ argv_multi(struct mdoc *m, int line, else if (ARGS_EOLN == c) break; - if (0 == v->sz % MULTI_STEP) { - v->value = realloc(v->value, + if (0 == v->sz % MULTI_STEP) + v->value = mandoc_realloc(v->value, (v->sz + MULTI_STEP) * sizeof(char *)); - if (NULL == v->value) { - (void)mdoc_nerr(m, m->last, EMALLOC); - return(ARGV_ERROR); - } - } - if (NULL == (v->value[(int)v->sz] = strdup(p))) - return(mdoc_nerr(m, m->last, EMALLOC)); + + v->value[(int)v->sz] = mandoc_strdup(p); } return(1); @@ -706,10 +691,8 @@ argv_opt_single(struct mdoc *m, int line, return(1); v->sz = 1; - if (NULL == (v->value = calloc(1, sizeof(char *)))) - return(mdoc_nerr(m, m->last, EMALLOC)); - if (NULL == (v->value[0] = strdup(p))) - return(mdoc_nerr(m, m->last, EMALLOC)); + v->value = mandoc_malloc(sizeof(char *)); + v->value[0] = mandoc_strdup(p); return(1); } @@ -734,10 +717,8 @@ argv_single(struct mdoc *m, int line, return(mdoc_perr(m, line, ppos, EARGVAL)); v->sz = 1; - if (NULL == (v->value = calloc(1, sizeof(char *)))) - return(mdoc_nerr(m, m->last, EMALLOC)); - if (NULL == (v->value[0] = strdup(p))) - return(mdoc_nerr(m, m->last, EMALLOC)); + v->value = mandoc_malloc(sizeof(char *)); + v->value[0] = mandoc_strdup(p); return(1); } diff --git a/usr.bin/mandoc/mdoc_html.c b/usr.bin/mandoc/mdoc_html.c index d3840f27d52..d88ac8abb92 100644 --- a/usr.bin/mandoc/mdoc_html.c +++ b/usr.bin/mandoc/mdoc_html.c @@ -1,4 +1,4 @@ -/* $Id: mdoc_html.c,v 1.2 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: mdoc_html.c,v 1.3 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -19,7 +19,6 @@ #include <assert.h> #include <ctype.h> -#include <err.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -451,7 +450,7 @@ print_mdoc_node(MDOC_ARGS) static void mdoc_root_post(MDOC_ARGS) { - struct htmlpair tag[2]; + struct htmlpair tag[3]; struct tag *t, *tt; char b[DATESIZ]; @@ -466,7 +465,9 @@ mdoc_root_post(MDOC_ARGS) PAIR_CLASS_INIT(&tag[0], "footer"); bufcat_style(h, "width", "100%"); PAIR_STYLE_INIT(&tag[1], h); - t = print_otag(h, TAG_TABLE, 2, tag); + PAIR_SUMMARY_INIT(&tag[2], "footer"); + + t = print_otag(h, TAG_TABLE, 3, tag); tt = print_otag(h, TAG_TR, 0, NULL); bufinit(h); @@ -490,7 +491,7 @@ mdoc_root_post(MDOC_ARGS) static int mdoc_root_pre(MDOC_ARGS) { - struct htmlpair tag[2]; + struct htmlpair tag[3]; struct tag *t, *tt; char b[BUFSIZ], title[BUFSIZ]; @@ -510,7 +511,10 @@ mdoc_root_pre(MDOC_ARGS) PAIR_CLASS_INIT(&tag[0], "header"); bufcat_style(h, "width", "100%"); PAIR_STYLE_INIT(&tag[1], h); - t = print_otag(h, TAG_TABLE, 2, tag); + PAIR_SUMMARY_INIT(&tag[2], "header"); + + t = print_otag(h, TAG_TABLE, 3, tag); + tt = print_otag(h, TAG_TR, 0, NULL); bufinit(h); @@ -546,7 +550,7 @@ mdoc_sh_pre(MDOC_ARGS) { struct htmlpair tag[2]; const struct mdoc_node *nn; - char lbuf[BUFSIZ]; + char buf[BUFSIZ]; struct roffsu su; if (MDOC_BODY == n->type) { @@ -573,11 +577,11 @@ mdoc_sh_pre(MDOC_ARGS) return(1); } - lbuf[0] = 0; + buf[0] = '\0'; for (nn = n->child; nn; nn = nn->next) { - (void)strlcat(lbuf, nn->string, BUFSIZ); + html_idcat(buf, nn->string, BUFSIZ); if (nn->next) - (void)strlcat(lbuf, "_", BUFSIZ); + html_idcat(buf, " ", BUFSIZ); } /* @@ -587,7 +591,7 @@ mdoc_sh_pre(MDOC_ARGS) PAIR_CLASS_INIT(&tag[0], "sec-head"); tag[1].key = ATTR_ID; - tag[1].val = lbuf; + tag[1].val = buf; print_otag(h, TAG_DIV, 2, tag); return(1); } @@ -599,7 +603,7 @@ mdoc_ss_pre(MDOC_ARGS) { struct htmlpair tag[3]; const struct mdoc_node *nn; - char lbuf[BUFSIZ]; + char buf[BUFSIZ]; struct roffsu su; SCALE_VS_INIT(&su, 1); @@ -626,11 +630,11 @@ mdoc_ss_pre(MDOC_ARGS) /* TODO: see note in mdoc_sh_pre() about duplicates. */ - lbuf[0] = 0; + buf[0] = '\0'; for (nn = n->child; nn; nn = nn->next) { - (void)strlcat(lbuf, nn->string, BUFSIZ); + html_idcat(buf, nn->string, BUFSIZ); if (nn->next) - (void)strlcat(lbuf, "_", BUFSIZ); + html_idcat(buf, " ", BUFSIZ); } SCALE_HS_INIT(&su, INDENT - HALFINDENT); @@ -640,7 +644,7 @@ mdoc_ss_pre(MDOC_ARGS) PAIR_CLASS_INIT(&tag[0], "ssec-head"); PAIR_STYLE_INIT(&tag[1], h); tag[2].key = ATTR_ID; - tag[2].val = lbuf; + tag[2].val = buf; print_otag(h, TAG_DIV, 3, tag); return(1); } @@ -944,8 +948,7 @@ mdoc_it_head_pre(MDOC_ARGS, int type, struct roffsu *width) case (MDOC_Item): /* FALLTHROUGH */ case (MDOC_Ohang): - print_otag(h, TAG_DIV, 0, NULL); - break; + return(0); case (MDOC_Column): bufcat_su(h, "min-width", width); bufcat_style(h, "clear", "none"); @@ -1106,8 +1109,10 @@ mdoc_bl_pre(MDOC_ARGS) return(1); ord = malloc(sizeof(struct ord)); - if (NULL == ord) - err(EXIT_FAILURE, "malloc"); + if (NULL == ord) { + perror(NULL); + exit(EXIT_FAILURE); + } ord->cookie = n; ord->pos = 1; ord->next = h->ords.head; @@ -1287,11 +1292,11 @@ mdoc_sx_pre(MDOC_ARGS) /* FIXME: duplicates? */ - (void)strlcpy(buf, "#", BUFSIZ); + strlcpy(buf, "#", BUFSIZ); for (nn = n->child; nn; nn = nn->next) { - (void)strlcat(buf, nn->string, BUFSIZ); + html_idcat(buf, nn->string, BUFSIZ); if (nn->next) - (void)strlcat(buf, "_", BUFSIZ); + html_idcat(buf, " ", BUFSIZ); } PAIR_CLASS_INIT(&tag[0], "link-sec"); @@ -1708,7 +1713,10 @@ mdoc_sp_pre(MDOC_ARGS) bufcat_su(h, "height", &su); PAIR_STYLE_INIT(&tag, h); print_otag(h, TAG_DIV, 1, &tag); - return(1); + /* So the div isn't empty: */ + print_text(h, "\\~"); + + return(0); } @@ -1855,6 +1863,7 @@ mdoc_in_pre(MDOC_ARGS) for (nn = n->child; nn; nn = nn->next) { PAIR_CLASS_INIT(&tag[0], "link-includes"); i = 1; + bufinit(h); if (h->base_includes) { buffmt_includes(h, nn->string); tag[i].key = ATTR_HREF; diff --git a/usr.bin/mandoc/mdoc_macro.c b/usr.bin/mandoc/mdoc_macro.c index c293b73877e..224ac65e8e2 100644 --- a/usr.bin/mandoc/mdoc_macro.c +++ b/usr.bin/mandoc/mdoc_macro.c @@ -1,4 +1,4 @@ -/* $Id: mdoc_macro.c,v 1.25 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: mdoc_macro.c,v 1.26 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -19,6 +19,7 @@ #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <time.h> #include "libmdoc.h" diff --git a/usr.bin/mandoc/mdoc_strings.c b/usr.bin/mandoc/mdoc_strings.c index 59e99433643..ca71d9e54da 100644 --- a/usr.bin/mandoc/mdoc_strings.c +++ b/usr.bin/mandoc/mdoc_strings.c @@ -1,4 +1,4 @@ -/* $Id: mdoc_strings.c,v 1.10 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: mdoc_strings.c,v 1.11 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se> * @@ -127,7 +127,7 @@ mdoc_atotime(const char *p) struct tm tm; char *pp; - bzero(&tm, sizeof(struct tm)); + memset(&tm, 0, sizeof(struct tm)); if (0 == strcmp(p, "$" "Mdocdate$")) return(time(NULL)); diff --git a/usr.bin/mandoc/mdoc_term.c b/usr.bin/mandoc/mdoc_term.c index 94dcad2fdd9..2f33f448de5 100644 --- a/usr.bin/mandoc/mdoc_term.c +++ b/usr.bin/mandoc/mdoc_term.c @@ -1,4 +1,4 @@ -/* $Id: mdoc_term.c,v 1.62 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: mdoc_term.c,v 1.63 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -18,7 +18,6 @@ #include <assert.h> #include <ctype.h> -#include <err.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -310,7 +309,7 @@ print_node(DECL_ARGS) bold = p->bold; under = p->under; - bzero(&npair, sizeof(struct termpair)); + memset(&npair, 0, sizeof(struct termpair)); npair.ppair = pair; if (MDOC_TEXT != n->type) { @@ -343,8 +342,7 @@ print_node(DECL_ARGS) static void print_foot(DECL_ARGS) { - char buf[DATESIZ]; - char *os; + char buf[DATESIZ], os[BUFSIZ]; /* * Output the footer in new-groff style, that is, three columns @@ -354,12 +352,8 @@ print_foot(DECL_ARGS) * SYSTEM DATE SYSTEM */ - if (NULL == (os = malloc(p->rmargin))) - err(EXIT_FAILURE, "malloc"); - time2a(m->date, buf, DATESIZ); - - (void)strlcpy(os, m->os, p->rmargin); + strlcpy(os, m->os, BUFSIZ); term_vspace(p); @@ -388,8 +382,6 @@ print_foot(DECL_ARGS) p->offset = 0; p->rmargin = p->maxrmargin; p->flags = 0; - - free(os); } @@ -398,16 +390,11 @@ print_foot(DECL_ARGS) static void print_head(DECL_ARGS) { - char *buf, *title; + char buf[BUFSIZ], title[BUFSIZ]; p->rmargin = p->maxrmargin; p->offset = 0; - if (NULL == (buf = malloc(p->rmargin))) - err(EXIT_FAILURE, "malloc"); - if (NULL == (title = malloc(p->rmargin))) - err(EXIT_FAILURE, "malloc"); - /* * The header is strange. It has three components, which are * really two with the first duplicated. It goes like this: @@ -422,15 +409,15 @@ print_head(DECL_ARGS) */ assert(m->vol); - (void)strlcpy(buf, m->vol, p->rmargin); + strlcpy(buf, m->vol, BUFSIZ); if (m->arch) { - (void)strlcat(buf, " (", p->rmargin); - (void)strlcat(buf, m->arch, p->rmargin); - (void)strlcat(buf, ")", p->rmargin); + strlcat(buf, " (", BUFSIZ); + strlcat(buf, m->arch, BUFSIZ); + strlcat(buf, ")", BUFSIZ); } - snprintf(title, p->rmargin, "%s(%d)", m->title, m->msec); + snprintf(title, BUFSIZ, "%s(%d)", m->title, m->msec); p->offset = 0; p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2; @@ -457,9 +444,6 @@ print_head(DECL_ARGS) p->offset = 0; p->rmargin = p->maxrmargin; p->flags &= ~TERMP_NOSPACE; - - free(title); - free(buf); } diff --git a/usr.bin/mandoc/mdoc_validate.c b/usr.bin/mandoc/mdoc_validate.c index c2c1c8fdbc8..b2930f52e9f 100644 --- a/usr.bin/mandoc/mdoc_validate.c +++ b/usr.bin/mandoc/mdoc_validate.c @@ -1,4 +1,4 @@ -/* $Id: mdoc_validate.c,v 1.38 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: mdoc_validate.c,v 1.39 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -18,7 +18,6 @@ #include <assert.h> #include <ctype.h> -#include <errno.h> #include <limits.h> #include <stdarg.h> #include <stdlib.h> diff --git a/usr.bin/mandoc/msec.c b/usr.bin/mandoc/msec.c index 569fa056f9c..606ee412990 100644 --- a/usr.bin/mandoc/msec.c +++ b/usr.bin/mandoc/msec.c @@ -1,4 +1,4 @@ -/* $Id: msec.c,v 1.2 2009/06/14 23:00:57 schwarze Exp $ */ +/* $Id: msec.c,v 1.3 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -16,6 +16,7 @@ */ #include <stdlib.h> #include <string.h> +#include <time.h> #include "libmdoc.h" diff --git a/usr.bin/mandoc/st.c b/usr.bin/mandoc/st.c index bac77eefbc7..1cffc895dad 100644 --- a/usr.bin/mandoc/st.c +++ b/usr.bin/mandoc/st.c @@ -1,4 +1,4 @@ -/* $Id: st.c,v 1.2 2009/06/14 23:00:57 schwarze Exp $ */ +/* $Id: st.c,v 1.3 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -16,6 +16,7 @@ */ #include <stdlib.h> #include <string.h> +#include <time.h> #include "libmdoc.h" diff --git a/usr.bin/mandoc/term.c b/usr.bin/mandoc/term.c index e6eee4706c8..3d32b3bee28 100644 --- a/usr.bin/mandoc/term.c +++ b/usr.bin/mandoc/term.c @@ -1,4 +1,4 @@ -/* $Id: term.c,v 1.18 2009/10/27 21:40:07 schwarze Exp $ */ +/* $Id: term.c,v 1.19 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -15,10 +15,10 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <assert.h> -#include <err.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <time.h> #include "chars.h" #include "out.h" @@ -76,9 +76,11 @@ term_alloc(enum termenc enc) { struct termp *p; - if (NULL == (p = malloc(sizeof(struct termp)))) - return(NULL); - bzero(p, sizeof(struct termp)); + p = calloc(1, sizeof(struct termp)); + if (NULL == p) { + perror(NULL); + exit(EXIT_FAILURE); + } p->maxrmargin = 78; p->enc = enc; return(p); @@ -119,36 +121,44 @@ term_alloc(enum termenc enc) * If TERMP_NOBREAK is specified and the line overruns the right * margin, it will break and pad-right to the right margin after * writing. If maxrmargin is violated, it will break and continue - * writing from the right-margin, which will lead to the above - * scenario upon exit. - * - * Otherwise, the line will break at the right margin. Extremely long - * lines will cause the system to emit a warning (TODO: hyphenate, if - * possible). + * writing from the right-margin, which will lead to the above scenario + * upon exit. Otherwise, the line will break at the right margin. */ void term_flushln(struct termp *p) { - int i, j; - size_t vbl, vsz, vis, maxvis, mmax, bp; + int i; /* current input position in p->buf */ + size_t vis; /* current visual position on output */ + size_t vbl; /* number of blanks to prepend to output */ + size_t vsz; /* visual characters to write to output */ + size_t bp; /* visual right border position */ + int j; /* temporary loop index */ + size_t maxvis, mmax; static int overstep = 0; /* * First, establish the maximum columns of "visible" content. * This is usually the difference between the right-margin and * an indentation, but can be, for tagged lists or columns, a - * small set of values. + * small set of values. */ assert(p->offset < p->rmargin); - assert((int)(p->rmargin - p->offset) - overstep > 0); - maxvis = /* LINTED */ - p->rmargin - p->offset - overstep; - mmax = /* LINTED */ - p->maxrmargin - p->offset - overstep; + maxvis = (int)(p->rmargin - p->offset) - overstep < 0 ? + /* LINTED */ + 0 : p->rmargin - p->offset - overstep; + mmax = (int)(p->maxrmargin - p->offset) - overstep < 0 ? + /* LINTED */ + 0 : p->maxrmargin - p->offset - overstep; bp = TERMP_NOBREAK & p->flags ? mmax : maxvis; + + /* + * FIXME: if bp is zero, we still output the first word before + * breaking the line. + */ + vis = 0; /* @@ -534,8 +544,10 @@ buffer(struct termp *p, char c) p->maxcols = 256; s = p->maxcols * 2; p->buf = realloc(p->buf, s); - if (NULL == p->buf) - err(1, "realloc"); /* FIXME: shouldn't be here! */ + if (NULL == p->buf) { + perror(NULL); + exit(EXIT_FAILURE); + } p->maxcols = s; } p->buf[(int)(p->col)++] = c; diff --git a/usr.bin/mandoc/tree.c b/usr.bin/mandoc/tree.c index b243df2a246..a2e857c22a1 100644 --- a/usr.bin/mandoc/tree.c +++ b/usr.bin/mandoc/tree.c @@ -1,4 +1,4 @@ -/* $Id: tree.c,v 1.5 2009/10/21 19:13:51 schwarze Exp $ */ +/* $Id: tree.c,v 1.6 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -15,9 +15,9 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <assert.h> -#include <err.h> #include <stdio.h> #include <stdlib.h> +#include <time.h> #include "mdoc.h" #include "man.h" diff --git a/usr.bin/mandoc/vol.c b/usr.bin/mandoc/vol.c index 9c0eae8c73b..4062e492424 100644 --- a/usr.bin/mandoc/vol.c +++ b/usr.bin/mandoc/vol.c @@ -1,4 +1,4 @@ -/* $Id: vol.c,v 1.2 2009/06/14 23:00:57 schwarze Exp $ */ +/* $Id: vol.c,v 1.3 2009/12/22 23:58:00 schwarze Exp $ */ /* * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se> * @@ -16,6 +16,7 @@ */ #include <stdlib.h> #include <string.h> +#include <time.h> #include "libmdoc.h" |