version 1.1, 2009/03/26 14:38:11 |
version 1.9, 2009/06/11 12:07:49 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org> |
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se> |
* |
* |
* Permission to use, copy, modify, and distribute this software for any |
* Permission to use, copy, modify, and distribute this software for any |
* purpose with or without fee is hereby granted, provided that the |
* purpose with or without fee is hereby granted, provided that the above |
* above copyright notice and this permission notice appear in all |
* copyright notice and this permission notice appear in all copies. |
* copies. |
|
* |
* |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
* PERFORMANCE OF THIS SOFTWARE. |
|
*/ |
*/ |
#include <sys/types.h> |
#include <sys/types.h> |
|
|
Line 306 static const struct termact termacts[MDOC_MAX] = { |
|
Line 304 static const struct termact termacts[MDOC_MAX] = { |
|
{ NULL, NULL }, /* %Q */ |
{ NULL, NULL }, /* %Q */ |
}; |
}; |
|
|
|
#ifdef __linux__ |
|
extern size_t strlcpy(char *, const char *, size_t); |
|
extern size_t strlcat(char *, const char *, size_t); |
|
#endif |
|
|
static int arg_hasattr(int, const struct mdoc_node *); |
static int arg_hasattr(int, const struct mdoc_node *); |
static int arg_getattrs(const int *, int *, size_t, |
static int arg_getattrs(const int *, int *, size_t, |
const struct mdoc_node *); |
const struct mdoc_node *); |
Line 316 static int arg_listtype(const struct mdoc_node *); |
|
Line 319 static int arg_listtype(const struct mdoc_node *); |
|
static int fmt_block_vspace(struct termp *, |
static int fmt_block_vspace(struct termp *, |
const struct mdoc_node *, |
const struct mdoc_node *, |
const struct mdoc_node *); |
const struct mdoc_node *); |
static int print_node(DECL_ARGS); |
static void print_node(DECL_ARGS); |
static int print_head(struct termp *, |
static void print_head(struct termp *, |
const struct mdoc_meta *); |
const struct mdoc_meta *); |
static int print_body(DECL_ARGS); |
static void print_body(DECL_ARGS); |
static int print_foot(struct termp *, |
static void print_foot(struct termp *, |
const struct mdoc_meta *); |
const struct mdoc_meta *); |
static void sanity(const struct mdoc_node *); |
|
|
|
|
|
int |
int |
mdoc_run(struct termp *p, const struct mdoc *m) |
mdoc_run(struct termp *p, const struct mdoc *m) |
{ |
{ |
|
/* |
|
* Main output function. When this is called, assume that the |
|
* tree is properly formed. |
|
*/ |
|
|
if ( ! print_head(p, mdoc_meta(m))) |
print_head(p, mdoc_meta(m)); |
return(0); |
print_body(p, NULL, mdoc_meta(m), mdoc_node(m)); |
if ( ! print_body(p, NULL, mdoc_meta(m), mdoc_node(m))) |
print_foot(p, mdoc_meta(m)); |
return(0); |
return(1); |
return(print_foot(p, mdoc_meta(m))); |
|
} |
} |
|
|
|
|
static int |
static void |
print_body(DECL_ARGS) |
print_body(DECL_ARGS) |
{ |
{ |
|
|
if ( ! print_node(p, pair, meta, node)) |
print_node(p, pair, meta, node); |
return(0); |
|
if ( ! node->next) |
if ( ! node->next) |
return(1); |
return; |
return(print_body(p, pair, meta, node->next)); |
print_body(p, pair, meta, node->next); |
} |
} |
|
|
|
|
static int |
static void |
print_node(DECL_ARGS) |
print_node(DECL_ARGS) |
{ |
{ |
int dochild; |
int dochild; |
struct termpair npair; |
struct termpair npair; |
|
|
/* Some quick sanity-checking. */ |
|
|
|
sanity(node); |
|
|
|
/* Pre-processing. */ |
/* Pre-processing. */ |
|
|
dochild = 1; |
dochild = 1; |
Line 391 print_node(DECL_ARGS) |
|
Line 391 print_node(DECL_ARGS) |
|
if (MDOC_TEXT != node->type) |
if (MDOC_TEXT != node->type) |
if (termacts[node->tok].post) |
if (termacts[node->tok].post) |
(*termacts[node->tok].post)(p, &npair, meta, node); |
(*termacts[node->tok].post)(p, &npair, meta, node); |
|
|
return(1); |
|
} |
} |
|
|
|
|
static int |
static void |
print_foot(struct termp *p, const struct mdoc_meta *meta) |
print_foot(struct termp *p, const struct mdoc_meta *meta) |
{ |
{ |
struct tm *tm; |
struct tm *tm; |
char *buf, *os; |
char *buf, *os; |
|
|
|
/* |
|
* Output the footer in new-groff style, that is, three columns |
|
* with the middle being the manual date and flanking columns |
|
* being the operating system: |
|
* |
|
* SYSTEM DATE SYSTEM |
|
*/ |
|
|
if (NULL == (buf = malloc(p->rmargin))) |
if (NULL == (buf = malloc(p->rmargin))) |
err(1, "malloc"); |
err(1, "malloc"); |
if (NULL == (os = malloc(p->rmargin))) |
if (NULL == (os = malloc(p->rmargin))) |
Line 418 print_foot(struct termp *p, const struct mdoc_meta *me |
|
Line 424 print_foot(struct termp *p, const struct mdoc_meta *me |
|
|
|
(void)strlcpy(os, meta->os, p->rmargin); |
(void)strlcpy(os, meta->os, p->rmargin); |
|
|
/* |
|
* This is /slightly/ different from regular groff output |
|
* because we don't have page numbers. Print the following: |
|
* |
|
* OS MDOCDATE |
|
*/ |
|
|
|
term_vspace(p); |
term_vspace(p); |
|
|
p->flags |= TERMP_NOSPACE | TERMP_NOBREAK; |
|
p->rmargin = p->maxrmargin - strlen(buf); |
|
p->offset = 0; |
p->offset = 0; |
|
p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2; |
|
p->flags |= TERMP_NOSPACE | TERMP_NOBREAK; |
|
|
term_word(p, os); |
term_word(p, os); |
term_flushln(p); |
term_flushln(p); |
|
|
|
p->offset = p->rmargin; |
|
p->rmargin = p->maxrmargin - strlen(os); |
p->flags |= TERMP_NOLPAD | TERMP_NOSPACE; |
p->flags |= TERMP_NOLPAD | TERMP_NOSPACE; |
|
|
|
term_word(p, buf); |
|
term_flushln(p); |
|
|
p->offset = p->rmargin; |
p->offset = p->rmargin; |
p->rmargin = p->maxrmargin; |
p->rmargin = p->maxrmargin; |
p->flags &= ~TERMP_NOBREAK; |
p->flags &= ~TERMP_NOBREAK; |
|
p->flags |= TERMP_NOLPAD | TERMP_NOSPACE; |
|
|
term_word(p, buf); |
term_word(p, os); |
term_flushln(p); |
term_flushln(p); |
|
|
|
p->offset = 0; |
|
p->rmargin = p->maxrmargin; |
|
p->flags = 0; |
|
|
free(buf); |
free(buf); |
free(os); |
free(os); |
|
|
return(1); |
|
} |
} |
|
|
|
|
static int |
static void |
print_head(struct termp *p, const struct mdoc_meta *meta) |
print_head(struct termp *p, const struct mdoc_meta *meta) |
{ |
{ |
char *buf, *title; |
char *buf, *title; |
Line 488 print_head(struct termp *p, const struct mdoc_meta *me |
|
Line 496 print_head(struct termp *p, const struct mdoc_meta *me |
|
meta->title, meta->msec); |
meta->title, meta->msec); |
|
|
p->offset = 0; |
p->offset = 0; |
p->rmargin = (p->maxrmargin - strlen(buf)) / 2; |
p->rmargin = (p->maxrmargin - strlen(buf) + 1) / 2; |
p->flags |= TERMP_NOBREAK | TERMP_NOSPACE; |
p->flags |= TERMP_NOBREAK | TERMP_NOSPACE; |
|
|
term_word(p, title); |
term_word(p, title); |
term_flushln(p); |
term_flushln(p); |
|
|
p->flags |= TERMP_NOLPAD | TERMP_NOSPACE; |
|
p->offset = p->rmargin; |
p->offset = p->rmargin; |
p->rmargin = p->maxrmargin - strlen(title); |
p->rmargin = p->maxrmargin - strlen(title); |
|
p->flags |= TERMP_NOLPAD | TERMP_NOSPACE; |
|
|
term_word(p, buf); |
term_word(p, buf); |
term_flushln(p); |
term_flushln(p); |
Line 509 print_head(struct termp *p, const struct mdoc_meta *me |
|
Line 517 print_head(struct termp *p, const struct mdoc_meta *me |
|
term_word(p, title); |
term_word(p, title); |
term_flushln(p); |
term_flushln(p); |
|
|
p->rmargin = p->maxrmargin; |
|
p->offset = 0; |
p->offset = 0; |
|
p->rmargin = p->maxrmargin; |
p->flags &= ~TERMP_NOSPACE; |
p->flags &= ~TERMP_NOSPACE; |
|
|
free(title); |
free(title); |
free(buf); |
free(buf); |
|
|
return(1); |
|
} |
} |
|
|
|
|
static void |
|
sanity(const struct mdoc_node *n) |
|
{ |
|
char *p; |
|
|
|
p = "regular form violated"; |
|
|
|
switch (n->type) { |
|
case (MDOC_TEXT): |
|
if (n->child) |
|
errx(1, p); |
|
if (NULL == n->parent) |
|
errx(1, p); |
|
if (NULL == n->string) |
|
errx(1, p); |
|
switch (n->parent->type) { |
|
case (MDOC_TEXT): |
|
/* FALLTHROUGH */ |
|
case (MDOC_ROOT): |
|
errx(1, p); |
|
/* NOTREACHED */ |
|
default: |
|
break; |
|
} |
|
break; |
|
case (MDOC_ELEM): |
|
if (NULL == n->parent) |
|
errx(1, p); |
|
switch (n->parent->type) { |
|
case (MDOC_TAIL): |
|
/* FALLTHROUGH */ |
|
case (MDOC_BODY): |
|
/* FALLTHROUGH */ |
|
case (MDOC_HEAD): |
|
break; |
|
default: |
|
errx(1, p); |
|
/* NOTREACHED */ |
|
} |
|
if (n->child) switch (n->child->type) { |
|
case (MDOC_TEXT): |
|
break; |
|
default: |
|
errx(1, p); |
|
/* NOTREACHED */ |
|
} |
|
break; |
|
case (MDOC_HEAD): |
|
/* FALLTHROUGH */ |
|
case (MDOC_BODY): |
|
/* FALLTHROUGH */ |
|
case (MDOC_TAIL): |
|
if (NULL == n->parent) |
|
errx(1, p); |
|
if (MDOC_BLOCK != n->parent->type) |
|
errx(1, p); |
|
if (n->child) switch (n->child->type) { |
|
case (MDOC_BLOCK): |
|
/* FALLTHROUGH */ |
|
case (MDOC_ELEM): |
|
/* FALLTHROUGH */ |
|
case (MDOC_TEXT): |
|
break; |
|
default: |
|
errx(1, p); |
|
/* NOTREACHED */ |
|
} |
|
break; |
|
case (MDOC_BLOCK): |
|
if (NULL == n->parent) |
|
errx(1, p); |
|
if (NULL == n->child) |
|
errx(1, p); |
|
switch (n->parent->type) { |
|
case (MDOC_ROOT): |
|
/* FALLTHROUGH */ |
|
case (MDOC_HEAD): |
|
/* FALLTHROUGH */ |
|
case (MDOC_BODY): |
|
/* FALLTHROUGH */ |
|
case (MDOC_TAIL): |
|
break; |
|
default: |
|
errx(1, p); |
|
/* NOTREACHED */ |
|
} |
|
switch (n->child->type) { |
|
case (MDOC_ROOT): |
|
/* FALLTHROUGH */ |
|
case (MDOC_ELEM): |
|
errx(1, p); |
|
/* NOTREACHED */ |
|
default: |
|
break; |
|
} |
|
break; |
|
case (MDOC_ROOT): |
|
if (n->parent) |
|
errx(1, p); |
|
if (NULL == n->child) |
|
errx(1, p); |
|
switch (n->child->type) { |
|
case (MDOC_BLOCK): |
|
break; |
|
default: |
|
errx(1, p); |
|
/* NOTREACHED */ |
|
} |
|
break; |
|
} |
|
} |
|
|
|
|
|
static size_t |
static size_t |
arg_width(const struct mdoc_argv *arg, int pos) |
arg_width(const struct mdoc_argv *arg, int pos) |
{ |
{ |
|
|
termp_d1_pre(DECL_ARGS) |
termp_d1_pre(DECL_ARGS) |
{ |
{ |
|
|
if (MDOC_BODY != node->type) |
if (MDOC_BLOCK != node->type) |
return(1); |
return(1); |
term_newln(p); |
term_newln(p); |
p->offset += (pair->offset = INDENT); |
p->offset += (pair->offset = INDENT); |
|
|
termp_d1_post(DECL_ARGS) |
termp_d1_post(DECL_ARGS) |
{ |
{ |
|
|
if (MDOC_BODY != node->type) |
if (MDOC_BLOCK != node->type) |
return; |
return; |
term_newln(p); |
term_newln(p); |
p->offset -= pair->offset; |
p->offset -= pair->offset; |
Line 1845 termp_ss_pre(DECL_ARGS) |
|
Line 1738 termp_ss_pre(DECL_ARGS) |
|
break; |
break; |
case (MDOC_HEAD): |
case (MDOC_HEAD): |
TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SSECTION]); |
TERMPAIR_SETFLAG(p, pair, ttypes[TTYPE_SSECTION]); |
p->offset = INDENT / 2; |
p->offset = HALFINDENT; |
break; |
break; |
default: |
default: |
break; |
break; |