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