diff options
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | action.c | 601 | ||||
-rw-r--r-- | ascii.c | 6 | ||||
-rw-r--r-- | mandoc.1 | 2 | ||||
-rw-r--r-- | mdoc.3 | 29 | ||||
-rw-r--r-- | mdoc.7 | 18 | ||||
-rw-r--r-- | mdoc.c | 49 | ||||
-rw-r--r-- | private.h | 4 | ||||
-rw-r--r-- | term.c | 239 | ||||
-rw-r--r-- | term.h | 20 | ||||
-rw-r--r-- | terminal.c | 73 | ||||
-rw-r--r-- | validate.c | 31 |
12 files changed, 599 insertions, 477 deletions
@@ -9,8 +9,8 @@ INSTALL_DATA = install -m 0444 INSTALL_LIB = install -m 0644 INSTALL_MAN = $(INSTALL_DATA) -VERSION = 1.6.1 -VDATE = 19 March 2009 +VERSION = 1.6.2 +VDATE = 20 March 2009 VFLAGS = -DVERSION=\"$(VERSION)\" CFLAGS += -W -Wall -Wstrict-prototypes -Wno-unused-parameter -g @@ -42,153 +42,199 @@ enum mwarn { WBADDATE }; +#define PRE_ARGS struct mdoc *m, const struct mdoc_node *n +#define POST_ARGS struct mdoc *m + struct actions { - int (*post)(struct mdoc *); + int (*pre)(PRE_ARGS); + int (*post)(POST_ARGS); }; -static int nwarn(struct mdoc *, - const struct mdoc_node *, enum mwarn); -static int nerr(struct mdoc *, - const struct mdoc_node *, enum merr); -static int post_ar(struct mdoc *); -static int post_bl(struct mdoc *); -static int post_bl_width(struct mdoc *); -static int post_bl_tagwidth(struct mdoc *); -static int post_dd(struct mdoc *); -static int post_dt(struct mdoc *); -static int post_nm(struct mdoc *); -static int post_os(struct mdoc *); -static int post_sh(struct mdoc *); -static int post_std(struct mdoc *); -static int post_prologue(struct mdoc *); - -#define merr(m, t) nerr((m), (m)->last, (t)) -#define mwarn(m, t) nwarn((m), (m)->last, (t)) +static int pwarn(struct mdoc *, int, int, enum mwarn); +static int perr(struct mdoc *, int, int, enum merr); + +static int post_ar(POST_ARGS); +static int post_bl(POST_ARGS); +static int post_bl_width(POST_ARGS); +static int post_bl_tagwidth(POST_ARGS); +static int post_display(POST_ARGS); +static int post_dd(POST_ARGS); +static int post_dt(POST_ARGS); +static int post_nm(POST_ARGS); +static int post_os(POST_ARGS); +static int post_prol(POST_ARGS); +static int post_sh(POST_ARGS); +static int post_std(POST_ARGS); + +static int pre_display(PRE_ARGS); + +#define merr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t)) +#define mwarn(m, t) pwarn((m), (m)->last->line, (m)->last->pos, (t)) const struct actions mdoc_actions[MDOC_MAX] = { - { NULL }, /* \" */ - { post_dd }, /* Dd */ - { post_dt }, /* Dt */ - { post_os }, /* Os */ - { post_sh }, /* Sh */ - { NULL }, /* Ss */ - { NULL }, /* Pp */ - { NULL }, /* D1 */ - { NULL }, /* Dl */ - { NULL }, /* Bd */ - { NULL }, /* Ed */ - { post_bl }, /* Bl */ - { NULL }, /* El */ - { NULL }, /* It */ - { NULL }, /* Ad */ - { NULL }, /* An */ - { post_ar }, /* Ar */ - { NULL }, /* Cd */ - { NULL }, /* Cm */ - { NULL }, /* Dv */ - { NULL }, /* Er */ - { NULL }, /* Ev */ - { post_std }, /* Ex */ - { NULL }, /* Fa */ - { NULL }, /* Fd */ - { NULL }, /* Fl */ - { NULL }, /* Fn */ - { NULL }, /* Ft */ - { NULL }, /* Ic */ - { NULL }, /* In */ - { NULL }, /* Li */ - { NULL }, /* Nd */ - { post_nm }, /* Nm */ - { NULL }, /* Op */ - { NULL }, /* Ot */ - { NULL }, /* Pa */ - { post_std }, /* Rv */ - { NULL }, /* St */ - { NULL }, /* Va */ - { NULL }, /* Vt */ - { NULL }, /* Xr */ - { NULL }, /* %A */ - { NULL }, /* %B */ - { NULL }, /* %D */ - { NULL }, /* %I */ - { NULL }, /* %J */ - { NULL }, /* %N */ - { NULL }, /* %O */ - { NULL }, /* %P */ - { NULL }, /* %R */ - { NULL }, /* %T */ - { NULL }, /* %V */ - { NULL }, /* Ac */ - { NULL }, /* Ao */ - { NULL }, /* Aq */ - { NULL }, /* At */ - { NULL }, /* Bc */ - { NULL }, /* Bf */ - { NULL }, /* Bo */ - { NULL }, /* Bq */ - { NULL }, /* Bsx */ - { NULL }, /* Bx */ - { NULL }, /* Db */ - { NULL }, /* Dc */ - { NULL }, /* Do */ - { NULL }, /* Dq */ - { NULL }, /* Ec */ - { NULL }, /* Ef */ - { NULL }, /* Em */ - { NULL }, /* Eo */ - { NULL }, /* Fx */ - { NULL }, /* Ms */ - { NULL }, /* No */ - { NULL }, /* Ns */ - { NULL }, /* Nx */ - { NULL }, /* Ox */ - { NULL }, /* Pc */ - { NULL }, /* Pf */ - { NULL }, /* Po */ - { NULL }, /* Pq */ - { NULL }, /* Qc */ - { NULL }, /* Ql */ - { NULL }, /* Qo */ - { NULL }, /* Qq */ - { NULL }, /* Re */ - { NULL }, /* Rs */ - { NULL }, /* Sc */ - { NULL }, /* So */ - { NULL }, /* Sq */ - { NULL }, /* Sm */ - { NULL }, /* Sx */ - { NULL }, /* Sy */ - { NULL }, /* Tn */ - { NULL }, /* Ux */ - { NULL }, /* Xc */ - { NULL }, /* Xo */ - { NULL }, /* Fo */ - { NULL }, /* Fc */ - { NULL }, /* Oo */ - { NULL }, /* Oc */ - { NULL }, /* Bk */ - { NULL }, /* Ek */ - { NULL }, /* Bt */ - { NULL }, /* Hf */ - { NULL }, /* Fr */ - { NULL }, /* Ud */ - { NULL }, /* Lb */ - { NULL }, /* Ap */ - { NULL }, /* Lp */ - { NULL }, /* Lk */ - { NULL }, /* Mt */ - { NULL }, /* Brq */ - { NULL }, /* Bro */ - { NULL }, /* Brc */ - { NULL }, /* %C */ - { NULL }, /* Es */ - { NULL }, /* En */ - { NULL }, /* Dx */ + { NULL, NULL }, /* \" */ + { NULL, post_dd }, /* Dd */ + { NULL, post_dt }, /* Dt */ + { NULL, post_os }, /* Os */ + { NULL, post_sh }, /* Sh */ + { NULL, NULL }, /* Ss */ + { NULL, NULL }, /* Pp */ + { NULL, NULL }, /* D1 */ + { pre_display, post_display }, /* Dl */ + { pre_display, post_display }, /* Bd */ + { NULL, NULL }, /* Ed */ + { NULL, post_bl }, /* Bl */ + { NULL, NULL }, /* El */ + { NULL, NULL }, /* It */ + { NULL, NULL }, /* Ad */ + { NULL, NULL }, /* An */ + { NULL, post_ar }, /* Ar */ + { NULL, NULL }, /* Cd */ + { NULL, NULL }, /* Cm */ + { NULL, NULL }, /* Dv */ + { NULL, NULL }, /* Er */ + { NULL, NULL }, /* Ev */ + { NULL, post_std }, /* 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 */ + { NULL, NULL }, /* Nd */ + { NULL, post_nm }, /* Nm */ + { NULL, NULL }, /* Op */ + { NULL, NULL }, /* Ot */ + { NULL, NULL }, /* Pa */ + { NULL, post_std }, /* 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 }, /* Ap */ + { 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 */ }; +int +mdoc_action_pre(struct mdoc *m, const struct mdoc_node *n) +{ + + switch (n->type) { + case (MDOC_ROOT): + break; + case (MDOC_TEXT): + break; + default: + if (NULL == mdoc_actions[m->last->tok].pre) + break; + return((*mdoc_actions[m->last->tok].pre)(m, n)); + } + return(1); +} + + +int +mdoc_action_post(struct mdoc *m) +{ + + if (MDOC_ACTED & m->last->flags) + return(1); + m->last->flags |= MDOC_ACTED; + + switch (m->last->type) { + case (MDOC_TEXT): + break; + case (MDOC_ROOT): + break; + default: + if (NULL == mdoc_actions[m->last->tok].post) + break; + return((*mdoc_actions[m->last->tok].post)(m)); + } + return(1); +} + + static int -nerr(struct mdoc *m, const struct mdoc_node *n, enum merr type) +perr(struct mdoc *m, int line, int pos, enum merr type) { char *p; @@ -201,12 +247,12 @@ nerr(struct mdoc *m, const struct mdoc_node *n, enum merr type) } assert(p); - return(mdoc_nerr(m, n, p)); + return(mdoc_perr(m, line, pos, p)); } static int -nwarn(struct mdoc *m, const struct mdoc_node *n, enum mwarn type) +pwarn(struct mdoc *m, int line, int pos, enum mwarn type) { char *p; int c; @@ -228,12 +274,12 @@ nwarn(struct mdoc *m, const struct mdoc_node *n, enum mwarn type) } assert(p); - return(mdoc_nwarn(m, n, c, p)); + return(mdoc_pwarn(m, line, pos, c, p)); } static int -post_std(struct mdoc *mdoc) +post_std(POST_ARGS) { /* @@ -241,42 +287,37 @@ post_std(struct mdoc *mdoc) * name (if it's been set). */ - if (NULL == mdoc->last->args) + if (NULL == m->last->args) return(1); - if (mdoc->last->args->argv[0].sz) + if (m->last->args->argv[0].sz) return(1); - assert(mdoc->meta.name); - - mdoc_msg(mdoc, "writing %s argument: %s", - mdoc_argnames[MDOC_Std], - mdoc->meta.name); + assert(m->meta.name); - mdoc->last->args->argv[0].value = xcalloc(1, sizeof(char *)); - mdoc->last->args->argv[0].sz = 1; - mdoc->last->args->argv[0].value[0] = xstrdup(mdoc->meta.name); + m->last->args->argv[0].value = xcalloc(1, sizeof(char *)); + m->last->args->argv[0].sz = 1; + m->last->args->argv[0].value[0] = xstrdup(m->meta.name); return(1); } static int -post_nm(struct mdoc *mdoc) +post_nm(POST_ARGS) { char buf[64]; - if (mdoc->meta.name) + if (m->meta.name) return(1); - (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf)); - mdoc->meta.name = xstrdup(buf); - mdoc_msg(mdoc, "name: %s", mdoc->meta.name); + (void)xstrlcpys(buf, m->last->child, sizeof(buf)); + m->meta.name = xstrdup(buf); return(1); } static int -post_sh(struct mdoc *mdoc) +post_sh(POST_ARGS) { enum mdoc_sec sec; char buf[64]; @@ -287,20 +328,18 @@ post_sh(struct mdoc *mdoc) * check ordering. */ - if (MDOC_HEAD != mdoc->last->type) + if (MDOC_HEAD != m->last->type) return(1); - (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf)); + (void)xstrlcpys(buf, m->last->child, sizeof(buf)); if (SEC_CUSTOM != (sec = mdoc_atosec(buf))) - mdoc->lastnamed = sec; + m->lastnamed = sec; - mdoc->lastsec = sec; - - switch (mdoc->lastsec) { + switch ((m->lastsec = sec)) { case (SEC_RETURN_VALUES): /* FALLTHROUGH */ case (SEC_ERRORS): - switch (mdoc->meta.msec) { + switch (m->meta.msec) { case (2): /* FALLTHROUGH */ case (3): @@ -308,7 +347,7 @@ post_sh(struct mdoc *mdoc) case (9): break; default: - return(mwarn(mdoc, WBADSEC)); + return(mwarn(m, WBADSEC)); } break; default: @@ -319,50 +358,42 @@ post_sh(struct mdoc *mdoc) static int -post_dt(struct mdoc *mdoc) +post_dt(POST_ARGS) { struct mdoc_node *n; const char *cp; char *ep; long lval; - if (mdoc->meta.title) - free(mdoc->meta.title); - if (mdoc->meta.vol) - free(mdoc->meta.vol); - if (mdoc->meta.arch) - free(mdoc->meta.arch); + if (m->meta.title) + free(m->meta.title); + if (m->meta.vol) + free(m->meta.vol); + if (m->meta.arch) + free(m->meta.arch); - mdoc->meta.title = mdoc->meta.vol = mdoc->meta.arch = NULL; - mdoc->meta.msec = 0; + m->meta.title = m->meta.vol = m->meta.arch = NULL; + m->meta.msec = 0; /* Handles: `.Dt' * --> title = unknown, volume = local, msec = 0, arch = NULL */ - if (NULL == (n = mdoc->last->child)) { - mdoc->meta.title = xstrdup("unknown"); - mdoc->meta.vol = xstrdup("local"); - mdoc_msg(mdoc, "title: %s", mdoc->meta.title); - mdoc_msg(mdoc, "volume: %s", mdoc->meta.vol); - mdoc_msg(mdoc, "arch: <unset>"); - mdoc_msg(mdoc, "msec: <unset>"); - return(post_prologue(mdoc)); + if (NULL == (n = m->last->child)) { + m->meta.title = xstrdup("unknown"); + m->meta.vol = xstrdup("local"); + return(post_prol(m)); } /* Handles: `.Dt TITLE' * --> title = TITLE, volume = local, msec = 0, arch = NULL */ - mdoc->meta.title = xstrdup(n->string); - mdoc_msg(mdoc, "title: %s", mdoc->meta.title); + m->meta.title = xstrdup(n->string); if (NULL == (n = n->next)) { - mdoc->meta.vol = xstrdup("local"); - mdoc_msg(mdoc, "volume: %s", mdoc->meta.vol); - mdoc_msg(mdoc, "arch: <unset>"); - mdoc_msg(mdoc, "msec: %d", mdoc->meta.msec); - return(post_prologue(mdoc)); + m->meta.vol = xstrdup("local"); + return(post_prol(m)); } /* Handles: `.Dt TITLE SEC' @@ -373,20 +404,16 @@ post_dt(struct mdoc *mdoc) */ if ((cp = mdoc_a2msec(n->string))) { - mdoc->meta.vol = xstrdup(cp); + m->meta.vol = xstrdup(cp); errno = 0; lval = strtol(n->string, &ep, 10); if (n->string[0] != '\0' && *ep == '\0') - mdoc->meta.msec = (int)lval; + m->meta.msec = (int)lval; } else - mdoc->meta.vol = xstrdup(n->string); + m->meta.vol = xstrdup(n->string); - if (NULL == (n = n->next)) { - mdoc_msg(mdoc, "volume: %s", mdoc->meta.vol); - mdoc_msg(mdoc, "arch: <unset>"); - mdoc_msg(mdoc, "msec: %d", mdoc->meta.msec); - return(post_prologue(mdoc)); - } + if (NULL == (n = n->next)) + return(post_prol(m)); /* Handles: `.Dt TITLE SEC VOL' * --> title = TITLE, volume = VOL is vol ? @@ -396,59 +423,52 @@ post_dt(struct mdoc *mdoc) */ if ((cp = mdoc_a2vol(n->string))) { - free(mdoc->meta.vol); - mdoc->meta.vol = xstrdup(cp); + free(m->meta.vol); + m->meta.vol = xstrdup(cp); n = n->next; } else { cp = mdoc_a2arch(n->string); if (NULL == cp) { - free(mdoc->meta.vol); - mdoc->meta.vol = xstrdup(n->string); + free(m->meta.vol); + m->meta.vol = xstrdup(n->string); } else - mdoc->meta.arch = xstrdup(cp); + m->meta.arch = xstrdup(cp); } - mdoc_msg(mdoc, "volume: %s", mdoc->meta.vol); - mdoc_msg(mdoc, "arch: %s", mdoc->meta.arch ? - mdoc->meta.arch : "<unset>"); - mdoc_msg(mdoc, "msec: %d", mdoc->meta.msec); - /* Ignore any subsequent parameters... */ - return(post_prologue(mdoc)); + return(post_prol(m)); } static int -post_os(struct mdoc *mdoc) +post_os(POST_ARGS) { char buf[64]; struct utsname utsname; - if (mdoc->meta.os) - free(mdoc->meta.os); + if (m->meta.os) + free(m->meta.os); - (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf)); + (void)xstrlcpys(buf, m->last->child, sizeof(buf)); if (0 == buf[0]) { if (-1 == uname(&utsname)) - return(mdoc_err(mdoc, "utsname")); + return(mdoc_err(m, "utsname")); (void)xstrlcpy(buf, utsname.sysname, sizeof(buf)); (void)xstrlcat(buf, " ", sizeof(buf)); (void)xstrlcat(buf, utsname.release, sizeof(buf)); } - mdoc->meta.os = xstrdup(buf); - mdoc_msg(mdoc, "system: %s", mdoc->meta.os); + m->meta.os = xstrdup(buf); + m->lastnamed = m->lastsec = SEC_BODY; - mdoc->lastnamed = mdoc->lastsec = SEC_BODY; - - return(post_prologue(mdoc)); + return(post_prol(m)); } static int -post_bl_tagwidth(struct mdoc *mdoc) +post_bl_tagwidth(struct mdoc *m) { struct mdoc_node *n; int sz; @@ -459,7 +479,7 @@ post_bl_tagwidth(struct mdoc *mdoc) * to intuit our width from the first body element. */ - if (NULL == (n = mdoc->last->body->child)) + if (NULL == (n = m->last->body->child)) return(1); /* @@ -477,7 +497,7 @@ post_bl_tagwidth(struct mdoc *mdoc) sz = -1; if (-1 == sz) { - if ( ! mwarn(mdoc, WNOWIDTH)) + if ( ! mwarn(m, WNOWIDTH)) return(0); sz = 10; } @@ -489,13 +509,13 @@ post_bl_tagwidth(struct mdoc *mdoc) * We're guaranteed that a MDOC_Width doesn't already exist. */ - if (NULL == mdoc->last->args) { - mdoc->last->args = xcalloc + if (NULL == m->last->args) { + m->last->args = xcalloc (1, sizeof(struct mdoc_arg)); - mdoc->last->args->refcnt = 1; + m->last->args->refcnt = 1; } - n = mdoc->last; + n = m->last; sz = (int)n->args->argc; (n->args->argc)++; @@ -504,15 +524,12 @@ post_bl_tagwidth(struct mdoc *mdoc) n->args->argc * sizeof(struct mdoc_arg)); n->args->argv[sz - 1].arg = MDOC_Width; - n->args->argv[sz - 1].line = mdoc->last->line; - n->args->argv[sz - 1].pos = mdoc->last->pos; + n->args->argv[sz - 1].line = m->last->line; + n->args->argv[sz - 1].pos = m->last->pos; n->args->argv[sz - 1].sz = 1; n->args->argv[sz - 1].value = xcalloc(1, sizeof(char *)); n->args->argv[sz - 1].value[0] = xstrdup(buf); - mdoc_msg(mdoc, "adding %s argument: %s", - mdoc_argnames[MDOC_Width], buf); - return(1); } @@ -549,9 +566,6 @@ post_bl_width(struct mdoc *m) else if (0 == (width = mdoc_macro2len(tok))) return(mwarn(m, WNOWIDTH)); - mdoc_msg(m, "re-writing %s argument: %s -> %zun", - mdoc_argnames[MDOC_Width], p, width); - /* The value already exists: free and reallocate it. */ (void)snprintf(buf, sizeof(buf), "%zun", width); @@ -564,11 +578,11 @@ post_bl_width(struct mdoc *m) static int -post_bl(struct mdoc *mdoc) +post_bl(POST_ARGS) { int i, r, len; - if (MDOC_BLOCK != mdoc->last->type) + if (MDOC_BLOCK != m->last->type) return(1); /* @@ -579,20 +593,20 @@ post_bl(struct mdoc *mdoc) * rewritten into real lengths). */ - len = (int)(mdoc->last->args ? mdoc->last->args->argc : 0); + len = (int)(m->last->args ? m->last->args->argc : 0); for (r = i = 0; i < len; i++) { - if (MDOC_Tag == mdoc->last->args->argv[i].arg) + if (MDOC_Tag == m->last->args->argv[i].arg) r |= 1 << 0; - if (MDOC_Width == mdoc->last->args->argv[i].arg) + if (MDOC_Width == m->last->args->argv[i].arg) r |= 1 << 1; } if (r & (1 << 0) && ! (r & (1 << 1))) { - if ( ! post_bl_tagwidth(mdoc)) + if ( ! post_bl_tagwidth(m)) return(0); } else if (r & (1 << 1)) - if ( ! post_bl_width(mdoc)) + if ( ! post_bl_width(m)) return(0); return(1); @@ -600,50 +614,48 @@ post_bl(struct mdoc *mdoc) static int -post_ar(struct mdoc *mdoc) +post_ar(POST_ARGS) { struct mdoc_node *n; - if (mdoc->last->child) + if (m->last->child) return(1); - n = mdoc->last; - - mdoc->next = MDOC_NEXT_CHILD; - if ( ! mdoc_word_alloc(mdoc, mdoc->last->line, - mdoc->last->pos, "file")) + n = m->last; + m->next = MDOC_NEXT_CHILD; + if ( ! mdoc_word_alloc(m, m->last->line, + m->last->pos, "file")) return(0); - mdoc->next = MDOC_NEXT_SIBLING; - if ( ! mdoc_word_alloc(mdoc, mdoc->last->line, - mdoc->last->pos, "...")) + m->next = MDOC_NEXT_SIBLING; + if ( ! mdoc_word_alloc(m, m->last->line, + m->last->pos, "...")) return(0); - mdoc->last = n; - mdoc->next = MDOC_NEXT_SIBLING; + m->last = n; + m->next = MDOC_NEXT_SIBLING; return(1); } static int -post_dd(struct mdoc *mdoc) +post_dd(POST_ARGS) { char buf[64]; - (void)xstrlcpys(buf, mdoc->last->child, sizeof(buf)); + (void)xstrlcpys(buf, m->last->child, sizeof(buf)); - if (0 == (mdoc->meta.date = mdoc_atotime(buf))) { - if ( ! mwarn(mdoc, WBADDATE)) + if (0 == (m->meta.date = mdoc_atotime(buf))) { + if ( ! mwarn(m, WBADDATE)) return(0); - mdoc->meta.date = time(NULL); + m->meta.date = time(NULL); } - mdoc_msg(mdoc, "date: %u", mdoc->meta.date); - return(post_prologue(mdoc)); + return(post_prol(m)); } static int -post_prologue(struct mdoc *mdoc) +post_prol(POST_ARGS) { struct mdoc_node *n; @@ -652,20 +664,20 @@ post_prologue(struct mdoc *mdoc) * of the syntax tree (they encompass only meta-data). */ - if (mdoc->last->parent->child == mdoc->last) - mdoc->last->parent->child = mdoc->last->prev; - if (mdoc->last->prev) - mdoc->last->prev->next = NULL; + if (m->last->parent->child == m->last) + m->last->parent->child = m->last->prev; + if (m->last->prev) + m->last->prev->next = NULL; - n = mdoc->last; - assert(NULL == mdoc->last->next); + n = m->last; + assert(NULL == m->last->next); - if (mdoc->last->prev) { - mdoc->last = mdoc->last->prev; - mdoc->next = MDOC_NEXT_SIBLING; + if (m->last->prev) { + m->last = m->last->prev; + m->next = MDOC_NEXT_SIBLING; } else { - mdoc->last = mdoc->last->parent; - mdoc->next = MDOC_NEXT_CHILD; + m->last = m->last->parent; + m->next = MDOC_NEXT_CHILD; } mdoc_node_freelist(n); @@ -673,19 +685,40 @@ post_prologue(struct mdoc *mdoc) } -int -mdoc_action_post(struct mdoc *mdoc) +static int +pre_display(PRE_ARGS) { + int i; - if (MDOC_ACTED & mdoc->last->flags) + if (MDOC_BODY != n->type) return(1); - mdoc->last->flags |= MDOC_ACTED; - if (MDOC_TEXT == mdoc->last->type) - return(1); - if (MDOC_ROOT == mdoc->last->type) - return(1); - if (NULL == mdoc_actions[mdoc->last->tok].post) - return(1); - return((*mdoc_actions[mdoc->last->tok].post)(mdoc)); + /* + * We ONLY enter a literal context if `Bd -literal' or `Bd + * -unfilled'. + */ + + n = n->parent; + for (i = 0; i < (int)n->args->argc; i++) + if (MDOC_Literal == n->args->argv[i].arg) + break; + else if (MDOC_Unfilled == n->args->argv[i].arg) + break; + + if (i < (int)n->args->argc) + m->flags |= MDOC_LITERAL; + + return(1); } + + +static int +post_display(POST_ARGS) +{ + + if (MDOC_BODY == m->last->type) + m->flags &= ~MDOC_LITERAL; + return(1); +} + + @@ -62,7 +62,7 @@ static inline int match(const struct line *, void -asciifree(void *arg) +term_asciifree(void *arg) { struct asciitab *tab; @@ -75,7 +75,7 @@ asciifree(void *arg) void * -ascii2htab(void) +term_ascii2htab(void) { struct asciitab *tab; void **htab; @@ -132,7 +132,7 @@ ascii2htab(void) const char * -a2ascii(void *arg, const char *p, size_t sz, size_t *rsz) +term_a2ascii(void *arg, const char *p, size_t sz, size_t *rsz) { struct asciitab *tab; struct linep *pp, *prev; @@ -155,7 +155,7 @@ list is not yet supported. .It The \-literal and \-unfilled .Sq \&Bd -displays only accept text contents. +displays types are synonyms, as are \-filled and \-ragged. .It The .Sq \&Xo/Xc @@ -26,7 +26,8 @@ .Nm mdoc_endparse , .Nm mdoc_node , .Nm mdoc_meta , -.Nm mdoc_free +.Nm mdoc_free , +.Nm mdoc_reset .Nd mdoc macro compiler library .\" SECTION .Sh SYNOPSIS @@ -34,7 +35,9 @@ .Vt extern const char * const * mdoc_macronames; .Vt extern const char * const * mdoc_argnames; .Ft "struct mdoc *" -.Fn mdoc_alloc "void *data" "const struct mdoc_cb *cb" +.Fn mdoc_alloc "void *data" "int pflags" "const struct mdoc_cb *cb" +.Ft void +.Fn mdoc_reset "struct mdoc *mdoc" .Ft void .Fn mdoc_free "struct mdoc *mdoc" .Ft int @@ -75,7 +78,10 @@ and .Fn mdoc_meta , then free all allocated memory with .Fn mdoc_free . -See the +The +.Fn mdoc_reset +function may be used in order to reset the parser for another input +sequence. See the .Sx EXAMPLES section for a full example. .\" PARAGRAPH @@ -123,10 +129,19 @@ Allocates a parsing structure. The .Fa data pointer is passed to callbacks in .Fa cb , -which are documented further in the header file. Returns NULL on -failure. If non-NULL, the pointer must be freed with +which are documented further in the header file. +The +.Fa pflags +arguments are defined in +.Pa mdoc.h . +Returns NULL on failure. If non-NULL, the pointer must be freed with .Fn mdoc_free . .\" LIST-ITEM +.It Fn mdoc_reset +Reset the parser for another parse routine. After its use, +.Fn mdoc_parseln +behaves as if invoked for the first time. +.\" LIST-ITEM .It Fn mdoc_free Free all resources of a parser. The pointer is no longer valid after invocation. @@ -285,9 +300,7 @@ mdoc_free(mdoc); .Ed .\" SECTION .Sh SEE ALSO -.Xr mdocterm 1 , -.Xr mdoclint 1 , -.Xr mdoctree 1 , +.Xr mandoc 1 , .Xr mdoc 7 .\" SECTION .Sh AUTHORS @@ -47,7 +47,23 @@ and, in certain circumstances, the tab character .Sq \et . All manuals must have .Sq \en -line termination. +line termination. +.Pp +The only time a blank line is acceptable is within +the context of +.Sq \&Bd \-literal +or +.Sq \&Bd \-unfilled . +.Pp +Tab characters +.Pq \et +are only acceptable when delimiting +.Sq \&Bl \-column +and in +.Sq \&Bd \-literal +or +.Sq \&Bd \-unfilled +contexts. .\" SUB-SECTION .Ss Reserved Characters Within a macro line, the following characters are reserved: @@ -29,7 +29,7 @@ /* * Main caller in the libmdoc library. This begins the parsing routine, * handles allocation of data, and so forth. Most of the "work" is done - * in macro.c and validate.c. + * in macro.c, validate.c and action.c. */ static struct mdoc_node *mdoc_node_alloc(const struct mdoc *); @@ -94,10 +94,17 @@ const char * const *mdoc_macronames = __mdoc_macronames; const char * const *mdoc_argnames = __mdoc_argnames; +/* + * Get the first (root) node of the parse tree. + */ const struct mdoc_node * mdoc_node(const struct mdoc *mdoc) { + if (MDOC_HALT & mdoc->flags) + return(NULL); + if (mdoc->first) + assert(MDOC_ROOT == mdoc->first->type); return(mdoc->first); } @@ -106,10 +113,16 @@ const struct mdoc_meta * mdoc_meta(const struct mdoc *mdoc) { + if (MDOC_HALT & mdoc->flags) + return(NULL); return(&mdoc->meta); } +/* + * Free up all resources contributed by a parse: the node tree, meta-data and + * so on. Then reallocate the root node for another parse. + */ void mdoc_reset(struct mdoc *mdoc) { @@ -138,14 +151,15 @@ mdoc_reset(struct mdoc *mdoc) } +/* + * Completely free up all resources. + */ void mdoc_free(struct mdoc *mdoc) { if (mdoc->first) mdoc_node_freelist(mdoc->first); - if (mdoc->htab) - mdoc_tokhash_free(mdoc->htab); if (mdoc->meta.title) free(mdoc->meta.title); if (mdoc->meta.os) @@ -157,6 +171,9 @@ mdoc_free(struct mdoc *mdoc) if (mdoc->meta.vol) free(mdoc->meta.vol); + if (mdoc->htab) + mdoc_tokhash_free(mdoc->htab); + free(mdoc); } @@ -182,6 +199,10 @@ mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb) } +/* + * Climb back up the parse tree, validating open scopes. Mostly calls + * through to macro_end in macro.c. + */ int mdoc_endparse(struct mdoc *mdoc) { @@ -318,6 +339,8 @@ mdoc_node_append(struct mdoc *mdoc, struct mdoc_node *p) if ( ! mdoc_valid_pre(mdoc, p)) return(0); + if ( ! mdoc_action_pre(mdoc, p)) + return(0); switch (p->type) { case (MDOC_HEAD): @@ -512,17 +535,21 @@ mdoc_node_freelist(struct mdoc_node *p) * control character. */ static int -parsetext(struct mdoc *mdoc, int line, char *buf) +parsetext(struct mdoc *m, int line, char *buf) { - if (SEC_PROLOGUE == mdoc->lastnamed) - return(mdoc_perr(mdoc, line, 0, + if (SEC_PROLOGUE == m->lastnamed) + return(mdoc_perr(m, line, 0, "text disallowed in prologue")); - if ( ! mdoc_word_alloc(mdoc, line, 0, buf)) + if (0 == buf[0] && ! (MDOC_LITERAL & m->flags)) + return(mdoc_perr(m, line, 0, + "blank lines only in literal context")); + + if ( ! mdoc_word_alloc(m, line, 0, buf)) return(0); - mdoc->next = MDOC_NEXT_SIBLING; + m->next = MDOC_NEXT_SIBLING; return(1); } @@ -601,6 +628,12 @@ parsemacro(struct mdoc *m, int ln, char *buf) if ( ! mdoc_macro(m, c, ln, 1, &i, buf)) goto err; + /* + * If we're in literal mode, then add a newline to the end of + * macro lines. Our frontends will interpret this correctly + * (it's documented in mdoc.3). + */ + return(1); err: /* Error out. */ @@ -33,6 +33,7 @@ struct mdoc { int linetok; int flags; #define MDOC_HALT (1 << 0) +#define MDOC_LITERAL (1 << 1) int pflags; enum mdoc_next next; struct mdoc_node *last; @@ -126,7 +127,8 @@ const char *mdoc_a2msec(const char *); int mdoc_valid_pre(struct mdoc *, const struct mdoc_node *); int mdoc_valid_post(struct mdoc *); -int mdoc_action_pre(struct mdoc *, struct mdoc_node *); +int mdoc_action_pre(struct mdoc *, + const struct mdoc_node *); int mdoc_action_post(struct mdoc *); int mdoc_argv(struct mdoc *, int, int, struct mdoc_arg **, int *, char *); @@ -434,7 +434,7 @@ termp_dq_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - word(p, "\\(lq"); + term_word(p, "\\(lq"); p->flags |= TERMP_NOSPACE; return(1); } @@ -449,7 +449,7 @@ termp_dq_post(DECL_ARGS) return; p->flags |= TERMP_NOSPACE; - word(p, "\\(rq"); + term_word(p, "\\(rq"); } @@ -458,11 +458,9 @@ static int termp_it_pre_block(DECL_ARGS) { - newln(p); + term_newln(p); if ( ! arg_hasattr(MDOC_Compact, node->parent->parent)) - /* FIXME: parent->parent->parent? */ - if (node->prev || node->parent->parent->prev) - vspace(p); + term_vspace(p); return(1); } @@ -664,18 +662,18 @@ termp_it_pre(DECL_ARGS) if (MDOC_HEAD == node->type) switch (type) { case (MDOC_Bullet): - word(p, "\\[bu]"); + term_word(p, "\\[bu]"); break; case (MDOC_Dash): /* FALLTHROUGH */ case (MDOC_Hyphen): - word(p, "\\-"); + term_word(p, "\\-"); break; case (MDOC_Enum): (pair->ppair->ppair->count)++; (void)snprintf(buf, sizeof(buf), "%d.", pair->ppair->ppair->count); - word(p, buf); + term_word(p, buf); break; default: break; @@ -728,14 +726,14 @@ termp_it_post(DECL_ARGS) /* FALLTHROUGH */ case (MDOC_Inset): if (MDOC_BODY == node->type) - flushln(p); + term_flushln(p); break; case (MDOC_Column): if (MDOC_HEAD == node->type) - flushln(p); + term_flushln(p); break; default: - flushln(p); + term_flushln(p); break; } @@ -751,11 +749,11 @@ termp_nm_pre(DECL_ARGS) { if (SEC_SYNOPSIS == node->sec) - newln(p); + term_newln(p); TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_PROG]); if (NULL == node->child) - word(p, meta->name); + term_word(p, meta->name); return(1); } @@ -767,7 +765,7 @@ termp_fl_pre(DECL_ARGS) { TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CMD_FLAG]); - word(p, "\\-"); + term_word(p, "\\-"); p->flags |= TERMP_NOSPACE; return(1); } @@ -798,7 +796,7 @@ static int termp_pp_pre(DECL_ARGS) { - vspace(p); + term_vspace(p); return(1); } @@ -810,7 +808,7 @@ termp_st_pre(DECL_ARGS) const char *cp; if (node->child && (cp = mdoc_a2st(node->child->string))) - word(p, cp); + term_word(p, cp); return(0); } @@ -821,7 +819,7 @@ termp_rs_pre(DECL_ARGS) { if (MDOC_BLOCK == node->type && node->prev) - vspace(p); + term_vspace(p); return(1); } @@ -837,23 +835,23 @@ termp_rv_pre(DECL_ARGS) if (1 != node->args->argv[i].sz) errx(1, "expected -std argument"); - newln(p); - word(p, "The"); + term_newln(p); + term_word(p, "The"); p->flags |= ttypes[TTYPE_FUNC_NAME]; - word(p, *node->args->argv[i].value); + term_word(p, *node->args->argv[i].value); p->flags &= ~ttypes[TTYPE_FUNC_NAME]; p->flags |= TERMP_NOSPACE; - word(p, "() function returns the value 0 if successful;"); - word(p, "otherwise the value -1 is returned and the"); - word(p, "global variable"); + term_word(p, "() function returns the value 0 if successful;"); + term_word(p, "otherwise the value -1 is returned and the"); + term_word(p, "global variable"); p->flags |= ttypes[TTYPE_VAR_DECL]; - word(p, "errno"); + term_word(p, "errno"); p->flags &= ~ttypes[TTYPE_VAR_DECL]; - word(p, "is set to indicate the error."); + term_word(p, "is set to indicate the error."); return(1); } @@ -870,11 +868,11 @@ termp_ex_pre(DECL_ARGS) if (1 != node->args->argv[i].sz) errx(1, "expected -std argument"); - word(p, "The"); + term_word(p, "The"); p->flags |= ttypes[TTYPE_PROG]; - word(p, *node->args->argv[i].value); + term_word(p, *node->args->argv[i].value); p->flags &= ~ttypes[TTYPE_PROG]; - word(p, "utility exits 0 on success, and >0 if an error occurs."); + term_word(p, "utility exits 0 on success, and >0 if an error occurs."); return(1); } @@ -885,7 +883,7 @@ static int termp_nd_pre(DECL_ARGS) { - word(p, "\\-"); + term_word(p, "\\-"); return(1); } @@ -896,7 +894,7 @@ termp_bl_post(DECL_ARGS) { if (MDOC_BLOCK == node->type) - newln(p); + term_newln(p); } @@ -908,7 +906,7 @@ termp_op_post(DECL_ARGS) if (MDOC_BODY != node->type) return; p->flags |= TERMP_NOSPACE; - word(p, "\\(rB"); + term_word(p, "\\(rB"); } @@ -920,15 +918,15 @@ termp_xr_pre(DECL_ARGS) if (NULL == (n = node->child)) errx(1, "expected text line argument"); - word(p, n->string); + term_word(p, n->string); if (NULL == (n = n->next)) return(0); p->flags |= TERMP_NOSPACE; - word(p, "("); + term_word(p, "("); p->flags |= TERMP_NOSPACE; - word(p, n->string); + term_word(p, n->string); p->flags |= TERMP_NOSPACE; - word(p, ")"); + term_word(p, ")"); return(0); } @@ -950,7 +948,7 @@ termp_vt_post(DECL_ARGS) { if (node->sec == SEC_SYNOPSIS) - vspace(p); + term_vspace(p); } @@ -975,9 +973,9 @@ termp_fd_post(DECL_ARGS) if (node->sec != SEC_SYNOPSIS) return; - newln(p); + term_newln(p); if (node->next && MDOC_Fd != node->next->tok) - vspace(p); + term_vspace(p); } @@ -988,7 +986,7 @@ termp_sh_pre(DECL_ARGS) switch (node->type) { case (MDOC_HEAD): - vspace(p); + term_vspace(p); TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SECTION]); break; case (MDOC_BODY): @@ -1008,10 +1006,10 @@ termp_sh_post(DECL_ARGS) switch (node->type) { case (MDOC_HEAD): - newln(p); + term_newln(p); break; case (MDOC_BODY): - newln(p); + term_newln(p); p->offset = 0; break; default: @@ -1027,7 +1025,7 @@ termp_op_pre(DECL_ARGS) switch (node->type) { case (MDOC_BODY): - word(p, "\\(lB"); + term_word(p, "\\(lB"); p->flags |= TERMP_NOSPACE; break; default: @@ -1042,7 +1040,7 @@ static int termp_bt_pre(DECL_ARGS) { - word(p, "is currently in beta test."); + term_word(p, "is currently in beta test."); return(1); } @@ -1056,10 +1054,10 @@ termp_lb_pre(DECL_ARGS) if (NULL == node->child) errx(1, "expected text line argument"); if ((lb = mdoc_a2lib(node->child->string))) { - word(p, lb); + term_word(p, lb); return(0); } - word(p, "library"); + term_word(p, "library"); return(1); } @@ -1069,7 +1067,7 @@ static void termp_lb_post(DECL_ARGS) { - newln(p); + term_newln(p); } @@ -1078,7 +1076,7 @@ static int termp_ud_pre(DECL_ARGS) { - word(p, "currently under development."); + term_word(p, "currently under development."); return(1); } @@ -1090,7 +1088,7 @@ termp_d1_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - newln(p); + term_newln(p); p->offset += (pair->offset = INDENT); return(1); } @@ -1103,7 +1101,7 @@ termp_d1_post(DECL_ARGS) if (MDOC_BODY != node->type) return; - newln(p); + term_newln(p); p->offset -= pair->offset; } @@ -1115,7 +1113,7 @@ termp_aq_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - word(p, "\\(la"); + term_word(p, "\\(la"); p->flags |= TERMP_NOSPACE; return(1); } @@ -1129,7 +1127,7 @@ termp_aq_post(DECL_ARGS) if (MDOC_BODY != node->type) return; p->flags |= TERMP_NOSPACE; - word(p, "\\(ra"); + term_word(p, "\\(ra"); } @@ -1140,7 +1138,7 @@ termp_ft_pre(DECL_ARGS) if (SEC_SYNOPSIS == node->sec) if (node->prev && MDOC_Fo == node->prev->tok) - vspace(p); + term_vspace(p); TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_FUNC_TYPE]); return(1); } @@ -1152,7 +1150,7 @@ termp_ft_post(DECL_ARGS) { if (SEC_SYNOPSIS == node->sec) - newln(p); + term_newln(p); } @@ -1168,24 +1166,24 @@ termp_fn_pre(DECL_ARGS) /* FIXME: can be "type funcname" "type varname"... */ p->flags |= ttypes[TTYPE_FUNC_NAME]; - word(p, node->child->string); + term_word(p, node->child->string); p->flags &= ~ttypes[TTYPE_FUNC_NAME]; - word(p, "("); + term_word(p, "("); p->flags |= TERMP_NOSPACE; for (n = node->child->next; n; n = n->next) { p->flags |= ttypes[TTYPE_FUNC_ARG]; - word(p, n->string); + term_word(p, n->string); p->flags &= ~ttypes[TTYPE_FUNC_ARG]; if (n->next) - word(p, ","); + term_word(p, ","); } - word(p, ")"); + term_word(p, ")"); if (SEC_SYNOPSIS == node->sec) - word(p, ";"); + term_word(p, ";"); return(0); } @@ -1197,7 +1195,7 @@ termp_fn_post(DECL_ARGS) { if (node->sec == SEC_SYNOPSIS && node->next) - vspace(p); + term_vspace(p); } @@ -1225,14 +1223,14 @@ termp_fa_pre(DECL_ARGS) for (n = node->child; n; n = n->next) { p->flags |= ttypes[TTYPE_FUNC_ARG]; - word(p, n->string); + term_word(p, n->string); p->flags &= ~ttypes[TTYPE_FUNC_ARG]; if (n->next) - word(p, ","); + term_word(p, ","); } if (node->next && node->next->tok == MDOC_Fa) - word(p, ","); + term_word(p, ","); return(0); } @@ -1252,13 +1250,23 @@ termp_va_pre(DECL_ARGS) static int termp_bd_pre(DECL_ARGS) { - const struct mdoc_node *n; - int i, type; + int i, type, ln; + + /* + * This is fairly tricky due primarily to crappy documentation. + * If -ragged or -filled are specified, the block does nothing + * but change the indentation. + * + * If, on the other hand, -unfilled or -literal are specified, + * then the game changes. Text is printed exactly as entered in + * the display: if a macro line, a newline is appended to the + * line. Blank lines are allowed. + */ if (MDOC_BLOCK == node->type) { /* FIXME: parent prev? */ if (node->prev) - vspace(p); + term_vspace(p); return(1); } else if (MDOC_BODY != node->type) return(1); @@ -1305,15 +1313,22 @@ termp_bd_pre(DECL_ARGS) return(1); } + /* + * Tricky. Iterate through all children. If we're on a + * different parse line, append a newline and then the contents. + * Ew. + */ + p->flags |= TERMP_LITERAL; + ln = node->child ? node->child->line : 0; - for (n = node->child; n; n = n->next) { - if (MDOC_TEXT != n->type) { - warnx("non-text children not yet allowed"); - continue; + for (node = node->child; node; node = node->next) { + if (ln < node->line) { + term_flushln(p); + p->flags |= TERMP_NOSPACE; } - word(p, n->string); - flushln(p); + ln = node->line; + term_node(p, pair, meta, node); } return(0); @@ -1328,11 +1343,9 @@ termp_bd_post(DECL_ARGS) if (MDOC_BODY != node->type) return; - if ( ! (p->flags & TERMP_LITERAL)) - flushln(p); - p->flags &= ~TERMP_LITERAL; p->offset = pair->offset; + p->flags |= TERMP_NOSPACE; } @@ -1343,7 +1356,7 @@ termp_qq_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - word(p, "\""); + term_word(p, "\""); p->flags |= TERMP_NOSPACE; return(1); } @@ -1357,7 +1370,7 @@ termp_qq_post(DECL_ARGS) if (MDOC_BODY != node->type) return; p->flags |= TERMP_NOSPACE; - word(p, "\""); + term_word(p, "\""); } @@ -1366,7 +1379,7 @@ static int termp_bsx_pre(DECL_ARGS) { - word(p, "BSDI BSD/OS"); + term_word(p, "BSDI BSD/OS"); return(1); } @@ -1378,7 +1391,7 @@ termp_bx_post(DECL_ARGS) if (node->child) p->flags |= TERMP_NOSPACE; - word(p, "BSD"); + term_word(p, "BSD"); } @@ -1387,7 +1400,7 @@ static int termp_ox_pre(DECL_ARGS) { - word(p, "OpenBSD"); + term_word(p, "OpenBSD"); return(1); } @@ -1397,7 +1410,7 @@ static int termp_dx_pre(DECL_ARGS) { - word(p, "DragonFly"); + term_word(p, "DragonFly"); return(1); } @@ -1407,7 +1420,7 @@ static int termp_ux_pre(DECL_ARGS) { - word(p, "UNIX"); + term_word(p, "UNIX"); return(1); } @@ -1417,7 +1430,7 @@ static int termp_fx_pre(DECL_ARGS) { - word(p, "FreeBSD"); + term_word(p, "FreeBSD"); return(1); } @@ -1427,7 +1440,7 @@ static int termp_nx_pre(DECL_ARGS) { - word(p, "NetBSD"); + term_word(p, "NetBSD"); return(1); } @@ -1439,7 +1452,7 @@ termp_sq_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - word(p, "\\(oq"); + term_word(p, "\\(oq"); p->flags |= TERMP_NOSPACE; return(1); } @@ -1453,7 +1466,7 @@ termp_sq_post(DECL_ARGS) if (MDOC_BODY != node->type) return; p->flags |= TERMP_NOSPACE; - word(p, "\\(aq"); + term_word(p, "\\(aq"); } @@ -1484,9 +1497,9 @@ termp_ss_pre(DECL_ARGS) switch (node->type) { case (MDOC_BLOCK): - newln(p); + term_newln(p); if (node->prev) - vspace(p); + term_vspace(p); break; case (MDOC_HEAD): TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SSECTION]); @@ -1507,7 +1520,7 @@ termp_ss_post(DECL_ARGS) switch (node->type) { case (MDOC_HEAD): - newln(p); + term_newln(p); p->offset = INDENT; break; default: @@ -1542,7 +1555,7 @@ termp_cd_pre(DECL_ARGS) { TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_CONFIG]); - newln(p); + term_newln(p); return(1); } @@ -1573,8 +1586,8 @@ termp_in_pre(DECL_ARGS) { TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_INCLUDE]); - word(p, "#include"); - word(p, "<"); + term_word(p, "#include"); + term_word(p, "<"); p->flags |= TERMP_NOSPACE; return(1); } @@ -1586,13 +1599,13 @@ termp_in_post(DECL_ARGS) { p->flags |= TERMP_NOSPACE; - word(p, ">"); + term_word(p, ">"); - newln(p); + term_newln(p); if (SEC_SYNOPSIS != node->sec) return; if (node->next && MDOC_In != node->next->tok) - vspace(p); + term_vspace(p); } @@ -1609,7 +1622,7 @@ termp_at_pre(DECL_ARGS) if (NULL == att) att = "AT&T UNIX"; - word(p, att); + term_word(p, att); return(0); } @@ -1621,7 +1634,7 @@ termp_brq_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - word(p, "\\(lC"); + term_word(p, "\\(lC"); p->flags |= TERMP_NOSPACE; return(1); } @@ -1635,7 +1648,7 @@ termp_brq_post(DECL_ARGS) if (MDOC_BODY != node->type) return; p->flags |= TERMP_NOSPACE; - word(p, "\\(rC"); + term_word(p, "\\(rC"); } @@ -1646,7 +1659,7 @@ termp_bq_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - word(p, "\\(lB"); + term_word(p, "\\(lB"); p->flags |= TERMP_NOSPACE; return(1); } @@ -1660,7 +1673,7 @@ termp_bq_post(DECL_ARGS) if (MDOC_BODY != node->type) return; p->flags |= TERMP_NOSPACE; - word(p, "\\(rB"); + term_word(p, "\\(rB"); } @@ -1671,7 +1684,7 @@ termp_pq_pre(DECL_ARGS) if (MDOC_BODY != node->type) return(1); - word(p, "\\&("); + term_word(p, "\\&("); p->flags |= TERMP_NOSPACE; return(1); } @@ -1684,7 +1697,7 @@ termp_pq_post(DECL_ARGS) if (MDOC_BODY != node->type) return; - word(p, ")"); + term_word(p, ")"); } @@ -1695,7 +1708,7 @@ termp_fo_pre(DECL_ARGS) const struct mdoc_node *n; if (MDOC_BODY == node->type) { - word(p, "("); + term_word(p, "("); p->flags |= TERMP_NOSPACE; return(1); } else if (MDOC_HEAD != node->type) @@ -1707,7 +1720,7 @@ termp_fo_pre(DECL_ARGS) for (n = node->child; n; n = n->next) { if (MDOC_TEXT != n->type) errx(1, "expected text line argument"); - word(p, n->string); + term_word(p, n->string); } p->flags &= ~ttypes[TTYPE_FUNC_NAME]; @@ -1723,10 +1736,10 @@ termp_fo_post(DECL_ARGS) if (MDOC_BODY != node->type) return; p->flags |= TERMP_NOSPACE; - word(p, ")"); + term_word(p, ")"); p->flags |= TERMP_NOSPACE; - word(p, ";"); - newln(p); + term_word(p, ";"); + term_newln(p); } @@ -1809,7 +1822,7 @@ termp_ap_pre(DECL_ARGS) { p->flags |= TERMP_NOSPACE; - word(p, "\\(aq"); + term_word(p, "\\(aq"); p->flags |= TERMP_NOSPACE; return(1); } @@ -1831,7 +1844,7 @@ termp____post(DECL_ARGS) { p->flags |= TERMP_NOSPACE; - word(p, node->next ? "," : "."); + term_word(p, node->next ? "," : "."); } @@ -1845,14 +1858,14 @@ termp_lk_pre(DECL_ARGS) errx(1, "expected line argument"); p->flags |= ttypes[TTYPE_LINK_ANCHOR]; - word(p, n->string); + term_word(p, n->string); p->flags &= ~ttypes[TTYPE_LINK_ANCHOR]; p->flags |= TERMP_NOSPACE; - word(p, ":"); + term_word(p, ":"); p->flags |= ttypes[TTYPE_LINK_TEXT]; for ( ; n; n = n->next) { - word(p, n->string); + term_word(p, n->string); } p->flags &= ~ttypes[TTYPE_LINK_TEXT]; @@ -84,17 +84,17 @@ struct termact { const struct mdoc_node *); }; -void *ascii2htab(void); -const char *a2ascii(void *, const char *, size_t, size_t *); -void asciifree(void *); +void *term_ascii2htab(void); +const char *term_a2ascii(void *, const char *, size_t, size_t *); +void term_asciifree(void *); -void newln(struct termp *); -void vspace(struct termp *); -void word(struct termp *, const char *); -void flushln(struct termp *); -void transcode(struct termp *, const char *, size_t); -void subtree(struct termp *, const struct mdoc_meta *, - const struct mdoc_node *); +void term_newln(struct termp *); +void term_vspace(struct termp *); +void term_word(struct termp *, const char *); +void term_flushln(struct termp *); +void term_node(struct termp *, struct termpair *, + const struct mdoc_meta *, + const struct mdoc_node *); const struct termact *termacts; @@ -81,7 +81,7 @@ terminal_run(void *arg, const struct mdoc *mdoc) p = (struct termp *)arg; if (NULL == p->symtab) - p->symtab = ascii2htab(); + p->symtab = term_ascii2htab(); termp_head(p, mdoc_meta(mdoc)); termp_body(p, NULL, mdoc_meta(mdoc), mdoc_node(mdoc)); @@ -106,7 +106,7 @@ termp_free(struct termp *p) if (p->buf) free(p->buf); if (TERMENC_ASCII == p->enc && p->symtab) - asciifree(p->symtab); + term_asciifree(p->symtab); free(p); } @@ -161,7 +161,7 @@ termp_alloc(enum termenc enc) * possible). */ void -flushln(struct termp *p) +term_flushln(struct termp *p) { int i, j; size_t vsz, vis, maxvis, mmax, bp; @@ -294,7 +294,7 @@ flushln(struct termp *p) * assertion. */ void -newln(struct termp *p) +term_newln(struct termp *p) { p->flags |= TERMP_NOSPACE; @@ -302,7 +302,7 @@ newln(struct termp *p) p->flags &= ~TERMP_NOLPAD; return; } - flushln(p); + term_flushln(p); p->flags &= ~TERMP_NOLPAD; } @@ -314,10 +314,10 @@ newln(struct termp *p) * assertion. */ void -vspace(struct termp *p) +term_vspace(struct termp *p) { - newln(p); + term_newln(p); putchar('\n'); } @@ -329,18 +329,17 @@ vspace(struct termp *p) * the word and put it verbatim into the output buffer. */ void -word(struct termp *p, const char *word) +term_word(struct termp *p, const char *word) { int i, j, len; + len = (int)strlen(word); + if (p->flags & TERMP_LITERAL) { - termp_pword(p, word, (int)strlen(word)); + termp_pword(p, word, len); return; } - if (0 == (len = (int)strlen(word))) - errx(1, "blank line not in literal context"); - if (mdoc_isdelim(word)) { if ( ! (p->flags & TERMP_IGNDELIM)) p->flags |= TERMP_NOSPACE; @@ -373,14 +372,26 @@ word(struct termp *p, const char *word) } +static void +termp_body(struct termp *p, struct termpair *ppair, + const struct mdoc_meta *meta, + const struct mdoc_node *node) +{ + + term_node(p, ppair, meta, node); + if (node->next) + termp_body(p, ppair, meta, node->next); +} + + /* * This is the main function for printing out nodes. It's constituted * of PRE and POST functions, which correspond to prefix and infix * processing. The termpair structure allows data to persist between * prefix and postfix invocations. */ -static void -termp_body(struct termp *p, struct termpair *ppair, +void +term_node(struct termp *p, struct termpair *ppair, const struct mdoc_meta *meta, const struct mdoc_node *node) { @@ -405,7 +416,7 @@ termp_body(struct termp *p, struct termpair *ppair, if ( ! (*termacts[node->tok].pre)(p, &pair, meta, node)) dochild = 0; } else /* MDOC_TEXT == node->type */ - word(p, node->string); + term_word(p, node->string); /* Children. */ @@ -423,11 +434,6 @@ termp_body(struct termp *p, struct termpair *ppair, if (MDOC_TEXT != node->type) if (termacts[node->tok].post) (*termacts[node->tok].post)(p, &pair, meta, node); - - /* Siblings. */ - - if (node->next) - termp_body(p, ppair, meta, node->next); } @@ -460,22 +466,22 @@ termp_foot(struct termp *p, const struct mdoc_meta *meta) * OS MDOCDATE */ - vspace(p); + term_vspace(p); p->flags |= TERMP_NOSPACE | TERMP_NOBREAK; p->rmargin = p->maxrmargin - strlen(buf); p->offset = 0; - word(p, os); - flushln(p); + term_word(p, os); + term_flushln(p); p->flags |= TERMP_NOLPAD | TERMP_NOSPACE; p->offset = p->rmargin; p->rmargin = p->maxrmargin; p->flags &= ~TERMP_NOBREAK; - word(p, buf); - flushln(p); + term_word(p, buf); + term_flushln(p); free(buf); free(os); @@ -524,23 +530,23 @@ termp_head(struct termp *p, const struct mdoc_meta *meta) p->rmargin = (p->maxrmargin - strlen(buf)) / 2; p->flags |= TERMP_NOBREAK | TERMP_NOSPACE; - word(p, title); - flushln(p); + term_word(p, title); + term_flushln(p); p->flags |= TERMP_NOLPAD | TERMP_NOSPACE; p->offset = p->rmargin; p->rmargin = p->maxrmargin - strlen(title); - word(p, buf); - flushln(p); + term_word(p, buf); + term_flushln(p); p->offset = p->rmargin; p->rmargin = p->maxrmargin; p->flags &= ~TERMP_NOBREAK; p->flags |= TERMP_NOLPAD | TERMP_NOSPACE; - word(p, title); - flushln(p); + term_word(p, title); + term_flushln(p); p->rmargin = p->maxrmargin; p->offset = 0; @@ -562,7 +568,7 @@ termp_nescape(struct termp *p, const char *word, size_t len) const char *rhs; size_t sz; - if (NULL == (rhs = a2ascii(p->symtab, word, len, &sz))) + if (NULL == (rhs = term_a2ascii(p->symtab, word, len, &sz))) return; termp_stringa(p, rhs, sz); } @@ -637,8 +643,7 @@ termp_pword(struct termp *p, const char *word, int len) { int i; - if ( ! (TERMP_NOSPACE & p->flags) && - ! (TERMP_LITERAL & p->flags)) + if ( ! (TERMP_NOSPACE & p->flags)) termp_chara(p, ' '); if ( ! (p->flags & TERMP_NONOSPACE)) @@ -40,6 +40,7 @@ #define POST_ARGS struct mdoc *mdoc enum merr { + EESCAPE, EPRINT, ENODATA, ENOPROLOGUE, @@ -56,6 +57,7 @@ enum merr { }; enum mwarn { + WESCAPE, WWRONGMSEC, WSECOOO, WSECREP, @@ -192,10 +194,7 @@ static v_post posts_at[] = { post_at, NULL }; static v_post posts_xr[] = { eerr_ge1, eerr_le2, NULL }; static v_post posts_nm[] = { post_nm, NULL }; static v_post posts_bf[] = { hwarn_le1, post_bf, NULL }; -static v_post posts_rs[] = { herr_eq0, bwarn_ge1, NULL }; static v_post posts_fo[] = { hwarn_eq1, bwarn_ge1, NULL }; -static v_post posts_bk[] = { herr_eq0, bwarn_ge1, NULL }; -static v_post posts_fd[] = { ewarn_ge1, NULL }; const struct valids mdoc_valids[MDOC_MAX] = { { NULL, NULL }, /* \" */ @@ -222,7 +221,7 @@ const struct valids mdoc_valids[MDOC_MAX] = { { NULL, posts_text }, /* Ev */ { pres_ex, posts_ex }, /* Ex */ { NULL, posts_text }, /* Fa */ - { pres_fd, posts_fd }, /* Fd */ + { pres_fd, posts_wtext }, /* Fd */ { NULL, NULL }, /* Fl */ { NULL, posts_text }, /* Fn */ { NULL, posts_wtext }, /* Ft */ @@ -283,7 +282,7 @@ const struct valids mdoc_valids[MDOC_MAX] = { { NULL, NULL }, /* Qo */ { NULL, posts_wline }, /* Qq */ { NULL, NULL }, /* Re */ - { NULL, posts_rs }, /* Rs */ + { NULL, posts_wline }, /* Rs */ { NULL, NULL }, /* Sc */ { NULL, NULL }, /* So */ { NULL, posts_wline }, /* Sq */ @@ -298,7 +297,7 @@ const struct valids mdoc_valids[MDOC_MAX] = { { NULL, NULL }, /* Fc */ { NULL, NULL }, /* Oo */ { NULL, NULL }, /* Oc */ - { NULL, posts_bk }, /* Bk */ + { NULL, posts_wline }, /* Bk */ { NULL, NULL }, /* Ek */ { NULL, posts_notext }, /* Bt */ { NULL, NULL }, /* Hf */ @@ -384,6 +383,9 @@ perr(struct mdoc *m, int line, int pos, enum merr type) p = NULL; switch (type) { + case (EESCAPE): + p = "invalid escape sequence"; + break; case (EPRINT): p = "invalid character"; break; @@ -458,6 +460,9 @@ pwarn(struct mdoc *m, int line, int pos, enum mwarn type) p = "prologue macros out-of-order"; c = WARN_COMPAT; break; + case (WESCAPE): + p = "invalid escape sequence"; + break; case (WNOLINE): p = "suggested no line arguments"; break; @@ -690,22 +695,24 @@ check_text(struct mdoc *mdoc, int line, int pos, const char *p) size_t c; /* FIXME: indicate deprecated escapes \*(xx and \*x. */ - /* FIXME: don't allow tabs unless in literal mode. */ for ( ; *p; p++) { - if ('\t' != *p && ! isprint((u_char)*p)) + if ('\t' == *p) { + if ( ! (MDOC_LITERAL & mdoc->flags)) + return(perr(mdoc, line, pos, EPRINT)); + } else if ( ! isprint((u_char)*p)) return(perr(mdoc, line, pos, EPRINT)); + if ('\\' != *p) continue; + if ((c = mdoc_isescape(p))) { p += (int)c - 1; continue; } if ( ! (MDOC_IGN_ESCAPE & mdoc->pflags)) - return(mdoc_perr(mdoc, line, pos, - "invalid escape sequence")); - if ( ! mdoc_pwarn(mdoc, line, pos, WARN_SYNTAX, - "invalid escape sequence")) + return(perr(mdoc, line, pos, EESCAPE)); + if ( ! pwarn(mdoc, line, pos, WESCAPE)) return(0); } |