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