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