Annotation of src/usr.bin/mandoc/roff.c, Revision 1.261
1.261 ! schwarze 1: /* $OpenBSD: roff.c,v 1.260 2022/05/19 15:17:51 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 */
1376: int deftype; /* type of definition to paste */
1377: int argi; /* macro argument index */
1378: int quote_args; /* true for \\$@, false for \\$* */
1379: int asz; /* length of the replacement */
1380: int rsz; /* length of the rest of the string */
1381: int npos; /* position in numeric expression */
1.53 schwarze 1382: int expand_count; /* to avoid infinite loops */
1.175 schwarze 1383:
1.43 schwarze 1384: expand_count = 0;
1.260 schwarze 1385: while (buf->buf[pos] != '\0') {
1.43 schwarze 1386:
1.260 schwarze 1387: /*
1388: * Skip plain ASCII characters.
1389: * If we have a non-standard escape character,
1390: * escape literal backslashes because all processing in
1391: * subsequent functions uses the standard escaping rules.
1392: */
1.78 schwarze 1393:
1.260 schwarze 1394: if (buf->buf[pos] != ec) {
1395: if (ec != ASCII_ESC && buf->buf[pos] == '\\') {
1396: roff_expand_patch(buf, pos, "\\e", pos + 1);
1397: pos++;
1.175 schwarze 1398: }
1.260 schwarze 1399: pos++;
1.78 schwarze 1400: continue;
1.175 schwarze 1401: }
1.78 schwarze 1402:
1.260 schwarze 1403: /*
1404: * Parse escape sequences,
1405: * issue diagnostic messages when appropriate,
1406: * and skip sequences that do not need expansion.
1407: * If we have a non-standard escape character, translate
1408: * it to backslashes and translate backslashes to \e.
1409: */
1.78 schwarze 1410:
1.260 schwarze 1411: if (roff_escape(buf->buf, ln, pos,
1412: &iesc, &iarg, &iendarg, &iend) != ESCAPE_EXPAND) {
1413: while (pos < iend) {
1414: if (buf->buf[pos] == ec) {
1415: buf->buf[pos] = '\\';
1416: if (pos + 1 < iend)
1417: pos++;
1418: } else if (buf->buf[pos] == '\\') {
1419: roff_expand_patch(buf,
1420: pos, "\\e", pos + 1);
1421: pos++;
1422: iend++;
1423: }
1424: pos++;
1.224 schwarze 1425: }
1.78 schwarze 1426: continue;
1.42 schwarze 1427: }
1428:
1.23 schwarze 1429: /*
1.260 schwarze 1430: * Treat "\E" just like "\";
1431: * it only makes a difference in copy mode.
1.23 schwarze 1432: */
1433:
1.260 schwarze 1434: inam = iesc + 1;
1435: while (buf->buf[inam] == 'E')
1436: inam++;
1437:
1438: /* Handle expansion. */
1439:
1440: res = NULL;
1441: switch (buf->buf[inam]) {
1442: case '*':
1443: if (iendarg == iarg)
1.79 schwarze 1444: break;
1.260 schwarze 1445: deftype = ROFFDEF_USER | ROFFDEF_PRE;
1446: if ((res = roff_getstrn(r, buf->buf + iarg,
1447: iendarg - iarg, &deftype)) != NULL)
1.79 schwarze 1448: break;
1.8 schwarze 1449:
1.260 schwarze 1450: /*
1451: * If not overriden,
1452: * let \*(.T through to the formatters.
1453: */
1.8 schwarze 1454:
1.260 schwarze 1455: if (iendarg - iarg == 2 &&
1456: buf->buf[iarg] == '.' &&
1457: buf->buf[iarg + 1] == 'T') {
1458: roff_setstrn(&r->strtab, ".T", 2, NULL, 0, 0);
1459: pos = iend;
1.125 schwarze 1460: continue;
1461: }
1.8 schwarze 1462:
1.260 schwarze 1463: mandoc_msg(MANDOCERR_STR_UNDEF, ln, iesc,
1464: "%.*s", iendarg - iarg, buf->buf + iarg);
1465: break;
1.23 schwarze 1466:
1.211 schwarze 1467: case '$':
1468: if (r->mstackpos < 0) {
1.260 schwarze 1469: mandoc_msg(MANDOCERR_ARG_UNDEF, ln, iesc,
1470: "%.*s", iend - iesc, buf->buf + iesc);
1.211 schwarze 1471: break;
1472: }
1473: ctx = r->mstack + r->mstackpos;
1.260 schwarze 1474: argi = buf->buf[iarg] - '1';
1475: if (argi >= 0 && argi <= 8) {
1476: if (argi < ctx->argc)
1477: res = ctx->argv[argi];
1.211 schwarze 1478: break;
1479: }
1.260 schwarze 1480: if (buf->buf[iarg] == '*')
1.211 schwarze 1481: quote_args = 0;
1.260 schwarze 1482: else if (buf->buf[iarg] == '@')
1.211 schwarze 1483: quote_args = 1;
1484: else {
1.260 schwarze 1485: mandoc_msg(MANDOCERR_ARG_NONUM, ln, iesc,
1486: "%.*s", iend - iesc, buf->buf + iesc);
1.211 schwarze 1487: break;
1488: }
1489: asz = 0;
1.260 schwarze 1490: for (argi = 0; argi < ctx->argc; argi++) {
1491: if (argi)
1.211 schwarze 1492: asz++; /* blank */
1493: if (quote_args)
1494: asz += 2; /* quotes */
1.260 schwarze 1495: asz += strlen(ctx->argv[argi]);
1.211 schwarze 1496: }
1.260 schwarze 1497: if (asz != iend - iesc) {
1498: rsz = buf->sz - iend;
1499: if (asz < iend - iesc)
1500: memmove(buf->buf + iesc + asz,
1501: buf->buf + iend, rsz);
1502: buf->sz = iesc + asz + rsz;
1503: buf->buf = mandoc_realloc(buf->buf, buf->sz);
1504: if (asz > iend - iesc)
1505: memmove(buf->buf + iesc + asz,
1506: buf->buf + iend, rsz);
1.211 schwarze 1507: }
1.260 schwarze 1508: dst = buf->buf + iesc;
1509: for (argi = 0; argi < ctx->argc; argi++) {
1510: if (argi)
1511: *dst++ = ' ';
1.211 schwarze 1512: if (quote_args)
1.260 schwarze 1513: *dst++ = '"';
1514: src = ctx->argv[argi];
1515: while (*src != '\0')
1516: *dst++ = *src++;
1.211 schwarze 1517: if (quote_args)
1.260 schwarze 1518: *dst++ = '"';
1.211 schwarze 1519: }
1520: continue;
1.80 schwarze 1521: case 'B':
1.79 schwarze 1522: npos = 0;
1.260 schwarze 1523: ubuf[0] = iendarg > iarg && iend > iendarg &&
1524: roff_evalnum(r, ln, buf->buf + iarg, &npos,
1525: NULL, ROFFNUM_SCALE) &&
1526: npos == iendarg - iarg ? '1' : '0';
1.79 schwarze 1527: ubuf[1] = '\0';
1.260 schwarze 1528: res = ubuf;
1.79 schwarze 1529: break;
1.261 ! schwarze 1530: case 'V':
! 1531: mandoc_msg(MANDOCERR_UNSUPP, ln, iesc,
! 1532: "%.*s", iend - iesc, buf->buf + iesc);
! 1533: roff_expand_patch(buf, iendarg, "}", iend);
! 1534: roff_expand_patch(buf, iesc, "${", iarg);
! 1535: continue;
1.80 schwarze 1536: case 'n':
1.260 schwarze 1537: if (iendarg > iarg)
1.91 schwarze 1538: (void)snprintf(ubuf, sizeof(ubuf), "%d",
1.260 schwarze 1539: roff_getregn(r, buf->buf + iarg,
1540: iendarg - iarg, buf->buf[inam + 1]));
1.91 schwarze 1541: else
1542: ubuf[0] = '\0';
1.260 schwarze 1543: res = ubuf;
1.79 schwarze 1544: break;
1.80 schwarze 1545: case 'w':
1.260 schwarze 1546: (void)snprintf(ubuf, sizeof(ubuf),
1547: "%d", (iendarg - iarg) * 24);
1548: res = ubuf;
1549: break;
1550: default:
1.79 schwarze 1551: break;
1552: }
1.260 schwarze 1553: if (res == NULL)
1.37 schwarze 1554: res = "";
1.260 schwarze 1555: if (++expand_count > EXPAND_LIMIT ||
1556: buf->sz + strlen(res) > SHRT_MAX) {
1557: mandoc_msg(MANDOCERR_ROFFLOOP, ln, iesc, NULL);
1.149 schwarze 1558: return ROFF_IGN;
1.8 schwarze 1559: }
1.260 schwarze 1560: roff_expand_patch(buf, iesc, res, iend);
1561: }
1562: return ROFF_CONT;
1563: }
1.8 schwarze 1564:
1.260 schwarze 1565: /*
1566: * Replace the substring from the start position (inclusive)
1567: * to end position (exclusive) with the repl(acement) string.
1568: */
1569: static void
1570: roff_expand_patch(struct buf *buf, int start, const char *repl, int end)
1571: {
1572: char *nbuf;
1.23 schwarze 1573:
1.261 ! schwarze 1574: buf->sz = mandoc_asprintf(&nbuf, "%.*s%s%s", start, buf->buf,
! 1575: repl, buf->buf + end) + 1;
1.260 schwarze 1576: free(buf->buf);
1577: buf->buf = nbuf;
1.42 schwarze 1578: }
1.225 schwarze 1579:
1580: /*
1581: * Parse a quoted or unquoted roff-style request or macro argument.
1582: * Return a pointer to the parsed argument, which is either the original
1583: * pointer or advanced by one byte in case the argument is quoted.
1584: * NUL-terminate the argument in place.
1585: * Collapse pairs of quotes inside quoted arguments.
1586: * Advance the argument pointer to the next argument,
1587: * or to the NUL byte terminating the argument line.
1588: */
1589: char *
1.227 schwarze 1590: roff_getarg(struct roff *r, char **cpp, int ln, int *pos)
1.225 schwarze 1591: {
1.227 schwarze 1592: struct buf buf;
1.238 schwarze 1593: char *cp, *start;
1.227 schwarze 1594: int newesc, pairs, quoted, white;
1.225 schwarze 1595:
1596: /* Quoting can only start with a new word. */
1597: start = *cpp;
1598: quoted = 0;
1599: if ('"' == *start) {
1600: quoted = 1;
1601: start++;
1602: }
1603:
1.227 schwarze 1604: newesc = pairs = white = 0;
1.225 schwarze 1605: for (cp = start; '\0' != *cp; cp++) {
1606:
1607: /*
1608: * Move the following text left
1609: * after quoted quotes and after "\\" and "\t".
1610: */
1611: if (pairs)
1612: cp[-pairs] = cp[0];
1613:
1614: if ('\\' == cp[0]) {
1615: /*
1616: * In copy mode, translate double to single
1617: * backslashes and backslash-t to literal tabs.
1618: */
1619: switch (cp[1]) {
1620: case 'a':
1621: case 't':
1.226 schwarze 1622: cp[-pairs] = '\t';
1.227 schwarze 1623: pairs++;
1624: cp++;
1625: break;
1.225 schwarze 1626: case '\\':
1.227 schwarze 1627: newesc = 1;
1628: cp[-pairs] = ASCII_ESC;
1.225 schwarze 1629: pairs++;
1630: cp++;
1631: break;
1632: case ' ':
1633: /* Skip escaped blanks. */
1634: if (0 == quoted)
1635: cp++;
1636: break;
1637: default:
1638: break;
1639: }
1640: } else if (0 == quoted) {
1641: if (' ' == cp[0]) {
1642: /* Unescaped blanks end unquoted args. */
1643: white = 1;
1644: break;
1645: }
1646: } else if ('"' == cp[0]) {
1647: if ('"' == cp[1]) {
1648: /* Quoted quotes collapse. */
1649: pairs++;
1650: cp++;
1651: } else {
1652: /* Unquoted quotes end quoted args. */
1653: quoted = 2;
1654: break;
1655: }
1656: }
1657: }
1658:
1659: /* Quoted argument without a closing quote. */
1660: if (1 == quoted)
1661: mandoc_msg(MANDOCERR_ARG_QUOTE, ln, *pos, NULL);
1662:
1663: /* NUL-terminate this argument and move to the next one. */
1664: if (pairs)
1665: cp[-pairs] = '\0';
1666: if ('\0' != *cp) {
1667: *cp++ = '\0';
1668: while (' ' == *cp)
1669: cp++;
1670: }
1671: *pos += (int)(cp - start) + (quoted ? 1 : 0);
1672: *cpp = cp;
1673:
1674: if ('\0' == *cp && (white || ' ' == cp[-1]))
1675: mandoc_msg(MANDOCERR_SPACE_EOL, ln, *pos, NULL);
1676:
1.227 schwarze 1677: start = mandoc_strdup(start);
1678: if (newesc == 0)
1679: return start;
1680:
1681: buf.buf = start;
1682: buf.sz = strlen(start) + 1;
1683: buf.next = NULL;
1684: if (roff_expand(r, &buf, ln, 0, ASCII_ESC) & ROFF_IGN) {
1685: free(buf.buf);
1686: buf.buf = mandoc_strdup("");
1687: }
1688: return buf.buf;
1.225 schwarze 1689: }
1690:
1.42 schwarze 1691:
1692: /*
1.147 schwarze 1693: * Process text streams.
1.42 schwarze 1694: */
1.212 schwarze 1695: static int
1.177 schwarze 1696: roff_parsetext(struct roff *r, struct buf *buf, int pos, int *offs)
1.42 schwarze 1697: {
1698: size_t sz;
1699: const char *start;
1.51 schwarze 1700: char *p;
1701: int isz;
1.42 schwarze 1702: enum mandoc_esc esc;
1703:
1.147 schwarze 1704: /* Spring the input line trap. */
1705:
1706: if (roffit_lines == 1) {
1707: isz = mandoc_asprintf(&p, "%s\n.%s", buf->buf, roffit_macro);
1708: free(buf->buf);
1709: buf->buf = p;
1710: buf->sz = isz + 1;
1711: *offs = 0;
1712: free(roffit_macro);
1713: roffit_lines = 0;
1.149 schwarze 1714: return ROFF_REPARSE;
1.147 schwarze 1715: } else if (roffit_lines > 1)
1716: --roffit_lines;
1717:
1.177 schwarze 1718: if (roffce_node != NULL && buf->buf[pos] != '\0') {
1719: if (roffce_lines < 1) {
1720: r->man->last = roffce_node;
1721: r->man->next = ROFF_NEXT_SIBLING;
1722: roffce_lines = 0;
1723: roffce_node = NULL;
1724: } else
1725: roffce_lines--;
1726: }
1727:
1.147 schwarze 1728: /* Convert all breakable hyphens into ASCII_HYPH. */
1729:
1.110 schwarze 1730: start = p = buf->buf + pos;
1.42 schwarze 1731:
1.110 schwarze 1732: while (*p != '\0') {
1.42 schwarze 1733: sz = strcspn(p, "-\\");
1734: p += sz;
1735:
1.110 schwarze 1736: if (*p == '\0')
1.42 schwarze 1737: break;
1738:
1.110 schwarze 1739: if (*p == '\\') {
1.42 schwarze 1740: /* Skip over escapes. */
1741: p++;
1.62 schwarze 1742: esc = mandoc_escape((const char **)&p, NULL, NULL);
1.110 schwarze 1743: if (esc == ESCAPE_ERROR)
1.42 schwarze 1744: break;
1.136 schwarze 1745: while (*p == '-')
1746: p++;
1.42 schwarze 1747: continue;
1748: } else if (p == start) {
1749: p++;
1750: continue;
1751: }
1752:
1.44 schwarze 1753: if (isalpha((unsigned char)p[-1]) &&
1754: isalpha((unsigned char)p[1]))
1.42 schwarze 1755: *p = ASCII_HYPH;
1756: p++;
1.8 schwarze 1757: }
1.149 schwarze 1758: return ROFF_CONT;
1.8 schwarze 1759: }
1760:
1.212 schwarze 1761: int
1.249 schwarze 1762: roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs, size_t len)
1.1 schwarze 1763: {
1.166 schwarze 1764: enum roff_tok t;
1.212 schwarze 1765: int e;
1.110 schwarze 1766: int pos; /* parse point */
1.115 schwarze 1767: int spos; /* saved parse point for messages */
1.110 schwarze 1768: int ppos; /* original offset in buf->buf */
1769: int ctl; /* macro line (boolean) */
1770:
1771: ppos = pos = *offs;
1.249 schwarze 1772:
1773: if (len > 80 && r->tbl == NULL && r->eqn == NULL &&
1774: (r->man->flags & ROFF_NOFILL) == 0 &&
1775: strchr(" .\\", buf->buf[pos]) == NULL &&
1776: buf->buf[pos] != r->control &&
1777: strcspn(buf->buf, " ") < 80)
1778: mandoc_msg(MANDOCERR_TEXT_LONG, ln, (int)len - 1,
1779: "%.20s...", buf->buf + pos);
1.1 schwarze 1780:
1.102 schwarze 1781: /* Handle in-line equation delimiters. */
1782:
1.108 schwarze 1783: if (r->tbl == NULL &&
1784: r->last_eqn != NULL && r->last_eqn->delim &&
1.102 schwarze 1785: (r->eqn == NULL || r->eqn_inline)) {
1.110 schwarze 1786: e = roff_eqndelim(r, buf, pos);
1.102 schwarze 1787: if (e == ROFF_REPARSE)
1.149 schwarze 1788: return e;
1.102 schwarze 1789: assert(e == ROFF_CONT);
1790: }
1791:
1.259 schwarze 1792: /* Handle comments and escape sequences. */
1793:
1794: e = roff_parse_comment(r, buf, ln, pos, r->escape);
1795: if ((e & ROFF_MASK) == ROFF_IGN)
1796: return e;
1797: assert(e == ROFF_CONT);
1.8 schwarze 1798:
1.227 schwarze 1799: e = roff_expand(r, buf, ln, pos, r->escape);
1.212 schwarze 1800: if ((e & ROFF_MASK) == ROFF_IGN)
1.149 schwarze 1801: return e;
1.110 schwarze 1802: assert(e == ROFF_CONT);
1.8 schwarze 1803:
1.110 schwarze 1804: ctl = roff_getcontrol(r, buf->buf, &pos);
1.35 schwarze 1805:
1.8 schwarze 1806: /*
1.2 schwarze 1807: * First, if a scope is open and we're not a macro, pass the
1.124 schwarze 1808: * text through the macro's filter.
1809: * Equations process all content themselves.
1810: * Tables process almost all content themselves, but we want
1811: * to warn about macros before passing it there.
1.2 schwarze 1812: */
1813:
1.124 schwarze 1814: if (r->last != NULL && ! ctl) {
1.2 schwarze 1815: t = r->last->tok;
1.110 schwarze 1816: e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
1.212 schwarze 1817: if ((e & ROFF_MASK) == ROFF_IGN)
1.149 schwarze 1818: return e;
1.212 schwarze 1819: e &= ~ROFF_MASK;
1820: } else
1821: e = ROFF_IGN;
1.191 schwarze 1822: if (r->eqn != NULL && strncmp(buf->buf + ppos, ".EN", 3)) {
1823: eqn_read(r->eqn, buf->buf + ppos);
1.212 schwarze 1824: return e;
1.191 schwarze 1825: }
1.193 schwarze 1826: if (r->tbl != NULL && (ctl == 0 || buf->buf[pos] == '\0')) {
1827: tbl_read(r->tbl, ln, buf->buf, ppos);
1.218 schwarze 1828: roff_addtbl(r->man, ln, r->tbl);
1.212 schwarze 1829: return e;
1.193 schwarze 1830: }
1.239 schwarze 1831: if ( ! ctl) {
1832: r->options &= ~MPARSE_COMMENT;
1.212 schwarze 1833: return roff_parsetext(r, buf, pos, offs) | e;
1.239 schwarze 1834: }
1.100 schwarze 1835:
1836: /* Skip empty request lines. */
1837:
1.110 schwarze 1838: if (buf->buf[pos] == '"') {
1.222 schwarze 1839: mandoc_msg(MANDOCERR_COMMENT_BAD, ln, pos, NULL);
1.149 schwarze 1840: return ROFF_IGN;
1.110 schwarze 1841: } else if (buf->buf[pos] == '\0')
1.149 schwarze 1842: return ROFF_IGN;
1.2 schwarze 1843:
1844: /*
1845: * If a scope is open, go to the child handler for that macro,
1846: * as it may want to preprocess before doing anything with it.
1847: */
1848:
1849: if (r->last) {
1.1 schwarze 1850: t = r->last->tok;
1.149 schwarze 1851: return (*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs);
1.2 schwarze 1852: }
1853:
1.239 schwarze 1854: r->options &= ~MPARSE_COMMENT;
1.115 schwarze 1855: spos = pos;
1856: t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.258 schwarze 1857: return roff_req_or_macro(r, t, buf, ln, spos, pos, offs);
1858: }
1859:
1860: /*
1861: * Handle a new request or macro.
1862: * May be called outside any scope or from inside a conditional scope.
1863: */
1864: static int
1865: roff_req_or_macro(ROFF_ARGS) {
1.115 schwarze 1866:
1.258 schwarze 1867: /* For now, tables ignore most macros and some request. */
1.115 schwarze 1868:
1.258 schwarze 1869: if (r->tbl != NULL && (tok == TOKEN_NONE || tok == ROFF_TS ||
1870: tok == ROFF_br || tok == ROFF_ce || tok == ROFF_rj ||
1871: tok == ROFF_sp)) {
1.222 schwarze 1872: mandoc_msg(MANDOCERR_TBLMACRO,
1.258 schwarze 1873: ln, ppos, "%s", buf->buf + ppos);
1874: if (tok != TOKEN_NONE)
1.149 schwarze 1875: return ROFF_IGN;
1.129 schwarze 1876: while (buf->buf[pos] != '\0' && buf->buf[pos] != ' ')
1877: pos++;
1.163 schwarze 1878: while (buf->buf[pos] == ' ')
1.129 schwarze 1879: pos++;
1.193 schwarze 1880: tbl_read(r->tbl, ln, buf->buf, pos);
1.218 schwarze 1881: roff_addtbl(r->man, ln, r->tbl);
1.193 schwarze 1882: return ROFF_IGN;
1.115 schwarze 1883: }
1884:
1.177 schwarze 1885: /* For now, let high level macros abort .ce mode. */
1886:
1.258 schwarze 1887: if (roffce_node != NULL &&
1888: (tok == TOKEN_NONE || tok == ROFF_Dd || tok == ROFF_EQ ||
1889: tok == ROFF_TH || tok == ROFF_TS)) {
1.177 schwarze 1890: r->man->last = roffce_node;
1891: r->man->next = ROFF_NEXT_SIBLING;
1892: roffce_lines = 0;
1893: roffce_node = NULL;
1894: }
1895:
1.2 schwarze 1896: /*
1.115 schwarze 1897: * This is neither a roff request nor a user-defined macro.
1898: * Let the standard macro set parsers handle it.
1.2 schwarze 1899: */
1900:
1.258 schwarze 1901: if (tok == TOKEN_NONE)
1.149 schwarze 1902: return ROFF_CONT;
1.115 schwarze 1903:
1.258 schwarze 1904: /* Execute a roff request or a user-defined macro. */
1.1 schwarze 1905:
1.258 schwarze 1906: return (*roffs[tok].proc)(r, tok, buf, ln, ppos, pos, offs);
1.2 schwarze 1907: }
1908:
1.211 schwarze 1909: /*
1910: * Internal interface function to tell the roff parser that execution
1911: * of the current macro ended. This is required because macro
1912: * definitions usually do not end with a .return request.
1913: */
1914: void
1915: roff_userret(struct roff *r)
1916: {
1917: struct mctx *ctx;
1918: int i;
1919:
1920: assert(r->mstackpos >= 0);
1921: ctx = r->mstack + r->mstackpos;
1922: for (i = 0; i < ctx->argc; i++)
1923: free(ctx->argv[i]);
1924: ctx->argc = 0;
1925: r->mstackpos--;
1926: }
1927:
1.27 schwarze 1928: void
1.2 schwarze 1929: roff_endparse(struct roff *r)
1930: {
1.193 schwarze 1931: if (r->last != NULL)
1.222 schwarze 1932: mandoc_msg(MANDOCERR_BLK_NOEND, r->last->line,
1933: r->last->col, "%s", roff_name[r->last->tok]);
1.27 schwarze 1934:
1.193 schwarze 1935: if (r->eqn != NULL) {
1.222 schwarze 1936: mandoc_msg(MANDOCERR_BLK_NOEND,
1.191 schwarze 1937: r->eqn->node->line, r->eqn->node->pos, "EQ");
1938: eqn_parse(r->eqn);
1939: r->eqn = NULL;
1.32 schwarze 1940: }
1941:
1.193 schwarze 1942: if (r->tbl != NULL) {
1.218 schwarze 1943: tbl_end(r->tbl, 1);
1.193 schwarze 1944: r->tbl = NULL;
1.27 schwarze 1945: }
1.1 schwarze 1946: }
1947:
1948: /*
1.258 schwarze 1949: * Parse the request or macro name at buf[*pos].
1950: * Return ROFF_RENAMED, ROFF_USERDEF, or a ROFF_* token value.
1951: * For empty, undefined, mdoc(7), and man(7) macros, return TOKEN_NONE.
1952: * As a side effect, set r->current_string to the definition or to NULL.
1.1 schwarze 1953: */
1.166 schwarze 1954: static enum roff_tok
1.87 schwarze 1955: roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
1.1 schwarze 1956: {
1.87 schwarze 1957: char *cp;
1.16 schwarze 1958: const char *mac;
1959: size_t maclen;
1.187 schwarze 1960: int deftype;
1.166 schwarze 1961: enum roff_tok t;
1.1 schwarze 1962:
1.87 schwarze 1963: cp = buf + *pos;
1964:
1965: if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
1.166 schwarze 1966: return TOKEN_NONE;
1.1 schwarze 1967:
1.87 schwarze 1968: mac = cp;
1969: maclen = roff_getname(r, &cp, ln, ppos);
1.1 schwarze 1970:
1.187 schwarze 1971: deftype = ROFFDEF_USER | ROFFDEF_REN;
1972: r->current_string = roff_getstrn(r, mac, maclen, &deftype);
1973: switch (deftype) {
1974: case ROFFDEF_USER:
1975: t = ROFF_USERDEF;
1976: break;
1977: case ROFFDEF_REN:
1978: t = ROFF_RENAMED;
1979: break;
1980: default:
1981: t = roffhash_find(r->reqtab, mac, maclen);
1982: break;
1983: }
1.166 schwarze 1984: if (t != TOKEN_NONE)
1.87 schwarze 1985: *pos = cp - buf;
1.197 schwarze 1986: else if (deftype == ROFFDEF_UNDEF) {
1987: /* Using an undefined macro defines it to be empty. */
1988: roff_setstrn(&r->strtab, mac, maclen, "", 0, 0);
1989: roff_setstrn(&r->rentab, mac, maclen, NULL, 0, 0);
1990: }
1.149 schwarze 1991: return t;
1.1 schwarze 1992: }
1993:
1.138 schwarze 1994: /* --- handling of request blocks ----------------------------------------- */
1995:
1.247 schwarze 1996: /*
1997: * Close a macro definition block or an "ignore" block.
1998: */
1.212 schwarze 1999: static int
1.2 schwarze 2000: roff_cblock(ROFF_ARGS)
1.1 schwarze 2001: {
1.247 schwarze 2002: int rr;
1.2 schwarze 2003:
1.110 schwarze 2004: if (r->last == NULL) {
1.222 schwarze 2005: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "..");
1.149 schwarze 2006: return ROFF_IGN;
1.2 schwarze 2007: }
1.1 schwarze 2008:
1.2 schwarze 2009: switch (r->last->tok) {
1.80 schwarze 2010: case ROFF_am:
2011: case ROFF_ami:
2012: case ROFF_de:
2013: case ROFF_dei:
2014: case ROFF_ig:
1.2 schwarze 2015: break;
1.247 schwarze 2016: case ROFF_am1:
2017: case ROFF_de1:
2018: /* Remapped in roff_block(). */
2019: abort();
1.2 schwarze 2020: default:
1.222 schwarze 2021: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "..");
1.149 schwarze 2022: return ROFF_IGN;
1.2 schwarze 2023: }
2024:
1.247 schwarze 2025: roffnode_pop(r);
2026: roffnode_cleanscope(r);
2027:
2028: /*
2029: * If a conditional block with braces is still open,
2030: * check for "\}" block end markers.
2031: */
2032:
2033: if (r->last != NULL && r->last->endspan < 0) {
2034: rr = 1; /* If arguments follow "\}", warn about them. */
2035: roff_cond_checkend(r, tok, buf, ln, ppos, pos, &rr);
2036: }
2037:
1.110 schwarze 2038: if (buf->buf[pos] != '\0')
1.222 schwarze 2039: mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
1.110 schwarze 2040: ".. %s", buf->buf + pos);
1.2 schwarze 2041:
1.149 schwarze 2042: return ROFF_IGN;
1.2 schwarze 2043: }
1.1 schwarze 2044:
1.236 schwarze 2045: /*
2046: * Pop all nodes ending at the end of the current input line.
2047: * Return the number of loops ended.
2048: */
1.212 schwarze 2049: static int
1.2 schwarze 2050: roffnode_cleanscope(struct roff *r)
2051: {
1.212 schwarze 2052: int inloop;
1.1 schwarze 2053:
1.212 schwarze 2054: inloop = 0;
1.247 schwarze 2055: while (r->last != NULL && r->last->endspan > 0) {
1.46 schwarze 2056: if (--r->last->endspan != 0)
1.2 schwarze 2057: break;
1.212 schwarze 2058: inloop += roffnode_pop(r);
1.2 schwarze 2059: }
1.212 schwarze 2060: return inloop;
1.2 schwarze 2061: }
1.1 schwarze 2062:
1.236 schwarze 2063: /*
1.247 schwarze 2064: * Handle the closing "\}" of a conditional block.
1.236 schwarze 2065: * Apart from generating warnings, this only pops nodes.
2066: * Return the number of loops ended.
2067: */
1.212 schwarze 2068: static int
1.68 schwarze 2069: roff_ccond(struct roff *r, int ln, int ppos)
1.2 schwarze 2070: {
2071: if (NULL == r->last) {
1.222 schwarze 2072: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "\\}");
1.212 schwarze 2073: return 0;
1.2 schwarze 2074: }
1.1 schwarze 2075:
1.2 schwarze 2076: switch (r->last->tok) {
1.80 schwarze 2077: case ROFF_el:
2078: case ROFF_ie:
2079: case ROFF_if:
1.212 schwarze 2080: case ROFF_while:
1.2 schwarze 2081: break;
2082: default:
1.222 schwarze 2083: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "\\}");
1.212 schwarze 2084: return 0;
1.2 schwarze 2085: }
1.1 schwarze 2086:
1.2 schwarze 2087: if (r->last->endspan > -1) {
1.222 schwarze 2088: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "\\}");
1.212 schwarze 2089: return 0;
1.2 schwarze 2090: }
2091:
1.212 schwarze 2092: return roffnode_pop(r) + roffnode_cleanscope(r);
1.1 schwarze 2093: }
2094:
1.212 schwarze 2095: static int
1.2 schwarze 2096: roff_block(ROFF_ARGS)
1.1 schwarze 2097: {
1.187 schwarze 2098: const char *name, *value;
2099: char *call, *cp, *iname, *rname;
2100: size_t csz, namesz, rsz;
2101: int deftype;
1.16 schwarze 2102:
1.93 schwarze 2103: /* Ignore groff compatibility mode for now. */
1.2 schwarze 2104:
1.110 schwarze 2105: if (tok == ROFF_de1)
1.93 schwarze 2106: tok = ROFF_de;
1.123 schwarze 2107: else if (tok == ROFF_dei1)
2108: tok = ROFF_dei;
1.110 schwarze 2109: else if (tok == ROFF_am1)
1.93 schwarze 2110: tok = ROFF_am;
1.123 schwarze 2111: else if (tok == ROFF_ami1)
2112: tok = ROFF_ami;
1.93 schwarze 2113:
2114: /* Parse the macro name argument. */
2115:
1.110 schwarze 2116: cp = buf->buf + pos;
2117: if (tok == ROFF_ig) {
1.93 schwarze 2118: iname = NULL;
2119: namesz = 0;
2120: } else {
2121: iname = cp;
2122: namesz = roff_getname(r, &cp, ln, ppos);
2123: iname[namesz] = '\0';
2124: }
1.22 schwarze 2125:
1.93 schwarze 2126: /* Resolve the macro name argument if it is indirect. */
1.22 schwarze 2127:
1.110 schwarze 2128: if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1.187 schwarze 2129: deftype = ROFFDEF_USER;
2130: name = roff_getstrn(r, iname, namesz, &deftype);
2131: if (name == NULL) {
1.222 schwarze 2132: mandoc_msg(MANDOCERR_STR_UNDEF,
2133: ln, (int)(iname - buf->buf),
1.93 schwarze 2134: "%.*s", (int)namesz, iname);
2135: namesz = 0;
2136: } else
2137: namesz = strlen(name);
2138: } else
2139: name = iname;
1.22 schwarze 2140:
1.110 schwarze 2141: if (namesz == 0 && tok != ROFF_ig) {
1.222 schwarze 2142: mandoc_msg(MANDOCERR_REQ_EMPTY,
2143: ln, ppos, "%s", roff_name[tok]);
1.149 schwarze 2144: return ROFF_IGN;
1.93 schwarze 2145: }
1.2 schwarze 2146:
1.16 schwarze 2147: roffnode_push(r, tok, name, ln, ppos);
2148:
2149: /*
2150: * At the beginning of a `de' macro, clear the existing string
2151: * with the same name, if there is one. New content will be
1.66 schwarze 2152: * appended from roff_block_text() in multiline mode.
1.16 schwarze 2153: */
1.22 schwarze 2154:
1.183 schwarze 2155: if (tok == ROFF_de || tok == ROFF_dei) {
1.86 schwarze 2156: roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
1.183 schwarze 2157: roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.187 schwarze 2158: } else if (tok == ROFF_am || tok == ROFF_ami) {
2159: deftype = ROFFDEF_ANY;
2160: value = roff_getstrn(r, iname, namesz, &deftype);
2161: switch (deftype) { /* Before appending, ... */
2162: case ROFFDEF_PRE: /* copy predefined to user-defined. */
2163: roff_setstrn(&r->strtab, name, namesz,
2164: value, strlen(value), 0);
2165: break;
2166: case ROFFDEF_REN: /* call original standard macro. */
2167: csz = mandoc_asprintf(&call, ".%.*s \\$* \\\"\n",
2168: (int)strlen(value), value);
2169: roff_setstrn(&r->strtab, name, namesz, call, csz, 0);
2170: roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
2171: free(call);
2172: break;
2173: case ROFFDEF_STD: /* rename and call standard macro. */
2174: rsz = mandoc_asprintf(&rname, "__%s_renamed", name);
2175: roff_setstrn(&r->rentab, rname, rsz, name, namesz, 0);
2176: csz = mandoc_asprintf(&call, ".%.*s \\$* \\\"\n",
2177: (int)rsz, rname);
2178: roff_setstrn(&r->strtab, name, namesz, call, csz, 0);
2179: free(call);
2180: free(rname);
2181: break;
2182: default:
2183: break;
2184: }
1.183 schwarze 2185: }
1.2 schwarze 2186:
1.110 schwarze 2187: if (*cp == '\0')
1.149 schwarze 2188: return ROFF_IGN;
1.1 schwarze 2189:
1.93 schwarze 2190: /* Get the custom end marker. */
1.22 schwarze 2191:
1.93 schwarze 2192: iname = cp;
1.86 schwarze 2193: namesz = roff_getname(r, &cp, ln, ppos);
1.93 schwarze 2194:
2195: /* Resolve the end marker if it is indirect. */
2196:
1.110 schwarze 2197: if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1.187 schwarze 2198: deftype = ROFFDEF_USER;
2199: name = roff_getstrn(r, iname, namesz, &deftype);
2200: if (name == NULL) {
1.222 schwarze 2201: mandoc_msg(MANDOCERR_STR_UNDEF,
2202: ln, (int)(iname - buf->buf),
1.93 schwarze 2203: "%.*s", (int)namesz, iname);
2204: namesz = 0;
2205: } else
2206: namesz = strlen(name);
2207: } else
2208: name = iname;
2209:
1.86 schwarze 2210: if (namesz)
2211: r->last->end = mandoc_strndup(name, namesz);
1.2 schwarze 2212:
1.110 schwarze 2213: if (*cp != '\0')
1.222 schwarze 2214: mandoc_msg(MANDOCERR_ARG_EXCESS,
1.167 schwarze 2215: ln, pos, ".%s ... %s", roff_name[tok], cp);
1.1 schwarze 2216:
1.149 schwarze 2217: return ROFF_IGN;
1.1 schwarze 2218: }
2219:
1.212 schwarze 2220: static int
1.2 schwarze 2221: roff_block_sub(ROFF_ARGS)
1.1 schwarze 2222: {
1.166 schwarze 2223: enum roff_tok t;
1.2 schwarze 2224: int i, j;
2225:
2226: /*
1.257 schwarze 2227: * If a custom end marker is a user-defined or predefined macro
2228: * or a request, interpret it.
1.2 schwarze 2229: */
2230:
2231: if (r->last->end) {
1.35 schwarze 2232: for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.110 schwarze 2233: if (buf->buf[i] != r->last->end[j])
1.2 schwarze 2234: break;
1.1 schwarze 2235:
1.110 schwarze 2236: if (r->last->end[j] == '\0' &&
2237: (buf->buf[i] == '\0' ||
2238: buf->buf[i] == ' ' ||
2239: buf->buf[i] == '\t')) {
1.2 schwarze 2240: roffnode_pop(r);
2241: roffnode_cleanscope(r);
1.1 schwarze 2242:
1.110 schwarze 2243: while (buf->buf[i] == ' ' || buf->buf[i] == '\t')
1.35 schwarze 2244: i++;
2245:
2246: pos = i;
1.110 schwarze 2247: if (roff_parse(r, buf->buf, &pos, ln, ppos) !=
1.166 schwarze 2248: TOKEN_NONE)
1.149 schwarze 2249: return ROFF_RERUN;
2250: return ROFF_IGN;
1.2 schwarze 2251: }
1.1 schwarze 2252: }
2253:
1.257 schwarze 2254: /* Handle the standard end marker. */
1.1 schwarze 2255:
1.110 schwarze 2256: t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.257 schwarze 2257: if (t == ROFF_cblock)
2258: return roff_cblock(r, t, buf, ln, ppos, pos, offs);
1.1 schwarze 2259:
1.257 schwarze 2260: /* Not an end marker, so append the line to the block. */
1.1 schwarze 2261:
1.257 schwarze 2262: if (tok != ROFF_ig)
2263: roff_setstr(r, r->last->name, buf->buf + ppos, 2);
2264: return ROFF_IGN;
1.2 schwarze 2265: }
2266:
1.212 schwarze 2267: static int
1.2 schwarze 2268: roff_block_text(ROFF_ARGS)
2269: {
2270:
1.110 schwarze 2271: if (tok != ROFF_ig)
2272: roff_setstr(r, r->last->name, buf->buf + pos, 2);
1.16 schwarze 2273:
1.149 schwarze 2274: return ROFF_IGN;
1.2 schwarze 2275: }
2276:
1.247 schwarze 2277: /*
2278: * Check for a closing "\}" and handle it.
2279: * In this function, the final "int *offs" argument is used for
2280: * different purposes than elsewhere:
2281: * Input: *offs == 0: caller wants to discard arguments following \}
2282: * *offs == 1: caller wants to preserve text following \}
2283: * Output: *offs = 0: tell caller to discard input line
2284: * *offs = 1: tell caller to use input line
2285: */
1.212 schwarze 2286: static int
1.247 schwarze 2287: roff_cond_checkend(ROFF_ARGS)
1.2 schwarze 2288: {
1.212 schwarze 2289: char *ep;
2290: int endloop, irc, rr;
1.2 schwarze 2291:
1.212 schwarze 2292: irc = ROFF_IGN;
1.2 schwarze 2293: rr = r->last->rule;
1.212 schwarze 2294: endloop = tok != ROFF_while ? ROFF_IGN :
2295: rr ? ROFF_LOOPCONT : ROFF_LOOPEXIT;
2296: if (roffnode_cleanscope(r))
2297: irc |= endloop;
1.39 schwarze 2298:
1.69 schwarze 2299: /*
1.247 schwarze 2300: * If "\}" occurs on a macro line without a preceding macro or
2301: * a text line contains nothing else, drop the line completely.
1.69 schwarze 2302: */
2303:
1.110 schwarze 2304: ep = buf->buf + pos;
1.247 schwarze 2305: if (ep[0] == '\\' && ep[1] == '}' && (ep[2] == '\0' || *offs == 0))
1.71 schwarze 2306: rr = 0;
1.69 schwarze 2307:
1.215 schwarze 2308: /*
1.247 schwarze 2309: * The closing delimiter "\}" rewinds the conditional scope
1.215 schwarze 2310: * but is otherwise ignored when interpreting the line.
2311: */
1.39 schwarze 2312:
1.110 schwarze 2313: while ((ep = strchr(ep, '\\')) != NULL) {
1.190 schwarze 2314: switch (ep[1]) {
2315: case '}':
1.248 schwarze 2316: if (ep[2] == '\0')
2317: ep[0] = '\0';
2318: else if (rr)
1.247 schwarze 2319: ep[1] = '&';
2320: else
2321: memmove(ep, ep + 2, strlen(ep + 2) + 1);
1.212 schwarze 2322: if (roff_ccond(r, ln, ep - buf->buf))
2323: irc |= endloop;
1.190 schwarze 2324: break;
2325: case '\0':
2326: ++ep;
2327: break;
2328: default:
2329: ep += 2;
2330: break;
1.70 schwarze 2331: }
1.50 schwarze 2332: }
1.247 schwarze 2333: *offs = rr;
2334: return irc;
2335: }
1.190 schwarze 2336:
1.247 schwarze 2337: /*
2338: * Parse and process a request or macro line in conditional scope.
2339: */
2340: static int
2341: roff_cond_sub(ROFF_ARGS)
2342: {
2343: struct roffnode *bl;
1.258 schwarze 2344: int irc, rr, spos;
1.247 schwarze 2345: enum roff_tok t;
2346:
2347: rr = 0; /* If arguments follow "\}", skip them. */
2348: irc = roff_cond_checkend(r, tok, buf, ln, ppos, pos, &rr);
1.258 schwarze 2349: spos = pos;
1.240 schwarze 2350: t = roff_parse(r, buf->buf, &pos, ln, ppos);
2351:
1.190 schwarze 2352: /*
1.258 schwarze 2353: * Handle requests and macros if the conditional evaluated
2354: * to true or if they are structurally required.
2355: * The .break request is always handled specially.
1.190 schwarze 2356: */
2357:
1.236 schwarze 2358: if (t == ROFF_break) {
2359: if (irc & ROFF_LOOPMASK)
2360: irc = ROFF_IGN | ROFF_LOOPEXIT;
2361: else if (rr) {
2362: for (bl = r->last; bl != NULL; bl = bl->parent) {
2363: bl->rule = 0;
2364: if (bl->tok == ROFF_while)
2365: break;
2366: }
2367: }
1.258 schwarze 2368: } else if (rr || (t < TOKEN_NONE && roffs[t].flags & ROFFMAC_STRUCT)) {
2369: irc |= roff_req_or_macro(r, t, buf, ln, spos, pos, offs);
1.255 schwarze 2370: if (irc & ROFF_WHILE)
2371: irc &= ~(ROFF_LOOPCONT | ROFF_LOOPEXIT);
1.258 schwarze 2372: }
1.212 schwarze 2373: return irc;
1.2 schwarze 2374: }
2375:
1.247 schwarze 2376: /*
2377: * Parse and process a text line in conditional scope.
2378: */
1.212 schwarze 2379: static int
1.2 schwarze 2380: roff_cond_text(ROFF_ARGS)
2381: {
1.247 schwarze 2382: int irc, rr;
1.215 schwarze 2383:
1.247 schwarze 2384: rr = 1; /* If arguments follow "\}", preserve them. */
2385: irc = roff_cond_checkend(r, tok, buf, ln, ppos, pos, &rr);
1.212 schwarze 2386: if (rr)
2387: irc |= ROFF_CONT;
2388: return irc;
1.2 schwarze 2389: }
2390:
1.138 schwarze 2391: /* --- handling of numeric and conditional expressions -------------------- */
2392:
1.77 schwarze 2393: /*
2394: * Parse a single signed integer number. Stop at the first non-digit.
2395: * If there is at least one digit, return success and advance the
2396: * parse point, else return failure and let the parse point unchanged.
2397: * Ignore overflows, treat them just like the C language.
2398: */
1.56 schwarze 2399: static int
1.133 schwarze 2400: roff_getnum(const char *v, int *pos, int *res, int flags)
1.56 schwarze 2401: {
1.133 schwarze 2402: int myres, scaled, n, p;
1.79 schwarze 2403:
2404: if (NULL == res)
2405: res = &myres;
1.56 schwarze 2406:
2407: p = *pos;
2408: n = v[p] == '-';
1.133 schwarze 2409: if (n || v[p] == '+')
1.56 schwarze 2410: p++;
2411:
1.133 schwarze 2412: if (flags & ROFFNUM_WHITE)
2413: while (isspace((unsigned char)v[p]))
2414: p++;
2415:
1.56 schwarze 2416: for (*res = 0; isdigit((unsigned char)v[p]); p++)
1.77 schwarze 2417: *res = 10 * *res + v[p] - '0';
1.56 schwarze 2418: if (p == *pos + n)
2419: return 0;
2420:
2421: if (n)
2422: *res = -*res;
2423:
1.126 schwarze 2424: /* Each number may be followed by one optional scaling unit. */
2425:
2426: switch (v[p]) {
2427: case 'f':
1.133 schwarze 2428: scaled = *res * 65536;
1.126 schwarze 2429: break;
2430: case 'i':
1.133 schwarze 2431: scaled = *res * 240;
1.126 schwarze 2432: break;
2433: case 'c':
1.133 schwarze 2434: scaled = *res * 240 / 2.54;
1.126 schwarze 2435: break;
2436: case 'v':
2437: case 'P':
1.133 schwarze 2438: scaled = *res * 40;
1.126 schwarze 2439: break;
2440: case 'm':
2441: case 'n':
1.133 schwarze 2442: scaled = *res * 24;
1.126 schwarze 2443: break;
2444: case 'p':
1.133 schwarze 2445: scaled = *res * 10 / 3;
1.126 schwarze 2446: break;
2447: case 'u':
1.133 schwarze 2448: scaled = *res;
1.126 schwarze 2449: break;
2450: case 'M':
1.133 schwarze 2451: scaled = *res * 6 / 25;
1.126 schwarze 2452: break;
2453: default:
1.133 schwarze 2454: scaled = *res;
1.126 schwarze 2455: p--;
2456: break;
2457: }
1.133 schwarze 2458: if (flags & ROFFNUM_SCALE)
2459: *res = scaled;
1.126 schwarze 2460:
2461: *pos = p + 1;
1.149 schwarze 2462: return 1;
1.56 schwarze 2463: }
2464:
1.71 schwarze 2465: /*
2466: * Evaluate a string comparison condition.
2467: * The first character is the delimiter.
2468: * Succeed if the string up to its second occurrence
2469: * matches the string up to its third occurence.
2470: * Advance the cursor after the third occurrence
2471: * or lacking that, to the end of the line.
2472: */
2473: static int
2474: roff_evalstrcond(const char *v, int *pos)
2475: {
2476: const char *s1, *s2, *s3;
2477: int match;
2478:
2479: match = 0;
2480: s1 = v + *pos; /* initial delimiter */
2481: s2 = s1 + 1; /* for scanning the first string */
2482: s3 = strchr(s2, *s1); /* for scanning the second string */
2483:
2484: if (NULL == s3) /* found no middle delimiter */
2485: goto out;
2486:
2487: while ('\0' != *++s3) {
2488: if (*s2 != *s3) { /* mismatch */
2489: s3 = strchr(s3, *s1);
2490: break;
2491: }
2492: if (*s3 == *s1) { /* found the final delimiter */
2493: match = 1;
2494: break;
2495: }
2496: s2++;
2497: }
2498:
2499: out:
2500: if (NULL == s3)
2501: s3 = strchr(s2, '\0');
1.114 schwarze 2502: else if (*s3 != '\0')
1.71 schwarze 2503: s3++;
2504: *pos = s3 - v;
1.149 schwarze 2505: return match;
1.71 schwarze 2506: }
2507:
1.77 schwarze 2508: /*
2509: * Evaluate an optionally negated single character, numerical,
2510: * or string condition.
2511: */
1.71 schwarze 2512: static int
1.143 schwarze 2513: roff_evalcond(struct roff *r, int ln, char *v, int *pos)
1.5 schwarze 2514: {
1.208 schwarze 2515: const char *start, *end;
2516: char *cp, *name;
2517: size_t sz;
2518: int deftype, len, number, savepos, istrue, wanttrue;
1.5 schwarze 2519:
1.71 schwarze 2520: if ('!' == v[*pos]) {
2521: wanttrue = 0;
2522: (*pos)++;
2523: } else
2524: wanttrue = 1;
2525:
1.5 schwarze 2526: switch (v[*pos]) {
1.112 schwarze 2527: case '\0':
1.149 schwarze 2528: return 0;
1.80 schwarze 2529: case 'n':
2530: case 'o':
1.5 schwarze 2531: (*pos)++;
1.149 schwarze 2532: return wanttrue;
1.80 schwarze 2533: case 'e':
2534: case 't':
1.111 schwarze 2535: case 'v':
1.5 schwarze 2536: (*pos)++;
1.149 schwarze 2537: return !wanttrue;
1.208 schwarze 2538: case 'c':
2539: do {
2540: (*pos)++;
2541: } while (v[*pos] == ' ');
2542:
2543: /*
2544: * Quirk for groff compatibility:
2545: * The horizontal tab is neither available nor unavailable.
2546: */
2547:
2548: if (v[*pos] == '\t') {
2549: (*pos)++;
2550: return 0;
2551: }
2552:
2553: /* Printable ASCII characters are available. */
2554:
2555: if (v[*pos] != '\\') {
2556: (*pos)++;
2557: return wanttrue;
2558: }
2559:
2560: end = v + ++*pos;
2561: switch (mandoc_escape(&end, &start, &len)) {
2562: case ESCAPE_SPECIAL:
2563: istrue = mchars_spec2cp(start, len) != -1;
2564: break;
2565: case ESCAPE_UNICODE:
2566: istrue = 1;
2567: break;
2568: case ESCAPE_NUMBERED:
2569: istrue = mchars_num2char(start, len) != -1;
2570: break;
2571: default:
2572: istrue = !wanttrue;
2573: break;
2574: }
2575: *pos = end - v;
2576: return istrue == wanttrue;
1.182 schwarze 2577: case 'd':
1.143 schwarze 2578: case 'r':
1.182 schwarze 2579: cp = v + *pos + 1;
2580: while (*cp == ' ')
2581: cp++;
2582: name = cp;
2583: sz = roff_getname(r, &cp, ln, cp - v);
1.187 schwarze 2584: if (sz == 0)
2585: istrue = 0;
2586: else if (v[*pos] == 'r')
2587: istrue = roff_hasregn(r, name, sz);
2588: else {
2589: deftype = ROFFDEF_ANY;
2590: roff_getstrn(r, name, sz, &deftype);
2591: istrue = !!deftype;
2592: }
1.235 schwarze 2593: *pos = (name + sz) - v;
1.182 schwarze 2594: return istrue == wanttrue;
1.5 schwarze 2595: default:
2596: break;
2597: }
2598:
1.113 schwarze 2599: savepos = *pos;
1.133 schwarze 2600: if (roff_evalnum(r, ln, v, pos, &number, ROFFNUM_SCALE))
1.149 schwarze 2601: return (number > 0) == wanttrue;
1.113 schwarze 2602: else if (*pos == savepos)
1.149 schwarze 2603: return roff_evalstrcond(v, pos) == wanttrue;
1.77 schwarze 2604: else
1.149 schwarze 2605: return 0;
1.5 schwarze 2606: }
2607:
1.212 schwarze 2608: static int
1.21 schwarze 2609: roff_line_ignore(ROFF_ARGS)
1.6 schwarze 2610: {
1.30 schwarze 2611:
1.149 schwarze 2612: return ROFF_IGN;
1.21 schwarze 2613: }
2614:
1.212 schwarze 2615: static int
1.123 schwarze 2616: roff_insec(ROFF_ARGS)
2617: {
2618:
1.222 schwarze 2619: mandoc_msg(MANDOCERR_REQ_INSEC, ln, ppos, "%s", roff_name[tok]);
1.149 schwarze 2620: return ROFF_IGN;
1.123 schwarze 2621: }
2622:
1.212 schwarze 2623: static int
1.123 schwarze 2624: roff_unsupp(ROFF_ARGS)
2625: {
2626:
1.222 schwarze 2627: mandoc_msg(MANDOCERR_REQ_UNSUPP, ln, ppos, "%s", roff_name[tok]);
1.149 schwarze 2628: return ROFF_IGN;
1.123 schwarze 2629: }
2630:
1.212 schwarze 2631: static int
1.2 schwarze 2632: roff_cond(ROFF_ARGS)
2633: {
1.212 schwarze 2634: int irc;
1.46 schwarze 2635:
2636: roffnode_push(r, tok, NULL, ln, ppos);
1.2 schwarze 2637:
1.80 schwarze 2638: /*
1.35 schwarze 2639: * An `.el' has no conditional body: it will consume the value
2640: * of the current rstack entry set in prior `ie' calls or
1.80 schwarze 2641: * defaults to DENY.
1.35 schwarze 2642: *
2643: * If we're not an `el', however, then evaluate the conditional.
2644: */
1.1 schwarze 2645:
1.110 schwarze 2646: r->last->rule = tok == ROFF_el ?
1.80 schwarze 2647: (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
1.110 schwarze 2648: roff_evalcond(r, ln, buf->buf, &pos);
1.2 schwarze 2649:
1.35 schwarze 2650: /*
2651: * An if-else will put the NEGATION of the current evaluated
2652: * conditional into the stack of rules.
2653: */
2654:
1.110 schwarze 2655: if (tok == ROFF_ie) {
1.96 schwarze 2656: if (r->rstackpos + 1 == r->rstacksz) {
2657: r->rstacksz += 16;
2658: r->rstack = mandoc_reallocarray(r->rstack,
2659: r->rstacksz, sizeof(int));
1.35 schwarze 2660: }
1.71 schwarze 2661: r->rstack[++r->rstackpos] = !r->last->rule;
1.2 schwarze 2662: }
1.5 schwarze 2663:
2664: /* If the parent has false as its rule, then so do we. */
2665:
1.71 schwarze 2666: if (r->last->parent && !r->last->parent->rule)
2667: r->last->rule = 0;
1.5 schwarze 2668:
2669: /*
1.46 schwarze 2670: * Determine scope.
2671: * If there is nothing on the line after the conditional,
2672: * not even whitespace, use next-line scope.
1.212 schwarze 2673: * Except that .while does not support next-line scope.
1.5 schwarze 2674: */
1.2 schwarze 2675:
1.212 schwarze 2676: if (buf->buf[pos] == '\0' && tok != ROFF_while) {
1.46 schwarze 2677: r->last->endspan = 2;
2678: goto out;
2679: }
2680:
1.110 schwarze 2681: while (buf->buf[pos] == ' ')
1.46 schwarze 2682: pos++;
2683:
2684: /* An opening brace requests multiline scope. */
1.2 schwarze 2685:
1.110 schwarze 2686: if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
1.2 schwarze 2687: r->last->endspan = -1;
2688: pos += 2;
1.144 schwarze 2689: while (buf->buf[pos] == ' ')
2690: pos++;
1.46 schwarze 2691: goto out;
1.80 schwarze 2692: }
1.2 schwarze 2693:
2694: /*
1.46 schwarze 2695: * Anything else following the conditional causes
2696: * single-line scope. Warn if the scope contains
2697: * nothing but trailing whitespace.
1.2 schwarze 2698: */
2699:
1.110 schwarze 2700: if (buf->buf[pos] == '\0')
1.222 schwarze 2701: mandoc_msg(MANDOCERR_COND_EMPTY,
2702: ln, ppos, "%s", roff_name[tok]);
1.2 schwarze 2703:
1.46 schwarze 2704: r->last->endspan = 1;
1.1 schwarze 2705:
1.46 schwarze 2706: out:
1.2 schwarze 2707: *offs = pos;
1.212 schwarze 2708: irc = ROFF_RERUN;
2709: if (tok == ROFF_while)
2710: irc |= ROFF_WHILE;
2711: return irc;
1.1 schwarze 2712: }
2713:
1.212 schwarze 2714: static int
1.7 schwarze 2715: roff_ds(ROFF_ARGS)
2716: {
1.85 schwarze 2717: char *string;
2718: const char *name;
2719: size_t namesz;
1.10 schwarze 2720:
1.123 schwarze 2721: /* Ignore groff compatibility mode for now. */
2722:
2723: if (tok == ROFF_ds1)
2724: tok = ROFF_ds;
2725: else if (tok == ROFF_as1)
2726: tok = ROFF_as;
2727:
1.10 schwarze 2728: /*
1.85 schwarze 2729: * The first word is the name of the string.
2730: * If it is empty or terminated by an escape sequence,
2731: * abort the `ds' request without defining anything.
1.10 schwarze 2732: */
1.7 schwarze 2733:
1.110 schwarze 2734: name = string = buf->buf + pos;
2735: if (*name == '\0')
1.149 schwarze 2736: return ROFF_IGN;
1.7 schwarze 2737:
1.85 schwarze 2738: namesz = roff_getname(r, &string, ln, pos);
1.235 schwarze 2739: switch (name[namesz]) {
2740: case '\\':
1.149 schwarze 2741: return ROFF_IGN;
1.235 schwarze 2742: case '\t':
2743: string = buf->buf + pos + namesz;
2744: break;
2745: default:
2746: break;
2747: }
1.85 schwarze 2748:
2749: /* Read past the initial double-quote, if any. */
1.110 schwarze 2750: if (*string == '"')
1.7 schwarze 2751: string++;
2752:
1.10 schwarze 2753: /* The rest is the value. */
1.85 schwarze 2754: roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
2755: ROFF_as == tok);
1.183 schwarze 2756: roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.149 schwarze 2757: return ROFF_IGN;
1.7 schwarze 2758: }
2759:
1.77 schwarze 2760: /*
2761: * Parse a single operator, one or two characters long.
2762: * If the operator is recognized, return success and advance the
2763: * parse point, else return failure and let the parse point unchanged.
2764: */
2765: static int
2766: roff_getop(const char *v, int *pos, char *res)
2767: {
2768:
2769: *res = v[*pos];
2770:
2771: switch (*res) {
1.80 schwarze 2772: case '+':
2773: case '-':
2774: case '*':
2775: case '/':
2776: case '%':
2777: case '&':
2778: case ':':
1.77 schwarze 2779: break;
2780: case '<':
2781: switch (v[*pos + 1]) {
1.80 schwarze 2782: case '=':
1.77 schwarze 2783: *res = 'l';
2784: (*pos)++;
2785: break;
1.80 schwarze 2786: case '>':
1.77 schwarze 2787: *res = '!';
2788: (*pos)++;
2789: break;
1.80 schwarze 2790: case '?':
1.77 schwarze 2791: *res = 'i';
2792: (*pos)++;
2793: break;
2794: default:
2795: break;
2796: }
2797: break;
2798: case '>':
2799: switch (v[*pos + 1]) {
1.80 schwarze 2800: case '=':
1.77 schwarze 2801: *res = 'g';
2802: (*pos)++;
2803: break;
1.80 schwarze 2804: case '?':
1.77 schwarze 2805: *res = 'a';
2806: (*pos)++;
2807: break;
2808: default:
2809: break;
2810: }
2811: break;
2812: case '=':
2813: if ('=' == v[*pos + 1])
2814: (*pos)++;
2815: break;
2816: default:
1.149 schwarze 2817: return 0;
1.77 schwarze 2818: }
2819: (*pos)++;
2820:
1.149 schwarze 2821: return *res;
1.77 schwarze 2822: }
2823:
2824: /*
2825: * Evaluate either a parenthesized numeric expression
2826: * or a single signed integer number.
2827: */
2828: static int
1.106 schwarze 2829: roff_evalpar(struct roff *r, int ln,
1.133 schwarze 2830: const char *v, int *pos, int *res, int flags)
1.77 schwarze 2831: {
2832:
2833: if ('(' != v[*pos])
1.149 schwarze 2834: return roff_getnum(v, pos, res, flags);
1.77 schwarze 2835:
2836: (*pos)++;
1.133 schwarze 2837: if ( ! roff_evalnum(r, ln, v, pos, res, flags | ROFFNUM_WHITE))
1.149 schwarze 2838: return 0;
1.77 schwarze 2839:
1.79 schwarze 2840: /*
2841: * Omission of the closing parenthesis
2842: * is an error in validation mode,
2843: * but ignored in evaluation mode.
2844: */
2845:
1.77 schwarze 2846: if (')' == v[*pos])
2847: (*pos)++;
1.79 schwarze 2848: else if (NULL == res)
1.149 schwarze 2849: return 0;
1.77 schwarze 2850:
1.149 schwarze 2851: return 1;
1.77 schwarze 2852: }
2853:
2854: /*
2855: * Evaluate a complete numeric expression.
2856: * Proceed left to right, there is no concept of precedence.
2857: */
2858: static int
1.106 schwarze 2859: roff_evalnum(struct roff *r, int ln, const char *v,
1.133 schwarze 2860: int *pos, int *res, int flags)
1.77 schwarze 2861: {
2862: int mypos, operand2;
2863: char operator;
2864:
2865: if (NULL == pos) {
2866: mypos = 0;
2867: pos = &mypos;
2868: }
2869:
1.133 schwarze 2870: if (flags & ROFFNUM_WHITE)
1.77 schwarze 2871: while (isspace((unsigned char)v[*pos]))
2872: (*pos)++;
2873:
1.133 schwarze 2874: if ( ! roff_evalpar(r, ln, v, pos, res, flags))
1.149 schwarze 2875: return 0;
1.77 schwarze 2876:
2877: while (1) {
1.133 schwarze 2878: if (flags & ROFFNUM_WHITE)
1.77 schwarze 2879: while (isspace((unsigned char)v[*pos]))
2880: (*pos)++;
2881:
2882: if ( ! roff_getop(v, pos, &operator))
2883: break;
2884:
1.133 schwarze 2885: if (flags & ROFFNUM_WHITE)
1.77 schwarze 2886: while (isspace((unsigned char)v[*pos]))
2887: (*pos)++;
2888:
1.133 schwarze 2889: if ( ! roff_evalpar(r, ln, v, pos, &operand2, flags))
1.149 schwarze 2890: return 0;
1.77 schwarze 2891:
1.133 schwarze 2892: if (flags & ROFFNUM_WHITE)
1.77 schwarze 2893: while (isspace((unsigned char)v[*pos]))
2894: (*pos)++;
1.79 schwarze 2895:
2896: if (NULL == res)
2897: continue;
1.77 schwarze 2898:
2899: switch (operator) {
1.80 schwarze 2900: case '+':
1.77 schwarze 2901: *res += operand2;
2902: break;
1.80 schwarze 2903: case '-':
1.77 schwarze 2904: *res -= operand2;
2905: break;
1.80 schwarze 2906: case '*':
1.77 schwarze 2907: *res *= operand2;
2908: break;
1.80 schwarze 2909: case '/':
1.116 schwarze 2910: if (operand2 == 0) {
1.106 schwarze 2911: mandoc_msg(MANDOCERR_DIVZERO,
1.222 schwarze 2912: ln, *pos, "%s", v);
1.106 schwarze 2913: *res = 0;
2914: break;
2915: }
1.77 schwarze 2916: *res /= operand2;
2917: break;
1.80 schwarze 2918: case '%':
1.116 schwarze 2919: if (operand2 == 0) {
2920: mandoc_msg(MANDOCERR_DIVZERO,
1.222 schwarze 2921: ln, *pos, "%s", v);
1.116 schwarze 2922: *res = 0;
2923: break;
2924: }
1.77 schwarze 2925: *res %= operand2;
2926: break;
1.80 schwarze 2927: case '<':
1.77 schwarze 2928: *res = *res < operand2;
2929: break;
1.80 schwarze 2930: case '>':
1.77 schwarze 2931: *res = *res > operand2;
2932: break;
1.80 schwarze 2933: case 'l':
1.77 schwarze 2934: *res = *res <= operand2;
2935: break;
1.80 schwarze 2936: case 'g':
1.77 schwarze 2937: *res = *res >= operand2;
2938: break;
1.80 schwarze 2939: case '=':
1.77 schwarze 2940: *res = *res == operand2;
2941: break;
1.80 schwarze 2942: case '!':
1.77 schwarze 2943: *res = *res != operand2;
2944: break;
1.80 schwarze 2945: case '&':
1.77 schwarze 2946: *res = *res && operand2;
2947: break;
1.80 schwarze 2948: case ':':
1.77 schwarze 2949: *res = *res || operand2;
2950: break;
1.80 schwarze 2951: case 'i':
1.77 schwarze 2952: if (operand2 < *res)
2953: *res = operand2;
2954: break;
1.80 schwarze 2955: case 'a':
1.77 schwarze 2956: if (operand2 > *res)
2957: *res = operand2;
2958: break;
2959: default:
2960: abort();
2961: }
2962: }
1.149 schwarze 2963: return 1;
1.77 schwarze 2964: }
2965:
1.138 schwarze 2966: /* --- register management ------------------------------------------------ */
2967:
1.52 schwarze 2968: void
1.60 schwarze 2969: roff_setreg(struct roff *r, const char *name, int val, char sign)
1.41 schwarze 2970: {
1.199 schwarze 2971: roff_setregn(r, name, strlen(name), val, sign, INT_MIN);
1.198 schwarze 2972: }
2973:
2974: static void
2975: roff_setregn(struct roff *r, const char *name, size_t len,
1.199 schwarze 2976: int val, char sign, int step)
1.198 schwarze 2977: {
1.52 schwarze 2978: struct roffreg *reg;
2979:
2980: /* Search for an existing register with the same name. */
2981: reg = r->regtab;
2982:
1.198 schwarze 2983: while (reg != NULL && (reg->key.sz != len ||
2984: strncmp(reg->key.p, name, len) != 0))
1.52 schwarze 2985: reg = reg->next;
1.41 schwarze 2986:
1.52 schwarze 2987: if (NULL == reg) {
2988: /* Create a new register. */
2989: reg = mandoc_malloc(sizeof(struct roffreg));
1.198 schwarze 2990: reg->key.p = mandoc_strndup(name, len);
2991: reg->key.sz = len;
1.60 schwarze 2992: reg->val = 0;
1.199 schwarze 2993: reg->step = 0;
1.52 schwarze 2994: reg->next = r->regtab;
2995: r->regtab = reg;
2996: }
2997:
1.60 schwarze 2998: if ('+' == sign)
2999: reg->val += val;
3000: else if ('-' == sign)
3001: reg->val -= val;
3002: else
3003: reg->val = val;
1.199 schwarze 3004: if (step != INT_MIN)
3005: reg->step = step;
1.41 schwarze 3006: }
3007:
1.65 schwarze 3008: /*
3009: * Handle some predefined read-only number registers.
3010: * For now, return -1 if the requested register is not predefined;
3011: * in case a predefined read-only register having the value -1
3012: * were to turn up, another special value would have to be chosen.
3013: */
3014: static int
1.145 schwarze 3015: roff_getregro(const struct roff *r, const char *name)
1.65 schwarze 3016: {
3017:
3018: switch (*name) {
1.145 schwarze 3019: case '$': /* Number of arguments of the last macro evaluated. */
1.211 schwarze 3020: return r->mstackpos < 0 ? 0 : r->mstack[r->mstackpos].argc;
1.80 schwarze 3021: case 'A': /* ASCII approximation mode is always off. */
1.149 schwarze 3022: return 0;
1.80 schwarze 3023: case 'g': /* Groff compatibility mode is always on. */
1.149 schwarze 3024: return 1;
1.80 schwarze 3025: case 'H': /* Fixed horizontal resolution. */
1.149 schwarze 3026: return 24;
1.80 schwarze 3027: case 'j': /* Always adjust left margin only. */
1.149 schwarze 3028: return 0;
1.80 schwarze 3029: case 'T': /* Some output device is always defined. */
1.149 schwarze 3030: return 1;
1.80 schwarze 3031: case 'V': /* Fixed vertical resolution. */
1.149 schwarze 3032: return 40;
1.65 schwarze 3033: default:
1.149 schwarze 3034: return -1;
1.65 schwarze 3035: }
3036: }
3037:
1.53 schwarze 3038: int
1.198 schwarze 3039: roff_getreg(struct roff *r, const char *name)
1.41 schwarze 3040: {
1.199 schwarze 3041: return roff_getregn(r, name, strlen(name), '\0');
1.53 schwarze 3042: }
3043:
3044: static int
1.199 schwarze 3045: roff_getregn(struct roff *r, const char *name, size_t len, char sign)
1.53 schwarze 3046: {
3047: struct roffreg *reg;
1.65 schwarze 3048: int val;
3049:
3050: if ('.' == name[0] && 2 == len) {
1.145 schwarze 3051: val = roff_getregro(r, name + 1);
1.65 schwarze 3052: if (-1 != val)
1.149 schwarze 3053: return val;
1.65 schwarze 3054: }
1.53 schwarze 3055:
1.199 schwarze 3056: for (reg = r->regtab; reg; reg = reg->next) {
1.53 schwarze 3057: if (len == reg->key.sz &&
1.199 schwarze 3058: 0 == strncmp(name, reg->key.p, len)) {
3059: switch (sign) {
3060: case '+':
3061: reg->val += reg->step;
3062: break;
3063: case '-':
3064: reg->val -= reg->step;
3065: break;
3066: default:
3067: break;
3068: }
1.149 schwarze 3069: return reg->val;
1.199 schwarze 3070: }
3071: }
1.143 schwarze 3072:
1.199 schwarze 3073: roff_setregn(r, name, len, 0, '\0', INT_MIN);
1.149 schwarze 3074: return 0;
1.143 schwarze 3075: }
3076:
3077: static int
3078: roff_hasregn(const struct roff *r, const char *name, size_t len)
3079: {
3080: struct roffreg *reg;
3081: int val;
3082:
3083: if ('.' == name[0] && 2 == len) {
1.145 schwarze 3084: val = roff_getregro(r, name + 1);
1.143 schwarze 3085: if (-1 != val)
1.149 schwarze 3086: return 1;
1.143 schwarze 3087: }
3088:
3089: for (reg = r->regtab; reg; reg = reg->next)
3090: if (len == reg->key.sz &&
3091: 0 == strncmp(name, reg->key.p, len))
1.149 schwarze 3092: return 1;
1.41 schwarze 3093:
1.149 schwarze 3094: return 0;
1.41 schwarze 3095: }
3096:
1.52 schwarze 3097: static void
3098: roff_freereg(struct roffreg *reg)
1.41 schwarze 3099: {
1.52 schwarze 3100: struct roffreg *old_reg;
1.41 schwarze 3101:
1.52 schwarze 3102: while (NULL != reg) {
3103: free(reg->key.p);
3104: old_reg = reg;
3105: reg = reg->next;
3106: free(old_reg);
3107: }
1.41 schwarze 3108: }
1.7 schwarze 3109:
1.212 schwarze 3110: static int
1.6 schwarze 3111: roff_nr(ROFF_ARGS)
1.1 schwarze 3112: {
1.199 schwarze 3113: char *key, *val, *step;
1.85 schwarze 3114: size_t keysz;
1.199 schwarze 3115: int iv, is, len;
1.60 schwarze 3116: char sign;
1.6 schwarze 3117:
1.110 schwarze 3118: key = val = buf->buf + pos;
3119: if (*key == '\0')
1.149 schwarze 3120: return ROFF_IGN;
1.85 schwarze 3121:
3122: keysz = roff_getname(r, &val, ln, pos);
1.235 schwarze 3123: if (key[keysz] == '\\' || key[keysz] == '\t')
1.149 schwarze 3124: return ROFF_IGN;
1.6 schwarze 3125:
1.60 schwarze 3126: sign = *val;
1.110 schwarze 3127: if (sign == '+' || sign == '-')
1.60 schwarze 3128: val++;
3129:
1.199 schwarze 3130: len = 0;
3131: if (roff_evalnum(r, ln, val, &len, &iv, ROFFNUM_SCALE) == 0)
3132: return ROFF_IGN;
3133:
3134: step = val + len;
3135: while (isspace((unsigned char)*step))
3136: step++;
3137: if (roff_evalnum(r, ln, step, NULL, &is, 0) == 0)
3138: is = INT_MIN;
1.1 schwarze 3139:
1.199 schwarze 3140: roff_setregn(r, key, keysz, iv, sign, is);
1.149 schwarze 3141: return ROFF_IGN;
1.76 schwarze 3142: }
3143:
1.212 schwarze 3144: static int
1.76 schwarze 3145: roff_rr(ROFF_ARGS)
3146: {
3147: struct roffreg *reg, **prev;
1.85 schwarze 3148: char *name, *cp;
3149: size_t namesz;
1.76 schwarze 3150:
1.110 schwarze 3151: name = cp = buf->buf + pos;
3152: if (*name == '\0')
1.149 schwarze 3153: return ROFF_IGN;
1.85 schwarze 3154: namesz = roff_getname(r, &cp, ln, pos);
3155: name[namesz] = '\0';
1.76 schwarze 3156:
3157: prev = &r->regtab;
3158: while (1) {
3159: reg = *prev;
1.110 schwarze 3160: if (reg == NULL || !strcmp(name, reg->key.p))
1.76 schwarze 3161: break;
3162: prev = ®->next;
3163: }
1.110 schwarze 3164: if (reg != NULL) {
1.76 schwarze 3165: *prev = reg->next;
3166: free(reg->key.p);
3167: free(reg);
3168: }
1.149 schwarze 3169: return ROFF_IGN;
1.29 schwarze 3170: }
3171:
1.138 schwarze 3172: /* --- handler functions for roff requests -------------------------------- */
3173:
1.212 schwarze 3174: static int
1.29 schwarze 3175: roff_rm(ROFF_ARGS)
3176: {
3177: const char *name;
3178: char *cp;
1.85 schwarze 3179: size_t namesz;
1.29 schwarze 3180:
1.110 schwarze 3181: cp = buf->buf + pos;
3182: while (*cp != '\0') {
1.85 schwarze 3183: name = cp;
1.110 schwarze 3184: namesz = roff_getname(r, &cp, ln, (int)(cp - buf->buf));
1.85 schwarze 3185: roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
1.183 schwarze 3186: roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.235 schwarze 3187: if (name[namesz] == '\\' || name[namesz] == '\t')
1.85 schwarze 3188: break;
1.29 schwarze 3189: }
1.149 schwarze 3190: return ROFF_IGN;
1.51 schwarze 3191: }
3192:
1.212 schwarze 3193: static int
1.51 schwarze 3194: roff_it(ROFF_ARGS)
3195: {
3196: int iv;
3197:
3198: /* Parse the number of lines. */
1.133 schwarze 3199:
3200: if ( ! roff_evalnum(r, ln, buf->buf, &pos, &iv, 0)) {
1.222 schwarze 3201: mandoc_msg(MANDOCERR_IT_NONUM,
3202: ln, ppos, "%s", buf->buf + 1);
1.149 schwarze 3203: return ROFF_IGN;
1.51 schwarze 3204: }
3205:
1.134 schwarze 3206: while (isspace((unsigned char)buf->buf[pos]))
3207: pos++;
3208:
3209: /*
3210: * Arm the input line trap.
3211: * Special-casing "an-trap" is an ugly workaround to cope
3212: * with DocBook stupidly fiddling with man(7) internals.
3213: */
1.133 schwarze 3214:
1.51 schwarze 3215: roffit_lines = iv;
1.134 schwarze 3216: roffit_macro = mandoc_strdup(iv != 1 ||
3217: strcmp(buf->buf + pos, "an-trap") ?
3218: buf->buf + pos : "br");
1.149 schwarze 3219: return ROFF_IGN;
1.47 schwarze 3220: }
3221:
1.212 schwarze 3222: static int
1.47 schwarze 3223: roff_Dd(ROFF_ARGS)
3224: {
1.187 schwarze 3225: int mask;
3226: enum roff_tok t, te;
1.99 schwarze 3227:
1.187 schwarze 3228: switch (tok) {
3229: case ROFF_Dd:
3230: tok = MDOC_Dd;
3231: te = MDOC_MAX;
3232: if (r->format == 0)
3233: r->format = MPARSE_MDOC;
3234: mask = MPARSE_MDOC | MPARSE_QUICK;
3235: break;
3236: case ROFF_TH:
3237: tok = MAN_TH;
3238: te = MAN_MAX;
3239: if (r->format == 0)
3240: r->format = MPARSE_MAN;
3241: mask = MPARSE_QUICK;
3242: break;
3243: default:
3244: abort();
3245: }
3246: if ((r->options & mask) == 0)
3247: for (t = tok; t < te; t++)
3248: roff_setstr(r, roff_name[t], NULL, 0);
1.149 schwarze 3249: return ROFF_CONT;
1.14 schwarze 3250: }
3251:
1.212 schwarze 3252: static int
1.27 schwarze 3253: roff_TE(ROFF_ARGS)
3254: {
1.233 schwarze 3255: r->man->flags &= ~ROFF_NONOFILL;
1.193 schwarze 3256: if (r->tbl == NULL) {
1.222 schwarze 3257: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "TE");
1.193 schwarze 3258: return ROFF_IGN;
3259: }
1.218 schwarze 3260: if (tbl_end(r->tbl, 0) == 0) {
1.193 schwarze 3261: r->tbl = NULL;
1.130 schwarze 3262: free(buf->buf);
3263: buf->buf = mandoc_strdup(".sp");
3264: buf->sz = 4;
1.201 schwarze 3265: *offs = 0;
1.149 schwarze 3266: return ROFF_REPARSE;
1.130 schwarze 3267: }
1.193 schwarze 3268: r->tbl = NULL;
1.149 schwarze 3269: return ROFF_IGN;
1.27 schwarze 3270: }
3271:
1.212 schwarze 3272: static int
1.27 schwarze 3273: roff_T_(ROFF_ARGS)
3274: {
3275:
3276: if (NULL == r->tbl)
1.222 schwarze 3277: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "T&");
1.27 schwarze 3278: else
1.168 schwarze 3279: tbl_restart(ln, ppos, r->tbl);
1.27 schwarze 3280:
1.149 schwarze 3281: return ROFF_IGN;
1.27 schwarze 3282: }
3283:
1.102 schwarze 3284: /*
3285: * Handle in-line equation delimiters.
3286: */
1.212 schwarze 3287: static int
1.110 schwarze 3288: roff_eqndelim(struct roff *r, struct buf *buf, int pos)
1.41 schwarze 3289: {
1.105 schwarze 3290: char *cp1, *cp2;
3291: const char *bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
1.41 schwarze 3292:
1.102 schwarze 3293: /*
3294: * Outside equations, look for an opening delimiter.
3295: * If we are inside an equation, we already know it is
3296: * in-line, or this function wouldn't have been called;
3297: * so look for a closing delimiter.
3298: */
3299:
1.110 schwarze 3300: cp1 = buf->buf + pos;
1.102 schwarze 3301: cp2 = strchr(cp1, r->eqn == NULL ?
3302: r->last_eqn->odelim : r->last_eqn->cdelim);
3303: if (cp2 == NULL)
1.149 schwarze 3304: return ROFF_CONT;
1.102 schwarze 3305:
1.105 schwarze 3306: *cp2++ = '\0';
3307: bef_pr = bef_nl = aft_nl = aft_pr = "";
3308:
3309: /* Handle preceding text, protecting whitespace. */
3310:
1.110 schwarze 3311: if (*buf->buf != '\0') {
1.105 schwarze 3312: if (r->eqn == NULL)
3313: bef_pr = "\\&";
3314: bef_nl = "\n";
3315: }
3316:
3317: /*
3318: * Prepare replacing the delimiter with an equation macro
3319: * and drop leading white space from the equation.
3320: */
1.102 schwarze 3321:
1.105 schwarze 3322: if (r->eqn == NULL) {
3323: while (*cp2 == ' ')
3324: cp2++;
3325: mac = ".EQ";
3326: } else
3327: mac = ".EN";
3328:
3329: /* Handle following text, protecting whitespace. */
3330:
3331: if (*cp2 != '\0') {
3332: aft_nl = "\n";
3333: if (r->eqn != NULL)
3334: aft_pr = "\\&";
3335: }
3336:
3337: /* Do the actual replacement. */
3338:
1.110 schwarze 3339: buf->sz = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", buf->buf,
1.105 schwarze 3340: bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
1.110 schwarze 3341: free(buf->buf);
3342: buf->buf = cp1;
1.102 schwarze 3343:
3344: /* Toggle the in-line state of the eqn subsystem. */
3345:
3346: r->eqn_inline = r->eqn == NULL;
1.149 schwarze 3347: return ROFF_REPARSE;
1.41 schwarze 3348: }
3349:
1.212 schwarze 3350: static int
1.107 schwarze 3351: roff_EQ(ROFF_ARGS)
1.32 schwarze 3352: {
1.191 schwarze 3353: struct roff_node *n;
3354:
1.228 schwarze 3355: if (r->man->meta.macroset == MACROSET_MAN)
1.194 schwarze 3356: man_breakscope(r->man, ROFF_EQ);
1.191 schwarze 3357: n = roff_node_alloc(r->man, ln, ppos, ROFFT_EQN, TOKEN_NONE);
3358: if (ln > r->man->last->line)
3359: n->flags |= NODE_LINE;
1.220 schwarze 3360: n->eqn = eqn_box_new();
1.191 schwarze 3361: roff_node_append(r->man, n);
3362: r->man->next = ROFF_NEXT_SIBLING;
1.32 schwarze 3363:
1.110 schwarze 3364: assert(r->eqn == NULL);
1.191 schwarze 3365: if (r->last_eqn == NULL)
1.223 schwarze 3366: r->last_eqn = eqn_alloc();
1.191 schwarze 3367: else
3368: eqn_reset(r->last_eqn);
3369: r->eqn = r->last_eqn;
3370: r->eqn->node = n;
1.41 schwarze 3371:
1.110 schwarze 3372: if (buf->buf[pos] != '\0')
1.222 schwarze 3373: mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
1.110 schwarze 3374: ".EQ %s", buf->buf + pos);
1.41 schwarze 3375:
1.149 schwarze 3376: return ROFF_IGN;
1.32 schwarze 3377: }
3378:
1.212 schwarze 3379: static int
1.32 schwarze 3380: roff_EN(ROFF_ARGS)
3381: {
1.191 schwarze 3382: if (r->eqn != NULL) {
3383: eqn_parse(r->eqn);
3384: r->eqn = NULL;
3385: } else
1.222 schwarze 3386: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "EN");
1.191 schwarze 3387: if (buf->buf[pos] != '\0')
1.222 schwarze 3388: mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
1.191 schwarze 3389: "EN %s", buf->buf + pos);
1.149 schwarze 3390: return ROFF_IGN;
1.32 schwarze 3391: }
3392:
1.212 schwarze 3393: static int
1.27 schwarze 3394: roff_TS(ROFF_ARGS)
3395: {
1.193 schwarze 3396: if (r->tbl != NULL) {
1.222 schwarze 3397: mandoc_msg(MANDOCERR_BLK_BROKEN, ln, ppos, "TS breaks TS");
1.218 schwarze 3398: tbl_end(r->tbl, 0);
1.27 schwarze 3399: }
1.233 schwarze 3400: r->man->flags |= ROFF_NONOFILL;
1.223 schwarze 3401: r->tbl = tbl_alloc(ppos, ln, r->last_tbl);
1.218 schwarze 3402: if (r->last_tbl == NULL)
1.193 schwarze 3403: r->first_tbl = r->tbl;
3404: r->last_tbl = r->tbl;
1.169 schwarze 3405: return ROFF_IGN;
3406: }
3407:
1.212 schwarze 3408: static int
1.230 schwarze 3409: roff_noarg(ROFF_ARGS)
3410: {
3411: if (r->man->flags & (MAN_BLINE | MAN_ELINE))
3412: man_breakscope(r->man, tok);
3413: if (tok == ROFF_brp)
3414: tok = ROFF_br;
3415: roff_elem_alloc(r->man, ln, ppos, tok);
3416: if (buf->buf[pos] != '\0')
3417: mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
3418: "%s %s", roff_name[tok], buf->buf + pos);
3419: if (tok == ROFF_nf)
3420: r->man->flags |= ROFF_NOFILL;
3421: else if (tok == ROFF_fi)
3422: r->man->flags &= ~ROFF_NOFILL;
3423: r->man->last->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
3424: r->man->next = ROFF_NEXT_SIBLING;
3425: return ROFF_IGN;
3426: }
3427:
3428: static int
1.169 schwarze 3429: roff_onearg(ROFF_ARGS)
3430: {
3431: struct roff_node *n;
3432: char *cp;
1.177 schwarze 3433: int npos;
1.169 schwarze 3434:
1.174 schwarze 3435: if (r->man->flags & (MAN_BLINE | MAN_ELINE) &&
1.192 schwarze 3436: (tok == ROFF_ce || tok == ROFF_rj || tok == ROFF_sp ||
3437: tok == ROFF_ti))
1.174 schwarze 3438: man_breakscope(r->man, tok);
3439:
1.181 schwarze 3440: if (roffce_node != NULL && (tok == ROFF_ce || tok == ROFF_rj)) {
1.177 schwarze 3441: r->man->last = roffce_node;
3442: r->man->next = ROFF_NEXT_SIBLING;
3443: }
3444:
1.169 schwarze 3445: roff_elem_alloc(r->man, ln, ppos, tok);
3446: n = r->man->last;
3447:
3448: cp = buf->buf + pos;
3449: if (*cp != '\0') {
3450: while (*cp != '\0' && *cp != ' ')
3451: cp++;
3452: while (*cp == ' ')
3453: *cp++ = '\0';
3454: if (*cp != '\0')
1.222 schwarze 3455: mandoc_msg(MANDOCERR_ARG_EXCESS,
3456: ln, (int)(cp - buf->buf),
1.169 schwarze 3457: "%s ... %s", roff_name[tok], cp);
3458: roff_word_alloc(r->man, ln, pos, buf->buf + pos);
1.172 schwarze 3459: }
3460:
1.181 schwarze 3461: if (tok == ROFF_ce || tok == ROFF_rj) {
3462: if (r->man->last->type == ROFFT_ELEM) {
1.177 schwarze 3463: roff_word_alloc(r->man, ln, pos, "1");
3464: r->man->last->flags |= NODE_NOSRC;
3465: }
3466: npos = 0;
3467: if (roff_evalnum(r, ln, r->man->last->string, &npos,
3468: &roffce_lines, 0) == 0) {
1.222 schwarze 3469: mandoc_msg(MANDOCERR_CE_NONUM,
3470: ln, pos, "ce %s", buf->buf + pos);
1.177 schwarze 3471: roffce_lines = 1;
3472: }
3473: if (roffce_lines < 1) {
3474: r->man->last = r->man->last->parent;
3475: roffce_node = NULL;
3476: roffce_lines = 0;
3477: } else
3478: roffce_node = r->man->last->parent;
3479: } else {
3480: n->flags |= NODE_VALID | NODE_ENDED;
3481: r->man->last = n;
3482: }
3483: n->flags |= NODE_LINE;
1.172 schwarze 3484: r->man->next = ROFF_NEXT_SIBLING;
3485: return ROFF_IGN;
3486: }
3487:
1.212 schwarze 3488: static int
1.172 schwarze 3489: roff_manyarg(ROFF_ARGS)
3490: {
3491: struct roff_node *n;
3492: char *sp, *ep;
3493:
3494: roff_elem_alloc(r->man, ln, ppos, tok);
3495: n = r->man->last;
3496:
3497: for (sp = ep = buf->buf + pos; *sp != '\0'; sp = ep) {
3498: while (*ep != '\0' && *ep != ' ')
3499: ep++;
3500: while (*ep == ' ')
3501: *ep++ = '\0';
3502: roff_word_alloc(r->man, ln, sp - buf->buf, sp);
1.169 schwarze 3503: }
3504:
3505: n->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
3506: r->man->last = n;
3507: r->man->next = ROFF_NEXT_SIBLING;
1.149 schwarze 3508: return ROFF_IGN;
1.123 schwarze 3509: }
3510:
1.212 schwarze 3511: static int
1.183 schwarze 3512: roff_als(ROFF_ARGS)
3513: {
3514: char *oldn, *newn, *end, *value;
3515: size_t oldsz, newsz, valsz;
3516:
3517: newn = oldn = buf->buf + pos;
3518: if (*newn == '\0')
3519: return ROFF_IGN;
3520:
3521: newsz = roff_getname(r, &oldn, ln, pos);
1.235 schwarze 3522: if (newn[newsz] == '\\' || newn[newsz] == '\t' || *oldn == '\0')
1.183 schwarze 3523: return ROFF_IGN;
3524:
3525: end = oldn;
3526: oldsz = roff_getname(r, &end, ln, oldn - buf->buf);
3527: if (oldsz == 0)
3528: return ROFF_IGN;
3529:
1.210 schwarze 3530: valsz = mandoc_asprintf(&value, ".%.*s \\$@\\\"\n",
1.187 schwarze 3531: (int)oldsz, oldn);
1.183 schwarze 3532: roff_setstrn(&r->strtab, newn, newsz, value, valsz, 0);
3533: roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
3534: free(value);
1.236 schwarze 3535: return ROFF_IGN;
3536: }
3537:
3538: /*
3539: * The .break request only makes sense inside conditionals,
3540: * and that case is already handled in roff_cond_sub().
3541: */
3542: static int
3543: roff_break(ROFF_ARGS)
3544: {
3545: mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, pos, "break");
1.168 schwarze 3546: return ROFF_IGN;
1.27 schwarze 3547: }
3548:
1.212 schwarze 3549: static int
1.48 schwarze 3550: roff_cc(ROFF_ARGS)
3551: {
3552: const char *p;
3553:
1.110 schwarze 3554: p = buf->buf + pos;
1.48 schwarze 3555:
1.110 schwarze 3556: if (*p == '\0' || (r->control = *p++) == '.')
1.175 schwarze 3557: r->control = '\0';
1.48 schwarze 3558:
1.110 schwarze 3559: if (*p != '\0')
1.222 schwarze 3560: mandoc_msg(MANDOCERR_ARG_EXCESS,
1.132 schwarze 3561: ln, p - buf->buf, "cc ... %s", p);
1.48 schwarze 3562:
1.213 schwarze 3563: return ROFF_IGN;
3564: }
3565:
3566: static int
3567: roff_char(ROFF_ARGS)
3568: {
3569: const char *p, *kp, *vp;
3570: size_t ksz, vsz;
3571: int font;
3572:
3573: /* Parse the character to be replaced. */
3574:
3575: kp = buf->buf + pos;
3576: p = kp + 1;
3577: if (*kp == '\0' || (*kp == '\\' &&
3578: mandoc_escape(&p, NULL, NULL) != ESCAPE_SPECIAL) ||
3579: (*p != ' ' && *p != '\0')) {
1.222 schwarze 3580: mandoc_msg(MANDOCERR_CHAR_ARG, ln, pos, "char %s", kp);
1.213 schwarze 3581: return ROFF_IGN;
3582: }
3583: ksz = p - kp;
3584: while (*p == ' ')
3585: p++;
3586:
3587: /*
3588: * If the replacement string contains a font escape sequence,
3589: * we have to restore the font at the end.
3590: */
3591:
3592: vp = p;
3593: vsz = strlen(p);
3594: font = 0;
3595: while (*p != '\0') {
3596: if (*p++ != '\\')
3597: continue;
3598: switch (mandoc_escape(&p, NULL, NULL)) {
3599: case ESCAPE_FONT:
3600: case ESCAPE_FONTROMAN:
3601: case ESCAPE_FONTITALIC:
3602: case ESCAPE_FONTBOLD:
3603: case ESCAPE_FONTBI:
1.250 schwarze 3604: case ESCAPE_FONTCR:
3605: case ESCAPE_FONTCB:
3606: case ESCAPE_FONTCI:
1.213 schwarze 3607: case ESCAPE_FONTPREV:
3608: font++;
3609: break;
3610: default:
3611: break;
3612: }
3613: }
3614: if (font > 1)
1.222 schwarze 3615: mandoc_msg(MANDOCERR_CHAR_FONT,
3616: ln, (int)(vp - buf->buf), "%s", vp);
1.213 schwarze 3617:
3618: /*
3619: * Approximate the effect of .char using the .tr tables.
3620: * XXX In groff, .char and .tr interact differently.
3621: */
3622:
3623: if (ksz == 1) {
3624: if (r->xtab == NULL)
3625: r->xtab = mandoc_calloc(128, sizeof(*r->xtab));
3626: assert((unsigned int)*kp < 128);
3627: free(r->xtab[(int)*kp].p);
3628: r->xtab[(int)*kp].sz = mandoc_asprintf(&r->xtab[(int)*kp].p,
3629: "%s%s", vp, font ? "\fP" : "");
3630: } else {
3631: roff_setstrn(&r->xmbtab, kp, ksz, vp, vsz, 0);
3632: if (font)
3633: roff_setstrn(&r->xmbtab, kp, ksz, "\\fP", 3, 1);
3634: }
1.149 schwarze 3635: return ROFF_IGN;
1.48 schwarze 3636: }
3637:
1.212 schwarze 3638: static int
1.175 schwarze 3639: roff_ec(ROFF_ARGS)
3640: {
3641: const char *p;
3642:
3643: p = buf->buf + pos;
3644: if (*p == '\0')
3645: r->escape = '\\';
3646: else {
3647: r->escape = *p;
3648: if (*++p != '\0')
1.222 schwarze 3649: mandoc_msg(MANDOCERR_ARG_EXCESS, ln,
3650: (int)(p - buf->buf), "ec ... %s", p);
1.175 schwarze 3651: }
3652: return ROFF_IGN;
3653: }
3654:
1.212 schwarze 3655: static int
1.175 schwarze 3656: roff_eo(ROFF_ARGS)
3657: {
3658: r->escape = '\0';
3659: if (buf->buf[pos] != '\0')
1.222 schwarze 3660: mandoc_msg(MANDOCERR_ARG_SKIP,
1.175 schwarze 3661: ln, pos, "eo %s", buf->buf + pos);
1.256 schwarze 3662: return ROFF_IGN;
3663: }
3664:
3665: static int
3666: roff_mc(ROFF_ARGS)
3667: {
3668: struct roff_node *n;
3669: char *cp;
3670:
3671: /* Parse the first argument. */
3672:
3673: cp = buf->buf + pos;
3674: if (*cp != '\0')
3675: cp++;
3676: if (buf->buf[pos] == '\\') {
3677: switch (mandoc_escape((const char **)&cp, NULL, NULL)) {
3678: case ESCAPE_SPECIAL:
3679: case ESCAPE_UNICODE:
3680: case ESCAPE_NUMBERED:
3681: break;
3682: default:
3683: *cp = '\0';
3684: mandoc_msg(MANDOCERR_MC_ESC, ln, pos,
3685: "mc %s", buf->buf + pos);
3686: buf->buf[pos] = '\0';
3687: break;
3688: }
3689: }
3690:
3691: /* Ignore additional arguments. */
3692:
3693: while (*cp == ' ')
3694: *cp++ = '\0';
3695: if (*cp != '\0') {
3696: mandoc_msg(MANDOCERR_MC_DIST, ln, (int)(cp - buf->buf),
3697: "mc ... %s", cp);
3698: *cp = '\0';
3699: }
3700:
3701: /* Create the .mc node. */
3702:
3703: roff_elem_alloc(r->man, ln, ppos, tok);
3704: n = r->man->last;
3705: if (buf->buf[pos] != '\0')
3706: roff_word_alloc(r->man, ln, pos, buf->buf + pos);
3707: n->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
3708: r->man->last = n;
3709: r->man->next = ROFF_NEXT_SIBLING;
1.175 schwarze 3710: return ROFF_IGN;
1.202 schwarze 3711: }
3712:
1.212 schwarze 3713: static int
1.202 schwarze 3714: roff_nop(ROFF_ARGS)
3715: {
3716: while (buf->buf[pos] == ' ')
3717: pos++;
3718: *offs = pos;
3719: return ROFF_RERUN;
1.175 schwarze 3720: }
3721:
1.212 schwarze 3722: static int
1.42 schwarze 3723: roff_tr(ROFF_ARGS)
3724: {
3725: const char *p, *first, *second;
3726: size_t fsz, ssz;
3727: enum mandoc_esc esc;
3728:
1.110 schwarze 3729: p = buf->buf + pos;
1.42 schwarze 3730:
1.110 schwarze 3731: if (*p == '\0') {
1.222 schwarze 3732: mandoc_msg(MANDOCERR_REQ_EMPTY, ln, ppos, "tr");
1.149 schwarze 3733: return ROFF_IGN;
1.42 schwarze 3734: }
3735:
1.110 schwarze 3736: while (*p != '\0') {
1.42 schwarze 3737: fsz = ssz = 1;
3738:
3739: first = p++;
1.110 schwarze 3740: if (*first == '\\') {
1.42 schwarze 3741: esc = mandoc_escape(&p, NULL, NULL);
1.110 schwarze 3742: if (esc == ESCAPE_ERROR) {
1.222 schwarze 3743: mandoc_msg(MANDOCERR_ESC_BAD, ln,
3744: (int)(p - buf->buf), "%s", first);
1.149 schwarze 3745: return ROFF_IGN;
1.42 schwarze 3746: }
3747: fsz = (size_t)(p - first);
3748: }
3749:
3750: second = p++;
1.110 schwarze 3751: if (*second == '\\') {
1.42 schwarze 3752: esc = mandoc_escape(&p, NULL, NULL);
1.110 schwarze 3753: if (esc == ESCAPE_ERROR) {
1.222 schwarze 3754: mandoc_msg(MANDOCERR_ESC_BAD, ln,
3755: (int)(p - buf->buf), "%s", second);
1.149 schwarze 3756: return ROFF_IGN;
1.42 schwarze 3757: }
3758: ssz = (size_t)(p - second);
1.110 schwarze 3759: } else if (*second == '\0') {
1.222 schwarze 3760: mandoc_msg(MANDOCERR_TR_ODD, ln,
3761: (int)(first - buf->buf), "tr %s", first);
1.42 schwarze 3762: second = " ";
3763: p--;
3764: }
3765:
3766: if (fsz > 1) {
1.80 schwarze 3767: roff_setstrn(&r->xmbtab, first, fsz,
3768: second, ssz, 0);
1.42 schwarze 3769: continue;
3770: }
3771:
1.110 schwarze 3772: if (r->xtab == NULL)
1.80 schwarze 3773: r->xtab = mandoc_calloc(128,
3774: sizeof(struct roffstr));
1.42 schwarze 3775:
3776: free(r->xtab[(int)*first].p);
3777: r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
3778: r->xtab[(int)*first].sz = ssz;
3779: }
3780:
1.149 schwarze 3781: return ROFF_IGN;
1.42 schwarze 3782: }
3783:
1.211 schwarze 3784: /*
3785: * Implementation of the .return request.
3786: * There is no need to call roff_userret() from here.
3787: * The read module will call that after rewinding the reader stack
3788: * to the place from where the current macro was called.
3789: */
1.212 schwarze 3790: static int
1.211 schwarze 3791: roff_return(ROFF_ARGS)
3792: {
3793: if (r->mstackpos >= 0)
1.212 schwarze 3794: return ROFF_IGN | ROFF_USERRET;
1.211 schwarze 3795:
1.222 schwarze 3796: mandoc_msg(MANDOCERR_REQ_NOMAC, ln, ppos, "return");
1.211 schwarze 3797: return ROFF_IGN;
3798: }
3799:
1.212 schwarze 3800: static int
1.178 schwarze 3801: roff_rn(ROFF_ARGS)
3802: {
3803: const char *value;
3804: char *oldn, *newn, *end;
3805: size_t oldsz, newsz;
1.187 schwarze 3806: int deftype;
1.178 schwarze 3807:
3808: oldn = newn = buf->buf + pos;
3809: if (*oldn == '\0')
3810: return ROFF_IGN;
3811:
3812: oldsz = roff_getname(r, &newn, ln, pos);
1.235 schwarze 3813: if (oldn[oldsz] == '\\' || oldn[oldsz] == '\t' || *newn == '\0')
1.178 schwarze 3814: return ROFF_IGN;
3815:
3816: end = newn;
3817: newsz = roff_getname(r, &end, ln, newn - buf->buf);
3818: if (newsz == 0)
3819: return ROFF_IGN;
3820:
1.187 schwarze 3821: deftype = ROFFDEF_ANY;
3822: value = roff_getstrn(r, oldn, oldsz, &deftype);
3823: switch (deftype) {
3824: case ROFFDEF_USER:
1.178 schwarze 3825: roff_setstrn(&r->strtab, newn, newsz, value, strlen(value), 0);
3826: roff_setstrn(&r->strtab, oldn, oldsz, NULL, 0, 0);
3827: roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
1.187 schwarze 3828: break;
3829: case ROFFDEF_PRE:
3830: roff_setstrn(&r->strtab, newn, newsz, value, strlen(value), 0);
3831: roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
3832: break;
3833: case ROFFDEF_REN:
1.178 schwarze 3834: roff_setstrn(&r->rentab, newn, newsz, value, strlen(value), 0);
3835: roff_setstrn(&r->rentab, oldn, oldsz, NULL, 0, 0);
1.187 schwarze 3836: roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
3837: break;
3838: case ROFFDEF_STD:
1.178 schwarze 3839: roff_setstrn(&r->rentab, newn, newsz, oldn, oldsz, 0);
1.187 schwarze 3840: roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
3841: break;
3842: default:
3843: roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
3844: roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
3845: break;
3846: }
1.178 schwarze 3847: return ROFF_IGN;
3848: }
3849:
1.212 schwarze 3850: static int
1.211 schwarze 3851: roff_shift(ROFF_ARGS)
3852: {
3853: struct mctx *ctx;
1.254 schwarze 3854: int argpos, levels, i;
1.211 schwarze 3855:
1.254 schwarze 3856: argpos = pos;
1.211 schwarze 3857: levels = 1;
3858: if (buf->buf[pos] != '\0' &&
3859: roff_evalnum(r, ln, buf->buf, &pos, &levels, 0) == 0) {
1.222 schwarze 3860: mandoc_msg(MANDOCERR_CE_NONUM,
1.211 schwarze 3861: ln, pos, "shift %s", buf->buf + pos);
3862: levels = 1;
3863: }
3864: if (r->mstackpos < 0) {
1.222 schwarze 3865: mandoc_msg(MANDOCERR_REQ_NOMAC, ln, ppos, "shift");
1.211 schwarze 3866: return ROFF_IGN;
3867: }
3868: ctx = r->mstack + r->mstackpos;
3869: if (levels > ctx->argc) {
1.222 schwarze 3870: mandoc_msg(MANDOCERR_SHIFT,
1.254 schwarze 3871: ln, argpos, "%d, but max is %d", levels, ctx->argc);
1.211 schwarze 3872: levels = ctx->argc;
1.254 schwarze 3873: }
3874: if (levels < 0) {
3875: mandoc_msg(MANDOCERR_ARG_NEG, ln, argpos, "shift %d", levels);
3876: levels = 0;
1.211 schwarze 3877: }
3878: if (levels == 0)
3879: return ROFF_IGN;
3880: for (i = 0; i < levels; i++)
3881: free(ctx->argv[i]);
3882: ctx->argc -= levels;
3883: for (i = 0; i < ctx->argc; i++)
3884: ctx->argv[i] = ctx->argv[i + levels];
3885: return ROFF_IGN;
3886: }
3887:
1.212 schwarze 3888: static int
1.14 schwarze 3889: roff_so(ROFF_ARGS)
3890: {
1.121 schwarze 3891: char *name, *cp;
1.15 schwarze 3892:
1.110 schwarze 3893: name = buf->buf + pos;
1.222 schwarze 3894: mandoc_msg(MANDOCERR_SO, ln, ppos, "so %s", name);
1.14 schwarze 3895:
1.22 schwarze 3896: /*
3897: * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3898: * opening anything that's not in our cwd or anything beneath
3899: * it. Thus, explicitly disallow traversing up the file-system
3900: * or using absolute paths.
3901: */
3902:
1.110 schwarze 3903: if (*name == '/' || strstr(name, "../") || strstr(name, "/..")) {
1.222 schwarze 3904: mandoc_msg(MANDOCERR_SO_PATH, ln, ppos, ".so %s", name);
1.121 schwarze 3905: buf->sz = mandoc_asprintf(&cp,
3906: ".sp\nSee the file %s.\n.sp", name) + 1;
3907: free(buf->buf);
3908: buf->buf = cp;
3909: *offs = 0;
1.149 schwarze 3910: return ROFF_REPARSE;
1.14 schwarze 3911: }
3912:
3913: *offs = pos;
1.149 schwarze 3914: return ROFF_SO;
1.7 schwarze 3915: }
3916:
1.138 schwarze 3917: /* --- user defined strings and macros ------------------------------------ */
3918:
1.212 schwarze 3919: static int
1.16 schwarze 3920: roff_userdef(ROFF_ARGS)
1.12 schwarze 3921: {
1.211 schwarze 3922: struct mctx *ctx;
3923: char *arg, *ap, *dst, *src;
3924: size_t sz;
3925:
1.237 schwarze 3926: /* If the macro is empty, ignore it altogether. */
3927:
3928: if (*r->current_string == '\0')
3929: return ROFF_IGN;
3930:
1.211 schwarze 3931: /* Initialize a new macro stack context. */
3932:
3933: if (++r->mstackpos == r->mstacksz) {
3934: r->mstack = mandoc_recallocarray(r->mstack,
3935: r->mstacksz, r->mstacksz + 8, sizeof(*r->mstack));
3936: r->mstacksz += 8;
3937: }
3938: ctx = r->mstack + r->mstackpos;
3939: ctx->argc = 0;
3940:
3941: /*
3942: * Collect pointers to macro argument strings,
3943: * NUL-terminating them and escaping quotes.
3944: */
3945:
3946: src = buf->buf + pos;
3947: while (*src != '\0') {
3948: if (ctx->argc == ctx->argsz) {
3949: ctx->argsz += 8;
3950: ctx->argv = mandoc_reallocarray(ctx->argv,
3951: ctx->argsz, sizeof(*ctx->argv));
3952: }
1.227 schwarze 3953: arg = roff_getarg(r, &src, ln, &pos);
1.211 schwarze 3954: sz = 1; /* For the terminating NUL. */
3955: for (ap = arg; *ap != '\0'; ap++)
3956: sz += *ap == '"' ? 4 : 1;
3957: ctx->argv[ctx->argc++] = dst = mandoc_malloc(sz);
3958: for (ap = arg; *ap != '\0'; ap++) {
3959: if (*ap == '"') {
3960: memcpy(dst, "\\(dq", 4);
3961: dst += 4;
3962: } else
3963: *dst++ = *ap;
1.16 schwarze 3964: }
1.211 schwarze 3965: *dst = '\0';
1.227 schwarze 3966: free(arg);
1.209 schwarze 3967: }
3968:
1.211 schwarze 3969: /* Replace the macro invocation by the macro definition. */
1.135 schwarze 3970:
1.110 schwarze 3971: free(buf->buf);
1.211 schwarze 3972: buf->buf = mandoc_strdup(r->current_string);
3973: buf->sz = strlen(buf->buf) + 1;
1.120 schwarze 3974: *offs = 0;
1.16 schwarze 3975:
1.237 schwarze 3976: return buf->buf[buf->sz - 2] == '\n' ?
1.212 schwarze 3977: ROFF_REPARSE | ROFF_USERCALL : ROFF_IGN | ROFF_APPEND;
1.12 schwarze 3978: }
1.28 schwarze 3979:
1.178 schwarze 3980: /*
3981: * Calling a high-level macro that was renamed with .rn.
3982: * r->current_string has already been set up by roff_parse().
3983: */
1.212 schwarze 3984: static int
1.178 schwarze 3985: roff_renamed(ROFF_ARGS)
3986: {
3987: char *nbuf;
3988:
1.187 schwarze 3989: buf->sz = mandoc_asprintf(&nbuf, ".%s%s%s", r->current_string,
3990: buf->buf[pos] == '\0' ? "" : " ", buf->buf + pos) + 1;
1.178 schwarze 3991: free(buf->buf);
3992: buf->buf = nbuf;
1.201 schwarze 3993: *offs = 0;
1.178 schwarze 3994: return ROFF_CONT;
3995: }
3996:
1.234 schwarze 3997: /*
3998: * Measure the length in bytes of the roff identifier at *cpp
3999: * and advance the pointer to the next word.
4000: */
1.85 schwarze 4001: static size_t
1.28 schwarze 4002: roff_getname(struct roff *r, char **cpp, int ln, int pos)
4003: {
4004: char *name, *cp;
1.85 schwarze 4005: size_t namesz;
1.28 schwarze 4006:
4007: name = *cpp;
1.234 schwarze 4008: if (*name == '\0')
1.149 schwarze 4009: return 0;
1.28 schwarze 4010:
1.234 schwarze 4011: /* Advance cp to the byte after the end of the name. */
4012:
1.85 schwarze 4013: for (cp = name; 1; cp++) {
1.234 schwarze 4014: namesz = cp - name;
1.235 schwarze 4015: if (*cp == '\0')
1.85 schwarze 4016: break;
1.235 schwarze 4017: if (*cp == ' ' || *cp == '\t') {
4018: cp++;
4019: break;
4020: }
1.234 schwarze 4021: if (*cp != '\\')
1.28 schwarze 4022: continue;
1.234 schwarze 4023: if (cp[1] == '{' || cp[1] == '}')
1.88 schwarze 4024: break;
1.234 schwarze 4025: if (*++cp == '\\')
1.28 schwarze 4026: continue;
1.222 schwarze 4027: mandoc_msg(MANDOCERR_NAMESC, ln, pos,
1.97 schwarze 4028: "%.*s", (int)(cp - name + 1), name);
1.85 schwarze 4029: mandoc_escape((const char **)&cp, NULL, NULL);
4030: break;
1.28 schwarze 4031: }
4032:
4033: /* Read past spaces. */
1.234 schwarze 4034:
4035: while (*cp == ' ')
1.28 schwarze 4036: cp++;
4037:
4038: *cpp = cp;
1.149 schwarze 4039: return namesz;
1.28 schwarze 4040: }
4041:
1.16 schwarze 4042: /*
4043: * Store *string into the user-defined string called *name.
4044: * To clear an existing entry, call with (*r, *name, NULL, 0).
1.66 schwarze 4045: * append == 0: replace mode
4046: * append == 1: single-line append mode
4047: * append == 2: multiline append mode, append '\n' after each call
1.16 schwarze 4048: */
1.8 schwarze 4049: static void
1.16 schwarze 4050: roff_setstr(struct roff *r, const char *name, const char *string,
1.66 schwarze 4051: int append)
1.7 schwarze 4052: {
1.187 schwarze 4053: size_t namesz;
1.42 schwarze 4054:
1.187 schwarze 4055: namesz = strlen(name);
4056: roff_setstrn(&r->strtab, name, namesz, string,
1.80 schwarze 4057: string ? strlen(string) : 0, append);
1.187 schwarze 4058: roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.42 schwarze 4059: }
4060:
4061: static void
4062: roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1.66 schwarze 4063: const char *string, size_t stringsz, int append)
1.42 schwarze 4064: {
4065: struct roffkv *n;
4066: char *c;
4067: int i;
4068: size_t oldch, newch;
1.7 schwarze 4069:
1.16 schwarze 4070: /* Search for an existing string with the same name. */
1.42 schwarze 4071: n = *r;
4072:
1.84 schwarze 4073: while (n && (namesz != n->key.sz ||
4074: strncmp(n->key.p, name, namesz)))
1.7 schwarze 4075: n = n->next;
1.8 schwarze 4076:
4077: if (NULL == n) {
1.16 schwarze 4078: /* Create a new string table entry. */
1.42 schwarze 4079: n = mandoc_malloc(sizeof(struct roffkv));
4080: n->key.p = mandoc_strndup(name, namesz);
4081: n->key.sz = namesz;
4082: n->val.p = NULL;
4083: n->val.sz = 0;
4084: n->next = *r;
4085: *r = n;
1.66 schwarze 4086: } else if (0 == append) {
1.42 schwarze 4087: free(n->val.p);
4088: n->val.p = NULL;
4089: n->val.sz = 0;
1.16 schwarze 4090: }
4091:
4092: if (NULL == string)
4093: return;
4094:
4095: /*
4096: * One additional byte for the '\n' in multiline mode,
4097: * and one for the terminating '\0'.
4098: */
1.66 schwarze 4099: newch = stringsz + (1 < append ? 2u : 1u);
1.42 schwarze 4100:
4101: if (NULL == n->val.p) {
4102: n->val.p = mandoc_malloc(newch);
4103: *n->val.p = '\0';
1.16 schwarze 4104: oldch = 0;
4105: } else {
1.42 schwarze 4106: oldch = n->val.sz;
4107: n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1.16 schwarze 4108: }
4109:
4110: /* Skip existing content in the destination buffer. */
1.42 schwarze 4111: c = n->val.p + (int)oldch;
1.16 schwarze 4112:
4113: /* Append new content to the destination buffer. */
1.42 schwarze 4114: i = 0;
4115: while (i < (int)stringsz) {
1.16 schwarze 4116: /*
4117: * Rudimentary roff copy mode:
4118: * Handle escaped backslashes.
4119: */
1.42 schwarze 4120: if ('\\' == string[i] && '\\' == string[i + 1])
4121: i++;
4122: *c++ = string[i++];
1.16 schwarze 4123: }
1.8 schwarze 4124:
1.16 schwarze 4125: /* Append terminating bytes. */
1.66 schwarze 4126: if (1 < append)
1.16 schwarze 4127: *c++ = '\n';
1.42 schwarze 4128:
1.16 schwarze 4129: *c = '\0';
1.42 schwarze 4130: n->val.sz = (int)(c - n->val.p);
1.7 schwarze 4131: }
4132:
1.8 schwarze 4133: static const char *
1.197 schwarze 4134: roff_getstrn(struct roff *r, const char *name, size_t len,
1.187 schwarze 4135: int *deftype)
1.7 schwarze 4136: {
1.187 schwarze 4137: const struct roffkv *n;
1.197 schwarze 4138: int found, i;
1.187 schwarze 4139: enum roff_tok tok;
4140:
1.197 schwarze 4141: found = 0;
4142: for (n = r->strtab; n != NULL; n = n->next) {
4143: if (strncmp(name, n->key.p, len) != 0 ||
4144: n->key.p[len] != '\0' || n->val.p == NULL)
4145: continue;
4146: if (*deftype & ROFFDEF_USER) {
4147: *deftype = ROFFDEF_USER;
4148: return n->val.p;
4149: } else {
4150: found = 1;
4151: break;
4152: }
4153: }
4154: for (n = r->rentab; n != NULL; n = n->next) {
4155: if (strncmp(name, n->key.p, len) != 0 ||
4156: n->key.p[len] != '\0' || n->val.p == NULL)
4157: continue;
4158: if (*deftype & ROFFDEF_REN) {
4159: *deftype = ROFFDEF_REN;
4160: return n->val.p;
4161: } else {
4162: found = 1;
4163: break;
1.187 schwarze 4164: }
4165: }
1.197 schwarze 4166: for (i = 0; i < PREDEFS_MAX; i++) {
4167: if (strncmp(name, predefs[i].name, len) != 0 ||
4168: predefs[i].name[len] != '\0')
4169: continue;
4170: if (*deftype & ROFFDEF_PRE) {
4171: *deftype = ROFFDEF_PRE;
4172: return predefs[i].str;
4173: } else {
4174: found = 1;
4175: break;
1.187 schwarze 4176: }
4177: }
1.228 schwarze 4178: if (r->man->meta.macroset != MACROSET_MAN) {
1.197 schwarze 4179: for (tok = MDOC_Dd; tok < MDOC_MAX; tok++) {
4180: if (strncmp(name, roff_name[tok], len) != 0 ||
4181: roff_name[tok][len] != '\0')
4182: continue;
4183: if (*deftype & ROFFDEF_STD) {
4184: *deftype = ROFFDEF_STD;
4185: return NULL;
4186: } else {
4187: found = 1;
4188: break;
1.187 schwarze 4189: }
4190: }
4191: }
1.228 schwarze 4192: if (r->man->meta.macroset != MACROSET_MDOC) {
1.197 schwarze 4193: for (tok = MAN_TH; tok < MAN_MAX; tok++) {
4194: if (strncmp(name, roff_name[tok], len) != 0 ||
4195: roff_name[tok][len] != '\0')
4196: continue;
4197: if (*deftype & ROFFDEF_STD) {
4198: *deftype = ROFFDEF_STD;
4199: return NULL;
4200: } else {
4201: found = 1;
4202: break;
1.187 schwarze 4203: }
4204: }
1.197 schwarze 4205: }
4206:
4207: if (found == 0 && *deftype != ROFFDEF_ANY) {
4208: if (*deftype & ROFFDEF_REN) {
4209: /*
4210: * This might still be a request,
4211: * so do not treat it as undefined yet.
4212: */
4213: *deftype = ROFFDEF_UNDEF;
4214: return NULL;
1.187 schwarze 4215: }
1.197 schwarze 4216:
4217: /* Using an undefined string defines it to be empty. */
4218:
4219: roff_setstrn(&r->strtab, name, len, "", 0, 0);
4220: roff_setstrn(&r->rentab, name, len, NULL, 0, 0);
1.187 schwarze 4221: }
1.197 schwarze 4222:
1.187 schwarze 4223: *deftype = 0;
1.149 schwarze 4224: return NULL;
1.7 schwarze 4225: }
4226:
1.8 schwarze 4227: static void
1.42 schwarze 4228: roff_freestr(struct roffkv *r)
1.7 schwarze 4229: {
1.42 schwarze 4230: struct roffkv *n, *nn;
1.7 schwarze 4231:
1.42 schwarze 4232: for (n = r; n; n = nn) {
4233: free(n->key.p);
4234: free(n->val.p);
1.7 schwarze 4235: nn = n->next;
4236: free(n);
4237: }
1.27 schwarze 4238: }
1.138 schwarze 4239:
4240: /* --- accessors and utility functions ------------------------------------ */
1.42 schwarze 4241:
4242: /*
4243: * Duplicate an input string, making the appropriate character
4244: * conversations (as stipulated by `tr') along the way.
4245: * Returns a heap-allocated string with all the replacements made.
4246: */
4247: char *
4248: roff_strdup(const struct roff *r, const char *p)
4249: {
4250: const struct roffkv *cp;
4251: char *res;
4252: const char *pp;
4253: size_t ssz, sz;
4254: enum mandoc_esc esc;
4255:
4256: if (NULL == r->xmbtab && NULL == r->xtab)
1.149 schwarze 4257: return mandoc_strdup(p);
1.42 schwarze 4258: else if ('\0' == *p)
1.149 schwarze 4259: return mandoc_strdup("");
1.42 schwarze 4260:
4261: /*
4262: * Step through each character looking for term matches
4263: * (remember that a `tr' can be invoked with an escape, which is
4264: * a glyph but the escape is multi-character).
4265: * We only do this if the character hash has been initialised
4266: * and the string is >0 length.
4267: */
4268:
4269: res = NULL;
4270: ssz = 0;
4271:
4272: while ('\0' != *p) {
1.161 schwarze 4273: assert((unsigned int)*p < 128);
4274: if ('\\' != *p && r->xtab && r->xtab[(unsigned int)*p].p) {
1.42 schwarze 4275: sz = r->xtab[(int)*p].sz;
4276: res = mandoc_realloc(res, ssz + sz + 1);
4277: memcpy(res + ssz, r->xtab[(int)*p].p, sz);
4278: ssz += sz;
4279: p++;
4280: continue;
4281: } else if ('\\' != *p) {
4282: res = mandoc_realloc(res, ssz + 2);
4283: res[ssz++] = *p++;
4284: continue;
4285: }
4286:
4287: /* Search for term matches. */
4288: for (cp = r->xmbtab; cp; cp = cp->next)
4289: if (0 == strncmp(p, cp->key.p, cp->key.sz))
4290: break;
4291:
4292: if (NULL != cp) {
4293: /*
4294: * A match has been found.
4295: * Append the match to the array and move
4296: * forward by its keysize.
4297: */
1.80 schwarze 4298: res = mandoc_realloc(res,
4299: ssz + cp->val.sz + 1);
1.42 schwarze 4300: memcpy(res + ssz, cp->val.p, cp->val.sz);
4301: ssz += cp->val.sz;
4302: p += (int)cp->key.sz;
4303: continue;
4304: }
4305:
4306: /*
4307: * Handle escapes carefully: we need to copy
4308: * over just the escape itself, or else we might
4309: * do replacements within the escape itself.
4310: * Make sure to pass along the bogus string.
4311: */
4312: pp = p++;
4313: esc = mandoc_escape(&p, NULL, NULL);
4314: if (ESCAPE_ERROR == esc) {
4315: sz = strlen(pp);
4316: res = mandoc_realloc(res, ssz + sz + 1);
4317: memcpy(res + ssz, pp, sz);
4318: break;
4319: }
1.80 schwarze 4320: /*
4321: * We bail out on bad escapes.
1.42 schwarze 4322: * No need to warn: we already did so when
1.227 schwarze 4323: * roff_expand() was called.
1.42 schwarze 4324: */
4325: sz = (int)(p - pp);
4326: res = mandoc_realloc(res, ssz + sz + 1);
4327: memcpy(res + ssz, pp, sz);
4328: ssz += sz;
4329: }
4330:
4331: res[(int)ssz] = '\0';
1.149 schwarze 4332: return res;
1.99 schwarze 4333: }
4334:
4335: int
4336: roff_getformat(const struct roff *r)
4337: {
4338:
1.149 schwarze 4339: return r->format;
1.48 schwarze 4340: }
4341:
4342: /*
1.80 schwarze 4343: * Find out whether a line is a macro line or not.
1.48 schwarze 4344: * If it is, adjust the current position and return one; if it isn't,
4345: * return zero and don't change the current position.
4346: * If the control character has been set with `.cc', then let that grain
4347: * precedence.
4348: * This is slighly contrary to groff, where using the non-breaking
4349: * control character when `cc' has been invoked will cause the
4350: * non-breaking macro contents to be printed verbatim.
4351: */
4352: int
4353: roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
4354: {
4355: int pos;
4356:
4357: pos = *ppos;
4358:
1.175 schwarze 4359: if (r->control != '\0' && cp[pos] == r->control)
1.48 schwarze 4360: pos++;
1.175 schwarze 4361: else if (r->control != '\0')
1.149 schwarze 4362: return 0;
1.48 schwarze 4363: else if ('\\' == cp[pos] && '.' == cp[pos + 1])
4364: pos += 2;
4365: else if ('.' == cp[pos] || '\'' == cp[pos])
4366: pos++;
4367: else
1.149 schwarze 4368: return 0;
1.48 schwarze 4369:
4370: while (' ' == cp[pos] || '\t' == cp[pos])
4371: pos++;
4372:
4373: *ppos = pos;
1.149 schwarze 4374: return 1;
1.1 schwarze 4375: }