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