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