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