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