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