/* ARGSUSED */ static int phrase_ta(MACRO_PROT_ARGS) { struct mdoc_node *n; int la; enum mdoct ntok; enum margserr ac; char *p; /* Make sure we are in a column list or ignore this macro. */ n = mdoc->last; while (NULL != n && MDOC_Bl != n->tok) n = n->parent; if (NULL == n || LIST_column != n->norm->Bl.type) { mdoc_pmsg(mdoc, line, ppos, MANDOCERR_STRAYTA); return(1); } /* Advance to the next column. */ if ( ! rew_sub(MDOC_BODY, mdoc, MDOC_It, line, ppos)) return(0); if ( ! mdoc_body_alloc(mdoc, line, ppos, MDOC_It)) return(0); for (;;) { la = *pos; ac = mdoc_zargs(mdoc, line, pos, buf, &p); if (ARGS_ERROR == ac) return(0); if (ARGS_EOLN == ac) break; ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p); if (MDOC_MAX == ntok) { if ( ! dword(mdoc, line, la, p, DELIM_MAX)) return(0); continue; } if ( ! mdoc_macro(mdoc, ntok, line, la, pos, buf)) return(0); return(append_delims(mdoc, line, pos, buf)); } return(1); }
/* ARGSUSED */ static int phrase_ta(MACRO_PROT_ARGS) { int la; enum mdoct ntok; enum margserr ac; char *p; /* * FIXME: this is overly restrictive: if the `Ta' is unexpected, * it should simply error out with ARGSLOST. */ if ( ! rew_sub(MDOC_BODY, m, MDOC_It, line, ppos)) return(0); if ( ! mdoc_body_alloc(m, line, ppos, MDOC_It)) return(0); for (;;) { la = *pos; ac = mdoc_zargs(m, line, pos, buf, &p); if (ARGS_ERROR == ac) return(0); if (ARGS_EOLN == ac) break; ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup_raw(p); if (MDOC_MAX == ntok) { if ( ! dword(m, line, la, p, DELIM_MAX)) return(0); continue; } if ( ! mdoc_macro(m, ntok, line, la, pos, buf)) return(0); return(append_delims(m, line, pos, buf)); } return(1); }
static int blk_full(MACRO_PROT_ARGS) { int la, nl, nparsed; struct mdoc_arg *arg; struct mdoc_node *head; /* save of head macro */ struct mdoc_node *body; /* save of body macro */ struct mdoc_node *n; enum mdoc_type mtt; enum mdoct ntok; enum margserr ac, lac; enum margverr av; char *p; nl = MDOC_NEWLINE & m->flags; /* Close out prior implicit scope. */ if ( ! (MDOC_EXPLICIT & mdoc_macros[tok].flags)) { if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos)) return(0); if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos)) return(0); } /* * This routine accommodates implicitly- and explicitly-scoped * macro openings. Implicit ones first close out prior scope * (seen above). Delay opening the head until necessary to * allow leading punctuation to print. Special consideration * for `It -column', which has phrase-part syntax instead of * regular child nodes. */ for (arg = NULL;; ) { la = *pos; av = mdoc_argv(m, line, tok, &arg, pos, buf); if (ARGV_WORD == av) { *pos = la; break; } if (ARGV_EOLN == av) break; if (ARGV_ARG == av) continue; mdoc_argv_free(arg); return(0); } if ( ! mdoc_block_alloc(m, line, ppos, tok, arg)) return(0); head = body = NULL; /* * Exception: Heads of `It' macros in `-diag' lists are not * parsed, even though `It' macros in general are parsed. */ nparsed = MDOC_It == tok && MDOC_Bl == m->last->parent->tok && LIST_diag == m->last->parent->norm->Bl.type; /* * The `Nd' macro has all arguments in its body: it's a hybrid * of block partial-explicit and full-implicit. Stupid. */ if (MDOC_Nd == tok) { if ( ! mdoc_head_alloc(m, line, ppos, tok)) return(0); head = m->last; if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) return(0); if ( ! mdoc_body_alloc(m, line, ppos, tok)) return(0); body = m->last; } ac = ARGS_ERROR; for ( ; ; ) { la = *pos; /* Initialise last-phrase-type with ARGS_PEND. */ lac = ARGS_ERROR == ac ? ARGS_PEND : ac; ac = mdoc_args(m, line, pos, buf, tok, &p); if (ARGS_PUNCT == ac) break; if (ARGS_ERROR == ac) return(0); if (ARGS_EOLN == ac) { if (ARGS_PPHRASE != lac && ARGS_PHRASE != lac) break; /* * This is necessary: if the last token on a * line is a `Ta' or tab, then we'll get * ARGS_EOLN, so we must be smart enough to * reopen our scope if the last parse was a * phrase or partial phrase. */ if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos)) return(0); if ( ! mdoc_body_alloc(m, line, ppos, tok)) return(0); body = m->last; break; } /* * Emit leading punctuation (i.e., punctuation before * the MDOC_HEAD) for non-phrase types. */ if (NULL == head && ARGS_PEND != ac && ARGS_PHRASE != ac && ARGS_PPHRASE != ac && ARGS_QWORD != ac && DELIM_OPEN == mdoc_isdelim(p)) { if ( ! dword(m, line, la, p, DELIM_OPEN)) return(0); continue; } /* Open a head if one hasn't been opened. */ if (NULL == head) { if ( ! mdoc_head_alloc(m, line, ppos, tok)) return(0); head = m->last; } if (ARGS_PHRASE == ac || ARGS_PEND == ac || ARGS_PPHRASE == ac) { /* * If we haven't opened a body yet, rewind the * head; if we have, rewind that instead. */ mtt = body ? MDOC_BODY : MDOC_HEAD; if ( ! rew_sub(mtt, m, tok, line, ppos)) return(0); /* Then allocate our body context. */ if ( ! mdoc_body_alloc(m, line, ppos, tok)) return(0); body = m->last; /* * Process phrases: set whether we're in a * partial-phrase (this effects line handling) * then call down into the phrase parser. */ if (ARGS_PPHRASE == ac) m->flags |= MDOC_PPHRASE; if (ARGS_PEND == ac && ARGS_PPHRASE == lac) m->flags |= MDOC_PPHRASE; if ( ! phrase(m, line, la, buf)) return(0); m->flags &= ~MDOC_PPHRASE; continue; } ntok = nparsed || ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p); if (MDOC_MAX == ntok) { if ( ! dword(m, line, la, p, DELIM_MAX)) return(0); continue; } if ( ! mdoc_macro(m, ntok, line, la, pos, buf)) return(0); break; } if (NULL == head) { if ( ! mdoc_head_alloc(m, line, ppos, tok)) return(0); head = m->last; } if (nl && ! append_delims(m, line, pos, buf)) return(0); /* If we've already opened our body, exit now. */ if (NULL != body) goto out; /* * If there is an open (i.e., unvalidated) sub-block requiring * explicit close-out, postpone switching the current block from * head to body until the rew_sub() call closing out that * sub-block. */ for (n = m->last; n && n != head; n = n->parent) { if (MDOC_BLOCK == n->type && MDOC_EXPLICIT & mdoc_macros[n->tok].flags && ! (MDOC_VALID & n->flags)) { n->pending = head; return(1); } } /* Close out scopes to remain in a consistent state. */ if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) return(0); if ( ! mdoc_body_alloc(m, line, ppos, tok)) return(0); out: if ( ! (MDOC_FREECOL & m->flags)) return(1); if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos)) return(0); if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos)) return(0); m->flags &= ~MDOC_FREECOL; return(1); }
/* * Close out block partial/full explicit. */ static int blk_exp_close(MACRO_PROT_ARGS) { struct mdoc_node *body; /* Our own body. */ struct mdoc_node *later; /* A sub-block starting later. */ struct mdoc_node *n; /* For searching backwards. */ int j, lastarg, maxargs, flushed, nl; enum margserr ac; enum mdoct atok, ntok; char *p; nl = MDOC_NEWLINE & m->flags; switch (tok) { case (MDOC_Ec): maxargs = 1; break; default: maxargs = 0; break; } /* * Search backwards for beginnings of blocks, * both of our own and of pending sub-blocks. */ atok = rew_alt(tok); body = later = NULL; for (n = m->last; n; n = n->parent) { if (MDOC_VALID & n->flags) continue; /* Remember the start of our own body. */ if (MDOC_BODY == n->type && atok == n->tok) { if (ENDBODY_NOT == n->end) body = n; continue; } if (MDOC_BLOCK != n->type || MDOC_Nm == n->tok) continue; if (atok == n->tok) { assert(body); /* * Found the start of our own block. * When there is no pending sub block, * just proceed to closing out. */ if (NULL == later) break; /* * When there is a pending sub block, * postpone closing out the current block * until the rew_sub() closing out the sub-block. */ make_pending(later, tok, m, line, ppos); /* * Mark the place where the formatting - but not * the scope - of the current block ends. */ if ( ! mdoc_endbody_alloc(m, line, ppos, atok, body, ENDBODY_SPACE)) return(0); break; } /* * When finding an open sub block, remember the last * open explicit block, or, in case there are only * implicit ones, the first open implicit block. */ if (later && MDOC_EXPLICIT & mdoc_macros[later->tok].flags) continue; if (MDOC_CALLABLE & mdoc_macros[n->tok].flags) later = n; } if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) { /* FIXME: do this in validate */ if (buf[*pos]) mdoc_pmsg(m, line, ppos, MANDOCERR_ARGSLOST); if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos)) return(0); return(rew_sub(MDOC_BLOCK, m, tok, line, ppos)); } if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos)) return(0); if (NULL == later && maxargs > 0) if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok))) return(0); for (flushed = j = 0; ; j++) { lastarg = *pos; if (j == maxargs && ! flushed) { if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos)) return(0); flushed = 1; } ac = mdoc_args(m, line, pos, buf, tok, &p); if (ARGS_ERROR == ac) return(0); if (ARGS_PUNCT == ac) break; if (ARGS_EOLN == ac) break; ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p); if (MDOC_MAX == ntok) { if ( ! dword(m, line, lastarg, p, DELIM_MAX)) return(0); continue; } if ( ! flushed) { if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos)) return(0); flushed = 1; } if ( ! mdoc_macro(m, ntok, line, lastarg, pos, buf)) return(0); break; } if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos)) return(0); if ( ! nl) return(1); return(append_delims(m, line, pos, buf)); }
static int in_line_eoln(MACRO_PROT_ARGS) { int la; enum margserr ac; enum margverr av; struct mdoc_arg *arg; char *p; enum mdoct ntok; assert( ! (MDOC_PARSED & mdoc_macros[tok].flags)); if (tok == MDOC_Pp) rew_sub(MDOC_BLOCK, m, MDOC_Nm, line, ppos); /* Parse macro arguments. */ for (arg = NULL; ; ) { la = *pos; av = mdoc_argv(m, line, tok, &arg, pos, buf); if (ARGV_WORD == av) { *pos = la; break; } if (ARGV_EOLN == av) break; if (ARGV_ARG == av) continue; mdoc_argv_free(arg); return(0); } /* Open element scope. */ if ( ! mdoc_elem_alloc(m, line, ppos, tok, arg)) return(0); /* Parse argument terms. */ for (;;) { la = *pos; ac = mdoc_args(m, line, pos, buf, tok, &p); if (ARGS_ERROR == ac) return(0); if (ARGS_EOLN == ac) break; ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p); if (MDOC_MAX == ntok) { if ( ! dword(m, line, la, p, DELIM_MAX)) return(0); continue; } if ( ! rew_elem(m, tok)) return(0); return(mdoc_macro(m, ntok, line, la, pos, buf)); } /* Close out (no delimiters). */ return(rew_elem(m, tok)); }
static int blk_part_exp(MACRO_PROT_ARGS) { int la, nl; enum margserr ac; struct mdoc_node *head; /* keep track of head */ struct mdoc_node *body; /* keep track of body */ char *p; enum mdoct ntok; nl = MDOC_NEWLINE & m->flags; /* * The opening of an explicit macro having zero or more leading * punctuation nodes; a head with optional single element (the * case of `Eo'); and a body that may be empty. */ if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL)) return(0); for (head = body = NULL; ; ) { la = *pos; ac = mdoc_args(m, line, pos, buf, tok, &p); if (ARGS_ERROR == ac) return(0); if (ARGS_PUNCT == ac) break; if (ARGS_EOLN == ac) break; /* Flush out leading punctuation. */ if (NULL == head && ARGS_QWORD != ac && DELIM_OPEN == mdoc_isdelim(p)) { assert(NULL == body); if ( ! dword(m, line, la, p, DELIM_OPEN)) return(0); continue; } if (NULL == head) { assert(NULL == body); if ( ! mdoc_head_alloc(m, line, ppos, tok)) return(0); head = m->last; } /* * `Eo' gobbles any data into the head, but most other * macros just immediately close out and begin the body. */ if (NULL == body) { assert(head); /* No check whether it's a macro! */ if (MDOC_Eo == tok) if ( ! dword(m, line, la, p, DELIM_MAX)) return(0); if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) return(0); if ( ! mdoc_body_alloc(m, line, ppos, tok)) return(0); body = m->last; if (MDOC_Eo == tok) continue; } assert(NULL != head && NULL != body); ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p); if (MDOC_MAX == ntok) { if ( ! dword(m, line, la, p, DELIM_MAX)) return(0); continue; } if ( ! mdoc_macro(m, ntok, line, la, pos, buf)) return(0); break; } /* Clean-up to leave in a consistent state. */ if (NULL == head) if ( ! mdoc_head_alloc(m, line, ppos, tok)) return(0); if (NULL == body) { if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) return(0); if ( ! mdoc_body_alloc(m, line, ppos, tok)) return(0); } /* Standard appending of delimiters. */ if ( ! nl) return(1); return(append_delims(m, line, pos, buf)); }
static int blk_part_imp(MACRO_PROT_ARGS) { int la, nl; enum mdoct ntok; enum margserr ac; char *p; struct mdoc_node *blk; /* saved block context */ struct mdoc_node *body; /* saved body context */ struct mdoc_node *n; nl = MDOC_NEWLINE & m->flags; /* * A macro that spans to the end of the line. This is generally * (but not necessarily) called as the first macro. The block * has a head as the immediate child, which is always empty, * followed by zero or more opening punctuation nodes, then the * body (which may be empty, depending on the macro), then zero * or more closing punctuation nodes. */ if ( ! mdoc_block_alloc(m, line, ppos, tok, NULL)) return(0); blk = m->last; if ( ! mdoc_head_alloc(m, line, ppos, tok)) return(0); if ( ! rew_sub(MDOC_HEAD, m, tok, line, ppos)) return(0); /* * Open the body scope "on-demand", that is, after we've * processed all our the leading delimiters (open parenthesis, * etc.). */ for (body = NULL; ; ) { la = *pos; ac = mdoc_args(m, line, pos, buf, tok, &p); if (ARGS_ERROR == ac) return(0); if (ARGS_EOLN == ac) break; if (ARGS_PUNCT == ac) break; if (NULL == body && ARGS_QWORD != ac && DELIM_OPEN == mdoc_isdelim(p)) { if ( ! dword(m, line, la, p, DELIM_OPEN)) return(0); continue; } if (NULL == body) { if ( ! mdoc_body_alloc(m, line, ppos, tok)) return(0); body = m->last; } ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p); if (MDOC_MAX == ntok) { if ( ! dword(m, line, la, p, DELIM_MAX)) return(0); continue; } if ( ! mdoc_macro(m, ntok, line, la, pos, buf)) return(0); break; } /* Clean-ups to leave in a consistent state. */ if (NULL == body) { if ( ! mdoc_body_alloc(m, line, ppos, tok)) return(0); body = m->last; } for (n = body->child; n && n->next; n = n->next) /* Do nothing. */ ; /* * End of sentence spacing: if the last node is a text node and * has a trailing period, then mark it as being end-of-sentence. */ if (n && MDOC_TEXT == n->type && n->string) if (mandoc_eos(n->string, strlen(n->string), 1)) n->flags |= MDOC_EOS; /* Up-propagate the end-of-space flag. */ if (n && (MDOC_EOS & n->flags)) { body->flags |= MDOC_EOS; body->parent->flags |= MDOC_EOS; } /* * If there is an open sub-block requiring explicit close-out, * postpone closing out the current block * until the rew_sub() call closing out the sub-block. */ for (n = m->last; n && n != body && n != blk->parent; n = n->parent) { if (MDOC_BLOCK == n->type && MDOC_EXPLICIT & mdoc_macros[n->tok].flags && ! (MDOC_VALID & n->flags)) { make_pending(n, tok, m, line, ppos); if ( ! mdoc_endbody_alloc(m, line, ppos, tok, body, ENDBODY_NOSPACE)) return(0); return(1); } } /* * If we can't rewind to our body, then our scope has already * been closed by another macro (like `Oc' closing `Op'). This * is ugly behaviour nodding its head to OpenBSD's overwhelming * crufty use of `Op' breakage. */ if (n != body) mandoc_vmsg(MANDOCERR_SCOPENEST, m->parse, line, ppos, "%s broken", mdoc_macronames[tok]); if (n && ! rew_sub(MDOC_BODY, m, tok, line, ppos)) return(0); /* Standard appending of delimiters. */ if (nl && ! append_delims(m, line, pos, buf)) return(0); /* Rewind scope, if applicable. */ if (n && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos)) return(0); return(1); }
/* * Close out block partial/full explicit. */ static int blk_exp_close(MACRO_PROT_ARGS) { int j, lastarg, maxargs, flushed, nl; enum margserr ac; enum mdoct ntok; char *p; nl = MDOC_NEWLINE & m->flags; switch (tok) { case (MDOC_Ec): maxargs = 1; break; default: maxargs = 0; break; } if ( ! (MDOC_CALLABLE & mdoc_macros[tok].flags)) { /* FIXME: do this in validate */ if (buf[*pos]) if ( ! mdoc_pmsg(m, line, ppos, MANDOCERR_ARGSLOST)) return(0); if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos)) return(0); return(rew_sub(MDOC_BLOCK, m, tok, line, ppos)); } if ( ! rew_sub(MDOC_BODY, m, tok, line, ppos)) return(0); if (maxargs > 0) if ( ! mdoc_tail_alloc(m, line, ppos, rew_alt(tok))) return(0); for (flushed = j = 0; ; j++) { lastarg = *pos; if (j == maxargs && ! flushed) { if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos)) return(0); flushed = 1; } ac = mdoc_args(m, line, pos, buf, tok, &p); if (ARGS_ERROR == ac) return(0); if (ARGS_PUNCT == ac) break; if (ARGS_EOLN == ac) break; ntok = ARGS_QWORD == ac ? MDOC_MAX : lookup(tok, p); if (MDOC_MAX == ntok) { if ( ! mdoc_word_alloc(m, line, lastarg, p)) return(0); continue; } if ( ! flushed) { if ( ! rew_sub(MDOC_BLOCK, m, tok, line, ppos)) return(0); flushed = 1; } if ( ! mdoc_macro(m, ntok, line, lastarg, pos, buf)) return(0); break; } if ( ! flushed && ! rew_sub(MDOC_BLOCK, m, tok, line, ppos)) return(0); if ( ! nl) return(1); return(append_delims(m, line, pos, buf)); }