Annotation of mandoc/roff.c, Revision 1.270
1.270 ! schwarze 1: /* $Id: roff.c,v 1.269 2015/04/23 16:17:44 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++;
1.270 ! schwarze 1025: n->parent->last = n;
1.266 schwarze 1026:
1027: /*
1028: * Copy over the normalised-data pointer of our parent. Not
1029: * everybody has one, but copying a null pointer is fine.
1030: */
1031:
1032: switch (n->type) {
1033: case ROFFT_BODY:
1034: if (n->end != ENDBODY_NOT)
1035: break;
1036: /* FALLTHROUGH */
1037: case ROFFT_TAIL:
1038: /* FALLTHROUGH */
1039: case ROFFT_HEAD:
1040: n->norm = n->parent->norm;
1041: break;
1042: default:
1043: break;
1044: }
1045:
1046: if (man->macroset == MACROSET_MDOC)
1047: mdoc_valid_pre(man, n);
1048:
1049: switch (n->type) {
1050: case ROFFT_HEAD:
1051: assert(n->parent->type == ROFFT_BLOCK);
1052: n->parent->head = n;
1053: break;
1054: case ROFFT_BODY:
1055: if (n->end)
1056: break;
1057: assert(n->parent->type == ROFFT_BLOCK);
1058: n->parent->body = n;
1059: break;
1060: case ROFFT_TAIL:
1061: assert(n->parent->type == ROFFT_BLOCK);
1062: n->parent->tail = n;
1063: break;
1064: default:
1065: break;
1066: }
1067: man->last = n;
1068: }
1069:
1.267 schwarze 1070: void
1071: roff_word_alloc(struct roff_man *man, int line, int pos, const char *word)
1072: {
1073: struct roff_node *n;
1074:
1075: n = roff_node_alloc(man, line, pos, ROFFT_TEXT, TOKEN_NONE);
1076: n->string = roff_strdup(man->roff, word);
1077: roff_node_append(man, n);
1078: if (man->macroset == MACROSET_MDOC)
1079: mdoc_valid_post(man);
1080: else
1081: man_valid_post(man);
1082: man->next = ROFF_NEXT_SIBLING;
1083: }
1084:
1085: void
1086: roff_word_append(struct roff_man *man, const char *word)
1087: {
1088: struct roff_node *n;
1089: char *addstr, *newstr;
1090:
1091: n = man->last;
1092: addstr = roff_strdup(man->roff, word);
1093: mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
1094: free(addstr);
1095: free(n->string);
1096: n->string = newstr;
1097: man->next = ROFF_NEXT_SIBLING;
1.268 schwarze 1098: }
1099:
1100: void
1101: roff_elem_alloc(struct roff_man *man, int line, int pos, int tok)
1102: {
1103: struct roff_node *n;
1104:
1105: n = roff_node_alloc(man, line, pos, ROFFT_ELEM, tok);
1106: roff_node_append(man, n);
1107: man->next = ROFF_NEXT_CHILD;
1108: }
1109:
1110: struct roff_node *
1111: roff_block_alloc(struct roff_man *man, int line, int pos, int tok)
1112: {
1113: struct roff_node *n;
1114:
1115: n = roff_node_alloc(man, line, pos, ROFFT_BLOCK, tok);
1116: roff_node_append(man, n);
1117: man->next = ROFF_NEXT_CHILD;
1118: return(n);
1.267 schwarze 1119: }
1120:
1.266 schwarze 1121: struct roff_node *
1122: roff_head_alloc(struct roff_man *man, int line, int pos, int tok)
1123: {
1124: struct roff_node *n;
1125:
1126: n = roff_node_alloc(man, line, pos, ROFFT_HEAD, tok);
1127: roff_node_append(man, n);
1128: man->next = ROFF_NEXT_CHILD;
1129: return(n);
1130: }
1131:
1132: struct roff_node *
1133: roff_body_alloc(struct roff_man *man, int line, int pos, int tok)
1134: {
1135: struct roff_node *n;
1136:
1137: n = roff_node_alloc(man, line, pos, ROFFT_BODY, tok);
1138: roff_node_append(man, n);
1139: man->next = ROFF_NEXT_CHILD;
1140: return(n);
1.267 schwarze 1141: }
1142:
1143: void
1144: roff_addeqn(struct roff_man *man, const struct eqn *eqn)
1145: {
1146: struct roff_node *n;
1147:
1148: n = roff_node_alloc(man, eqn->ln, eqn->pos, ROFFT_EQN, TOKEN_NONE);
1149: n->eqn = eqn;
1150: if (eqn->ln > man->last->line)
1151: n->flags |= MDOC_LINE;
1152: roff_node_append(man, n);
1153: man->next = ROFF_NEXT_SIBLING;
1154: }
1155:
1156: void
1157: roff_addtbl(struct roff_man *man, const struct tbl_span *tbl)
1158: {
1159: struct roff_node *n;
1160:
1161: if (man->macroset == MACROSET_MAN)
1162: man_breakscope(man, TOKEN_NONE);
1163: n = roff_node_alloc(man, tbl->line, 0, ROFFT_TBL, TOKEN_NONE);
1164: n->span = tbl;
1165: roff_node_append(man, n);
1166: if (man->macroset == MACROSET_MDOC)
1167: mdoc_valid_post(man);
1168: else
1169: man_valid_post(man);
1170: man->next = ROFF_NEXT_SIBLING;
1.266 schwarze 1171: }
1172:
1173: void
1174: roff_node_unlink(struct roff_man *man, struct roff_node *n)
1175: {
1176:
1177: /* Adjust siblings. */
1178:
1179: if (n->prev)
1180: n->prev->next = n->next;
1181: if (n->next)
1182: n->next->prev = n->prev;
1183:
1184: /* Adjust parent. */
1185:
1186: if (n->parent != NULL) {
1187: n->parent->nchild--;
1188: if (n->parent->child == n)
1189: n->parent->child = n->next;
1190: if (n->parent->last == n)
1191: n->parent->last = n->prev;
1192: }
1193:
1194: /* Adjust parse point. */
1195:
1196: if (man == NULL)
1197: return;
1198: if (man->last == n) {
1199: if (n->prev == NULL) {
1200: man->last = n->parent;
1201: man->next = ROFF_NEXT_CHILD;
1202: } else {
1203: man->last = n->prev;
1204: man->next = ROFF_NEXT_SIBLING;
1205: }
1206: }
1207: if (man->first == n)
1208: man->first = NULL;
1209: }
1210:
1211: void
1212: roff_node_free(struct roff_node *n)
1213: {
1214:
1215: if (n->args != NULL)
1216: mdoc_argv_free(n->args);
1217: if (n->type == ROFFT_BLOCK || n->type == ROFFT_ELEM)
1218: free(n->norm);
1219: free(n->string);
1220: free(n);
1221: }
1222:
1223: void
1224: roff_node_delete(struct roff_man *man, struct roff_node *n)
1225: {
1226:
1227: while (n->child != NULL)
1228: roff_node_delete(man, n->child);
1229: assert(n->nchild == 0);
1230: roff_node_unlink(man, n);
1231: roff_node_free(n);
1.269 schwarze 1232: }
1233:
1234: void
1235: deroff(char **dest, const struct roff_node *n)
1236: {
1237: char *cp;
1238: size_t sz;
1239:
1240: if (n->type != ROFFT_TEXT) {
1241: for (n = n->child; n != NULL; n = n->next)
1242: deroff(dest, n);
1243: return;
1244: }
1245:
1246: /* Skip leading whitespace and escape sequences. */
1247:
1248: cp = n->string;
1249: while (*cp != '\0') {
1250: if ('\\' == *cp) {
1251: cp++;
1252: mandoc_escape((const char **)&cp, NULL, NULL);
1253: } else if (isspace((unsigned char)*cp))
1254: cp++;
1255: else
1256: break;
1257: }
1258:
1259: /* Skip trailing whitespace. */
1260:
1261: for (sz = strlen(cp); sz; sz--)
1262: if ( ! isspace((unsigned char)cp[sz-1]))
1263: break;
1264:
1265: /* Skip empty strings. */
1266:
1267: if (sz == 0)
1268: return;
1269:
1270: if (*dest == NULL) {
1271: *dest = mandoc_strndup(cp, sz);
1272: return;
1273: }
1274:
1275: mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
1276: free(*dest);
1277: *dest = cp;
1.266 schwarze 1278: }
1279:
1280: /* --- main functions of the roff parser ---------------------------------- */
1281:
1.94 kristaps 1282: /*
1.206 schwarze 1283: * In the current line, expand escape sequences that tend to get
1284: * used in numerical expressions and conditional requests.
1285: * Also check the syntax of the remaining escape sequences.
1.154 kristaps 1286: */
1.172 schwarze 1287: static enum rofferr
1.238 schwarze 1288: roff_res(struct roff *r, struct buf *buf, int ln, int pos)
1.94 kristaps 1289: {
1.208 schwarze 1290: char ubuf[24]; /* buffer to print the number */
1.205 schwarze 1291: const char *start; /* start of the string to process */
1.209 schwarze 1292: char *stesc; /* start of an escape sequence ('\\') */
1.108 schwarze 1293: const char *stnam; /* start of the name, after "[(*" */
1294: const char *cp; /* end of the name, e.g. before ']' */
1295: const char *res; /* the string to be substituted */
1.238 schwarze 1296: char *nbuf; /* new buffer to copy buf->buf to */
1.181 schwarze 1297: size_t maxl; /* expected length of the escape name */
1298: size_t naml; /* actual length of the escape name */
1.237 schwarze 1299: enum mandoc_esc esc; /* type of the escape sequence */
1300: int inaml; /* length returned from mandoc_escape() */
1.181 schwarze 1301: int expand_count; /* to avoid infinite loops */
1.206 schwarze 1302: int npos; /* position in numeric expression */
1.218 schwarze 1303: int arg_complete; /* argument not interrupted by eol */
1.206 schwarze 1304: char term; /* character terminating the escape */
1.94 kristaps 1305:
1.170 schwarze 1306: expand_count = 0;
1.238 schwarze 1307: start = buf->buf + pos;
1.205 schwarze 1308: stesc = strchr(start, '\0') - 1;
1309: while (stesc-- > start) {
1.170 schwarze 1310:
1.205 schwarze 1311: /* Search backwards for the next backslash. */
1312:
1.238 schwarze 1313: if (*stesc != '\\')
1.205 schwarze 1314: continue;
1315:
1316: /* If it is escaped, skip it. */
1317:
1318: for (cp = stesc - 1; cp >= start; cp--)
1.238 schwarze 1319: if (*cp != '\\')
1.205 schwarze 1320: break;
1321:
1.238 schwarze 1322: if ((stesc - cp) % 2 == 0) {
1.209 schwarze 1323: stesc = (char *)cp;
1.205 schwarze 1324: continue;
1325: }
1.108 schwarze 1326:
1.206 schwarze 1327: /* Decide whether to expand or to check only. */
1.108 schwarze 1328:
1.206 schwarze 1329: term = '\0';
1.205 schwarze 1330: cp = stesc + 1;
1.181 schwarze 1331: switch (*cp) {
1.207 schwarze 1332: case '*':
1.181 schwarze 1333: res = NULL;
1334: break;
1.207 schwarze 1335: case 'B':
1.206 schwarze 1336: /* FALLTHROUGH */
1.207 schwarze 1337: case 'w':
1.206 schwarze 1338: term = cp[1];
1339: /* FALLTHROUGH */
1.207 schwarze 1340: case 'n':
1.181 schwarze 1341: res = ubuf;
1342: break;
1343: default:
1.237 schwarze 1344: esc = mandoc_escape(&cp, &stnam, &inaml);
1345: if (esc == ESCAPE_ERROR ||
1346: (esc == ESCAPE_SPECIAL &&
1347: mchars_spec2cp(r->mchars, stnam, inaml) < 0))
1.219 schwarze 1348: mandoc_vmsg(MANDOCERR_ESC_BAD,
1.238 schwarze 1349: r->parse, ln, (int)(stesc - buf->buf),
1.219 schwarze 1350: "%.*s", (int)(cp - stesc), stesc);
1.205 schwarze 1351: continue;
1.152 kristaps 1352: }
1353:
1.205 schwarze 1354: if (EXPAND_LIMIT < ++expand_count) {
1355: mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
1.238 schwarze 1356: ln, (int)(stesc - buf->buf), NULL);
1.205 schwarze 1357: return(ROFF_IGN);
1358: }
1.108 schwarze 1359:
1360: /*
1361: * The third character decides the length
1.181 schwarze 1362: * of the name of the string or register.
1.108 schwarze 1363: * Save a pointer to the name.
1364: */
1365:
1.238 schwarze 1366: if (term == '\0') {
1.206 schwarze 1367: switch (*++cp) {
1.207 schwarze 1368: case '\0':
1.206 schwarze 1369: maxl = 0;
1370: break;
1.207 schwarze 1371: case '(':
1.206 schwarze 1372: cp++;
1373: maxl = 2;
1374: break;
1.207 schwarze 1375: case '[':
1.206 schwarze 1376: cp++;
1377: term = ']';
1378: maxl = 0;
1379: break;
1380: default:
1381: maxl = 1;
1382: break;
1383: }
1384: } else {
1385: cp += 2;
1.94 kristaps 1386: maxl = 0;
1387: }
1.108 schwarze 1388: stnam = cp;
1.94 kristaps 1389:
1.108 schwarze 1390: /* Advance to the end of the name. */
1.94 kristaps 1391:
1.253 schwarze 1392: naml = 0;
1.218 schwarze 1393: arg_complete = 1;
1.253 schwarze 1394: while (maxl == 0 || naml < maxl) {
1.238 schwarze 1395: if (*cp == '\0') {
1.219 schwarze 1396: mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238 schwarze 1397: ln, (int)(stesc - buf->buf), stesc);
1.218 schwarze 1398: arg_complete = 0;
1.206 schwarze 1399: break;
1.153 kristaps 1400: }
1.238 schwarze 1401: if (maxl == 0 && *cp == term) {
1.206 schwarze 1402: cp++;
1.253 schwarze 1403: break;
1404: }
1405: if (*cp++ != '\\' || stesc[1] != 'w') {
1406: naml++;
1407: continue;
1408: }
1409: switch (mandoc_escape(&cp, NULL, NULL)) {
1410: case ESCAPE_SPECIAL:
1411: /* FALLTHROUGH */
1412: case ESCAPE_UNICODE:
1413: /* FALLTHROUGH */
1414: case ESCAPE_NUMBERED:
1415: /* FALLTHROUGH */
1416: case ESCAPE_OVERSTRIKE:
1417: naml++;
1418: break;
1419: default:
1.94 kristaps 1420: break;
1.206 schwarze 1421: }
1.94 kristaps 1422: }
1423:
1.108 schwarze 1424: /*
1425: * Retrieve the replacement string; if it is
1426: * undefined, resume searching for escapes.
1427: */
1428:
1.206 schwarze 1429: switch (stesc[1]) {
1.207 schwarze 1430: case '*':
1.218 schwarze 1431: if (arg_complete)
1432: res = roff_getstrn(r, stnam, naml);
1.206 schwarze 1433: break;
1.207 schwarze 1434: case 'B':
1.206 schwarze 1435: npos = 0;
1.218 schwarze 1436: ubuf[0] = arg_complete &&
1.261 schwarze 1437: roff_evalnum(r, ln, stnam, &npos,
1438: NULL, ROFFNUM_SCALE) &&
1.218 schwarze 1439: stnam + npos + 1 == cp ? '1' : '0';
1.206 schwarze 1440: ubuf[1] = '\0';
1441: break;
1.207 schwarze 1442: case 'n':
1.218 schwarze 1443: if (arg_complete)
1444: (void)snprintf(ubuf, sizeof(ubuf), "%d",
1445: roff_getregn(r, stnam, naml));
1446: else
1447: ubuf[0] = '\0';
1.206 schwarze 1448: break;
1.207 schwarze 1449: case 'w':
1.218 schwarze 1450: /* use even incomplete args */
1.208 schwarze 1451: (void)snprintf(ubuf, sizeof(ubuf), "%d",
1.206 schwarze 1452: 24 * (int)naml);
1453: break;
1454: }
1.94 kristaps 1455:
1.238 schwarze 1456: if (res == NULL) {
1.219 schwarze 1457: mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238 schwarze 1458: r->parse, ln, (int)(stesc - buf->buf),
1.219 schwarze 1459: "%.*s", (int)naml, stnam);
1.142 kristaps 1460: res = "";
1.246 schwarze 1461: } else if (buf->sz + strlen(res) > SHRT_MAX) {
1462: mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
1463: ln, (int)(stesc - buf->buf), NULL);
1464: return(ROFF_IGN);
1.94 kristaps 1465: }
1466:
1.108 schwarze 1467: /* Replace the escape sequence by the string. */
1468:
1.209 schwarze 1469: *stesc = '\0';
1.238 schwarze 1470: buf->sz = mandoc_asprintf(&nbuf, "%s%s%s",
1471: buf->buf, res, cp) + 1;
1.94 kristaps 1472:
1.205 schwarze 1473: /* Prepare for the next replacement. */
1.94 kristaps 1474:
1.205 schwarze 1475: start = nbuf + pos;
1.238 schwarze 1476: stesc = nbuf + (stesc - buf->buf) + strlen(res);
1477: free(buf->buf);
1478: buf->buf = nbuf;
1.154 kristaps 1479: }
1.172 schwarze 1480: return(ROFF_CONT);
1.154 kristaps 1481: }
1482:
1483: /*
1.178 schwarze 1484: * Process text streams:
1485: * Convert all breakable hyphens into ASCII_HYPH.
1486: * Decrement and spring input line trap.
1.154 kristaps 1487: */
1488: static enum rofferr
1.238 schwarze 1489: roff_parsetext(struct buf *buf, int pos, int *offs)
1.154 kristaps 1490: {
1491: size_t sz;
1492: const char *start;
1.178 schwarze 1493: char *p;
1494: int isz;
1.154 kristaps 1495: enum mandoc_esc esc;
1496:
1.238 schwarze 1497: start = p = buf->buf + pos;
1.154 kristaps 1498:
1.238 schwarze 1499: while (*p != '\0') {
1.154 kristaps 1500: sz = strcspn(p, "-\\");
1501: p += sz;
1502:
1.238 schwarze 1503: if (*p == '\0')
1.159 kristaps 1504: break;
1505:
1.238 schwarze 1506: if (*p == '\\') {
1.154 kristaps 1507: /* Skip over escapes. */
1508: p++;
1.189 schwarze 1509: esc = mandoc_escape((const char **)&p, NULL, NULL);
1.238 schwarze 1510: if (esc == ESCAPE_ERROR)
1.154 kristaps 1511: break;
1.264 schwarze 1512: while (*p == '-')
1513: p++;
1.155 kristaps 1514: continue;
1.159 kristaps 1515: } else if (p == start) {
1.158 kristaps 1516: p++;
1.155 kristaps 1517: continue;
1.158 kristaps 1518: }
1.155 kristaps 1519:
1.171 schwarze 1520: if (isalpha((unsigned char)p[-1]) &&
1521: isalpha((unsigned char)p[1]))
1.155 kristaps 1522: *p = ASCII_HYPH;
1523: p++;
1.94 kristaps 1524: }
1525:
1.178 schwarze 1526: /* Spring the input line trap. */
1.238 schwarze 1527: if (roffit_lines == 1) {
1528: isz = mandoc_asprintf(&p, "%s\n.%s", buf->buf, roffit_macro);
1529: free(buf->buf);
1530: buf->buf = p;
1531: buf->sz = isz + 1;
1.178 schwarze 1532: *offs = 0;
1533: free(roffit_macro);
1534: roffit_lines = 0;
1535: return(ROFF_REPARSE);
1.238 schwarze 1536: } else if (roffit_lines > 1)
1.178 schwarze 1537: --roffit_lines;
1.154 kristaps 1538: return(ROFF_CONT);
1.94 kristaps 1539: }
1540:
1.67 kristaps 1541: enum rofferr
1.238 schwarze 1542: roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs)
1.67 kristaps 1543: {
1544: enum rofft t;
1.109 kristaps 1545: enum rofferr e;
1.238 schwarze 1546: int pos; /* parse point */
1.243 schwarze 1547: int spos; /* saved parse point for messages */
1.238 schwarze 1548: int ppos; /* original offset in buf->buf */
1549: int ctl; /* macro line (boolean) */
1550:
1551: ppos = pos = *offs;
1.79 kristaps 1552:
1.230 schwarze 1553: /* Handle in-line equation delimiters. */
1554:
1.236 schwarze 1555: if (r->tbl == NULL &&
1556: r->last_eqn != NULL && r->last_eqn->delim &&
1.230 schwarze 1557: (r->eqn == NULL || r->eqn_inline)) {
1.238 schwarze 1558: e = roff_eqndelim(r, buf, pos);
1.230 schwarze 1559: if (e == ROFF_REPARSE)
1560: return(e);
1561: assert(e == ROFF_CONT);
1562: }
1563:
1564: /* Expand some escape sequences. */
1.94 kristaps 1565:
1.238 schwarze 1566: e = roff_res(r, buf, ln, pos);
1567: if (e == ROFF_IGN)
1.172 schwarze 1568: return(e);
1.238 schwarze 1569: assert(e == ROFF_CONT);
1.94 kristaps 1570:
1.238 schwarze 1571: ctl = roff_getcontrol(r, buf->buf, &pos);
1.130 kristaps 1572:
1.94 kristaps 1573: /*
1.79 kristaps 1574: * First, if a scope is open and we're not a macro, pass the
1.252 schwarze 1575: * text through the macro's filter.
1576: * Equations process all content themselves.
1577: * Tables process almost all content themselves, but we want
1578: * to warn about macros before passing it there.
1.79 kristaps 1579: */
1.74 kristaps 1580:
1.252 schwarze 1581: if (r->last != NULL && ! ctl) {
1.78 kristaps 1582: t = r->last->tok;
1583: assert(roffs[t].text);
1.238 schwarze 1584: e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
1585: assert(e == ROFF_IGN || e == ROFF_CONT);
1586: if (e != ROFF_CONT)
1.125 kristaps 1587: return(e);
1.182 schwarze 1588: }
1.252 schwarze 1589: if (r->eqn != NULL)
1.238 schwarze 1590: return(eqn_read(&r->eqn, ln, buf->buf, ppos, offs));
1.252 schwarze 1591: if (r->tbl != NULL && ( ! ctl || buf->buf[pos] == '\0'))
1.259 schwarze 1592: return(tbl_read(r->tbl, ln, buf->buf, ppos));
1.252 schwarze 1593: if ( ! ctl)
1.238 schwarze 1594: return(roff_parsetext(buf, pos, offs));
1.228 schwarze 1595:
1596: /* Skip empty request lines. */
1597:
1.238 schwarze 1598: if (buf->buf[pos] == '"') {
1.228 schwarze 1599: mandoc_msg(MANDOCERR_COMMENT_BAD, r->parse,
1600: ln, pos, NULL);
1601: return(ROFF_IGN);
1.238 schwarze 1602: } else if (buf->buf[pos] == '\0')
1.228 schwarze 1603: return(ROFF_IGN);
1.67 kristaps 1604:
1.79 kristaps 1605: /*
1606: * If a scope is open, go to the child handler for that macro,
1607: * as it may want to preprocess before doing anything with it.
1.125 kristaps 1608: * Don't do so if an equation is open.
1.79 kristaps 1609: */
1.78 kristaps 1610:
1.79 kristaps 1611: if (r->last) {
1612: t = r->last->tok;
1613: assert(roffs[t].sub);
1.238 schwarze 1614: return((*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs));
1.79 kristaps 1615: }
1.78 kristaps 1616:
1.243 schwarze 1617: /* No scope is open. This is a new request or macro. */
1618:
1619: spos = pos;
1620: t = roff_parse(r, buf->buf, &pos, ln, ppos);
1621:
1622: /* Tables ignore most macros. */
1623:
1624: if (r->tbl != NULL && (t == ROFF_MAX || t == ROFF_TS)) {
1625: mandoc_msg(MANDOCERR_TBLMACRO, r->parse,
1626: ln, pos, buf->buf + spos);
1.257 schwarze 1627: if (t == ROFF_TS)
1628: return(ROFF_IGN);
1629: while (buf->buf[pos] != '\0' && buf->buf[pos] != ' ')
1630: pos++;
1631: while (buf->buf[pos] != '\0' && buf->buf[pos] == ' ')
1632: pos++;
1633: return(tbl_read(r->tbl, ln, buf->buf, pos));
1.243 schwarze 1634: }
1635:
1.79 kristaps 1636: /*
1.243 schwarze 1637: * This is neither a roff request nor a user-defined macro.
1638: * Let the standard macro set parsers handle it.
1.79 kristaps 1639: */
1.67 kristaps 1640:
1.243 schwarze 1641: if (t == ROFF_MAX)
1.79 kristaps 1642: return(ROFF_CONT);
1.243 schwarze 1643:
1644: /* Execute a roff request or a user defined macro. */
1.67 kristaps 1645:
1.75 kristaps 1646: assert(roffs[t].proc);
1.238 schwarze 1647: return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.74 kristaps 1648: }
1649:
1.117 kristaps 1650: void
1.74 kristaps 1651: roff_endparse(struct roff *r)
1652: {
1653:
1.110 kristaps 1654: if (r->last)
1.221 schwarze 1655: mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1656: r->last->line, r->last->col,
1657: roffs[r->last->tok].name);
1.117 kristaps 1658:
1.125 kristaps 1659: if (r->eqn) {
1.221 schwarze 1660: mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1661: r->eqn->eqn.ln, r->eqn->eqn.pos, "EQ");
1.151 kristaps 1662: eqn_end(&r->eqn);
1.125 kristaps 1663: }
1664:
1.117 kristaps 1665: if (r->tbl) {
1.221 schwarze 1666: mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1667: r->tbl->line, r->tbl->pos, "TS");
1.151 kristaps 1668: tbl_end(&r->tbl);
1.117 kristaps 1669: }
1.67 kristaps 1670: }
1671:
1672: /*
1673: * Parse a roff node's type from the input buffer. This must be in the
1674: * form of ".foo xxx" in the usual way.
1675: */
1676: static enum rofft
1.214 schwarze 1677: roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
1.67 kristaps 1678: {
1.214 schwarze 1679: char *cp;
1.106 kristaps 1680: const char *mac;
1681: size_t maclen;
1.67 kristaps 1682: enum rofft t;
1683:
1.214 schwarze 1684: cp = buf + *pos;
1685:
1686: if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
1.67 kristaps 1687: return(ROFF_MAX);
1688:
1.214 schwarze 1689: mac = cp;
1690: maclen = roff_getname(r, &cp, ln, ppos);
1.67 kristaps 1691:
1.106 kristaps 1692: t = (r->current_string = roff_getstrn(r, mac, maclen))
1.155 kristaps 1693: ? ROFF_USERDEF : roffhash_find(mac, maclen);
1.67 kristaps 1694:
1.214 schwarze 1695: if (ROFF_MAX != t)
1696: *pos = cp - buf;
1.67 kristaps 1697:
1698: return(t);
1699: }
1700:
1.266 schwarze 1701: /* --- handling of request blocks ----------------------------------------- */
1702:
1.67 kristaps 1703: static enum rofferr
1.76 kristaps 1704: roff_cblock(ROFF_ARGS)
1.67 kristaps 1705: {
1706:
1.79 kristaps 1707: /*
1708: * A block-close `..' should only be invoked as a child of an
1709: * ignore macro, otherwise raise a warning and just ignore it.
1710: */
1711:
1.238 schwarze 1712: if (r->last == NULL) {
1.221 schwarze 1713: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1714: ln, ppos, "..");
1.76 kristaps 1715: return(ROFF_IGN);
1716: }
1.67 kristaps 1717:
1.81 kristaps 1718: switch (r->last->tok) {
1.207 schwarze 1719: case ROFF_am:
1.220 schwarze 1720: /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1.81 kristaps 1721: /* FALLTHROUGH */
1.207 schwarze 1722: case ROFF_ami:
1.81 kristaps 1723: /* FALLTHROUGH */
1.207 schwarze 1724: case ROFF_de:
1.108 schwarze 1725: /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1.81 kristaps 1726: /* FALLTHROUGH */
1.207 schwarze 1727: case ROFF_dei:
1.81 kristaps 1728: /* FALLTHROUGH */
1.207 schwarze 1729: case ROFF_ig:
1.81 kristaps 1730: break;
1731: default:
1.221 schwarze 1732: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1733: ln, ppos, "..");
1.67 kristaps 1734: return(ROFF_IGN);
1.76 kristaps 1735: }
1.67 kristaps 1736:
1.238 schwarze 1737: if (buf->buf[pos] != '\0')
1.217 schwarze 1738: mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
1.238 schwarze 1739: ".. %s", buf->buf + pos);
1.71 kristaps 1740:
1741: roffnode_pop(r);
1.76 kristaps 1742: roffnode_cleanscope(r);
1743: return(ROFF_IGN);
1.71 kristaps 1744:
1.67 kristaps 1745: }
1746:
1.76 kristaps 1747: static void
1748: roffnode_cleanscope(struct roff *r)
1.67 kristaps 1749: {
1750:
1.76 kristaps 1751: while (r->last) {
1.173 schwarze 1752: if (--r->last->endspan != 0)
1.76 kristaps 1753: break;
1754: roffnode_pop(r);
1755: }
1.67 kristaps 1756: }
1757:
1.195 schwarze 1758: static void
1759: roff_ccond(struct roff *r, int ln, int ppos)
1.74 kristaps 1760: {
1761:
1.76 kristaps 1762: if (NULL == r->last) {
1.221 schwarze 1763: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1764: ln, ppos, "\\}");
1.195 schwarze 1765: return;
1.76 kristaps 1766: }
1767:
1.82 kristaps 1768: switch (r->last->tok) {
1.207 schwarze 1769: case ROFF_el:
1.82 kristaps 1770: /* FALLTHROUGH */
1.207 schwarze 1771: case ROFF_ie:
1.82 kristaps 1772: /* FALLTHROUGH */
1.207 schwarze 1773: case ROFF_if:
1.82 kristaps 1774: break;
1775: default:
1.221 schwarze 1776: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1777: ln, ppos, "\\}");
1.195 schwarze 1778: return;
1.75 kristaps 1779: }
1780:
1.76 kristaps 1781: if (r->last->endspan > -1) {
1.221 schwarze 1782: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1783: ln, ppos, "\\}");
1.195 schwarze 1784: return;
1.76 kristaps 1785: }
1786:
1.75 kristaps 1787: roffnode_pop(r);
1.76 kristaps 1788: roffnode_cleanscope(r);
1.195 schwarze 1789: return;
1.76 kristaps 1790: }
1791:
1792: static enum rofferr
1.80 kristaps 1793: roff_block(ROFF_ARGS)
1.76 kristaps 1794: {
1.220 schwarze 1795: const char *name;
1796: char *iname, *cp;
1.213 schwarze 1797: size_t namesz;
1.106 kristaps 1798:
1.220 schwarze 1799: /* Ignore groff compatibility mode for now. */
1.76 kristaps 1800:
1.238 schwarze 1801: if (tok == ROFF_de1)
1.220 schwarze 1802: tok = ROFF_de;
1.251 schwarze 1803: else if (tok == ROFF_dei1)
1804: tok = ROFF_dei;
1.238 schwarze 1805: else if (tok == ROFF_am1)
1.220 schwarze 1806: tok = ROFF_am;
1.251 schwarze 1807: else if (tok == ROFF_ami1)
1808: tok = ROFF_ami;
1.220 schwarze 1809:
1810: /* Parse the macro name argument. */
1811:
1.238 schwarze 1812: cp = buf->buf + pos;
1813: if (tok == ROFF_ig) {
1.220 schwarze 1814: iname = NULL;
1815: namesz = 0;
1816: } else {
1817: iname = cp;
1818: namesz = roff_getname(r, &cp, ln, ppos);
1819: iname[namesz] = '\0';
1820: }
1.107 kristaps 1821:
1.220 schwarze 1822: /* Resolve the macro name argument if it is indirect. */
1.107 kristaps 1823:
1.238 schwarze 1824: if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1825: if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1.220 schwarze 1826: mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238 schwarze 1827: r->parse, ln, (int)(iname - buf->buf),
1.220 schwarze 1828: "%.*s", (int)namesz, iname);
1829: namesz = 0;
1830: } else
1831: namesz = strlen(name);
1832: } else
1833: name = iname;
1.107 kristaps 1834:
1.238 schwarze 1835: if (namesz == 0 && tok != ROFF_ig) {
1.220 schwarze 1836: mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse,
1837: ln, ppos, roffs[tok].name);
1838: return(ROFF_IGN);
1839: }
1.80 kristaps 1840:
1.106 kristaps 1841: roffnode_push(r, tok, name, ln, ppos);
1842:
1843: /*
1844: * At the beginning of a `de' macro, clear the existing string
1845: * with the same name, if there is one. New content will be
1.193 schwarze 1846: * appended from roff_block_text() in multiline mode.
1.106 kristaps 1847: */
1.107 kristaps 1848:
1.238 schwarze 1849: if (tok == ROFF_de || tok == ROFF_dei)
1.213 schwarze 1850: roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
1.76 kristaps 1851:
1.238 schwarze 1852: if (*cp == '\0')
1.78 kristaps 1853: return(ROFF_IGN);
1854:
1.220 schwarze 1855: /* Get the custom end marker. */
1.107 kristaps 1856:
1.220 schwarze 1857: iname = cp;
1.213 schwarze 1858: namesz = roff_getname(r, &cp, ln, ppos);
1.220 schwarze 1859:
1860: /* Resolve the end marker if it is indirect. */
1861:
1.238 schwarze 1862: if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1863: if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1.220 schwarze 1864: mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238 schwarze 1865: r->parse, ln, (int)(iname - buf->buf),
1.220 schwarze 1866: "%.*s", (int)namesz, iname);
1867: namesz = 0;
1868: } else
1869: namesz = strlen(name);
1870: } else
1871: name = iname;
1872:
1.213 schwarze 1873: if (namesz)
1874: r->last->end = mandoc_strndup(name, namesz);
1.78 kristaps 1875:
1.238 schwarze 1876: if (*cp != '\0')
1.217 schwarze 1877: mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
1878: ln, pos, ".%s ... %s", roffs[tok].name, cp);
1.74 kristaps 1879:
1.78 kristaps 1880: return(ROFF_IGN);
1881: }
1882:
1883: static enum rofferr
1.80 kristaps 1884: roff_block_sub(ROFF_ARGS)
1.79 kristaps 1885: {
1886: enum rofft t;
1887: int i, j;
1888:
1889: /*
1890: * First check whether a custom macro exists at this level. If
1891: * it does, then check against it. This is some of groff's
1892: * stranger behaviours. If we encountered a custom end-scope
1893: * tag and that tag also happens to be a "real" macro, then we
1894: * need to try interpreting it again as a real macro. If it's
1895: * not, then return ignore. Else continue.
1896: */
1897:
1898: if (r->last->end) {
1.130 kristaps 1899: for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.238 schwarze 1900: if (buf->buf[i] != r->last->end[j])
1.79 kristaps 1901: break;
1902:
1.238 schwarze 1903: if (r->last->end[j] == '\0' &&
1904: (buf->buf[i] == '\0' ||
1905: buf->buf[i] == ' ' ||
1906: buf->buf[i] == '\t')) {
1.79 kristaps 1907: roffnode_pop(r);
1908: roffnode_cleanscope(r);
1909:
1.238 schwarze 1910: while (buf->buf[i] == ' ' || buf->buf[i] == '\t')
1.130 kristaps 1911: i++;
1912:
1913: pos = i;
1.238 schwarze 1914: if (roff_parse(r, buf->buf, &pos, ln, ppos) !=
1915: ROFF_MAX)
1.79 kristaps 1916: return(ROFF_RERUN);
1917: return(ROFF_IGN);
1918: }
1919: }
1920:
1921: /*
1922: * If we have no custom end-query or lookup failed, then try
1923: * pulling it out of the hashtable.
1924: */
1925:
1.238 schwarze 1926: t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.79 kristaps 1927:
1.238 schwarze 1928: if (t != ROFF_cblock) {
1929: if (tok != ROFF_ig)
1930: roff_setstr(r, r->last->name, buf->buf + ppos, 2);
1.79 kristaps 1931: return(ROFF_IGN);
1.106 kristaps 1932: }
1.79 kristaps 1933:
1934: assert(roffs[t].proc);
1.238 schwarze 1935: return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.79 kristaps 1936: }
1937:
1938: static enum rofferr
1.80 kristaps 1939: roff_block_text(ROFF_ARGS)
1.78 kristaps 1940: {
1941:
1.238 schwarze 1942: if (tok != ROFF_ig)
1943: roff_setstr(r, r->last->name, buf->buf + pos, 2);
1.106 kristaps 1944:
1.78 kristaps 1945: return(ROFF_IGN);
1946: }
1947:
1948: static enum rofferr
1.82 kristaps 1949: roff_cond_sub(ROFF_ARGS)
1950: {
1951: enum rofft t;
1.139 kristaps 1952: char *ep;
1.198 schwarze 1953: int rr;
1.82 kristaps 1954:
1955: rr = r->last->rule;
1.139 kristaps 1956: roffnode_cleanscope(r);
1.238 schwarze 1957: t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.82 kristaps 1958:
1.139 kristaps 1959: /*
1.177 schwarze 1960: * Fully handle known macros when they are structurally
1961: * required or when the conditional evaluated to true.
1.87 kristaps 1962: */
1963:
1.238 schwarze 1964: if ((t != ROFF_MAX) &&
1965: (rr || roffs[t].flags & ROFFMAC_STRUCT)) {
1.177 schwarze 1966: assert(roffs[t].proc);
1.238 schwarze 1967: return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.177 schwarze 1968: }
1.144 kristaps 1969:
1.196 schwarze 1970: /*
1971: * If `\}' occurs on a macro line without a preceding macro,
1972: * drop the line completely.
1973: */
1974:
1.238 schwarze 1975: ep = buf->buf + pos;
1976: if (ep[0] == '\\' && ep[1] == '}')
1.198 schwarze 1977: rr = 0;
1.196 schwarze 1978:
1.177 schwarze 1979: /* Always check for the closing delimiter `\}'. */
1.144 kristaps 1980:
1.238 schwarze 1981: while ((ep = strchr(ep, '\\')) != NULL) {
1982: if (*(++ep) == '}') {
1.197 schwarze 1983: *ep = '&';
1.238 schwarze 1984: roff_ccond(r, ln, ep - buf->buf - 1);
1.197 schwarze 1985: }
1.247 schwarze 1986: if (*ep != '\0')
1987: ++ep;
1.177 schwarze 1988: }
1.198 schwarze 1989: return(rr ? ROFF_CONT : ROFF_IGN);
1.82 kristaps 1990: }
1991:
1992: static enum rofferr
1993: roff_cond_text(ROFF_ARGS)
1.78 kristaps 1994: {
1.140 kristaps 1995: char *ep;
1.198 schwarze 1996: int rr;
1.82 kristaps 1997:
1998: rr = r->last->rule;
1.140 kristaps 1999: roffnode_cleanscope(r);
1.82 kristaps 2000:
1.238 schwarze 2001: ep = buf->buf + pos;
2002: while ((ep = strchr(ep, '\\')) != NULL) {
2003: if (*(++ep) == '}') {
1.197 schwarze 2004: *ep = '&';
1.238 schwarze 2005: roff_ccond(r, ln, ep - buf->buf - 1);
1.197 schwarze 2006: }
1.247 schwarze 2007: if (*ep != '\0')
2008: ++ep;
1.78 kristaps 2009: }
1.198 schwarze 2010: return(rr ? ROFF_CONT : ROFF_IGN);
1.74 kristaps 2011: }
2012:
1.266 schwarze 2013: /* --- handling of numeric and conditional expressions -------------------- */
2014:
1.204 schwarze 2015: /*
2016: * Parse a single signed integer number. Stop at the first non-digit.
2017: * If there is at least one digit, return success and advance the
2018: * parse point, else return failure and let the parse point unchanged.
2019: * Ignore overflows, treat them just like the C language.
2020: */
1.184 schwarze 2021: static int
1.261 schwarze 2022: roff_getnum(const char *v, int *pos, int *res, int flags)
1.184 schwarze 2023: {
1.261 schwarze 2024: int myres, scaled, n, p;
1.206 schwarze 2025:
2026: if (NULL == res)
2027: res = &myres;
1.184 schwarze 2028:
2029: p = *pos;
2030: n = v[p] == '-';
1.261 schwarze 2031: if (n || v[p] == '+')
1.184 schwarze 2032: p++;
2033:
1.261 schwarze 2034: if (flags & ROFFNUM_WHITE)
2035: while (isspace((unsigned char)v[p]))
2036: p++;
2037:
1.184 schwarze 2038: for (*res = 0; isdigit((unsigned char)v[p]); p++)
1.204 schwarze 2039: *res = 10 * *res + v[p] - '0';
1.184 schwarze 2040: if (p == *pos + n)
2041: return 0;
2042:
2043: if (n)
2044: *res = -*res;
2045:
1.254 schwarze 2046: /* Each number may be followed by one optional scaling unit. */
2047:
2048: switch (v[p]) {
2049: case 'f':
1.261 schwarze 2050: scaled = *res * 65536;
1.254 schwarze 2051: break;
2052: case 'i':
1.261 schwarze 2053: scaled = *res * 240;
1.254 schwarze 2054: break;
2055: case 'c':
1.261 schwarze 2056: scaled = *res * 240 / 2.54;
1.254 schwarze 2057: break;
2058: case 'v':
2059: /* FALLTROUGH */
2060: case 'P':
1.261 schwarze 2061: scaled = *res * 40;
1.254 schwarze 2062: break;
2063: case 'm':
2064: /* FALLTROUGH */
2065: case 'n':
1.261 schwarze 2066: scaled = *res * 24;
1.254 schwarze 2067: break;
2068: case 'p':
1.261 schwarze 2069: scaled = *res * 10 / 3;
1.254 schwarze 2070: break;
2071: case 'u':
1.261 schwarze 2072: scaled = *res;
1.254 schwarze 2073: break;
2074: case 'M':
1.261 schwarze 2075: scaled = *res * 6 / 25;
1.254 schwarze 2076: break;
2077: default:
1.261 schwarze 2078: scaled = *res;
1.254 schwarze 2079: p--;
2080: break;
2081: }
1.261 schwarze 2082: if (flags & ROFFNUM_SCALE)
2083: *res = scaled;
1.254 schwarze 2084:
2085: *pos = p + 1;
2086: return(1);
1.184 schwarze 2087: }
2088:
1.198 schwarze 2089: /*
2090: * Evaluate a string comparison condition.
2091: * The first character is the delimiter.
2092: * Succeed if the string up to its second occurrence
2093: * matches the string up to its third occurence.
2094: * Advance the cursor after the third occurrence
2095: * or lacking that, to the end of the line.
2096: */
2097: static int
2098: roff_evalstrcond(const char *v, int *pos)
2099: {
2100: const char *s1, *s2, *s3;
2101: int match;
2102:
2103: match = 0;
2104: s1 = v + *pos; /* initial delimiter */
2105: s2 = s1 + 1; /* for scanning the first string */
2106: s3 = strchr(s2, *s1); /* for scanning the second string */
2107:
2108: if (NULL == s3) /* found no middle delimiter */
2109: goto out;
2110:
2111: while ('\0' != *++s3) {
2112: if (*s2 != *s3) { /* mismatch */
2113: s3 = strchr(s3, *s1);
2114: break;
2115: }
2116: if (*s3 == *s1) { /* found the final delimiter */
2117: match = 1;
2118: break;
2119: }
2120: s2++;
2121: }
2122:
2123: out:
2124: if (NULL == s3)
2125: s3 = strchr(s2, '\0');
1.242 schwarze 2126: else if (*s3 != '\0')
1.198 schwarze 2127: s3++;
2128: *pos = s3 - v;
2129: return(match);
2130: }
2131:
1.204 schwarze 2132: /*
2133: * Evaluate an optionally negated single character, numerical,
2134: * or string condition.
2135: */
1.198 schwarze 2136: static int
1.234 kristaps 2137: roff_evalcond(struct roff *r, int ln, const char *v, int *pos)
1.88 kristaps 2138: {
1.241 schwarze 2139: int number, savepos, wanttrue;
1.88 kristaps 2140:
1.198 schwarze 2141: if ('!' == v[*pos]) {
2142: wanttrue = 0;
2143: (*pos)++;
2144: } else
2145: wanttrue = 1;
2146:
1.88 kristaps 2147: switch (v[*pos]) {
1.240 schwarze 2148: case '\0':
2149: return(0);
1.207 schwarze 2150: case 'n':
1.198 schwarze 2151: /* FALLTHROUGH */
1.207 schwarze 2152: case 'o':
1.88 kristaps 2153: (*pos)++;
1.198 schwarze 2154: return(wanttrue);
1.207 schwarze 2155: case 'c':
1.198 schwarze 2156: /* FALLTHROUGH */
1.207 schwarze 2157: case 'd':
1.198 schwarze 2158: /* FALLTHROUGH */
1.207 schwarze 2159: case 'e':
1.88 kristaps 2160: /* FALLTHROUGH */
1.207 schwarze 2161: case 'r':
1.88 kristaps 2162: /* FALLTHROUGH */
1.207 schwarze 2163: case 't':
1.239 schwarze 2164: /* FALLTHROUGH */
2165: case 'v':
1.88 kristaps 2166: (*pos)++;
1.198 schwarze 2167: return(!wanttrue);
1.88 kristaps 2168: default:
2169: break;
2170: }
2171:
1.241 schwarze 2172: savepos = *pos;
1.261 schwarze 2173: if (roff_evalnum(r, ln, v, pos, &number, ROFFNUM_SCALE))
1.204 schwarze 2174: return((number > 0) == wanttrue);
1.241 schwarze 2175: else if (*pos == savepos)
2176: return(roff_evalstrcond(v, pos) == wanttrue);
1.204 schwarze 2177: else
1.241 schwarze 2178: return (0);
1.88 kristaps 2179: }
2180:
1.74 kristaps 2181: static enum rofferr
1.103 kristaps 2182: roff_line_ignore(ROFF_ARGS)
1.89 kristaps 2183: {
1.123 schwarze 2184:
1.89 kristaps 2185: return(ROFF_IGN);
2186: }
2187:
1.104 kristaps 2188: static enum rofferr
1.251 schwarze 2189: roff_insec(ROFF_ARGS)
2190: {
2191:
2192: mandoc_msg(MANDOCERR_REQ_INSEC, r->parse,
2193: ln, ppos, roffs[tok].name);
2194: return(ROFF_IGN);
2195: }
2196:
2197: static enum rofferr
2198: roff_unsupp(ROFF_ARGS)
2199: {
2200:
2201: mandoc_msg(MANDOCERR_REQ_UNSUPP, r->parse,
2202: ln, ppos, roffs[tok].name);
2203: return(ROFF_IGN);
2204: }
2205:
2206: static enum rofferr
1.82 kristaps 2207: roff_cond(ROFF_ARGS)
1.74 kristaps 2208: {
1.173 schwarze 2209:
2210: roffnode_push(r, tok, NULL, ln, ppos);
1.74 kristaps 2211:
1.207 schwarze 2212: /*
1.134 kristaps 2213: * An `.el' has no conditional body: it will consume the value
2214: * of the current rstack entry set in prior `ie' calls or
1.207 schwarze 2215: * defaults to DENY.
1.134 kristaps 2216: *
2217: * If we're not an `el', however, then evaluate the conditional.
2218: */
1.133 kristaps 2219:
1.238 schwarze 2220: r->last->rule = tok == ROFF_el ?
1.207 schwarze 2221: (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
1.238 schwarze 2222: roff_evalcond(r, ln, buf->buf, &pos);
1.77 kristaps 2223:
1.134 kristaps 2224: /*
2225: * An if-else will put the NEGATION of the current evaluated
2226: * conditional into the stack of rules.
2227: */
2228:
1.238 schwarze 2229: if (tok == ROFF_ie) {
1.223 schwarze 2230: if (r->rstackpos + 1 == r->rstacksz) {
2231: r->rstacksz += 16;
2232: r->rstack = mandoc_reallocarray(r->rstack,
2233: r->rstacksz, sizeof(int));
1.134 kristaps 2234: }
1.198 schwarze 2235: r->rstack[++r->rstackpos] = !r->last->rule;
1.82 kristaps 2236: }
1.88 kristaps 2237:
2238: /* If the parent has false as its rule, then so do we. */
2239:
1.198 schwarze 2240: if (r->last->parent && !r->last->parent->rule)
2241: r->last->rule = 0;
1.88 kristaps 2242:
2243: /*
1.173 schwarze 2244: * Determine scope.
2245: * If there is nothing on the line after the conditional,
2246: * not even whitespace, use next-line scope.
1.88 kristaps 2247: */
1.74 kristaps 2248:
1.238 schwarze 2249: if (buf->buf[pos] == '\0') {
1.173 schwarze 2250: r->last->endspan = 2;
2251: goto out;
2252: }
2253:
1.238 schwarze 2254: while (buf->buf[pos] == ' ')
1.173 schwarze 2255: pos++;
2256:
2257: /* An opening brace requests multiline scope. */
1.75 kristaps 2258:
1.238 schwarze 2259: if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
1.75 kristaps 2260: r->last->endspan = -1;
2261: pos += 2;
1.173 schwarze 2262: goto out;
1.207 schwarze 2263: }
1.74 kristaps 2264:
1.77 kristaps 2265: /*
1.173 schwarze 2266: * Anything else following the conditional causes
2267: * single-line scope. Warn if the scope contains
2268: * nothing but trailing whitespace.
1.77 kristaps 2269: */
2270:
1.238 schwarze 2271: if (buf->buf[pos] == '\0')
1.216 schwarze 2272: mandoc_msg(MANDOCERR_COND_EMPTY, r->parse,
2273: ln, ppos, roffs[tok].name);
1.77 kristaps 2274:
1.173 schwarze 2275: r->last->endspan = 1;
1.74 kristaps 2276:
1.173 schwarze 2277: out:
1.75 kristaps 2278: *offs = pos;
2279: return(ROFF_RERUN);
1.83 schwarze 2280: }
2281:
2282: static enum rofferr
1.92 schwarze 2283: roff_ds(ROFF_ARGS)
2284: {
1.212 schwarze 2285: char *string;
2286: const char *name;
2287: size_t namesz;
1.96 kristaps 2288:
1.251 schwarze 2289: /* Ignore groff compatibility mode for now. */
2290:
2291: if (tok == ROFF_ds1)
2292: tok = ROFF_ds;
2293: else if (tok == ROFF_as1)
2294: tok = ROFF_as;
2295:
1.96 kristaps 2296: /*
1.212 schwarze 2297: * The first word is the name of the string.
2298: * If it is empty or terminated by an escape sequence,
2299: * abort the `ds' request without defining anything.
1.96 kristaps 2300: */
1.92 schwarze 2301:
1.238 schwarze 2302: name = string = buf->buf + pos;
2303: if (*name == '\0')
1.92 schwarze 2304: return(ROFF_IGN);
2305:
1.212 schwarze 2306: namesz = roff_getname(r, &string, ln, pos);
1.238 schwarze 2307: if (name[namesz] == '\\')
1.212 schwarze 2308: return(ROFF_IGN);
2309:
2310: /* Read past the initial double-quote, if any. */
1.238 schwarze 2311: if (*string == '"')
1.92 schwarze 2312: string++;
2313:
1.96 kristaps 2314: /* The rest is the value. */
1.212 schwarze 2315: roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
2316: ROFF_as == tok);
1.92 schwarze 2317: return(ROFF_IGN);
2318: }
2319:
1.204 schwarze 2320: /*
2321: * Parse a single operator, one or two characters long.
2322: * If the operator is recognized, return success and advance the
2323: * parse point, else return failure and let the parse point unchanged.
2324: */
2325: static int
2326: roff_getop(const char *v, int *pos, char *res)
2327: {
2328:
2329: *res = v[*pos];
2330:
2331: switch (*res) {
1.207 schwarze 2332: case '+':
1.204 schwarze 2333: /* FALLTHROUGH */
1.207 schwarze 2334: case '-':
1.204 schwarze 2335: /* FALLTHROUGH */
1.207 schwarze 2336: case '*':
1.204 schwarze 2337: /* FALLTHROUGH */
1.207 schwarze 2338: case '/':
1.204 schwarze 2339: /* FALLTHROUGH */
1.207 schwarze 2340: case '%':
1.204 schwarze 2341: /* FALLTHROUGH */
1.207 schwarze 2342: case '&':
1.204 schwarze 2343: /* FALLTHROUGH */
1.207 schwarze 2344: case ':':
1.204 schwarze 2345: break;
2346: case '<':
2347: switch (v[*pos + 1]) {
1.207 schwarze 2348: case '=':
1.204 schwarze 2349: *res = 'l';
2350: (*pos)++;
2351: break;
1.207 schwarze 2352: case '>':
1.204 schwarze 2353: *res = '!';
2354: (*pos)++;
2355: break;
1.207 schwarze 2356: case '?':
1.204 schwarze 2357: *res = 'i';
2358: (*pos)++;
2359: break;
2360: default:
2361: break;
2362: }
2363: break;
2364: case '>':
2365: switch (v[*pos + 1]) {
1.207 schwarze 2366: case '=':
1.204 schwarze 2367: *res = 'g';
2368: (*pos)++;
2369: break;
1.207 schwarze 2370: case '?':
1.204 schwarze 2371: *res = 'a';
2372: (*pos)++;
2373: break;
2374: default:
2375: break;
2376: }
2377: break;
2378: case '=':
2379: if ('=' == v[*pos + 1])
2380: (*pos)++;
2381: break;
2382: default:
2383: return(0);
2384: }
2385: (*pos)++;
2386:
2387: return(*res);
2388: }
2389:
2390: /*
2391: * Evaluate either a parenthesized numeric expression
2392: * or a single signed integer number.
2393: */
2394: static int
1.235 schwarze 2395: roff_evalpar(struct roff *r, int ln,
1.261 schwarze 2396: const char *v, int *pos, int *res, int flags)
1.204 schwarze 2397: {
2398:
2399: if ('(' != v[*pos])
1.261 schwarze 2400: return(roff_getnum(v, pos, res, flags));
1.204 schwarze 2401:
2402: (*pos)++;
1.261 schwarze 2403: if ( ! roff_evalnum(r, ln, v, pos, res, flags | ROFFNUM_WHITE))
1.204 schwarze 2404: return(0);
2405:
1.206 schwarze 2406: /*
2407: * Omission of the closing parenthesis
2408: * is an error in validation mode,
2409: * but ignored in evaluation mode.
2410: */
2411:
1.204 schwarze 2412: if (')' == v[*pos])
2413: (*pos)++;
1.206 schwarze 2414: else if (NULL == res)
2415: return(0);
1.204 schwarze 2416:
2417: return(1);
2418: }
2419:
2420: /*
2421: * Evaluate a complete numeric expression.
2422: * Proceed left to right, there is no concept of precedence.
2423: */
2424: static int
1.235 schwarze 2425: roff_evalnum(struct roff *r, int ln, const char *v,
1.261 schwarze 2426: int *pos, int *res, int flags)
1.204 schwarze 2427: {
2428: int mypos, operand2;
2429: char operator;
2430:
2431: if (NULL == pos) {
2432: mypos = 0;
2433: pos = &mypos;
2434: }
2435:
1.261 schwarze 2436: if (flags & ROFFNUM_WHITE)
1.204 schwarze 2437: while (isspace((unsigned char)v[*pos]))
2438: (*pos)++;
2439:
1.261 schwarze 2440: if ( ! roff_evalpar(r, ln, v, pos, res, flags))
1.204 schwarze 2441: return(0);
2442:
2443: while (1) {
1.261 schwarze 2444: if (flags & ROFFNUM_WHITE)
1.204 schwarze 2445: while (isspace((unsigned char)v[*pos]))
2446: (*pos)++;
2447:
2448: if ( ! roff_getop(v, pos, &operator))
2449: break;
2450:
1.261 schwarze 2451: if (flags & ROFFNUM_WHITE)
1.204 schwarze 2452: while (isspace((unsigned char)v[*pos]))
2453: (*pos)++;
2454:
1.261 schwarze 2455: if ( ! roff_evalpar(r, ln, v, pos, &operand2, flags))
1.204 schwarze 2456: return(0);
2457:
1.261 schwarze 2458: if (flags & ROFFNUM_WHITE)
1.204 schwarze 2459: while (isspace((unsigned char)v[*pos]))
2460: (*pos)++;
1.206 schwarze 2461:
2462: if (NULL == res)
2463: continue;
1.204 schwarze 2464:
2465: switch (operator) {
1.207 schwarze 2466: case '+':
1.204 schwarze 2467: *res += operand2;
2468: break;
1.207 schwarze 2469: case '-':
1.204 schwarze 2470: *res -= operand2;
2471: break;
1.207 schwarze 2472: case '*':
1.204 schwarze 2473: *res *= operand2;
2474: break;
1.207 schwarze 2475: case '/':
1.244 schwarze 2476: if (operand2 == 0) {
1.235 schwarze 2477: mandoc_msg(MANDOCERR_DIVZERO,
1.234 kristaps 2478: r->parse, ln, *pos, v);
2479: *res = 0;
2480: break;
2481: }
1.204 schwarze 2482: *res /= operand2;
2483: break;
1.207 schwarze 2484: case '%':
1.244 schwarze 2485: if (operand2 == 0) {
2486: mandoc_msg(MANDOCERR_DIVZERO,
2487: r->parse, ln, *pos, v);
2488: *res = 0;
2489: break;
2490: }
1.204 schwarze 2491: *res %= operand2;
2492: break;
1.207 schwarze 2493: case '<':
1.204 schwarze 2494: *res = *res < operand2;
2495: break;
1.207 schwarze 2496: case '>':
1.204 schwarze 2497: *res = *res > operand2;
2498: break;
1.207 schwarze 2499: case 'l':
1.204 schwarze 2500: *res = *res <= operand2;
2501: break;
1.207 schwarze 2502: case 'g':
1.204 schwarze 2503: *res = *res >= operand2;
2504: break;
1.207 schwarze 2505: case '=':
1.204 schwarze 2506: *res = *res == operand2;
2507: break;
1.207 schwarze 2508: case '!':
1.204 schwarze 2509: *res = *res != operand2;
2510: break;
1.207 schwarze 2511: case '&':
1.204 schwarze 2512: *res = *res && operand2;
2513: break;
1.207 schwarze 2514: case ':':
1.204 schwarze 2515: *res = *res || operand2;
2516: break;
1.207 schwarze 2517: case 'i':
1.204 schwarze 2518: if (operand2 < *res)
2519: *res = operand2;
2520: break;
1.207 schwarze 2521: case 'a':
1.204 schwarze 2522: if (operand2 > *res)
2523: *res = operand2;
2524: break;
2525: default:
2526: abort();
2527: }
2528: }
2529: return(1);
2530: }
2531:
1.266 schwarze 2532: /* --- register management ------------------------------------------------ */
2533:
1.180 schwarze 2534: void
1.187 schwarze 2535: roff_setreg(struct roff *r, const char *name, int val, char sign)
1.147 kristaps 2536: {
1.180 schwarze 2537: struct roffreg *reg;
2538:
2539: /* Search for an existing register with the same name. */
2540: reg = r->regtab;
2541:
2542: while (reg && strcmp(name, reg->key.p))
2543: reg = reg->next;
1.147 kristaps 2544:
1.180 schwarze 2545: if (NULL == reg) {
2546: /* Create a new register. */
2547: reg = mandoc_malloc(sizeof(struct roffreg));
2548: reg->key.p = mandoc_strdup(name);
2549: reg->key.sz = strlen(name);
1.187 schwarze 2550: reg->val = 0;
1.180 schwarze 2551: reg->next = r->regtab;
2552: r->regtab = reg;
2553: }
2554:
1.187 schwarze 2555: if ('+' == sign)
2556: reg->val += val;
2557: else if ('-' == sign)
2558: reg->val -= val;
2559: else
2560: reg->val = val;
1.147 kristaps 2561: }
2562:
1.192 schwarze 2563: /*
2564: * Handle some predefined read-only number registers.
2565: * For now, return -1 if the requested register is not predefined;
2566: * in case a predefined read-only register having the value -1
2567: * were to turn up, another special value would have to be chosen.
2568: */
2569: static int
2570: roff_getregro(const char *name)
2571: {
2572:
2573: switch (*name) {
1.207 schwarze 2574: case 'A': /* ASCII approximation mode is always off. */
1.192 schwarze 2575: return(0);
1.207 schwarze 2576: case 'g': /* Groff compatibility mode is always on. */
1.192 schwarze 2577: return(1);
1.207 schwarze 2578: case 'H': /* Fixed horizontal resolution. */
1.192 schwarze 2579: return (24);
1.207 schwarze 2580: case 'j': /* Always adjust left margin only. */
1.192 schwarze 2581: return(0);
1.207 schwarze 2582: case 'T': /* Some output device is always defined. */
1.192 schwarze 2583: return(1);
1.207 schwarze 2584: case 'V': /* Fixed vertical resolution. */
1.192 schwarze 2585: return (40);
2586: default:
2587: return (-1);
2588: }
2589: }
2590:
1.181 schwarze 2591: int
1.180 schwarze 2592: roff_getreg(const struct roff *r, const char *name)
1.147 kristaps 2593: {
1.180 schwarze 2594: struct roffreg *reg;
1.192 schwarze 2595: int val;
2596:
2597: if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) {
2598: val = roff_getregro(name + 1);
2599: if (-1 != val)
2600: return (val);
2601: }
1.180 schwarze 2602:
2603: for (reg = r->regtab; reg; reg = reg->next)
2604: if (0 == strcmp(name, reg->key.p))
1.181 schwarze 2605: return(reg->val);
2606:
2607: return(0);
2608: }
2609:
2610: static int
2611: roff_getregn(const struct roff *r, const char *name, size_t len)
2612: {
2613: struct roffreg *reg;
1.192 schwarze 2614: int val;
2615:
2616: if ('.' == name[0] && 2 == len) {
2617: val = roff_getregro(name + 1);
2618: if (-1 != val)
2619: return (val);
2620: }
1.181 schwarze 2621:
2622: for (reg = r->regtab; reg; reg = reg->next)
2623: if (len == reg->key.sz &&
2624: 0 == strncmp(name, reg->key.p, len))
2625: return(reg->val);
1.147 kristaps 2626:
1.180 schwarze 2627: return(0);
1.147 kristaps 2628: }
2629:
1.180 schwarze 2630: static void
2631: roff_freereg(struct roffreg *reg)
1.147 kristaps 2632: {
1.180 schwarze 2633: struct roffreg *old_reg;
1.147 kristaps 2634:
1.180 schwarze 2635: while (NULL != reg) {
2636: free(reg->key.p);
2637: old_reg = reg;
2638: reg = reg->next;
2639: free(old_reg);
2640: }
1.147 kristaps 2641: }
1.92 schwarze 2642:
2643: static enum rofferr
1.89 kristaps 2644: roff_nr(ROFF_ARGS)
1.83 schwarze 2645: {
1.212 schwarze 2646: char *key, *val;
2647: size_t keysz;
1.138 kristaps 2648: int iv;
1.187 schwarze 2649: char sign;
1.89 kristaps 2650:
1.238 schwarze 2651: key = val = buf->buf + pos;
2652: if (*key == '\0')
1.212 schwarze 2653: return(ROFF_IGN);
2654:
2655: keysz = roff_getname(r, &val, ln, pos);
1.238 schwarze 2656: if (key[keysz] == '\\')
1.212 schwarze 2657: return(ROFF_IGN);
2658: key[keysz] = '\0';
1.89 kristaps 2659:
1.187 schwarze 2660: sign = *val;
1.238 schwarze 2661: if (sign == '+' || sign == '-')
1.187 schwarze 2662: val++;
2663:
1.261 schwarze 2664: if (roff_evalnum(r, ln, val, NULL, &iv, ROFFNUM_SCALE))
1.204 schwarze 2665: roff_setreg(r, key, iv, sign);
1.109 kristaps 2666:
1.203 schwarze 2667: return(ROFF_IGN);
2668: }
2669:
2670: static enum rofferr
2671: roff_rr(ROFF_ARGS)
2672: {
2673: struct roffreg *reg, **prev;
1.212 schwarze 2674: char *name, *cp;
2675: size_t namesz;
1.203 schwarze 2676:
1.238 schwarze 2677: name = cp = buf->buf + pos;
2678: if (*name == '\0')
1.212 schwarze 2679: return(ROFF_IGN);
2680: namesz = roff_getname(r, &cp, ln, pos);
2681: name[namesz] = '\0';
1.203 schwarze 2682:
2683: prev = &r->regtab;
2684: while (1) {
2685: reg = *prev;
1.238 schwarze 2686: if (reg == NULL || !strcmp(name, reg->key.p))
1.203 schwarze 2687: break;
2688: prev = ®->next;
2689: }
1.238 schwarze 2690: if (reg != NULL) {
1.203 schwarze 2691: *prev = reg->next;
2692: free(reg->key.p);
2693: free(reg);
2694: }
1.122 schwarze 2695: return(ROFF_IGN);
2696: }
2697:
1.266 schwarze 2698: /* --- handler functions for roff requests -------------------------------- */
2699:
1.122 schwarze 2700: static enum rofferr
2701: roff_rm(ROFF_ARGS)
2702: {
2703: const char *name;
2704: char *cp;
1.212 schwarze 2705: size_t namesz;
1.122 schwarze 2706:
1.238 schwarze 2707: cp = buf->buf + pos;
2708: while (*cp != '\0') {
1.212 schwarze 2709: name = cp;
1.238 schwarze 2710: namesz = roff_getname(r, &cp, ln, (int)(cp - buf->buf));
1.212 schwarze 2711: roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
1.238 schwarze 2712: if (name[namesz] == '\\')
1.212 schwarze 2713: break;
1.122 schwarze 2714: }
1.178 schwarze 2715: return(ROFF_IGN);
2716: }
2717:
2718: static enum rofferr
2719: roff_it(ROFF_ARGS)
2720: {
2721: int iv;
2722:
2723: /* Parse the number of lines. */
1.261 schwarze 2724:
2725: if ( ! roff_evalnum(r, ln, buf->buf, &pos, &iv, 0)) {
1.222 schwarze 2726: mandoc_msg(MANDOCERR_IT_NONUM, r->parse,
1.238 schwarze 2727: ln, ppos, buf->buf + 1);
1.178 schwarze 2728: return(ROFF_IGN);
2729: }
2730:
1.262 schwarze 2731: while (isspace((unsigned char)buf->buf[pos]))
2732: pos++;
2733:
2734: /*
2735: * Arm the input line trap.
2736: * Special-casing "an-trap" is an ugly workaround to cope
2737: * with DocBook stupidly fiddling with man(7) internals.
2738: */
1.261 schwarze 2739:
1.178 schwarze 2740: roffit_lines = iv;
1.262 schwarze 2741: roffit_macro = mandoc_strdup(iv != 1 ||
2742: strcmp(buf->buf + pos, "an-trap") ?
2743: buf->buf + pos : "br");
1.109 kristaps 2744: return(ROFF_IGN);
1.175 schwarze 2745: }
2746:
2747: static enum rofferr
2748: roff_Dd(ROFF_ARGS)
2749: {
2750: const char *const *cp;
2751:
1.227 schwarze 2752: if ((r->options & (MPARSE_MDOC | MPARSE_QUICK)) == 0)
1.175 schwarze 2753: for (cp = __mdoc_reserved; *cp; cp++)
2754: roff_setstr(r, *cp, NULL, 0);
2755:
1.227 schwarze 2756: if (r->format == 0)
2757: r->format = MPARSE_MDOC;
2758:
1.175 schwarze 2759: return(ROFF_CONT);
2760: }
2761:
2762: static enum rofferr
2763: roff_TH(ROFF_ARGS)
2764: {
2765: const char *const *cp;
2766:
1.227 schwarze 2767: if ((r->options & MPARSE_QUICK) == 0)
1.175 schwarze 2768: for (cp = __man_reserved; *cp; cp++)
2769: roff_setstr(r, *cp, NULL, 0);
2770:
1.227 schwarze 2771: if (r->format == 0)
2772: r->format = MPARSE_MAN;
2773:
1.175 schwarze 2774: return(ROFF_CONT);
1.109 kristaps 2775: }
2776:
2777: static enum rofferr
2778: roff_TE(ROFF_ARGS)
2779: {
2780:
2781: if (NULL == r->tbl)
1.221 schwarze 2782: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
2783: ln, ppos, "TE");
1.258 schwarze 2784: else if ( ! tbl_end(&r->tbl)) {
2785: free(buf->buf);
2786: buf->buf = mandoc_strdup(".sp");
2787: buf->sz = 4;
2788: return(ROFF_REPARSE);
2789: }
1.112 kristaps 2790: return(ROFF_IGN);
2791: }
2792:
2793: static enum rofferr
2794: roff_T_(ROFF_ARGS)
2795: {
2796:
2797: if (NULL == r->tbl)
1.221 schwarze 2798: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
2799: ln, ppos, "T&");
1.112 kristaps 2800: else
1.116 kristaps 2801: tbl_restart(ppos, ln, r->tbl);
1.112 kristaps 2802:
1.109 kristaps 2803: return(ROFF_IGN);
2804: }
2805:
1.230 schwarze 2806: /*
2807: * Handle in-line equation delimiters.
2808: */
2809: static enum rofferr
1.238 schwarze 2810: roff_eqndelim(struct roff *r, struct buf *buf, int pos)
1.151 kristaps 2811: {
1.233 schwarze 2812: char *cp1, *cp2;
2813: const char *bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
1.151 kristaps 2814:
1.230 schwarze 2815: /*
2816: * Outside equations, look for an opening delimiter.
2817: * If we are inside an equation, we already know it is
2818: * in-line, or this function wouldn't have been called;
2819: * so look for a closing delimiter.
2820: */
2821:
1.238 schwarze 2822: cp1 = buf->buf + pos;
1.230 schwarze 2823: cp2 = strchr(cp1, r->eqn == NULL ?
2824: r->last_eqn->odelim : r->last_eqn->cdelim);
2825: if (cp2 == NULL)
2826: return(ROFF_CONT);
2827:
1.233 schwarze 2828: *cp2++ = '\0';
2829: bef_pr = bef_nl = aft_nl = aft_pr = "";
2830:
2831: /* Handle preceding text, protecting whitespace. */
2832:
1.238 schwarze 2833: if (*buf->buf != '\0') {
1.233 schwarze 2834: if (r->eqn == NULL)
2835: bef_pr = "\\&";
2836: bef_nl = "\n";
2837: }
2838:
2839: /*
2840: * Prepare replacing the delimiter with an equation macro
2841: * and drop leading white space from the equation.
2842: */
1.230 schwarze 2843:
1.233 schwarze 2844: if (r->eqn == NULL) {
2845: while (*cp2 == ' ')
2846: cp2++;
2847: mac = ".EQ";
2848: } else
2849: mac = ".EN";
2850:
2851: /* Handle following text, protecting whitespace. */
2852:
2853: if (*cp2 != '\0') {
2854: aft_nl = "\n";
2855: if (r->eqn != NULL)
2856: aft_pr = "\\&";
2857: }
2858:
2859: /* Do the actual replacement. */
2860:
1.238 schwarze 2861: buf->sz = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", buf->buf,
1.233 schwarze 2862: bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
1.238 schwarze 2863: free(buf->buf);
2864: buf->buf = cp1;
1.230 schwarze 2865:
2866: /* Toggle the in-line state of the eqn subsystem. */
2867:
2868: r->eqn_inline = r->eqn == NULL;
2869: return(ROFF_REPARSE);
1.151 kristaps 2870: }
2871:
1.235 schwarze 2872: static enum rofferr
2873: roff_EQ(ROFF_ARGS)
1.125 kristaps 2874: {
1.151 kristaps 2875: struct eqn_node *e;
1.125 kristaps 2876:
1.238 schwarze 2877: assert(r->eqn == NULL);
1.235 schwarze 2878: e = eqn_alloc(ppos, ln, r->parse);
1.125 kristaps 2879:
1.230 schwarze 2880: if (r->last_eqn) {
1.125 kristaps 2881: r->last_eqn->next = e;
1.230 schwarze 2882: e->delim = r->last_eqn->delim;
2883: e->odelim = r->last_eqn->odelim;
2884: e->cdelim = r->last_eqn->cdelim;
2885: } else
1.125 kristaps 2886: r->first_eqn = r->last_eqn = e;
2887:
2888: r->eqn = r->last_eqn = e;
1.151 kristaps 2889:
1.238 schwarze 2890: if (buf->buf[pos] != '\0')
1.235 schwarze 2891: mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
1.238 schwarze 2892: ".EQ %s", buf->buf + pos);
1.151 kristaps 2893:
1.125 kristaps 2894: return(ROFF_IGN);
2895: }
2896:
2897: static enum rofferr
2898: roff_EN(ROFF_ARGS)
2899: {
2900:
1.221 schwarze 2901: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse, ln, ppos, "EN");
1.125 kristaps 2902: return(ROFF_IGN);
2903: }
2904:
2905: static enum rofferr
1.109 kristaps 2906: roff_TS(ROFF_ARGS)
2907: {
1.176 schwarze 2908: struct tbl_node *tbl;
1.89 kristaps 2909:
1.115 kristaps 2910: if (r->tbl) {
1.221 schwarze 2911: mandoc_msg(MANDOCERR_BLK_BROKEN, r->parse,
2912: ln, ppos, "TS breaks TS");
1.151 kristaps 2913: tbl_end(&r->tbl);
1.115 kristaps 2914: }
1.83 schwarze 2915:
1.176 schwarze 2916: tbl = tbl_alloc(ppos, ln, r->parse);
1.113 kristaps 2917:
2918: if (r->last_tbl)
1.176 schwarze 2919: r->last_tbl->next = tbl;
1.113 kristaps 2920: else
1.176 schwarze 2921: r->first_tbl = r->last_tbl = tbl;
1.113 kristaps 2922:
1.176 schwarze 2923: r->tbl = r->last_tbl = tbl;
1.83 schwarze 2924: return(ROFF_IGN);
1.251 schwarze 2925: }
2926:
2927: static enum rofferr
2928: roff_brp(ROFF_ARGS)
2929: {
2930:
2931: buf->buf[pos - 1] = '\0';
2932: return(ROFF_CONT);
1.92 schwarze 2933: }
2934:
1.105 kristaps 2935: static enum rofferr
1.174 kristaps 2936: roff_cc(ROFF_ARGS)
2937: {
2938: const char *p;
2939:
1.238 schwarze 2940: p = buf->buf + pos;
1.174 kristaps 2941:
1.238 schwarze 2942: if (*p == '\0' || (r->control = *p++) == '.')
1.174 kristaps 2943: r->control = 0;
2944:
1.238 schwarze 2945: if (*p != '\0')
1.260 schwarze 2946: mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
2947: ln, p - buf->buf, "cc ... %s", p);
1.174 kristaps 2948:
2949: return(ROFF_IGN);
2950: }
2951:
2952: static enum rofferr
1.164 kristaps 2953: roff_tr(ROFF_ARGS)
2954: {
2955: const char *p, *first, *second;
2956: size_t fsz, ssz;
2957: enum mandoc_esc esc;
2958:
1.238 schwarze 2959: p = buf->buf + pos;
1.164 kristaps 2960:
1.238 schwarze 2961: if (*p == '\0') {
1.260 schwarze 2962: mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse, ln, ppos, "tr");
1.164 kristaps 2963: return(ROFF_IGN);
2964: }
2965:
1.238 schwarze 2966: while (*p != '\0') {
1.164 kristaps 2967: fsz = ssz = 1;
2968:
2969: first = p++;
1.238 schwarze 2970: if (*first == '\\') {
1.164 kristaps 2971: esc = mandoc_escape(&p, NULL, NULL);
1.238 schwarze 2972: if (esc == ESCAPE_ERROR) {
1.219 schwarze 2973: mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238 schwarze 2974: ln, (int)(p - buf->buf), first);
1.164 kristaps 2975: return(ROFF_IGN);
2976: }
2977: fsz = (size_t)(p - first);
2978: }
2979:
2980: second = p++;
1.238 schwarze 2981: if (*second == '\\') {
1.164 kristaps 2982: esc = mandoc_escape(&p, NULL, NULL);
1.238 schwarze 2983: if (esc == ESCAPE_ERROR) {
1.219 schwarze 2984: mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238 schwarze 2985: ln, (int)(p - buf->buf), second);
1.164 kristaps 2986: return(ROFF_IGN);
2987: }
2988: ssz = (size_t)(p - second);
1.238 schwarze 2989: } else if (*second == '\0') {
1.260 schwarze 2990: mandoc_vmsg(MANDOCERR_TR_ODD, r->parse,
2991: ln, first - buf->buf, "tr %s", first);
1.164 kristaps 2992: second = " ";
1.165 kristaps 2993: p--;
1.164 kristaps 2994: }
2995:
1.167 kristaps 2996: if (fsz > 1) {
1.207 schwarze 2997: roff_setstrn(&r->xmbtab, first, fsz,
2998: second, ssz, 0);
1.167 kristaps 2999: continue;
3000: }
3001:
1.238 schwarze 3002: if (r->xtab == NULL)
1.207 schwarze 3003: r->xtab = mandoc_calloc(128,
3004: sizeof(struct roffstr));
1.167 kristaps 3005:
3006: free(r->xtab[(int)*first].p);
3007: r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
3008: r->xtab[(int)*first].sz = ssz;
1.164 kristaps 3009: }
3010:
3011: return(ROFF_IGN);
3012: }
3013:
3014: static enum rofferr
1.105 kristaps 3015: roff_so(ROFF_ARGS)
3016: {
1.249 schwarze 3017: char *name, *cp;
1.105 kristaps 3018:
1.238 schwarze 3019: name = buf->buf + pos;
1.224 schwarze 3020: mandoc_vmsg(MANDOCERR_SO, r->parse, ln, ppos, "so %s", name);
1.105 kristaps 3021:
3022: /*
3023: * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3024: * opening anything that's not in our cwd or anything beneath
3025: * it. Thus, explicitly disallow traversing up the file-system
3026: * or using absolute paths.
3027: */
3028:
1.238 schwarze 3029: if (*name == '/' || strstr(name, "../") || strstr(name, "/..")) {
1.210 schwarze 3030: mandoc_vmsg(MANDOCERR_SO_PATH, r->parse, ln, ppos,
3031: ".so %s", name);
1.249 schwarze 3032: buf->sz = mandoc_asprintf(&cp,
3033: ".sp\nSee the file %s.\n.sp", name) + 1;
3034: free(buf->buf);
3035: buf->buf = cp;
3036: *offs = 0;
3037: return(ROFF_REPARSE);
1.105 kristaps 3038: }
3039:
3040: *offs = pos;
3041: return(ROFF_SO);
3042: }
1.92 schwarze 3043:
1.266 schwarze 3044: /* --- user defined strings and macros ------------------------------------ */
3045:
1.106 kristaps 3046: static enum rofferr
3047: roff_userdef(ROFF_ARGS)
1.99 kristaps 3048: {
1.263 schwarze 3049: const char *arg[9], *ap;
1.106 kristaps 3050: char *cp, *n1, *n2;
1.119 schwarze 3051: int i;
1.263 schwarze 3052: size_t asz, rsz;
1.106 kristaps 3053:
3054: /*
3055: * Collect pointers to macro argument strings
1.188 schwarze 3056: * and NUL-terminate them.
1.106 kristaps 3057: */
1.263 schwarze 3058:
1.238 schwarze 3059: cp = buf->buf + pos;
1.119 schwarze 3060: for (i = 0; i < 9; i++)
1.238 schwarze 3061: arg[i] = *cp == '\0' ? "" :
1.136 kristaps 3062: mandoc_getarg(r->parse, &cp, ln, &pos);
1.99 kristaps 3063:
1.106 kristaps 3064: /*
3065: * Expand macro arguments.
1.99 kristaps 3066: */
1.263 schwarze 3067:
3068: buf->sz = strlen(r->current_string) + 1;
3069: n1 = cp = mandoc_malloc(buf->sz);
3070: memcpy(n1, r->current_string, buf->sz);
3071: while (*cp != '\0') {
3072:
3073: /* Scan ahead for the next argument invocation. */
3074:
3075: if (*cp++ != '\\')
3076: continue;
3077: if (*cp++ != '$')
3078: continue;
3079: i = *cp - '1';
3080: if (0 > i || 8 < i)
1.106 kristaps 3081: continue;
1.263 schwarze 3082: cp -= 2;
3083:
3084: /*
3085: * Determine the size of the expanded argument,
3086: * taking escaping of quotes into account.
3087: */
3088:
3089: asz = 0;
3090: for (ap = arg[i]; *ap != '\0'; ap++) {
3091: asz++;
3092: if (*ap == '"')
3093: asz += 3;
3094: }
3095: if (asz != 3) {
3096:
3097: /*
3098: * Determine the size of the rest of the
3099: * unexpanded macro, including the NUL.
3100: */
3101:
3102: rsz = buf->sz - (cp - n1) - 3;
3103:
3104: /*
3105: * When shrinking, move before
3106: * releasing the storage.
3107: */
3108:
3109: if (asz < 3)
3110: memmove(cp + asz, cp + 3, rsz);
3111:
3112: /*
3113: * Resize the storage for the macro
3114: * and readjust the parse pointer.
3115: */
3116:
3117: buf->sz += asz - 3;
3118: n2 = mandoc_realloc(n1, buf->sz);
3119: cp = n2 + (cp - n1);
3120: n1 = n2;
3121:
3122: /*
3123: * When growing, make room
3124: * for the expanded argument.
3125: */
3126:
3127: if (asz > 3)
3128: memmove(cp + asz, cp + 3, rsz);
3129: }
3130:
3131: /* Copy the expanded argument, escaping quotes. */
3132:
3133: n2 = cp;
3134: for (ap = arg[i]; *ap != '\0'; ap++) {
3135: if (*ap == '"') {
3136: memcpy(n2, "\\(dq", 4);
3137: n2 += 4;
3138: } else
3139: *n2++ = *ap;
1.106 kristaps 3140: }
1.99 kristaps 3141: }
3142:
1.106 kristaps 3143: /*
3144: * Replace the macro invocation
3145: * by the expanded macro.
3146: */
1.263 schwarze 3147:
1.238 schwarze 3148: free(buf->buf);
3149: buf->buf = n1;
1.248 schwarze 3150: *offs = 0;
1.106 kristaps 3151:
1.238 schwarze 3152: return(buf->sz > 1 && buf->buf[buf->sz - 2] == '\n' ?
1.106 kristaps 3153: ROFF_REPARSE : ROFF_APPEND);
1.99 kristaps 3154: }
1.121 schwarze 3155:
1.212 schwarze 3156: static size_t
1.121 schwarze 3157: roff_getname(struct roff *r, char **cpp, int ln, int pos)
3158: {
3159: char *name, *cp;
1.212 schwarze 3160: size_t namesz;
1.121 schwarze 3161:
3162: name = *cpp;
3163: if ('\0' == *name)
1.212 schwarze 3164: return(0);
1.121 schwarze 3165:
1.212 schwarze 3166: /* Read until end of name and terminate it with NUL. */
3167: for (cp = name; 1; cp++) {
3168: if ('\0' == *cp || ' ' == *cp) {
3169: namesz = cp - name;
3170: break;
3171: }
1.121 schwarze 3172: if ('\\' != *cp)
3173: continue;
1.215 schwarze 3174: namesz = cp - name;
3175: if ('{' == cp[1] || '}' == cp[1])
3176: break;
1.121 schwarze 3177: cp++;
3178: if ('\\' == *cp)
3179: continue;
1.224 schwarze 3180: mandoc_vmsg(MANDOCERR_NAMESC, r->parse, ln, pos,
3181: "%.*s", (int)(cp - name + 1), name);
1.212 schwarze 3182: mandoc_escape((const char **)&cp, NULL, NULL);
3183: break;
1.121 schwarze 3184: }
3185:
3186: /* Read past spaces. */
3187: while (' ' == *cp)
3188: cp++;
3189:
3190: *cpp = cp;
1.212 schwarze 3191: return(namesz);
1.121 schwarze 3192: }
3193:
1.106 kristaps 3194: /*
3195: * Store *string into the user-defined string called *name.
3196: * To clear an existing entry, call with (*r, *name, NULL, 0).
1.193 schwarze 3197: * append == 0: replace mode
3198: * append == 1: single-line append mode
3199: * append == 2: multiline append mode, append '\n' after each call
1.106 kristaps 3200: */
1.94 kristaps 3201: static void
1.106 kristaps 3202: roff_setstr(struct roff *r, const char *name, const char *string,
1.193 schwarze 3203: int append)
1.92 schwarze 3204: {
1.164 kristaps 3205:
3206: roff_setstrn(&r->strtab, name, strlen(name), string,
1.207 schwarze 3207: string ? strlen(string) : 0, append);
1.164 kristaps 3208: }
3209:
3210: static void
1.166 kristaps 3211: roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1.193 schwarze 3212: const char *string, size_t stringsz, int append)
1.164 kristaps 3213: {
1.166 kristaps 3214: struct roffkv *n;
1.164 kristaps 3215: char *c;
3216: int i;
3217: size_t oldch, newch;
1.92 schwarze 3218:
1.106 kristaps 3219: /* Search for an existing string with the same name. */
1.164 kristaps 3220: n = *r;
3221:
1.211 schwarze 3222: while (n && (namesz != n->key.sz ||
3223: strncmp(n->key.p, name, namesz)))
1.92 schwarze 3224: n = n->next;
1.94 kristaps 3225:
3226: if (NULL == n) {
1.106 kristaps 3227: /* Create a new string table entry. */
1.166 kristaps 3228: n = mandoc_malloc(sizeof(struct roffkv));
3229: n->key.p = mandoc_strndup(name, namesz);
3230: n->key.sz = namesz;
3231: n->val.p = NULL;
3232: n->val.sz = 0;
1.164 kristaps 3233: n->next = *r;
3234: *r = n;
1.193 schwarze 3235: } else if (0 == append) {
1.166 kristaps 3236: free(n->val.p);
3237: n->val.p = NULL;
3238: n->val.sz = 0;
1.106 kristaps 3239: }
3240:
3241: if (NULL == string)
3242: return;
3243:
3244: /*
3245: * One additional byte for the '\n' in multiline mode,
3246: * and one for the terminating '\0'.
3247: */
1.193 schwarze 3248: newch = stringsz + (1 < append ? 2u : 1u);
1.164 kristaps 3249:
1.166 kristaps 3250: if (NULL == n->val.p) {
3251: n->val.p = mandoc_malloc(newch);
3252: *n->val.p = '\0';
1.106 kristaps 3253: oldch = 0;
3254: } else {
1.166 kristaps 3255: oldch = n->val.sz;
3256: n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1.106 kristaps 3257: }
3258:
3259: /* Skip existing content in the destination buffer. */
1.166 kristaps 3260: c = n->val.p + (int)oldch;
1.106 kristaps 3261:
3262: /* Append new content to the destination buffer. */
1.164 kristaps 3263: i = 0;
3264: while (i < (int)stringsz) {
1.106 kristaps 3265: /*
3266: * Rudimentary roff copy mode:
3267: * Handle escaped backslashes.
3268: */
1.164 kristaps 3269: if ('\\' == string[i] && '\\' == string[i + 1])
3270: i++;
3271: *c++ = string[i++];
1.106 kristaps 3272: }
1.94 kristaps 3273:
1.106 kristaps 3274: /* Append terminating bytes. */
1.193 schwarze 3275: if (1 < append)
1.106 kristaps 3276: *c++ = '\n';
1.163 kristaps 3277:
1.106 kristaps 3278: *c = '\0';
1.166 kristaps 3279: n->val.sz = (int)(c - n->val.p);
1.92 schwarze 3280: }
3281:
1.94 kristaps 3282: static const char *
3283: roff_getstrn(const struct roff *r, const char *name, size_t len)
1.92 schwarze 3284: {
1.166 kristaps 3285: const struct roffkv *n;
1.191 schwarze 3286: int i;
1.92 schwarze 3287:
1.164 kristaps 3288: for (n = r->strtab; n; n = n->next)
1.207 schwarze 3289: if (0 == strncmp(name, n->key.p, len) &&
3290: '\0' == n->key.p[(int)len])
1.166 kristaps 3291: return(n->val.p);
1.191 schwarze 3292:
3293: for (i = 0; i < PREDEFS_MAX; i++)
3294: if (0 == strncmp(name, predefs[i].name, len) &&
3295: '\0' == predefs[i].name[(int)len])
3296: return(predefs[i].str);
1.94 kristaps 3297:
1.157 kristaps 3298: return(NULL);
1.92 schwarze 3299: }
3300:
1.94 kristaps 3301: static void
1.167 kristaps 3302: roff_freestr(struct roffkv *r)
1.92 schwarze 3303: {
1.166 kristaps 3304: struct roffkv *n, *nn;
1.92 schwarze 3305:
1.167 kristaps 3306: for (n = r; n; n = nn) {
1.166 kristaps 3307: free(n->key.p);
3308: free(n->val.p);
1.92 schwarze 3309: nn = n->next;
3310: free(n);
3311: }
1.114 kristaps 3312: }
1.266 schwarze 3313:
3314: /* --- accessors and utility functions ------------------------------------ */
1.114 kristaps 3315:
3316: const struct tbl_span *
3317: roff_span(const struct roff *r)
3318: {
1.207 schwarze 3319:
1.114 kristaps 3320: return(r->tbl ? tbl_span(r->tbl) : NULL);
1.125 kristaps 3321: }
3322:
3323: const struct eqn *
3324: roff_eqn(const struct roff *r)
3325: {
1.207 schwarze 3326:
1.125 kristaps 3327: return(r->last_eqn ? &r->last_eqn->eqn : NULL);
1.164 kristaps 3328: }
3329:
3330: /*
3331: * Duplicate an input string, making the appropriate character
3332: * conversations (as stipulated by `tr') along the way.
3333: * Returns a heap-allocated string with all the replacements made.
3334: */
3335: char *
3336: roff_strdup(const struct roff *r, const char *p)
3337: {
1.166 kristaps 3338: const struct roffkv *cp;
1.164 kristaps 3339: char *res;
3340: const char *pp;
3341: size_t ssz, sz;
3342: enum mandoc_esc esc;
3343:
1.167 kristaps 3344: if (NULL == r->xmbtab && NULL == r->xtab)
1.164 kristaps 3345: return(mandoc_strdup(p));
3346: else if ('\0' == *p)
3347: return(mandoc_strdup(""));
3348:
3349: /*
3350: * Step through each character looking for term matches
3351: * (remember that a `tr' can be invoked with an escape, which is
3352: * a glyph but the escape is multi-character).
3353: * We only do this if the character hash has been initialised
3354: * and the string is >0 length.
3355: */
3356:
3357: res = NULL;
3358: ssz = 0;
3359:
3360: while ('\0' != *p) {
1.167 kristaps 3361: if ('\\' != *p && r->xtab && r->xtab[(int)*p].p) {
3362: sz = r->xtab[(int)*p].sz;
3363: res = mandoc_realloc(res, ssz + sz + 1);
3364: memcpy(res + ssz, r->xtab[(int)*p].p, sz);
3365: ssz += sz;
3366: p++;
3367: continue;
3368: } else if ('\\' != *p) {
3369: res = mandoc_realloc(res, ssz + 2);
3370: res[ssz++] = *p++;
3371: continue;
3372: }
3373:
1.164 kristaps 3374: /* Search for term matches. */
1.167 kristaps 3375: for (cp = r->xmbtab; cp; cp = cp->next)
1.166 kristaps 3376: if (0 == strncmp(p, cp->key.p, cp->key.sz))
1.164 kristaps 3377: break;
3378:
3379: if (NULL != cp) {
3380: /*
3381: * A match has been found.
3382: * Append the match to the array and move
3383: * forward by its keysize.
3384: */
1.207 schwarze 3385: res = mandoc_realloc(res,
3386: ssz + cp->val.sz + 1);
1.166 kristaps 3387: memcpy(res + ssz, cp->val.p, cp->val.sz);
3388: ssz += cp->val.sz;
3389: p += (int)cp->key.sz;
1.164 kristaps 3390: continue;
3391: }
3392:
1.167 kristaps 3393: /*
3394: * Handle escapes carefully: we need to copy
3395: * over just the escape itself, or else we might
3396: * do replacements within the escape itself.
3397: * Make sure to pass along the bogus string.
3398: */
3399: pp = p++;
3400: esc = mandoc_escape(&p, NULL, NULL);
3401: if (ESCAPE_ERROR == esc) {
3402: sz = strlen(pp);
1.164 kristaps 3403: res = mandoc_realloc(res, ssz + sz + 1);
3404: memcpy(res + ssz, pp, sz);
1.167 kristaps 3405: break;
1.164 kristaps 3406: }
1.207 schwarze 3407: /*
3408: * We bail out on bad escapes.
1.167 kristaps 3409: * No need to warn: we already did so when
3410: * roff_res() was called.
3411: */
3412: sz = (int)(p - pp);
3413: res = mandoc_realloc(res, ssz + sz + 1);
3414: memcpy(res + ssz, pp, sz);
3415: ssz += sz;
1.164 kristaps 3416: }
3417:
3418: res[(int)ssz] = '\0';
3419: return(res);
1.227 schwarze 3420: }
3421:
3422: int
3423: roff_getformat(const struct roff *r)
3424: {
3425:
3426: return(r->format);
1.174 kristaps 3427: }
3428:
3429: /*
1.207 schwarze 3430: * Find out whether a line is a macro line or not.
1.174 kristaps 3431: * If it is, adjust the current position and return one; if it isn't,
3432: * return zero and don't change the current position.
3433: * If the control character has been set with `.cc', then let that grain
3434: * precedence.
3435: * This is slighly contrary to groff, where using the non-breaking
3436: * control character when `cc' has been invoked will cause the
3437: * non-breaking macro contents to be printed verbatim.
3438: */
3439: int
3440: roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
3441: {
3442: int pos;
3443:
3444: pos = *ppos;
3445:
3446: if (0 != r->control && cp[pos] == r->control)
3447: pos++;
3448: else if (0 != r->control)
3449: return(0);
3450: else if ('\\' == cp[pos] && '.' == cp[pos + 1])
3451: pos += 2;
3452: else if ('.' == cp[pos] || '\'' == cp[pos])
3453: pos++;
3454: else
3455: return(0);
3456:
3457: while (' ' == cp[pos] || '\t' == cp[pos])
3458: pos++;
3459:
3460: *ppos = pos;
3461: return(1);
1.74 kristaps 3462: }
CVSweb