Annotation of src/usr.bin/mandoc/roff.c, Revision 1.241
1.241 ! schwarze 1: /* $OpenBSD: roff.c,v 1.240 2019/12/26 19:51:47 schwarze Exp $ */
1.1 schwarze 2: /*
1.139 schwarze 3: * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.241 ! schwarze 4: * Copyright (c) 2010-2015, 2017-2020 Ingo Schwarze <schwarze@openbsd.org>
1.1 schwarze 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
1.16 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1 schwarze 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.16 schwarze 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1 schwarze 13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
1.99 schwarze 18: #include <sys/types.h>
19:
1.1 schwarze 20: #include <assert.h>
1.3 schwarze 21: #include <ctype.h>
1.117 schwarze 22: #include <limits.h>
1.167 schwarze 23: #include <stddef.h>
24: #include <stdint.h>
1.51 schwarze 25: #include <stdio.h>
1.1 schwarze 26: #include <stdlib.h>
27: #include <string.h>
28:
1.74 schwarze 29: #include "mandoc_aux.h"
1.167 schwarze 30: #include "mandoc_ohash.h"
1.217 schwarze 31: #include "mandoc.h"
1.137 schwarze 32: #include "roff.h"
1.221 schwarze 33: #include "mandoc_parse.h"
1.98 daniel 34: #include "libmandoc.h"
1.138 schwarze 35: #include "roff_int.h"
1.218 schwarze 36: #include "tbl_parse.h"
1.219 schwarze 37: #include "eqn_parse.h"
1.2 schwarze 38:
1.227 schwarze 39: /*
40: * ASCII_ESC is used to signal from roff_getarg() to roff_expand()
41: * that an escape sequence resulted from copy-in processing and
42: * needs to be checked or interpolated. As it is used nowhere
43: * else, it is defined here rather than in a header file.
44: */
45: #define ASCII_ESC 27
46:
1.43 schwarze 47: /* Maximum number of string expansions per line, to break infinite loops. */
48: #define EXPAND_LIMIT 1000
49:
1.187 schwarze 50: /* Types of definitions of macros and strings. */
51: #define ROFFDEF_USER (1 << 1) /* User-defined. */
52: #define ROFFDEF_PRE (1 << 2) /* Predefined. */
53: #define ROFFDEF_REN (1 << 3) /* Renamed standard macro. */
54: #define ROFFDEF_STD (1 << 4) /* mdoc(7) or man(7) macro. */
55: #define ROFFDEF_ANY (ROFFDEF_USER | ROFFDEF_PRE | \
56: ROFFDEF_REN | ROFFDEF_STD)
1.197 schwarze 57: #define ROFFDEF_UNDEF (1 << 5) /* Completely undefined. */
1.187 schwarze 58:
1.138 schwarze 59: /* --- data types --------------------------------------------------------- */
60:
1.41 schwarze 61: /*
1.42 schwarze 62: * An incredibly-simple string buffer.
63: */
1.8 schwarze 64: struct roffstr {
1.42 schwarze 65: char *p; /* nil-terminated buffer */
66: size_t sz; /* saved strlen(p) */
67: };
68:
69: /*
70: * A key-value roffstr pair as part of a singly-linked list.
71: */
72: struct roffkv {
73: struct roffstr key;
74: struct roffstr val;
75: struct roffkv *next; /* next in list */
1.8 schwarze 76: };
77:
1.52 schwarze 78: /*
79: * A single number register as part of a singly-linked list.
80: */
81: struct roffreg {
82: struct roffstr key;
1.53 schwarze 83: int val;
1.199 schwarze 84: int step;
1.52 schwarze 85: struct roffreg *next;
86: };
87:
1.167 schwarze 88: /*
89: * Association of request and macro names with token IDs.
90: */
91: struct roffreq {
92: enum roff_tok tok;
93: char name[];
94: };
95:
1.211 schwarze 96: /*
97: * A macro processing context.
98: * More than one is needed when macro calls are nested.
99: */
100: struct mctx {
101: char **argv;
102: int argc;
103: int argsz;
104: };
105:
1.1 schwarze 106: struct roff {
1.168 schwarze 107: struct roff_man *man; /* mdoc or man parser */
1.1 schwarze 108: struct roffnode *last; /* leaf of stack */
1.211 schwarze 109: struct mctx *mstack; /* stack of macro contexts */
1.96 schwarze 110: int *rstack; /* stack of inverted `ie' values */
1.167 schwarze 111: struct ohash *reqtab; /* request lookup table */
1.52 schwarze 112: struct roffreg *regtab; /* number registers */
1.42 schwarze 113: struct roffkv *strtab; /* user-defined strings & macros */
1.178 schwarze 114: struct roffkv *rentab; /* renamed strings & macros */
1.42 schwarze 115: struct roffkv *xmbtab; /* multi-byte trans table (`tr') */
116: struct roffstr *xtab; /* single-byte trans table (`tr') */
1.16 schwarze 117: const char *current_string; /* value of last called user macro */
1.27 schwarze 118: struct tbl_node *first_tbl; /* first table parsed */
119: struct tbl_node *last_tbl; /* last table parsed */
120: struct tbl_node *tbl; /* current table being parsed */
1.191 schwarze 121: struct eqn_node *last_eqn; /* equation parser */
122: struct eqn_node *eqn; /* active equation parser */
1.102 schwarze 123: int eqn_inline; /* current equation is inline */
1.96 schwarze 124: int options; /* parse options */
1.211 schwarze 125: int mstacksz; /* current size of mstack */
126: int mstackpos; /* position in mstack */
1.96 schwarze 127: int rstacksz; /* current size limit of rstack */
128: int rstackpos; /* position in rstack */
1.99 schwarze 129: int format; /* current file in mdoc or man format */
1.96 schwarze 130: char control; /* control character */
1.175 schwarze 131: char escape; /* escape character */
1.1 schwarze 132: };
133:
1.236 schwarze 134: /*
135: * A macro definition, condition, or ignored block.
136: */
1.1 schwarze 137: struct roffnode {
1.166 schwarze 138: enum roff_tok tok; /* type of node */
1.1 schwarze 139: struct roffnode *parent; /* up one in stack */
140: int line; /* parse line */
141: int col; /* parse col */
1.16 schwarze 142: char *name; /* node name, e.g. macro name */
1.236 schwarze 143: char *end; /* custom end macro of the block */
144: int endspan; /* scope to: 1=eol 2=next line -1=\} */
145: int rule; /* content is: 1=evaluated 0=skipped */
1.1 schwarze 146: };
147:
148: #define ROFF_ARGS struct roff *r, /* parse ctx */ \
1.166 schwarze 149: enum roff_tok tok, /* tok of macro */ \
1.110 schwarze 150: struct buf *buf, /* input buffer */ \
1.1 schwarze 151: int ln, /* parse line */ \
1.2 schwarze 152: int ppos, /* original pos in buffer */ \
153: int pos, /* current pos in buffer */ \
154: int *offs /* reset offset of buffer data */
1.1 schwarze 155:
1.212 schwarze 156: typedef int (*roffproc)(ROFF_ARGS);
1.1 schwarze 157:
158: struct roffmac {
1.2 schwarze 159: roffproc proc; /* process new macro */
160: roffproc text; /* process as child text of macro */
161: roffproc sub; /* process as child of macro */
162: int flags;
163: #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
1.1 schwarze 164: };
165:
1.37 schwarze 166: struct predef {
167: const char *name; /* predefined input name */
168: const char *str; /* replacement symbol */
169: };
170:
171: #define PREDEF(__name, __str) \
172: { (__name), (__str) },
173:
1.138 schwarze 174: /* --- function prototypes ------------------------------------------------ */
175:
1.212 schwarze 176: static int roffnode_cleanscope(struct roff *);
177: static int roffnode_pop(struct roff *);
1.166 schwarze 178: static void roffnode_push(struct roff *, enum roff_tok,
1.42 schwarze 179: const char *, int, int);
1.218 schwarze 180: static void roff_addtbl(struct roff_man *, int, struct tbl_node *);
1.212 schwarze 181: static int roff_als(ROFF_ARGS);
182: static int roff_block(ROFF_ARGS);
183: static int roff_block_text(ROFF_ARGS);
184: static int roff_block_sub(ROFF_ARGS);
1.236 schwarze 185: static int roff_break(ROFF_ARGS);
1.212 schwarze 186: static int roff_cblock(ROFF_ARGS);
187: static int roff_cc(ROFF_ARGS);
188: static int roff_ccond(struct roff *, int, int);
1.213 schwarze 189: static int roff_char(ROFF_ARGS);
1.212 schwarze 190: static int roff_cond(ROFF_ARGS);
191: static int roff_cond_text(ROFF_ARGS);
192: static int roff_cond_sub(ROFF_ARGS);
193: static int roff_ds(ROFF_ARGS);
194: static int roff_ec(ROFF_ARGS);
195: static int roff_eo(ROFF_ARGS);
196: static int roff_eqndelim(struct roff *, struct buf *, int);
1.143 schwarze 197: static int roff_evalcond(struct roff *r, int, char *, int *);
1.106 schwarze 198: static int roff_evalnum(struct roff *, int,
199: const char *, int *, int *, int);
200: static int roff_evalpar(struct roff *, int,
1.133 schwarze 201: const char *, int *, int *, int);
1.71 schwarze 202: static int roff_evalstrcond(const char *, int *);
1.227 schwarze 203: static int roff_expand(struct roff *, struct buf *,
204: int, int, char);
1.42 schwarze 205: static void roff_free1(struct roff *);
1.52 schwarze 206: static void roff_freereg(struct roffreg *);
1.42 schwarze 207: static void roff_freestr(struct roffkv *);
1.85 schwarze 208: static size_t roff_getname(struct roff *, char **, int, int);
1.133 schwarze 209: static int roff_getnum(const char *, int *, int *, int);
1.56 schwarze 210: static int roff_getop(const char *, int *, char *);
1.199 schwarze 211: static int roff_getregn(struct roff *,
212: const char *, size_t, char);
1.145 schwarze 213: static int roff_getregro(const struct roff *,
214: const char *name);
1.197 schwarze 215: static const char *roff_getstrn(struct roff *,
1.187 schwarze 216: const char *, size_t, int *);
1.143 schwarze 217: static int roff_hasregn(const struct roff *,
218: const char *, size_t);
1.212 schwarze 219: static int roff_insec(ROFF_ARGS);
220: static int roff_it(ROFF_ARGS);
221: static int roff_line_ignore(ROFF_ARGS);
1.137 schwarze 222: static void roff_man_alloc1(struct roff_man *);
223: static void roff_man_free1(struct roff_man *);
1.212 schwarze 224: static int roff_manyarg(ROFF_ARGS);
1.230 schwarze 225: static int roff_noarg(ROFF_ARGS);
1.212 schwarze 226: static int roff_nop(ROFF_ARGS);
227: static int roff_nr(ROFF_ARGS);
228: static int roff_onearg(ROFF_ARGS);
1.166 schwarze 229: static enum roff_tok roff_parse(struct roff *, char *, int *,
1.87 schwarze 230: int, int);
1.212 schwarze 231: static int roff_parsetext(struct roff *, struct buf *,
1.177 schwarze 232: int, int *);
1.212 schwarze 233: static int roff_renamed(ROFF_ARGS);
234: static int roff_return(ROFF_ARGS);
235: static int roff_rm(ROFF_ARGS);
236: static int roff_rn(ROFF_ARGS);
237: static int roff_rr(ROFF_ARGS);
1.198 schwarze 238: static void roff_setregn(struct roff *, const char *,
1.199 schwarze 239: size_t, int, char, int);
1.8 schwarze 240: static void roff_setstr(struct roff *,
1.16 schwarze 241: const char *, const char *, int);
1.80 schwarze 242: static void roff_setstrn(struct roffkv **, const char *,
1.42 schwarze 243: size_t, const char *, size_t, int);
1.212 schwarze 244: static int roff_shift(ROFF_ARGS);
245: static int roff_so(ROFF_ARGS);
246: static int roff_tr(ROFF_ARGS);
247: static int roff_Dd(ROFF_ARGS);
248: static int roff_TE(ROFF_ARGS);
249: static int roff_TS(ROFF_ARGS);
250: static int roff_EQ(ROFF_ARGS);
251: static int roff_EN(ROFF_ARGS);
252: static int roff_T_(ROFF_ARGS);
253: static int roff_unsupp(ROFF_ARGS);
254: static int roff_userdef(ROFF_ARGS);
1.1 schwarze 255:
1.138 schwarze 256: /* --- constant data ------------------------------------------------------ */
257:
1.133 schwarze 258: #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
259: #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
260:
1.166 schwarze 261: const char *__roff_name[MAN_MAX + 1] = {
1.230 schwarze 262: "br", "ce", "fi", "ft",
263: "ll", "mc", "nf",
264: "po", "rj", "sp",
1.184 schwarze 265: "ta", "ti", NULL,
1.166 schwarze 266: "ab", "ad", "af", "aln",
267: "als", "am", "am1", "ami",
268: "ami1", "as", "as1", "asciify",
269: "backtrace", "bd", "bleedat", "blm",
270: "box", "boxa", "bp", "BP",
271: "break", "breakchar", "brnl", "brp",
1.177 schwarze 272: "brpnl", "c2", "cc",
1.166 schwarze 273: "cf", "cflags", "ch", "char",
274: "chop", "class", "close", "CL",
275: "color", "composite", "continue", "cp",
276: "cropat", "cs", "cu", "da",
277: "dch", "Dd", "de", "de1",
278: "defcolor", "dei", "dei1", "device",
279: "devicem", "di", "do", "ds",
280: "ds1", "dwh", "dt", "ec",
281: "ecr", "ecs", "el", "em",
282: "EN", "eo", "EP", "EQ",
283: "errprint", "ev", "evc", "ex",
284: "fallback", "fam", "fc", "fchar",
285: "fcolor", "fdeferlig", "feature", "fkern",
286: "fl", "flig", "fp", "fps",
287: "fschar", "fspacewidth", "fspecial", "ftr",
288: "fzoom", "gcolor", "hc", "hcode",
289: "hidechar", "hla", "hlm", "hpf",
290: "hpfa", "hpfcode", "hw", "hy",
291: "hylang", "hylen", "hym", "hypp",
292: "hys", "ie", "if", "ig",
293: "index", "it", "itc", "IX",
294: "kern", "kernafter", "kernbefore", "kernpair",
295: "lc", "lc_ctype", "lds", "length",
296: "letadj", "lf", "lg", "lhang",
297: "linetabs", "lnr", "lnrf", "lpfx",
1.176 schwarze 298: "ls", "lsm", "lt",
1.166 schwarze 299: "mediasize", "minss", "mk", "mso",
300: "na", "ne", "nh", "nhychar",
301: "nm", "nn", "nop", "nr",
302: "nrf", "nroff", "ns", "nx",
303: "open", "opena", "os", "output",
304: "padj", "papersize", "pc", "pev",
305: "pi", "PI", "pl", "pm",
1.184 schwarze 306: "pn", "pnr", "ps",
1.166 schwarze 307: "psbb", "pshape", "pso", "ptr",
308: "pvs", "rchar", "rd", "recursionlimit",
1.181 schwarze 309: "return", "rfschar", "rhang",
1.166 schwarze 310: "rm", "rn", "rnn", "rr",
311: "rs", "rt", "schar", "sentchar",
312: "shc", "shift", "sizes", "so",
313: "spacewidth", "special", "spreadwarn", "ss",
314: "sty", "substring", "sv", "sy",
1.172 schwarze 315: "T&", "tc", "TE",
1.173 schwarze 316: "TH", "tkf", "tl",
1.166 schwarze 317: "tm", "tm1", "tmc", "tr",
318: "track", "transchar", "trf", "trimat",
319: "trin", "trnt", "troff", "TS",
320: "uf", "ul", "unformat", "unwatch",
321: "unwatchn", "vpt", "vs", "warn",
322: "warnscale", "watch", "watchlength", "watchn",
323: "wh", "while", "write", "writec",
324: "writem", "xflag", ".", NULL,
1.178 schwarze 325: NULL, "text",
1.166 schwarze 326: "Dd", "Dt", "Os", "Sh",
327: "Ss", "Pp", "D1", "Dl",
328: "Bd", "Ed", "Bl", "El",
329: "It", "Ad", "An", "Ap",
330: "Ar", "Cd", "Cm", "Dv",
331: "Er", "Ev", "Ex", "Fa",
332: "Fd", "Fl", "Fn", "Ft",
333: "Ic", "In", "Li", "Nd",
334: "Nm", "Op", "Ot", "Pa",
335: "Rv", "St", "Va", "Vt",
336: "Xr", "%A", "%B", "%D",
337: "%I", "%J", "%N", "%O",
338: "%P", "%R", "%T", "%V",
339: "Ac", "Ao", "Aq", "At",
340: "Bc", "Bf", "Bo", "Bq",
341: "Bsx", "Bx", "Db", "Dc",
342: "Do", "Dq", "Ec", "Ef",
343: "Em", "Eo", "Fx", "Ms",
344: "No", "Ns", "Nx", "Ox",
345: "Pc", "Pf", "Po", "Pq",
346: "Qc", "Ql", "Qo", "Qq",
347: "Re", "Rs", "Sc", "So",
348: "Sq", "Sm", "Sx", "Sy",
349: "Tn", "Ux", "Xc", "Xo",
350: "Fo", "Fc", "Oo", "Oc",
351: "Bk", "Ek", "Bt", "Hf",
352: "Fr", "Ud", "Lb", "Lp",
353: "Lk", "Mt", "Brq", "Bro",
354: "Brc", "%C", "Es", "En",
1.171 schwarze 355: "Dx", "%Q", "%U", "Ta",
1.241 ! schwarze 356: "Tg", NULL,
1.166 schwarze 357: "TH", "SH", "SS", "TP",
1.204 schwarze 358: "TQ",
1.166 schwarze 359: "LP", "PP", "P", "IP",
360: "HP", "SM", "SB", "BI",
361: "IB", "BR", "RB", "R",
362: "B", "I", "IR", "RI",
363: "RE", "RS", "DT", "UC",
1.169 schwarze 364: "PD", "AT", "in",
1.205 schwarze 365: "SY", "YS", "OP",
366: "EX", "EE", "UR",
1.189 bentley 367: "UE", "MT", "ME", NULL
1.166 schwarze 368: };
369: const char *const *roff_name = __roff_name;
370:
371: static struct roffmac roffs[TOKEN_NONE] = {
1.230 schwarze 372: { roff_noarg, NULL, NULL, 0 }, /* br */
1.177 schwarze 373: { roff_onearg, NULL, NULL, 0 }, /* ce */
1.230 schwarze 374: { roff_noarg, NULL, NULL, 0 }, /* fi */
1.169 schwarze 375: { roff_onearg, NULL, NULL, 0 }, /* ft */
1.170 schwarze 376: { roff_onearg, NULL, NULL, 0 }, /* ll */
1.176 schwarze 377: { roff_onearg, NULL, NULL, 0 }, /* mc */
1.230 schwarze 378: { roff_noarg, NULL, NULL, 0 }, /* nf */
1.184 schwarze 379: { roff_onearg, NULL, NULL, 0 }, /* po */
1.181 schwarze 380: { roff_onearg, NULL, NULL, 0 }, /* rj */
1.171 schwarze 381: { roff_onearg, NULL, NULL, 0 }, /* sp */
1.172 schwarze 382: { roff_manyarg, NULL, NULL, 0 }, /* ta */
1.173 schwarze 383: { roff_onearg, NULL, NULL, 0 }, /* ti */
1.168 schwarze 384: { NULL, NULL, NULL, 0 }, /* ROFF_MAX */
1.167 schwarze 385: { roff_unsupp, NULL, NULL, 0 }, /* ab */
386: { roff_line_ignore, NULL, NULL, 0 }, /* ad */
387: { roff_line_ignore, NULL, NULL, 0 }, /* af */
388: { roff_unsupp, NULL, NULL, 0 }, /* aln */
1.183 schwarze 389: { roff_als, NULL, NULL, 0 }, /* als */
1.167 schwarze 390: { roff_block, roff_block_text, roff_block_sub, 0 }, /* am */
391: { roff_block, roff_block_text, roff_block_sub, 0 }, /* am1 */
392: { roff_block, roff_block_text, roff_block_sub, 0 }, /* ami */
393: { roff_block, roff_block_text, roff_block_sub, 0 }, /* ami1 */
394: { roff_ds, NULL, NULL, 0 }, /* as */
395: { roff_ds, NULL, NULL, 0 }, /* as1 */
396: { roff_unsupp, NULL, NULL, 0 }, /* asciify */
397: { roff_line_ignore, NULL, NULL, 0 }, /* backtrace */
398: { roff_line_ignore, NULL, NULL, 0 }, /* bd */
399: { roff_line_ignore, NULL, NULL, 0 }, /* bleedat */
400: { roff_unsupp, NULL, NULL, 0 }, /* blm */
401: { roff_unsupp, NULL, NULL, 0 }, /* box */
402: { roff_unsupp, NULL, NULL, 0 }, /* boxa */
403: { roff_line_ignore, NULL, NULL, 0 }, /* bp */
404: { roff_unsupp, NULL, NULL, 0 }, /* BP */
1.236 schwarze 405: { roff_break, NULL, NULL, 0 }, /* break */
1.167 schwarze 406: { roff_line_ignore, NULL, NULL, 0 }, /* breakchar */
407: { roff_line_ignore, NULL, NULL, 0 }, /* brnl */
1.230 schwarze 408: { roff_noarg, NULL, NULL, 0 }, /* brp */
1.167 schwarze 409: { roff_line_ignore, NULL, NULL, 0 }, /* brpnl */
410: { roff_unsupp, NULL, NULL, 0 }, /* c2 */
411: { roff_cc, NULL, NULL, 0 }, /* cc */
412: { roff_insec, NULL, NULL, 0 }, /* cf */
413: { roff_line_ignore, NULL, NULL, 0 }, /* cflags */
414: { roff_line_ignore, NULL, NULL, 0 }, /* ch */
1.213 schwarze 415: { roff_char, NULL, NULL, 0 }, /* char */
1.167 schwarze 416: { roff_unsupp, NULL, NULL, 0 }, /* chop */
417: { roff_line_ignore, NULL, NULL, 0 }, /* class */
418: { roff_insec, NULL, NULL, 0 }, /* close */
419: { roff_unsupp, NULL, NULL, 0 }, /* CL */
420: { roff_line_ignore, NULL, NULL, 0 }, /* color */
421: { roff_unsupp, NULL, NULL, 0 }, /* composite */
422: { roff_unsupp, NULL, NULL, 0 }, /* continue */
423: { roff_line_ignore, NULL, NULL, 0 }, /* cp */
424: { roff_line_ignore, NULL, NULL, 0 }, /* cropat */
425: { roff_line_ignore, NULL, NULL, 0 }, /* cs */
426: { roff_line_ignore, NULL, NULL, 0 }, /* cu */
427: { roff_unsupp, NULL, NULL, 0 }, /* da */
428: { roff_unsupp, NULL, NULL, 0 }, /* dch */
429: { roff_Dd, NULL, NULL, 0 }, /* Dd */
430: { roff_block, roff_block_text, roff_block_sub, 0 }, /* de */
431: { roff_block, roff_block_text, roff_block_sub, 0 }, /* de1 */
432: { roff_line_ignore, NULL, NULL, 0 }, /* defcolor */
433: { roff_block, roff_block_text, roff_block_sub, 0 }, /* dei */
434: { roff_block, roff_block_text, roff_block_sub, 0 }, /* dei1 */
435: { roff_unsupp, NULL, NULL, 0 }, /* device */
436: { roff_unsupp, NULL, NULL, 0 }, /* devicem */
437: { roff_unsupp, NULL, NULL, 0 }, /* di */
438: { roff_unsupp, NULL, NULL, 0 }, /* do */
439: { roff_ds, NULL, NULL, 0 }, /* ds */
440: { roff_ds, NULL, NULL, 0 }, /* ds1 */
441: { roff_unsupp, NULL, NULL, 0 }, /* dwh */
442: { roff_unsupp, NULL, NULL, 0 }, /* dt */
1.175 schwarze 443: { roff_ec, NULL, NULL, 0 }, /* ec */
1.167 schwarze 444: { roff_unsupp, NULL, NULL, 0 }, /* ecr */
445: { roff_unsupp, NULL, NULL, 0 }, /* ecs */
446: { roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT }, /* el */
447: { roff_unsupp, NULL, NULL, 0 }, /* em */
448: { roff_EN, NULL, NULL, 0 }, /* EN */
1.175 schwarze 449: { roff_eo, NULL, NULL, 0 }, /* eo */
1.167 schwarze 450: { roff_unsupp, NULL, NULL, 0 }, /* EP */
451: { roff_EQ, NULL, NULL, 0 }, /* EQ */
452: { roff_line_ignore, NULL, NULL, 0 }, /* errprint */
453: { roff_unsupp, NULL, NULL, 0 }, /* ev */
454: { roff_unsupp, NULL, NULL, 0 }, /* evc */
455: { roff_unsupp, NULL, NULL, 0 }, /* ex */
456: { roff_line_ignore, NULL, NULL, 0 }, /* fallback */
457: { roff_line_ignore, NULL, NULL, 0 }, /* fam */
458: { roff_unsupp, NULL, NULL, 0 }, /* fc */
459: { roff_unsupp, NULL, NULL, 0 }, /* fchar */
460: { roff_line_ignore, NULL, NULL, 0 }, /* fcolor */
461: { roff_line_ignore, NULL, NULL, 0 }, /* fdeferlig */
462: { roff_line_ignore, NULL, NULL, 0 }, /* feature */
463: { roff_line_ignore, NULL, NULL, 0 }, /* fkern */
464: { roff_line_ignore, NULL, NULL, 0 }, /* fl */
465: { roff_line_ignore, NULL, NULL, 0 }, /* flig */
466: { roff_line_ignore, NULL, NULL, 0 }, /* fp */
467: { roff_line_ignore, NULL, NULL, 0 }, /* fps */
468: { roff_unsupp, NULL, NULL, 0 }, /* fschar */
469: { roff_line_ignore, NULL, NULL, 0 }, /* fspacewidth */
470: { roff_line_ignore, NULL, NULL, 0 }, /* fspecial */
471: { roff_line_ignore, NULL, NULL, 0 }, /* ftr */
472: { roff_line_ignore, NULL, NULL, 0 }, /* fzoom */
473: { roff_line_ignore, NULL, NULL, 0 }, /* gcolor */
474: { roff_line_ignore, NULL, NULL, 0 }, /* hc */
475: { roff_line_ignore, NULL, NULL, 0 }, /* hcode */
476: { roff_line_ignore, NULL, NULL, 0 }, /* hidechar */
477: { roff_line_ignore, NULL, NULL, 0 }, /* hla */
478: { roff_line_ignore, NULL, NULL, 0 }, /* hlm */
479: { roff_line_ignore, NULL, NULL, 0 }, /* hpf */
480: { roff_line_ignore, NULL, NULL, 0 }, /* hpfa */
481: { roff_line_ignore, NULL, NULL, 0 }, /* hpfcode */
482: { roff_line_ignore, NULL, NULL, 0 }, /* hw */
483: { roff_line_ignore, NULL, NULL, 0 }, /* hy */
484: { roff_line_ignore, NULL, NULL, 0 }, /* hylang */
485: { roff_line_ignore, NULL, NULL, 0 }, /* hylen */
486: { roff_line_ignore, NULL, NULL, 0 }, /* hym */
487: { roff_line_ignore, NULL, NULL, 0 }, /* hypp */
488: { roff_line_ignore, NULL, NULL, 0 }, /* hys */
489: { roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT }, /* ie */
490: { roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT }, /* if */
491: { roff_block, roff_block_text, roff_block_sub, 0 }, /* ig */
492: { roff_unsupp, NULL, NULL, 0 }, /* index */
493: { roff_it, NULL, NULL, 0 }, /* it */
494: { roff_unsupp, NULL, NULL, 0 }, /* itc */
495: { roff_line_ignore, NULL, NULL, 0 }, /* IX */
496: { roff_line_ignore, NULL, NULL, 0 }, /* kern */
497: { roff_line_ignore, NULL, NULL, 0 }, /* kernafter */
498: { roff_line_ignore, NULL, NULL, 0 }, /* kernbefore */
499: { roff_line_ignore, NULL, NULL, 0 }, /* kernpair */
500: { roff_unsupp, NULL, NULL, 0 }, /* lc */
501: { roff_unsupp, NULL, NULL, 0 }, /* lc_ctype */
502: { roff_unsupp, NULL, NULL, 0 }, /* lds */
503: { roff_unsupp, NULL, NULL, 0 }, /* length */
504: { roff_line_ignore, NULL, NULL, 0 }, /* letadj */
505: { roff_insec, NULL, NULL, 0 }, /* lf */
506: { roff_line_ignore, NULL, NULL, 0 }, /* lg */
507: { roff_line_ignore, NULL, NULL, 0 }, /* lhang */
508: { roff_unsupp, NULL, NULL, 0 }, /* linetabs */
509: { roff_unsupp, NULL, NULL, 0 }, /* lnr */
510: { roff_unsupp, NULL, NULL, 0 }, /* lnrf */
511: { roff_unsupp, NULL, NULL, 0 }, /* lpfx */
512: { roff_line_ignore, NULL, NULL, 0 }, /* ls */
513: { roff_unsupp, NULL, NULL, 0 }, /* lsm */
514: { roff_line_ignore, NULL, NULL, 0 }, /* lt */
515: { roff_line_ignore, NULL, NULL, 0 }, /* mediasize */
516: { roff_line_ignore, NULL, NULL, 0 }, /* minss */
517: { roff_line_ignore, NULL, NULL, 0 }, /* mk */
518: { roff_insec, NULL, NULL, 0 }, /* mso */
519: { roff_line_ignore, NULL, NULL, 0 }, /* na */
520: { roff_line_ignore, NULL, NULL, 0 }, /* ne */
521: { roff_line_ignore, NULL, NULL, 0 }, /* nh */
522: { roff_line_ignore, NULL, NULL, 0 }, /* nhychar */
523: { roff_unsupp, NULL, NULL, 0 }, /* nm */
524: { roff_unsupp, NULL, NULL, 0 }, /* nn */
1.202 schwarze 525: { roff_nop, NULL, NULL, 0 }, /* nop */
1.167 schwarze 526: { roff_nr, NULL, NULL, 0 }, /* nr */
527: { roff_unsupp, NULL, NULL, 0 }, /* nrf */
528: { roff_line_ignore, NULL, NULL, 0 }, /* nroff */
529: { roff_line_ignore, NULL, NULL, 0 }, /* ns */
530: { roff_insec, NULL, NULL, 0 }, /* nx */
531: { roff_insec, NULL, NULL, 0 }, /* open */
532: { roff_insec, NULL, NULL, 0 }, /* opena */
533: { roff_line_ignore, NULL, NULL, 0 }, /* os */
534: { roff_unsupp, NULL, NULL, 0 }, /* output */
535: { roff_line_ignore, NULL, NULL, 0 }, /* padj */
536: { roff_line_ignore, NULL, NULL, 0 }, /* papersize */
537: { roff_line_ignore, NULL, NULL, 0 }, /* pc */
538: { roff_line_ignore, NULL, NULL, 0 }, /* pev */
539: { roff_insec, NULL, NULL, 0 }, /* pi */
540: { roff_unsupp, NULL, NULL, 0 }, /* PI */
541: { roff_line_ignore, NULL, NULL, 0 }, /* pl */
542: { roff_line_ignore, NULL, NULL, 0 }, /* pm */
543: { roff_line_ignore, NULL, NULL, 0 }, /* pn */
544: { roff_line_ignore, NULL, NULL, 0 }, /* pnr */
545: { roff_line_ignore, NULL, NULL, 0 }, /* ps */
546: { roff_unsupp, NULL, NULL, 0 }, /* psbb */
547: { roff_unsupp, NULL, NULL, 0 }, /* pshape */
548: { roff_insec, NULL, NULL, 0 }, /* pso */
549: { roff_line_ignore, NULL, NULL, 0 }, /* ptr */
550: { roff_line_ignore, NULL, NULL, 0 }, /* pvs */
551: { roff_unsupp, NULL, NULL, 0 }, /* rchar */
552: { roff_line_ignore, NULL, NULL, 0 }, /* rd */
553: { roff_line_ignore, NULL, NULL, 0 }, /* recursionlimit */
1.211 schwarze 554: { roff_return, NULL, NULL, 0 }, /* return */
1.167 schwarze 555: { roff_unsupp, NULL, NULL, 0 }, /* rfschar */
556: { roff_line_ignore, NULL, NULL, 0 }, /* rhang */
557: { roff_rm, NULL, NULL, 0 }, /* rm */
1.178 schwarze 558: { roff_rn, NULL, NULL, 0 }, /* rn */
1.167 schwarze 559: { roff_unsupp, NULL, NULL, 0 }, /* rnn */
560: { roff_rr, NULL, NULL, 0 }, /* rr */
561: { roff_line_ignore, NULL, NULL, 0 }, /* rs */
562: { roff_line_ignore, NULL, NULL, 0 }, /* rt */
563: { roff_unsupp, NULL, NULL, 0 }, /* schar */
564: { roff_line_ignore, NULL, NULL, 0 }, /* sentchar */
565: { roff_line_ignore, NULL, NULL, 0 }, /* shc */
1.211 schwarze 566: { roff_shift, NULL, NULL, 0 }, /* shift */
1.167 schwarze 567: { roff_line_ignore, NULL, NULL, 0 }, /* sizes */
568: { roff_so, NULL, NULL, 0 }, /* so */
569: { roff_line_ignore, NULL, NULL, 0 }, /* spacewidth */
570: { roff_line_ignore, NULL, NULL, 0 }, /* special */
571: { roff_line_ignore, NULL, NULL, 0 }, /* spreadwarn */
572: { roff_line_ignore, NULL, NULL, 0 }, /* ss */
573: { roff_line_ignore, NULL, NULL, 0 }, /* sty */
574: { roff_unsupp, NULL, NULL, 0 }, /* substring */
575: { roff_line_ignore, NULL, NULL, 0 }, /* sv */
576: { roff_insec, NULL, NULL, 0 }, /* sy */
577: { roff_T_, NULL, NULL, 0 }, /* T& */
578: { roff_unsupp, NULL, NULL, 0 }, /* tc */
579: { roff_TE, NULL, NULL, 0 }, /* TE */
1.187 schwarze 580: { roff_Dd, NULL, NULL, 0 }, /* TH */
1.167 schwarze 581: { roff_line_ignore, NULL, NULL, 0 }, /* tkf */
582: { roff_unsupp, NULL, NULL, 0 }, /* tl */
583: { roff_line_ignore, NULL, NULL, 0 }, /* tm */
584: { roff_line_ignore, NULL, NULL, 0 }, /* tm1 */
585: { roff_line_ignore, NULL, NULL, 0 }, /* tmc */
586: { roff_tr, NULL, NULL, 0 }, /* tr */
587: { roff_line_ignore, NULL, NULL, 0 }, /* track */
588: { roff_line_ignore, NULL, NULL, 0 }, /* transchar */
589: { roff_insec, NULL, NULL, 0 }, /* trf */
590: { roff_line_ignore, NULL, NULL, 0 }, /* trimat */
591: { roff_unsupp, NULL, NULL, 0 }, /* trin */
592: { roff_unsupp, NULL, NULL, 0 }, /* trnt */
593: { roff_line_ignore, NULL, NULL, 0 }, /* troff */
594: { roff_TS, NULL, NULL, 0 }, /* TS */
595: { roff_line_ignore, NULL, NULL, 0 }, /* uf */
596: { roff_line_ignore, NULL, NULL, 0 }, /* ul */
597: { roff_unsupp, NULL, NULL, 0 }, /* unformat */
598: { roff_line_ignore, NULL, NULL, 0 }, /* unwatch */
599: { roff_line_ignore, NULL, NULL, 0 }, /* unwatchn */
600: { roff_line_ignore, NULL, NULL, 0 }, /* vpt */
601: { roff_line_ignore, NULL, NULL, 0 }, /* vs */
602: { roff_line_ignore, NULL, NULL, 0 }, /* warn */
603: { roff_line_ignore, NULL, NULL, 0 }, /* warnscale */
604: { roff_line_ignore, NULL, NULL, 0 }, /* watch */
605: { roff_line_ignore, NULL, NULL, 0 }, /* watchlength */
606: { roff_line_ignore, NULL, NULL, 0 }, /* watchn */
607: { roff_unsupp, NULL, NULL, 0 }, /* wh */
1.212 schwarze 608: { roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT }, /*while*/
1.167 schwarze 609: { roff_insec, NULL, NULL, 0 }, /* write */
610: { roff_insec, NULL, NULL, 0 }, /* writec */
611: { roff_insec, NULL, NULL, 0 }, /* writem */
612: { roff_line_ignore, NULL, NULL, 0 }, /* xflag */
613: { roff_cblock, NULL, NULL, 0 }, /* . */
1.178 schwarze 614: { roff_renamed, NULL, NULL, 0 },
1.167 schwarze 615: { roff_userdef, NULL, NULL, 0 }
1.1 schwarze 616: };
617:
1.37 schwarze 618: /* Array of injected predefined strings. */
619: #define PREDEFS_MAX 38
620: static const struct predef predefs[PREDEFS_MAX] = {
621: #include "predefs.in"
622: };
623:
1.177 schwarze 624: static int roffce_lines; /* number of input lines to center */
625: static struct roff_node *roffce_node; /* active request */
1.51 schwarze 626: static int roffit_lines; /* number of lines to delay */
627: static char *roffit_macro; /* nil-terminated macro line */
628:
1.80 schwarze 629:
1.138 schwarze 630: /* --- request table ------------------------------------------------------ */
631:
1.167 schwarze 632: struct ohash *
633: roffhash_alloc(enum roff_tok mintok, enum roff_tok maxtok)
1.3 schwarze 634: {
1.167 schwarze 635: struct ohash *htab;
636: struct roffreq *req;
637: enum roff_tok tok;
638: size_t sz;
639: unsigned int slot;
1.3 schwarze 640:
1.167 schwarze 641: htab = mandoc_malloc(sizeof(*htab));
642: mandoc_ohash_init(htab, 8, offsetof(struct roffreq, name));
1.3 schwarze 643:
1.167 schwarze 644: for (tok = mintok; tok < maxtok; tok++) {
1.168 schwarze 645: if (roff_name[tok] == NULL)
646: continue;
1.167 schwarze 647: sz = strlen(roff_name[tok]);
648: req = mandoc_malloc(sizeof(*req) + sz + 1);
649: req->tok = tok;
650: memcpy(req->name, roff_name[tok], sz + 1);
651: slot = ohash_qlookup(htab, req->name);
652: ohash_insert(htab, slot, req);
1.3 schwarze 653: }
1.167 schwarze 654: return htab;
1.3 schwarze 655: }
656:
1.167 schwarze 657: void
658: roffhash_free(struct ohash *htab)
1.1 schwarze 659: {
1.167 schwarze 660: struct roffreq *req;
661: unsigned int slot;
1.1 schwarze 662:
1.167 schwarze 663: if (htab == NULL)
664: return;
665: for (req = ohash_first(htab, &slot); req != NULL;
666: req = ohash_next(htab, &slot))
667: free(req);
668: ohash_delete(htab);
669: free(htab);
670: }
671:
672: enum roff_tok
673: roffhash_find(struct ohash *htab, const char *name, size_t sz)
674: {
675: struct roffreq *req;
676: const char *end;
677:
678: if (sz) {
679: end = name + sz;
680: req = ohash_find(htab, ohash_qlookupi(htab, name, &end));
681: } else
682: req = ohash_find(htab, ohash_qlookup(htab, name));
683: return req == NULL ? TOKEN_NONE : req->tok;
1.1 schwarze 684: }
685:
1.138 schwarze 686: /* --- stack of request blocks -------------------------------------------- */
687:
1.1 schwarze 688: /*
689: * Pop the current node off of the stack of roff instructions currently
1.236 schwarze 690: * pending. Return 1 if it is a loop or 0 otherwise.
1.1 schwarze 691: */
1.212 schwarze 692: static int
1.1 schwarze 693: roffnode_pop(struct roff *r)
694: {
695: struct roffnode *p;
1.212 schwarze 696: int inloop;
1.1 schwarze 697:
1.80 schwarze 698: p = r->last;
1.212 schwarze 699: inloop = p->tok == ROFF_while;
700: r->last = p->parent;
1.16 schwarze 701: free(p->name);
702: free(p->end);
1.1 schwarze 703: free(p);
1.212 schwarze 704: return inloop;
1.1 schwarze 705: }
706:
707: /*
708: * Push a roff node onto the instruction stack. This must later be
709: * removed with roffnode_pop().
710: */
1.11 schwarze 711: static void
1.166 schwarze 712: roffnode_push(struct roff *r, enum roff_tok tok, const char *name,
1.16 schwarze 713: int line, int col)
1.1 schwarze 714: {
715: struct roffnode *p;
716:
1.11 schwarze 717: p = mandoc_calloc(1, sizeof(struct roffnode));
1.1 schwarze 718: p->tok = tok;
1.16 schwarze 719: if (name)
720: p->name = mandoc_strdup(name);
1.1 schwarze 721: p->parent = r->last;
722: p->line = line;
723: p->col = col;
1.71 schwarze 724: p->rule = p->parent ? p->parent->rule : 0;
1.1 schwarze 725:
726: r->last = p;
727: }
728:
1.138 schwarze 729: /* --- roff parser state data management ---------------------------------- */
730:
1.1 schwarze 731: static void
732: roff_free1(struct roff *r)
733: {
1.42 schwarze 734: int i;
1.27 schwarze 735:
1.218 schwarze 736: tbl_free(r->first_tbl);
1.27 schwarze 737: r->first_tbl = r->last_tbl = r->tbl = NULL;
1.1 schwarze 738:
1.219 schwarze 739: eqn_free(r->last_eqn);
1.191 schwarze 740: r->last_eqn = r->eqn = NULL;
1.32 schwarze 741:
1.211 schwarze 742: while (r->mstackpos >= 0)
743: roff_userret(r);
744:
1.1 schwarze 745: while (r->last)
746: roffnode_pop(r);
1.27 schwarze 747:
1.96 schwarze 748: free (r->rstack);
749: r->rstack = NULL;
750: r->rstacksz = 0;
751: r->rstackpos = -1;
752:
753: roff_freereg(r->regtab);
754: r->regtab = NULL;
755:
1.42 schwarze 756: roff_freestr(r->strtab);
1.178 schwarze 757: roff_freestr(r->rentab);
1.42 schwarze 758: roff_freestr(r->xmbtab);
1.178 schwarze 759: r->strtab = r->rentab = r->xmbtab = NULL;
1.42 schwarze 760:
761: if (r->xtab)
762: for (i = 0; i < 128; i++)
763: free(r->xtab[i].p);
764: free(r->xtab);
765: r->xtab = NULL;
1.1 schwarze 766: }
767:
768: void
769: roff_reset(struct roff *r)
770: {
771: roff_free1(r);
1.239 schwarze 772: r->options |= MPARSE_COMMENT;
1.99 schwarze 773: r->format = r->options & (MPARSE_MDOC | MPARSE_MAN);
1.175 schwarze 774: r->control = '\0';
775: r->escape = '\\';
1.179 schwarze 776: roffce_lines = 0;
777: roffce_node = NULL;
778: roffit_lines = 0;
779: roffit_macro = NULL;
1.1 schwarze 780: }
781:
782: void
783: roff_free(struct roff *r)
784: {
1.238 schwarze 785: int i;
1.211 schwarze 786:
1.1 schwarze 787: roff_free1(r);
1.211 schwarze 788: for (i = 0; i < r->mstacksz; i++)
789: free(r->mstack[i].argv);
790: free(r->mstack);
1.167 schwarze 791: roffhash_free(r->reqtab);
1.1 schwarze 792: free(r);
793: }
794:
795: struct roff *
1.223 schwarze 796: roff_alloc(int options)
1.1 schwarze 797: {
798: struct roff *r;
799:
1.11 schwarze 800: r = mandoc_calloc(1, sizeof(struct roff));
1.200 schwarze 801: r->reqtab = roffhash_alloc(0, ROFF_RENAMED);
1.239 schwarze 802: r->options = options | MPARSE_COMMENT;
1.99 schwarze 803: r->format = options & (MPARSE_MDOC | MPARSE_MAN);
1.211 schwarze 804: r->mstackpos = -1;
1.2 schwarze 805: r->rstackpos = -1;
1.175 schwarze 806: r->escape = '\\';
1.149 schwarze 807: return r;
1.137 schwarze 808: }
809:
1.138 schwarze 810: /* --- syntax tree state data management ---------------------------------- */
811:
1.137 schwarze 812: static void
813: roff_man_free1(struct roff_man *man)
814: {
1.228 schwarze 815: if (man->meta.first != NULL)
816: roff_node_delete(man, man->meta.first);
1.137 schwarze 817: free(man->meta.msec);
818: free(man->meta.vol);
819: free(man->meta.os);
820: free(man->meta.arch);
821: free(man->meta.title);
822: free(man->meta.name);
823: free(man->meta.date);
1.228 schwarze 824: free(man->meta.sodest);
1.137 schwarze 825: }
826:
1.229 schwarze 827: void
828: roff_state_reset(struct roff_man *man)
829: {
830: man->last = man->meta.first;
831: man->last_es = NULL;
832: man->flags = 0;
833: man->lastsec = man->lastnamed = SEC_NONE;
834: man->next = ROFF_NEXT_CHILD;
835: roff_setreg(man->roff, "nS", 0, '=');
836: }
837:
1.137 schwarze 838: static void
839: roff_man_alloc1(struct roff_man *man)
840: {
841: memset(&man->meta, 0, sizeof(man->meta));
1.228 schwarze 842: man->meta.first = mandoc_calloc(1, sizeof(*man->meta.first));
843: man->meta.first->type = ROFFT_ROOT;
844: man->meta.macroset = MACROSET_NONE;
1.229 schwarze 845: roff_state_reset(man);
1.137 schwarze 846: }
847:
848: void
849: roff_man_reset(struct roff_man *man)
850: {
851: roff_man_free1(man);
852: roff_man_alloc1(man);
853: }
854:
855: void
856: roff_man_free(struct roff_man *man)
857: {
858: roff_man_free1(man);
859: free(man);
860: }
861:
862: struct roff_man *
1.223 schwarze 863: roff_man_alloc(struct roff *roff, const char *os_s, int quick)
1.137 schwarze 864: {
865: struct roff_man *man;
866:
867: man = mandoc_calloc(1, sizeof(*man));
868: man->roff = roff;
1.188 schwarze 869: man->os_s = os_s;
1.137 schwarze 870: man->quick = quick;
871: roff_man_alloc1(man);
1.168 schwarze 872: roff->man = man;
1.149 schwarze 873: return man;
1.1 schwarze 874: }
875:
1.138 schwarze 876: /* --- syntax tree handling ----------------------------------------------- */
877:
878: struct roff_node *
879: roff_node_alloc(struct roff_man *man, int line, int pos,
880: enum roff_type type, int tok)
881: {
882: struct roff_node *n;
883:
884: n = mandoc_calloc(1, sizeof(*n));
885: n->line = line;
886: n->pos = pos;
887: n->tok = tok;
888: n->type = type;
889: n->sec = man->lastsec;
890:
891: if (man->flags & MDOC_SYNOPSIS)
1.157 schwarze 892: n->flags |= NODE_SYNPRETTY;
1.138 schwarze 893: else
1.157 schwarze 894: n->flags &= ~NODE_SYNPRETTY;
1.232 schwarze 895: if ((man->flags & (ROFF_NOFILL | ROFF_NONOFILL)) == ROFF_NOFILL)
1.231 schwarze 896: n->flags |= NODE_NOFILL;
897: else
898: n->flags &= ~NODE_NOFILL;
1.138 schwarze 899: if (man->flags & MDOC_NEWLINE)
1.157 schwarze 900: n->flags |= NODE_LINE;
1.138 schwarze 901: man->flags &= ~MDOC_NEWLINE;
902:
1.149 schwarze 903: return n;
1.138 schwarze 904: }
905:
906: void
907: roff_node_append(struct roff_man *man, struct roff_node *n)
908: {
909:
910: switch (man->next) {
911: case ROFF_NEXT_SIBLING:
1.153 schwarze 912: if (man->last->next != NULL) {
913: n->next = man->last->next;
914: man->last->next->prev = n;
915: } else
916: man->last->parent->last = n;
1.138 schwarze 917: man->last->next = n;
918: n->prev = man->last;
919: n->parent = man->last->parent;
920: break;
921: case ROFF_NEXT_CHILD:
1.159 schwarze 922: if (man->last->child != NULL) {
923: n->next = man->last->child;
924: man->last->child->prev = n;
925: } else
926: man->last->last = n;
1.138 schwarze 927: man->last->child = n;
928: n->parent = man->last;
929: break;
930: default:
931: abort();
932: }
1.154 schwarze 933: man->last = n;
1.138 schwarze 934:
935: switch (n->type) {
936: case ROFFT_HEAD:
937: n->parent->head = n;
938: break;
939: case ROFFT_BODY:
1.154 schwarze 940: if (n->end != ENDBODY_NOT)
941: return;
1.138 schwarze 942: n->parent->body = n;
943: break;
944: case ROFFT_TAIL:
945: n->parent->tail = n;
946: break;
947: default:
1.154 schwarze 948: return;
1.138 schwarze 949: }
1.154 schwarze 950:
951: /*
952: * Copy over the normalised-data pointer of our parent. Not
953: * everybody has one, but copying a null pointer is fine.
954: */
955:
956: n->norm = n->parent->norm;
957: assert(n->parent->type == ROFFT_BLOCK);
1.138 schwarze 958: }
959:
1.139 schwarze 960: void
961: roff_word_alloc(struct roff_man *man, int line, int pos, const char *word)
962: {
963: struct roff_node *n;
964:
965: n = roff_node_alloc(man, line, pos, ROFFT_TEXT, TOKEN_NONE);
966: n->string = roff_strdup(man->roff, word);
967: roff_node_append(man, n);
1.158 schwarze 968: n->flags |= NODE_VALID | NODE_ENDED;
1.139 schwarze 969: man->next = ROFF_NEXT_SIBLING;
970: }
971:
972: void
973: roff_word_append(struct roff_man *man, const char *word)
974: {
975: struct roff_node *n;
976: char *addstr, *newstr;
977:
978: n = man->last;
979: addstr = roff_strdup(man->roff, word);
980: mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
981: free(addstr);
982: free(n->string);
983: n->string = newstr;
984: man->next = ROFF_NEXT_SIBLING;
1.140 schwarze 985: }
986:
987: void
988: roff_elem_alloc(struct roff_man *man, int line, int pos, int tok)
989: {
990: struct roff_node *n;
991:
992: n = roff_node_alloc(man, line, pos, ROFFT_ELEM, tok);
993: roff_node_append(man, n);
994: man->next = ROFF_NEXT_CHILD;
995: }
996:
997: struct roff_node *
998: roff_block_alloc(struct roff_man *man, int line, int pos, int tok)
999: {
1000: struct roff_node *n;
1001:
1002: n = roff_node_alloc(man, line, pos, ROFFT_BLOCK, tok);
1003: roff_node_append(man, n);
1004: man->next = ROFF_NEXT_CHILD;
1.149 schwarze 1005: return n;
1.139 schwarze 1006: }
1007:
1.138 schwarze 1008: struct roff_node *
1009: roff_head_alloc(struct roff_man *man, int line, int pos, int tok)
1010: {
1011: struct roff_node *n;
1012:
1013: n = roff_node_alloc(man, line, pos, ROFFT_HEAD, tok);
1014: roff_node_append(man, n);
1015: man->next = ROFF_NEXT_CHILD;
1.149 schwarze 1016: return n;
1.138 schwarze 1017: }
1018:
1019: struct roff_node *
1020: roff_body_alloc(struct roff_man *man, int line, int pos, int tok)
1021: {
1022: struct roff_node *n;
1023:
1024: n = roff_node_alloc(man, line, pos, ROFFT_BODY, tok);
1025: roff_node_append(man, n);
1026: man->next = ROFF_NEXT_CHILD;
1.149 schwarze 1027: return n;
1.139 schwarze 1028: }
1029:
1.193 schwarze 1030: static void
1.218 schwarze 1031: roff_addtbl(struct roff_man *man, int line, struct tbl_node *tbl)
1.139 schwarze 1032: {
1033: struct roff_node *n;
1.218 schwarze 1034: struct tbl_span *span;
1.139 schwarze 1035:
1.228 schwarze 1036: if (man->meta.macroset == MACROSET_MAN)
1.174 schwarze 1037: man_breakscope(man, ROFF_TS);
1.193 schwarze 1038: while ((span = tbl_span(tbl)) != NULL) {
1.218 schwarze 1039: n = roff_node_alloc(man, line, 0, ROFFT_TBL, TOKEN_NONE);
1.193 schwarze 1040: n->span = span;
1041: roff_node_append(man, n);
1042: n->flags |= NODE_VALID | NODE_ENDED;
1043: man->next = ROFF_NEXT_SIBLING;
1044: }
1.138 schwarze 1045: }
1046:
1047: void
1048: roff_node_unlink(struct roff_man *man, struct roff_node *n)
1049: {
1050:
1051: /* Adjust siblings. */
1052:
1053: if (n->prev)
1054: n->prev->next = n->next;
1055: if (n->next)
1056: n->next->prev = n->prev;
1057:
1058: /* Adjust parent. */
1059:
1060: if (n->parent != NULL) {
1061: if (n->parent->child == n)
1062: n->parent->child = n->next;
1063: if (n->parent->last == n)
1064: n->parent->last = n->prev;
1065: }
1066:
1067: /* Adjust parse point. */
1068:
1069: if (man == NULL)
1070: return;
1071: if (man->last == n) {
1072: if (n->prev == NULL) {
1073: man->last = n->parent;
1074: man->next = ROFF_NEXT_CHILD;
1075: } else {
1076: man->last = n->prev;
1077: man->next = ROFF_NEXT_SIBLING;
1078: }
1079: }
1.228 schwarze 1080: if (man->meta.first == n)
1081: man->meta.first = NULL;
1.216 schwarze 1082: }
1083:
1084: void
1085: roff_node_relink(struct roff_man *man, struct roff_node *n)
1086: {
1087: roff_node_unlink(man, n);
1088: n->prev = n->next = NULL;
1089: roff_node_append(man, n);
1.138 schwarze 1090: }
1091:
1092: void
1093: roff_node_free(struct roff_node *n)
1094: {
1095:
1096: if (n->args != NULL)
1097: mdoc_argv_free(n->args);
1098: if (n->type == ROFFT_BLOCK || n->type == ROFFT_ELEM)
1099: free(n->norm);
1.219 schwarze 1100: eqn_box_free(n->eqn);
1.138 schwarze 1101: free(n->string);
1102: free(n);
1103: }
1104:
1105: void
1106: roff_node_delete(struct roff_man *man, struct roff_node *n)
1107: {
1108:
1109: while (n->child != NULL)
1110: roff_node_delete(man, n->child);
1111: roff_node_unlink(man, n);
1112: roff_node_free(n);
1.141 schwarze 1113: }
1114:
1115: void
1116: deroff(char **dest, const struct roff_node *n)
1117: {
1118: char *cp;
1119: size_t sz;
1120:
1121: if (n->type != ROFFT_TEXT) {
1122: for (n = n->child; n != NULL; n = n->next)
1123: deroff(dest, n);
1124: return;
1125: }
1126:
1.160 schwarze 1127: /* Skip leading whitespace. */
1.141 schwarze 1128:
1.160 schwarze 1129: for (cp = n->string; *cp != '\0'; cp++) {
1.161 schwarze 1130: if (cp[0] == '\\' && cp[1] != '\0' &&
1131: strchr(" %&0^|~", cp[1]) != NULL)
1.141 schwarze 1132: cp++;
1.160 schwarze 1133: else if ( ! isspace((unsigned char)*cp))
1.141 schwarze 1134: break;
1135: }
1136:
1.161 schwarze 1137: /* Skip trailing backslash. */
1138:
1139: sz = strlen(cp);
1.162 schwarze 1140: if (sz > 0 && cp[sz - 1] == '\\')
1.161 schwarze 1141: sz--;
1142:
1.141 schwarze 1143: /* Skip trailing whitespace. */
1144:
1.161 schwarze 1145: for (; sz; sz--)
1.141 schwarze 1146: if ( ! isspace((unsigned char)cp[sz-1]))
1147: break;
1148:
1149: /* Skip empty strings. */
1150:
1151: if (sz == 0)
1152: return;
1153:
1154: if (*dest == NULL) {
1155: *dest = mandoc_strndup(cp, sz);
1156: return;
1157: }
1158:
1159: mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
1160: free(*dest);
1161: *dest = cp;
1.138 schwarze 1162: }
1163:
1164: /* --- main functions of the roff parser ---------------------------------- */
1165:
1.8 schwarze 1166: /*
1.227 schwarze 1167: * In the current line, expand escape sequences that produce parsable
1168: * input text. Also check the syntax of the remaining escape sequences,
1169: * which typically produce output glyphs or change formatter state.
1.8 schwarze 1170: */
1.212 schwarze 1171: static int
1.227 schwarze 1172: roff_expand(struct roff *r, struct buf *buf, int ln, int pos, char newesc)
1.8 schwarze 1173: {
1.211 schwarze 1174: struct mctx *ctx; /* current macro call context */
1.81 schwarze 1175: char ubuf[24]; /* buffer to print the number */
1.200 schwarze 1176: struct roff_node *n; /* used for header comments */
1.78 schwarze 1177: const char *start; /* start of the string to process */
1.82 schwarze 1178: char *stesc; /* start of an escape sequence ('\\') */
1.224 schwarze 1179: const char *esct; /* type of esccape sequence */
1.200 schwarze 1180: char *ep; /* end of comment string */
1.23 schwarze 1181: const char *stnam; /* start of the name, after "[(*" */
1182: const char *cp; /* end of the name, e.g. before ']' */
1183: const char *res; /* the string to be substituted */
1.110 schwarze 1184: char *nbuf; /* new buffer to copy buf->buf to */
1.53 schwarze 1185: size_t maxl; /* expected length of the escape name */
1186: size_t naml; /* actual length of the escape name */
1.211 schwarze 1187: size_t asz; /* length of the replacement */
1188: size_t rsz; /* length of the rest of the string */
1.109 schwarze 1189: int inaml; /* length returned from mandoc_escape() */
1.53 schwarze 1190: int expand_count; /* to avoid infinite loops */
1.79 schwarze 1191: int npos; /* position in numeric expression */
1.91 schwarze 1192: int arg_complete; /* argument not interrupted by eol */
1.211 schwarze 1193: int quote_args; /* true for \\$@, false for \\$* */
1.175 schwarze 1194: int done; /* no more input available */
1.187 schwarze 1195: int deftype; /* type of definition to paste */
1.195 schwarze 1196: int rcsid; /* kind of RCS id seen */
1.224 schwarze 1197: enum mandocerr err; /* for escape sequence problems */
1.199 schwarze 1198: char sign; /* increment number register */
1.79 schwarze 1199: char term; /* character terminating the escape */
1.8 schwarze 1200:
1.175 schwarze 1201: /* Search forward for comments. */
1202:
1203: done = 0;
1204: start = buf->buf + pos;
1205: for (stesc = buf->buf + pos; *stesc != '\0'; stesc++) {
1.227 schwarze 1206: if (stesc[0] != newesc || stesc[1] == '\0')
1.175 schwarze 1207: continue;
1208: stesc++;
1209: if (*stesc != '"' && *stesc != '#')
1210: continue;
1.185 schwarze 1211:
1212: /* Comment found, look for RCS id. */
1213:
1.195 schwarze 1214: rcsid = 0;
1.185 schwarze 1215: if ((cp = strstr(stesc, "$" "OpenBSD")) != NULL) {
1.195 schwarze 1216: rcsid = 1 << MANDOC_OS_OPENBSD;
1.185 schwarze 1217: cp += 8;
1218: } else if ((cp = strstr(stesc, "$" "NetBSD")) != NULL) {
1.195 schwarze 1219: rcsid = 1 << MANDOC_OS_NETBSD;
1.185 schwarze 1220: cp += 7;
1221: }
1222: if (cp != NULL &&
1223: isalnum((unsigned char)*cp) == 0 &&
1.186 schwarze 1224: strchr(cp, '$') != NULL) {
1.195 schwarze 1225: if (r->man->meta.rcsids & rcsid)
1.222 schwarze 1226: mandoc_msg(MANDOCERR_RCS_REP, ln,
1227: (int)(stesc - buf->buf) + 1,
1228: "%s", stesc + 1);
1.195 schwarze 1229: r->man->meta.rcsids |= rcsid;
1.186 schwarze 1230: }
1.185 schwarze 1231:
1232: /* Handle trailing whitespace. */
1233:
1.200 schwarze 1234: ep = strchr(stesc--, '\0') - 1;
1235: if (*ep == '\n') {
1.175 schwarze 1236: done = 1;
1.200 schwarze 1237: ep--;
1.175 schwarze 1238: }
1.200 schwarze 1239: if (*ep == ' ' || *ep == '\t')
1.222 schwarze 1240: mandoc_msg(MANDOCERR_SPACE_EOL,
1241: ln, (int)(ep - buf->buf), NULL);
1.200 schwarze 1242:
1243: /*
1244: * Save comments preceding the title macro
1245: * in the syntax tree.
1246: */
1247:
1.239 schwarze 1248: if (newesc != ASCII_ESC && r->options & MPARSE_COMMENT) {
1.200 schwarze 1249: while (*ep == ' ' || *ep == '\t')
1250: ep--;
1251: ep[1] = '\0';
1252: n = roff_node_alloc(r->man,
1253: ln, stesc + 1 - buf->buf,
1254: ROFFT_COMMENT, TOKEN_NONE);
1255: n->string = mandoc_strdup(stesc + 2);
1256: roff_node_append(r->man, n);
1257: n->flags |= NODE_VALID | NODE_ENDED;
1258: r->man->next = ROFF_NEXT_SIBLING;
1259: }
1260:
1.206 schwarze 1261: /* Line continuation with comment. */
1262:
1263: if (stesc[1] == '#') {
1264: *stesc = '\0';
1.212 schwarze 1265: return ROFF_IGN | ROFF_APPEND;
1.206 schwarze 1266: }
1267:
1268: /* Discard normal comments. */
1.200 schwarze 1269:
1.207 schwarze 1270: while (stesc > start && stesc[-1] == ' ' &&
1271: (stesc == start + 1 || stesc[-2] != '\\'))
1.175 schwarze 1272: stesc--;
1273: *stesc = '\0';
1274: break;
1275: }
1276: if (stesc == start)
1277: return ROFF_CONT;
1278: stesc--;
1279:
1280: /* Notice the end of the input. */
1281:
1282: if (*stesc == '\n') {
1283: *stesc-- = '\0';
1284: done = 1;
1285: }
1286:
1.43 schwarze 1287: expand_count = 0;
1.175 schwarze 1288: while (stesc >= start) {
1.227 schwarze 1289: if (*stesc != newesc) {
1.43 schwarze 1290:
1.227 schwarze 1291: /*
1292: * If we have a non-standard escape character,
1293: * escape literal backslashes because all
1294: * processing in subsequent functions uses
1295: * the standard escaping rules.
1296: */
1.78 schwarze 1297:
1.227 schwarze 1298: if (newesc != ASCII_ESC && *stesc == '\\') {
1.175 schwarze 1299: *stesc = '\0';
1300: buf->sz = mandoc_asprintf(&nbuf, "%s\\e%s",
1301: buf->buf, stesc + 1) + 1;
1302: start = nbuf + pos;
1303: stesc = nbuf + (stesc - buf->buf);
1304: free(buf->buf);
1305: buf->buf = nbuf;
1306: }
1.227 schwarze 1307:
1308: /* Search backwards for the next escape. */
1309:
1.175 schwarze 1310: stesc--;
1.78 schwarze 1311: continue;
1.175 schwarze 1312: }
1.78 schwarze 1313:
1314: /* If it is escaped, skip it. */
1315:
1316: for (cp = stesc - 1; cp >= start; cp--)
1.175 schwarze 1317: if (*cp != r->escape)
1.78 schwarze 1318: break;
1319:
1.110 schwarze 1320: if ((stesc - cp) % 2 == 0) {
1.175 schwarze 1321: while (stesc > cp)
1322: *stesc-- = '\\';
1.78 schwarze 1323: continue;
1.175 schwarze 1324: } else if (stesc[1] != '\0') {
1325: *stesc = '\\';
1326: } else {
1327: *stesc-- = '\0';
1328: if (done)
1329: continue;
1330: else
1.212 schwarze 1331: return ROFF_IGN | ROFF_APPEND;
1.78 schwarze 1332: }
1.23 schwarze 1333:
1.79 schwarze 1334: /* Decide whether to expand or to check only. */
1.23 schwarze 1335:
1.79 schwarze 1336: term = '\0';
1.78 schwarze 1337: cp = stesc + 1;
1.224 schwarze 1338: if (*cp == 'E')
1339: cp++;
1340: esct = cp;
1341: switch (*esct) {
1.80 schwarze 1342: case '*':
1.211 schwarze 1343: case '$':
1.53 schwarze 1344: res = NULL;
1345: break;
1.80 schwarze 1346: case 'B':
1347: case 'w':
1.79 schwarze 1348: term = cp[1];
1349: /* FALLTHROUGH */
1.80 schwarze 1350: case 'n':
1.199 schwarze 1351: sign = cp[1];
1352: if (sign == '+' || sign == '-')
1353: cp++;
1.53 schwarze 1354: res = ubuf;
1355: break;
1356: default:
1.224 schwarze 1357: err = MANDOCERR_OK;
1358: switch(mandoc_escape(&cp, &stnam, &inaml)) {
1359: case ESCAPE_SPECIAL:
1360: if (mchars_spec2cp(stnam, inaml) >= 0)
1361: break;
1362: /* FALLTHROUGH */
1363: case ESCAPE_ERROR:
1364: err = MANDOCERR_ESC_BAD;
1365: break;
1366: case ESCAPE_UNDEF:
1367: err = MANDOCERR_ESC_UNDEF;
1368: break;
1369: case ESCAPE_UNSUPP:
1370: err = MANDOCERR_ESC_UNSUPP;
1371: break;
1372: default:
1373: break;
1374: }
1375: if (err != MANDOCERR_OK)
1376: mandoc_msg(err, ln, (int)(stesc - buf->buf),
1.92 schwarze 1377: "%.*s", (int)(cp - stesc), stesc);
1.175 schwarze 1378: stesc--;
1.78 schwarze 1379: continue;
1.42 schwarze 1380: }
1381:
1.78 schwarze 1382: if (EXPAND_LIMIT < ++expand_count) {
1.222 schwarze 1383: mandoc_msg(MANDOCERR_ROFFLOOP,
1.110 schwarze 1384: ln, (int)(stesc - buf->buf), NULL);
1.149 schwarze 1385: return ROFF_IGN;
1.78 schwarze 1386: }
1.23 schwarze 1387:
1388: /*
1389: * The third character decides the length
1.53 schwarze 1390: * of the name of the string or register.
1.23 schwarze 1391: * Save a pointer to the name.
1392: */
1393:
1.110 schwarze 1394: if (term == '\0') {
1.79 schwarze 1395: switch (*++cp) {
1.80 schwarze 1396: case '\0':
1.79 schwarze 1397: maxl = 0;
1398: break;
1.80 schwarze 1399: case '(':
1.79 schwarze 1400: cp++;
1401: maxl = 2;
1402: break;
1.80 schwarze 1403: case '[':
1.79 schwarze 1404: cp++;
1405: term = ']';
1406: maxl = 0;
1407: break;
1408: default:
1409: maxl = 1;
1410: break;
1411: }
1412: } else {
1413: cp += 2;
1.8 schwarze 1414: maxl = 0;
1415: }
1.23 schwarze 1416: stnam = cp;
1.8 schwarze 1417:
1.23 schwarze 1418: /* Advance to the end of the name. */
1.8 schwarze 1419:
1.125 schwarze 1420: naml = 0;
1.91 schwarze 1421: arg_complete = 1;
1.125 schwarze 1422: while (maxl == 0 || naml < maxl) {
1.110 schwarze 1423: if (*cp == '\0') {
1.222 schwarze 1424: mandoc_msg(MANDOCERR_ESC_BAD, ln,
1425: (int)(stesc - buf->buf), "%s", stesc);
1.91 schwarze 1426: arg_complete = 0;
1.79 schwarze 1427: break;
1.42 schwarze 1428: }
1.110 schwarze 1429: if (maxl == 0 && *cp == term) {
1.79 schwarze 1430: cp++;
1.125 schwarze 1431: break;
1432: }
1.224 schwarze 1433: if (*cp++ != '\\' || *esct != 'w') {
1.125 schwarze 1434: naml++;
1435: continue;
1436: }
1437: switch (mandoc_escape(&cp, NULL, NULL)) {
1438: case ESCAPE_SPECIAL:
1439: case ESCAPE_UNICODE:
1440: case ESCAPE_NUMBERED:
1.224 schwarze 1441: case ESCAPE_UNDEF:
1.125 schwarze 1442: case ESCAPE_OVERSTRIKE:
1443: naml++;
1444: break;
1445: default:
1.8 schwarze 1446: break;
1.79 schwarze 1447: }
1.8 schwarze 1448: }
1449:
1.23 schwarze 1450: /*
1451: * Retrieve the replacement string; if it is
1452: * undefined, resume searching for escapes.
1453: */
1454:
1.224 schwarze 1455: switch (*esct) {
1.80 schwarze 1456: case '*':
1.187 schwarze 1457: if (arg_complete) {
1458: deftype = ROFFDEF_USER | ROFFDEF_PRE;
1459: res = roff_getstrn(r, stnam, naml, &deftype);
1.203 schwarze 1460:
1461: /*
1462: * If not overriden, let \*(.T
1463: * through to the formatters.
1464: */
1465:
1466: if (res == NULL && naml == 2 &&
1467: stnam[0] == '.' && stnam[1] == 'T') {
1468: roff_setstrn(&r->strtab,
1469: ".T", 2, NULL, 0, 0);
1470: stesc--;
1471: continue;
1472: }
1.187 schwarze 1473: }
1.79 schwarze 1474: break;
1.211 schwarze 1475: case '$':
1476: if (r->mstackpos < 0) {
1.222 schwarze 1477: mandoc_msg(MANDOCERR_ARG_UNDEF, ln,
1478: (int)(stesc - buf->buf), "%.3s", stesc);
1.211 schwarze 1479: break;
1480: }
1481: ctx = r->mstack + r->mstackpos;
1.224 schwarze 1482: npos = esct[1] - '1';
1.211 schwarze 1483: if (npos >= 0 && npos <= 8) {
1484: res = npos < ctx->argc ?
1485: ctx->argv[npos] : "";
1486: break;
1487: }
1.224 schwarze 1488: if (esct[1] == '*')
1.211 schwarze 1489: quote_args = 0;
1.224 schwarze 1490: else if (esct[1] == '@')
1.211 schwarze 1491: quote_args = 1;
1492: else {
1.222 schwarze 1493: mandoc_msg(MANDOCERR_ARG_NONUM, ln,
1494: (int)(stesc - buf->buf), "%.3s", stesc);
1.211 schwarze 1495: break;
1496: }
1497: asz = 0;
1498: for (npos = 0; npos < ctx->argc; npos++) {
1499: if (npos)
1500: asz++; /* blank */
1501: if (quote_args)
1502: asz += 2; /* quotes */
1503: asz += strlen(ctx->argv[npos]);
1504: }
1505: if (asz != 3) {
1506: rsz = buf->sz - (stesc - buf->buf) - 3;
1507: if (asz < 3)
1508: memmove(stesc + asz, stesc + 3, rsz);
1509: buf->sz += asz - 3;
1510: nbuf = mandoc_realloc(buf->buf, buf->sz);
1511: start = nbuf + pos;
1512: stesc = nbuf + (stesc - buf->buf);
1513: buf->buf = nbuf;
1514: if (asz > 3)
1515: memmove(stesc + asz, stesc + 3, rsz);
1516: }
1517: for (npos = 0; npos < ctx->argc; npos++) {
1518: if (npos)
1519: *stesc++ = ' ';
1520: if (quote_args)
1521: *stesc++ = '"';
1522: cp = ctx->argv[npos];
1523: while (*cp != '\0')
1524: *stesc++ = *cp++;
1525: if (quote_args)
1526: *stesc++ = '"';
1527: }
1528: continue;
1.80 schwarze 1529: case 'B':
1.79 schwarze 1530: npos = 0;
1.91 schwarze 1531: ubuf[0] = arg_complete &&
1.133 schwarze 1532: roff_evalnum(r, ln, stnam, &npos,
1533: NULL, ROFFNUM_SCALE) &&
1.91 schwarze 1534: stnam + npos + 1 == cp ? '1' : '0';
1.79 schwarze 1535: ubuf[1] = '\0';
1536: break;
1.80 schwarze 1537: case 'n':
1.91 schwarze 1538: if (arg_complete)
1539: (void)snprintf(ubuf, sizeof(ubuf), "%d",
1.199 schwarze 1540: roff_getregn(r, stnam, naml, sign));
1.91 schwarze 1541: else
1542: ubuf[0] = '\0';
1.79 schwarze 1543: break;
1.80 schwarze 1544: case 'w':
1.91 schwarze 1545: /* use even incomplete args */
1.81 schwarze 1546: (void)snprintf(ubuf, sizeof(ubuf), "%d",
1.79 schwarze 1547: 24 * (int)naml);
1548: break;
1549: }
1.8 schwarze 1550:
1.110 schwarze 1551: if (res == NULL) {
1.224 schwarze 1552: if (*esct == '*')
1.222 schwarze 1553: mandoc_msg(MANDOCERR_STR_UNDEF,
1554: ln, (int)(stesc - buf->buf),
1.211 schwarze 1555: "%.*s", (int)naml, stnam);
1.37 schwarze 1556: res = "";
1.118 schwarze 1557: } else if (buf->sz + strlen(res) > SHRT_MAX) {
1.222 schwarze 1558: mandoc_msg(MANDOCERR_ROFFLOOP,
1.118 schwarze 1559: ln, (int)(stesc - buf->buf), NULL);
1.149 schwarze 1560: return ROFF_IGN;
1.8 schwarze 1561: }
1562:
1.23 schwarze 1563: /* Replace the escape sequence by the string. */
1564:
1.82 schwarze 1565: *stesc = '\0';
1.110 schwarze 1566: buf->sz = mandoc_asprintf(&nbuf, "%s%s%s",
1567: buf->buf, res, cp) + 1;
1.8 schwarze 1568:
1.78 schwarze 1569: /* Prepare for the next replacement. */
1.8 schwarze 1570:
1.78 schwarze 1571: start = nbuf + pos;
1.110 schwarze 1572: stesc = nbuf + (stesc - buf->buf) + strlen(res);
1573: free(buf->buf);
1574: buf->buf = nbuf;
1.42 schwarze 1575: }
1.149 schwarze 1576: return ROFF_CONT;
1.42 schwarze 1577: }
1.225 schwarze 1578:
1579: /*
1580: * Parse a quoted or unquoted roff-style request or macro argument.
1581: * Return a pointer to the parsed argument, which is either the original
1582: * pointer or advanced by one byte in case the argument is quoted.
1583: * NUL-terminate the argument in place.
1584: * Collapse pairs of quotes inside quoted arguments.
1585: * Advance the argument pointer to the next argument,
1586: * or to the NUL byte terminating the argument line.
1587: */
1588: char *
1.227 schwarze 1589: roff_getarg(struct roff *r, char **cpp, int ln, int *pos)
1.225 schwarze 1590: {
1.227 schwarze 1591: struct buf buf;
1.238 schwarze 1592: char *cp, *start;
1.227 schwarze 1593: int newesc, pairs, quoted, white;
1.225 schwarze 1594:
1595: /* Quoting can only start with a new word. */
1596: start = *cpp;
1597: quoted = 0;
1598: if ('"' == *start) {
1599: quoted = 1;
1600: start++;
1601: }
1602:
1.227 schwarze 1603: newesc = pairs = white = 0;
1.225 schwarze 1604: for (cp = start; '\0' != *cp; cp++) {
1605:
1606: /*
1607: * Move the following text left
1608: * after quoted quotes and after "\\" and "\t".
1609: */
1610: if (pairs)
1611: cp[-pairs] = cp[0];
1612:
1613: if ('\\' == cp[0]) {
1614: /*
1615: * In copy mode, translate double to single
1616: * backslashes and backslash-t to literal tabs.
1617: */
1618: switch (cp[1]) {
1619: case 'a':
1620: case 't':
1.226 schwarze 1621: cp[-pairs] = '\t';
1.227 schwarze 1622: pairs++;
1623: cp++;
1624: break;
1.225 schwarze 1625: case '\\':
1.227 schwarze 1626: newesc = 1;
1627: cp[-pairs] = ASCII_ESC;
1.225 schwarze 1628: pairs++;
1629: cp++;
1630: break;
1631: case ' ':
1632: /* Skip escaped blanks. */
1633: if (0 == quoted)
1634: cp++;
1635: break;
1636: default:
1637: break;
1638: }
1639: } else if (0 == quoted) {
1640: if (' ' == cp[0]) {
1641: /* Unescaped blanks end unquoted args. */
1642: white = 1;
1643: break;
1644: }
1645: } else if ('"' == cp[0]) {
1646: if ('"' == cp[1]) {
1647: /* Quoted quotes collapse. */
1648: pairs++;
1649: cp++;
1650: } else {
1651: /* Unquoted quotes end quoted args. */
1652: quoted = 2;
1653: break;
1654: }
1655: }
1656: }
1657:
1658: /* Quoted argument without a closing quote. */
1659: if (1 == quoted)
1660: mandoc_msg(MANDOCERR_ARG_QUOTE, ln, *pos, NULL);
1661:
1662: /* NUL-terminate this argument and move to the next one. */
1663: if (pairs)
1664: cp[-pairs] = '\0';
1665: if ('\0' != *cp) {
1666: *cp++ = '\0';
1667: while (' ' == *cp)
1668: cp++;
1669: }
1670: *pos += (int)(cp - start) + (quoted ? 1 : 0);
1671: *cpp = cp;
1672:
1673: if ('\0' == *cp && (white || ' ' == cp[-1]))
1674: mandoc_msg(MANDOCERR_SPACE_EOL, ln, *pos, NULL);
1675:
1.227 schwarze 1676: start = mandoc_strdup(start);
1677: if (newesc == 0)
1678: return start;
1679:
1680: buf.buf = start;
1681: buf.sz = strlen(start) + 1;
1682: buf.next = NULL;
1683: if (roff_expand(r, &buf, ln, 0, ASCII_ESC) & ROFF_IGN) {
1684: free(buf.buf);
1685: buf.buf = mandoc_strdup("");
1686: }
1687: return buf.buf;
1.225 schwarze 1688: }
1689:
1.42 schwarze 1690:
1691: /*
1.147 schwarze 1692: * Process text streams.
1.42 schwarze 1693: */
1.212 schwarze 1694: static int
1.177 schwarze 1695: roff_parsetext(struct roff *r, struct buf *buf, int pos, int *offs)
1.42 schwarze 1696: {
1697: size_t sz;
1698: const char *start;
1.51 schwarze 1699: char *p;
1700: int isz;
1.42 schwarze 1701: enum mandoc_esc esc;
1702:
1.147 schwarze 1703: /* Spring the input line trap. */
1704:
1705: if (roffit_lines == 1) {
1706: isz = mandoc_asprintf(&p, "%s\n.%s", buf->buf, roffit_macro);
1707: free(buf->buf);
1708: buf->buf = p;
1709: buf->sz = isz + 1;
1710: *offs = 0;
1711: free(roffit_macro);
1712: roffit_lines = 0;
1.149 schwarze 1713: return ROFF_REPARSE;
1.147 schwarze 1714: } else if (roffit_lines > 1)
1715: --roffit_lines;
1716:
1.177 schwarze 1717: if (roffce_node != NULL && buf->buf[pos] != '\0') {
1718: if (roffce_lines < 1) {
1719: r->man->last = roffce_node;
1720: r->man->next = ROFF_NEXT_SIBLING;
1721: roffce_lines = 0;
1722: roffce_node = NULL;
1723: } else
1724: roffce_lines--;
1725: }
1726:
1.147 schwarze 1727: /* Convert all breakable hyphens into ASCII_HYPH. */
1728:
1.110 schwarze 1729: start = p = buf->buf + pos;
1.42 schwarze 1730:
1.110 schwarze 1731: while (*p != '\0') {
1.42 schwarze 1732: sz = strcspn(p, "-\\");
1733: p += sz;
1734:
1.110 schwarze 1735: if (*p == '\0')
1.42 schwarze 1736: break;
1737:
1.110 schwarze 1738: if (*p == '\\') {
1.42 schwarze 1739: /* Skip over escapes. */
1740: p++;
1.62 schwarze 1741: esc = mandoc_escape((const char **)&p, NULL, NULL);
1.110 schwarze 1742: if (esc == ESCAPE_ERROR)
1.42 schwarze 1743: break;
1.136 schwarze 1744: while (*p == '-')
1745: p++;
1.42 schwarze 1746: continue;
1747: } else if (p == start) {
1748: p++;
1749: continue;
1750: }
1751:
1.44 schwarze 1752: if (isalpha((unsigned char)p[-1]) &&
1753: isalpha((unsigned char)p[1]))
1.42 schwarze 1754: *p = ASCII_HYPH;
1755: p++;
1.8 schwarze 1756: }
1.149 schwarze 1757: return ROFF_CONT;
1.8 schwarze 1758: }
1759:
1.212 schwarze 1760: int
1.110 schwarze 1761: roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs)
1.1 schwarze 1762: {
1.166 schwarze 1763: enum roff_tok t;
1.212 schwarze 1764: int e;
1.110 schwarze 1765: int pos; /* parse point */
1.115 schwarze 1766: int spos; /* saved parse point for messages */
1.110 schwarze 1767: int ppos; /* original offset in buf->buf */
1768: int ctl; /* macro line (boolean) */
1769:
1770: ppos = pos = *offs;
1.1 schwarze 1771:
1.102 schwarze 1772: /* Handle in-line equation delimiters. */
1773:
1.108 schwarze 1774: if (r->tbl == NULL &&
1775: r->last_eqn != NULL && r->last_eqn->delim &&
1.102 schwarze 1776: (r->eqn == NULL || r->eqn_inline)) {
1.110 schwarze 1777: e = roff_eqndelim(r, buf, pos);
1.102 schwarze 1778: if (e == ROFF_REPARSE)
1.149 schwarze 1779: return e;
1.102 schwarze 1780: assert(e == ROFF_CONT);
1781: }
1782:
1783: /* Expand some escape sequences. */
1.8 schwarze 1784:
1.227 schwarze 1785: e = roff_expand(r, buf, ln, pos, r->escape);
1.212 schwarze 1786: if ((e & ROFF_MASK) == ROFF_IGN)
1.149 schwarze 1787: return e;
1.110 schwarze 1788: assert(e == ROFF_CONT);
1.8 schwarze 1789:
1.110 schwarze 1790: ctl = roff_getcontrol(r, buf->buf, &pos);
1.35 schwarze 1791:
1.8 schwarze 1792: /*
1.2 schwarze 1793: * First, if a scope is open and we're not a macro, pass the
1.124 schwarze 1794: * text through the macro's filter.
1795: * Equations process all content themselves.
1796: * Tables process almost all content themselves, but we want
1797: * to warn about macros before passing it there.
1.2 schwarze 1798: */
1799:
1.124 schwarze 1800: if (r->last != NULL && ! ctl) {
1.2 schwarze 1801: t = r->last->tok;
1.110 schwarze 1802: e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
1.212 schwarze 1803: if ((e & ROFF_MASK) == ROFF_IGN)
1.149 schwarze 1804: return e;
1.212 schwarze 1805: e &= ~ROFF_MASK;
1806: } else
1807: e = ROFF_IGN;
1.191 schwarze 1808: if (r->eqn != NULL && strncmp(buf->buf + ppos, ".EN", 3)) {
1809: eqn_read(r->eqn, buf->buf + ppos);
1.212 schwarze 1810: return e;
1.191 schwarze 1811: }
1.193 schwarze 1812: if (r->tbl != NULL && (ctl == 0 || buf->buf[pos] == '\0')) {
1813: tbl_read(r->tbl, ln, buf->buf, ppos);
1.218 schwarze 1814: roff_addtbl(r->man, ln, r->tbl);
1.212 schwarze 1815: return e;
1.193 schwarze 1816: }
1.239 schwarze 1817: if ( ! ctl) {
1818: r->options &= ~MPARSE_COMMENT;
1.212 schwarze 1819: return roff_parsetext(r, buf, pos, offs) | e;
1.239 schwarze 1820: }
1.100 schwarze 1821:
1822: /* Skip empty request lines. */
1823:
1.110 schwarze 1824: if (buf->buf[pos] == '"') {
1.222 schwarze 1825: mandoc_msg(MANDOCERR_COMMENT_BAD, ln, pos, NULL);
1.149 schwarze 1826: return ROFF_IGN;
1.110 schwarze 1827: } else if (buf->buf[pos] == '\0')
1.149 schwarze 1828: return ROFF_IGN;
1.2 schwarze 1829:
1830: /*
1831: * If a scope is open, go to the child handler for that macro,
1832: * as it may want to preprocess before doing anything with it.
1.32 schwarze 1833: * Don't do so if an equation is open.
1.2 schwarze 1834: */
1835:
1836: if (r->last) {
1.1 schwarze 1837: t = r->last->tok;
1.149 schwarze 1838: return (*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs);
1.2 schwarze 1839: }
1840:
1.115 schwarze 1841: /* No scope is open. This is a new request or macro. */
1842:
1.239 schwarze 1843: r->options &= ~MPARSE_COMMENT;
1.115 schwarze 1844: spos = pos;
1845: t = roff_parse(r, buf->buf, &pos, ln, ppos);
1846:
1847: /* Tables ignore most macros. */
1848:
1.180 schwarze 1849: if (r->tbl != NULL && (t == TOKEN_NONE || t == ROFF_TS ||
1.181 schwarze 1850: t == ROFF_br || t == ROFF_ce || t == ROFF_rj || t == ROFF_sp)) {
1.222 schwarze 1851: mandoc_msg(MANDOCERR_TBLMACRO,
1852: ln, pos, "%s", buf->buf + spos);
1.180 schwarze 1853: if (t != TOKEN_NONE)
1.149 schwarze 1854: return ROFF_IGN;
1.129 schwarze 1855: while (buf->buf[pos] != '\0' && buf->buf[pos] != ' ')
1856: pos++;
1.163 schwarze 1857: while (buf->buf[pos] == ' ')
1.129 schwarze 1858: pos++;
1.193 schwarze 1859: tbl_read(r->tbl, ln, buf->buf, pos);
1.218 schwarze 1860: roff_addtbl(r->man, ln, r->tbl);
1.193 schwarze 1861: return ROFF_IGN;
1.115 schwarze 1862: }
1863:
1.177 schwarze 1864: /* For now, let high level macros abort .ce mode. */
1865:
1866: if (ctl && roffce_node != NULL &&
1.196 schwarze 1867: (t == TOKEN_NONE || t == ROFF_Dd || t == ROFF_EQ ||
1868: t == ROFF_TH || t == ROFF_TS)) {
1.177 schwarze 1869: r->man->last = roffce_node;
1870: r->man->next = ROFF_NEXT_SIBLING;
1871: roffce_lines = 0;
1872: roffce_node = NULL;
1873: }
1874:
1.2 schwarze 1875: /*
1.115 schwarze 1876: * This is neither a roff request nor a user-defined macro.
1877: * Let the standard macro set parsers handle it.
1.2 schwarze 1878: */
1879:
1.166 schwarze 1880: if (t == TOKEN_NONE)
1.149 schwarze 1881: return ROFF_CONT;
1.115 schwarze 1882:
1883: /* Execute a roff request or a user defined macro. */
1.1 schwarze 1884:
1.168 schwarze 1885: return (*roffs[t].proc)(r, t, buf, ln, spos, pos, offs);
1.2 schwarze 1886: }
1887:
1.211 schwarze 1888: /*
1889: * Internal interface function to tell the roff parser that execution
1890: * of the current macro ended. This is required because macro
1891: * definitions usually do not end with a .return request.
1892: */
1893: void
1894: roff_userret(struct roff *r)
1895: {
1896: struct mctx *ctx;
1897: int i;
1898:
1899: assert(r->mstackpos >= 0);
1900: ctx = r->mstack + r->mstackpos;
1901: for (i = 0; i < ctx->argc; i++)
1902: free(ctx->argv[i]);
1903: ctx->argc = 0;
1904: r->mstackpos--;
1905: }
1906:
1.27 schwarze 1907: void
1.2 schwarze 1908: roff_endparse(struct roff *r)
1909: {
1.193 schwarze 1910: if (r->last != NULL)
1.222 schwarze 1911: mandoc_msg(MANDOCERR_BLK_NOEND, r->last->line,
1912: r->last->col, "%s", roff_name[r->last->tok]);
1.27 schwarze 1913:
1.193 schwarze 1914: if (r->eqn != NULL) {
1.222 schwarze 1915: mandoc_msg(MANDOCERR_BLK_NOEND,
1.191 schwarze 1916: r->eqn->node->line, r->eqn->node->pos, "EQ");
1917: eqn_parse(r->eqn);
1918: r->eqn = NULL;
1.32 schwarze 1919: }
1920:
1.193 schwarze 1921: if (r->tbl != NULL) {
1.218 schwarze 1922: tbl_end(r->tbl, 1);
1.193 schwarze 1923: r->tbl = NULL;
1.27 schwarze 1924: }
1.1 schwarze 1925: }
1926:
1927: /*
1928: * Parse a roff node's type from the input buffer. This must be in the
1929: * form of ".foo xxx" in the usual way.
1930: */
1.166 schwarze 1931: static enum roff_tok
1.87 schwarze 1932: roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
1.1 schwarze 1933: {
1.87 schwarze 1934: char *cp;
1.16 schwarze 1935: const char *mac;
1936: size_t maclen;
1.187 schwarze 1937: int deftype;
1.166 schwarze 1938: enum roff_tok t;
1.1 schwarze 1939:
1.87 schwarze 1940: cp = buf + *pos;
1941:
1942: if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
1.166 schwarze 1943: return TOKEN_NONE;
1.1 schwarze 1944:
1.87 schwarze 1945: mac = cp;
1946: maclen = roff_getname(r, &cp, ln, ppos);
1.1 schwarze 1947:
1.187 schwarze 1948: deftype = ROFFDEF_USER | ROFFDEF_REN;
1949: r->current_string = roff_getstrn(r, mac, maclen, &deftype);
1950: switch (deftype) {
1951: case ROFFDEF_USER:
1952: t = ROFF_USERDEF;
1953: break;
1954: case ROFFDEF_REN:
1955: t = ROFF_RENAMED;
1956: break;
1957: default:
1958: t = roffhash_find(r->reqtab, mac, maclen);
1959: break;
1960: }
1.166 schwarze 1961: if (t != TOKEN_NONE)
1.87 schwarze 1962: *pos = cp - buf;
1.197 schwarze 1963: else if (deftype == ROFFDEF_UNDEF) {
1964: /* Using an undefined macro defines it to be empty. */
1965: roff_setstrn(&r->strtab, mac, maclen, "", 0, 0);
1966: roff_setstrn(&r->rentab, mac, maclen, NULL, 0, 0);
1967: }
1.149 schwarze 1968: return t;
1.1 schwarze 1969: }
1970:
1.138 schwarze 1971: /* --- handling of request blocks ----------------------------------------- */
1972:
1.212 schwarze 1973: static int
1.2 schwarze 1974: roff_cblock(ROFF_ARGS)
1.1 schwarze 1975: {
1976:
1.2 schwarze 1977: /*
1978: * A block-close `..' should only be invoked as a child of an
1979: * ignore macro, otherwise raise a warning and just ignore it.
1980: */
1981:
1.110 schwarze 1982: if (r->last == NULL) {
1.222 schwarze 1983: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "..");
1.149 schwarze 1984: return ROFF_IGN;
1.2 schwarze 1985: }
1.1 schwarze 1986:
1.2 schwarze 1987: switch (r->last->tok) {
1.80 schwarze 1988: case ROFF_am:
1.93 schwarze 1989: /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1.80 schwarze 1990: case ROFF_ami:
1991: case ROFF_de:
1.23 schwarze 1992: /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1.80 schwarze 1993: case ROFF_dei:
1994: case ROFF_ig:
1.2 schwarze 1995: break;
1996: default:
1.222 schwarze 1997: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "..");
1.149 schwarze 1998: return ROFF_IGN;
1.2 schwarze 1999: }
2000:
1.110 schwarze 2001: if (buf->buf[pos] != '\0')
1.222 schwarze 2002: mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
1.110 schwarze 2003: ".. %s", buf->buf + pos);
1.2 schwarze 2004:
2005: roffnode_pop(r);
2006: roffnode_cleanscope(r);
1.149 schwarze 2007: return ROFF_IGN;
1.2 schwarze 2008:
2009: }
1.1 schwarze 2010:
1.236 schwarze 2011: /*
2012: * Pop all nodes ending at the end of the current input line.
2013: * Return the number of loops ended.
2014: */
1.212 schwarze 2015: static int
1.2 schwarze 2016: roffnode_cleanscope(struct roff *r)
2017: {
1.212 schwarze 2018: int inloop;
1.1 schwarze 2019:
1.212 schwarze 2020: inloop = 0;
2021: while (r->last != NULL) {
1.46 schwarze 2022: if (--r->last->endspan != 0)
1.2 schwarze 2023: break;
1.212 schwarze 2024: inloop += roffnode_pop(r);
1.2 schwarze 2025: }
1.212 schwarze 2026: return inloop;
1.2 schwarze 2027: }
1.1 schwarze 2028:
1.236 schwarze 2029: /*
2030: * Handle the closing \} of a conditional block.
2031: * Apart from generating warnings, this only pops nodes.
2032: * Return the number of loops ended.
2033: */
1.212 schwarze 2034: static int
1.68 schwarze 2035: roff_ccond(struct roff *r, int ln, int ppos)
1.2 schwarze 2036: {
2037: if (NULL == r->last) {
1.222 schwarze 2038: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "\\}");
1.212 schwarze 2039: return 0;
1.2 schwarze 2040: }
1.1 schwarze 2041:
1.2 schwarze 2042: switch (r->last->tok) {
1.80 schwarze 2043: case ROFF_el:
2044: case ROFF_ie:
2045: case ROFF_if:
1.212 schwarze 2046: case ROFF_while:
1.2 schwarze 2047: break;
2048: default:
1.222 schwarze 2049: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "\\}");
1.212 schwarze 2050: return 0;
1.2 schwarze 2051: }
1.1 schwarze 2052:
1.2 schwarze 2053: if (r->last->endspan > -1) {
1.222 schwarze 2054: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "\\}");
1.212 schwarze 2055: return 0;
1.2 schwarze 2056: }
2057:
1.212 schwarze 2058: return roffnode_pop(r) + roffnode_cleanscope(r);
1.1 schwarze 2059: }
2060:
1.212 schwarze 2061: static int
1.2 schwarze 2062: roff_block(ROFF_ARGS)
1.1 schwarze 2063: {
1.187 schwarze 2064: const char *name, *value;
2065: char *call, *cp, *iname, *rname;
2066: size_t csz, namesz, rsz;
2067: int deftype;
1.16 schwarze 2068:
1.93 schwarze 2069: /* Ignore groff compatibility mode for now. */
1.2 schwarze 2070:
1.110 schwarze 2071: if (tok == ROFF_de1)
1.93 schwarze 2072: tok = ROFF_de;
1.123 schwarze 2073: else if (tok == ROFF_dei1)
2074: tok = ROFF_dei;
1.110 schwarze 2075: else if (tok == ROFF_am1)
1.93 schwarze 2076: tok = ROFF_am;
1.123 schwarze 2077: else if (tok == ROFF_ami1)
2078: tok = ROFF_ami;
1.93 schwarze 2079:
2080: /* Parse the macro name argument. */
2081:
1.110 schwarze 2082: cp = buf->buf + pos;
2083: if (tok == ROFF_ig) {
1.93 schwarze 2084: iname = NULL;
2085: namesz = 0;
2086: } else {
2087: iname = cp;
2088: namesz = roff_getname(r, &cp, ln, ppos);
2089: iname[namesz] = '\0';
2090: }
1.22 schwarze 2091:
1.93 schwarze 2092: /* Resolve the macro name argument if it is indirect. */
1.22 schwarze 2093:
1.110 schwarze 2094: if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1.187 schwarze 2095: deftype = ROFFDEF_USER;
2096: name = roff_getstrn(r, iname, namesz, &deftype);
2097: if (name == NULL) {
1.222 schwarze 2098: mandoc_msg(MANDOCERR_STR_UNDEF,
2099: ln, (int)(iname - buf->buf),
1.93 schwarze 2100: "%.*s", (int)namesz, iname);
2101: namesz = 0;
2102: } else
2103: namesz = strlen(name);
2104: } else
2105: name = iname;
1.22 schwarze 2106:
1.110 schwarze 2107: if (namesz == 0 && tok != ROFF_ig) {
1.222 schwarze 2108: mandoc_msg(MANDOCERR_REQ_EMPTY,
2109: ln, ppos, "%s", roff_name[tok]);
1.149 schwarze 2110: return ROFF_IGN;
1.93 schwarze 2111: }
1.2 schwarze 2112:
1.16 schwarze 2113: roffnode_push(r, tok, name, ln, ppos);
2114:
2115: /*
2116: * At the beginning of a `de' macro, clear the existing string
2117: * with the same name, if there is one. New content will be
1.66 schwarze 2118: * appended from roff_block_text() in multiline mode.
1.16 schwarze 2119: */
1.22 schwarze 2120:
1.183 schwarze 2121: if (tok == ROFF_de || tok == ROFF_dei) {
1.86 schwarze 2122: roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
1.183 schwarze 2123: roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.187 schwarze 2124: } else if (tok == ROFF_am || tok == ROFF_ami) {
2125: deftype = ROFFDEF_ANY;
2126: value = roff_getstrn(r, iname, namesz, &deftype);
2127: switch (deftype) { /* Before appending, ... */
2128: case ROFFDEF_PRE: /* copy predefined to user-defined. */
2129: roff_setstrn(&r->strtab, name, namesz,
2130: value, strlen(value), 0);
2131: break;
2132: case ROFFDEF_REN: /* call original standard macro. */
2133: csz = mandoc_asprintf(&call, ".%.*s \\$* \\\"\n",
2134: (int)strlen(value), value);
2135: roff_setstrn(&r->strtab, name, namesz, call, csz, 0);
2136: roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
2137: free(call);
2138: break;
2139: case ROFFDEF_STD: /* rename and call standard macro. */
2140: rsz = mandoc_asprintf(&rname, "__%s_renamed", name);
2141: roff_setstrn(&r->rentab, rname, rsz, name, namesz, 0);
2142: csz = mandoc_asprintf(&call, ".%.*s \\$* \\\"\n",
2143: (int)rsz, rname);
2144: roff_setstrn(&r->strtab, name, namesz, call, csz, 0);
2145: free(call);
2146: free(rname);
2147: break;
2148: default:
2149: break;
2150: }
1.183 schwarze 2151: }
1.2 schwarze 2152:
1.110 schwarze 2153: if (*cp == '\0')
1.149 schwarze 2154: return ROFF_IGN;
1.1 schwarze 2155:
1.93 schwarze 2156: /* Get the custom end marker. */
1.22 schwarze 2157:
1.93 schwarze 2158: iname = cp;
1.86 schwarze 2159: namesz = roff_getname(r, &cp, ln, ppos);
1.93 schwarze 2160:
2161: /* Resolve the end marker if it is indirect. */
2162:
1.110 schwarze 2163: if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1.187 schwarze 2164: deftype = ROFFDEF_USER;
2165: name = roff_getstrn(r, iname, namesz, &deftype);
2166: if (name == NULL) {
1.222 schwarze 2167: mandoc_msg(MANDOCERR_STR_UNDEF,
2168: ln, (int)(iname - buf->buf),
1.93 schwarze 2169: "%.*s", (int)namesz, iname);
2170: namesz = 0;
2171: } else
2172: namesz = strlen(name);
2173: } else
2174: name = iname;
2175:
1.86 schwarze 2176: if (namesz)
2177: r->last->end = mandoc_strndup(name, namesz);
1.2 schwarze 2178:
1.110 schwarze 2179: if (*cp != '\0')
1.222 schwarze 2180: mandoc_msg(MANDOCERR_ARG_EXCESS,
1.167 schwarze 2181: ln, pos, ".%s ... %s", roff_name[tok], cp);
1.1 schwarze 2182:
1.149 schwarze 2183: return ROFF_IGN;
1.1 schwarze 2184: }
2185:
1.212 schwarze 2186: static int
1.2 schwarze 2187: roff_block_sub(ROFF_ARGS)
1.1 schwarze 2188: {
1.166 schwarze 2189: enum roff_tok t;
1.2 schwarze 2190: int i, j;
2191:
2192: /*
2193: * First check whether a custom macro exists at this level. If
2194: * it does, then check against it. This is some of groff's
2195: * stranger behaviours. If we encountered a custom end-scope
2196: * tag and that tag also happens to be a "real" macro, then we
2197: * need to try interpreting it again as a real macro. If it's
2198: * not, then return ignore. Else continue.
2199: */
2200:
2201: if (r->last->end) {
1.35 schwarze 2202: for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.110 schwarze 2203: if (buf->buf[i] != r->last->end[j])
1.2 schwarze 2204: break;
1.1 schwarze 2205:
1.110 schwarze 2206: if (r->last->end[j] == '\0' &&
2207: (buf->buf[i] == '\0' ||
2208: buf->buf[i] == ' ' ||
2209: buf->buf[i] == '\t')) {
1.2 schwarze 2210: roffnode_pop(r);
2211: roffnode_cleanscope(r);
1.1 schwarze 2212:
1.110 schwarze 2213: while (buf->buf[i] == ' ' || buf->buf[i] == '\t')
1.35 schwarze 2214: i++;
2215:
2216: pos = i;
1.110 schwarze 2217: if (roff_parse(r, buf->buf, &pos, ln, ppos) !=
1.166 schwarze 2218: TOKEN_NONE)
1.149 schwarze 2219: return ROFF_RERUN;
2220: return ROFF_IGN;
1.2 schwarze 2221: }
1.1 schwarze 2222: }
2223:
1.2 schwarze 2224: /*
2225: * If we have no custom end-query or lookup failed, then try
2226: * pulling it out of the hashtable.
2227: */
1.1 schwarze 2228:
1.110 schwarze 2229: t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.1 schwarze 2230:
1.110 schwarze 2231: if (t != ROFF_cblock) {
2232: if (tok != ROFF_ig)
2233: roff_setstr(r, r->last->name, buf->buf + ppos, 2);
1.149 schwarze 2234: return ROFF_IGN;
1.16 schwarze 2235: }
1.1 schwarze 2236:
1.149 schwarze 2237: return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
1.2 schwarze 2238: }
2239:
1.212 schwarze 2240: static int
1.2 schwarze 2241: roff_block_text(ROFF_ARGS)
2242: {
2243:
1.110 schwarze 2244: if (tok != ROFF_ig)
2245: roff_setstr(r, r->last->name, buf->buf + pos, 2);
1.16 schwarze 2246:
1.149 schwarze 2247: return ROFF_IGN;
1.2 schwarze 2248: }
2249:
1.212 schwarze 2250: static int
1.2 schwarze 2251: roff_cond_sub(ROFF_ARGS)
2252: {
1.236 schwarze 2253: struct roffnode *bl;
1.212 schwarze 2254: char *ep;
2255: int endloop, irc, rr;
1.166 schwarze 2256: enum roff_tok t;
1.2 schwarze 2257:
1.212 schwarze 2258: irc = ROFF_IGN;
1.2 schwarze 2259: rr = r->last->rule;
1.212 schwarze 2260: endloop = tok != ROFF_while ? ROFF_IGN :
2261: rr ? ROFF_LOOPCONT : ROFF_LOOPEXIT;
2262: if (roffnode_cleanscope(r))
2263: irc |= endloop;
1.39 schwarze 2264:
1.69 schwarze 2265: /*
2266: * If `\}' occurs on a macro line without a preceding macro,
2267: * drop the line completely.
2268: */
2269:
1.110 schwarze 2270: ep = buf->buf + pos;
2271: if (ep[0] == '\\' && ep[1] == '}')
1.71 schwarze 2272: rr = 0;
1.69 schwarze 2273:
1.215 schwarze 2274: /*
2275: * The closing delimiter `\}' rewinds the conditional scope
2276: * but is otherwise ignored when interpreting the line.
2277: */
1.39 schwarze 2278:
1.110 schwarze 2279: while ((ep = strchr(ep, '\\')) != NULL) {
1.190 schwarze 2280: switch (ep[1]) {
2281: case '}':
2282: memmove(ep, ep + 2, strlen(ep + 2) + 1);
1.212 schwarze 2283: if (roff_ccond(r, ln, ep - buf->buf))
2284: irc |= endloop;
1.190 schwarze 2285: break;
2286: case '\0':
2287: ++ep;
2288: break;
2289: default:
2290: ep += 2;
2291: break;
1.70 schwarze 2292: }
1.50 schwarze 2293: }
1.190 schwarze 2294:
1.240 schwarze 2295: t = roff_parse(r, buf->buf, &pos, ln, ppos);
2296:
2297: /* For now, let high level macros abort .ce mode. */
2298:
2299: if (roffce_node != NULL &&
2300: (t == TOKEN_NONE || t == ROFF_Dd || t == ROFF_EQ ||
2301: t == ROFF_TH || t == ROFF_TS)) {
2302: r->man->last = roffce_node;
2303: r->man->next = ROFF_NEXT_SIBLING;
2304: roffce_lines = 0;
2305: roffce_node = NULL;
2306: }
2307:
1.190 schwarze 2308: /*
2309: * Fully handle known macros when they are structurally
2310: * required or when the conditional evaluated to true.
2311: */
2312:
1.236 schwarze 2313: if (t == ROFF_break) {
2314: if (irc & ROFF_LOOPMASK)
2315: irc = ROFF_IGN | ROFF_LOOPEXIT;
2316: else if (rr) {
2317: for (bl = r->last; bl != NULL; bl = bl->parent) {
2318: bl->rule = 0;
2319: if (bl->tok == ROFF_while)
2320: break;
2321: }
2322: }
2323: } else if (t != TOKEN_NONE &&
2324: (rr || roffs[t].flags & ROFFMAC_STRUCT))
2325: irc |= (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
2326: else
2327: irc |= rr ? ROFF_CONT : ROFF_IGN;
1.212 schwarze 2328: return irc;
1.2 schwarze 2329: }
2330:
1.212 schwarze 2331: static int
1.2 schwarze 2332: roff_cond_text(ROFF_ARGS)
2333: {
1.37 schwarze 2334: char *ep;
1.212 schwarze 2335: int endloop, irc, rr;
1.2 schwarze 2336:
1.212 schwarze 2337: irc = ROFF_IGN;
1.2 schwarze 2338: rr = r->last->rule;
1.212 schwarze 2339: endloop = tok != ROFF_while ? ROFF_IGN :
2340: rr ? ROFF_LOOPCONT : ROFF_LOOPEXIT;
2341: if (roffnode_cleanscope(r))
2342: irc |= endloop;
1.1 schwarze 2343:
1.215 schwarze 2344: /*
2345: * If `\}' occurs on a text line with neither preceding
2346: * nor following characters, drop the line completely.
2347: */
2348:
1.110 schwarze 2349: ep = buf->buf + pos;
1.215 schwarze 2350: if (strcmp(ep, "\\}") == 0)
2351: rr = 0;
2352:
2353: /*
2354: * The closing delimiter `\}' rewinds the conditional scope
2355: * but is otherwise ignored when interpreting the line.
2356: */
2357:
1.110 schwarze 2358: while ((ep = strchr(ep, '\\')) != NULL) {
1.215 schwarze 2359: switch (ep[1]) {
2360: case '}':
2361: memmove(ep, ep + 2, strlen(ep + 2) + 1);
2362: if (roff_ccond(r, ln, ep - buf->buf))
1.212 schwarze 2363: irc |= endloop;
1.215 schwarze 2364: break;
2365: case '\0':
2366: ++ep;
2367: break;
2368: default:
2369: ep += 2;
2370: break;
1.70 schwarze 2371: }
1.2 schwarze 2372: }
1.212 schwarze 2373: if (rr)
2374: irc |= ROFF_CONT;
2375: return irc;
1.2 schwarze 2376: }
2377:
1.138 schwarze 2378: /* --- handling of numeric and conditional expressions -------------------- */
2379:
1.77 schwarze 2380: /*
2381: * Parse a single signed integer number. Stop at the first non-digit.
2382: * If there is at least one digit, return success and advance the
2383: * parse point, else return failure and let the parse point unchanged.
2384: * Ignore overflows, treat them just like the C language.
2385: */
1.56 schwarze 2386: static int
1.133 schwarze 2387: roff_getnum(const char *v, int *pos, int *res, int flags)
1.56 schwarze 2388: {
1.133 schwarze 2389: int myres, scaled, n, p;
1.79 schwarze 2390:
2391: if (NULL == res)
2392: res = &myres;
1.56 schwarze 2393:
2394: p = *pos;
2395: n = v[p] == '-';
1.133 schwarze 2396: if (n || v[p] == '+')
1.56 schwarze 2397: p++;
2398:
1.133 schwarze 2399: if (flags & ROFFNUM_WHITE)
2400: while (isspace((unsigned char)v[p]))
2401: p++;
2402:
1.56 schwarze 2403: for (*res = 0; isdigit((unsigned char)v[p]); p++)
1.77 schwarze 2404: *res = 10 * *res + v[p] - '0';
1.56 schwarze 2405: if (p == *pos + n)
2406: return 0;
2407:
2408: if (n)
2409: *res = -*res;
2410:
1.126 schwarze 2411: /* Each number may be followed by one optional scaling unit. */
2412:
2413: switch (v[p]) {
2414: case 'f':
1.133 schwarze 2415: scaled = *res * 65536;
1.126 schwarze 2416: break;
2417: case 'i':
1.133 schwarze 2418: scaled = *res * 240;
1.126 schwarze 2419: break;
2420: case 'c':
1.133 schwarze 2421: scaled = *res * 240 / 2.54;
1.126 schwarze 2422: break;
2423: case 'v':
2424: case 'P':
1.133 schwarze 2425: scaled = *res * 40;
1.126 schwarze 2426: break;
2427: case 'm':
2428: case 'n':
1.133 schwarze 2429: scaled = *res * 24;
1.126 schwarze 2430: break;
2431: case 'p':
1.133 schwarze 2432: scaled = *res * 10 / 3;
1.126 schwarze 2433: break;
2434: case 'u':
1.133 schwarze 2435: scaled = *res;
1.126 schwarze 2436: break;
2437: case 'M':
1.133 schwarze 2438: scaled = *res * 6 / 25;
1.126 schwarze 2439: break;
2440: default:
1.133 schwarze 2441: scaled = *res;
1.126 schwarze 2442: p--;
2443: break;
2444: }
1.133 schwarze 2445: if (flags & ROFFNUM_SCALE)
2446: *res = scaled;
1.126 schwarze 2447:
2448: *pos = p + 1;
1.149 schwarze 2449: return 1;
1.56 schwarze 2450: }
2451:
1.71 schwarze 2452: /*
2453: * Evaluate a string comparison condition.
2454: * The first character is the delimiter.
2455: * Succeed if the string up to its second occurrence
2456: * matches the string up to its third occurence.
2457: * Advance the cursor after the third occurrence
2458: * or lacking that, to the end of the line.
2459: */
2460: static int
2461: roff_evalstrcond(const char *v, int *pos)
2462: {
2463: const char *s1, *s2, *s3;
2464: int match;
2465:
2466: match = 0;
2467: s1 = v + *pos; /* initial delimiter */
2468: s2 = s1 + 1; /* for scanning the first string */
2469: s3 = strchr(s2, *s1); /* for scanning the second string */
2470:
2471: if (NULL == s3) /* found no middle delimiter */
2472: goto out;
2473:
2474: while ('\0' != *++s3) {
2475: if (*s2 != *s3) { /* mismatch */
2476: s3 = strchr(s3, *s1);
2477: break;
2478: }
2479: if (*s3 == *s1) { /* found the final delimiter */
2480: match = 1;
2481: break;
2482: }
2483: s2++;
2484: }
2485:
2486: out:
2487: if (NULL == s3)
2488: s3 = strchr(s2, '\0');
1.114 schwarze 2489: else if (*s3 != '\0')
1.71 schwarze 2490: s3++;
2491: *pos = s3 - v;
1.149 schwarze 2492: return match;
1.71 schwarze 2493: }
2494:
1.77 schwarze 2495: /*
2496: * Evaluate an optionally negated single character, numerical,
2497: * or string condition.
2498: */
1.71 schwarze 2499: static int
1.143 schwarze 2500: roff_evalcond(struct roff *r, int ln, char *v, int *pos)
1.5 schwarze 2501: {
1.208 schwarze 2502: const char *start, *end;
2503: char *cp, *name;
2504: size_t sz;
2505: int deftype, len, number, savepos, istrue, wanttrue;
1.5 schwarze 2506:
1.71 schwarze 2507: if ('!' == v[*pos]) {
2508: wanttrue = 0;
2509: (*pos)++;
2510: } else
2511: wanttrue = 1;
2512:
1.5 schwarze 2513: switch (v[*pos]) {
1.112 schwarze 2514: case '\0':
1.149 schwarze 2515: return 0;
1.80 schwarze 2516: case 'n':
2517: case 'o':
1.5 schwarze 2518: (*pos)++;
1.149 schwarze 2519: return wanttrue;
1.80 schwarze 2520: case 'e':
2521: case 't':
1.111 schwarze 2522: case 'v':
1.5 schwarze 2523: (*pos)++;
1.149 schwarze 2524: return !wanttrue;
1.208 schwarze 2525: case 'c':
2526: do {
2527: (*pos)++;
2528: } while (v[*pos] == ' ');
2529:
2530: /*
2531: * Quirk for groff compatibility:
2532: * The horizontal tab is neither available nor unavailable.
2533: */
2534:
2535: if (v[*pos] == '\t') {
2536: (*pos)++;
2537: return 0;
2538: }
2539:
2540: /* Printable ASCII characters are available. */
2541:
2542: if (v[*pos] != '\\') {
2543: (*pos)++;
2544: return wanttrue;
2545: }
2546:
2547: end = v + ++*pos;
2548: switch (mandoc_escape(&end, &start, &len)) {
2549: case ESCAPE_SPECIAL:
2550: istrue = mchars_spec2cp(start, len) != -1;
2551: break;
2552: case ESCAPE_UNICODE:
2553: istrue = 1;
2554: break;
2555: case ESCAPE_NUMBERED:
2556: istrue = mchars_num2char(start, len) != -1;
2557: break;
2558: default:
2559: istrue = !wanttrue;
2560: break;
2561: }
2562: *pos = end - v;
2563: return istrue == wanttrue;
1.182 schwarze 2564: case 'd':
1.143 schwarze 2565: case 'r':
1.182 schwarze 2566: cp = v + *pos + 1;
2567: while (*cp == ' ')
2568: cp++;
2569: name = cp;
2570: sz = roff_getname(r, &cp, ln, cp - v);
1.187 schwarze 2571: if (sz == 0)
2572: istrue = 0;
2573: else if (v[*pos] == 'r')
2574: istrue = roff_hasregn(r, name, sz);
2575: else {
2576: deftype = ROFFDEF_ANY;
2577: roff_getstrn(r, name, sz, &deftype);
2578: istrue = !!deftype;
2579: }
1.235 schwarze 2580: *pos = (name + sz) - v;
1.182 schwarze 2581: return istrue == wanttrue;
1.5 schwarze 2582: default:
2583: break;
2584: }
2585:
1.113 schwarze 2586: savepos = *pos;
1.133 schwarze 2587: if (roff_evalnum(r, ln, v, pos, &number, ROFFNUM_SCALE))
1.149 schwarze 2588: return (number > 0) == wanttrue;
1.113 schwarze 2589: else if (*pos == savepos)
1.149 schwarze 2590: return roff_evalstrcond(v, pos) == wanttrue;
1.77 schwarze 2591: else
1.149 schwarze 2592: return 0;
1.5 schwarze 2593: }
2594:
1.212 schwarze 2595: static int
1.21 schwarze 2596: roff_line_ignore(ROFF_ARGS)
1.6 schwarze 2597: {
1.30 schwarze 2598:
1.149 schwarze 2599: return ROFF_IGN;
1.21 schwarze 2600: }
2601:
1.212 schwarze 2602: static int
1.123 schwarze 2603: roff_insec(ROFF_ARGS)
2604: {
2605:
1.222 schwarze 2606: mandoc_msg(MANDOCERR_REQ_INSEC, ln, ppos, "%s", roff_name[tok]);
1.149 schwarze 2607: return ROFF_IGN;
1.123 schwarze 2608: }
2609:
1.212 schwarze 2610: static int
1.123 schwarze 2611: roff_unsupp(ROFF_ARGS)
2612: {
2613:
1.222 schwarze 2614: mandoc_msg(MANDOCERR_REQ_UNSUPP, ln, ppos, "%s", roff_name[tok]);
1.149 schwarze 2615: return ROFF_IGN;
1.123 schwarze 2616: }
2617:
1.212 schwarze 2618: static int
1.2 schwarze 2619: roff_cond(ROFF_ARGS)
2620: {
1.212 schwarze 2621: int irc;
1.46 schwarze 2622:
2623: roffnode_push(r, tok, NULL, ln, ppos);
1.2 schwarze 2624:
1.80 schwarze 2625: /*
1.35 schwarze 2626: * An `.el' has no conditional body: it will consume the value
2627: * of the current rstack entry set in prior `ie' calls or
1.80 schwarze 2628: * defaults to DENY.
1.35 schwarze 2629: *
2630: * If we're not an `el', however, then evaluate the conditional.
2631: */
1.1 schwarze 2632:
1.110 schwarze 2633: r->last->rule = tok == ROFF_el ?
1.80 schwarze 2634: (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
1.110 schwarze 2635: roff_evalcond(r, ln, buf->buf, &pos);
1.2 schwarze 2636:
1.35 schwarze 2637: /*
2638: * An if-else will put the NEGATION of the current evaluated
2639: * conditional into the stack of rules.
2640: */
2641:
1.110 schwarze 2642: if (tok == ROFF_ie) {
1.96 schwarze 2643: if (r->rstackpos + 1 == r->rstacksz) {
2644: r->rstacksz += 16;
2645: r->rstack = mandoc_reallocarray(r->rstack,
2646: r->rstacksz, sizeof(int));
1.35 schwarze 2647: }
1.71 schwarze 2648: r->rstack[++r->rstackpos] = !r->last->rule;
1.2 schwarze 2649: }
1.5 schwarze 2650:
2651: /* If the parent has false as its rule, then so do we. */
2652:
1.71 schwarze 2653: if (r->last->parent && !r->last->parent->rule)
2654: r->last->rule = 0;
1.5 schwarze 2655:
2656: /*
1.46 schwarze 2657: * Determine scope.
2658: * If there is nothing on the line after the conditional,
2659: * not even whitespace, use next-line scope.
1.212 schwarze 2660: * Except that .while does not support next-line scope.
1.5 schwarze 2661: */
1.2 schwarze 2662:
1.212 schwarze 2663: if (buf->buf[pos] == '\0' && tok != ROFF_while) {
1.46 schwarze 2664: r->last->endspan = 2;
2665: goto out;
2666: }
2667:
1.110 schwarze 2668: while (buf->buf[pos] == ' ')
1.46 schwarze 2669: pos++;
2670:
2671: /* An opening brace requests multiline scope. */
1.2 schwarze 2672:
1.110 schwarze 2673: if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
1.2 schwarze 2674: r->last->endspan = -1;
2675: pos += 2;
1.144 schwarze 2676: while (buf->buf[pos] == ' ')
2677: pos++;
1.46 schwarze 2678: goto out;
1.80 schwarze 2679: }
1.2 schwarze 2680:
2681: /*
1.46 schwarze 2682: * Anything else following the conditional causes
2683: * single-line scope. Warn if the scope contains
2684: * nothing but trailing whitespace.
1.2 schwarze 2685: */
2686:
1.110 schwarze 2687: if (buf->buf[pos] == '\0')
1.222 schwarze 2688: mandoc_msg(MANDOCERR_COND_EMPTY,
2689: ln, ppos, "%s", roff_name[tok]);
1.2 schwarze 2690:
1.46 schwarze 2691: r->last->endspan = 1;
1.1 schwarze 2692:
1.46 schwarze 2693: out:
1.2 schwarze 2694: *offs = pos;
1.212 schwarze 2695: irc = ROFF_RERUN;
2696: if (tok == ROFF_while)
2697: irc |= ROFF_WHILE;
2698: return irc;
1.1 schwarze 2699: }
2700:
1.212 schwarze 2701: static int
1.7 schwarze 2702: roff_ds(ROFF_ARGS)
2703: {
1.85 schwarze 2704: char *string;
2705: const char *name;
2706: size_t namesz;
1.10 schwarze 2707:
1.123 schwarze 2708: /* Ignore groff compatibility mode for now. */
2709:
2710: if (tok == ROFF_ds1)
2711: tok = ROFF_ds;
2712: else if (tok == ROFF_as1)
2713: tok = ROFF_as;
2714:
1.10 schwarze 2715: /*
1.85 schwarze 2716: * The first word is the name of the string.
2717: * If it is empty or terminated by an escape sequence,
2718: * abort the `ds' request without defining anything.
1.10 schwarze 2719: */
1.7 schwarze 2720:
1.110 schwarze 2721: name = string = buf->buf + pos;
2722: if (*name == '\0')
1.149 schwarze 2723: return ROFF_IGN;
1.7 schwarze 2724:
1.85 schwarze 2725: namesz = roff_getname(r, &string, ln, pos);
1.235 schwarze 2726: switch (name[namesz]) {
2727: case '\\':
1.149 schwarze 2728: return ROFF_IGN;
1.235 schwarze 2729: case '\t':
2730: string = buf->buf + pos + namesz;
2731: break;
2732: default:
2733: break;
2734: }
1.85 schwarze 2735:
2736: /* Read past the initial double-quote, if any. */
1.110 schwarze 2737: if (*string == '"')
1.7 schwarze 2738: string++;
2739:
1.10 schwarze 2740: /* The rest is the value. */
1.85 schwarze 2741: roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
2742: ROFF_as == tok);
1.183 schwarze 2743: roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.149 schwarze 2744: return ROFF_IGN;
1.7 schwarze 2745: }
2746:
1.77 schwarze 2747: /*
2748: * Parse a single operator, one or two characters long.
2749: * If the operator is recognized, return success and advance the
2750: * parse point, else return failure and let the parse point unchanged.
2751: */
2752: static int
2753: roff_getop(const char *v, int *pos, char *res)
2754: {
2755:
2756: *res = v[*pos];
2757:
2758: switch (*res) {
1.80 schwarze 2759: case '+':
2760: case '-':
2761: case '*':
2762: case '/':
2763: case '%':
2764: case '&':
2765: case ':':
1.77 schwarze 2766: break;
2767: case '<':
2768: switch (v[*pos + 1]) {
1.80 schwarze 2769: case '=':
1.77 schwarze 2770: *res = 'l';
2771: (*pos)++;
2772: break;
1.80 schwarze 2773: case '>':
1.77 schwarze 2774: *res = '!';
2775: (*pos)++;
2776: break;
1.80 schwarze 2777: case '?':
1.77 schwarze 2778: *res = 'i';
2779: (*pos)++;
2780: break;
2781: default:
2782: break;
2783: }
2784: break;
2785: case '>':
2786: switch (v[*pos + 1]) {
1.80 schwarze 2787: case '=':
1.77 schwarze 2788: *res = 'g';
2789: (*pos)++;
2790: break;
1.80 schwarze 2791: case '?':
1.77 schwarze 2792: *res = 'a';
2793: (*pos)++;
2794: break;
2795: default:
2796: break;
2797: }
2798: break;
2799: case '=':
2800: if ('=' == v[*pos + 1])
2801: (*pos)++;
2802: break;
2803: default:
1.149 schwarze 2804: return 0;
1.77 schwarze 2805: }
2806: (*pos)++;
2807:
1.149 schwarze 2808: return *res;
1.77 schwarze 2809: }
2810:
2811: /*
2812: * Evaluate either a parenthesized numeric expression
2813: * or a single signed integer number.
2814: */
2815: static int
1.106 schwarze 2816: roff_evalpar(struct roff *r, int ln,
1.133 schwarze 2817: const char *v, int *pos, int *res, int flags)
1.77 schwarze 2818: {
2819:
2820: if ('(' != v[*pos])
1.149 schwarze 2821: return roff_getnum(v, pos, res, flags);
1.77 schwarze 2822:
2823: (*pos)++;
1.133 schwarze 2824: if ( ! roff_evalnum(r, ln, v, pos, res, flags | ROFFNUM_WHITE))
1.149 schwarze 2825: return 0;
1.77 schwarze 2826:
1.79 schwarze 2827: /*
2828: * Omission of the closing parenthesis
2829: * is an error in validation mode,
2830: * but ignored in evaluation mode.
2831: */
2832:
1.77 schwarze 2833: if (')' == v[*pos])
2834: (*pos)++;
1.79 schwarze 2835: else if (NULL == res)
1.149 schwarze 2836: return 0;
1.77 schwarze 2837:
1.149 schwarze 2838: return 1;
1.77 schwarze 2839: }
2840:
2841: /*
2842: * Evaluate a complete numeric expression.
2843: * Proceed left to right, there is no concept of precedence.
2844: */
2845: static int
1.106 schwarze 2846: roff_evalnum(struct roff *r, int ln, const char *v,
1.133 schwarze 2847: int *pos, int *res, int flags)
1.77 schwarze 2848: {
2849: int mypos, operand2;
2850: char operator;
2851:
2852: if (NULL == pos) {
2853: mypos = 0;
2854: pos = &mypos;
2855: }
2856:
1.133 schwarze 2857: if (flags & ROFFNUM_WHITE)
1.77 schwarze 2858: while (isspace((unsigned char)v[*pos]))
2859: (*pos)++;
2860:
1.133 schwarze 2861: if ( ! roff_evalpar(r, ln, v, pos, res, flags))
1.149 schwarze 2862: return 0;
1.77 schwarze 2863:
2864: while (1) {
1.133 schwarze 2865: if (flags & ROFFNUM_WHITE)
1.77 schwarze 2866: while (isspace((unsigned char)v[*pos]))
2867: (*pos)++;
2868:
2869: if ( ! roff_getop(v, pos, &operator))
2870: break;
2871:
1.133 schwarze 2872: if (flags & ROFFNUM_WHITE)
1.77 schwarze 2873: while (isspace((unsigned char)v[*pos]))
2874: (*pos)++;
2875:
1.133 schwarze 2876: if ( ! roff_evalpar(r, ln, v, pos, &operand2, flags))
1.149 schwarze 2877: return 0;
1.77 schwarze 2878:
1.133 schwarze 2879: if (flags & ROFFNUM_WHITE)
1.77 schwarze 2880: while (isspace((unsigned char)v[*pos]))
2881: (*pos)++;
1.79 schwarze 2882:
2883: if (NULL == res)
2884: continue;
1.77 schwarze 2885:
2886: switch (operator) {
1.80 schwarze 2887: case '+':
1.77 schwarze 2888: *res += operand2;
2889: break;
1.80 schwarze 2890: case '-':
1.77 schwarze 2891: *res -= operand2;
2892: break;
1.80 schwarze 2893: case '*':
1.77 schwarze 2894: *res *= operand2;
2895: break;
1.80 schwarze 2896: case '/':
1.116 schwarze 2897: if (operand2 == 0) {
1.106 schwarze 2898: mandoc_msg(MANDOCERR_DIVZERO,
1.222 schwarze 2899: ln, *pos, "%s", v);
1.106 schwarze 2900: *res = 0;
2901: break;
2902: }
1.77 schwarze 2903: *res /= operand2;
2904: break;
1.80 schwarze 2905: case '%':
1.116 schwarze 2906: if (operand2 == 0) {
2907: mandoc_msg(MANDOCERR_DIVZERO,
1.222 schwarze 2908: ln, *pos, "%s", v);
1.116 schwarze 2909: *res = 0;
2910: break;
2911: }
1.77 schwarze 2912: *res %= operand2;
2913: break;
1.80 schwarze 2914: case '<':
1.77 schwarze 2915: *res = *res < operand2;
2916: break;
1.80 schwarze 2917: case '>':
1.77 schwarze 2918: *res = *res > operand2;
2919: break;
1.80 schwarze 2920: case 'l':
1.77 schwarze 2921: *res = *res <= operand2;
2922: break;
1.80 schwarze 2923: case 'g':
1.77 schwarze 2924: *res = *res >= operand2;
2925: break;
1.80 schwarze 2926: case '=':
1.77 schwarze 2927: *res = *res == operand2;
2928: break;
1.80 schwarze 2929: case '!':
1.77 schwarze 2930: *res = *res != operand2;
2931: break;
1.80 schwarze 2932: case '&':
1.77 schwarze 2933: *res = *res && operand2;
2934: break;
1.80 schwarze 2935: case ':':
1.77 schwarze 2936: *res = *res || operand2;
2937: break;
1.80 schwarze 2938: case 'i':
1.77 schwarze 2939: if (operand2 < *res)
2940: *res = operand2;
2941: break;
1.80 schwarze 2942: case 'a':
1.77 schwarze 2943: if (operand2 > *res)
2944: *res = operand2;
2945: break;
2946: default:
2947: abort();
2948: }
2949: }
1.149 schwarze 2950: return 1;
1.77 schwarze 2951: }
2952:
1.138 schwarze 2953: /* --- register management ------------------------------------------------ */
2954:
1.52 schwarze 2955: void
1.60 schwarze 2956: roff_setreg(struct roff *r, const char *name, int val, char sign)
1.41 schwarze 2957: {
1.199 schwarze 2958: roff_setregn(r, name, strlen(name), val, sign, INT_MIN);
1.198 schwarze 2959: }
2960:
2961: static void
2962: roff_setregn(struct roff *r, const char *name, size_t len,
1.199 schwarze 2963: int val, char sign, int step)
1.198 schwarze 2964: {
1.52 schwarze 2965: struct roffreg *reg;
2966:
2967: /* Search for an existing register with the same name. */
2968: reg = r->regtab;
2969:
1.198 schwarze 2970: while (reg != NULL && (reg->key.sz != len ||
2971: strncmp(reg->key.p, name, len) != 0))
1.52 schwarze 2972: reg = reg->next;
1.41 schwarze 2973:
1.52 schwarze 2974: if (NULL == reg) {
2975: /* Create a new register. */
2976: reg = mandoc_malloc(sizeof(struct roffreg));
1.198 schwarze 2977: reg->key.p = mandoc_strndup(name, len);
2978: reg->key.sz = len;
1.60 schwarze 2979: reg->val = 0;
1.199 schwarze 2980: reg->step = 0;
1.52 schwarze 2981: reg->next = r->regtab;
2982: r->regtab = reg;
2983: }
2984:
1.60 schwarze 2985: if ('+' == sign)
2986: reg->val += val;
2987: else if ('-' == sign)
2988: reg->val -= val;
2989: else
2990: reg->val = val;
1.199 schwarze 2991: if (step != INT_MIN)
2992: reg->step = step;
1.41 schwarze 2993: }
2994:
1.65 schwarze 2995: /*
2996: * Handle some predefined read-only number registers.
2997: * For now, return -1 if the requested register is not predefined;
2998: * in case a predefined read-only register having the value -1
2999: * were to turn up, another special value would have to be chosen.
3000: */
3001: static int
1.145 schwarze 3002: roff_getregro(const struct roff *r, const char *name)
1.65 schwarze 3003: {
3004:
3005: switch (*name) {
1.145 schwarze 3006: case '$': /* Number of arguments of the last macro evaluated. */
1.211 schwarze 3007: return r->mstackpos < 0 ? 0 : r->mstack[r->mstackpos].argc;
1.80 schwarze 3008: case 'A': /* ASCII approximation mode is always off. */
1.149 schwarze 3009: return 0;
1.80 schwarze 3010: case 'g': /* Groff compatibility mode is always on. */
1.149 schwarze 3011: return 1;
1.80 schwarze 3012: case 'H': /* Fixed horizontal resolution. */
1.149 schwarze 3013: return 24;
1.80 schwarze 3014: case 'j': /* Always adjust left margin only. */
1.149 schwarze 3015: return 0;
1.80 schwarze 3016: case 'T': /* Some output device is always defined. */
1.149 schwarze 3017: return 1;
1.80 schwarze 3018: case 'V': /* Fixed vertical resolution. */
1.149 schwarze 3019: return 40;
1.65 schwarze 3020: default:
1.149 schwarze 3021: return -1;
1.65 schwarze 3022: }
3023: }
3024:
1.53 schwarze 3025: int
1.198 schwarze 3026: roff_getreg(struct roff *r, const char *name)
1.41 schwarze 3027: {
1.199 schwarze 3028: return roff_getregn(r, name, strlen(name), '\0');
1.53 schwarze 3029: }
3030:
3031: static int
1.199 schwarze 3032: roff_getregn(struct roff *r, const char *name, size_t len, char sign)
1.53 schwarze 3033: {
3034: struct roffreg *reg;
1.65 schwarze 3035: int val;
3036:
3037: if ('.' == name[0] && 2 == len) {
1.145 schwarze 3038: val = roff_getregro(r, name + 1);
1.65 schwarze 3039: if (-1 != val)
1.149 schwarze 3040: return val;
1.65 schwarze 3041: }
1.53 schwarze 3042:
1.199 schwarze 3043: for (reg = r->regtab; reg; reg = reg->next) {
1.53 schwarze 3044: if (len == reg->key.sz &&
1.199 schwarze 3045: 0 == strncmp(name, reg->key.p, len)) {
3046: switch (sign) {
3047: case '+':
3048: reg->val += reg->step;
3049: break;
3050: case '-':
3051: reg->val -= reg->step;
3052: break;
3053: default:
3054: break;
3055: }
1.149 schwarze 3056: return reg->val;
1.199 schwarze 3057: }
3058: }
1.143 schwarze 3059:
1.199 schwarze 3060: roff_setregn(r, name, len, 0, '\0', INT_MIN);
1.149 schwarze 3061: return 0;
1.143 schwarze 3062: }
3063:
3064: static int
3065: roff_hasregn(const struct roff *r, const char *name, size_t len)
3066: {
3067: struct roffreg *reg;
3068: int val;
3069:
3070: if ('.' == name[0] && 2 == len) {
1.145 schwarze 3071: val = roff_getregro(r, name + 1);
1.143 schwarze 3072: if (-1 != val)
1.149 schwarze 3073: return 1;
1.143 schwarze 3074: }
3075:
3076: for (reg = r->regtab; reg; reg = reg->next)
3077: if (len == reg->key.sz &&
3078: 0 == strncmp(name, reg->key.p, len))
1.149 schwarze 3079: return 1;
1.41 schwarze 3080:
1.149 schwarze 3081: return 0;
1.41 schwarze 3082: }
3083:
1.52 schwarze 3084: static void
3085: roff_freereg(struct roffreg *reg)
1.41 schwarze 3086: {
1.52 schwarze 3087: struct roffreg *old_reg;
1.41 schwarze 3088:
1.52 schwarze 3089: while (NULL != reg) {
3090: free(reg->key.p);
3091: old_reg = reg;
3092: reg = reg->next;
3093: free(old_reg);
3094: }
1.41 schwarze 3095: }
1.7 schwarze 3096:
1.212 schwarze 3097: static int
1.6 schwarze 3098: roff_nr(ROFF_ARGS)
1.1 schwarze 3099: {
1.199 schwarze 3100: char *key, *val, *step;
1.85 schwarze 3101: size_t keysz;
1.199 schwarze 3102: int iv, is, len;
1.60 schwarze 3103: char sign;
1.6 schwarze 3104:
1.110 schwarze 3105: key = val = buf->buf + pos;
3106: if (*key == '\0')
1.149 schwarze 3107: return ROFF_IGN;
1.85 schwarze 3108:
3109: keysz = roff_getname(r, &val, ln, pos);
1.235 schwarze 3110: if (key[keysz] == '\\' || key[keysz] == '\t')
1.149 schwarze 3111: return ROFF_IGN;
1.6 schwarze 3112:
1.60 schwarze 3113: sign = *val;
1.110 schwarze 3114: if (sign == '+' || sign == '-')
1.60 schwarze 3115: val++;
3116:
1.199 schwarze 3117: len = 0;
3118: if (roff_evalnum(r, ln, val, &len, &iv, ROFFNUM_SCALE) == 0)
3119: return ROFF_IGN;
3120:
3121: step = val + len;
3122: while (isspace((unsigned char)*step))
3123: step++;
3124: if (roff_evalnum(r, ln, step, NULL, &is, 0) == 0)
3125: is = INT_MIN;
1.1 schwarze 3126:
1.199 schwarze 3127: roff_setregn(r, key, keysz, iv, sign, is);
1.149 schwarze 3128: return ROFF_IGN;
1.76 schwarze 3129: }
3130:
1.212 schwarze 3131: static int
1.76 schwarze 3132: roff_rr(ROFF_ARGS)
3133: {
3134: struct roffreg *reg, **prev;
1.85 schwarze 3135: char *name, *cp;
3136: size_t namesz;
1.76 schwarze 3137:
1.110 schwarze 3138: name = cp = buf->buf + pos;
3139: if (*name == '\0')
1.149 schwarze 3140: return ROFF_IGN;
1.85 schwarze 3141: namesz = roff_getname(r, &cp, ln, pos);
3142: name[namesz] = '\0';
1.76 schwarze 3143:
3144: prev = &r->regtab;
3145: while (1) {
3146: reg = *prev;
1.110 schwarze 3147: if (reg == NULL || !strcmp(name, reg->key.p))
1.76 schwarze 3148: break;
3149: prev = ®->next;
3150: }
1.110 schwarze 3151: if (reg != NULL) {
1.76 schwarze 3152: *prev = reg->next;
3153: free(reg->key.p);
3154: free(reg);
3155: }
1.149 schwarze 3156: return ROFF_IGN;
1.29 schwarze 3157: }
3158:
1.138 schwarze 3159: /* --- handler functions for roff requests -------------------------------- */
3160:
1.212 schwarze 3161: static int
1.29 schwarze 3162: roff_rm(ROFF_ARGS)
3163: {
3164: const char *name;
3165: char *cp;
1.85 schwarze 3166: size_t namesz;
1.29 schwarze 3167:
1.110 schwarze 3168: cp = buf->buf + pos;
3169: while (*cp != '\0') {
1.85 schwarze 3170: name = cp;
1.110 schwarze 3171: namesz = roff_getname(r, &cp, ln, (int)(cp - buf->buf));
1.85 schwarze 3172: roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
1.183 schwarze 3173: roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.235 schwarze 3174: if (name[namesz] == '\\' || name[namesz] == '\t')
1.85 schwarze 3175: break;
1.29 schwarze 3176: }
1.149 schwarze 3177: return ROFF_IGN;
1.51 schwarze 3178: }
3179:
1.212 schwarze 3180: static int
1.51 schwarze 3181: roff_it(ROFF_ARGS)
3182: {
3183: int iv;
3184:
3185: /* Parse the number of lines. */
1.133 schwarze 3186:
3187: if ( ! roff_evalnum(r, ln, buf->buf, &pos, &iv, 0)) {
1.222 schwarze 3188: mandoc_msg(MANDOCERR_IT_NONUM,
3189: ln, ppos, "%s", buf->buf + 1);
1.149 schwarze 3190: return ROFF_IGN;
1.51 schwarze 3191: }
3192:
1.134 schwarze 3193: while (isspace((unsigned char)buf->buf[pos]))
3194: pos++;
3195:
3196: /*
3197: * Arm the input line trap.
3198: * Special-casing "an-trap" is an ugly workaround to cope
3199: * with DocBook stupidly fiddling with man(7) internals.
3200: */
1.133 schwarze 3201:
1.51 schwarze 3202: roffit_lines = iv;
1.134 schwarze 3203: roffit_macro = mandoc_strdup(iv != 1 ||
3204: strcmp(buf->buf + pos, "an-trap") ?
3205: buf->buf + pos : "br");
1.149 schwarze 3206: return ROFF_IGN;
1.47 schwarze 3207: }
3208:
1.212 schwarze 3209: static int
1.47 schwarze 3210: roff_Dd(ROFF_ARGS)
3211: {
1.187 schwarze 3212: int mask;
3213: enum roff_tok t, te;
1.99 schwarze 3214:
1.187 schwarze 3215: switch (tok) {
3216: case ROFF_Dd:
3217: tok = MDOC_Dd;
3218: te = MDOC_MAX;
3219: if (r->format == 0)
3220: r->format = MPARSE_MDOC;
3221: mask = MPARSE_MDOC | MPARSE_QUICK;
3222: break;
3223: case ROFF_TH:
3224: tok = MAN_TH;
3225: te = MAN_MAX;
3226: if (r->format == 0)
3227: r->format = MPARSE_MAN;
3228: mask = MPARSE_QUICK;
3229: break;
3230: default:
3231: abort();
3232: }
3233: if ((r->options & mask) == 0)
3234: for (t = tok; t < te; t++)
3235: roff_setstr(r, roff_name[t], NULL, 0);
1.149 schwarze 3236: return ROFF_CONT;
1.14 schwarze 3237: }
3238:
1.212 schwarze 3239: static int
1.27 schwarze 3240: roff_TE(ROFF_ARGS)
3241: {
1.233 schwarze 3242: r->man->flags &= ~ROFF_NONOFILL;
1.193 schwarze 3243: if (r->tbl == NULL) {
1.222 schwarze 3244: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "TE");
1.193 schwarze 3245: return ROFF_IGN;
3246: }
1.218 schwarze 3247: if (tbl_end(r->tbl, 0) == 0) {
1.193 schwarze 3248: r->tbl = NULL;
1.130 schwarze 3249: free(buf->buf);
3250: buf->buf = mandoc_strdup(".sp");
3251: buf->sz = 4;
1.201 schwarze 3252: *offs = 0;
1.149 schwarze 3253: return ROFF_REPARSE;
1.130 schwarze 3254: }
1.193 schwarze 3255: r->tbl = NULL;
1.149 schwarze 3256: return ROFF_IGN;
1.27 schwarze 3257: }
3258:
1.212 schwarze 3259: static int
1.27 schwarze 3260: roff_T_(ROFF_ARGS)
3261: {
3262:
3263: if (NULL == r->tbl)
1.222 schwarze 3264: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "T&");
1.27 schwarze 3265: else
1.168 schwarze 3266: tbl_restart(ln, ppos, r->tbl);
1.27 schwarze 3267:
1.149 schwarze 3268: return ROFF_IGN;
1.27 schwarze 3269: }
3270:
1.102 schwarze 3271: /*
3272: * Handle in-line equation delimiters.
3273: */
1.212 schwarze 3274: static int
1.110 schwarze 3275: roff_eqndelim(struct roff *r, struct buf *buf, int pos)
1.41 schwarze 3276: {
1.105 schwarze 3277: char *cp1, *cp2;
3278: const char *bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
1.41 schwarze 3279:
1.102 schwarze 3280: /*
3281: * Outside equations, look for an opening delimiter.
3282: * If we are inside an equation, we already know it is
3283: * in-line, or this function wouldn't have been called;
3284: * so look for a closing delimiter.
3285: */
3286:
1.110 schwarze 3287: cp1 = buf->buf + pos;
1.102 schwarze 3288: cp2 = strchr(cp1, r->eqn == NULL ?
3289: r->last_eqn->odelim : r->last_eqn->cdelim);
3290: if (cp2 == NULL)
1.149 schwarze 3291: return ROFF_CONT;
1.102 schwarze 3292:
1.105 schwarze 3293: *cp2++ = '\0';
3294: bef_pr = bef_nl = aft_nl = aft_pr = "";
3295:
3296: /* Handle preceding text, protecting whitespace. */
3297:
1.110 schwarze 3298: if (*buf->buf != '\0') {
1.105 schwarze 3299: if (r->eqn == NULL)
3300: bef_pr = "\\&";
3301: bef_nl = "\n";
3302: }
3303:
3304: /*
3305: * Prepare replacing the delimiter with an equation macro
3306: * and drop leading white space from the equation.
3307: */
1.102 schwarze 3308:
1.105 schwarze 3309: if (r->eqn == NULL) {
3310: while (*cp2 == ' ')
3311: cp2++;
3312: mac = ".EQ";
3313: } else
3314: mac = ".EN";
3315:
3316: /* Handle following text, protecting whitespace. */
3317:
3318: if (*cp2 != '\0') {
3319: aft_nl = "\n";
3320: if (r->eqn != NULL)
3321: aft_pr = "\\&";
3322: }
3323:
3324: /* Do the actual replacement. */
3325:
1.110 schwarze 3326: buf->sz = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", buf->buf,
1.105 schwarze 3327: bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
1.110 schwarze 3328: free(buf->buf);
3329: buf->buf = cp1;
1.102 schwarze 3330:
3331: /* Toggle the in-line state of the eqn subsystem. */
3332:
3333: r->eqn_inline = r->eqn == NULL;
1.149 schwarze 3334: return ROFF_REPARSE;
1.41 schwarze 3335: }
3336:
1.212 schwarze 3337: static int
1.107 schwarze 3338: roff_EQ(ROFF_ARGS)
1.32 schwarze 3339: {
1.191 schwarze 3340: struct roff_node *n;
3341:
1.228 schwarze 3342: if (r->man->meta.macroset == MACROSET_MAN)
1.194 schwarze 3343: man_breakscope(r->man, ROFF_EQ);
1.191 schwarze 3344: n = roff_node_alloc(r->man, ln, ppos, ROFFT_EQN, TOKEN_NONE);
3345: if (ln > r->man->last->line)
3346: n->flags |= NODE_LINE;
1.220 schwarze 3347: n->eqn = eqn_box_new();
1.191 schwarze 3348: roff_node_append(r->man, n);
3349: r->man->next = ROFF_NEXT_SIBLING;
1.32 schwarze 3350:
1.110 schwarze 3351: assert(r->eqn == NULL);
1.191 schwarze 3352: if (r->last_eqn == NULL)
1.223 schwarze 3353: r->last_eqn = eqn_alloc();
1.191 schwarze 3354: else
3355: eqn_reset(r->last_eqn);
3356: r->eqn = r->last_eqn;
3357: r->eqn->node = n;
1.41 schwarze 3358:
1.110 schwarze 3359: if (buf->buf[pos] != '\0')
1.222 schwarze 3360: mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
1.110 schwarze 3361: ".EQ %s", buf->buf + pos);
1.41 schwarze 3362:
1.149 schwarze 3363: return ROFF_IGN;
1.32 schwarze 3364: }
3365:
1.212 schwarze 3366: static int
1.32 schwarze 3367: roff_EN(ROFF_ARGS)
3368: {
1.191 schwarze 3369: if (r->eqn != NULL) {
3370: eqn_parse(r->eqn);
3371: r->eqn = NULL;
3372: } else
1.222 schwarze 3373: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "EN");
1.191 schwarze 3374: if (buf->buf[pos] != '\0')
1.222 schwarze 3375: mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
1.191 schwarze 3376: "EN %s", buf->buf + pos);
1.149 schwarze 3377: return ROFF_IGN;
1.32 schwarze 3378: }
3379:
1.212 schwarze 3380: static int
1.27 schwarze 3381: roff_TS(ROFF_ARGS)
3382: {
1.193 schwarze 3383: if (r->tbl != NULL) {
1.222 schwarze 3384: mandoc_msg(MANDOCERR_BLK_BROKEN, ln, ppos, "TS breaks TS");
1.218 schwarze 3385: tbl_end(r->tbl, 0);
1.27 schwarze 3386: }
1.233 schwarze 3387: r->man->flags |= ROFF_NONOFILL;
1.223 schwarze 3388: r->tbl = tbl_alloc(ppos, ln, r->last_tbl);
1.218 schwarze 3389: if (r->last_tbl == NULL)
1.193 schwarze 3390: r->first_tbl = r->tbl;
3391: r->last_tbl = r->tbl;
1.169 schwarze 3392: return ROFF_IGN;
3393: }
3394:
1.212 schwarze 3395: static int
1.230 schwarze 3396: roff_noarg(ROFF_ARGS)
3397: {
3398: if (r->man->flags & (MAN_BLINE | MAN_ELINE))
3399: man_breakscope(r->man, tok);
3400: if (tok == ROFF_brp)
3401: tok = ROFF_br;
3402: roff_elem_alloc(r->man, ln, ppos, tok);
3403: if (buf->buf[pos] != '\0')
3404: mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
3405: "%s %s", roff_name[tok], buf->buf + pos);
3406: if (tok == ROFF_nf)
3407: r->man->flags |= ROFF_NOFILL;
3408: else if (tok == ROFF_fi)
3409: r->man->flags &= ~ROFF_NOFILL;
3410: r->man->last->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
3411: r->man->next = ROFF_NEXT_SIBLING;
3412: return ROFF_IGN;
3413: }
3414:
3415: static int
1.169 schwarze 3416: roff_onearg(ROFF_ARGS)
3417: {
3418: struct roff_node *n;
3419: char *cp;
1.177 schwarze 3420: int npos;
1.169 schwarze 3421:
1.174 schwarze 3422: if (r->man->flags & (MAN_BLINE | MAN_ELINE) &&
1.192 schwarze 3423: (tok == ROFF_ce || tok == ROFF_rj || tok == ROFF_sp ||
3424: tok == ROFF_ti))
1.174 schwarze 3425: man_breakscope(r->man, tok);
3426:
1.181 schwarze 3427: if (roffce_node != NULL && (tok == ROFF_ce || tok == ROFF_rj)) {
1.177 schwarze 3428: r->man->last = roffce_node;
3429: r->man->next = ROFF_NEXT_SIBLING;
3430: }
3431:
1.169 schwarze 3432: roff_elem_alloc(r->man, ln, ppos, tok);
3433: n = r->man->last;
3434:
3435: cp = buf->buf + pos;
3436: if (*cp != '\0') {
3437: while (*cp != '\0' && *cp != ' ')
3438: cp++;
3439: while (*cp == ' ')
3440: *cp++ = '\0';
3441: if (*cp != '\0')
1.222 schwarze 3442: mandoc_msg(MANDOCERR_ARG_EXCESS,
3443: ln, (int)(cp - buf->buf),
1.169 schwarze 3444: "%s ... %s", roff_name[tok], cp);
3445: roff_word_alloc(r->man, ln, pos, buf->buf + pos);
1.172 schwarze 3446: }
3447:
1.181 schwarze 3448: if (tok == ROFF_ce || tok == ROFF_rj) {
3449: if (r->man->last->type == ROFFT_ELEM) {
1.177 schwarze 3450: roff_word_alloc(r->man, ln, pos, "1");
3451: r->man->last->flags |= NODE_NOSRC;
3452: }
3453: npos = 0;
3454: if (roff_evalnum(r, ln, r->man->last->string, &npos,
3455: &roffce_lines, 0) == 0) {
1.222 schwarze 3456: mandoc_msg(MANDOCERR_CE_NONUM,
3457: ln, pos, "ce %s", buf->buf + pos);
1.177 schwarze 3458: roffce_lines = 1;
3459: }
3460: if (roffce_lines < 1) {
3461: r->man->last = r->man->last->parent;
3462: roffce_node = NULL;
3463: roffce_lines = 0;
3464: } else
3465: roffce_node = r->man->last->parent;
3466: } else {
3467: n->flags |= NODE_VALID | NODE_ENDED;
3468: r->man->last = n;
3469: }
3470: n->flags |= NODE_LINE;
1.172 schwarze 3471: r->man->next = ROFF_NEXT_SIBLING;
3472: return ROFF_IGN;
3473: }
3474:
1.212 schwarze 3475: static int
1.172 schwarze 3476: roff_manyarg(ROFF_ARGS)
3477: {
3478: struct roff_node *n;
3479: char *sp, *ep;
3480:
3481: roff_elem_alloc(r->man, ln, ppos, tok);
3482: n = r->man->last;
3483:
3484: for (sp = ep = buf->buf + pos; *sp != '\0'; sp = ep) {
3485: while (*ep != '\0' && *ep != ' ')
3486: ep++;
3487: while (*ep == ' ')
3488: *ep++ = '\0';
3489: roff_word_alloc(r->man, ln, sp - buf->buf, sp);
1.169 schwarze 3490: }
3491:
3492: n->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
3493: r->man->last = n;
3494: r->man->next = ROFF_NEXT_SIBLING;
1.149 schwarze 3495: return ROFF_IGN;
1.123 schwarze 3496: }
3497:
1.212 schwarze 3498: static int
1.183 schwarze 3499: roff_als(ROFF_ARGS)
3500: {
3501: char *oldn, *newn, *end, *value;
3502: size_t oldsz, newsz, valsz;
3503:
3504: newn = oldn = buf->buf + pos;
3505: if (*newn == '\0')
3506: return ROFF_IGN;
3507:
3508: newsz = roff_getname(r, &oldn, ln, pos);
1.235 schwarze 3509: if (newn[newsz] == '\\' || newn[newsz] == '\t' || *oldn == '\0')
1.183 schwarze 3510: return ROFF_IGN;
3511:
3512: end = oldn;
3513: oldsz = roff_getname(r, &end, ln, oldn - buf->buf);
3514: if (oldsz == 0)
3515: return ROFF_IGN;
3516:
1.210 schwarze 3517: valsz = mandoc_asprintf(&value, ".%.*s \\$@\\\"\n",
1.187 schwarze 3518: (int)oldsz, oldn);
1.183 schwarze 3519: roff_setstrn(&r->strtab, newn, newsz, value, valsz, 0);
3520: roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
3521: free(value);
1.236 schwarze 3522: return ROFF_IGN;
3523: }
3524:
3525: /*
3526: * The .break request only makes sense inside conditionals,
3527: * and that case is already handled in roff_cond_sub().
3528: */
3529: static int
3530: roff_break(ROFF_ARGS)
3531: {
3532: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, pos, "break");
1.168 schwarze 3533: return ROFF_IGN;
1.27 schwarze 3534: }
3535:
1.212 schwarze 3536: static int
1.48 schwarze 3537: roff_cc(ROFF_ARGS)
3538: {
3539: const char *p;
3540:
1.110 schwarze 3541: p = buf->buf + pos;
1.48 schwarze 3542:
1.110 schwarze 3543: if (*p == '\0' || (r->control = *p++) == '.')
1.175 schwarze 3544: r->control = '\0';
1.48 schwarze 3545:
1.110 schwarze 3546: if (*p != '\0')
1.222 schwarze 3547: mandoc_msg(MANDOCERR_ARG_EXCESS,
1.132 schwarze 3548: ln, p - buf->buf, "cc ... %s", p);
1.48 schwarze 3549:
1.213 schwarze 3550: return ROFF_IGN;
3551: }
3552:
3553: static int
3554: roff_char(ROFF_ARGS)
3555: {
3556: const char *p, *kp, *vp;
3557: size_t ksz, vsz;
3558: int font;
3559:
3560: /* Parse the character to be replaced. */
3561:
3562: kp = buf->buf + pos;
3563: p = kp + 1;
3564: if (*kp == '\0' || (*kp == '\\' &&
3565: mandoc_escape(&p, NULL, NULL) != ESCAPE_SPECIAL) ||
3566: (*p != ' ' && *p != '\0')) {
1.222 schwarze 3567: mandoc_msg(MANDOCERR_CHAR_ARG, ln, pos, "char %s", kp);
1.213 schwarze 3568: return ROFF_IGN;
3569: }
3570: ksz = p - kp;
3571: while (*p == ' ')
3572: p++;
3573:
3574: /*
3575: * If the replacement string contains a font escape sequence,
3576: * we have to restore the font at the end.
3577: */
3578:
3579: vp = p;
3580: vsz = strlen(p);
3581: font = 0;
3582: while (*p != '\0') {
3583: if (*p++ != '\\')
3584: continue;
3585: switch (mandoc_escape(&p, NULL, NULL)) {
3586: case ESCAPE_FONT:
3587: case ESCAPE_FONTROMAN:
3588: case ESCAPE_FONTITALIC:
3589: case ESCAPE_FONTBOLD:
3590: case ESCAPE_FONTBI:
1.214 schwarze 3591: case ESCAPE_FONTCW:
1.213 schwarze 3592: case ESCAPE_FONTPREV:
3593: font++;
3594: break;
3595: default:
3596: break;
3597: }
3598: }
3599: if (font > 1)
1.222 schwarze 3600: mandoc_msg(MANDOCERR_CHAR_FONT,
3601: ln, (int)(vp - buf->buf), "%s", vp);
1.213 schwarze 3602:
3603: /*
3604: * Approximate the effect of .char using the .tr tables.
3605: * XXX In groff, .char and .tr interact differently.
3606: */
3607:
3608: if (ksz == 1) {
3609: if (r->xtab == NULL)
3610: r->xtab = mandoc_calloc(128, sizeof(*r->xtab));
3611: assert((unsigned int)*kp < 128);
3612: free(r->xtab[(int)*kp].p);
3613: r->xtab[(int)*kp].sz = mandoc_asprintf(&r->xtab[(int)*kp].p,
3614: "%s%s", vp, font ? "\fP" : "");
3615: } else {
3616: roff_setstrn(&r->xmbtab, kp, ksz, vp, vsz, 0);
3617: if (font)
3618: roff_setstrn(&r->xmbtab, kp, ksz, "\\fP", 3, 1);
3619: }
1.149 schwarze 3620: return ROFF_IGN;
1.48 schwarze 3621: }
3622:
1.212 schwarze 3623: static int
1.175 schwarze 3624: roff_ec(ROFF_ARGS)
3625: {
3626: const char *p;
3627:
3628: p = buf->buf + pos;
3629: if (*p == '\0')
3630: r->escape = '\\';
3631: else {
3632: r->escape = *p;
3633: if (*++p != '\0')
1.222 schwarze 3634: mandoc_msg(MANDOCERR_ARG_EXCESS, ln,
3635: (int)(p - buf->buf), "ec ... %s", p);
1.175 schwarze 3636: }
3637: return ROFF_IGN;
3638: }
3639:
1.212 schwarze 3640: static int
1.175 schwarze 3641: roff_eo(ROFF_ARGS)
3642: {
3643: r->escape = '\0';
3644: if (buf->buf[pos] != '\0')
1.222 schwarze 3645: mandoc_msg(MANDOCERR_ARG_SKIP,
1.175 schwarze 3646: ln, pos, "eo %s", buf->buf + pos);
3647: return ROFF_IGN;
1.202 schwarze 3648: }
3649:
1.212 schwarze 3650: static int
1.202 schwarze 3651: roff_nop(ROFF_ARGS)
3652: {
3653: while (buf->buf[pos] == ' ')
3654: pos++;
3655: *offs = pos;
3656: return ROFF_RERUN;
1.175 schwarze 3657: }
3658:
1.212 schwarze 3659: static int
1.42 schwarze 3660: roff_tr(ROFF_ARGS)
3661: {
3662: const char *p, *first, *second;
3663: size_t fsz, ssz;
3664: enum mandoc_esc esc;
3665:
1.110 schwarze 3666: p = buf->buf + pos;
1.42 schwarze 3667:
1.110 schwarze 3668: if (*p == '\0') {
1.222 schwarze 3669: mandoc_msg(MANDOCERR_REQ_EMPTY, ln, ppos, "tr");
1.149 schwarze 3670: return ROFF_IGN;
1.42 schwarze 3671: }
3672:
1.110 schwarze 3673: while (*p != '\0') {
1.42 schwarze 3674: fsz = ssz = 1;
3675:
3676: first = p++;
1.110 schwarze 3677: if (*first == '\\') {
1.42 schwarze 3678: esc = mandoc_escape(&p, NULL, NULL);
1.110 schwarze 3679: if (esc == ESCAPE_ERROR) {
1.222 schwarze 3680: mandoc_msg(MANDOCERR_ESC_BAD, ln,
3681: (int)(p - buf->buf), "%s", first);
1.149 schwarze 3682: return ROFF_IGN;
1.42 schwarze 3683: }
3684: fsz = (size_t)(p - first);
3685: }
3686:
3687: second = p++;
1.110 schwarze 3688: if (*second == '\\') {
1.42 schwarze 3689: esc = mandoc_escape(&p, NULL, NULL);
1.110 schwarze 3690: if (esc == ESCAPE_ERROR) {
1.222 schwarze 3691: mandoc_msg(MANDOCERR_ESC_BAD, ln,
3692: (int)(p - buf->buf), "%s", second);
1.149 schwarze 3693: return ROFF_IGN;
1.42 schwarze 3694: }
3695: ssz = (size_t)(p - second);
1.110 schwarze 3696: } else if (*second == '\0') {
1.222 schwarze 3697: mandoc_msg(MANDOCERR_TR_ODD, ln,
3698: (int)(first - buf->buf), "tr %s", first);
1.42 schwarze 3699: second = " ";
3700: p--;
3701: }
3702:
3703: if (fsz > 1) {
1.80 schwarze 3704: roff_setstrn(&r->xmbtab, first, fsz,
3705: second, ssz, 0);
1.42 schwarze 3706: continue;
3707: }
3708:
1.110 schwarze 3709: if (r->xtab == NULL)
1.80 schwarze 3710: r->xtab = mandoc_calloc(128,
3711: sizeof(struct roffstr));
1.42 schwarze 3712:
3713: free(r->xtab[(int)*first].p);
3714: r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
3715: r->xtab[(int)*first].sz = ssz;
3716: }
3717:
1.149 schwarze 3718: return ROFF_IGN;
1.42 schwarze 3719: }
3720:
1.211 schwarze 3721: /*
3722: * Implementation of the .return request.
3723: * There is no need to call roff_userret() from here.
3724: * The read module will call that after rewinding the reader stack
3725: * to the place from where the current macro was called.
3726: */
1.212 schwarze 3727: static int
1.211 schwarze 3728: roff_return(ROFF_ARGS)
3729: {
3730: if (r->mstackpos >= 0)
1.212 schwarze 3731: return ROFF_IGN | ROFF_USERRET;
1.211 schwarze 3732:
1.222 schwarze 3733: mandoc_msg(MANDOCERR_REQ_NOMAC, ln, ppos, "return");
1.211 schwarze 3734: return ROFF_IGN;
3735: }
3736:
1.212 schwarze 3737: static int
1.178 schwarze 3738: roff_rn(ROFF_ARGS)
3739: {
3740: const char *value;
3741: char *oldn, *newn, *end;
3742: size_t oldsz, newsz;
1.187 schwarze 3743: int deftype;
1.178 schwarze 3744:
3745: oldn = newn = buf->buf + pos;
3746: if (*oldn == '\0')
3747: return ROFF_IGN;
3748:
3749: oldsz = roff_getname(r, &newn, ln, pos);
1.235 schwarze 3750: if (oldn[oldsz] == '\\' || oldn[oldsz] == '\t' || *newn == '\0')
1.178 schwarze 3751: return ROFF_IGN;
3752:
3753: end = newn;
3754: newsz = roff_getname(r, &end, ln, newn - buf->buf);
3755: if (newsz == 0)
3756: return ROFF_IGN;
3757:
1.187 schwarze 3758: deftype = ROFFDEF_ANY;
3759: value = roff_getstrn(r, oldn, oldsz, &deftype);
3760: switch (deftype) {
3761: case ROFFDEF_USER:
1.178 schwarze 3762: roff_setstrn(&r->strtab, newn, newsz, value, strlen(value), 0);
3763: roff_setstrn(&r->strtab, oldn, oldsz, NULL, 0, 0);
3764: roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
1.187 schwarze 3765: break;
3766: case ROFFDEF_PRE:
3767: roff_setstrn(&r->strtab, newn, newsz, value, strlen(value), 0);
3768: roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
3769: break;
3770: case ROFFDEF_REN:
1.178 schwarze 3771: roff_setstrn(&r->rentab, newn, newsz, value, strlen(value), 0);
3772: roff_setstrn(&r->rentab, oldn, oldsz, NULL, 0, 0);
1.187 schwarze 3773: roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
3774: break;
3775: case ROFFDEF_STD:
1.178 schwarze 3776: roff_setstrn(&r->rentab, newn, newsz, oldn, oldsz, 0);
1.187 schwarze 3777: roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
3778: break;
3779: default:
3780: roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
3781: roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
3782: break;
3783: }
1.178 schwarze 3784: return ROFF_IGN;
3785: }
3786:
1.212 schwarze 3787: static int
1.211 schwarze 3788: roff_shift(ROFF_ARGS)
3789: {
3790: struct mctx *ctx;
3791: int levels, i;
3792:
3793: levels = 1;
3794: if (buf->buf[pos] != '\0' &&
3795: roff_evalnum(r, ln, buf->buf, &pos, &levels, 0) == 0) {
1.222 schwarze 3796: mandoc_msg(MANDOCERR_CE_NONUM,
1.211 schwarze 3797: ln, pos, "shift %s", buf->buf + pos);
3798: levels = 1;
3799: }
3800: if (r->mstackpos < 0) {
1.222 schwarze 3801: mandoc_msg(MANDOCERR_REQ_NOMAC, ln, ppos, "shift");
1.211 schwarze 3802: return ROFF_IGN;
3803: }
3804: ctx = r->mstack + r->mstackpos;
3805: if (levels > ctx->argc) {
1.222 schwarze 3806: mandoc_msg(MANDOCERR_SHIFT,
1.211 schwarze 3807: ln, pos, "%d, but max is %d", levels, ctx->argc);
3808: levels = ctx->argc;
3809: }
3810: if (levels == 0)
3811: return ROFF_IGN;
3812: for (i = 0; i < levels; i++)
3813: free(ctx->argv[i]);
3814: ctx->argc -= levels;
3815: for (i = 0; i < ctx->argc; i++)
3816: ctx->argv[i] = ctx->argv[i + levels];
3817: return ROFF_IGN;
3818: }
3819:
1.212 schwarze 3820: static int
1.14 schwarze 3821: roff_so(ROFF_ARGS)
3822: {
1.121 schwarze 3823: char *name, *cp;
1.15 schwarze 3824:
1.110 schwarze 3825: name = buf->buf + pos;
1.222 schwarze 3826: mandoc_msg(MANDOCERR_SO, ln, ppos, "so %s", name);
1.14 schwarze 3827:
1.22 schwarze 3828: /*
3829: * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3830: * opening anything that's not in our cwd or anything beneath
3831: * it. Thus, explicitly disallow traversing up the file-system
3832: * or using absolute paths.
3833: */
3834:
1.110 schwarze 3835: if (*name == '/' || strstr(name, "../") || strstr(name, "/..")) {
1.222 schwarze 3836: mandoc_msg(MANDOCERR_SO_PATH, ln, ppos, ".so %s", name);
1.121 schwarze 3837: buf->sz = mandoc_asprintf(&cp,
3838: ".sp\nSee the file %s.\n.sp", name) + 1;
3839: free(buf->buf);
3840: buf->buf = cp;
3841: *offs = 0;
1.149 schwarze 3842: return ROFF_REPARSE;
1.14 schwarze 3843: }
3844:
3845: *offs = pos;
1.149 schwarze 3846: return ROFF_SO;
1.7 schwarze 3847: }
3848:
1.138 schwarze 3849: /* --- user defined strings and macros ------------------------------------ */
3850:
1.212 schwarze 3851: static int
1.16 schwarze 3852: roff_userdef(ROFF_ARGS)
1.12 schwarze 3853: {
1.211 schwarze 3854: struct mctx *ctx;
3855: char *arg, *ap, *dst, *src;
3856: size_t sz;
3857:
1.237 schwarze 3858: /* If the macro is empty, ignore it altogether. */
3859:
3860: if (*r->current_string == '\0')
3861: return ROFF_IGN;
3862:
1.211 schwarze 3863: /* Initialize a new macro stack context. */
3864:
3865: if (++r->mstackpos == r->mstacksz) {
3866: r->mstack = mandoc_recallocarray(r->mstack,
3867: r->mstacksz, r->mstacksz + 8, sizeof(*r->mstack));
3868: r->mstacksz += 8;
3869: }
3870: ctx = r->mstack + r->mstackpos;
3871: ctx->argsz = 0;
3872: ctx->argc = 0;
3873: ctx->argv = NULL;
3874:
3875: /*
3876: * Collect pointers to macro argument strings,
3877: * NUL-terminating them and escaping quotes.
3878: */
3879:
3880: src = buf->buf + pos;
3881: while (*src != '\0') {
3882: if (ctx->argc == ctx->argsz) {
3883: ctx->argsz += 8;
3884: ctx->argv = mandoc_reallocarray(ctx->argv,
3885: ctx->argsz, sizeof(*ctx->argv));
3886: }
1.227 schwarze 3887: arg = roff_getarg(r, &src, ln, &pos);
1.211 schwarze 3888: sz = 1; /* For the terminating NUL. */
3889: for (ap = arg; *ap != '\0'; ap++)
3890: sz += *ap == '"' ? 4 : 1;
3891: ctx->argv[ctx->argc++] = dst = mandoc_malloc(sz);
3892: for (ap = arg; *ap != '\0'; ap++) {
3893: if (*ap == '"') {
3894: memcpy(dst, "\\(dq", 4);
3895: dst += 4;
3896: } else
3897: *dst++ = *ap;
1.16 schwarze 3898: }
1.211 schwarze 3899: *dst = '\0';
1.227 schwarze 3900: free(arg);
1.209 schwarze 3901: }
3902:
1.211 schwarze 3903: /* Replace the macro invocation by the macro definition. */
1.135 schwarze 3904:
1.110 schwarze 3905: free(buf->buf);
1.211 schwarze 3906: buf->buf = mandoc_strdup(r->current_string);
3907: buf->sz = strlen(buf->buf) + 1;
1.120 schwarze 3908: *offs = 0;
1.16 schwarze 3909:
1.237 schwarze 3910: return buf->buf[buf->sz - 2] == '\n' ?
1.212 schwarze 3911: ROFF_REPARSE | ROFF_USERCALL : ROFF_IGN | ROFF_APPEND;
1.12 schwarze 3912: }
1.28 schwarze 3913:
1.178 schwarze 3914: /*
3915: * Calling a high-level macro that was renamed with .rn.
3916: * r->current_string has already been set up by roff_parse().
3917: */
1.212 schwarze 3918: static int
1.178 schwarze 3919: roff_renamed(ROFF_ARGS)
3920: {
3921: char *nbuf;
3922:
1.187 schwarze 3923: buf->sz = mandoc_asprintf(&nbuf, ".%s%s%s", r->current_string,
3924: buf->buf[pos] == '\0' ? "" : " ", buf->buf + pos) + 1;
1.178 schwarze 3925: free(buf->buf);
3926: buf->buf = nbuf;
1.201 schwarze 3927: *offs = 0;
1.178 schwarze 3928: return ROFF_CONT;
3929: }
3930:
1.234 schwarze 3931: /*
3932: * Measure the length in bytes of the roff identifier at *cpp
3933: * and advance the pointer to the next word.
3934: */
1.85 schwarze 3935: static size_t
1.28 schwarze 3936: roff_getname(struct roff *r, char **cpp, int ln, int pos)
3937: {
3938: char *name, *cp;
1.85 schwarze 3939: size_t namesz;
1.28 schwarze 3940:
3941: name = *cpp;
1.234 schwarze 3942: if (*name == '\0')
1.149 schwarze 3943: return 0;
1.28 schwarze 3944:
1.234 schwarze 3945: /* Advance cp to the byte after the end of the name. */
3946:
1.85 schwarze 3947: for (cp = name; 1; cp++) {
1.234 schwarze 3948: namesz = cp - name;
1.235 schwarze 3949: if (*cp == '\0')
1.85 schwarze 3950: break;
1.235 schwarze 3951: if (*cp == ' ' || *cp == '\t') {
3952: cp++;
3953: break;
3954: }
1.234 schwarze 3955: if (*cp != '\\')
1.28 schwarze 3956: continue;
1.234 schwarze 3957: if (cp[1] == '{' || cp[1] == '}')
1.88 schwarze 3958: break;
1.234 schwarze 3959: if (*++cp == '\\')
1.28 schwarze 3960: continue;
1.222 schwarze 3961: mandoc_msg(MANDOCERR_NAMESC, ln, pos,
1.97 schwarze 3962: "%.*s", (int)(cp - name + 1), name);
1.85 schwarze 3963: mandoc_escape((const char **)&cp, NULL, NULL);
3964: break;
1.28 schwarze 3965: }
3966:
3967: /* Read past spaces. */
1.234 schwarze 3968:
3969: while (*cp == ' ')
1.28 schwarze 3970: cp++;
3971:
3972: *cpp = cp;
1.149 schwarze 3973: return namesz;
1.28 schwarze 3974: }
3975:
1.16 schwarze 3976: /*
3977: * Store *string into the user-defined string called *name.
3978: * To clear an existing entry, call with (*r, *name, NULL, 0).
1.66 schwarze 3979: * append == 0: replace mode
3980: * append == 1: single-line append mode
3981: * append == 2: multiline append mode, append '\n' after each call
1.16 schwarze 3982: */
1.8 schwarze 3983: static void
1.16 schwarze 3984: roff_setstr(struct roff *r, const char *name, const char *string,
1.66 schwarze 3985: int append)
1.7 schwarze 3986: {
1.187 schwarze 3987: size_t namesz;
1.42 schwarze 3988:
1.187 schwarze 3989: namesz = strlen(name);
3990: roff_setstrn(&r->strtab, name, namesz, string,
1.80 schwarze 3991: string ? strlen(string) : 0, append);
1.187 schwarze 3992: roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.42 schwarze 3993: }
3994:
3995: static void
3996: roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1.66 schwarze 3997: const char *string, size_t stringsz, int append)
1.42 schwarze 3998: {
3999: struct roffkv *n;
4000: char *c;
4001: int i;
4002: size_t oldch, newch;
1.7 schwarze 4003:
1.16 schwarze 4004: /* Search for an existing string with the same name. */
1.42 schwarze 4005: n = *r;
4006:
1.84 schwarze 4007: while (n && (namesz != n->key.sz ||
4008: strncmp(n->key.p, name, namesz)))
1.7 schwarze 4009: n = n->next;
1.8 schwarze 4010:
4011: if (NULL == n) {
1.16 schwarze 4012: /* Create a new string table entry. */
1.42 schwarze 4013: n = mandoc_malloc(sizeof(struct roffkv));
4014: n->key.p = mandoc_strndup(name, namesz);
4015: n->key.sz = namesz;
4016: n->val.p = NULL;
4017: n->val.sz = 0;
4018: n->next = *r;
4019: *r = n;
1.66 schwarze 4020: } else if (0 == append) {
1.42 schwarze 4021: free(n->val.p);
4022: n->val.p = NULL;
4023: n->val.sz = 0;
1.16 schwarze 4024: }
4025:
4026: if (NULL == string)
4027: return;
4028:
4029: /*
4030: * One additional byte for the '\n' in multiline mode,
4031: * and one for the terminating '\0'.
4032: */
1.66 schwarze 4033: newch = stringsz + (1 < append ? 2u : 1u);
1.42 schwarze 4034:
4035: if (NULL == n->val.p) {
4036: n->val.p = mandoc_malloc(newch);
4037: *n->val.p = '\0';
1.16 schwarze 4038: oldch = 0;
4039: } else {
1.42 schwarze 4040: oldch = n->val.sz;
4041: n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1.16 schwarze 4042: }
4043:
4044: /* Skip existing content in the destination buffer. */
1.42 schwarze 4045: c = n->val.p + (int)oldch;
1.16 schwarze 4046:
4047: /* Append new content to the destination buffer. */
1.42 schwarze 4048: i = 0;
4049: while (i < (int)stringsz) {
1.16 schwarze 4050: /*
4051: * Rudimentary roff copy mode:
4052: * Handle escaped backslashes.
4053: */
1.42 schwarze 4054: if ('\\' == string[i] && '\\' == string[i + 1])
4055: i++;
4056: *c++ = string[i++];
1.16 schwarze 4057: }
1.8 schwarze 4058:
1.16 schwarze 4059: /* Append terminating bytes. */
1.66 schwarze 4060: if (1 < append)
1.16 schwarze 4061: *c++ = '\n';
1.42 schwarze 4062:
1.16 schwarze 4063: *c = '\0';
1.42 schwarze 4064: n->val.sz = (int)(c - n->val.p);
1.7 schwarze 4065: }
4066:
1.8 schwarze 4067: static const char *
1.197 schwarze 4068: roff_getstrn(struct roff *r, const char *name, size_t len,
1.187 schwarze 4069: int *deftype)
1.7 schwarze 4070: {
1.187 schwarze 4071: const struct roffkv *n;
1.197 schwarze 4072: int found, i;
1.187 schwarze 4073: enum roff_tok tok;
4074:
1.197 schwarze 4075: found = 0;
4076: for (n = r->strtab; n != NULL; n = n->next) {
4077: if (strncmp(name, n->key.p, len) != 0 ||
4078: n->key.p[len] != '\0' || n->val.p == NULL)
4079: continue;
4080: if (*deftype & ROFFDEF_USER) {
4081: *deftype = ROFFDEF_USER;
4082: return n->val.p;
4083: } else {
4084: found = 1;
4085: break;
4086: }
4087: }
4088: for (n = r->rentab; n != NULL; n = n->next) {
4089: if (strncmp(name, n->key.p, len) != 0 ||
4090: n->key.p[len] != '\0' || n->val.p == NULL)
4091: continue;
4092: if (*deftype & ROFFDEF_REN) {
4093: *deftype = ROFFDEF_REN;
4094: return n->val.p;
4095: } else {
4096: found = 1;
4097: break;
1.187 schwarze 4098: }
4099: }
1.197 schwarze 4100: for (i = 0; i < PREDEFS_MAX; i++) {
4101: if (strncmp(name, predefs[i].name, len) != 0 ||
4102: predefs[i].name[len] != '\0')
4103: continue;
4104: if (*deftype & ROFFDEF_PRE) {
4105: *deftype = ROFFDEF_PRE;
4106: return predefs[i].str;
4107: } else {
4108: found = 1;
4109: break;
1.187 schwarze 4110: }
4111: }
1.228 schwarze 4112: if (r->man->meta.macroset != MACROSET_MAN) {
1.197 schwarze 4113: for (tok = MDOC_Dd; tok < MDOC_MAX; tok++) {
4114: if (strncmp(name, roff_name[tok], len) != 0 ||
4115: roff_name[tok][len] != '\0')
4116: continue;
4117: if (*deftype & ROFFDEF_STD) {
4118: *deftype = ROFFDEF_STD;
4119: return NULL;
4120: } else {
4121: found = 1;
4122: break;
1.187 schwarze 4123: }
4124: }
4125: }
1.228 schwarze 4126: if (r->man->meta.macroset != MACROSET_MDOC) {
1.197 schwarze 4127: for (tok = MAN_TH; tok < MAN_MAX; tok++) {
4128: if (strncmp(name, roff_name[tok], len) != 0 ||
4129: roff_name[tok][len] != '\0')
4130: continue;
4131: if (*deftype & ROFFDEF_STD) {
4132: *deftype = ROFFDEF_STD;
4133: return NULL;
4134: } else {
4135: found = 1;
4136: break;
1.187 schwarze 4137: }
4138: }
1.197 schwarze 4139: }
4140:
4141: if (found == 0 && *deftype != ROFFDEF_ANY) {
4142: if (*deftype & ROFFDEF_REN) {
4143: /*
4144: * This might still be a request,
4145: * so do not treat it as undefined yet.
4146: */
4147: *deftype = ROFFDEF_UNDEF;
4148: return NULL;
1.187 schwarze 4149: }
1.197 schwarze 4150:
4151: /* Using an undefined string defines it to be empty. */
4152:
4153: roff_setstrn(&r->strtab, name, len, "", 0, 0);
4154: roff_setstrn(&r->rentab, name, len, NULL, 0, 0);
1.187 schwarze 4155: }
1.197 schwarze 4156:
1.187 schwarze 4157: *deftype = 0;
1.149 schwarze 4158: return NULL;
1.7 schwarze 4159: }
4160:
1.8 schwarze 4161: static void
1.42 schwarze 4162: roff_freestr(struct roffkv *r)
1.7 schwarze 4163: {
1.42 schwarze 4164: struct roffkv *n, *nn;
1.7 schwarze 4165:
1.42 schwarze 4166: for (n = r; n; n = nn) {
4167: free(n->key.p);
4168: free(n->val.p);
1.7 schwarze 4169: nn = n->next;
4170: free(n);
4171: }
1.27 schwarze 4172: }
1.138 schwarze 4173:
4174: /* --- accessors and utility functions ------------------------------------ */
1.42 schwarze 4175:
4176: /*
4177: * Duplicate an input string, making the appropriate character
4178: * conversations (as stipulated by `tr') along the way.
4179: * Returns a heap-allocated string with all the replacements made.
4180: */
4181: char *
4182: roff_strdup(const struct roff *r, const char *p)
4183: {
4184: const struct roffkv *cp;
4185: char *res;
4186: const char *pp;
4187: size_t ssz, sz;
4188: enum mandoc_esc esc;
4189:
4190: if (NULL == r->xmbtab && NULL == r->xtab)
1.149 schwarze 4191: return mandoc_strdup(p);
1.42 schwarze 4192: else if ('\0' == *p)
1.149 schwarze 4193: return mandoc_strdup("");
1.42 schwarze 4194:
4195: /*
4196: * Step through each character looking for term matches
4197: * (remember that a `tr' can be invoked with an escape, which is
4198: * a glyph but the escape is multi-character).
4199: * We only do this if the character hash has been initialised
4200: * and the string is >0 length.
4201: */
4202:
4203: res = NULL;
4204: ssz = 0;
4205:
4206: while ('\0' != *p) {
1.161 schwarze 4207: assert((unsigned int)*p < 128);
4208: if ('\\' != *p && r->xtab && r->xtab[(unsigned int)*p].p) {
1.42 schwarze 4209: sz = r->xtab[(int)*p].sz;
4210: res = mandoc_realloc(res, ssz + sz + 1);
4211: memcpy(res + ssz, r->xtab[(int)*p].p, sz);
4212: ssz += sz;
4213: p++;
4214: continue;
4215: } else if ('\\' != *p) {
4216: res = mandoc_realloc(res, ssz + 2);
4217: res[ssz++] = *p++;
4218: continue;
4219: }
4220:
4221: /* Search for term matches. */
4222: for (cp = r->xmbtab; cp; cp = cp->next)
4223: if (0 == strncmp(p, cp->key.p, cp->key.sz))
4224: break;
4225:
4226: if (NULL != cp) {
4227: /*
4228: * A match has been found.
4229: * Append the match to the array and move
4230: * forward by its keysize.
4231: */
1.80 schwarze 4232: res = mandoc_realloc(res,
4233: ssz + cp->val.sz + 1);
1.42 schwarze 4234: memcpy(res + ssz, cp->val.p, cp->val.sz);
4235: ssz += cp->val.sz;
4236: p += (int)cp->key.sz;
4237: continue;
4238: }
4239:
4240: /*
4241: * Handle escapes carefully: we need to copy
4242: * over just the escape itself, or else we might
4243: * do replacements within the escape itself.
4244: * Make sure to pass along the bogus string.
4245: */
4246: pp = p++;
4247: esc = mandoc_escape(&p, NULL, NULL);
4248: if (ESCAPE_ERROR == esc) {
4249: sz = strlen(pp);
4250: res = mandoc_realloc(res, ssz + sz + 1);
4251: memcpy(res + ssz, pp, sz);
4252: break;
4253: }
1.80 schwarze 4254: /*
4255: * We bail out on bad escapes.
1.42 schwarze 4256: * No need to warn: we already did so when
1.227 schwarze 4257: * roff_expand() was called.
1.42 schwarze 4258: */
4259: sz = (int)(p - pp);
4260: res = mandoc_realloc(res, ssz + sz + 1);
4261: memcpy(res + ssz, pp, sz);
4262: ssz += sz;
4263: }
4264:
4265: res[(int)ssz] = '\0';
1.149 schwarze 4266: return res;
1.99 schwarze 4267: }
4268:
4269: int
4270: roff_getformat(const struct roff *r)
4271: {
4272:
1.149 schwarze 4273: return r->format;
1.48 schwarze 4274: }
4275:
4276: /*
1.80 schwarze 4277: * Find out whether a line is a macro line or not.
1.48 schwarze 4278: * If it is, adjust the current position and return one; if it isn't,
4279: * return zero and don't change the current position.
4280: * If the control character has been set with `.cc', then let that grain
4281: * precedence.
4282: * This is slighly contrary to groff, where using the non-breaking
4283: * control character when `cc' has been invoked will cause the
4284: * non-breaking macro contents to be printed verbatim.
4285: */
4286: int
4287: roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
4288: {
4289: int pos;
4290:
4291: pos = *ppos;
4292:
1.175 schwarze 4293: if (r->control != '\0' && cp[pos] == r->control)
1.48 schwarze 4294: pos++;
1.175 schwarze 4295: else if (r->control != '\0')
1.149 schwarze 4296: return 0;
1.48 schwarze 4297: else if ('\\' == cp[pos] && '.' == cp[pos + 1])
4298: pos += 2;
4299: else if ('.' == cp[pos] || '\'' == cp[pos])
4300: pos++;
4301: else
1.149 schwarze 4302: return 0;
1.48 schwarze 4303:
4304: while (' ' == cp[pos] || '\t' == cp[pos])
4305: pos++;
4306:
4307: *ppos = pos;
1.149 schwarze 4308: return 1;
1.1 schwarze 4309: }