From ccc10e57fbb135c2fc3114a4d642963527f2fbc5 Mon Sep 17 00:00:00 2001 From: Ingo Schwarze Date: Sun, 19 Apr 2015 14:25:41 +0000 Subject: Unify some node handling functions that use TOKEN_NONE. * mdoc_word_alloc(), man_word_alloc() -> roff_word_alloc() * mdoc_word_append(), man_word_append() -> roff_word_append() * mdoc_addspan(), man_addspan() -> roff_addtbl() * mdoc_addeqn(), man_addeqn() -> roff_addeqn() Minus 50 lines of code, no functional change. --- libman.h | 4 +--- libmandoc.h | 6 +----- libmdoc.h | 7 +------ man.c | 62 +++--------------------------------------------------- man_macro.c | 10 ++++----- mdoc.c | 56 ++----------------------------------------------- mdoc_macro.c | 6 +++--- mdoc_validate.c | 12 +++++------ read.c | 19 +++++++---------- roff.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- roff_int.h | 21 +++++++++++++++++-- 11 files changed, 110 insertions(+), 158 deletions(-) diff --git a/libman.h b/libman.h index ab407fc5..30b0a326 100644 --- a/libman.h +++ b/libman.h @@ -1,4 +1,4 @@ -/* $Id: libman.h,v 1.74 2015/04/19 13:50:25 schwarze Exp $ */ +/* $Id: libman.h,v 1.75 2015/04/19 14:25:41 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2014, 2015 Ingo Schwarze @@ -36,8 +36,6 @@ extern const struct man_macro *const man_macros; __BEGIN_DECLS -void man_word_alloc(struct roff_man *, int, int, const char *); -void man_word_append(struct roff_man *, const char *); void man_block_alloc(struct roff_man *, int, int, int); void man_elem_alloc(struct roff_man *, int, int, int); int man_hash_find(const char *); diff --git a/libmandoc.h b/libmandoc.h index 2e8a58af..22dcb5e7 100644 --- a/libmandoc.h +++ b/libmandoc.h @@ -1,4 +1,4 @@ -/* $Id: libmandoc.h,v 1.59 2015/04/19 13:50:25 schwarze Exp $ */ +/* $Id: libmandoc.h,v 1.60 2015/04/19 14:25:41 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011, 2012 Kristaps Dzonsons * Copyright (c) 2013, 2014, 2015 Ingo Schwarze @@ -58,14 +58,10 @@ const char *mandoc_a2msec(const char*); void mdoc_hash_init(void); int mdoc_parseln(struct roff_man *, int, char *, int); void mdoc_endparse(struct roff_man *); -void mdoc_addspan(struct roff_man *, const struct tbl_span *); -void mdoc_addeqn(struct roff_man *, const struct eqn *); void man_hash_init(void); int man_parseln(struct roff_man *, int, char *, int); void man_endparse(struct roff_man *); -void man_addspan(struct roff_man *, const struct tbl_span *); -void man_addeqn(struct roff_man *, const struct eqn *); int preconv_cue(const struct buf *, size_t); int preconv_encode(struct buf *, size_t *, diff --git a/libmdoc.h b/libmdoc.h index abee0967..3f9e3365 100644 --- a/libmdoc.h +++ b/libmdoc.h @@ -1,4 +1,4 @@ -/* $Id: libmdoc.h,v 1.103 2015/04/19 13:50:25 schwarze Exp $ */ +/* $Id: libmdoc.h,v 1.104 2015/04/19 14:25:41 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2013, 2014, 2015 Ingo Schwarze @@ -67,8 +67,6 @@ extern const struct mdoc_macro *const mdoc_macros; __BEGIN_DECLS void mdoc_macro(MACRO_PROT_ARGS); -void mdoc_word_alloc(struct roff_man *, int, int, const char *); -void mdoc_word_append(struct roff_man *, const char *); void mdoc_elem_alloc(struct roff_man *, int, int, int, struct mdoc_arg *); struct roff_node *mdoc_block_alloc(struct roff_man *, int, int, @@ -82,11 +80,8 @@ const char *mdoc_a2att(const char *); const char *mdoc_a2lib(const char *); const char *mdoc_a2st(const char *); const char *mdoc_a2arch(const char *); -void mdoc_valid_pre(struct roff_man *, struct roff_node *); -void mdoc_valid_post(struct roff_man *); void mdoc_argv(struct roff_man *, int, int, struct mdoc_arg **, int *, char *); -void mdoc_argv_free(struct mdoc_arg *); enum margserr mdoc_args(struct roff_man *, int, int *, char *, int, char **); void mdoc_macroend(struct roff_man *); diff --git a/man.c b/man.c index cebe1b73..6c0b1787 100644 --- a/man.c +++ b/man.c @@ -1,4 +1,4 @@ -/* $Id: man.c,v 1.159 2015/04/19 14:00:19 schwarze Exp $ */ +/* $Id: man.c,v 1.160 2015/04/19 14:25:41 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2013, 2014, 2015 Ingo Schwarze @@ -50,7 +50,6 @@ const char *const __man_macronames[MAN_MAX] = { const char * const *man_macronames = __man_macronames; -static void man_breakscope(struct roff_man *, int); static void man_descope(struct roff_man *, int, int); static int man_ptext(struct roff_man *, int, char *, int); static int man_pmacro(struct roff_man *, int, char *, int); @@ -95,61 +94,6 @@ man_block_alloc(struct roff_man *man, int line, int pos, int tok) man->next = ROFF_NEXT_CHILD; } -void -man_word_alloc(struct roff_man *man, int line, int pos, const char *word) -{ - struct roff_node *n; - - n = roff_node_alloc(man, line, pos, ROFFT_TEXT, TOKEN_NONE); - n->string = roff_strdup(man->roff, word); - roff_node_append(man, n); - man_valid_post(man); - man->next = ROFF_NEXT_SIBLING; -} - -void -man_word_append(struct roff_man *man, const char *word) -{ - struct roff_node *n; - char *addstr, *newstr; - - n = man->last; - addstr = roff_strdup(man->roff, word); - mandoc_asprintf(&newstr, "%s %s", n->string, addstr); - free(addstr); - free(n->string); - n->string = newstr; - man->next = ROFF_NEXT_SIBLING; -} - -void -man_addeqn(struct roff_man *man, const struct eqn *ep) -{ - struct roff_node *n; - - n = roff_node_alloc(man, ep->ln, ep->pos, ROFFT_EQN, TOKEN_NONE); - n->eqn = ep; - if (ep->ln > man->last->line) - n->flags |= MAN_LINE; - roff_node_append(man, n); - man->next = ROFF_NEXT_SIBLING; - man_descope(man, ep->ln, ep->pos); -} - -void -man_addspan(struct roff_man *man, const struct tbl_span *sp) -{ - struct roff_node *n; - - man_breakscope(man, TOKEN_NONE); - n = roff_node_alloc(man, sp->line, 0, ROFFT_TBL, TOKEN_NONE); - n->span = sp; - roff_node_append(man, n); - man_valid_post(man); - man->next = ROFF_NEXT_SIBLING; - man_descope(man, sp->line, 0); -} - static void man_descope(struct roff_man *man, int line, int offs) { @@ -178,7 +122,7 @@ man_ptext(struct roff_man *man, int line, char *buf, int offs) /* Literal free-form text whitespace is preserved. */ if (man->flags & MAN_LITERAL) { - man_word_alloc(man, line, offs, buf + offs); + roff_word_alloc(man, line, offs, buf + offs); man_descope(man, line, offs); return(1); } @@ -222,7 +166,7 @@ man_ptext(struct roff_man *man, int line, char *buf, int offs) buf[i] = '\0'; } - man_word_alloc(man, line, offs, buf + offs); + roff_word_alloc(man, line, offs, buf + offs); /* * End-of-sentence check. If the last character is an unescaped diff --git a/man_macro.c b/man_macro.c index 55dc31cd..d82811d4 100644 --- a/man_macro.c +++ b/man_macro.c @@ -1,4 +1,4 @@ -/* $Id: man_macro.c,v 1.106 2015/04/19 13:50:25 schwarze Exp $ */ +/* $Id: man_macro.c,v 1.107 2015/04/19 14:25:41 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2012, 2013, 2014, 2015 Ingo Schwarze @@ -264,7 +264,7 @@ blk_exp(MACRO_PROT_ARGS) la = *pos; if (man_args(man, line, pos, buf, &p)) - man_word_alloc(man, line, la, p); + roff_word_alloc(man, line, la, p); if (buf[*pos] != '\0') mandoc_vmsg(MANDOCERR_ARG_EXCESS, @@ -301,7 +301,7 @@ blk_imp(MACRO_PROT_ARGS) la = *pos; if ( ! man_args(man, line, pos, buf, &p)) break; - man_word_alloc(man, line, la, p); + roff_word_alloc(man, line, la, p); } /* @@ -352,9 +352,9 @@ in_line_eoln(MACRO_PROT_ARGS) break; if (man_macros[tok].flags & MAN_JOIN && man->last->type == ROFFT_TEXT) - man_word_append(man, p); + roff_word_append(man, p); else - man_word_alloc(man, line, la, p); + roff_word_alloc(man, line, la, p); } /* diff --git a/mdoc.c b/mdoc.c index 881355f9..a397fafc 100644 --- a/mdoc.c +++ b/mdoc.c @@ -1,4 +1,4 @@ -/* $Id: mdoc.c,v 1.248 2015/04/19 14:00:19 schwarze Exp $ */ +/* $Id: mdoc.c,v 1.249 2015/04/19 14:25:41 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010, 2012-2015 Ingo Schwarze @@ -96,31 +96,6 @@ mdoc_endparse(struct roff_man *mdoc) mdoc_macroend(mdoc); } -void -mdoc_addeqn(struct roff_man *mdoc, const struct eqn *ep) -{ - struct roff_node *n; - - n = roff_node_alloc(mdoc, ep->ln, ep->pos, ROFFT_EQN, TOKEN_NONE); - n->eqn = ep; - if (ep->ln > mdoc->last->line) - n->flags |= MDOC_LINE; - roff_node_append(mdoc, n); - mdoc->next = ROFF_NEXT_SIBLING; -} - -void -mdoc_addspan(struct roff_man *mdoc, const struct tbl_span *sp) -{ - struct roff_node *n; - - n = roff_node_alloc(mdoc, sp->line, 0, ROFFT_TBL, TOKEN_NONE); - n->span = sp; - roff_node_append(mdoc, n); - mdoc_valid_post(mdoc); - mdoc->next = ROFF_NEXT_SIBLING; -} - /* * Main parse routine. Parses a single line -- really just hands off to * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()). @@ -254,33 +229,6 @@ mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos, mdoc->next = ROFF_NEXT_CHILD; } -void -mdoc_word_alloc(struct roff_man *mdoc, int line, int pos, const char *p) -{ - struct roff_node *n; - - n = roff_node_alloc(mdoc, line, pos, ROFFT_TEXT, TOKEN_NONE); - n->string = roff_strdup(mdoc->roff, p); - roff_node_append(mdoc, n); - mdoc_valid_post(mdoc); - mdoc->next = ROFF_NEXT_SIBLING; -} - -void -mdoc_word_append(struct roff_man *mdoc, const char *p) -{ - struct roff_node *n; - char *addstr, *newstr; - - n = mdoc->last; - addstr = roff_strdup(mdoc->roff, p); - mandoc_asprintf(&newstr, "%s %s", n->string, addstr); - free(addstr); - free(n->string); - n->string = newstr; - mdoc->next = ROFF_NEXT_SIBLING; -} - void mdoc_node_relink(struct roff_man *mdoc, struct roff_node *p) { @@ -387,7 +335,7 @@ mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs) return(1); } - mdoc_word_alloc(mdoc, line, offs, buf+offs); + roff_word_alloc(mdoc, line, offs, buf+offs); if (mdoc->flags & MDOC_LITERAL) return(1); diff --git a/mdoc_macro.c b/mdoc_macro.c index b19189e4..2f4bc6e1 100644 --- a/mdoc_macro.c +++ b/mdoc_macro.c @@ -1,4 +1,4 @@ -/* $Id: mdoc_macro.c,v 1.191 2015/04/19 14:00:19 schwarze Exp $ */ +/* $Id: mdoc_macro.c,v 1.192 2015/04/19 14:25:41 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010, 2012-2015 Ingo Schwarze @@ -432,11 +432,11 @@ dword(struct roff_man *mdoc, int line, int col, const char *p, ! (mdoc->flags & (MDOC_SYNOPSIS | MDOC_KEEP | MDOC_SMOFF)) && d == DELIM_NONE && mdoc->last->type == ROFFT_TEXT && mdoc_isdelim(mdoc->last->string) == DELIM_NONE) { - mdoc_word_append(mdoc, p); + roff_word_append(mdoc, p); return; } - mdoc_word_alloc(mdoc, line, col, p); + roff_word_alloc(mdoc, line, col, p); /* * If the word consists of a bare delimiter, diff --git a/mdoc_validate.c b/mdoc_validate.c index 4941865c..1df68760 100644 --- a/mdoc_validate.c +++ b/mdoc_validate.c @@ -1,4 +1,4 @@ -/* $Id: mdoc_validate.c,v 1.288 2015/04/19 14:00:20 schwarze Exp $ */ +/* $Id: mdoc_validate.c,v 1.289 2015/04/19 14:25:41 schwarze Exp $ */ /* * Copyright (c) 2008-2012 Kristaps Dzonsons * Copyright (c) 2010-2015 Ingo Schwarze @@ -1051,13 +1051,13 @@ post_defaults(POST_ARGS) switch (nn->tok) { case MDOC_Ar: - mdoc_word_alloc(mdoc, nn->line, nn->pos, "file"); - mdoc_word_alloc(mdoc, nn->line, nn->pos, "..."); + roff_word_alloc(mdoc, nn->line, nn->pos, "file"); + roff_word_alloc(mdoc, nn->line, nn->pos, "..."); break; case MDOC_Pa: /* FALLTHROUGH */ case MDOC_Mt: - mdoc_word_alloc(mdoc, nn->line, nn->pos, "~"); + roff_word_alloc(mdoc, nn->line, nn->pos, "~"); break; default: abort(); @@ -1076,7 +1076,7 @@ post_at(POST_ARGS) n = mdoc->last; if (n->child == NULL) { mdoc->next = ROFF_NEXT_CHILD; - mdoc_word_alloc(mdoc, n->line, n->pos, "AT&T UNIX"); + roff_word_alloc(mdoc, n->line, n->pos, "AT&T UNIX"); mdoc->last = n; return; } @@ -2315,7 +2315,7 @@ post_ex(POST_ARGS) } mdoc->next = ROFF_NEXT_CHILD; - mdoc_word_alloc(mdoc, n->line, n->pos, mdoc->meta.name); + roff_word_alloc(mdoc, n->line, n->pos, mdoc->meta.name); mdoc->last = n; } diff --git a/read.c b/read.c index b4b71b5d..11836f5c 100644 --- a/read.c +++ b/read.c @@ -1,4 +1,4 @@ -/* $Id: read.c,v 1.138 2015/04/19 14:00:20 schwarze Exp $ */ +/* $Id: read.c,v 1.139 2015/04/19 14:25:41 schwarze Exp $ */ /* * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons * Copyright (c) 2010-2015 Ingo Schwarze @@ -43,6 +43,7 @@ #include "mdoc.h" #include "man.h" #include "libmandoc.h" +#include "roff_int.h" #define REPARSE_LIMIT 1000 @@ -583,18 +584,12 @@ rerun: * Do the same for ROFF_EQN. */ - if (rr == ROFF_TBL) { + if (rr == ROFF_TBL) while ((span = roff_span(curp->roff)) != NULL) - if (curp->man->macroset == MACROSET_MDOC) - mdoc_addspan(curp->man, span); - else - man_addspan(curp->man, span); - } else if (rr == ROFF_EQN) { - if (curp->man->macroset == MACROSET_MDOC) - mdoc_addeqn(curp->man, roff_eqn(curp->roff)); - else - man_addeqn(curp->man, roff_eqn(curp->roff)); - } else if ((curp->man->macroset == MACROSET_MDOC ? + roff_addtbl(curp->man, span); + else if (rr == ROFF_EQN) + roff_addeqn(curp->man, roff_eqn(curp->roff)); + else if ((curp->man->macroset == MACROSET_MDOC ? mdoc_parseln(curp->man, curp->line, ln.buf, of) : man_parseln(curp->man, curp->line, ln.buf, of)) == 2) break; diff --git a/roff.c b/roff.c index d671e5cd..6995bbd3 100644 --- a/roff.c +++ b/roff.c @@ -1,6 +1,6 @@ -/* $Id: roff.c,v 1.266 2015/04/19 13:50:26 schwarze Exp $ */ +/* $Id: roff.c,v 1.267 2015/04/19 14:25:41 schwarze Exp $ */ /* - * Copyright (c) 2009-2012, 2014 Kristaps Dzonsons + * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons * Copyright (c) 2010-2015 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any @@ -32,7 +32,6 @@ #include "libmandoc.h" #include "roff_int.h" #include "libroff.h" -#include "libmdoc.h" /* Maximum number of nested if-else conditionals. */ #define RSTACK_MAX 128 @@ -1067,6 +1066,36 @@ roff_node_append(struct roff_man *man, struct roff_node *n) man->last = n; } +void +roff_word_alloc(struct roff_man *man, int line, int pos, const char *word) +{ + struct roff_node *n; + + n = roff_node_alloc(man, line, pos, ROFFT_TEXT, TOKEN_NONE); + n->string = roff_strdup(man->roff, word); + roff_node_append(man, n); + if (man->macroset == MACROSET_MDOC) + mdoc_valid_post(man); + else + man_valid_post(man); + man->next = ROFF_NEXT_SIBLING; +} + +void +roff_word_append(struct roff_man *man, const char *word) +{ + struct roff_node *n; + char *addstr, *newstr; + + n = man->last; + addstr = roff_strdup(man->roff, word); + mandoc_asprintf(&newstr, "%s %s", n->string, addstr); + free(addstr); + free(n->string); + n->string = newstr; + man->next = ROFF_NEXT_SIBLING; +} + struct roff_node * roff_head_alloc(struct roff_man *man, int line, int pos, int tok) { @@ -1089,6 +1118,36 @@ roff_body_alloc(struct roff_man *man, int line, int pos, int tok) return(n); } +void +roff_addeqn(struct roff_man *man, const struct eqn *eqn) +{ + struct roff_node *n; + + n = roff_node_alloc(man, eqn->ln, eqn->pos, ROFFT_EQN, TOKEN_NONE); + n->eqn = eqn; + if (eqn->ln > man->last->line) + n->flags |= MDOC_LINE; + roff_node_append(man, n); + man->next = ROFF_NEXT_SIBLING; +} + +void +roff_addtbl(struct roff_man *man, const struct tbl_span *tbl) +{ + struct roff_node *n; + + if (man->macroset == MACROSET_MAN) + man_breakscope(man, TOKEN_NONE); + n = roff_node_alloc(man, tbl->line, 0, ROFFT_TBL, TOKEN_NONE); + n->span = tbl; + roff_node_append(man, n); + if (man->macroset == MACROSET_MDOC) + mdoc_valid_post(man); + else + man_valid_post(man); + man->next = ROFF_NEXT_SIBLING; +} + void roff_node_unlink(struct roff_man *man, struct roff_node *n) { diff --git a/roff_int.h b/roff_int.h index 4d338628..41cb4d93 100644 --- a/roff_int.h +++ b/roff_int.h @@ -1,7 +1,7 @@ -/* $OpenBSD$ */ +/* $Id: roff_int.h,v 1.2 2015/04/19 14:25:41 schwarze Exp $ */ /* * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons - * Copyright (c) 2014, 2015 Ingo Schwarze + * Copyright (c) 2013, 2014, 2015 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -21,10 +21,27 @@ __BEGIN_DECLS struct roff_node *roff_node_alloc(struct roff_man *, int, int, enum roff_type, int); void roff_node_append(struct roff_man *, struct roff_node *); +void roff_word_alloc(struct roff_man *, int, int, const char *); +void roff_word_append(struct roff_man *, const char *); struct roff_node *roff_head_alloc(struct roff_man *, int, int, int); struct roff_node *roff_body_alloc(struct roff_man *, int, int, int); +void roff_addeqn(struct roff_man *, const struct eqn *); +void roff_addtbl(struct roff_man *, const struct tbl_span *); void roff_node_unlink(struct roff_man *, struct roff_node *); void roff_node_free(struct roff_node *); void roff_node_delete(struct roff_man *, struct roff_node *); +/* + * Functions called from roff.c need to be declared here, + * not in libmdoc.h or libman.h, even if they are specific + * to either the mdoc(7) or the man(7) parser. + */ + +void man_breakscope(struct roff_man *, int); +void man_valid_post(struct roff_man *); + +void mdoc_valid_pre(struct roff_man *, struct roff_node *); +void mdoc_valid_post(struct roff_man *); +void mdoc_argv_free(struct mdoc_arg *); + __END_DECLS -- cgit v1.2.3-56-ge451