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