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