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