-/* $Id: man.c,v 1.47 2010/01/01 17:14:27 kristaps Exp $ */
+/* $Id: man.c,v 1.51 2010/03/22 14:03:03 kristaps Exp $ */
/*
* Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
*
"invalid manual section", /* WMSEC */
"invalid date format", /* WDATE */
"scope of prior line violated", /* WLNSCOPE */
+ "over-zealous prior line scope violation", /* WLNSCOPE2 */
"trailing whitespace", /* WTSPACE */
"unterminated quoted parameter", /* WTQUOTE */
"document has no body", /* WNODATA */
man_ptext(struct man *m, int line, char *buf)
{
int i, j;
+ char sv;
/* Literal free-form text whitespace is preserved. */
for (i = 0; ' ' == buf[i]; i++)
/* Skip leading whitespace. */ ;
- if (0 == buf[i]) {
+
+ if ('\0' == buf[i]) {
+ /* Trailing whitespace? */
+ if (i && ' ' == buf[i - 1])
+ if ( ! man_pwarn(m, line, i - 1, WTSPACE))
+ return(0);
if ( ! pstring(m, line, 0, &buf[i], 0))
return(0);
goto descope;
if (i && ' ' == buf[i] && '\\' == buf[i - 1])
continue;
- buf[i++] = 0;
+ sv = buf[i];
+ buf[i++] = '\0';
+
if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
return(0);
+ /* Trailing whitespace? Check at overwritten byte. */
+
+ if (' ' == sv && '\0' == buf[i])
+ if ( ! man_pwarn(m, line, i - 1, WTSPACE))
+ return(0);
+
for ( ; ' ' == buf[i]; i++)
/* Skip trailing whitespace. */ ;
j = i;
- if (0 == buf[i])
+
+ /* Trailing whitespace? */
+
+ if (' ' == buf[i - 1] && '\0' == buf[i])
+ if ( ! man_pwarn(m, line, i - 1, WTSPACE))
+ return(0);
+
+ if ('\0' == buf[i])
break;
}
i++;
while (buf[i] && ' ' == buf[i])
i++;
- if (0 == buf[i])
+ if ('\0' == buf[i])
goto out;
}
/* Copy the first word into a nil-terminated buffer. */
for (j = 0; j < 4; j++, i++) {
- if (0 == (mac[j] = buf[i]))
+ if ('\0' == (mac[j] = buf[i]))
break;
else if (' ' == buf[i])
break;
while (buf[i] && ' ' == buf[i])
i++;
- /* Remove prior ELINE macro, if applicable. */
+ /* Trailing whitespace? */
+
+ if ('\0' == buf[i] && ' ' == buf[i - 1])
+ if ( ! man_pwarn(m, ln, i - 1, WTSPACE))
+ goto err;
+
+ /*
+ * Remove prior ELINE macro, as it's being clobbering by a new
+ * macro. Note that NSCOPED macros do not close out ELINE
+ * macros---they don't print text---so we let those slip by.
+ */
+
+ if ( ! (MAN_NSCOPED & man_macros[c].flags) &&
+ m->flags & MAN_ELINE) {
+ assert(MAN_TEXT != m->last->type);
+
+ /*
+ * This occurs in the following construction:
+ * .B
+ * .br
+ * .B
+ * .br
+ * I hate man macros.
+ * Flat-out disallow this madness.
+ */
+ if (MAN_NSCOPED & man_macros[m->last->tok].flags)
+ return(man_perr(m, ln, ppos, WLNSCOPE));
- if (m->flags & MAN_ELINE) {
n = m->last;
+
+ assert(n);
assert(NULL == n->child);
assert(0 == n->nchild);
+
if ( ! man_nwarn(m, n, WLNSCOPE))
return(0);
- if (n->prev) {
- assert(n != n->parent->child);
- assert(n == n->prev->next);
- n->prev->next = NULL;
- m->last = n->prev;
- m->next = MAN_NEXT_SIBLING;
- } else {
- assert(n == n->parent->child);
- n->parent->child = NULL;
- m->last = n->parent;
- m->next = MAN_NEXT_CHILD;
- }
-
+ man_node_unlink(m, n);
man_node_free(n);
m->flags &= ~MAN_ELINE;
}
goto err;
out:
- if ( ! (MAN_BLINE & fl))
+ /*
+ * We weren't in a block-line scope when entering the
+ * above-parsed macro, so return.
+ */
+
+ if ( ! (MAN_BLINE & fl)) {
+ m->flags &= ~MAN_ILINE;
return(1);
+ }
+
+ /*
+ * If we're in a block scope, then allow this macro to slip by
+ * without closing scope around it.
+ */
+
+ if (MAN_ILINE & m->flags) {
+ m->flags &= ~MAN_ILINE;
+ return(1);
+ }
/*
* If we've opened a new next-line element scope, then return
return(man_vwarn(m, line, pos, p));
}
+
+
+void
+man_node_unlink(struct man *m, struct man_node *n)
+{
+
+ if (n->prev) {
+ n->prev->next = n->next;
+ if (m->last == n) {
+ assert(NULL == n->next);
+ m->last = n->prev;
+ m->next = MAN_NEXT_SIBLING;
+ }
+ } else {
+ n->parent->child = n->next;
+ if (m->last == n) {
+ assert(NULL == n->next);
+ m->last = n->parent;
+ m->next = MAN_NEXT_CHILD;
+ }
+ }
+
+ if (n->next)
+ n->next->prev = n->prev;
+}