+ /* If it is escaped, skip it. */
+
+ for (cp = stesc - 1; cp >= start; cp--)
+ if (*cp != '\\')
+ break;
+
+ if ((stesc - cp) % 2 == 0) {
+ stesc = (char *)cp;
+ continue;
+ }
+
+ /* Decide whether to expand or to check only. */
+
+ term = '\0';
+ cp = stesc + 1;
+ switch (*cp) {
+ case '*':
+ res = NULL;
+ break;
+ case 'B':
+ case 'w':
+ term = cp[1];
+ /* FALLTHROUGH */
+ case 'n':
+ res = ubuf;
+ break;
+ default:
+ esc = mandoc_escape(&cp, &stnam, &inaml);
+ if (esc == ESCAPE_ERROR ||
+ (esc == ESCAPE_SPECIAL &&
+ mchars_spec2cp(stnam, inaml) < 0))
+ mandoc_vmsg(MANDOCERR_ESC_BAD,
+ r->parse, ln, (int)(stesc - buf->buf),
+ "%.*s", (int)(cp - stesc), stesc);
+ continue;
+ }
+
+ if (EXPAND_LIMIT < ++expand_count) {
+ mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
+ ln, (int)(stesc - buf->buf), NULL);
+ return ROFF_IGN;
+ }
+
+ /*
+ * The third character decides the length
+ * of the name of the string or register.
+ * Save a pointer to the name.
+ */
+
+ if (term == '\0') {
+ switch (*++cp) {
+ case '\0':
+ maxl = 0;
+ break;
+ case '(':
+ cp++;
+ maxl = 2;
+ break;
+ case '[':
+ cp++;
+ term = ']';
+ maxl = 0;
+ break;
+ default:
+ maxl = 1;
+ break;
+ }
+ } else {
+ cp += 2;
+ maxl = 0;
+ }
+ stnam = cp;
+
+ /* Advance to the end of the name. */
+
+ naml = 0;
+ arg_complete = 1;
+ while (maxl == 0 || naml < maxl) {
+ if (*cp == '\0') {
+ mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
+ ln, (int)(stesc - buf->buf), stesc);
+ arg_complete = 0;
+ break;
+ }
+ if (maxl == 0 && *cp == term) {
+ cp++;
+ break;
+ }
+ if (*cp++ != '\\' || stesc[1] != 'w') {
+ naml++;
+ continue;
+ }
+ switch (mandoc_escape(&cp, NULL, NULL)) {
+ case ESCAPE_SPECIAL:
+ case ESCAPE_UNICODE:
+ case ESCAPE_NUMBERED:
+ case ESCAPE_OVERSTRIKE:
+ naml++;
+ break;
+ default:
+ break;
+ }
+ }
+
+ /*
+ * Retrieve the replacement string; if it is
+ * undefined, resume searching for escapes.
+ */
+
+ switch (stesc[1]) {
+ case '*':
+ if (arg_complete)
+ res = roff_getstrn(r, stnam, naml);
+ break;
+ case 'B':
+ npos = 0;
+ ubuf[0] = arg_complete &&
+ roff_evalnum(r, ln, stnam, &npos,
+ NULL, ROFFNUM_SCALE) &&
+ stnam + npos + 1 == cp ? '1' : '0';
+ ubuf[1] = '\0';
+ break;
+ case 'n':
+ if (arg_complete)
+ (void)snprintf(ubuf, sizeof(ubuf), "%d",
+ roff_getregn(r, stnam, naml));
+ else
+ ubuf[0] = '\0';
+ break;
+ case 'w':
+ /* use even incomplete args */
+ (void)snprintf(ubuf, sizeof(ubuf), "%d",
+ 24 * (int)naml);
+ break;
+ }
+
+ if (res == NULL) {
+ mandoc_vmsg(MANDOCERR_STR_UNDEF,
+ r->parse, ln, (int)(stesc - buf->buf),
+ "%.*s", (int)naml, stnam);
+ res = "";
+ } else if (buf->sz + strlen(res) > SHRT_MAX) {
+ mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
+ ln, (int)(stesc - buf->buf), NULL);
+ return ROFF_IGN;
+ }
+
+ /* Replace the escape sequence by the string. */
+
+ *stesc = '\0';
+ buf->sz = mandoc_asprintf(&nbuf, "%s%s%s",
+ buf->buf, res, cp) + 1;
+
+ /* Prepare for the next replacement. */
+
+ start = nbuf + pos;
+ stesc = nbuf + (stesc - buf->buf) + strlen(res);
+ free(buf->buf);
+ buf->buf = nbuf;
+ }
+ return ROFF_CONT;
+}
+
+/*
+ * Process text streams.
+ */
+static enum rofferr
+roff_parsetext(struct buf *buf, int pos, int *offs)
+{
+ size_t sz;
+ const char *start;
+ char *p;
+ int isz;
+ enum mandoc_esc esc;
+
+ /* Spring the input line trap. */
+
+ if (roffit_lines == 1) {
+ isz = mandoc_asprintf(&p, "%s\n.%s", buf->buf, roffit_macro);
+ free(buf->buf);
+ buf->buf = p;
+ buf->sz = isz + 1;
+ *offs = 0;
+ free(roffit_macro);
+ roffit_lines = 0;
+ return ROFF_REPARSE;
+ } else if (roffit_lines > 1)
+ --roffit_lines;
+
+ /* Convert all breakable hyphens into ASCII_HYPH. */
+
+ start = p = buf->buf + pos;
+
+ while (*p != '\0') {
+ sz = strcspn(p, "-\\");
+ p += sz;
+
+ if (*p == '\0')
+ break;
+
+ if (*p == '\\') {
+ /* Skip over escapes. */
+ p++;
+ esc = mandoc_escape((const char **)&p, NULL, NULL);
+ if (esc == ESCAPE_ERROR)
+ break;
+ while (*p == '-')
+ p++;
+ continue;
+ } else if (p == start) {
+ p++;
+ continue;
+ }
+
+ if (isalpha((unsigned char)p[-1]) &&
+ isalpha((unsigned char)p[1]))
+ *p = ASCII_HYPH;
+ p++;
+ }
+ return ROFF_CONT;
+}
+
+enum rofferr
+roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs)
+{
+ enum rofft t;
+ enum rofferr e;
+ int pos; /* parse point */
+ int spos; /* saved parse point for messages */
+ int ppos; /* original offset in buf->buf */
+ int ctl; /* macro line (boolean) */
+
+ ppos = pos = *offs;
+
+ /* Handle in-line equation delimiters. */
+
+ if (r->tbl == NULL &&
+ r->last_eqn != NULL && r->last_eqn->delim &&
+ (r->eqn == NULL || r->eqn_inline)) {
+ e = roff_eqndelim(r, buf, pos);
+ if (e == ROFF_REPARSE)
+ return e;
+ assert(e == ROFF_CONT);
+ }
+
+ /* Expand some escape sequences. */
+
+ e = roff_res(r, buf, ln, pos);
+ if (e == ROFF_IGN)
+ return e;
+ assert(e == ROFF_CONT);
+
+ ctl = roff_getcontrol(r, buf->buf, &pos);
+
+ /*
+ * First, if a scope is open and we're not a macro, pass the
+ * text through the macro's filter.
+ * Equations process all content themselves.
+ * Tables process almost all content themselves, but we want
+ * to warn about macros before passing it there.
+ */
+
+ if (r->last != NULL && ! ctl) {
+ t = r->last->tok;
+ assert(roffs[t].text);
+ e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
+ assert(e == ROFF_IGN || e == ROFF_CONT);
+ if (e != ROFF_CONT)
+ return e;
+ }
+ if (r->eqn != NULL)
+ return eqn_read(&r->eqn, ln, buf->buf, ppos, offs);
+ if (r->tbl != NULL && ( ! ctl || buf->buf[pos] == '\0'))
+ return tbl_read(r->tbl, ln, buf->buf, ppos);
+ if ( ! ctl)
+ return roff_parsetext(buf, pos, offs);
+
+ /* Skip empty request lines. */
+
+ if (buf->buf[pos] == '"') {
+ mandoc_msg(MANDOCERR_COMMENT_BAD, r->parse,
+ ln, pos, NULL);
+ return ROFF_IGN;
+ } else if (buf->buf[pos] == '\0')
+ return ROFF_IGN;
+
+ /*
+ * If a scope is open, go to the child handler for that macro,
+ * as it may want to preprocess before doing anything with it.
+ * Don't do so if an equation is open.
+ */
+
+ if (r->last) {
+ t = r->last->tok;
+ assert(roffs[t].sub);
+ return (*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs);
+ }
+
+ /* No scope is open. This is a new request or macro. */
+
+ spos = pos;
+ t = roff_parse(r, buf->buf, &pos, ln, ppos);
+
+ /* Tables ignore most macros. */
+
+ if (r->tbl != NULL && (t == ROFF_MAX || t == ROFF_TS)) {
+ mandoc_msg(MANDOCERR_TBLMACRO, r->parse,
+ ln, pos, buf->buf + spos);
+ if (t == ROFF_TS)
+ return ROFF_IGN;
+ while (buf->buf[pos] != '\0' && buf->buf[pos] != ' ')
+ pos++;
+ while (buf->buf[pos] != '\0' && buf->buf[pos] == ' ')
+ pos++;
+ return tbl_read(r->tbl, ln, buf->buf, pos);
+ }
+
+ /*
+ * This is neither a roff request nor a user-defined macro.
+ * Let the standard macro set parsers handle it.
+ */
+
+ if (t == ROFF_MAX)
+ return ROFF_CONT;
+
+ /* Execute a roff request or a user defined macro. */
+
+ assert(roffs[t].proc);
+ return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
+}
+
+void
+roff_endparse(struct roff *r)
+{
+
+ if (r->last)
+ mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
+ r->last->line, r->last->col,
+ roffs[r->last->tok].name);
+
+ if (r->eqn) {
+ mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
+ r->eqn->eqn.ln, r->eqn->eqn.pos, "EQ");
+ eqn_end(&r->eqn);
+ }
+
+ if (r->tbl) {
+ mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
+ r->tbl->line, r->tbl->pos, "TS");
+ tbl_end(&r->tbl);
+ }
+}
+
+/*
+ * Parse a roff node's type from the input buffer. This must be in the
+ * form of ".foo xxx" in the usual way.
+ */
+static enum rofft
+roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
+{
+ char *cp;
+ const char *mac;
+ size_t maclen;
+ enum rofft t;
+
+ cp = buf + *pos;
+
+ if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
+ return ROFF_MAX;
+
+ mac = cp;
+ maclen = roff_getname(r, &cp, ln, ppos);
+
+ t = (r->current_string = roff_getstrn(r, mac, maclen))
+ ? ROFF_USERDEF : roffhash_find(mac, maclen);
+
+ if (ROFF_MAX != t)
+ *pos = cp - buf;
+
+ return t;
+}
+
+/* --- handling of request blocks ----------------------------------------- */
+
+static enum rofferr
+roff_cblock(ROFF_ARGS)
+{
+
+ /*
+ * A block-close `..' should only be invoked as a child of an
+ * ignore macro, otherwise raise a warning and just ignore it.
+ */
+
+ if (r->last == NULL) {
+ mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
+ ln, ppos, "..");
+ return ROFF_IGN;
+ }