roff.c revision 260e9a87725c090ba5835b1f9f0b62fa2f96036f
/* $Id: roff.c,v 1.263 2015/02/21 14:46:58 schwarze Exp $ */
/*
* Copyright (c) 2010, 2011, 2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2010-2015 Ingo Schwarze <schwarze@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "config.h"
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mandoc.h"
#include "mandoc_aux.h"
#include "libmandoc.h"
#include "libroff.h"
/* Maximum number of nested if-else conditionals. */
#define RSTACK_MAX 128
/* Maximum number of string expansions per line, to break infinite loops. */
#define EXPAND_LIMIT 1000
enum rofft {
/* MAN_br, MDOC_br */
/* MAN_fi; ignored in mdoc(7) */
/* MAN_ft; ignored in mdoc(7) */
/* MAN_in; ignored in mdoc(7) */
/* MAN_ll, MDOC_ll */
/* MAN_nf; ignored in mdoc(7) */
/* MAN_sp, MDOC_sp */
};
/*
* An incredibly-simple string buffer.
*/
struct roffstr {
char *p; /* nil-terminated buffer */
};
/*
* A key-value roffstr pair as part of a singly-linked list.
*/
struct roffkv {
};
/*
* A single number register as part of a singly-linked list.
*/
struct roffreg {
int val;
};
struct roff {
int *rstack; /* stack of inverted `ie' values */
const char *current_string; /* value of last called user macro */
int eqn_inline; /* current equation is inline */
int options; /* parse options */
int rstacksz; /* current size limit of rstack */
int rstackpos; /* position in rstack */
int format; /* current file in mdoc or man format */
char control; /* control character */
};
struct roffnode {
int line; /* parse line */
int col; /* parse col */
char *name; /* node name, e.g. macro name */
char *end; /* end-rules: custom token */
int endspan; /* end-rules: next-line or infty */
int rule; /* current evaluation rule */
};
int ln, /* parse line */ \
int ppos, /* original pos in buffer */ \
int pos, /* current pos in buffer */ \
int *offs /* reset offset of buffer data */
struct roffmac {
const char *name; /* macro name */
int flags;
};
struct predef {
const char *name; /* predefined input name */
const char *str; /* replacement symbol */
};
static void roffhash_init(void);
static void roffnode_cleanscope(struct roff *);
static void roffnode_pop(struct roff *);
const char *, int, int);
static void roff_ccond(struct roff *, int, int);
static int roff_evalcond(struct roff *r, int,
const char *, int *);
static int roff_evalnum(struct roff *, int,
const char *, int *, int *, int);
static int roff_evalpar(struct roff *, int,
const char *, int *, int *, int);
static int roff_evalstrcond(const char *, int *);
static void roff_free1(struct roff *);
static void roff_freereg(struct roffreg *);
static void roff_freestr(struct roffkv *);
static int roff_getnum(const char *, int *, int *, int);
static int roff_getop(const char *, int *, char *);
static int roff_getregn(const struct roff *,
const char *, size_t);
static int roff_getregro(const char *name);
static const char *roff_getstrn(const struct roff *,
const char *, size_t);
int, int);
static void roff_setstr(struct roff *,
const char *, const char *, int);
static void roff_setstrn(struct roffkv **, const char *,
/* See roffhash_find() */
#define ASCII_HI 126
#define ASCII_LO 33
};
/* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
const char *const __mdoc_reserved[] = {
"Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
"Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
"Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
"Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
"Dt", "Dv", "Dx", "D1",
"Ec", "Ed", "Ef", "Ek", "El", "Em",
"En", "Eo", "Er", "Es", "Ev", "Ex",
"Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
"Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
"Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
"Oc", "Oo", "Op", "Os", "Ot", "Ox",
"Pa", "Pc", "Pf", "Po", "Pp", "Pq",
"Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
"Sc", "Sh", "Sm", "So", "Sq",
"Ss", "St", "Sx", "Sy",
"Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
"%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
"%P", "%Q", "%R", "%T", "%U", "%V",
};
/* not currently implemented: BT DE DS ME MT PT SY TQ YS */
const char *const __man_reserved[] = {
"AT", "B", "BI", "BR", "DT",
"EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
"LP", "OP", "P", "PD", "PP",
"R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
"TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
};
/* Array of injected predefined strings. */
#define PREDEFS_MAX 38
#include "predefs.in"
};
/* See roffhash_find() */
static int roffit_lines; /* number of lines to delay */
static char *roffit_macro; /* nil-terminated macro line */
static void
roffhash_init(void)
{
struct roffmac *n;
int buc, i;
for (i = 0; i < (int)ROFF_USERDEF; i++) {
/* Do nothing. */ ;
} else
}
}
/*
* Look up a roff token by its name. Returns ROFF_MAX if no macro by
* the nil-terminated string name could be found.
*/
static enum rofft
roffhash_find(const char *p, size_t s)
{
int buc;
struct roffmac *n;
/*
* libroff has an extremely simple hashtable, for the time
* being, which simply keys on the first character, which must
* be printable, then walks a chain. It works well enough until
* optimised.
*/
return(ROFF_MAX);
return(ROFF_MAX);
for ( ; n; n = n->next)
return(ROFF_MAX);
}
/*
* Pop the current node off of the stack of roff instructions currently
* pending.
*/
static void
roffnode_pop(struct roff *r)
{
struct roffnode *p;
p = r->last;
free(p);
}
/*
* Push a roff node onto the instruction stack. This must later be
* removed with roffnode_pop().
*/
static void
{
struct roffnode *p;
if (name)
r->last = p;
}
static void
roff_free1(struct roff *r)
{
struct eqn_node *e;
int i;
}
eqn_free(e);
}
while (r->last)
roffnode_pop(r);
r->rstacksz = 0;
r->rstackpos = -1;
roff_freereg(r->regtab);
roff_freestr(r->strtab);
roff_freestr(r->xmbtab);
if (r->xtab)
for (i = 0; i < 128; i++)
}
void
roff_reset(struct roff *r)
{
roff_free1(r);
r->control = 0;
}
void
{
roff_free1(r);
free(r);
}
struct roff *
{
struct roff *r;
r->rstackpos = -1;
return(r);
}
/*
* In the current line, expand escape sequences that tend to get
* used in numerical expressions and conditional requests.
* Also check the syntax of the remaining escape sequences.
*/
static enum rofferr
{
const char *start; /* start of the string to process */
char *stesc; /* start of an escape sequence ('\\') */
const char *stnam; /* start of the name, after "[(*" */
const char *cp; /* end of the name, e.g. before ']' */
const char *res; /* the string to be substituted */
char *nbuf; /* new buffer to copy buf->buf to */
int inaml; /* length returned from mandoc_escape() */
int expand_count; /* to avoid infinite loops */
int npos; /* position in numeric expression */
int arg_complete; /* argument not interrupted by eol */
char term; /* character terminating the escape */
expand_count = 0;
/* Search backwards for the next backslash. */
if (*stesc != '\\')
continue;
/* If it is escaped, skip it. */
if (*cp != '\\')
break;
continue;
}
/* Decide whether to expand or to check only. */
term = '\0';
switch (*cp) {
case '*':
break;
case 'B':
/* FALLTHROUGH */
case 'w':
/* FALLTHROUGH */
case 'n':
break;
default:
if (esc == ESCAPE_ERROR ||
(esc == ESCAPE_SPECIAL &&
continue;
}
if (EXPAND_LIMIT < ++expand_count) {
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;
}
/* Advance to the end of the name. */
naml = 0;
arg_complete = 1;
if (*cp == '\0') {
arg_complete = 0;
break;
}
cp++;
break;
}
naml++;
continue;
}
case ESCAPE_SPECIAL:
/* FALLTHROUGH */
case ESCAPE_UNICODE:
/* FALLTHROUGH */
case ESCAPE_NUMBERED:
/* FALLTHROUGH */
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)
break;
case 'B':
npos = 0;
ubuf[0] = arg_complete &&
NULL, ROFFNUM_SCALE) &&
break;
case 'n':
if (arg_complete)
else
ubuf[0] = '\0';
break;
case 'w':
/* use even incomplete args */
24 * (int)naml);
break;
}
res = "";
return(ROFF_IGN);
}
/* Replace the escape sequence by the string. */
*stesc = '\0';
/* Prepare for the next replacement. */
}
return(ROFF_CONT);
}
/*
* Process text streams:
* Convert all breakable hyphens into ASCII_HYPH.
* Decrement and spring input line trap.
*/
static enum rofferr
{
const char *start;
char *p;
int isz;
enum mandoc_esc esc;
while (*p != '\0') {
p += sz;
if (*p == '\0')
break;
if (*p == '\\') {
/* Skip over escapes. */
p++;
if (esc == ESCAPE_ERROR)
break;
continue;
} else if (p == start) {
p++;
continue;
}
if (isalpha((unsigned char)p[-1]) &&
isalpha((unsigned char)p[1]))
*p = ASCII_HYPH;
p++;
}
/* Spring the input line trap. */
if (roffit_lines == 1) {
*offs = 0;
roffit_lines = 0;
return(ROFF_REPARSE);
} else if (roffit_lines > 1)
--roffit_lines;
return(ROFF_CONT);
}
enum rofferr
{
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) */
/* Handle in-line equation delimiters. */
if (e == ROFF_REPARSE)
return(e);
}
/* Expand some escape sequences. */
if (e == ROFF_IGN)
return(e);
/*
* 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 (e != ROFF_CONT)
return(e);
}
if ( ! ctl)
/* Skip empty request lines. */
return(ROFF_IGN);
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) {
}
/* No scope is open. This is a new request or macro. */
/* Tables ignore most macros. */
if (t == ROFF_TS)
return(ROFF_IGN);
pos++;
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. */
}
void
roff_endparse(struct roff *r)
{
if (r->last)
if (r->eqn) {
}
if (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
{
char *cp;
const char *mac;
enum rofft t;
return(ROFF_MAX);
if (ROFF_MAX != t)
return(t);
}
static enum rofferr
{
/*
* A block-close `..' should only be invoked as a child of an
* ignore macro, otherwise raise a warning and just ignore it.
*/
return(ROFF_IGN);
}
case ROFF_am:
/* ROFF_am1 is remapped to ROFF_am in roff_block(). */
/* FALLTHROUGH */
case ROFF_ami:
/* FALLTHROUGH */
case ROFF_de:
/* ROFF_de1 is remapped to ROFF_de in roff_block(). */
/* FALLTHROUGH */
case ROFF_dei:
/* FALLTHROUGH */
case ROFF_ig:
break;
default:
return(ROFF_IGN);
}
roffnode_pop(r);
return(ROFF_IGN);
}
static void
roffnode_cleanscope(struct roff *r)
{
while (r->last) {
break;
roffnode_pop(r);
}
}
static void
{
return;
}
case ROFF_el:
/* FALLTHROUGH */
case ROFF_ie:
/* FALLTHROUGH */
case ROFF_if:
break;
default:
return;
}
return;
}
roffnode_pop(r);
return;
}
static enum rofferr
{
const char *name;
/* Ignore groff compatibility mode for now. */
/* Parse the macro name argument. */
namesz = 0;
} else {
}
/* Resolve the macro name argument if it is indirect. */
namesz = 0;
} else
} else
return(ROFF_IGN);
}
/*
* At the beginning of a `de' macro, clear the existing string
* with the same name, if there is one. New content will be
* appended from roff_block_text() in multiline mode.
*/
if (*cp == '\0')
return(ROFF_IGN);
/* Get the custom end marker. */
/* Resolve the end marker if it is indirect. */
namesz = 0;
} else
} else
if (namesz)
if (*cp != '\0')
return(ROFF_IGN);
}
static enum rofferr
{
enum rofft t;
int i, j;
/*
* First check whether a custom macro exists at this level. If
* it does, then check against it. This is some of groff's
* stranger behaviours. If we encountered a custom end-scope
* tag and that tag also happens to be a "real" macro, then we
* need to try interpreting it again as a real macro. If it's
* not, then return ignore. Else continue.
*/
break;
roffnode_pop(r);
i++;
pos = i;
return(ROFF_RERUN);
return(ROFF_IGN);
}
}
/*
* If we have no custom end-query or lookup failed, then try
* pulling it out of the hashtable.
*/
if (t != ROFF_cblock) {
return(ROFF_IGN);
}
}
static enum rofferr
{
return(ROFF_IGN);
}
static enum rofferr
{
enum rofft t;
char *ep;
int rr;
/*
* Fully handle known macros when they are structurally
* required or when the conditional evaluated to true.
*/
if ((t != ROFF_MAX) &&
}
/*
* If `\}' occurs on a macro line without a preceding macro,
* drop the line completely.
*/
rr = 0;
/* Always check for the closing delimiter `\}'. */
if (*(++ep) == '}') {
*ep = '&';
}
if (*ep != '\0')
++ep;
}
}
static enum rofferr
{
char *ep;
int rr;
if (*(++ep) == '}') {
*ep = '&';
}
if (*ep != '\0')
++ep;
}
}
/*
* Parse a single signed integer number. Stop at the first non-digit.
* If there is at least one digit, return success and advance the
* parse point, else return failure and let the parse point unchanged.
* Ignore overflows, treat them just like the C language.
*/
static int
{
p = *pos;
n = v[p] == '-';
if (n || v[p] == '+')
p++;
if (flags & ROFFNUM_WHITE)
while (isspace((unsigned char)v[p]))
p++;
if (p == *pos + n)
return 0;
if (n)
/* Each number may be followed by one optional scaling unit. */
switch (v[p]) {
case 'f':
break;
case 'i':
break;
case 'c':
break;
case 'v':
/* FALLTROUGH */
case 'P':
break;
case 'm':
/* FALLTROUGH */
case 'n':
break;
case 'p':
break;
case 'u':
break;
case 'M':
break;
default:
p--;
break;
}
if (flags & ROFFNUM_SCALE)
*pos = p + 1;
return(1);
}
/*
* Evaluate a string comparison condition.
* The first character is the delimiter.
* Succeed if the string up to its second occurrence
* matches the string up to its third occurence.
* Advance the cursor after the third occurrence
* or lacking that, to the end of the line.
*/
static int
roff_evalstrcond(const char *v, int *pos)
{
int match;
match = 0;
goto out;
while ('\0' != *++s3) {
break;
}
match = 1;
break;
}
s2++;
}
out:
else if (*s3 != '\0')
s3++;
return(match);
}
/*
* Evaluate an optionally negated single character, numerical,
* or string condition.
*/
static int
{
if ('!' == v[*pos]) {
wanttrue = 0;
(*pos)++;
} else
wanttrue = 1;
switch (v[*pos]) {
case '\0':
return(0);
case 'n':
/* FALLTHROUGH */
case 'o':
(*pos)++;
return(wanttrue);
case 'c':
/* FALLTHROUGH */
case 'd':
/* FALLTHROUGH */
case 'e':
/* FALLTHROUGH */
case 'r':
/* FALLTHROUGH */
case 't':
/* FALLTHROUGH */
case 'v':
(*pos)++;
return(!wanttrue);
default:
break;
}
else
return (0);
}
static enum rofferr
{
return(ROFF_IGN);
}
static enum rofferr
{
return(ROFF_IGN);
}
static enum rofferr
{
return(ROFF_IGN);
}
static enum rofferr
{
/*
* An `.el' has no conditional body: it will consume the value
* of the current rstack entry set in prior `ie' calls or
* defaults to DENY.
*
* If we're not an `el', however, then evaluate the conditional.
*/
/*
* An if-else will put the NEGATION of the current evaluated
* conditional into the stack of rules.
*/
r->rstacksz += 16;
r->rstacksz, sizeof(int));
}
}
/* If the parent has false as its rule, then so do we. */
/*
* Determine scope.
* If there is nothing on the line after the conditional,
* not even whitespace, use next-line scope.
*/
goto out;
}
pos++;
/* An opening brace requests multiline scope. */
pos += 2;
goto out;
}
/*
* Anything else following the conditional causes
* single-line scope. Warn if the scope contains
* nothing but trailing whitespace.
*/
out:
return(ROFF_RERUN);
}
static enum rofferr
{
char *string;
const char *name;
/* Ignore groff compatibility mode for now. */
/*
* The first word is the name of the string.
* If it is empty or terminated by an escape sequence,
* abort the `ds' request without defining anything.
*/
if (*name == '\0')
return(ROFF_IGN);
return(ROFF_IGN);
/* Read past the initial double-quote, if any. */
if (*string == '"')
string++;
/* The rest is the value. */
return(ROFF_IGN);
}
/*
* Parse a single operator, one or two characters long.
* If the operator is recognized, return success and advance the
* parse point, else return failure and let the parse point unchanged.
*/
static int
{
switch (*res) {
case '+':
/* FALLTHROUGH */
case '-':
/* FALLTHROUGH */
case '*':
/* FALLTHROUGH */
case '/':
/* FALLTHROUGH */
case '%':
/* FALLTHROUGH */
case '&':
/* FALLTHROUGH */
case ':':
break;
case '<':
switch (v[*pos + 1]) {
case '=':
*res = 'l';
(*pos)++;
break;
case '>':
*res = '!';
(*pos)++;
break;
case '?':
*res = 'i';
(*pos)++;
break;
default:
break;
}
break;
case '>':
switch (v[*pos + 1]) {
case '=':
*res = 'g';
(*pos)++;
break;
case '?':
*res = 'a';
(*pos)++;
break;
default:
break;
}
break;
case '=':
(*pos)++;
break;
default:
return(0);
}
(*pos)++;
return(*res);
}
/*
* Evaluate either a parenthesized numeric expression
* or a single signed integer number.
*/
static int
{
if ('(' != v[*pos])
(*pos)++;
return(0);
/*
* Omission of the closing parenthesis
* is an error in validation mode,
* but ignored in evaluation mode.
*/
if (')' == v[*pos])
(*pos)++;
return(0);
return(1);
}
/*
* Evaluate a complete numeric expression.
* Proceed left to right, there is no concept of precedence.
*/
static int
{
char operator;
mypos = 0;
}
if (flags & ROFFNUM_WHITE)
(*pos)++;
return(0);
while (1) {
if (flags & ROFFNUM_WHITE)
(*pos)++;
break;
if (flags & ROFFNUM_WHITE)
(*pos)++;
return(0);
if (flags & ROFFNUM_WHITE)
(*pos)++;
continue;
switch (operator) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
if (operand2 == 0) {
*res = 0;
break;
}
break;
case '%':
if (operand2 == 0) {
*res = 0;
break;
}
break;
case '<':
break;
case '>':
break;
case 'l':
break;
case 'g':
break;
case '=':
break;
case '!':
break;
case '&':
break;
case ':':
break;
case 'i':
break;
case 'a':
break;
default:
abort();
}
}
return(1);
}
void
{
/* Search for an existing register with the same name. */
/* Create a new register. */
}
if ('+' == sign)
else if ('-' == sign)
else
}
/*
* Handle some predefined read-only number registers.
* For now, return -1 if the requested register is not predefined;
* in case a predefined read-only register having the value -1
* were to turn up, another special value would have to be chosen.
*/
static int
roff_getregro(const char *name)
{
switch (*name) {
case 'A': /* ASCII approximation mode is always off. */
return(0);
case 'g': /* Groff compatibility mode is always on. */
return(1);
case 'H': /* Fixed horizontal resolution. */
return (24);
case 'j': /* Always adjust left margin only. */
return(0);
case 'T': /* Some output device is always defined. */
return(1);
case 'V': /* Fixed vertical resolution. */
return (40);
default:
return (-1);
}
}
int
{
int val;
if (-1 != val)
return (val);
}
return(0);
}
static int
{
int val;
if (-1 != val)
return (val);
}
return(0);
}
static void
{
}
}
static enum rofferr
{
int iv;
char sign;
if (*key == '\0')
return(ROFF_IGN);
return(ROFF_IGN);
val++;
return(ROFF_IGN);
}
static enum rofferr
{
if (*name == '\0')
return(ROFF_IGN);
while (1) {
break;
}
}
return(ROFF_IGN);
}
static enum rofferr
{
const char *name;
char *cp;
while (*cp != '\0') {
break;
}
return(ROFF_IGN);
}
static enum rofferr
{
int iv;
/* Parse the number of lines. */
return(ROFF_IGN);
}
pos++;
/*
* Arm the input line trap.
* Special-casing "an-trap" is an ugly workaround to cope
* with DocBook stupidly fiddling with man(7) internals.
*/
roffit_lines = iv;
return(ROFF_IGN);
}
static enum rofferr
{
const char *const *cp;
if (r->format == 0)
r->format = MPARSE_MDOC;
return(ROFF_CONT);
}
static enum rofferr
{
const char *const *cp;
if ((r->options & MPARSE_QUICK) == 0)
if (r->format == 0)
r->format = MPARSE_MAN;
return(ROFF_CONT);
}
static enum rofferr
{
return(ROFF_REPARSE);
}
return(ROFF_IGN);
}
static enum rofferr
{
else
return(ROFF_IGN);
}
/*
* Handle in-line equation delimiters.
*/
static enum rofferr
{
/*
* Outside equations, look for an opening delimiter.
* If we are inside an equation, we already know it is
* in-line, or this function wouldn't have been called;
* so look for a closing delimiter.
*/
return(ROFF_CONT);
*cp2++ = '\0';
/* Handle preceding text, protecting whitespace. */
bef_pr = "\\&";
bef_nl = "\n";
}
/*
* Prepare replacing the delimiter with an equation macro
* and drop leading white space from the equation.
*/
while (*cp2 == ' ')
cp2++;
mac = ".EQ";
} else
mac = ".EN";
/* Handle following text, protecting whitespace. */
if (*cp2 != '\0') {
aft_nl = "\n";
aft_pr = "\\&";
}
/* Do the actual replacement. */
/* Toggle the in-line state of the eqn subsystem. */
return(ROFF_REPARSE);
}
static enum rofferr
{
struct eqn_node *e;
if (r->last_eqn) {
} else
return(ROFF_IGN);
}
static enum rofferr
{
return(ROFF_IGN);
}
static enum rofferr
{
if (r->tbl) {
}
if (r->last_tbl)
else
return(ROFF_IGN);
}
static enum rofferr
{
return(ROFF_CONT);
}
static enum rofferr
{
const char *p;
r->control = 0;
if (*p != '\0')
return(ROFF_IGN);
}
static enum rofferr
{
enum mandoc_esc esc;
if (*p == '\0') {
return(ROFF_IGN);
}
while (*p != '\0') {
first = p++;
if (*first == '\\') {
if (esc == ESCAPE_ERROR) {
return(ROFF_IGN);
}
}
second = p++;
if (*second == '\\') {
if (esc == ESCAPE_ERROR) {
return(ROFF_IGN);
}
} else if (*second == '\0') {
second = " ";
p--;
}
if (fsz > 1) {
continue;
}
sizeof(struct roffstr));
}
return(ROFF_IGN);
}
static enum rofferr
{
/*
* Handle `so'. Be EXTREMELY careful, as we shouldn't be
* opening anything that's not in our cwd or anything beneath
* it. Thus, explicitly disallow traversing up the file-system
* or using absolute paths.
*/
".so %s", name);
*offs = 0;
return(ROFF_REPARSE);
}
return(ROFF_SO);
}
static enum rofferr
{
int i;
/*
* Collect pointers to macro argument strings
* and NUL-terminate them.
*/
for (i = 0; i < 9; i++)
/*
* Expand macro arguments.
*/
while (*cp != '\0') {
/* Scan ahead for the next argument invocation. */
if (*cp++ != '\\')
continue;
if (*cp++ != '$')
continue;
i = *cp - '1';
if (0 > i || 8 < i)
continue;
cp -= 2;
/*
* Determine the size of the expanded argument,
* taking escaping of quotes into account.
*/
asz = 0;
asz++;
if (*ap == '"')
asz += 3;
}
if (asz != 3) {
/*
* Determine the size of the rest of the
* unexpanded macro, including the NUL.
*/
/*
* When shrinking, move before
* releasing the storage.
*/
if (asz < 3)
/*
* Resize the storage for the macro
* and readjust the parse pointer.
*/
/*
* When growing, make room
* for the expanded argument.
*/
if (asz > 3)
}
/* Copy the expanded argument, escaping quotes. */
if (*ap == '"') {
n2 += 4;
} else
}
}
/*
* Replace the macro invocation
* by the expanded macro.
*/
*offs = 0;
}
static size_t
{
if ('\0' == *name)
return(0);
/* Read until end of name and terminate it with NUL. */
break;
}
if ('\\' != *cp)
continue;
break;
cp++;
if ('\\' == *cp)
continue;
break;
}
/* Read past spaces. */
while (' ' == *cp)
cp++;
return(namesz);
}
/*
* Store *string into the user-defined string called *name.
* To clear an existing entry, call with (*r, *name, NULL, 0).
* append == 0: replace mode
* append == 1: single-line append mode
* append == 2: multiline append mode, append '\n' after each call
*/
static void
int append)
{
}
static void
{
struct roffkv *n;
char *c;
int i;
/* Search for an existing string with the same name. */
n = *r;
n = n->next;
if (NULL == n) {
/* Create a new string table entry. */
n = mandoc_malloc(sizeof(struct roffkv));
n->next = *r;
*r = n;
} else if (0 == append) {
}
return;
/*
* One additional byte for the '\n' in multiline mode,
* and one for the terminating '\0'.
*/
*n->val.p = '\0';
oldch = 0;
} else {
}
/* Skip existing content in the destination buffer. */
/* Append new content to the destination buffer. */
i = 0;
while (i < (int)stringsz) {
/*
* Rudimentary roff copy mode:
* Handle escaped backslashes.
*/
i++;
*c++ = string[i++];
}
/* Append terminating bytes. */
if (1 < append)
*c++ = '\n';
*c = '\0';
}
static const char *
{
const struct roffkv *n;
int i;
return(n->val.p);
for (i = 0; i < PREDEFS_MAX; i++)
return(NULL);
}
static void
roff_freestr(struct roffkv *r)
{
for (n = r; n; n = nn) {
free(n);
}
}
const struct tbl_span *
{
}
const struct eqn *
{
}
/*
* Duplicate an input string, making the appropriate character
* conversations (as stipulated by `tr') along the way.
* Returns a heap-allocated string with all the replacements made.
*/
char *
roff_strdup(const struct roff *r, const char *p)
{
char *res;
const char *pp;
enum mandoc_esc esc;
return(mandoc_strdup(p));
else if ('\0' == *p)
return(mandoc_strdup(""));
/*
* Step through each character looking for term matches
* (remember that a `tr' can be invoked with an escape, which is
* a glyph but the escape is multi-character).
* We only do this if the character hash has been initialised
* and the string is >0 length.
*/
ssz = 0;
while ('\0' != *p) {
p++;
continue;
} else if ('\\' != *p) {
continue;
}
/* Search for term matches. */
break;
/*
* A match has been found.
* Append the match to the array and move
* forward by its keysize.
*/
continue;
}
/*
* Handle escapes carefully: we need to copy
* over just the escape itself, or else we might
* do replacements within the escape itself.
* Make sure to pass along the bogus string.
*/
pp = p++;
if (ESCAPE_ERROR == esc) {
break;
}
/*
* We bail out on bad escapes.
* No need to warn: we already did so when
* roff_res() was called.
*/
}
return(res);
}
int
roff_getformat(const struct roff *r)
{
return(r->format);
}
/*
* Find out whether a line is a macro line or not.
* If it is, adjust the current position and return one; if it isn't,
* return zero and don't change the current position.
* If the control character has been set with `.cc', then let that grain
* precedence.
* This is slighly contrary to groff, where using the non-breaking
* control character when `cc' has been invoked will cause the
* non-breaking macro contents to be printed verbatim.
*/
int
{
int pos;
pos++;
else if (0 != r->control)
return(0);
pos += 2;
pos++;
else
return(0);
pos++;
return(1);
}