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