Annotation of mandoc/roff.c, Revision 1.269
1.269 ! schwarze 1: /* $Id: roff.c,v 1.268 2015/04/19 14:57:38 schwarze Exp $ */
1.1 kristaps 2: /*
1.267 schwarze 3: * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.247 schwarze 4: * Copyright (c) 2010-2015 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.178 schwarze 25: #include <stdio.h>
1.1 kristaps 26: #include <stdlib.h>
1.67 kristaps 27: #include <string.h>
1.1 kristaps 28:
1.67 kristaps 29: #include "mandoc.h"
1.201 schwarze 30: #include "mandoc_aux.h"
1.265 schwarze 31: #include "roff.h"
1.226 schwarze 32: #include "libmandoc.h"
1.266 schwarze 33: #include "roff_int.h"
1.109 kristaps 34: #include "libroff.h"
1.33 kristaps 35:
1.141 kristaps 36: /* Maximum number of nested if-else conditionals. */
1.82 kristaps 37: #define RSTACK_MAX 128
38:
1.170 schwarze 39: /* Maximum number of string expansions per line, to break infinite loops. */
40: #define EXPAND_LIMIT 1000
41:
1.266 schwarze 42: /* --- data types --------------------------------------------------------- */
43:
1.67 kristaps 44: enum rofft {
1.251 schwarze 45: ROFF_ab,
1.103 kristaps 46: ROFF_ad,
1.251 schwarze 47: ROFF_af,
48: ROFF_aln,
49: ROFF_als,
1.80 kristaps 50: ROFF_am,
1.251 schwarze 51: ROFF_am1,
1.80 kristaps 52: ROFF_ami,
1.251 schwarze 53: ROFF_ami1,
1.193 schwarze 54: ROFF_as,
1.251 schwarze 55: ROFF_as1,
56: ROFF_asciify,
57: ROFF_backtrace,
58: ROFF_bd,
59: ROFF_bleedat,
60: ROFF_blm,
61: ROFF_box,
62: ROFF_boxa,
63: ROFF_bp,
64: ROFF_BP,
65: /* MAN_br, MDOC_br */
66: ROFF_break,
67: ROFF_breakchar,
68: ROFF_brnl,
69: ROFF_brp,
70: ROFF_brpnl,
71: ROFF_c2,
1.174 kristaps 72: ROFF_cc,
1.194 schwarze 73: ROFF_ce,
1.251 schwarze 74: ROFF_cf,
75: ROFF_cflags,
76: ROFF_ch,
77: ROFF_char,
78: ROFF_chop,
79: ROFF_class,
80: ROFF_close,
81: ROFF_CL,
82: ROFF_color,
83: ROFF_composite,
84: ROFF_continue,
85: ROFF_cp,
86: ROFF_cropat,
87: ROFF_cs,
88: ROFF_cu,
89: ROFF_da,
90: ROFF_dch,
91: ROFF_Dd,
1.80 kristaps 92: ROFF_de,
1.251 schwarze 93: ROFF_de1,
94: ROFF_defcolor,
1.80 kristaps 95: ROFF_dei,
1.251 schwarze 96: ROFF_dei1,
97: ROFF_device,
98: ROFF_devicem,
99: ROFF_di,
100: ROFF_do,
1.83 schwarze 101: ROFF_ds,
1.251 schwarze 102: ROFF_ds1,
103: ROFF_dwh,
104: ROFF_dt,
105: ROFF_ec,
106: ROFF_ecr,
107: ROFF_ecs,
1.82 kristaps 108: ROFF_el,
1.251 schwarze 109: ROFF_em,
110: ROFF_EN,
111: ROFF_eo,
112: ROFF_EP,
113: ROFF_EQ,
114: ROFF_errprint,
115: ROFF_ev,
116: ROFF_evc,
117: ROFF_ex,
118: ROFF_fallback,
1.185 schwarze 119: ROFF_fam,
1.251 schwarze 120: ROFF_fc,
121: ROFF_fchar,
122: ROFF_fcolor,
123: ROFF_fdeferlig,
124: ROFF_feature,
125: /* MAN_fi; ignored in mdoc(7) */
126: ROFF_fkern,
127: ROFF_fl,
128: ROFF_flig,
129: ROFF_fp,
130: ROFF_fps,
131: ROFF_fschar,
132: ROFF_fspacewidth,
133: ROFF_fspecial,
134: /* MAN_ft; ignored in mdoc(7) */
135: ROFF_ftr,
136: ROFF_fzoom,
137: ROFF_gcolor,
138: ROFF_hc,
139: ROFF_hcode,
140: ROFF_hidechar,
141: ROFF_hla,
142: ROFF_hlm,
143: ROFF_hpf,
144: ROFF_hpfa,
145: ROFF_hpfcode,
1.186 schwarze 146: ROFF_hw,
1.103 kristaps 147: ROFF_hy,
1.251 schwarze 148: ROFF_hylang,
149: ROFF_hylen,
150: ROFF_hym,
151: ROFF_hypp,
152: ROFF_hys,
1.82 kristaps 153: ROFF_ie,
1.75 kristaps 154: ROFF_if,
1.76 kristaps 155: ROFF_ig,
1.251 schwarze 156: /* MAN_in; ignored in mdoc(7) */
157: ROFF_index,
1.123 schwarze 158: ROFF_it,
1.251 schwarze 159: ROFF_itc,
160: ROFF_IX,
161: ROFF_kern,
162: ROFF_kernafter,
163: ROFF_kernbefore,
164: ROFF_kernpair,
165: ROFF_lc,
166: ROFF_lc_ctype,
167: ROFF_lds,
168: ROFF_length,
169: ROFF_letadj,
170: ROFF_lf,
171: ROFF_lg,
172: ROFF_lhang,
173: ROFF_linetabs,
174: /* MAN_ll, MDOC_ll */
175: ROFF_lnr,
176: ROFF_lnrf,
177: ROFF_lpfx,
178: ROFF_ls,
179: ROFF_lsm,
180: ROFF_lt,
181: ROFF_mc,
182: ROFF_mediasize,
183: ROFF_minss,
184: ROFF_mk,
185: ROFF_mso,
1.256 schwarze 186: ROFF_na,
1.103 kristaps 187: ROFF_ne,
1.251 schwarze 188: /* MAN_nf; ignored in mdoc(7) */
1.103 kristaps 189: ROFF_nh,
1.251 schwarze 190: ROFF_nhychar,
191: ROFF_nm,
192: ROFF_nn,
193: ROFF_nop,
1.104 kristaps 194: ROFF_nr,
1.251 schwarze 195: ROFF_nrf,
196: ROFF_nroff,
1.124 schwarze 197: ROFF_ns,
1.251 schwarze 198: ROFF_nx,
199: ROFF_open,
200: ROFF_opena,
201: ROFF_os,
202: ROFF_output,
203: ROFF_padj,
204: ROFF_papersize,
205: ROFF_pc,
206: ROFF_pev,
207: ROFF_pi,
208: ROFF_PI,
1.229 schwarze 209: ROFF_pl,
1.251 schwarze 210: ROFF_pm,
211: ROFF_pn,
212: ROFF_pnr,
213: ROFF_po,
1.124 schwarze 214: ROFF_ps,
1.251 schwarze 215: ROFF_psbb,
216: ROFF_pshape,
217: ROFF_pso,
218: ROFF_ptr,
219: ROFF_pvs,
220: ROFF_rchar,
221: ROFF_rd,
222: ROFF_recursionlimit,
223: ROFF_return,
224: ROFF_rfschar,
225: ROFF_rhang,
226: ROFF_rj,
1.83 schwarze 227: ROFF_rm,
1.251 schwarze 228: ROFF_rn,
229: ROFF_rnn,
1.203 schwarze 230: ROFF_rr,
1.251 schwarze 231: ROFF_rs,
232: ROFF_rt,
233: ROFF_schar,
234: ROFF_sentchar,
235: ROFF_shc,
236: ROFF_shift,
237: ROFF_sizes,
1.105 kristaps 238: ROFF_so,
1.251 schwarze 239: /* MAN_sp, MDOC_sp */
240: ROFF_spacewidth,
241: ROFF_special,
242: ROFF_spreadwarn,
243: ROFF_ss,
244: ROFF_sty,
245: ROFF_substring,
246: ROFF_sv,
247: ROFF_sy,
248: ROFF_T_,
1.124 schwarze 249: ROFF_ta,
1.251 schwarze 250: ROFF_tc,
251: ROFF_TE,
252: ROFF_TH,
253: ROFF_ti,
254: ROFF_tkf,
255: ROFF_tl,
256: ROFF_tm,
257: ROFF_tm1,
258: ROFF_tmc,
1.83 schwarze 259: ROFF_tr,
1.251 schwarze 260: ROFF_track,
261: ROFF_transchar,
262: ROFF_trf,
263: ROFF_trimat,
264: ROFF_trin,
265: ROFF_trnt,
266: ROFF_troff,
1.109 kristaps 267: ROFF_TS,
1.251 schwarze 268: ROFF_uf,
269: ROFF_ul,
270: ROFF_unformat,
271: ROFF_unwatch,
272: ROFF_unwatchn,
273: ROFF_vpt,
274: ROFF_vs,
275: ROFF_warn,
276: ROFF_warnscale,
277: ROFF_watch,
278: ROFF_watchlength,
279: ROFF_watchn,
280: ROFF_wh,
281: ROFF_while,
282: ROFF_write,
283: ROFF_writec,
284: ROFF_writem,
285: ROFF_xflag,
1.76 kristaps 286: ROFF_cblock,
1.106 kristaps 287: ROFF_USERDEF,
1.67 kristaps 288: ROFF_MAX
289: };
290:
1.147 kristaps 291: /*
1.167 kristaps 292: * An incredibly-simple string buffer.
293: */
1.94 kristaps 294: struct roffstr {
1.167 kristaps 295: char *p; /* nil-terminated buffer */
296: size_t sz; /* saved strlen(p) */
1.166 kristaps 297: };
298:
299: /*
1.167 kristaps 300: * A key-value roffstr pair as part of a singly-linked list.
1.166 kristaps 301: */
302: struct roffkv {
303: struct roffstr key;
304: struct roffstr val;
305: struct roffkv *next; /* next in list */
1.94 kristaps 306: };
307:
1.180 schwarze 308: /*
309: * A single number register as part of a singly-linked list.
310: */
311: struct roffreg {
312: struct roffstr key;
1.181 schwarze 313: int val;
1.180 schwarze 314: struct roffreg *next;
315: };
316:
1.67 kristaps 317: struct roff {
1.128 kristaps 318: struct mparse *parse; /* parse point */
1.237 schwarze 319: const struct mchars *mchars; /* character table */
1.67 kristaps 320: struct roffnode *last; /* leaf of stack */
1.223 schwarze 321: int *rstack; /* stack of inverted `ie' values */
1.180 schwarze 322: struct roffreg *regtab; /* number registers */
1.166 kristaps 323: struct roffkv *strtab; /* user-defined strings & macros */
1.167 kristaps 324: struct roffkv *xmbtab; /* multi-byte trans table (`tr') */
325: struct roffstr *xtab; /* single-byte trans table (`tr') */
1.106 kristaps 326: const char *current_string; /* value of last called user macro */
1.118 kristaps 327: struct tbl_node *first_tbl; /* first table parsed */
328: struct tbl_node *last_tbl; /* last table parsed */
329: struct tbl_node *tbl; /* current table being parsed */
1.125 kristaps 330: struct eqn_node *last_eqn; /* last equation parsed */
331: struct eqn_node *first_eqn; /* first equation parsed */
332: struct eqn_node *eqn; /* current equation being parsed */
1.230 schwarze 333: int eqn_inline; /* current equation is inline */
1.223 schwarze 334: int options; /* parse options */
335: int rstacksz; /* current size limit of rstack */
336: int rstackpos; /* position in rstack */
1.227 schwarze 337: int format; /* current file in mdoc or man format */
1.223 schwarze 338: char control; /* control character */
1.79 kristaps 339: };
340:
1.67 kristaps 341: struct roffnode {
342: enum rofft tok; /* type of node */
343: struct roffnode *parent; /* up one in stack */
344: int line; /* parse line */
345: int col; /* parse col */
1.106 kristaps 346: char *name; /* node name, e.g. macro name */
1.79 kristaps 347: char *end; /* end-rules: custom token */
348: int endspan; /* end-rules: next-line or infty */
1.198 schwarze 349: int rule; /* current evaluation rule */
1.67 kristaps 350: };
351:
352: #define ROFF_ARGS struct roff *r, /* parse ctx */ \
1.72 kristaps 353: enum rofft tok, /* tok of macro */ \
1.238 schwarze 354: struct buf *buf, /* input buffer */ \
1.67 kristaps 355: int ln, /* parse line */ \
1.75 kristaps 356: int ppos, /* original pos in buffer */ \
357: int pos, /* current pos in buffer */ \
1.74 kristaps 358: int *offs /* reset offset of buffer data */
1.67 kristaps 359:
360: typedef enum rofferr (*roffproc)(ROFF_ARGS);
361:
362: struct roffmac {
363: const char *name; /* macro name */
1.79 kristaps 364: roffproc proc; /* process new macro */
365: roffproc text; /* process as child text of macro */
366: roffproc sub; /* process as child of macro */
367: int flags;
368: #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
1.85 kristaps 369: struct roffmac *next;
1.67 kristaps 370: };
371:
1.141 kristaps 372: struct predef {
373: const char *name; /* predefined input name */
374: const char *str; /* replacement symbol */
375: };
376:
377: #define PREDEF(__name, __str) \
378: { (__name), (__str) },
379:
1.266 schwarze 380: /* --- function prototypes ------------------------------------------------ */
381:
1.155 kristaps 382: static enum rofft roffhash_find(const char *, size_t);
383: static void roffhash_init(void);
384: static void roffnode_cleanscope(struct roff *);
385: static void roffnode_pop(struct roff *);
386: static void roffnode_push(struct roff *, enum rofft,
387: const char *, int, int);
1.80 kristaps 388: static enum rofferr roff_block(ROFF_ARGS);
389: static enum rofferr roff_block_text(ROFF_ARGS);
390: static enum rofferr roff_block_sub(ROFF_ARGS);
1.251 schwarze 391: static enum rofferr roff_brp(ROFF_ARGS);
1.80 kristaps 392: static enum rofferr roff_cblock(ROFF_ARGS);
1.174 kristaps 393: static enum rofferr roff_cc(ROFF_ARGS);
1.195 schwarze 394: static void roff_ccond(struct roff *, int, int);
1.82 kristaps 395: static enum rofferr roff_cond(ROFF_ARGS);
396: static enum rofferr roff_cond_text(ROFF_ARGS);
397: static enum rofferr roff_cond_sub(ROFF_ARGS);
1.92 schwarze 398: static enum rofferr roff_ds(ROFF_ARGS);
1.238 schwarze 399: static enum rofferr roff_eqndelim(struct roff *, struct buf *, int);
1.234 kristaps 400: static int roff_evalcond(struct roff *r, int,
401: const char *, int *);
402: static int roff_evalnum(struct roff *, int,
403: const char *, int *, int *, int);
404: static int roff_evalpar(struct roff *, int,
1.261 schwarze 405: const char *, int *, int *, int);
1.198 schwarze 406: static int roff_evalstrcond(const char *, int *);
1.155 kristaps 407: static void roff_free1(struct roff *);
1.180 schwarze 408: static void roff_freereg(struct roffreg *);
1.167 kristaps 409: static void roff_freestr(struct roffkv *);
1.212 schwarze 410: static size_t roff_getname(struct roff *, char **, int, int);
1.261 schwarze 411: static int roff_getnum(const char *, int *, int *, int);
1.184 schwarze 412: static int roff_getop(const char *, int *, char *);
1.181 schwarze 413: static int roff_getregn(const struct roff *,
414: const char *, size_t);
1.192 schwarze 415: static int roff_getregro(const char *name);
1.207 schwarze 416: static const char *roff_getstrn(const struct roff *,
1.94 kristaps 417: const char *, size_t);
1.251 schwarze 418: static enum rofferr roff_insec(ROFF_ARGS);
1.178 schwarze 419: static enum rofferr roff_it(ROFF_ARGS);
1.103 kristaps 420: static enum rofferr roff_line_ignore(ROFF_ARGS);
1.265 schwarze 421: static void roff_man_alloc1(struct roff_man *);
422: static void roff_man_free1(struct roff_man *);
1.89 kristaps 423: static enum rofferr roff_nr(ROFF_ARGS);
1.214 schwarze 424: static enum rofft roff_parse(struct roff *, char *, int *,
425: int, int);
1.238 schwarze 426: static enum rofferr roff_parsetext(struct buf *, int, int *);
427: static enum rofferr roff_res(struct roff *, struct buf *, int, int);
1.122 schwarze 428: static enum rofferr roff_rm(ROFF_ARGS);
1.203 schwarze 429: static enum rofferr roff_rr(ROFF_ARGS);
1.94 kristaps 430: static void roff_setstr(struct roff *,
1.106 kristaps 431: const char *, const char *, int);
1.207 schwarze 432: static void roff_setstrn(struct roffkv **, const char *,
1.164 kristaps 433: size_t, const char *, size_t, int);
1.105 kristaps 434: static enum rofferr roff_so(ROFF_ARGS);
1.164 kristaps 435: static enum rofferr roff_tr(ROFF_ARGS);
1.175 schwarze 436: static enum rofferr roff_Dd(ROFF_ARGS);
437: static enum rofferr roff_TH(ROFF_ARGS);
1.109 kristaps 438: static enum rofferr roff_TE(ROFF_ARGS);
439: static enum rofferr roff_TS(ROFF_ARGS);
1.125 kristaps 440: static enum rofferr roff_EQ(ROFF_ARGS);
441: static enum rofferr roff_EN(ROFF_ARGS);
1.112 kristaps 442: static enum rofferr roff_T_(ROFF_ARGS);
1.251 schwarze 443: static enum rofferr roff_unsupp(ROFF_ARGS);
1.106 kristaps 444: static enum rofferr roff_userdef(ROFF_ARGS);
1.67 kristaps 445:
1.266 schwarze 446: /* --- constant data ------------------------------------------------------ */
447:
1.155 kristaps 448: /* See roffhash_find() */
1.85 kristaps 449:
450: #define ASCII_HI 126
451: #define ASCII_LO 33
452: #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
453:
1.261 schwarze 454: #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
455: #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
456:
1.85 kristaps 457: static struct roffmac *hash[HASHWIDTH];
458:
459: static struct roffmac roffs[ROFF_MAX] = {
1.251 schwarze 460: { "ab", roff_unsupp, NULL, NULL, 0, NULL },
1.103 kristaps 461: { "ad", roff_line_ignore, NULL, NULL, 0, NULL },
1.251 schwarze 462: { "af", roff_line_ignore, NULL, NULL, 0, NULL },
463: { "aln", roff_unsupp, NULL, NULL, 0, NULL },
464: { "als", roff_unsupp, NULL, NULL, 0, NULL },
1.85 kristaps 465: { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.251 schwarze 466: { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.85 kristaps 467: { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.251 schwarze 468: { "ami1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.193 schwarze 469: { "as", roff_ds, NULL, NULL, 0, NULL },
1.251 schwarze 470: { "as1", roff_ds, NULL, NULL, 0, NULL },
471: { "asciify", roff_unsupp, NULL, NULL, 0, NULL },
472: { "backtrace", roff_line_ignore, NULL, NULL, 0, NULL },
473: { "bd", roff_line_ignore, NULL, NULL, 0, NULL },
474: { "bleedat", roff_line_ignore, NULL, NULL, 0, NULL },
475: { "blm", roff_unsupp, NULL, NULL, 0, NULL },
476: { "box", roff_unsupp, NULL, NULL, 0, NULL },
477: { "boxa", roff_unsupp, NULL, NULL, 0, NULL },
478: { "bp", roff_line_ignore, NULL, NULL, 0, NULL },
479: { "BP", roff_unsupp, NULL, NULL, 0, NULL },
480: { "break", roff_unsupp, NULL, NULL, 0, NULL },
481: { "breakchar", roff_line_ignore, NULL, NULL, 0, NULL },
482: { "brnl", roff_line_ignore, NULL, NULL, 0, NULL },
483: { "brp", roff_brp, NULL, NULL, 0, NULL },
484: { "brpnl", roff_line_ignore, NULL, NULL, 0, NULL },
485: { "c2", roff_unsupp, NULL, NULL, 0, NULL },
1.174 kristaps 486: { "cc", roff_cc, NULL, NULL, 0, NULL },
1.194 schwarze 487: { "ce", roff_line_ignore, NULL, NULL, 0, NULL },
1.251 schwarze 488: { "cf", roff_insec, NULL, NULL, 0, NULL },
489: { "cflags", roff_line_ignore, NULL, NULL, 0, NULL },
490: { "ch", roff_line_ignore, NULL, NULL, 0, NULL },
491: { "char", roff_unsupp, NULL, NULL, 0, NULL },
492: { "chop", roff_unsupp, NULL, NULL, 0, NULL },
493: { "class", roff_line_ignore, NULL, NULL, 0, NULL },
494: { "close", roff_insec, NULL, NULL, 0, NULL },
495: { "CL", roff_unsupp, NULL, NULL, 0, NULL },
496: { "color", roff_line_ignore, NULL, NULL, 0, NULL },
497: { "composite", roff_unsupp, NULL, NULL, 0, NULL },
498: { "continue", roff_unsupp, NULL, NULL, 0, NULL },
499: { "cp", roff_line_ignore, NULL, NULL, 0, NULL },
500: { "cropat", roff_line_ignore, NULL, NULL, 0, NULL },
501: { "cs", roff_line_ignore, NULL, NULL, 0, NULL },
502: { "cu", roff_line_ignore, NULL, NULL, 0, NULL },
503: { "da", roff_unsupp, NULL, NULL, 0, NULL },
504: { "dch", roff_unsupp, NULL, NULL, 0, NULL },
505: { "Dd", roff_Dd, NULL, NULL, 0, NULL },
1.85 kristaps 506: { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.251 schwarze 507: { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
508: { "defcolor", roff_line_ignore, NULL, NULL, 0, NULL },
1.85 kristaps 509: { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.251 schwarze 510: { "dei1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
511: { "device", roff_unsupp, NULL, NULL, 0, NULL },
512: { "devicem", roff_unsupp, NULL, NULL, 0, NULL },
513: { "di", roff_unsupp, NULL, NULL, 0, NULL },
514: { "do", roff_unsupp, NULL, NULL, 0, NULL },
1.92 schwarze 515: { "ds", roff_ds, NULL, NULL, 0, NULL },
1.251 schwarze 516: { "ds1", roff_ds, NULL, NULL, 0, NULL },
517: { "dwh", roff_unsupp, NULL, NULL, 0, NULL },
518: { "dt", roff_unsupp, NULL, NULL, 0, NULL },
519: { "ec", roff_unsupp, NULL, NULL, 0, NULL },
520: { "ecr", roff_unsupp, NULL, NULL, 0, NULL },
521: { "ecs", roff_unsupp, NULL, NULL, 0, NULL },
1.85 kristaps 522: { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
1.251 schwarze 523: { "em", roff_unsupp, NULL, NULL, 0, NULL },
524: { "EN", roff_EN, NULL, NULL, 0, NULL },
525: { "eo", roff_unsupp, NULL, NULL, 0, NULL },
526: { "EP", roff_unsupp, NULL, NULL, 0, NULL },
527: { "EQ", roff_EQ, NULL, NULL, 0, NULL },
528: { "errprint", roff_line_ignore, NULL, NULL, 0, NULL },
529: { "ev", roff_unsupp, NULL, NULL, 0, NULL },
530: { "evc", roff_unsupp, NULL, NULL, 0, NULL },
531: { "ex", roff_unsupp, NULL, NULL, 0, NULL },
532: { "fallback", roff_line_ignore, NULL, NULL, 0, NULL },
1.185 schwarze 533: { "fam", roff_line_ignore, NULL, NULL, 0, NULL },
1.251 schwarze 534: { "fc", roff_unsupp, NULL, NULL, 0, NULL },
535: { "fchar", roff_unsupp, NULL, NULL, 0, NULL },
536: { "fcolor", roff_line_ignore, NULL, NULL, 0, NULL },
537: { "fdeferlig", roff_line_ignore, NULL, NULL, 0, NULL },
538: { "feature", roff_line_ignore, NULL, NULL, 0, NULL },
539: { "fkern", roff_line_ignore, NULL, NULL, 0, NULL },
540: { "fl", roff_line_ignore, NULL, NULL, 0, NULL },
541: { "flig", roff_line_ignore, NULL, NULL, 0, NULL },
542: { "fp", roff_line_ignore, NULL, NULL, 0, NULL },
543: { "fps", roff_line_ignore, NULL, NULL, 0, NULL },
544: { "fschar", roff_unsupp, NULL, NULL, 0, NULL },
545: { "fspacewidth", roff_line_ignore, NULL, NULL, 0, NULL },
546: { "fspecial", roff_line_ignore, NULL, NULL, 0, NULL },
547: { "ftr", roff_line_ignore, NULL, NULL, 0, NULL },
548: { "fzoom", roff_line_ignore, NULL, NULL, 0, NULL },
549: { "gcolor", roff_line_ignore, NULL, NULL, 0, NULL },
550: { "hc", roff_line_ignore, NULL, NULL, 0, NULL },
551: { "hcode", roff_line_ignore, NULL, NULL, 0, NULL },
552: { "hidechar", roff_line_ignore, NULL, NULL, 0, NULL },
553: { "hla", roff_line_ignore, NULL, NULL, 0, NULL },
554: { "hlm", roff_line_ignore, NULL, NULL, 0, NULL },
555: { "hpf", roff_line_ignore, NULL, NULL, 0, NULL },
556: { "hpfa", roff_line_ignore, NULL, NULL, 0, NULL },
557: { "hpfcode", roff_line_ignore, NULL, NULL, 0, NULL },
1.186 schwarze 558: { "hw", roff_line_ignore, NULL, NULL, 0, NULL },
1.103 kristaps 559: { "hy", roff_line_ignore, NULL, NULL, 0, NULL },
1.251 schwarze 560: { "hylang", roff_line_ignore, NULL, NULL, 0, NULL },
561: { "hylen", roff_line_ignore, NULL, NULL, 0, NULL },
562: { "hym", roff_line_ignore, NULL, NULL, 0, NULL },
563: { "hypp", roff_line_ignore, NULL, NULL, 0, NULL },
564: { "hys", roff_line_ignore, NULL, NULL, 0, NULL },
1.85 kristaps 565: { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
566: { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
567: { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.251 schwarze 568: { "index", roff_unsupp, NULL, NULL, 0, NULL },
1.178 schwarze 569: { "it", roff_it, NULL, NULL, 0, NULL },
1.251 schwarze 570: { "itc", roff_unsupp, NULL, NULL, 0, NULL },
571: { "IX", roff_line_ignore, NULL, NULL, 0, NULL },
572: { "kern", roff_line_ignore, NULL, NULL, 0, NULL },
573: { "kernafter", roff_line_ignore, NULL, NULL, 0, NULL },
574: { "kernbefore", roff_line_ignore, NULL, NULL, 0, NULL },
575: { "kernpair", roff_line_ignore, NULL, NULL, 0, NULL },
576: { "lc", roff_unsupp, NULL, NULL, 0, NULL },
577: { "lc_ctype", roff_unsupp, NULL, NULL, 0, NULL },
578: { "lds", roff_unsupp, NULL, NULL, 0, NULL },
579: { "length", roff_unsupp, NULL, NULL, 0, NULL },
580: { "letadj", roff_line_ignore, NULL, NULL, 0, NULL },
581: { "lf", roff_insec, NULL, NULL, 0, NULL },
582: { "lg", roff_line_ignore, NULL, NULL, 0, NULL },
583: { "lhang", roff_line_ignore, NULL, NULL, 0, NULL },
584: { "linetabs", roff_unsupp, NULL, NULL, 0, NULL },
585: { "lnr", roff_unsupp, NULL, NULL, 0, NULL },
586: { "lnrf", roff_unsupp, NULL, NULL, 0, NULL },
587: { "lpfx", roff_unsupp, NULL, NULL, 0, NULL },
588: { "ls", roff_line_ignore, NULL, NULL, 0, NULL },
589: { "lsm", roff_unsupp, NULL, NULL, 0, NULL },
590: { "lt", roff_line_ignore, NULL, NULL, 0, NULL },
591: { "mc", roff_line_ignore, NULL, NULL, 0, NULL },
592: { "mediasize", roff_line_ignore, NULL, NULL, 0, NULL },
593: { "minss", roff_line_ignore, NULL, NULL, 0, NULL },
594: { "mk", roff_line_ignore, NULL, NULL, 0, NULL },
595: { "mso", roff_insec, NULL, NULL, 0, NULL },
1.256 schwarze 596: { "na", roff_line_ignore, NULL, NULL, 0, NULL },
1.103 kristaps 597: { "ne", roff_line_ignore, NULL, NULL, 0, NULL },
598: { "nh", roff_line_ignore, NULL, NULL, 0, NULL },
1.251 schwarze 599: { "nhychar", roff_line_ignore, NULL, NULL, 0, NULL },
600: { "nm", roff_unsupp, NULL, NULL, 0, NULL },
601: { "nn", roff_unsupp, NULL, NULL, 0, NULL },
602: { "nop", roff_unsupp, NULL, NULL, 0, NULL },
1.104 kristaps 603: { "nr", roff_nr, NULL, NULL, 0, NULL },
1.251 schwarze 604: { "nrf", roff_unsupp, NULL, NULL, 0, NULL },
605: { "nroff", roff_line_ignore, NULL, NULL, 0, NULL },
1.124 schwarze 606: { "ns", roff_line_ignore, NULL, NULL, 0, NULL },
1.251 schwarze 607: { "nx", roff_insec, NULL, NULL, 0, NULL },
608: { "open", roff_insec, NULL, NULL, 0, NULL },
609: { "opena", roff_insec, NULL, NULL, 0, NULL },
610: { "os", roff_line_ignore, NULL, NULL, 0, NULL },
611: { "output", roff_unsupp, NULL, NULL, 0, NULL },
612: { "padj", roff_line_ignore, NULL, NULL, 0, NULL },
613: { "papersize", roff_line_ignore, NULL, NULL, 0, NULL },
614: { "pc", roff_line_ignore, NULL, NULL, 0, NULL },
615: { "pev", roff_line_ignore, NULL, NULL, 0, NULL },
616: { "pi", roff_insec, NULL, NULL, 0, NULL },
617: { "PI", roff_unsupp, NULL, NULL, 0, NULL },
1.229 schwarze 618: { "pl", roff_line_ignore, NULL, NULL, 0, NULL },
1.251 schwarze 619: { "pm", roff_line_ignore, NULL, NULL, 0, NULL },
620: { "pn", roff_line_ignore, NULL, NULL, 0, NULL },
621: { "pnr", roff_line_ignore, NULL, NULL, 0, NULL },
622: { "po", roff_line_ignore, NULL, NULL, 0, NULL },
1.124 schwarze 623: { "ps", roff_line_ignore, NULL, NULL, 0, NULL },
1.251 schwarze 624: { "psbb", roff_unsupp, NULL, NULL, 0, NULL },
1.255 schwarze 625: { "pshape", roff_unsupp, NULL, NULL, 0, NULL },
1.251 schwarze 626: { "pso", roff_insec, NULL, NULL, 0, NULL },
627: { "ptr", roff_line_ignore, NULL, NULL, 0, NULL },
628: { "pvs", roff_line_ignore, NULL, NULL, 0, NULL },
629: { "rchar", roff_unsupp, NULL, NULL, 0, NULL },
630: { "rd", roff_line_ignore, NULL, NULL, 0, NULL },
631: { "recursionlimit", roff_line_ignore, NULL, NULL, 0, NULL },
632: { "return", roff_unsupp, NULL, NULL, 0, NULL },
633: { "rfschar", roff_unsupp, NULL, NULL, 0, NULL },
634: { "rhang", roff_line_ignore, NULL, NULL, 0, NULL },
635: { "rj", roff_line_ignore, NULL, NULL, 0, NULL },
1.122 schwarze 636: { "rm", roff_rm, NULL, NULL, 0, NULL },
1.251 schwarze 637: { "rn", roff_unsupp, NULL, NULL, 0, NULL },
638: { "rnn", roff_unsupp, NULL, NULL, 0, NULL },
1.203 schwarze 639: { "rr", roff_rr, NULL, NULL, 0, NULL },
1.251 schwarze 640: { "rs", roff_line_ignore, NULL, NULL, 0, NULL },
641: { "rt", roff_line_ignore, NULL, NULL, 0, NULL },
642: { "schar", roff_unsupp, NULL, NULL, 0, NULL },
643: { "sentchar", roff_line_ignore, NULL, NULL, 0, NULL },
644: { "shc", roff_line_ignore, NULL, NULL, 0, NULL },
645: { "shift", roff_unsupp, NULL, NULL, 0, NULL },
646: { "sizes", roff_line_ignore, NULL, NULL, 0, NULL },
1.105 kristaps 647: { "so", roff_so, NULL, NULL, 0, NULL },
1.251 schwarze 648: { "spacewidth", roff_line_ignore, NULL, NULL, 0, NULL },
649: { "special", roff_line_ignore, NULL, NULL, 0, NULL },
650: { "spreadwarn", roff_line_ignore, NULL, NULL, 0, NULL },
651: { "ss", roff_line_ignore, NULL, NULL, 0, NULL },
652: { "sty", roff_line_ignore, NULL, NULL, 0, NULL },
653: { "substring", roff_unsupp, NULL, NULL, 0, NULL },
654: { "sv", roff_line_ignore, NULL, NULL, 0, NULL },
655: { "sy", roff_insec, NULL, NULL, 0, NULL },
656: { "T&", roff_T_, NULL, NULL, 0, NULL },
1.255 schwarze 657: { "ta", roff_unsupp, NULL, NULL, 0, NULL },
1.251 schwarze 658: { "tc", roff_unsupp, NULL, NULL, 0, NULL },
659: { "TE", roff_TE, NULL, NULL, 0, NULL },
660: { "TH", roff_TH, NULL, NULL, 0, NULL },
1.255 schwarze 661: { "ti", roff_unsupp, NULL, NULL, 0, NULL },
1.251 schwarze 662: { "tkf", roff_line_ignore, NULL, NULL, 0, NULL },
663: { "tl", roff_unsupp, NULL, NULL, 0, NULL },
664: { "tm", roff_line_ignore, NULL, NULL, 0, NULL },
665: { "tm1", roff_line_ignore, NULL, NULL, 0, NULL },
666: { "tmc", roff_line_ignore, NULL, NULL, 0, NULL },
1.164 kristaps 667: { "tr", roff_tr, NULL, NULL, 0, NULL },
1.251 schwarze 668: { "track", roff_line_ignore, NULL, NULL, 0, NULL },
669: { "transchar", roff_line_ignore, NULL, NULL, 0, NULL },
670: { "trf", roff_insec, NULL, NULL, 0, NULL },
671: { "trimat", roff_line_ignore, NULL, NULL, 0, NULL },
672: { "trin", roff_unsupp, NULL, NULL, 0, NULL },
673: { "trnt", roff_unsupp, NULL, NULL, 0, NULL },
674: { "troff", roff_line_ignore, NULL, NULL, 0, NULL },
1.109 kristaps 675: { "TS", roff_TS, NULL, NULL, 0, NULL },
1.251 schwarze 676: { "uf", roff_line_ignore, NULL, NULL, 0, NULL },
677: { "ul", roff_line_ignore, NULL, NULL, 0, NULL },
678: { "unformat", roff_unsupp, NULL, NULL, 0, NULL },
679: { "unwatch", roff_line_ignore, NULL, NULL, 0, NULL },
680: { "unwatchn", roff_line_ignore, NULL, NULL, 0, NULL },
681: { "vpt", roff_line_ignore, NULL, NULL, 0, NULL },
682: { "vs", roff_line_ignore, NULL, NULL, 0, NULL },
683: { "warn", roff_line_ignore, NULL, NULL, 0, NULL },
684: { "warnscale", roff_line_ignore, NULL, NULL, 0, NULL },
685: { "watch", roff_line_ignore, NULL, NULL, 0, NULL },
686: { "watchlength", roff_line_ignore, NULL, NULL, 0, NULL },
687: { "watchn", roff_line_ignore, NULL, NULL, 0, NULL },
688: { "wh", roff_unsupp, NULL, NULL, 0, NULL },
689: { "while", roff_unsupp, NULL, NULL, 0, NULL },
690: { "write", roff_insec, NULL, NULL, 0, NULL },
691: { "writec", roff_insec, NULL, NULL, 0, NULL },
692: { "writem", roff_insec, NULL, NULL, 0, NULL },
693: { "xflag", roff_line_ignore, NULL, NULL, 0, NULL },
1.85 kristaps 694: { ".", roff_cblock, NULL, NULL, 0, NULL },
1.106 kristaps 695: { NULL, roff_userdef, NULL, NULL, 0, NULL },
1.67 kristaps 696: };
697:
1.200 schwarze 698: /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
1.175 schwarze 699: const char *const __mdoc_reserved[] = {
700: "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
701: "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
702: "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
703: "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
1.200 schwarze 704: "Dt", "Dv", "Dx", "D1",
705: "Ec", "Ed", "Ef", "Ek", "El", "Em",
706: "En", "Eo", "Er", "Es", "Ev", "Ex",
1.175 schwarze 707: "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
1.200 schwarze 708: "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
709: "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
1.175 schwarze 710: "Oc", "Oo", "Op", "Os", "Ot", "Ox",
1.200 schwarze 711: "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
712: "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
713: "Sc", "Sh", "Sm", "So", "Sq",
1.175 schwarze 714: "Ss", "St", "Sx", "Sy",
715: "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
1.200 schwarze 716: "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
1.175 schwarze 717: "%P", "%Q", "%R", "%T", "%U", "%V",
718: NULL
719: };
720:
1.200 schwarze 721: /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
1.175 schwarze 722: const char *const __man_reserved[] = {
1.200 schwarze 723: "AT", "B", "BI", "BR", "DT",
724: "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
725: "LP", "OP", "P", "PD", "PP",
726: "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
727: "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
1.175 schwarze 728: NULL
729: };
730:
1.141 kristaps 731: /* Array of injected predefined strings. */
732: #define PREDEFS_MAX 38
733: static const struct predef predefs[PREDEFS_MAX] = {
734: #include "predefs.in"
735: };
736:
1.155 kristaps 737: /* See roffhash_find() */
1.85 kristaps 738: #define ROFF_HASH(p) (p[0] - ASCII_LO)
739:
1.178 schwarze 740: static int roffit_lines; /* number of lines to delay */
741: static char *roffit_macro; /* nil-terminated macro line */
742:
1.207 schwarze 743:
1.266 schwarze 744: /* --- request table ------------------------------------------------------ */
745:
1.85 kristaps 746: static void
1.155 kristaps 747: roffhash_init(void)
1.85 kristaps 748: {
749: struct roffmac *n;
750: int buc, i;
751:
1.106 kristaps 752: for (i = 0; i < (int)ROFF_USERDEF; i++) {
1.85 kristaps 753: assert(roffs[i].name[0] >= ASCII_LO);
754: assert(roffs[i].name[0] <= ASCII_HI);
755:
756: buc = ROFF_HASH(roffs[i].name);
757:
758: if (NULL != (n = hash[buc])) {
759: for ( ; n->next; n = n->next)
760: /* Do nothing. */ ;
761: n->next = &roffs[i];
762: } else
763: hash[buc] = &roffs[i];
764: }
765: }
766:
1.67 kristaps 767: /*
768: * Look up a roff token by its name. Returns ROFF_MAX if no macro by
769: * the nil-terminated string name could be found.
770: */
771: static enum rofft
1.155 kristaps 772: roffhash_find(const char *p, size_t s)
1.67 kristaps 773: {
1.85 kristaps 774: int buc;
775: struct roffmac *n;
1.67 kristaps 776:
1.85 kristaps 777: /*
778: * libroff has an extremely simple hashtable, for the time
779: * being, which simply keys on the first character, which must
780: * be printable, then walks a chain. It works well enough until
781: * optimised.
782: */
783:
784: if (p[0] < ASCII_LO || p[0] > ASCII_HI)
785: return(ROFF_MAX);
786:
787: buc = ROFF_HASH(p);
788:
789: if (NULL == (n = hash[buc]))
790: return(ROFF_MAX);
791: for ( ; n; n = n->next)
1.106 kristaps 792: if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])
1.85 kristaps 793: return((enum rofft)(n - roffs));
1.67 kristaps 794:
795: return(ROFF_MAX);
796: }
797:
1.266 schwarze 798: /* --- stack of request blocks -------------------------------------------- */
799:
1.67 kristaps 800: /*
801: * Pop the current node off of the stack of roff instructions currently
802: * pending.
803: */
804: static void
805: roffnode_pop(struct roff *r)
806: {
807: struct roffnode *p;
808:
1.75 kristaps 809: assert(r->last);
1.207 schwarze 810: p = r->last;
1.82 kristaps 811:
1.75 kristaps 812: r->last = r->last->parent;
1.106 kristaps 813: free(p->name);
814: free(p->end);
1.67 kristaps 815: free(p);
816: }
817:
818: /*
819: * Push a roff node onto the instruction stack. This must later be
820: * removed with roffnode_pop().
821: */
1.98 schwarze 822: static void
1.106 kristaps 823: roffnode_push(struct roff *r, enum rofft tok, const char *name,
824: int line, int col)
1.67 kristaps 825: {
826: struct roffnode *p;
827:
1.98 schwarze 828: p = mandoc_calloc(1, sizeof(struct roffnode));
1.67 kristaps 829: p->tok = tok;
1.106 kristaps 830: if (name)
831: p->name = mandoc_strdup(name);
1.67 kristaps 832: p->parent = r->last;
833: p->line = line;
834: p->col = col;
1.198 schwarze 835: p->rule = p->parent ? p->parent->rule : 0;
1.67 kristaps 836:
837: r->last = p;
838: }
839:
1.266 schwarze 840: /* --- roff parser state data management ---------------------------------- */
841:
1.67 kristaps 842: static void
843: roff_free1(struct roff *r)
844: {
1.176 schwarze 845: struct tbl_node *tbl;
1.125 kristaps 846: struct eqn_node *e;
1.167 kristaps 847: int i;
1.67 kristaps 848:
1.176 schwarze 849: while (NULL != (tbl = r->first_tbl)) {
850: r->first_tbl = tbl->next;
851: tbl_free(tbl);
1.109 kristaps 852: }
1.113 kristaps 853: r->first_tbl = r->last_tbl = r->tbl = NULL;
854:
1.125 kristaps 855: while (NULL != (e = r->first_eqn)) {
856: r->first_eqn = e->next;
857: eqn_free(e);
858: }
859: r->first_eqn = r->last_eqn = r->eqn = NULL;
860:
1.67 kristaps 861: while (r->last)
862: roffnode_pop(r);
1.109 kristaps 863:
1.223 schwarze 864: free (r->rstack);
865: r->rstack = NULL;
866: r->rstacksz = 0;
867: r->rstackpos = -1;
868:
869: roff_freereg(r->regtab);
870: r->regtab = NULL;
871:
1.167 kristaps 872: roff_freestr(r->strtab);
873: roff_freestr(r->xmbtab);
874: r->strtab = r->xmbtab = NULL;
875:
876: if (r->xtab)
877: for (i = 0; i < 128; i++)
878: free(r->xtab[i].p);
879: free(r->xtab);
880: r->xtab = NULL;
1.67 kristaps 881: }
882:
883: void
884: roff_reset(struct roff *r)
885: {
886:
887: roff_free1(r);
1.227 schwarze 888: r->format = r->options & (MPARSE_MDOC | MPARSE_MAN);
1.174 kristaps 889: r->control = 0;
1.67 kristaps 890: }
891:
892: void
893: roff_free(struct roff *r)
894: {
895:
896: roff_free1(r);
897: free(r);
898: }
899:
900: struct roff *
1.237 schwarze 901: roff_alloc(struct mparse *parse, const struct mchars *mchars, int options)
1.67 kristaps 902: {
903: struct roff *r;
904:
1.98 schwarze 905: r = mandoc_calloc(1, sizeof(struct roff));
1.128 kristaps 906: r->parse = parse;
1.237 schwarze 907: r->mchars = mchars;
1.199 schwarze 908: r->options = options;
1.227 schwarze 909: r->format = options & (MPARSE_MDOC | MPARSE_MAN);
1.82 kristaps 910: r->rstackpos = -1;
1.207 schwarze 911:
1.155 kristaps 912: roffhash_init();
1.141 kristaps 913:
1.67 kristaps 914: return(r);
1.265 schwarze 915: }
916:
1.266 schwarze 917: /* --- syntax tree state data management ---------------------------------- */
918:
1.265 schwarze 919: static void
920: roff_man_free1(struct roff_man *man)
921: {
922:
1.266 schwarze 923: if (man->first != NULL)
924: roff_node_delete(man, man->first);
1.265 schwarze 925: free(man->meta.msec);
926: free(man->meta.vol);
927: free(man->meta.os);
928: free(man->meta.arch);
929: free(man->meta.title);
930: free(man->meta.name);
931: free(man->meta.date);
932: }
933:
934: static void
935: roff_man_alloc1(struct roff_man *man)
936: {
937:
938: memset(&man->meta, 0, sizeof(man->meta));
939: man->first = mandoc_calloc(1, sizeof(*man->first));
940: man->first->type = ROFFT_ROOT;
941: man->last = man->first;
942: man->last_es = NULL;
943: man->flags = 0;
944: man->macroset = MACROSET_NONE;
945: man->lastsec = man->lastnamed = SEC_NONE;
946: man->next = ROFF_NEXT_CHILD;
947: }
948:
949: void
950: roff_man_reset(struct roff_man *man)
951: {
952:
953: roff_man_free1(man);
954: roff_man_alloc1(man);
955: }
956:
957: void
958: roff_man_free(struct roff_man *man)
959: {
960:
961: roff_man_free1(man);
962: free(man);
963: }
964:
965: struct roff_man *
966: roff_man_alloc(struct roff *roff, struct mparse *parse,
967: const char *defos, int quick)
968: {
969: struct roff_man *man;
970:
971: man = mandoc_calloc(1, sizeof(*man));
972: man->parse = parse;
973: man->roff = roff;
974: man->defos = defos;
975: man->quick = quick;
976: roff_man_alloc1(man);
977: return(man);
1.67 kristaps 978: }
979:
1.266 schwarze 980: /* --- syntax tree handling ----------------------------------------------- */
981:
982: struct roff_node *
983: roff_node_alloc(struct roff_man *man, int line, int pos,
984: enum roff_type type, int tok)
985: {
986: struct roff_node *n;
987:
988: n = mandoc_calloc(1, sizeof(*n));
989: n->line = line;
990: n->pos = pos;
991: n->tok = tok;
992: n->type = type;
993: n->sec = man->lastsec;
994:
995: if (man->flags & MDOC_SYNOPSIS)
996: n->flags |= MDOC_SYNPRETTY;
997: else
998: n->flags &= ~MDOC_SYNPRETTY;
999: if (man->flags & MDOC_NEWLINE)
1000: n->flags |= MDOC_LINE;
1001: man->flags &= ~MDOC_NEWLINE;
1002:
1003: return(n);
1004: }
1005:
1006: void
1007: roff_node_append(struct roff_man *man, struct roff_node *n)
1008: {
1009:
1010: switch (man->next) {
1011: case ROFF_NEXT_SIBLING:
1012: man->last->next = n;
1013: n->prev = man->last;
1014: n->parent = man->last->parent;
1015: break;
1016: case ROFF_NEXT_CHILD:
1017: man->last->child = n;
1018: n->parent = man->last;
1019: break;
1020: default:
1021: abort();
1022: /* NOTREACHED */
1023: }
1024: n->parent->nchild++;
1025:
1026: /*
1027: * Copy over the normalised-data pointer of our parent. Not
1028: * everybody has one, but copying a null pointer is fine.
1029: */
1030:
1031: switch (n->type) {
1032: case ROFFT_BODY:
1033: if (n->end != ENDBODY_NOT)
1034: break;
1035: /* FALLTHROUGH */
1036: case ROFFT_TAIL:
1037: /* FALLTHROUGH */
1038: case ROFFT_HEAD:
1039: n->norm = n->parent->norm;
1040: break;
1041: default:
1042: break;
1043: }
1044:
1045: if (man->macroset == MACROSET_MDOC)
1046: mdoc_valid_pre(man, n);
1047:
1048: switch (n->type) {
1049: case ROFFT_HEAD:
1050: assert(n->parent->type == ROFFT_BLOCK);
1051: n->parent->head = n;
1052: break;
1053: case ROFFT_BODY:
1054: if (n->end)
1055: break;
1056: assert(n->parent->type == ROFFT_BLOCK);
1057: n->parent->body = n;
1058: break;
1059: case ROFFT_TAIL:
1060: assert(n->parent->type == ROFFT_BLOCK);
1061: n->parent->tail = n;
1062: break;
1063: default:
1064: break;
1065: }
1066: man->last = n;
1067: }
1068:
1.267 schwarze 1069: void
1070: roff_word_alloc(struct roff_man *man, int line, int pos, const char *word)
1071: {
1072: struct roff_node *n;
1073:
1074: n = roff_node_alloc(man, line, pos, ROFFT_TEXT, TOKEN_NONE);
1075: n->string = roff_strdup(man->roff, word);
1076: roff_node_append(man, n);
1077: if (man->macroset == MACROSET_MDOC)
1078: mdoc_valid_post(man);
1079: else
1080: man_valid_post(man);
1081: man->next = ROFF_NEXT_SIBLING;
1082: }
1083:
1084: void
1085: roff_word_append(struct roff_man *man, const char *word)
1086: {
1087: struct roff_node *n;
1088: char *addstr, *newstr;
1089:
1090: n = man->last;
1091: addstr = roff_strdup(man->roff, word);
1092: mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
1093: free(addstr);
1094: free(n->string);
1095: n->string = newstr;
1096: man->next = ROFF_NEXT_SIBLING;
1.268 schwarze 1097: }
1098:
1099: void
1100: roff_elem_alloc(struct roff_man *man, int line, int pos, int tok)
1101: {
1102: struct roff_node *n;
1103:
1104: n = roff_node_alloc(man, line, pos, ROFFT_ELEM, tok);
1105: roff_node_append(man, n);
1106: man->next = ROFF_NEXT_CHILD;
1107: }
1108:
1109: struct roff_node *
1110: roff_block_alloc(struct roff_man *man, int line, int pos, int tok)
1111: {
1112: struct roff_node *n;
1113:
1114: n = roff_node_alloc(man, line, pos, ROFFT_BLOCK, tok);
1115: roff_node_append(man, n);
1116: man->next = ROFF_NEXT_CHILD;
1117: return(n);
1.267 schwarze 1118: }
1119:
1.266 schwarze 1120: struct roff_node *
1121: roff_head_alloc(struct roff_man *man, int line, int pos, int tok)
1122: {
1123: struct roff_node *n;
1124:
1125: n = roff_node_alloc(man, line, pos, ROFFT_HEAD, tok);
1126: roff_node_append(man, n);
1127: man->next = ROFF_NEXT_CHILD;
1128: return(n);
1129: }
1130:
1131: struct roff_node *
1132: roff_body_alloc(struct roff_man *man, int line, int pos, int tok)
1133: {
1134: struct roff_node *n;
1135:
1136: n = roff_node_alloc(man, line, pos, ROFFT_BODY, tok);
1137: roff_node_append(man, n);
1138: man->next = ROFF_NEXT_CHILD;
1139: return(n);
1.267 schwarze 1140: }
1141:
1142: void
1143: roff_addeqn(struct roff_man *man, const struct eqn *eqn)
1144: {
1145: struct roff_node *n;
1146:
1147: n = roff_node_alloc(man, eqn->ln, eqn->pos, ROFFT_EQN, TOKEN_NONE);
1148: n->eqn = eqn;
1149: if (eqn->ln > man->last->line)
1150: n->flags |= MDOC_LINE;
1151: roff_node_append(man, n);
1152: man->next = ROFF_NEXT_SIBLING;
1153: }
1154:
1155: void
1156: roff_addtbl(struct roff_man *man, const struct tbl_span *tbl)
1157: {
1158: struct roff_node *n;
1159:
1160: if (man->macroset == MACROSET_MAN)
1161: man_breakscope(man, TOKEN_NONE);
1162: n = roff_node_alloc(man, tbl->line, 0, ROFFT_TBL, TOKEN_NONE);
1163: n->span = tbl;
1164: roff_node_append(man, n);
1165: if (man->macroset == MACROSET_MDOC)
1166: mdoc_valid_post(man);
1167: else
1168: man_valid_post(man);
1169: man->next = ROFF_NEXT_SIBLING;
1.266 schwarze 1170: }
1171:
1172: void
1173: roff_node_unlink(struct roff_man *man, struct roff_node *n)
1174: {
1175:
1176: /* Adjust siblings. */
1177:
1178: if (n->prev)
1179: n->prev->next = n->next;
1180: if (n->next)
1181: n->next->prev = n->prev;
1182:
1183: /* Adjust parent. */
1184:
1185: if (n->parent != NULL) {
1186: n->parent->nchild--;
1187: if (n->parent->child == n)
1188: n->parent->child = n->next;
1189: if (n->parent->last == n)
1190: n->parent->last = n->prev;
1191: }
1192:
1193: /* Adjust parse point. */
1194:
1195: if (man == NULL)
1196: return;
1197: if (man->last == n) {
1198: if (n->prev == NULL) {
1199: man->last = n->parent;
1200: man->next = ROFF_NEXT_CHILD;
1201: } else {
1202: man->last = n->prev;
1203: man->next = ROFF_NEXT_SIBLING;
1204: }
1205: }
1206: if (man->first == n)
1207: man->first = NULL;
1208: }
1209:
1210: void
1211: roff_node_free(struct roff_node *n)
1212: {
1213:
1214: if (n->args != NULL)
1215: mdoc_argv_free(n->args);
1216: if (n->type == ROFFT_BLOCK || n->type == ROFFT_ELEM)
1217: free(n->norm);
1218: free(n->string);
1219: free(n);
1220: }
1221:
1222: void
1223: roff_node_delete(struct roff_man *man, struct roff_node *n)
1224: {
1225:
1226: while (n->child != NULL)
1227: roff_node_delete(man, n->child);
1228: assert(n->nchild == 0);
1229: roff_node_unlink(man, n);
1230: roff_node_free(n);
1.269 ! schwarze 1231: }
! 1232:
! 1233: void
! 1234: deroff(char **dest, const struct roff_node *n)
! 1235: {
! 1236: char *cp;
! 1237: size_t sz;
! 1238:
! 1239: if (n->type != ROFFT_TEXT) {
! 1240: for (n = n->child; n != NULL; n = n->next)
! 1241: deroff(dest, n);
! 1242: return;
! 1243: }
! 1244:
! 1245: /* Skip leading whitespace and escape sequences. */
! 1246:
! 1247: cp = n->string;
! 1248: while (*cp != '\0') {
! 1249: if ('\\' == *cp) {
! 1250: cp++;
! 1251: mandoc_escape((const char **)&cp, NULL, NULL);
! 1252: } else if (isspace((unsigned char)*cp))
! 1253: cp++;
! 1254: else
! 1255: break;
! 1256: }
! 1257:
! 1258: /* Skip trailing whitespace. */
! 1259:
! 1260: for (sz = strlen(cp); sz; sz--)
! 1261: if ( ! isspace((unsigned char)cp[sz-1]))
! 1262: break;
! 1263:
! 1264: /* Skip empty strings. */
! 1265:
! 1266: if (sz == 0)
! 1267: return;
! 1268:
! 1269: if (*dest == NULL) {
! 1270: *dest = mandoc_strndup(cp, sz);
! 1271: return;
! 1272: }
! 1273:
! 1274: mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
! 1275: free(*dest);
! 1276: *dest = cp;
1.266 schwarze 1277: }
1278:
1279: /* --- main functions of the roff parser ---------------------------------- */
1280:
1.94 kristaps 1281: /*
1.206 schwarze 1282: * In the current line, expand escape sequences that tend to get
1283: * used in numerical expressions and conditional requests.
1284: * Also check the syntax of the remaining escape sequences.
1.154 kristaps 1285: */
1.172 schwarze 1286: static enum rofferr
1.238 schwarze 1287: roff_res(struct roff *r, struct buf *buf, int ln, int pos)
1.94 kristaps 1288: {
1.208 schwarze 1289: char ubuf[24]; /* buffer to print the number */
1.205 schwarze 1290: const char *start; /* start of the string to process */
1.209 schwarze 1291: char *stesc; /* start of an escape sequence ('\\') */
1.108 schwarze 1292: const char *stnam; /* start of the name, after "[(*" */
1293: const char *cp; /* end of the name, e.g. before ']' */
1294: const char *res; /* the string to be substituted */
1.238 schwarze 1295: char *nbuf; /* new buffer to copy buf->buf to */
1.181 schwarze 1296: size_t maxl; /* expected length of the escape name */
1297: size_t naml; /* actual length of the escape name */
1.237 schwarze 1298: enum mandoc_esc esc; /* type of the escape sequence */
1299: int inaml; /* length returned from mandoc_escape() */
1.181 schwarze 1300: int expand_count; /* to avoid infinite loops */
1.206 schwarze 1301: int npos; /* position in numeric expression */
1.218 schwarze 1302: int arg_complete; /* argument not interrupted by eol */
1.206 schwarze 1303: char term; /* character terminating the escape */
1.94 kristaps 1304:
1.170 schwarze 1305: expand_count = 0;
1.238 schwarze 1306: start = buf->buf + pos;
1.205 schwarze 1307: stesc = strchr(start, '\0') - 1;
1308: while (stesc-- > start) {
1.170 schwarze 1309:
1.205 schwarze 1310: /* Search backwards for the next backslash. */
1311:
1.238 schwarze 1312: if (*stesc != '\\')
1.205 schwarze 1313: continue;
1314:
1315: /* If it is escaped, skip it. */
1316:
1317: for (cp = stesc - 1; cp >= start; cp--)
1.238 schwarze 1318: if (*cp != '\\')
1.205 schwarze 1319: break;
1320:
1.238 schwarze 1321: if ((stesc - cp) % 2 == 0) {
1.209 schwarze 1322: stesc = (char *)cp;
1.205 schwarze 1323: continue;
1324: }
1.108 schwarze 1325:
1.206 schwarze 1326: /* Decide whether to expand or to check only. */
1.108 schwarze 1327:
1.206 schwarze 1328: term = '\0';
1.205 schwarze 1329: cp = stesc + 1;
1.181 schwarze 1330: switch (*cp) {
1.207 schwarze 1331: case '*':
1.181 schwarze 1332: res = NULL;
1333: break;
1.207 schwarze 1334: case 'B':
1.206 schwarze 1335: /* FALLTHROUGH */
1.207 schwarze 1336: case 'w':
1.206 schwarze 1337: term = cp[1];
1338: /* FALLTHROUGH */
1.207 schwarze 1339: case 'n':
1.181 schwarze 1340: res = ubuf;
1341: break;
1342: default:
1.237 schwarze 1343: esc = mandoc_escape(&cp, &stnam, &inaml);
1344: if (esc == ESCAPE_ERROR ||
1345: (esc == ESCAPE_SPECIAL &&
1346: mchars_spec2cp(r->mchars, stnam, inaml) < 0))
1.219 schwarze 1347: mandoc_vmsg(MANDOCERR_ESC_BAD,
1.238 schwarze 1348: r->parse, ln, (int)(stesc - buf->buf),
1.219 schwarze 1349: "%.*s", (int)(cp - stesc), stesc);
1.205 schwarze 1350: continue;
1.152 kristaps 1351: }
1352:
1.205 schwarze 1353: if (EXPAND_LIMIT < ++expand_count) {
1354: mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
1.238 schwarze 1355: ln, (int)(stesc - buf->buf), NULL);
1.205 schwarze 1356: return(ROFF_IGN);
1357: }
1.108 schwarze 1358:
1359: /*
1360: * The third character decides the length
1.181 schwarze 1361: * of the name of the string or register.
1.108 schwarze 1362: * Save a pointer to the name.
1363: */
1364:
1.238 schwarze 1365: if (term == '\0') {
1.206 schwarze 1366: switch (*++cp) {
1.207 schwarze 1367: case '\0':
1.206 schwarze 1368: maxl = 0;
1369: break;
1.207 schwarze 1370: case '(':
1.206 schwarze 1371: cp++;
1372: maxl = 2;
1373: break;
1.207 schwarze 1374: case '[':
1.206 schwarze 1375: cp++;
1376: term = ']';
1377: maxl = 0;
1378: break;
1379: default:
1380: maxl = 1;
1381: break;
1382: }
1383: } else {
1384: cp += 2;
1.94 kristaps 1385: maxl = 0;
1386: }
1.108 schwarze 1387: stnam = cp;
1.94 kristaps 1388:
1.108 schwarze 1389: /* Advance to the end of the name. */
1.94 kristaps 1390:
1.253 schwarze 1391: naml = 0;
1.218 schwarze 1392: arg_complete = 1;
1.253 schwarze 1393: while (maxl == 0 || naml < maxl) {
1.238 schwarze 1394: if (*cp == '\0') {
1.219 schwarze 1395: mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238 schwarze 1396: ln, (int)(stesc - buf->buf), stesc);
1.218 schwarze 1397: arg_complete = 0;
1.206 schwarze 1398: break;
1.153 kristaps 1399: }
1.238 schwarze 1400: if (maxl == 0 && *cp == term) {
1.206 schwarze 1401: cp++;
1.253 schwarze 1402: break;
1403: }
1404: if (*cp++ != '\\' || stesc[1] != 'w') {
1405: naml++;
1406: continue;
1407: }
1408: switch (mandoc_escape(&cp, NULL, NULL)) {
1409: case ESCAPE_SPECIAL:
1410: /* FALLTHROUGH */
1411: case ESCAPE_UNICODE:
1412: /* FALLTHROUGH */
1413: case ESCAPE_NUMBERED:
1414: /* FALLTHROUGH */
1415: case ESCAPE_OVERSTRIKE:
1416: naml++;
1417: break;
1418: default:
1.94 kristaps 1419: break;
1.206 schwarze 1420: }
1.94 kristaps 1421: }
1422:
1.108 schwarze 1423: /*
1424: * Retrieve the replacement string; if it is
1425: * undefined, resume searching for escapes.
1426: */
1427:
1.206 schwarze 1428: switch (stesc[1]) {
1.207 schwarze 1429: case '*':
1.218 schwarze 1430: if (arg_complete)
1431: res = roff_getstrn(r, stnam, naml);
1.206 schwarze 1432: break;
1.207 schwarze 1433: case 'B':
1.206 schwarze 1434: npos = 0;
1.218 schwarze 1435: ubuf[0] = arg_complete &&
1.261 schwarze 1436: roff_evalnum(r, ln, stnam, &npos,
1437: NULL, ROFFNUM_SCALE) &&
1.218 schwarze 1438: stnam + npos + 1 == cp ? '1' : '0';
1.206 schwarze 1439: ubuf[1] = '\0';
1440: break;
1.207 schwarze 1441: case 'n':
1.218 schwarze 1442: if (arg_complete)
1443: (void)snprintf(ubuf, sizeof(ubuf), "%d",
1444: roff_getregn(r, stnam, naml));
1445: else
1446: ubuf[0] = '\0';
1.206 schwarze 1447: break;
1.207 schwarze 1448: case 'w':
1.218 schwarze 1449: /* use even incomplete args */
1.208 schwarze 1450: (void)snprintf(ubuf, sizeof(ubuf), "%d",
1.206 schwarze 1451: 24 * (int)naml);
1452: break;
1453: }
1.94 kristaps 1454:
1.238 schwarze 1455: if (res == NULL) {
1.219 schwarze 1456: mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238 schwarze 1457: r->parse, ln, (int)(stesc - buf->buf),
1.219 schwarze 1458: "%.*s", (int)naml, stnam);
1.142 kristaps 1459: res = "";
1.246 schwarze 1460: } else if (buf->sz + strlen(res) > SHRT_MAX) {
1461: mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
1462: ln, (int)(stesc - buf->buf), NULL);
1463: return(ROFF_IGN);
1.94 kristaps 1464: }
1465:
1.108 schwarze 1466: /* Replace the escape sequence by the string. */
1467:
1.209 schwarze 1468: *stesc = '\0';
1.238 schwarze 1469: buf->sz = mandoc_asprintf(&nbuf, "%s%s%s",
1470: buf->buf, res, cp) + 1;
1.94 kristaps 1471:
1.205 schwarze 1472: /* Prepare for the next replacement. */
1.94 kristaps 1473:
1.205 schwarze 1474: start = nbuf + pos;
1.238 schwarze 1475: stesc = nbuf + (stesc - buf->buf) + strlen(res);
1476: free(buf->buf);
1477: buf->buf = nbuf;
1.154 kristaps 1478: }
1.172 schwarze 1479: return(ROFF_CONT);
1.154 kristaps 1480: }
1481:
1482: /*
1.178 schwarze 1483: * Process text streams:
1484: * Convert all breakable hyphens into ASCII_HYPH.
1485: * Decrement and spring input line trap.
1.154 kristaps 1486: */
1487: static enum rofferr
1.238 schwarze 1488: roff_parsetext(struct buf *buf, int pos, int *offs)
1.154 kristaps 1489: {
1490: size_t sz;
1491: const char *start;
1.178 schwarze 1492: char *p;
1493: int isz;
1.154 kristaps 1494: enum mandoc_esc esc;
1495:
1.238 schwarze 1496: start = p = buf->buf + pos;
1.154 kristaps 1497:
1.238 schwarze 1498: while (*p != '\0') {
1.154 kristaps 1499: sz = strcspn(p, "-\\");
1500: p += sz;
1501:
1.238 schwarze 1502: if (*p == '\0')
1.159 kristaps 1503: break;
1504:
1.238 schwarze 1505: if (*p == '\\') {
1.154 kristaps 1506: /* Skip over escapes. */
1507: p++;
1.189 schwarze 1508: esc = mandoc_escape((const char **)&p, NULL, NULL);
1.238 schwarze 1509: if (esc == ESCAPE_ERROR)
1.154 kristaps 1510: break;
1.264 schwarze 1511: while (*p == '-')
1512: p++;
1.155 kristaps 1513: continue;
1.159 kristaps 1514: } else if (p == start) {
1.158 kristaps 1515: p++;
1.155 kristaps 1516: continue;
1.158 kristaps 1517: }
1.155 kristaps 1518:
1.171 schwarze 1519: if (isalpha((unsigned char)p[-1]) &&
1520: isalpha((unsigned char)p[1]))
1.155 kristaps 1521: *p = ASCII_HYPH;
1522: p++;
1.94 kristaps 1523: }
1524:
1.178 schwarze 1525: /* Spring the input line trap. */
1.238 schwarze 1526: if (roffit_lines == 1) {
1527: isz = mandoc_asprintf(&p, "%s\n.%s", buf->buf, roffit_macro);
1528: free(buf->buf);
1529: buf->buf = p;
1530: buf->sz = isz + 1;
1.178 schwarze 1531: *offs = 0;
1532: free(roffit_macro);
1533: roffit_lines = 0;
1534: return(ROFF_REPARSE);
1.238 schwarze 1535: } else if (roffit_lines > 1)
1.178 schwarze 1536: --roffit_lines;
1.154 kristaps 1537: return(ROFF_CONT);
1.94 kristaps 1538: }
1539:
1.67 kristaps 1540: enum rofferr
1.238 schwarze 1541: roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs)
1.67 kristaps 1542: {
1543: enum rofft t;
1.109 kristaps 1544: enum rofferr e;
1.238 schwarze 1545: int pos; /* parse point */
1.243 schwarze 1546: int spos; /* saved parse point for messages */
1.238 schwarze 1547: int ppos; /* original offset in buf->buf */
1548: int ctl; /* macro line (boolean) */
1549:
1550: ppos = pos = *offs;
1.79 kristaps 1551:
1.230 schwarze 1552: /* Handle in-line equation delimiters. */
1553:
1.236 schwarze 1554: if (r->tbl == NULL &&
1555: r->last_eqn != NULL && r->last_eqn->delim &&
1.230 schwarze 1556: (r->eqn == NULL || r->eqn_inline)) {
1.238 schwarze 1557: e = roff_eqndelim(r, buf, pos);
1.230 schwarze 1558: if (e == ROFF_REPARSE)
1559: return(e);
1560: assert(e == ROFF_CONT);
1561: }
1562:
1563: /* Expand some escape sequences. */
1.94 kristaps 1564:
1.238 schwarze 1565: e = roff_res(r, buf, ln, pos);
1566: if (e == ROFF_IGN)
1.172 schwarze 1567: return(e);
1.238 schwarze 1568: assert(e == ROFF_CONT);
1.94 kristaps 1569:
1.238 schwarze 1570: ctl = roff_getcontrol(r, buf->buf, &pos);
1.130 kristaps 1571:
1.94 kristaps 1572: /*
1.79 kristaps 1573: * First, if a scope is open and we're not a macro, pass the
1.252 schwarze 1574: * text through the macro's filter.
1575: * Equations process all content themselves.
1576: * Tables process almost all content themselves, but we want
1577: * to warn about macros before passing it there.
1.79 kristaps 1578: */
1.74 kristaps 1579:
1.252 schwarze 1580: if (r->last != NULL && ! ctl) {
1.78 kristaps 1581: t = r->last->tok;
1582: assert(roffs[t].text);
1.238 schwarze 1583: e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
1584: assert(e == ROFF_IGN || e == ROFF_CONT);
1585: if (e != ROFF_CONT)
1.125 kristaps 1586: return(e);
1.182 schwarze 1587: }
1.252 schwarze 1588: if (r->eqn != NULL)
1.238 schwarze 1589: return(eqn_read(&r->eqn, ln, buf->buf, ppos, offs));
1.252 schwarze 1590: if (r->tbl != NULL && ( ! ctl || buf->buf[pos] == '\0'))
1.259 schwarze 1591: return(tbl_read(r->tbl, ln, buf->buf, ppos));
1.252 schwarze 1592: if ( ! ctl)
1.238 schwarze 1593: return(roff_parsetext(buf, pos, offs));
1.228 schwarze 1594:
1595: /* Skip empty request lines. */
1596:
1.238 schwarze 1597: if (buf->buf[pos] == '"') {
1.228 schwarze 1598: mandoc_msg(MANDOCERR_COMMENT_BAD, r->parse,
1599: ln, pos, NULL);
1600: return(ROFF_IGN);
1.238 schwarze 1601: } else if (buf->buf[pos] == '\0')
1.228 schwarze 1602: return(ROFF_IGN);
1.67 kristaps 1603:
1.79 kristaps 1604: /*
1605: * If a scope is open, go to the child handler for that macro,
1606: * as it may want to preprocess before doing anything with it.
1.125 kristaps 1607: * Don't do so if an equation is open.
1.79 kristaps 1608: */
1.78 kristaps 1609:
1.79 kristaps 1610: if (r->last) {
1611: t = r->last->tok;
1612: assert(roffs[t].sub);
1.238 schwarze 1613: return((*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs));
1.79 kristaps 1614: }
1.78 kristaps 1615:
1.243 schwarze 1616: /* No scope is open. This is a new request or macro. */
1617:
1618: spos = pos;
1619: t = roff_parse(r, buf->buf, &pos, ln, ppos);
1620:
1621: /* Tables ignore most macros. */
1622:
1623: if (r->tbl != NULL && (t == ROFF_MAX || t == ROFF_TS)) {
1624: mandoc_msg(MANDOCERR_TBLMACRO, r->parse,
1625: ln, pos, buf->buf + spos);
1.257 schwarze 1626: if (t == ROFF_TS)
1627: return(ROFF_IGN);
1628: while (buf->buf[pos] != '\0' && buf->buf[pos] != ' ')
1629: pos++;
1630: while (buf->buf[pos] != '\0' && buf->buf[pos] == ' ')
1631: pos++;
1632: return(tbl_read(r->tbl, ln, buf->buf, pos));
1.243 schwarze 1633: }
1634:
1.79 kristaps 1635: /*
1.243 schwarze 1636: * This is neither a roff request nor a user-defined macro.
1637: * Let the standard macro set parsers handle it.
1.79 kristaps 1638: */
1.67 kristaps 1639:
1.243 schwarze 1640: if (t == ROFF_MAX)
1.79 kristaps 1641: return(ROFF_CONT);
1.243 schwarze 1642:
1643: /* Execute a roff request or a user defined macro. */
1.67 kristaps 1644:
1.75 kristaps 1645: assert(roffs[t].proc);
1.238 schwarze 1646: return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.74 kristaps 1647: }
1648:
1.117 kristaps 1649: void
1.74 kristaps 1650: roff_endparse(struct roff *r)
1651: {
1652:
1.110 kristaps 1653: if (r->last)
1.221 schwarze 1654: mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1655: r->last->line, r->last->col,
1656: roffs[r->last->tok].name);
1.117 kristaps 1657:
1.125 kristaps 1658: if (r->eqn) {
1.221 schwarze 1659: mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1660: r->eqn->eqn.ln, r->eqn->eqn.pos, "EQ");
1.151 kristaps 1661: eqn_end(&r->eqn);
1.125 kristaps 1662: }
1663:
1.117 kristaps 1664: if (r->tbl) {
1.221 schwarze 1665: mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1666: r->tbl->line, r->tbl->pos, "TS");
1.151 kristaps 1667: tbl_end(&r->tbl);
1.117 kristaps 1668: }
1.67 kristaps 1669: }
1670:
1671: /*
1672: * Parse a roff node's type from the input buffer. This must be in the
1673: * form of ".foo xxx" in the usual way.
1674: */
1675: static enum rofft
1.214 schwarze 1676: roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
1.67 kristaps 1677: {
1.214 schwarze 1678: char *cp;
1.106 kristaps 1679: const char *mac;
1680: size_t maclen;
1.67 kristaps 1681: enum rofft t;
1682:
1.214 schwarze 1683: cp = buf + *pos;
1684:
1685: if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
1.67 kristaps 1686: return(ROFF_MAX);
1687:
1.214 schwarze 1688: mac = cp;
1689: maclen = roff_getname(r, &cp, ln, ppos);
1.67 kristaps 1690:
1.106 kristaps 1691: t = (r->current_string = roff_getstrn(r, mac, maclen))
1.155 kristaps 1692: ? ROFF_USERDEF : roffhash_find(mac, maclen);
1.67 kristaps 1693:
1.214 schwarze 1694: if (ROFF_MAX != t)
1695: *pos = cp - buf;
1.67 kristaps 1696:
1697: return(t);
1698: }
1699:
1.266 schwarze 1700: /* --- handling of request blocks ----------------------------------------- */
1701:
1.67 kristaps 1702: static enum rofferr
1.76 kristaps 1703: roff_cblock(ROFF_ARGS)
1.67 kristaps 1704: {
1705:
1.79 kristaps 1706: /*
1707: * A block-close `..' should only be invoked as a child of an
1708: * ignore macro, otherwise raise a warning and just ignore it.
1709: */
1710:
1.238 schwarze 1711: if (r->last == NULL) {
1.221 schwarze 1712: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1713: ln, ppos, "..");
1.76 kristaps 1714: return(ROFF_IGN);
1715: }
1.67 kristaps 1716:
1.81 kristaps 1717: switch (r->last->tok) {
1.207 schwarze 1718: case ROFF_am:
1.220 schwarze 1719: /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1.81 kristaps 1720: /* FALLTHROUGH */
1.207 schwarze 1721: case ROFF_ami:
1.81 kristaps 1722: /* FALLTHROUGH */
1.207 schwarze 1723: case ROFF_de:
1.108 schwarze 1724: /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1.81 kristaps 1725: /* FALLTHROUGH */
1.207 schwarze 1726: case ROFF_dei:
1.81 kristaps 1727: /* FALLTHROUGH */
1.207 schwarze 1728: case ROFF_ig:
1.81 kristaps 1729: break;
1730: default:
1.221 schwarze 1731: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1732: ln, ppos, "..");
1.67 kristaps 1733: return(ROFF_IGN);
1.76 kristaps 1734: }
1.67 kristaps 1735:
1.238 schwarze 1736: if (buf->buf[pos] != '\0')
1.217 schwarze 1737: mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
1.238 schwarze 1738: ".. %s", buf->buf + pos);
1.71 kristaps 1739:
1740: roffnode_pop(r);
1.76 kristaps 1741: roffnode_cleanscope(r);
1742: return(ROFF_IGN);
1.71 kristaps 1743:
1.67 kristaps 1744: }
1745:
1.76 kristaps 1746: static void
1747: roffnode_cleanscope(struct roff *r)
1.67 kristaps 1748: {
1749:
1.76 kristaps 1750: while (r->last) {
1.173 schwarze 1751: if (--r->last->endspan != 0)
1.76 kristaps 1752: break;
1753: roffnode_pop(r);
1754: }
1.67 kristaps 1755: }
1756:
1.195 schwarze 1757: static void
1758: roff_ccond(struct roff *r, int ln, int ppos)
1.74 kristaps 1759: {
1760:
1.76 kristaps 1761: if (NULL == r->last) {
1.221 schwarze 1762: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1763: ln, ppos, "\\}");
1.195 schwarze 1764: return;
1.76 kristaps 1765: }
1766:
1.82 kristaps 1767: switch (r->last->tok) {
1.207 schwarze 1768: case ROFF_el:
1.82 kristaps 1769: /* FALLTHROUGH */
1.207 schwarze 1770: case ROFF_ie:
1.82 kristaps 1771: /* FALLTHROUGH */
1.207 schwarze 1772: case ROFF_if:
1.82 kristaps 1773: break;
1774: default:
1.221 schwarze 1775: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1776: ln, ppos, "\\}");
1.195 schwarze 1777: return;
1.75 kristaps 1778: }
1779:
1.76 kristaps 1780: if (r->last->endspan > -1) {
1.221 schwarze 1781: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1782: ln, ppos, "\\}");
1.195 schwarze 1783: return;
1.76 kristaps 1784: }
1785:
1.75 kristaps 1786: roffnode_pop(r);
1.76 kristaps 1787: roffnode_cleanscope(r);
1.195 schwarze 1788: return;
1.76 kristaps 1789: }
1790:
1791: static enum rofferr
1.80 kristaps 1792: roff_block(ROFF_ARGS)
1.76 kristaps 1793: {
1.220 schwarze 1794: const char *name;
1795: char *iname, *cp;
1.213 schwarze 1796: size_t namesz;
1.106 kristaps 1797:
1.220 schwarze 1798: /* Ignore groff compatibility mode for now. */
1.76 kristaps 1799:
1.238 schwarze 1800: if (tok == ROFF_de1)
1.220 schwarze 1801: tok = ROFF_de;
1.251 schwarze 1802: else if (tok == ROFF_dei1)
1803: tok = ROFF_dei;
1.238 schwarze 1804: else if (tok == ROFF_am1)
1.220 schwarze 1805: tok = ROFF_am;
1.251 schwarze 1806: else if (tok == ROFF_ami1)
1807: tok = ROFF_ami;
1.220 schwarze 1808:
1809: /* Parse the macro name argument. */
1810:
1.238 schwarze 1811: cp = buf->buf + pos;
1812: if (tok == ROFF_ig) {
1.220 schwarze 1813: iname = NULL;
1814: namesz = 0;
1815: } else {
1816: iname = cp;
1817: namesz = roff_getname(r, &cp, ln, ppos);
1818: iname[namesz] = '\0';
1819: }
1.107 kristaps 1820:
1.220 schwarze 1821: /* Resolve the macro name argument if it is indirect. */
1.107 kristaps 1822:
1.238 schwarze 1823: if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1824: if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1.220 schwarze 1825: mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238 schwarze 1826: r->parse, ln, (int)(iname - buf->buf),
1.220 schwarze 1827: "%.*s", (int)namesz, iname);
1828: namesz = 0;
1829: } else
1830: namesz = strlen(name);
1831: } else
1832: name = iname;
1.107 kristaps 1833:
1.238 schwarze 1834: if (namesz == 0 && tok != ROFF_ig) {
1.220 schwarze 1835: mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse,
1836: ln, ppos, roffs[tok].name);
1837: return(ROFF_IGN);
1838: }
1.80 kristaps 1839:
1.106 kristaps 1840: roffnode_push(r, tok, name, ln, ppos);
1841:
1842: /*
1843: * At the beginning of a `de' macro, clear the existing string
1844: * with the same name, if there is one. New content will be
1.193 schwarze 1845: * appended from roff_block_text() in multiline mode.
1.106 kristaps 1846: */
1.107 kristaps 1847:
1.238 schwarze 1848: if (tok == ROFF_de || tok == ROFF_dei)
1.213 schwarze 1849: roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
1.76 kristaps 1850:
1.238 schwarze 1851: if (*cp == '\0')
1.78 kristaps 1852: return(ROFF_IGN);
1853:
1.220 schwarze 1854: /* Get the custom end marker. */
1.107 kristaps 1855:
1.220 schwarze 1856: iname = cp;
1.213 schwarze 1857: namesz = roff_getname(r, &cp, ln, ppos);
1.220 schwarze 1858:
1859: /* Resolve the end marker if it is indirect. */
1860:
1.238 schwarze 1861: if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1862: if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1.220 schwarze 1863: mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238 schwarze 1864: r->parse, ln, (int)(iname - buf->buf),
1.220 schwarze 1865: "%.*s", (int)namesz, iname);
1866: namesz = 0;
1867: } else
1868: namesz = strlen(name);
1869: } else
1870: name = iname;
1871:
1.213 schwarze 1872: if (namesz)
1873: r->last->end = mandoc_strndup(name, namesz);
1.78 kristaps 1874:
1.238 schwarze 1875: if (*cp != '\0')
1.217 schwarze 1876: mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
1877: ln, pos, ".%s ... %s", roffs[tok].name, cp);
1.74 kristaps 1878:
1.78 kristaps 1879: return(ROFF_IGN);
1880: }
1881:
1882: static enum rofferr
1.80 kristaps 1883: roff_block_sub(ROFF_ARGS)
1.79 kristaps 1884: {
1885: enum rofft t;
1886: int i, j;
1887:
1888: /*
1889: * First check whether a custom macro exists at this level. If
1890: * it does, then check against it. This is some of groff's
1891: * stranger behaviours. If we encountered a custom end-scope
1892: * tag and that tag also happens to be a "real" macro, then we
1893: * need to try interpreting it again as a real macro. If it's
1894: * not, then return ignore. Else continue.
1895: */
1896:
1897: if (r->last->end) {
1.130 kristaps 1898: for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.238 schwarze 1899: if (buf->buf[i] != r->last->end[j])
1.79 kristaps 1900: break;
1901:
1.238 schwarze 1902: if (r->last->end[j] == '\0' &&
1903: (buf->buf[i] == '\0' ||
1904: buf->buf[i] == ' ' ||
1905: buf->buf[i] == '\t')) {
1.79 kristaps 1906: roffnode_pop(r);
1907: roffnode_cleanscope(r);
1908:
1.238 schwarze 1909: while (buf->buf[i] == ' ' || buf->buf[i] == '\t')
1.130 kristaps 1910: i++;
1911:
1912: pos = i;
1.238 schwarze 1913: if (roff_parse(r, buf->buf, &pos, ln, ppos) !=
1914: ROFF_MAX)
1.79 kristaps 1915: return(ROFF_RERUN);
1916: return(ROFF_IGN);
1917: }
1918: }
1919:
1920: /*
1921: * If we have no custom end-query or lookup failed, then try
1922: * pulling it out of the hashtable.
1923: */
1924:
1.238 schwarze 1925: t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.79 kristaps 1926:
1.238 schwarze 1927: if (t != ROFF_cblock) {
1928: if (tok != ROFF_ig)
1929: roff_setstr(r, r->last->name, buf->buf + ppos, 2);
1.79 kristaps 1930: return(ROFF_IGN);
1.106 kristaps 1931: }
1.79 kristaps 1932:
1933: assert(roffs[t].proc);
1.238 schwarze 1934: return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.79 kristaps 1935: }
1936:
1937: static enum rofferr
1.80 kristaps 1938: roff_block_text(ROFF_ARGS)
1.78 kristaps 1939: {
1940:
1.238 schwarze 1941: if (tok != ROFF_ig)
1942: roff_setstr(r, r->last->name, buf->buf + pos, 2);
1.106 kristaps 1943:
1.78 kristaps 1944: return(ROFF_IGN);
1945: }
1946:
1947: static enum rofferr
1.82 kristaps 1948: roff_cond_sub(ROFF_ARGS)
1949: {
1950: enum rofft t;
1.139 kristaps 1951: char *ep;
1.198 schwarze 1952: int rr;
1.82 kristaps 1953:
1954: rr = r->last->rule;
1.139 kristaps 1955: roffnode_cleanscope(r);
1.238 schwarze 1956: t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.82 kristaps 1957:
1.139 kristaps 1958: /*
1.177 schwarze 1959: * Fully handle known macros when they are structurally
1960: * required or when the conditional evaluated to true.
1.87 kristaps 1961: */
1962:
1.238 schwarze 1963: if ((t != ROFF_MAX) &&
1964: (rr || roffs[t].flags & ROFFMAC_STRUCT)) {
1.177 schwarze 1965: assert(roffs[t].proc);
1.238 schwarze 1966: return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.177 schwarze 1967: }
1.144 kristaps 1968:
1.196 schwarze 1969: /*
1970: * If `\}' occurs on a macro line without a preceding macro,
1971: * drop the line completely.
1972: */
1973:
1.238 schwarze 1974: ep = buf->buf + pos;
1975: if (ep[0] == '\\' && ep[1] == '}')
1.198 schwarze 1976: rr = 0;
1.196 schwarze 1977:
1.177 schwarze 1978: /* Always check for the closing delimiter `\}'. */
1.144 kristaps 1979:
1.238 schwarze 1980: while ((ep = strchr(ep, '\\')) != NULL) {
1981: if (*(++ep) == '}') {
1.197 schwarze 1982: *ep = '&';
1.238 schwarze 1983: roff_ccond(r, ln, ep - buf->buf - 1);
1.197 schwarze 1984: }
1.247 schwarze 1985: if (*ep != '\0')
1986: ++ep;
1.177 schwarze 1987: }
1.198 schwarze 1988: return(rr ? ROFF_CONT : ROFF_IGN);
1.82 kristaps 1989: }
1990:
1991: static enum rofferr
1992: roff_cond_text(ROFF_ARGS)
1.78 kristaps 1993: {
1.140 kristaps 1994: char *ep;
1.198 schwarze 1995: int rr;
1.82 kristaps 1996:
1997: rr = r->last->rule;
1.140 kristaps 1998: roffnode_cleanscope(r);
1.82 kristaps 1999:
1.238 schwarze 2000: ep = buf->buf + pos;
2001: while ((ep = strchr(ep, '\\')) != NULL) {
2002: if (*(++ep) == '}') {
1.197 schwarze 2003: *ep = '&';
1.238 schwarze 2004: roff_ccond(r, ln, ep - buf->buf - 1);
1.197 schwarze 2005: }
1.247 schwarze 2006: if (*ep != '\0')
2007: ++ep;
1.78 kristaps 2008: }
1.198 schwarze 2009: return(rr ? ROFF_CONT : ROFF_IGN);
1.74 kristaps 2010: }
2011:
1.266 schwarze 2012: /* --- handling of numeric and conditional expressions -------------------- */
2013:
1.204 schwarze 2014: /*
2015: * Parse a single signed integer number. Stop at the first non-digit.
2016: * If there is at least one digit, return success and advance the
2017: * parse point, else return failure and let the parse point unchanged.
2018: * Ignore overflows, treat them just like the C language.
2019: */
1.184 schwarze 2020: static int
1.261 schwarze 2021: roff_getnum(const char *v, int *pos, int *res, int flags)
1.184 schwarze 2022: {
1.261 schwarze 2023: int myres, scaled, n, p;
1.206 schwarze 2024:
2025: if (NULL == res)
2026: res = &myres;
1.184 schwarze 2027:
2028: p = *pos;
2029: n = v[p] == '-';
1.261 schwarze 2030: if (n || v[p] == '+')
1.184 schwarze 2031: p++;
2032:
1.261 schwarze 2033: if (flags & ROFFNUM_WHITE)
2034: while (isspace((unsigned char)v[p]))
2035: p++;
2036:
1.184 schwarze 2037: for (*res = 0; isdigit((unsigned char)v[p]); p++)
1.204 schwarze 2038: *res = 10 * *res + v[p] - '0';
1.184 schwarze 2039: if (p == *pos + n)
2040: return 0;
2041:
2042: if (n)
2043: *res = -*res;
2044:
1.254 schwarze 2045: /* Each number may be followed by one optional scaling unit. */
2046:
2047: switch (v[p]) {
2048: case 'f':
1.261 schwarze 2049: scaled = *res * 65536;
1.254 schwarze 2050: break;
2051: case 'i':
1.261 schwarze 2052: scaled = *res * 240;
1.254 schwarze 2053: break;
2054: case 'c':
1.261 schwarze 2055: scaled = *res * 240 / 2.54;
1.254 schwarze 2056: break;
2057: case 'v':
2058: /* FALLTROUGH */
2059: case 'P':
1.261 schwarze 2060: scaled = *res * 40;
1.254 schwarze 2061: break;
2062: case 'm':
2063: /* FALLTROUGH */
2064: case 'n':
1.261 schwarze 2065: scaled = *res * 24;
1.254 schwarze 2066: break;
2067: case 'p':
1.261 schwarze 2068: scaled = *res * 10 / 3;
1.254 schwarze 2069: break;
2070: case 'u':
1.261 schwarze 2071: scaled = *res;
1.254 schwarze 2072: break;
2073: case 'M':
1.261 schwarze 2074: scaled = *res * 6 / 25;
1.254 schwarze 2075: break;
2076: default:
1.261 schwarze 2077: scaled = *res;
1.254 schwarze 2078: p--;
2079: break;
2080: }
1.261 schwarze 2081: if (flags & ROFFNUM_SCALE)
2082: *res = scaled;
1.254 schwarze 2083:
2084: *pos = p + 1;
2085: return(1);
1.184 schwarze 2086: }
2087:
1.198 schwarze 2088: /*
2089: * Evaluate a string comparison condition.
2090: * The first character is the delimiter.
2091: * Succeed if the string up to its second occurrence
2092: * matches the string up to its third occurence.
2093: * Advance the cursor after the third occurrence
2094: * or lacking that, to the end of the line.
2095: */
2096: static int
2097: roff_evalstrcond(const char *v, int *pos)
2098: {
2099: const char *s1, *s2, *s3;
2100: int match;
2101:
2102: match = 0;
2103: s1 = v + *pos; /* initial delimiter */
2104: s2 = s1 + 1; /* for scanning the first string */
2105: s3 = strchr(s2, *s1); /* for scanning the second string */
2106:
2107: if (NULL == s3) /* found no middle delimiter */
2108: goto out;
2109:
2110: while ('\0' != *++s3) {
2111: if (*s2 != *s3) { /* mismatch */
2112: s3 = strchr(s3, *s1);
2113: break;
2114: }
2115: if (*s3 == *s1) { /* found the final delimiter */
2116: match = 1;
2117: break;
2118: }
2119: s2++;
2120: }
2121:
2122: out:
2123: if (NULL == s3)
2124: s3 = strchr(s2, '\0');
1.242 schwarze 2125: else if (*s3 != '\0')
1.198 schwarze 2126: s3++;
2127: *pos = s3 - v;
2128: return(match);
2129: }
2130:
1.204 schwarze 2131: /*
2132: * Evaluate an optionally negated single character, numerical,
2133: * or string condition.
2134: */
1.198 schwarze 2135: static int
1.234 kristaps 2136: roff_evalcond(struct roff *r, int ln, const char *v, int *pos)
1.88 kristaps 2137: {
1.241 schwarze 2138: int number, savepos, wanttrue;
1.88 kristaps 2139:
1.198 schwarze 2140: if ('!' == v[*pos]) {
2141: wanttrue = 0;
2142: (*pos)++;
2143: } else
2144: wanttrue = 1;
2145:
1.88 kristaps 2146: switch (v[*pos]) {
1.240 schwarze 2147: case '\0':
2148: return(0);
1.207 schwarze 2149: case 'n':
1.198 schwarze 2150: /* FALLTHROUGH */
1.207 schwarze 2151: case 'o':
1.88 kristaps 2152: (*pos)++;
1.198 schwarze 2153: return(wanttrue);
1.207 schwarze 2154: case 'c':
1.198 schwarze 2155: /* FALLTHROUGH */
1.207 schwarze 2156: case 'd':
1.198 schwarze 2157: /* FALLTHROUGH */
1.207 schwarze 2158: case 'e':
1.88 kristaps 2159: /* FALLTHROUGH */
1.207 schwarze 2160: case 'r':
1.88 kristaps 2161: /* FALLTHROUGH */
1.207 schwarze 2162: case 't':
1.239 schwarze 2163: /* FALLTHROUGH */
2164: case 'v':
1.88 kristaps 2165: (*pos)++;
1.198 schwarze 2166: return(!wanttrue);
1.88 kristaps 2167: default:
2168: break;
2169: }
2170:
1.241 schwarze 2171: savepos = *pos;
1.261 schwarze 2172: if (roff_evalnum(r, ln, v, pos, &number, ROFFNUM_SCALE))
1.204 schwarze 2173: return((number > 0) == wanttrue);
1.241 schwarze 2174: else if (*pos == savepos)
2175: return(roff_evalstrcond(v, pos) == wanttrue);
1.204 schwarze 2176: else
1.241 schwarze 2177: return (0);
1.88 kristaps 2178: }
2179:
1.74 kristaps 2180: static enum rofferr
1.103 kristaps 2181: roff_line_ignore(ROFF_ARGS)
1.89 kristaps 2182: {
1.123 schwarze 2183:
1.89 kristaps 2184: return(ROFF_IGN);
2185: }
2186:
1.104 kristaps 2187: static enum rofferr
1.251 schwarze 2188: roff_insec(ROFF_ARGS)
2189: {
2190:
2191: mandoc_msg(MANDOCERR_REQ_INSEC, r->parse,
2192: ln, ppos, roffs[tok].name);
2193: return(ROFF_IGN);
2194: }
2195:
2196: static enum rofferr
2197: roff_unsupp(ROFF_ARGS)
2198: {
2199:
2200: mandoc_msg(MANDOCERR_REQ_UNSUPP, r->parse,
2201: ln, ppos, roffs[tok].name);
2202: return(ROFF_IGN);
2203: }
2204:
2205: static enum rofferr
1.82 kristaps 2206: roff_cond(ROFF_ARGS)
1.74 kristaps 2207: {
1.173 schwarze 2208:
2209: roffnode_push(r, tok, NULL, ln, ppos);
1.74 kristaps 2210:
1.207 schwarze 2211: /*
1.134 kristaps 2212: * An `.el' has no conditional body: it will consume the value
2213: * of the current rstack entry set in prior `ie' calls or
1.207 schwarze 2214: * defaults to DENY.
1.134 kristaps 2215: *
2216: * If we're not an `el', however, then evaluate the conditional.
2217: */
1.133 kristaps 2218:
1.238 schwarze 2219: r->last->rule = tok == ROFF_el ?
1.207 schwarze 2220: (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
1.238 schwarze 2221: roff_evalcond(r, ln, buf->buf, &pos);
1.77 kristaps 2222:
1.134 kristaps 2223: /*
2224: * An if-else will put the NEGATION of the current evaluated
2225: * conditional into the stack of rules.
2226: */
2227:
1.238 schwarze 2228: if (tok == ROFF_ie) {
1.223 schwarze 2229: if (r->rstackpos + 1 == r->rstacksz) {
2230: r->rstacksz += 16;
2231: r->rstack = mandoc_reallocarray(r->rstack,
2232: r->rstacksz, sizeof(int));
1.134 kristaps 2233: }
1.198 schwarze 2234: r->rstack[++r->rstackpos] = !r->last->rule;
1.82 kristaps 2235: }
1.88 kristaps 2236:
2237: /* If the parent has false as its rule, then so do we. */
2238:
1.198 schwarze 2239: if (r->last->parent && !r->last->parent->rule)
2240: r->last->rule = 0;
1.88 kristaps 2241:
2242: /*
1.173 schwarze 2243: * Determine scope.
2244: * If there is nothing on the line after the conditional,
2245: * not even whitespace, use next-line scope.
1.88 kristaps 2246: */
1.74 kristaps 2247:
1.238 schwarze 2248: if (buf->buf[pos] == '\0') {
1.173 schwarze 2249: r->last->endspan = 2;
2250: goto out;
2251: }
2252:
1.238 schwarze 2253: while (buf->buf[pos] == ' ')
1.173 schwarze 2254: pos++;
2255:
2256: /* An opening brace requests multiline scope. */
1.75 kristaps 2257:
1.238 schwarze 2258: if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
1.75 kristaps 2259: r->last->endspan = -1;
2260: pos += 2;
1.173 schwarze 2261: goto out;
1.207 schwarze 2262: }
1.74 kristaps 2263:
1.77 kristaps 2264: /*
1.173 schwarze 2265: * Anything else following the conditional causes
2266: * single-line scope. Warn if the scope contains
2267: * nothing but trailing whitespace.
1.77 kristaps 2268: */
2269:
1.238 schwarze 2270: if (buf->buf[pos] == '\0')
1.216 schwarze 2271: mandoc_msg(MANDOCERR_COND_EMPTY, r->parse,
2272: ln, ppos, roffs[tok].name);
1.77 kristaps 2273:
1.173 schwarze 2274: r->last->endspan = 1;
1.74 kristaps 2275:
1.173 schwarze 2276: out:
1.75 kristaps 2277: *offs = pos;
2278: return(ROFF_RERUN);
1.83 schwarze 2279: }
2280:
2281: static enum rofferr
1.92 schwarze 2282: roff_ds(ROFF_ARGS)
2283: {
1.212 schwarze 2284: char *string;
2285: const char *name;
2286: size_t namesz;
1.96 kristaps 2287:
1.251 schwarze 2288: /* Ignore groff compatibility mode for now. */
2289:
2290: if (tok == ROFF_ds1)
2291: tok = ROFF_ds;
2292: else if (tok == ROFF_as1)
2293: tok = ROFF_as;
2294:
1.96 kristaps 2295: /*
1.212 schwarze 2296: * The first word is the name of the string.
2297: * If it is empty or terminated by an escape sequence,
2298: * abort the `ds' request without defining anything.
1.96 kristaps 2299: */
1.92 schwarze 2300:
1.238 schwarze 2301: name = string = buf->buf + pos;
2302: if (*name == '\0')
1.92 schwarze 2303: return(ROFF_IGN);
2304:
1.212 schwarze 2305: namesz = roff_getname(r, &string, ln, pos);
1.238 schwarze 2306: if (name[namesz] == '\\')
1.212 schwarze 2307: return(ROFF_IGN);
2308:
2309: /* Read past the initial double-quote, if any. */
1.238 schwarze 2310: if (*string == '"')
1.92 schwarze 2311: string++;
2312:
1.96 kristaps 2313: /* The rest is the value. */
1.212 schwarze 2314: roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
2315: ROFF_as == tok);
1.92 schwarze 2316: return(ROFF_IGN);
2317: }
2318:
1.204 schwarze 2319: /*
2320: * Parse a single operator, one or two characters long.
2321: * If the operator is recognized, return success and advance the
2322: * parse point, else return failure and let the parse point unchanged.
2323: */
2324: static int
2325: roff_getop(const char *v, int *pos, char *res)
2326: {
2327:
2328: *res = v[*pos];
2329:
2330: switch (*res) {
1.207 schwarze 2331: case '+':
1.204 schwarze 2332: /* FALLTHROUGH */
1.207 schwarze 2333: case '-':
1.204 schwarze 2334: /* FALLTHROUGH */
1.207 schwarze 2335: case '*':
1.204 schwarze 2336: /* FALLTHROUGH */
1.207 schwarze 2337: case '/':
1.204 schwarze 2338: /* FALLTHROUGH */
1.207 schwarze 2339: case '%':
1.204 schwarze 2340: /* FALLTHROUGH */
1.207 schwarze 2341: case '&':
1.204 schwarze 2342: /* FALLTHROUGH */
1.207 schwarze 2343: case ':':
1.204 schwarze 2344: break;
2345: case '<':
2346: switch (v[*pos + 1]) {
1.207 schwarze 2347: case '=':
1.204 schwarze 2348: *res = 'l';
2349: (*pos)++;
2350: break;
1.207 schwarze 2351: case '>':
1.204 schwarze 2352: *res = '!';
2353: (*pos)++;
2354: break;
1.207 schwarze 2355: case '?':
1.204 schwarze 2356: *res = 'i';
2357: (*pos)++;
2358: break;
2359: default:
2360: break;
2361: }
2362: break;
2363: case '>':
2364: switch (v[*pos + 1]) {
1.207 schwarze 2365: case '=':
1.204 schwarze 2366: *res = 'g';
2367: (*pos)++;
2368: break;
1.207 schwarze 2369: case '?':
1.204 schwarze 2370: *res = 'a';
2371: (*pos)++;
2372: break;
2373: default:
2374: break;
2375: }
2376: break;
2377: case '=':
2378: if ('=' == v[*pos + 1])
2379: (*pos)++;
2380: break;
2381: default:
2382: return(0);
2383: }
2384: (*pos)++;
2385:
2386: return(*res);
2387: }
2388:
2389: /*
2390: * Evaluate either a parenthesized numeric expression
2391: * or a single signed integer number.
2392: */
2393: static int
1.235 schwarze 2394: roff_evalpar(struct roff *r, int ln,
1.261 schwarze 2395: const char *v, int *pos, int *res, int flags)
1.204 schwarze 2396: {
2397:
2398: if ('(' != v[*pos])
1.261 schwarze 2399: return(roff_getnum(v, pos, res, flags));
1.204 schwarze 2400:
2401: (*pos)++;
1.261 schwarze 2402: if ( ! roff_evalnum(r, ln, v, pos, res, flags | ROFFNUM_WHITE))
1.204 schwarze 2403: return(0);
2404:
1.206 schwarze 2405: /*
2406: * Omission of the closing parenthesis
2407: * is an error in validation mode,
2408: * but ignored in evaluation mode.
2409: */
2410:
1.204 schwarze 2411: if (')' == v[*pos])
2412: (*pos)++;
1.206 schwarze 2413: else if (NULL == res)
2414: return(0);
1.204 schwarze 2415:
2416: return(1);
2417: }
2418:
2419: /*
2420: * Evaluate a complete numeric expression.
2421: * Proceed left to right, there is no concept of precedence.
2422: */
2423: static int
1.235 schwarze 2424: roff_evalnum(struct roff *r, int ln, const char *v,
1.261 schwarze 2425: int *pos, int *res, int flags)
1.204 schwarze 2426: {
2427: int mypos, operand2;
2428: char operator;
2429:
2430: if (NULL == pos) {
2431: mypos = 0;
2432: pos = &mypos;
2433: }
2434:
1.261 schwarze 2435: if (flags & ROFFNUM_WHITE)
1.204 schwarze 2436: while (isspace((unsigned char)v[*pos]))
2437: (*pos)++;
2438:
1.261 schwarze 2439: if ( ! roff_evalpar(r, ln, v, pos, res, flags))
1.204 schwarze 2440: return(0);
2441:
2442: while (1) {
1.261 schwarze 2443: if (flags & ROFFNUM_WHITE)
1.204 schwarze 2444: while (isspace((unsigned char)v[*pos]))
2445: (*pos)++;
2446:
2447: if ( ! roff_getop(v, pos, &operator))
2448: break;
2449:
1.261 schwarze 2450: if (flags & ROFFNUM_WHITE)
1.204 schwarze 2451: while (isspace((unsigned char)v[*pos]))
2452: (*pos)++;
2453:
1.261 schwarze 2454: if ( ! roff_evalpar(r, ln, v, pos, &operand2, flags))
1.204 schwarze 2455: return(0);
2456:
1.261 schwarze 2457: if (flags & ROFFNUM_WHITE)
1.204 schwarze 2458: while (isspace((unsigned char)v[*pos]))
2459: (*pos)++;
1.206 schwarze 2460:
2461: if (NULL == res)
2462: continue;
1.204 schwarze 2463:
2464: switch (operator) {
1.207 schwarze 2465: case '+':
1.204 schwarze 2466: *res += operand2;
2467: break;
1.207 schwarze 2468: case '-':
1.204 schwarze 2469: *res -= operand2;
2470: break;
1.207 schwarze 2471: case '*':
1.204 schwarze 2472: *res *= operand2;
2473: break;
1.207 schwarze 2474: case '/':
1.244 schwarze 2475: if (operand2 == 0) {
1.235 schwarze 2476: mandoc_msg(MANDOCERR_DIVZERO,
1.234 kristaps 2477: r->parse, ln, *pos, v);
2478: *res = 0;
2479: break;
2480: }
1.204 schwarze 2481: *res /= operand2;
2482: break;
1.207 schwarze 2483: case '%':
1.244 schwarze 2484: if (operand2 == 0) {
2485: mandoc_msg(MANDOCERR_DIVZERO,
2486: r->parse, ln, *pos, v);
2487: *res = 0;
2488: break;
2489: }
1.204 schwarze 2490: *res %= operand2;
2491: break;
1.207 schwarze 2492: case '<':
1.204 schwarze 2493: *res = *res < operand2;
2494: break;
1.207 schwarze 2495: case '>':
1.204 schwarze 2496: *res = *res > operand2;
2497: break;
1.207 schwarze 2498: case 'l':
1.204 schwarze 2499: *res = *res <= operand2;
2500: break;
1.207 schwarze 2501: case 'g':
1.204 schwarze 2502: *res = *res >= operand2;
2503: break;
1.207 schwarze 2504: case '=':
1.204 schwarze 2505: *res = *res == operand2;
2506: break;
1.207 schwarze 2507: case '!':
1.204 schwarze 2508: *res = *res != operand2;
2509: break;
1.207 schwarze 2510: case '&':
1.204 schwarze 2511: *res = *res && operand2;
2512: break;
1.207 schwarze 2513: case ':':
1.204 schwarze 2514: *res = *res || operand2;
2515: break;
1.207 schwarze 2516: case 'i':
1.204 schwarze 2517: if (operand2 < *res)
2518: *res = operand2;
2519: break;
1.207 schwarze 2520: case 'a':
1.204 schwarze 2521: if (operand2 > *res)
2522: *res = operand2;
2523: break;
2524: default:
2525: abort();
2526: }
2527: }
2528: return(1);
2529: }
2530:
1.266 schwarze 2531: /* --- register management ------------------------------------------------ */
2532:
1.180 schwarze 2533: void
1.187 schwarze 2534: roff_setreg(struct roff *r, const char *name, int val, char sign)
1.147 kristaps 2535: {
1.180 schwarze 2536: struct roffreg *reg;
2537:
2538: /* Search for an existing register with the same name. */
2539: reg = r->regtab;
2540:
2541: while (reg && strcmp(name, reg->key.p))
2542: reg = reg->next;
1.147 kristaps 2543:
1.180 schwarze 2544: if (NULL == reg) {
2545: /* Create a new register. */
2546: reg = mandoc_malloc(sizeof(struct roffreg));
2547: reg->key.p = mandoc_strdup(name);
2548: reg->key.sz = strlen(name);
1.187 schwarze 2549: reg->val = 0;
1.180 schwarze 2550: reg->next = r->regtab;
2551: r->regtab = reg;
2552: }
2553:
1.187 schwarze 2554: if ('+' == sign)
2555: reg->val += val;
2556: else if ('-' == sign)
2557: reg->val -= val;
2558: else
2559: reg->val = val;
1.147 kristaps 2560: }
2561:
1.192 schwarze 2562: /*
2563: * Handle some predefined read-only number registers.
2564: * For now, return -1 if the requested register is not predefined;
2565: * in case a predefined read-only register having the value -1
2566: * were to turn up, another special value would have to be chosen.
2567: */
2568: static int
2569: roff_getregro(const char *name)
2570: {
2571:
2572: switch (*name) {
1.207 schwarze 2573: case 'A': /* ASCII approximation mode is always off. */
1.192 schwarze 2574: return(0);
1.207 schwarze 2575: case 'g': /* Groff compatibility mode is always on. */
1.192 schwarze 2576: return(1);
1.207 schwarze 2577: case 'H': /* Fixed horizontal resolution. */
1.192 schwarze 2578: return (24);
1.207 schwarze 2579: case 'j': /* Always adjust left margin only. */
1.192 schwarze 2580: return(0);
1.207 schwarze 2581: case 'T': /* Some output device is always defined. */
1.192 schwarze 2582: return(1);
1.207 schwarze 2583: case 'V': /* Fixed vertical resolution. */
1.192 schwarze 2584: return (40);
2585: default:
2586: return (-1);
2587: }
2588: }
2589:
1.181 schwarze 2590: int
1.180 schwarze 2591: roff_getreg(const struct roff *r, const char *name)
1.147 kristaps 2592: {
1.180 schwarze 2593: struct roffreg *reg;
1.192 schwarze 2594: int val;
2595:
2596: if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) {
2597: val = roff_getregro(name + 1);
2598: if (-1 != val)
2599: return (val);
2600: }
1.180 schwarze 2601:
2602: for (reg = r->regtab; reg; reg = reg->next)
2603: if (0 == strcmp(name, reg->key.p))
1.181 schwarze 2604: return(reg->val);
2605:
2606: return(0);
2607: }
2608:
2609: static int
2610: roff_getregn(const struct roff *r, const char *name, size_t len)
2611: {
2612: struct roffreg *reg;
1.192 schwarze 2613: int val;
2614:
2615: if ('.' == name[0] && 2 == len) {
2616: val = roff_getregro(name + 1);
2617: if (-1 != val)
2618: return (val);
2619: }
1.181 schwarze 2620:
2621: for (reg = r->regtab; reg; reg = reg->next)
2622: if (len == reg->key.sz &&
2623: 0 == strncmp(name, reg->key.p, len))
2624: return(reg->val);
1.147 kristaps 2625:
1.180 schwarze 2626: return(0);
1.147 kristaps 2627: }
2628:
1.180 schwarze 2629: static void
2630: roff_freereg(struct roffreg *reg)
1.147 kristaps 2631: {
1.180 schwarze 2632: struct roffreg *old_reg;
1.147 kristaps 2633:
1.180 schwarze 2634: while (NULL != reg) {
2635: free(reg->key.p);
2636: old_reg = reg;
2637: reg = reg->next;
2638: free(old_reg);
2639: }
1.147 kristaps 2640: }
1.92 schwarze 2641:
2642: static enum rofferr
1.89 kristaps 2643: roff_nr(ROFF_ARGS)
1.83 schwarze 2644: {
1.212 schwarze 2645: char *key, *val;
2646: size_t keysz;
1.138 kristaps 2647: int iv;
1.187 schwarze 2648: char sign;
1.89 kristaps 2649:
1.238 schwarze 2650: key = val = buf->buf + pos;
2651: if (*key == '\0')
1.212 schwarze 2652: return(ROFF_IGN);
2653:
2654: keysz = roff_getname(r, &val, ln, pos);
1.238 schwarze 2655: if (key[keysz] == '\\')
1.212 schwarze 2656: return(ROFF_IGN);
2657: key[keysz] = '\0';
1.89 kristaps 2658:
1.187 schwarze 2659: sign = *val;
1.238 schwarze 2660: if (sign == '+' || sign == '-')
1.187 schwarze 2661: val++;
2662:
1.261 schwarze 2663: if (roff_evalnum(r, ln, val, NULL, &iv, ROFFNUM_SCALE))
1.204 schwarze 2664: roff_setreg(r, key, iv, sign);
1.109 kristaps 2665:
1.203 schwarze 2666: return(ROFF_IGN);
2667: }
2668:
2669: static enum rofferr
2670: roff_rr(ROFF_ARGS)
2671: {
2672: struct roffreg *reg, **prev;
1.212 schwarze 2673: char *name, *cp;
2674: size_t namesz;
1.203 schwarze 2675:
1.238 schwarze 2676: name = cp = buf->buf + pos;
2677: if (*name == '\0')
1.212 schwarze 2678: return(ROFF_IGN);
2679: namesz = roff_getname(r, &cp, ln, pos);
2680: name[namesz] = '\0';
1.203 schwarze 2681:
2682: prev = &r->regtab;
2683: while (1) {
2684: reg = *prev;
1.238 schwarze 2685: if (reg == NULL || !strcmp(name, reg->key.p))
1.203 schwarze 2686: break;
2687: prev = ®->next;
2688: }
1.238 schwarze 2689: if (reg != NULL) {
1.203 schwarze 2690: *prev = reg->next;
2691: free(reg->key.p);
2692: free(reg);
2693: }
1.122 schwarze 2694: return(ROFF_IGN);
2695: }
2696:
1.266 schwarze 2697: /* --- handler functions for roff requests -------------------------------- */
2698:
1.122 schwarze 2699: static enum rofferr
2700: roff_rm(ROFF_ARGS)
2701: {
2702: const char *name;
2703: char *cp;
1.212 schwarze 2704: size_t namesz;
1.122 schwarze 2705:
1.238 schwarze 2706: cp = buf->buf + pos;
2707: while (*cp != '\0') {
1.212 schwarze 2708: name = cp;
1.238 schwarze 2709: namesz = roff_getname(r, &cp, ln, (int)(cp - buf->buf));
1.212 schwarze 2710: roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
1.238 schwarze 2711: if (name[namesz] == '\\')
1.212 schwarze 2712: break;
1.122 schwarze 2713: }
1.178 schwarze 2714: return(ROFF_IGN);
2715: }
2716:
2717: static enum rofferr
2718: roff_it(ROFF_ARGS)
2719: {
2720: int iv;
2721:
2722: /* Parse the number of lines. */
1.261 schwarze 2723:
2724: if ( ! roff_evalnum(r, ln, buf->buf, &pos, &iv, 0)) {
1.222 schwarze 2725: mandoc_msg(MANDOCERR_IT_NONUM, r->parse,
1.238 schwarze 2726: ln, ppos, buf->buf + 1);
1.178 schwarze 2727: return(ROFF_IGN);
2728: }
2729:
1.262 schwarze 2730: while (isspace((unsigned char)buf->buf[pos]))
2731: pos++;
2732:
2733: /*
2734: * Arm the input line trap.
2735: * Special-casing "an-trap" is an ugly workaround to cope
2736: * with DocBook stupidly fiddling with man(7) internals.
2737: */
1.261 schwarze 2738:
1.178 schwarze 2739: roffit_lines = iv;
1.262 schwarze 2740: roffit_macro = mandoc_strdup(iv != 1 ||
2741: strcmp(buf->buf + pos, "an-trap") ?
2742: buf->buf + pos : "br");
1.109 kristaps 2743: return(ROFF_IGN);
1.175 schwarze 2744: }
2745:
2746: static enum rofferr
2747: roff_Dd(ROFF_ARGS)
2748: {
2749: const char *const *cp;
2750:
1.227 schwarze 2751: if ((r->options & (MPARSE_MDOC | MPARSE_QUICK)) == 0)
1.175 schwarze 2752: for (cp = __mdoc_reserved; *cp; cp++)
2753: roff_setstr(r, *cp, NULL, 0);
2754:
1.227 schwarze 2755: if (r->format == 0)
2756: r->format = MPARSE_MDOC;
2757:
1.175 schwarze 2758: return(ROFF_CONT);
2759: }
2760:
2761: static enum rofferr
2762: roff_TH(ROFF_ARGS)
2763: {
2764: const char *const *cp;
2765:
1.227 schwarze 2766: if ((r->options & MPARSE_QUICK) == 0)
1.175 schwarze 2767: for (cp = __man_reserved; *cp; cp++)
2768: roff_setstr(r, *cp, NULL, 0);
2769:
1.227 schwarze 2770: if (r->format == 0)
2771: r->format = MPARSE_MAN;
2772:
1.175 schwarze 2773: return(ROFF_CONT);
1.109 kristaps 2774: }
2775:
2776: static enum rofferr
2777: roff_TE(ROFF_ARGS)
2778: {
2779:
2780: if (NULL == r->tbl)
1.221 schwarze 2781: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
2782: ln, ppos, "TE");
1.258 schwarze 2783: else if ( ! tbl_end(&r->tbl)) {
2784: free(buf->buf);
2785: buf->buf = mandoc_strdup(".sp");
2786: buf->sz = 4;
2787: return(ROFF_REPARSE);
2788: }
1.112 kristaps 2789: return(ROFF_IGN);
2790: }
2791:
2792: static enum rofferr
2793: roff_T_(ROFF_ARGS)
2794: {
2795:
2796: if (NULL == r->tbl)
1.221 schwarze 2797: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
2798: ln, ppos, "T&");
1.112 kristaps 2799: else
1.116 kristaps 2800: tbl_restart(ppos, ln, r->tbl);
1.112 kristaps 2801:
1.109 kristaps 2802: return(ROFF_IGN);
2803: }
2804:
1.230 schwarze 2805: /*
2806: * Handle in-line equation delimiters.
2807: */
2808: static enum rofferr
1.238 schwarze 2809: roff_eqndelim(struct roff *r, struct buf *buf, int pos)
1.151 kristaps 2810: {
1.233 schwarze 2811: char *cp1, *cp2;
2812: const char *bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
1.151 kristaps 2813:
1.230 schwarze 2814: /*
2815: * Outside equations, look for an opening delimiter.
2816: * If we are inside an equation, we already know it is
2817: * in-line, or this function wouldn't have been called;
2818: * so look for a closing delimiter.
2819: */
2820:
1.238 schwarze 2821: cp1 = buf->buf + pos;
1.230 schwarze 2822: cp2 = strchr(cp1, r->eqn == NULL ?
2823: r->last_eqn->odelim : r->last_eqn->cdelim);
2824: if (cp2 == NULL)
2825: return(ROFF_CONT);
2826:
1.233 schwarze 2827: *cp2++ = '\0';
2828: bef_pr = bef_nl = aft_nl = aft_pr = "";
2829:
2830: /* Handle preceding text, protecting whitespace. */
2831:
1.238 schwarze 2832: if (*buf->buf != '\0') {
1.233 schwarze 2833: if (r->eqn == NULL)
2834: bef_pr = "\\&";
2835: bef_nl = "\n";
2836: }
2837:
2838: /*
2839: * Prepare replacing the delimiter with an equation macro
2840: * and drop leading white space from the equation.
2841: */
1.230 schwarze 2842:
1.233 schwarze 2843: if (r->eqn == NULL) {
2844: while (*cp2 == ' ')
2845: cp2++;
2846: mac = ".EQ";
2847: } else
2848: mac = ".EN";
2849:
2850: /* Handle following text, protecting whitespace. */
2851:
2852: if (*cp2 != '\0') {
2853: aft_nl = "\n";
2854: if (r->eqn != NULL)
2855: aft_pr = "\\&";
2856: }
2857:
2858: /* Do the actual replacement. */
2859:
1.238 schwarze 2860: buf->sz = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", buf->buf,
1.233 schwarze 2861: bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
1.238 schwarze 2862: free(buf->buf);
2863: buf->buf = cp1;
1.230 schwarze 2864:
2865: /* Toggle the in-line state of the eqn subsystem. */
2866:
2867: r->eqn_inline = r->eqn == NULL;
2868: return(ROFF_REPARSE);
1.151 kristaps 2869: }
2870:
1.235 schwarze 2871: static enum rofferr
2872: roff_EQ(ROFF_ARGS)
1.125 kristaps 2873: {
1.151 kristaps 2874: struct eqn_node *e;
1.125 kristaps 2875:
1.238 schwarze 2876: assert(r->eqn == NULL);
1.235 schwarze 2877: e = eqn_alloc(ppos, ln, r->parse);
1.125 kristaps 2878:
1.230 schwarze 2879: if (r->last_eqn) {
1.125 kristaps 2880: r->last_eqn->next = e;
1.230 schwarze 2881: e->delim = r->last_eqn->delim;
2882: e->odelim = r->last_eqn->odelim;
2883: e->cdelim = r->last_eqn->cdelim;
2884: } else
1.125 kristaps 2885: r->first_eqn = r->last_eqn = e;
2886:
2887: r->eqn = r->last_eqn = e;
1.151 kristaps 2888:
1.238 schwarze 2889: if (buf->buf[pos] != '\0')
1.235 schwarze 2890: mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
1.238 schwarze 2891: ".EQ %s", buf->buf + pos);
1.151 kristaps 2892:
1.125 kristaps 2893: return(ROFF_IGN);
2894: }
2895:
2896: static enum rofferr
2897: roff_EN(ROFF_ARGS)
2898: {
2899:
1.221 schwarze 2900: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse, ln, ppos, "EN");
1.125 kristaps 2901: return(ROFF_IGN);
2902: }
2903:
2904: static enum rofferr
1.109 kristaps 2905: roff_TS(ROFF_ARGS)
2906: {
1.176 schwarze 2907: struct tbl_node *tbl;
1.89 kristaps 2908:
1.115 kristaps 2909: if (r->tbl) {
1.221 schwarze 2910: mandoc_msg(MANDOCERR_BLK_BROKEN, r->parse,
2911: ln, ppos, "TS breaks TS");
1.151 kristaps 2912: tbl_end(&r->tbl);
1.115 kristaps 2913: }
1.83 schwarze 2914:
1.176 schwarze 2915: tbl = tbl_alloc(ppos, ln, r->parse);
1.113 kristaps 2916:
2917: if (r->last_tbl)
1.176 schwarze 2918: r->last_tbl->next = tbl;
1.113 kristaps 2919: else
1.176 schwarze 2920: r->first_tbl = r->last_tbl = tbl;
1.113 kristaps 2921:
1.176 schwarze 2922: r->tbl = r->last_tbl = tbl;
1.83 schwarze 2923: return(ROFF_IGN);
1.251 schwarze 2924: }
2925:
2926: static enum rofferr
2927: roff_brp(ROFF_ARGS)
2928: {
2929:
2930: buf->buf[pos - 1] = '\0';
2931: return(ROFF_CONT);
1.92 schwarze 2932: }
2933:
1.105 kristaps 2934: static enum rofferr
1.174 kristaps 2935: roff_cc(ROFF_ARGS)
2936: {
2937: const char *p;
2938:
1.238 schwarze 2939: p = buf->buf + pos;
1.174 kristaps 2940:
1.238 schwarze 2941: if (*p == '\0' || (r->control = *p++) == '.')
1.174 kristaps 2942: r->control = 0;
2943:
1.238 schwarze 2944: if (*p != '\0')
1.260 schwarze 2945: mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
2946: ln, p - buf->buf, "cc ... %s", p);
1.174 kristaps 2947:
2948: return(ROFF_IGN);
2949: }
2950:
2951: static enum rofferr
1.164 kristaps 2952: roff_tr(ROFF_ARGS)
2953: {
2954: const char *p, *first, *second;
2955: size_t fsz, ssz;
2956: enum mandoc_esc esc;
2957:
1.238 schwarze 2958: p = buf->buf + pos;
1.164 kristaps 2959:
1.238 schwarze 2960: if (*p == '\0') {
1.260 schwarze 2961: mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse, ln, ppos, "tr");
1.164 kristaps 2962: return(ROFF_IGN);
2963: }
2964:
1.238 schwarze 2965: while (*p != '\0') {
1.164 kristaps 2966: fsz = ssz = 1;
2967:
2968: first = p++;
1.238 schwarze 2969: if (*first == '\\') {
1.164 kristaps 2970: esc = mandoc_escape(&p, NULL, NULL);
1.238 schwarze 2971: if (esc == ESCAPE_ERROR) {
1.219 schwarze 2972: mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238 schwarze 2973: ln, (int)(p - buf->buf), first);
1.164 kristaps 2974: return(ROFF_IGN);
2975: }
2976: fsz = (size_t)(p - first);
2977: }
2978:
2979: second = p++;
1.238 schwarze 2980: if (*second == '\\') {
1.164 kristaps 2981: esc = mandoc_escape(&p, NULL, NULL);
1.238 schwarze 2982: if (esc == ESCAPE_ERROR) {
1.219 schwarze 2983: mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238 schwarze 2984: ln, (int)(p - buf->buf), second);
1.164 kristaps 2985: return(ROFF_IGN);
2986: }
2987: ssz = (size_t)(p - second);
1.238 schwarze 2988: } else if (*second == '\0') {
1.260 schwarze 2989: mandoc_vmsg(MANDOCERR_TR_ODD, r->parse,
2990: ln, first - buf->buf, "tr %s", first);
1.164 kristaps 2991: second = " ";
1.165 kristaps 2992: p--;
1.164 kristaps 2993: }
2994:
1.167 kristaps 2995: if (fsz > 1) {
1.207 schwarze 2996: roff_setstrn(&r->xmbtab, first, fsz,
2997: second, ssz, 0);
1.167 kristaps 2998: continue;
2999: }
3000:
1.238 schwarze 3001: if (r->xtab == NULL)
1.207 schwarze 3002: r->xtab = mandoc_calloc(128,
3003: sizeof(struct roffstr));
1.167 kristaps 3004:
3005: free(r->xtab[(int)*first].p);
3006: r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
3007: r->xtab[(int)*first].sz = ssz;
1.164 kristaps 3008: }
3009:
3010: return(ROFF_IGN);
3011: }
3012:
3013: static enum rofferr
1.105 kristaps 3014: roff_so(ROFF_ARGS)
3015: {
1.249 schwarze 3016: char *name, *cp;
1.105 kristaps 3017:
1.238 schwarze 3018: name = buf->buf + pos;
1.224 schwarze 3019: mandoc_vmsg(MANDOCERR_SO, r->parse, ln, ppos, "so %s", name);
1.105 kristaps 3020:
3021: /*
3022: * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3023: * opening anything that's not in our cwd or anything beneath
3024: * it. Thus, explicitly disallow traversing up the file-system
3025: * or using absolute paths.
3026: */
3027:
1.238 schwarze 3028: if (*name == '/' || strstr(name, "../") || strstr(name, "/..")) {
1.210 schwarze 3029: mandoc_vmsg(MANDOCERR_SO_PATH, r->parse, ln, ppos,
3030: ".so %s", name);
1.249 schwarze 3031: buf->sz = mandoc_asprintf(&cp,
3032: ".sp\nSee the file %s.\n.sp", name) + 1;
3033: free(buf->buf);
3034: buf->buf = cp;
3035: *offs = 0;
3036: return(ROFF_REPARSE);
1.105 kristaps 3037: }
3038:
3039: *offs = pos;
3040: return(ROFF_SO);
3041: }
1.92 schwarze 3042:
1.266 schwarze 3043: /* --- user defined strings and macros ------------------------------------ */
3044:
1.106 kristaps 3045: static enum rofferr
3046: roff_userdef(ROFF_ARGS)
1.99 kristaps 3047: {
1.263 schwarze 3048: const char *arg[9], *ap;
1.106 kristaps 3049: char *cp, *n1, *n2;
1.119 schwarze 3050: int i;
1.263 schwarze 3051: size_t asz, rsz;
1.106 kristaps 3052:
3053: /*
3054: * Collect pointers to macro argument strings
1.188 schwarze 3055: * and NUL-terminate them.
1.106 kristaps 3056: */
1.263 schwarze 3057:
1.238 schwarze 3058: cp = buf->buf + pos;
1.119 schwarze 3059: for (i = 0; i < 9; i++)
1.238 schwarze 3060: arg[i] = *cp == '\0' ? "" :
1.136 kristaps 3061: mandoc_getarg(r->parse, &cp, ln, &pos);
1.99 kristaps 3062:
1.106 kristaps 3063: /*
3064: * Expand macro arguments.
1.99 kristaps 3065: */
1.263 schwarze 3066:
3067: buf->sz = strlen(r->current_string) + 1;
3068: n1 = cp = mandoc_malloc(buf->sz);
3069: memcpy(n1, r->current_string, buf->sz);
3070: while (*cp != '\0') {
3071:
3072: /* Scan ahead for the next argument invocation. */
3073:
3074: if (*cp++ != '\\')
3075: continue;
3076: if (*cp++ != '$')
3077: continue;
3078: i = *cp - '1';
3079: if (0 > i || 8 < i)
1.106 kristaps 3080: continue;
1.263 schwarze 3081: cp -= 2;
3082:
3083: /*
3084: * Determine the size of the expanded argument,
3085: * taking escaping of quotes into account.
3086: */
3087:
3088: asz = 0;
3089: for (ap = arg[i]; *ap != '\0'; ap++) {
3090: asz++;
3091: if (*ap == '"')
3092: asz += 3;
3093: }
3094: if (asz != 3) {
3095:
3096: /*
3097: * Determine the size of the rest of the
3098: * unexpanded macro, including the NUL.
3099: */
3100:
3101: rsz = buf->sz - (cp - n1) - 3;
3102:
3103: /*
3104: * When shrinking, move before
3105: * releasing the storage.
3106: */
3107:
3108: if (asz < 3)
3109: memmove(cp + asz, cp + 3, rsz);
3110:
3111: /*
3112: * Resize the storage for the macro
3113: * and readjust the parse pointer.
3114: */
3115:
3116: buf->sz += asz - 3;
3117: n2 = mandoc_realloc(n1, buf->sz);
3118: cp = n2 + (cp - n1);
3119: n1 = n2;
3120:
3121: /*
3122: * When growing, make room
3123: * for the expanded argument.
3124: */
3125:
3126: if (asz > 3)
3127: memmove(cp + asz, cp + 3, rsz);
3128: }
3129:
3130: /* Copy the expanded argument, escaping quotes. */
3131:
3132: n2 = cp;
3133: for (ap = arg[i]; *ap != '\0'; ap++) {
3134: if (*ap == '"') {
3135: memcpy(n2, "\\(dq", 4);
3136: n2 += 4;
3137: } else
3138: *n2++ = *ap;
1.106 kristaps 3139: }
1.99 kristaps 3140: }
3141:
1.106 kristaps 3142: /*
3143: * Replace the macro invocation
3144: * by the expanded macro.
3145: */
1.263 schwarze 3146:
1.238 schwarze 3147: free(buf->buf);
3148: buf->buf = n1;
1.248 schwarze 3149: *offs = 0;
1.106 kristaps 3150:
1.238 schwarze 3151: return(buf->sz > 1 && buf->buf[buf->sz - 2] == '\n' ?
1.106 kristaps 3152: ROFF_REPARSE : ROFF_APPEND);
1.99 kristaps 3153: }
1.121 schwarze 3154:
1.212 schwarze 3155: static size_t
1.121 schwarze 3156: roff_getname(struct roff *r, char **cpp, int ln, int pos)
3157: {
3158: char *name, *cp;
1.212 schwarze 3159: size_t namesz;
1.121 schwarze 3160:
3161: name = *cpp;
3162: if ('\0' == *name)
1.212 schwarze 3163: return(0);
1.121 schwarze 3164:
1.212 schwarze 3165: /* Read until end of name and terminate it with NUL. */
3166: for (cp = name; 1; cp++) {
3167: if ('\0' == *cp || ' ' == *cp) {
3168: namesz = cp - name;
3169: break;
3170: }
1.121 schwarze 3171: if ('\\' != *cp)
3172: continue;
1.215 schwarze 3173: namesz = cp - name;
3174: if ('{' == cp[1] || '}' == cp[1])
3175: break;
1.121 schwarze 3176: cp++;
3177: if ('\\' == *cp)
3178: continue;
1.224 schwarze 3179: mandoc_vmsg(MANDOCERR_NAMESC, r->parse, ln, pos,
3180: "%.*s", (int)(cp - name + 1), name);
1.212 schwarze 3181: mandoc_escape((const char **)&cp, NULL, NULL);
3182: break;
1.121 schwarze 3183: }
3184:
3185: /* Read past spaces. */
3186: while (' ' == *cp)
3187: cp++;
3188:
3189: *cpp = cp;
1.212 schwarze 3190: return(namesz);
1.121 schwarze 3191: }
3192:
1.106 kristaps 3193: /*
3194: * Store *string into the user-defined string called *name.
3195: * To clear an existing entry, call with (*r, *name, NULL, 0).
1.193 schwarze 3196: * append == 0: replace mode
3197: * append == 1: single-line append mode
3198: * append == 2: multiline append mode, append '\n' after each call
1.106 kristaps 3199: */
1.94 kristaps 3200: static void
1.106 kristaps 3201: roff_setstr(struct roff *r, const char *name, const char *string,
1.193 schwarze 3202: int append)
1.92 schwarze 3203: {
1.164 kristaps 3204:
3205: roff_setstrn(&r->strtab, name, strlen(name), string,
1.207 schwarze 3206: string ? strlen(string) : 0, append);
1.164 kristaps 3207: }
3208:
3209: static void
1.166 kristaps 3210: roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1.193 schwarze 3211: const char *string, size_t stringsz, int append)
1.164 kristaps 3212: {
1.166 kristaps 3213: struct roffkv *n;
1.164 kristaps 3214: char *c;
3215: int i;
3216: size_t oldch, newch;
1.92 schwarze 3217:
1.106 kristaps 3218: /* Search for an existing string with the same name. */
1.164 kristaps 3219: n = *r;
3220:
1.211 schwarze 3221: while (n && (namesz != n->key.sz ||
3222: strncmp(n->key.p, name, namesz)))
1.92 schwarze 3223: n = n->next;
1.94 kristaps 3224:
3225: if (NULL == n) {
1.106 kristaps 3226: /* Create a new string table entry. */
1.166 kristaps 3227: n = mandoc_malloc(sizeof(struct roffkv));
3228: n->key.p = mandoc_strndup(name, namesz);
3229: n->key.sz = namesz;
3230: n->val.p = NULL;
3231: n->val.sz = 0;
1.164 kristaps 3232: n->next = *r;
3233: *r = n;
1.193 schwarze 3234: } else if (0 == append) {
1.166 kristaps 3235: free(n->val.p);
3236: n->val.p = NULL;
3237: n->val.sz = 0;
1.106 kristaps 3238: }
3239:
3240: if (NULL == string)
3241: return;
3242:
3243: /*
3244: * One additional byte for the '\n' in multiline mode,
3245: * and one for the terminating '\0'.
3246: */
1.193 schwarze 3247: newch = stringsz + (1 < append ? 2u : 1u);
1.164 kristaps 3248:
1.166 kristaps 3249: if (NULL == n->val.p) {
3250: n->val.p = mandoc_malloc(newch);
3251: *n->val.p = '\0';
1.106 kristaps 3252: oldch = 0;
3253: } else {
1.166 kristaps 3254: oldch = n->val.sz;
3255: n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1.106 kristaps 3256: }
3257:
3258: /* Skip existing content in the destination buffer. */
1.166 kristaps 3259: c = n->val.p + (int)oldch;
1.106 kristaps 3260:
3261: /* Append new content to the destination buffer. */
1.164 kristaps 3262: i = 0;
3263: while (i < (int)stringsz) {
1.106 kristaps 3264: /*
3265: * Rudimentary roff copy mode:
3266: * Handle escaped backslashes.
3267: */
1.164 kristaps 3268: if ('\\' == string[i] && '\\' == string[i + 1])
3269: i++;
3270: *c++ = string[i++];
1.106 kristaps 3271: }
1.94 kristaps 3272:
1.106 kristaps 3273: /* Append terminating bytes. */
1.193 schwarze 3274: if (1 < append)
1.106 kristaps 3275: *c++ = '\n';
1.163 kristaps 3276:
1.106 kristaps 3277: *c = '\0';
1.166 kristaps 3278: n->val.sz = (int)(c - n->val.p);
1.92 schwarze 3279: }
3280:
1.94 kristaps 3281: static const char *
3282: roff_getstrn(const struct roff *r, const char *name, size_t len)
1.92 schwarze 3283: {
1.166 kristaps 3284: const struct roffkv *n;
1.191 schwarze 3285: int i;
1.92 schwarze 3286:
1.164 kristaps 3287: for (n = r->strtab; n; n = n->next)
1.207 schwarze 3288: if (0 == strncmp(name, n->key.p, len) &&
3289: '\0' == n->key.p[(int)len])
1.166 kristaps 3290: return(n->val.p);
1.191 schwarze 3291:
3292: for (i = 0; i < PREDEFS_MAX; i++)
3293: if (0 == strncmp(name, predefs[i].name, len) &&
3294: '\0' == predefs[i].name[(int)len])
3295: return(predefs[i].str);
1.94 kristaps 3296:
1.157 kristaps 3297: return(NULL);
1.92 schwarze 3298: }
3299:
1.94 kristaps 3300: static void
1.167 kristaps 3301: roff_freestr(struct roffkv *r)
1.92 schwarze 3302: {
1.166 kristaps 3303: struct roffkv *n, *nn;
1.92 schwarze 3304:
1.167 kristaps 3305: for (n = r; n; n = nn) {
1.166 kristaps 3306: free(n->key.p);
3307: free(n->val.p);
1.92 schwarze 3308: nn = n->next;
3309: free(n);
3310: }
1.114 kristaps 3311: }
1.266 schwarze 3312:
3313: /* --- accessors and utility functions ------------------------------------ */
1.114 kristaps 3314:
3315: const struct tbl_span *
3316: roff_span(const struct roff *r)
3317: {
1.207 schwarze 3318:
1.114 kristaps 3319: return(r->tbl ? tbl_span(r->tbl) : NULL);
1.125 kristaps 3320: }
3321:
3322: const struct eqn *
3323: roff_eqn(const struct roff *r)
3324: {
1.207 schwarze 3325:
1.125 kristaps 3326: return(r->last_eqn ? &r->last_eqn->eqn : NULL);
1.164 kristaps 3327: }
3328:
3329: /*
3330: * Duplicate an input string, making the appropriate character
3331: * conversations (as stipulated by `tr') along the way.
3332: * Returns a heap-allocated string with all the replacements made.
3333: */
3334: char *
3335: roff_strdup(const struct roff *r, const char *p)
3336: {
1.166 kristaps 3337: const struct roffkv *cp;
1.164 kristaps 3338: char *res;
3339: const char *pp;
3340: size_t ssz, sz;
3341: enum mandoc_esc esc;
3342:
1.167 kristaps 3343: if (NULL == r->xmbtab && NULL == r->xtab)
1.164 kristaps 3344: return(mandoc_strdup(p));
3345: else if ('\0' == *p)
3346: return(mandoc_strdup(""));
3347:
3348: /*
3349: * Step through each character looking for term matches
3350: * (remember that a `tr' can be invoked with an escape, which is
3351: * a glyph but the escape is multi-character).
3352: * We only do this if the character hash has been initialised
3353: * and the string is >0 length.
3354: */
3355:
3356: res = NULL;
3357: ssz = 0;
3358:
3359: while ('\0' != *p) {
1.167 kristaps 3360: if ('\\' != *p && r->xtab && r->xtab[(int)*p].p) {
3361: sz = r->xtab[(int)*p].sz;
3362: res = mandoc_realloc(res, ssz + sz + 1);
3363: memcpy(res + ssz, r->xtab[(int)*p].p, sz);
3364: ssz += sz;
3365: p++;
3366: continue;
3367: } else if ('\\' != *p) {
3368: res = mandoc_realloc(res, ssz + 2);
3369: res[ssz++] = *p++;
3370: continue;
3371: }
3372:
1.164 kristaps 3373: /* Search for term matches. */
1.167 kristaps 3374: for (cp = r->xmbtab; cp; cp = cp->next)
1.166 kristaps 3375: if (0 == strncmp(p, cp->key.p, cp->key.sz))
1.164 kristaps 3376: break;
3377:
3378: if (NULL != cp) {
3379: /*
3380: * A match has been found.
3381: * Append the match to the array and move
3382: * forward by its keysize.
3383: */
1.207 schwarze 3384: res = mandoc_realloc(res,
3385: ssz + cp->val.sz + 1);
1.166 kristaps 3386: memcpy(res + ssz, cp->val.p, cp->val.sz);
3387: ssz += cp->val.sz;
3388: p += (int)cp->key.sz;
1.164 kristaps 3389: continue;
3390: }
3391:
1.167 kristaps 3392: /*
3393: * Handle escapes carefully: we need to copy
3394: * over just the escape itself, or else we might
3395: * do replacements within the escape itself.
3396: * Make sure to pass along the bogus string.
3397: */
3398: pp = p++;
3399: esc = mandoc_escape(&p, NULL, NULL);
3400: if (ESCAPE_ERROR == esc) {
3401: sz = strlen(pp);
1.164 kristaps 3402: res = mandoc_realloc(res, ssz + sz + 1);
3403: memcpy(res + ssz, pp, sz);
1.167 kristaps 3404: break;
1.164 kristaps 3405: }
1.207 schwarze 3406: /*
3407: * We bail out on bad escapes.
1.167 kristaps 3408: * No need to warn: we already did so when
3409: * roff_res() was called.
3410: */
3411: sz = (int)(p - pp);
3412: res = mandoc_realloc(res, ssz + sz + 1);
3413: memcpy(res + ssz, pp, sz);
3414: ssz += sz;
1.164 kristaps 3415: }
3416:
3417: res[(int)ssz] = '\0';
3418: return(res);
1.227 schwarze 3419: }
3420:
3421: int
3422: roff_getformat(const struct roff *r)
3423: {
3424:
3425: return(r->format);
1.174 kristaps 3426: }
3427:
3428: /*
1.207 schwarze 3429: * Find out whether a line is a macro line or not.
1.174 kristaps 3430: * If it is, adjust the current position and return one; if it isn't,
3431: * return zero and don't change the current position.
3432: * If the control character has been set with `.cc', then let that grain
3433: * precedence.
3434: * This is slighly contrary to groff, where using the non-breaking
3435: * control character when `cc' has been invoked will cause the
3436: * non-breaking macro contents to be printed verbatim.
3437: */
3438: int
3439: roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
3440: {
3441: int pos;
3442:
3443: pos = *ppos;
3444:
3445: if (0 != r->control && cp[pos] == r->control)
3446: pos++;
3447: else if (0 != r->control)
3448: return(0);
3449: else if ('\\' == cp[pos] && '.' == cp[pos + 1])
3450: pos += 2;
3451: else if ('.' == cp[pos] || '\'' == cp[pos])
3452: pos++;
3453: else
3454: return(0);
3455:
3456: while (' ' == cp[pos] || '\t' == cp[pos])
3457: pos++;
3458:
3459: *ppos = pos;
3460: return(1);
1.74 kristaps 3461: }
CVSweb