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