-/* $Id: man_macro.c,v 1.51 2010/11/30 15:36:28 kristaps Exp $ */
+/* $Id: man_macro.c,v 1.62 2011/04/19 16:38:48 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
*
#include <stdlib.h>
#include <string.h>
+#include "man.h"
#include "mandoc.h"
+#include "libmandoc.h"
#include "libman.h"
enum rew {
static int blk_exp(MACRO_PROT_ARGS);
static int blk_imp(MACRO_PROT_ARGS);
static int in_line_eoln(MACRO_PROT_ARGS);
+static int man_args(struct man *, int,
+ int *, char *, char **);
static int rew_scope(enum man_type,
struct man *, enum mant);
const struct man_node *);
static enum rew rew_block(enum mant, enum man_type,
const struct man_node *);
-static int rew_warn(struct man *,
+static void rew_warn(struct man *,
struct man_node *, enum mandocerr);
const struct man_macro __man_macros[MAN_MAX] = {
{ in_line_eoln, 0 }, /* IR */
{ in_line_eoln, 0 }, /* RI */
{ in_line_eoln, MAN_NSCOPED }, /* na */
- { in_line_eoln, 0 }, /* i */
{ in_line_eoln, MAN_NSCOPED }, /* sp */
{ in_line_eoln, 0 }, /* nf */
{ in_line_eoln, 0 }, /* fi */
- { in_line_eoln, 0 }, /* r */
{ blk_close, 0 }, /* RE */
{ blk_exp, MAN_EXPLICIT }, /* RS */
{ in_line_eoln, 0 }, /* DT */
{ in_line_eoln, 0 }, /* UC */
{ in_line_eoln, 0 }, /* PD */
- { in_line_eoln, MAN_NSCOPED }, /* Sp */
- { in_line_eoln, 0 }, /* Vb */
- { in_line_eoln, 0 }, /* Ve */
{ in_line_eoln, 0 }, /* AT */
{ in_line_eoln, 0 }, /* in */
+ { in_line_eoln, 0 }, /* ft */
};
const struct man_macro * const man_macros = __man_macros;
/*
* Warn when "n" is an explicit non-roff macro.
*/
-static int
+static void
rew_warn(struct man *m, struct man_node *n, enum mandocerr er)
{
if (er == MANDOCERR_MAX || MAN_BLOCK != n->type)
- return(1);
+ return;
if (MAN_VALID & n->flags)
- return(1);
+ return;
if ( ! (MAN_EXPLICIT & man_macros[n->tok].flags))
- return(1);
- return(man_nmsg(m, n, er));
+ return;
+
+ assert(er < MANDOCERR_FATAL);
+ man_nmsg(m, n, er);
}
* will be used if an explicit block scope is being closed out.
*/
int
-man_unscope(struct man *m, const struct man_node *n,
+man_unscope(struct man *m, const struct man_node *to,
enum mandocerr er)
{
+ struct man_node *n;
- assert(n);
+ assert(to);
/* LINTED */
- while (m->last != n) {
- if ( ! rew_warn(m, m->last, er))
- return(0);
+ while (m->last != to) {
+ /*
+ * Save the parent here, because we may delete the
+ * m->last node in the post-validation phase and reset
+ * it to m->last->parent, causing a step in the closing
+ * out to be lost.
+ */
+ n = m->last->parent;
+ rew_warn(m, m->last, er);
if ( ! man_valid_post(m))
return(0);
- m->last = m->last->parent;
+ m->last = n;
assert(m->last);
}
- if ( ! rew_warn(m, m->last, er))
- return(0);
+ rew_warn(m, m->last, er);
if ( ! man_valid_post(m))
return(0);
break;
if (NULL == nn)
- if ( ! man_pmsg(m, line, ppos, MANDOCERR_NOSCOPE))
- return(0);
+ man_pmsg(m, line, ppos, MANDOCERR_NOSCOPE);
if ( ! rew_scope(MAN_BODY, m, ntok))
return(0);
int
blk_exp(MACRO_PROT_ARGS)
{
- int w, la;
+ int la;
char *p;
/*
for (;;) {
la = *pos;
- w = man_args(m, line, pos, buf, &p);
-
- if (-1 == w)
- return(0);
- if (0 == w)
+ if ( ! man_args(m, line, pos, buf, &p))
break;
-
if ( ! man_word_alloc(m, line, la, p))
return(0);
}
int
blk_imp(MACRO_PROT_ARGS)
{
- int w, la;
+ int la;
char *p;
struct man_node *n;
for (;;) {
la = *pos;
- w = man_args(m, line, pos, buf, &p);
-
- if (-1 == w)
- return(0);
- if (0 == w)
+ if ( ! man_args(m, line, pos, buf, &p))
break;
-
if ( ! man_word_alloc(m, line, la, p))
return(0);
}
int
in_line_eoln(MACRO_PROT_ARGS)
{
- int w, la;
+ int la;
char *p;
struct man_node *n;
for (;;) {
la = *pos;
- w = man_args(m, line, pos, buf, &p);
-
- if (-1 == w)
- return(0);
- if (0 == w)
+ if ( ! man_args(m, line, pos, buf, &p))
break;
if ( ! man_word_alloc(m, line, la, p))
return(0);
return(man_unscope(m, m->first, MANDOCERR_SCOPEEXIT));
}
+static int
+man_args(struct man *m, int line, int *pos, char *buf, char **v)
+{
+ char *start;
+
+ assert(*pos);
+ *v = start = buf + *pos;
+ assert(' ' != *start);
+
+ if ('\0' == *start)
+ return(0);
+
+ *v = mandoc_getarg(m->parse, v, line, pos);
+ return(1);
+}