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