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