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