version 1.197, 2013/10/21 23:47:58 |
version 1.205, 2014/03/23 15:14:50 |
|
|
/* $Id$ */ |
/* $Id$ */ |
/* |
/* |
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv> |
* Copyright (c) 2010, 2011, 2012, 2013 Ingo Schwarze <schwarze@openbsd.org> |
* Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org> |
|
* Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org> |
* |
* |
* 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 above |
* purpose with or without fee is hereby granted, provided that the above |
|
|
|
|
#include "mdoc.h" |
#include "mdoc.h" |
#include "mandoc.h" |
#include "mandoc.h" |
|
#include "mandoc_aux.h" |
#include "libmdoc.h" |
#include "libmdoc.h" |
#include "libmandoc.h" |
#include "libmandoc.h" |
|
|
|
|
#define POST_ARGS struct mdoc *mdoc |
#define POST_ARGS struct mdoc *mdoc |
|
|
#define NUMSIZ 32 |
#define NUMSIZ 32 |
#define DATESIZE 32 |
|
|
|
enum check_ineq { |
enum check_ineq { |
CHECK_LT, |
CHECK_LT, |
Line 72 static void check_text(struct mdoc *, int, int, char |
|
Line 73 static void check_text(struct mdoc *, int, int, char |
|
static void check_argv(struct mdoc *, |
static void check_argv(struct mdoc *, |
struct mdoc_node *, struct mdoc_argv *); |
struct mdoc_node *, struct mdoc_argv *); |
static void check_args(struct mdoc *, struct mdoc_node *); |
static void check_args(struct mdoc *, struct mdoc_node *); |
static int concat(char *, const struct mdoc_node *, size_t); |
|
static enum mdoc_sec a2sec(const char *); |
static enum mdoc_sec a2sec(const char *); |
static size_t macro2len(enum mdoct); |
static size_t macro2len(enum mdoct); |
|
|
Line 1121 post_vt(POST_ARGS) |
|
Line 1121 post_vt(POST_ARGS) |
|
static int |
static int |
post_nm(POST_ARGS) |
post_nm(POST_ARGS) |
{ |
{ |
char buf[BUFSIZ]; |
|
int c; |
|
|
|
if (NULL != mdoc->meta.name) |
if (NULL != mdoc->meta.name) |
return(1); |
return(1); |
|
|
/* Try to use our children for setting the meta name. */ |
mdoc_deroff(&mdoc->meta.name, mdoc->last); |
|
|
if (NULL != mdoc->last->child) { |
if (NULL == mdoc->meta.name) { |
buf[0] = '\0'; |
|
c = concat(buf, mdoc->last->child, BUFSIZ); |
|
} else |
|
c = 0; |
|
|
|
switch (c) { |
|
case (-1): |
|
mdoc_nmsg(mdoc, mdoc->last->child, MANDOCERR_MEM); |
|
return(0); |
|
case (0): |
|
mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NONAME); |
mdoc_nmsg(mdoc, mdoc->last, MANDOCERR_NONAME); |
mdoc->meta.name = mandoc_strdup("UNKNOWN"); |
mdoc->meta.name = mandoc_strdup("UNKNOWN"); |
break; |
|
default: |
|
mdoc->meta.name = mandoc_strdup(buf); |
|
break; |
|
} |
} |
return(1); |
return(1); |
} |
} |
Line 1893 post_hyph(POST_ARGS) |
|
Line 1877 post_hyph(POST_ARGS) |
|
if (MDOC_TEXT != nch->type) |
if (MDOC_TEXT != nch->type) |
continue; |
continue; |
cp = nch->string; |
cp = nch->string; |
if (3 > strnlen(cp, 3)) |
if ('\0' == *cp) |
continue; |
continue; |
while ('\0' != *(++cp)) |
while ('\0' != *(++cp)) |
if ('-' == *cp && |
if ('-' == *cp && |
Line 1963 post_sh_body(POST_ARGS) |
|
Line 1947 post_sh_body(POST_ARGS) |
|
static int |
static int |
post_sh_head(POST_ARGS) |
post_sh_head(POST_ARGS) |
{ |
{ |
char buf[BUFSIZ]; |
|
struct mdoc_node *n; |
struct mdoc_node *n; |
|
const char *secname; |
enum mdoc_sec sec; |
enum mdoc_sec sec; |
int c; |
|
|
|
/* |
/* |
* Process a new section. Sections are either "named" or |
* Process a new section. Sections are either "named" or |
Line 1975 post_sh_head(POST_ARGS) |
|
Line 1958 post_sh_head(POST_ARGS) |
|
* manual sections. |
* manual sections. |
*/ |
*/ |
|
|
|
secname = NULL; |
sec = SEC_CUSTOM; |
sec = SEC_CUSTOM; |
buf[0] = '\0'; |
n = mdoc->last; |
if (-1 == (c = concat(buf, mdoc->last->child, BUFSIZ))) { |
if (n->child) { |
mdoc_nmsg(mdoc, mdoc->last->child, MANDOCERR_MEM); |
assert(1 == n->nchild); |
return(0); |
n = n->child; |
} else if (1 == c) |
assert(NULL != n); |
sec = a2sec(buf); |
assert(MDOC_TEXT == n->type); |
|
secname = n->string; |
|
sec = a2sec(secname); |
|
} |
|
|
/* The NAME should be first. */ |
/* The NAME should be first. */ |
|
|
Line 1991 post_sh_head(POST_ARGS) |
|
Line 1978 post_sh_head(POST_ARGS) |
|
/* The SYNOPSIS gets special attention in other areas. */ |
/* The SYNOPSIS gets special attention in other areas. */ |
|
|
if (SEC_SYNOPSIS == sec) { |
if (SEC_SYNOPSIS == sec) { |
roff_setreg(mdoc->roff, "nS", 1); |
roff_setreg(mdoc->roff, "nS", 1, '='); |
mdoc->flags |= MDOC_SYNOPSIS; |
mdoc->flags |= MDOC_SYNOPSIS; |
} else { |
} else { |
roff_setreg(mdoc->roff, "nS", 0); |
roff_setreg(mdoc->roff, "nS", 0, '='); |
mdoc->flags &= ~MDOC_SYNOPSIS; |
mdoc->flags &= ~MDOC_SYNOPSIS; |
} |
} |
|
|
Line 2053 post_sh_head(POST_ARGS) |
|
Line 2040 post_sh_head(POST_ARGS) |
|
if (*mdoc->meta.msec == '9') |
if (*mdoc->meta.msec == '9') |
break; |
break; |
mandoc_msg(MANDOCERR_SECMSEC, mdoc->parse, |
mandoc_msg(MANDOCERR_SECMSEC, mdoc->parse, |
mdoc->last->line, mdoc->last->pos, buf); |
mdoc->last->line, mdoc->last->pos, secname); |
break; |
break; |
default: |
default: |
break; |
break; |
Line 2174 pre_literal(PRE_ARGS) |
|
Line 2161 pre_literal(PRE_ARGS) |
|
static int |
static int |
post_dd(POST_ARGS) |
post_dd(POST_ARGS) |
{ |
{ |
char buf[DATESIZE]; |
|
struct mdoc_node *n; |
struct mdoc_node *n; |
int c; |
char *datestr; |
|
|
if (mdoc->meta.date) |
if (mdoc->meta.date) |
free(mdoc->meta.date); |
free(mdoc->meta.date); |
|
|
n = mdoc->last; |
n = mdoc->last; |
if (NULL == n->child || '\0' == n->child->string[0]) { |
if (NULL == n->child || '\0' == n->child->string[0]) { |
mdoc->meta.date = mandoc_normdate |
mdoc->meta.date = mdoc->quick ? mandoc_strdup("") : |
(mdoc->parse, NULL, n->line, n->pos); |
mandoc_normdate(mdoc->parse, NULL, n->line, n->pos); |
return(1); |
return(1); |
} |
} |
|
|
buf[0] = '\0'; |
datestr = NULL; |
if (-1 == (c = concat(buf, n->child, DATESIZE))) { |
mdoc_deroff(&datestr, n); |
mdoc_nmsg(mdoc, n->child, MANDOCERR_MEM); |
if (mdoc->quick) |
return(0); |
mdoc->meta.date = datestr; |
|
else { |
|
mdoc->meta.date = mandoc_normdate(mdoc->parse, |
|
datestr, n->line, n->pos); |
|
free(datestr); |
} |
} |
|
|
assert(c); |
|
mdoc->meta.date = mandoc_normdate |
|
(mdoc->parse, buf, n->line, n->pos); |
|
|
|
return(1); |
return(1); |
} |
} |
|
|
Line 2347 post_bx(POST_ARGS) |
|
Line 2332 post_bx(POST_ARGS) |
|
static int |
static int |
post_os(POST_ARGS) |
post_os(POST_ARGS) |
{ |
{ |
struct mdoc_node *n; |
|
char buf[BUFSIZ]; |
|
int c; |
|
#ifndef OSNAME |
#ifndef OSNAME |
struct utsname utsname; |
struct utsname utsname; |
|
static char *defbuf; |
#endif |
#endif |
|
struct mdoc_node *n; |
|
|
n = mdoc->last; |
n = mdoc->last; |
|
|
Line 2366 post_os(POST_ARGS) |
|
Line 2350 post_os(POST_ARGS) |
|
*/ |
*/ |
|
|
free(mdoc->meta.os); |
free(mdoc->meta.os); |
|
mdoc->meta.os = NULL; |
|
mdoc_deroff(&mdoc->meta.os, n); |
|
if (mdoc->meta.os) |
|
return(1); |
|
|
buf[0] = '\0'; |
if (mdoc->defos) { |
if (-1 == (c = concat(buf, n->child, BUFSIZ))) { |
mdoc->meta.os = mandoc_strdup(mdoc->defos); |
mdoc_nmsg(mdoc, n->child, MANDOCERR_MEM); |
return(1); |
return(0); |
|
} |
} |
|
|
assert(c); |
|
|
|
if ('\0' == buf[0]) { |
|
if (mdoc->defos) { |
|
mdoc->meta.os = mandoc_strdup(mdoc->defos); |
|
return(1); |
|
} |
|
#ifdef OSNAME |
#ifdef OSNAME |
if (strlcat(buf, OSNAME, BUFSIZ) >= BUFSIZ) { |
mdoc->meta.os = mandoc_strdup(OSNAME); |
mdoc_nmsg(mdoc, n, MANDOCERR_MEM); |
|
return(0); |
|
} |
|
#else /*!OSNAME */ |
#else /*!OSNAME */ |
|
if (NULL == defbuf) { |
if (-1 == uname(&utsname)) { |
if (-1 == uname(&utsname)) { |
mdoc_nmsg(mdoc, n, MANDOCERR_UNAME); |
mdoc_nmsg(mdoc, n, MANDOCERR_UNAME); |
mdoc->meta.os = mandoc_strdup("UNKNOWN"); |
defbuf = mandoc_strdup("UNKNOWN"); |
return(post_prol(mdoc)); |
} else |
} |
mandoc_asprintf(&defbuf, "%s %s", |
|
utsname.sysname, utsname.release); |
if (strlcat(buf, utsname.sysname, BUFSIZ) >= BUFSIZ) { |
|
mdoc_nmsg(mdoc, n, MANDOCERR_MEM); |
|
return(0); |
|
} |
|
if (strlcat(buf, " ", BUFSIZ) >= BUFSIZ) { |
|
mdoc_nmsg(mdoc, n, MANDOCERR_MEM); |
|
return(0); |
|
} |
|
if (strlcat(buf, utsname.release, BUFSIZ) >= BUFSIZ) { |
|
mdoc_nmsg(mdoc, n, MANDOCERR_MEM); |
|
return(0); |
|
} |
|
#endif /*!OSNAME*/ |
|
} |
} |
|
mdoc->meta.os = mandoc_strdup(defbuf); |
mdoc->meta.os = mandoc_strdup(buf); |
#endif /*!OSNAME*/ |
return(1); |
return(1); |
} |
} |
|
|
Line 2437 post_std(POST_ARGS) |
|
Line 2402 post_std(POST_ARGS) |
|
return(0); |
return(0); |
|
|
mdoc->last = nn; |
mdoc->last = nn; |
return(1); |
|
} |
|
|
|
/* |
|
* Concatenate a node, stopping at the first non-text. |
|
* Concatenation is separated by a single whitespace. |
|
* Returns -1 on fatal (string overrun) error, 0 if child nodes were |
|
* encountered, 1 otherwise. |
|
*/ |
|
static int |
|
concat(char *p, const struct mdoc_node *n, size_t sz) |
|
{ |
|
|
|
for ( ; NULL != n; n = n->next) { |
|
if (MDOC_TEXT != n->type) |
|
return(0); |
|
if ('\0' != p[0] && strlcat(p, " ", sz) >= sz) |
|
return(-1); |
|
if (strlcat(p, n->string, sz) >= sz) |
|
return(-1); |
|
concat(p, n->child, sz); |
|
} |
|
|
|
return(1); |
return(1); |
} |
} |
|
|