Annotation of mandoc/roff.c, Revision 1.261
1.261 ! schwarze 1: /* $Id: roff.c,v 1.260 2015/02/06 16:06:25 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.155 kristaps 1131: continue;
1.159 kristaps 1132: } else if (p == start) {
1.158 kristaps 1133: p++;
1.155 kristaps 1134: continue;
1.158 kristaps 1135: }
1.155 kristaps 1136:
1.171 schwarze 1137: if (isalpha((unsigned char)p[-1]) &&
1138: isalpha((unsigned char)p[1]))
1.155 kristaps 1139: *p = ASCII_HYPH;
1140: p++;
1.94 kristaps 1141: }
1142:
1.178 schwarze 1143: /* Spring the input line trap. */
1.238 schwarze 1144: if (roffit_lines == 1) {
1145: isz = mandoc_asprintf(&p, "%s\n.%s", buf->buf, roffit_macro);
1146: free(buf->buf);
1147: buf->buf = p;
1148: buf->sz = isz + 1;
1.178 schwarze 1149: *offs = 0;
1150: free(roffit_macro);
1151: roffit_lines = 0;
1152: return(ROFF_REPARSE);
1.238 schwarze 1153: } else if (roffit_lines > 1)
1.178 schwarze 1154: --roffit_lines;
1.154 kristaps 1155: return(ROFF_CONT);
1.94 kristaps 1156: }
1157:
1.67 kristaps 1158: enum rofferr
1.238 schwarze 1159: roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs)
1.67 kristaps 1160: {
1161: enum rofft t;
1.109 kristaps 1162: enum rofferr e;
1.238 schwarze 1163: int pos; /* parse point */
1.243 schwarze 1164: int spos; /* saved parse point for messages */
1.238 schwarze 1165: int ppos; /* original offset in buf->buf */
1166: int ctl; /* macro line (boolean) */
1167:
1168: ppos = pos = *offs;
1.79 kristaps 1169:
1.230 schwarze 1170: /* Handle in-line equation delimiters. */
1171:
1.236 schwarze 1172: if (r->tbl == NULL &&
1173: r->last_eqn != NULL && r->last_eqn->delim &&
1.230 schwarze 1174: (r->eqn == NULL || r->eqn_inline)) {
1.238 schwarze 1175: e = roff_eqndelim(r, buf, pos);
1.230 schwarze 1176: if (e == ROFF_REPARSE)
1177: return(e);
1178: assert(e == ROFF_CONT);
1179: }
1180:
1181: /* Expand some escape sequences. */
1.94 kristaps 1182:
1.238 schwarze 1183: e = roff_res(r, buf, ln, pos);
1184: if (e == ROFF_IGN)
1.172 schwarze 1185: return(e);
1.238 schwarze 1186: assert(e == ROFF_CONT);
1.94 kristaps 1187:
1.238 schwarze 1188: ctl = roff_getcontrol(r, buf->buf, &pos);
1.130 kristaps 1189:
1.94 kristaps 1190: /*
1.79 kristaps 1191: * First, if a scope is open and we're not a macro, pass the
1.252 schwarze 1192: * text through the macro's filter.
1193: * Equations process all content themselves.
1194: * Tables process almost all content themselves, but we want
1195: * to warn about macros before passing it there.
1.79 kristaps 1196: */
1.74 kristaps 1197:
1.252 schwarze 1198: if (r->last != NULL && ! ctl) {
1.78 kristaps 1199: t = r->last->tok;
1200: assert(roffs[t].text);
1.238 schwarze 1201: e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
1202: assert(e == ROFF_IGN || e == ROFF_CONT);
1203: if (e != ROFF_CONT)
1.125 kristaps 1204: return(e);
1.182 schwarze 1205: }
1.252 schwarze 1206: if (r->eqn != NULL)
1.238 schwarze 1207: return(eqn_read(&r->eqn, ln, buf->buf, ppos, offs));
1.252 schwarze 1208: if (r->tbl != NULL && ( ! ctl || buf->buf[pos] == '\0'))
1.259 schwarze 1209: return(tbl_read(r->tbl, ln, buf->buf, ppos));
1.252 schwarze 1210: if ( ! ctl)
1.238 schwarze 1211: return(roff_parsetext(buf, pos, offs));
1.228 schwarze 1212:
1213: /* Skip empty request lines. */
1214:
1.238 schwarze 1215: if (buf->buf[pos] == '"') {
1.228 schwarze 1216: mandoc_msg(MANDOCERR_COMMENT_BAD, r->parse,
1217: ln, pos, NULL);
1218: return(ROFF_IGN);
1.238 schwarze 1219: } else if (buf->buf[pos] == '\0')
1.228 schwarze 1220: return(ROFF_IGN);
1.67 kristaps 1221:
1.79 kristaps 1222: /*
1223: * If a scope is open, go to the child handler for that macro,
1224: * as it may want to preprocess before doing anything with it.
1.125 kristaps 1225: * Don't do so if an equation is open.
1.79 kristaps 1226: */
1.78 kristaps 1227:
1.79 kristaps 1228: if (r->last) {
1229: t = r->last->tok;
1230: assert(roffs[t].sub);
1.238 schwarze 1231: return((*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs));
1.79 kristaps 1232: }
1.78 kristaps 1233:
1.243 schwarze 1234: /* No scope is open. This is a new request or macro. */
1235:
1236: spos = pos;
1237: t = roff_parse(r, buf->buf, &pos, ln, ppos);
1238:
1239: /* Tables ignore most macros. */
1240:
1241: if (r->tbl != NULL && (t == ROFF_MAX || t == ROFF_TS)) {
1242: mandoc_msg(MANDOCERR_TBLMACRO, r->parse,
1243: ln, pos, buf->buf + spos);
1.257 schwarze 1244: if (t == ROFF_TS)
1245: return(ROFF_IGN);
1246: while (buf->buf[pos] != '\0' && buf->buf[pos] != ' ')
1247: pos++;
1248: while (buf->buf[pos] != '\0' && buf->buf[pos] == ' ')
1249: pos++;
1250: return(tbl_read(r->tbl, ln, buf->buf, pos));
1.243 schwarze 1251: }
1252:
1.79 kristaps 1253: /*
1.243 schwarze 1254: * This is neither a roff request nor a user-defined macro.
1255: * Let the standard macro set parsers handle it.
1.79 kristaps 1256: */
1.67 kristaps 1257:
1.243 schwarze 1258: if (t == ROFF_MAX)
1.79 kristaps 1259: return(ROFF_CONT);
1.243 schwarze 1260:
1261: /* Execute a roff request or a user defined macro. */
1.67 kristaps 1262:
1.75 kristaps 1263: assert(roffs[t].proc);
1.238 schwarze 1264: return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.74 kristaps 1265: }
1266:
1.117 kristaps 1267: void
1.74 kristaps 1268: roff_endparse(struct roff *r)
1269: {
1270:
1.110 kristaps 1271: if (r->last)
1.221 schwarze 1272: mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1273: r->last->line, r->last->col,
1274: roffs[r->last->tok].name);
1.117 kristaps 1275:
1.125 kristaps 1276: if (r->eqn) {
1.221 schwarze 1277: mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1278: r->eqn->eqn.ln, r->eqn->eqn.pos, "EQ");
1.151 kristaps 1279: eqn_end(&r->eqn);
1.125 kristaps 1280: }
1281:
1.117 kristaps 1282: if (r->tbl) {
1.221 schwarze 1283: mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
1284: r->tbl->line, r->tbl->pos, "TS");
1.151 kristaps 1285: tbl_end(&r->tbl);
1.117 kristaps 1286: }
1.67 kristaps 1287: }
1288:
1289: /*
1290: * Parse a roff node's type from the input buffer. This must be in the
1291: * form of ".foo xxx" in the usual way.
1292: */
1293: static enum rofft
1.214 schwarze 1294: roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
1.67 kristaps 1295: {
1.214 schwarze 1296: char *cp;
1.106 kristaps 1297: const char *mac;
1298: size_t maclen;
1.67 kristaps 1299: enum rofft t;
1300:
1.214 schwarze 1301: cp = buf + *pos;
1302:
1303: if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
1.67 kristaps 1304: return(ROFF_MAX);
1305:
1.214 schwarze 1306: mac = cp;
1307: maclen = roff_getname(r, &cp, ln, ppos);
1.67 kristaps 1308:
1.106 kristaps 1309: t = (r->current_string = roff_getstrn(r, mac, maclen))
1.155 kristaps 1310: ? ROFF_USERDEF : roffhash_find(mac, maclen);
1.67 kristaps 1311:
1.214 schwarze 1312: if (ROFF_MAX != t)
1313: *pos = cp - buf;
1.67 kristaps 1314:
1315: return(t);
1316: }
1317:
1318: static enum rofferr
1.76 kristaps 1319: roff_cblock(ROFF_ARGS)
1.67 kristaps 1320: {
1321:
1.79 kristaps 1322: /*
1323: * A block-close `..' should only be invoked as a child of an
1324: * ignore macro, otherwise raise a warning and just ignore it.
1325: */
1326:
1.238 schwarze 1327: if (r->last == NULL) {
1.221 schwarze 1328: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1329: ln, ppos, "..");
1.76 kristaps 1330: return(ROFF_IGN);
1331: }
1.67 kristaps 1332:
1.81 kristaps 1333: switch (r->last->tok) {
1.207 schwarze 1334: case ROFF_am:
1.220 schwarze 1335: /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1.81 kristaps 1336: /* FALLTHROUGH */
1.207 schwarze 1337: case ROFF_ami:
1.81 kristaps 1338: /* FALLTHROUGH */
1.207 schwarze 1339: case ROFF_de:
1.108 schwarze 1340: /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1.81 kristaps 1341: /* FALLTHROUGH */
1.207 schwarze 1342: case ROFF_dei:
1.81 kristaps 1343: /* FALLTHROUGH */
1.207 schwarze 1344: case ROFF_ig:
1.81 kristaps 1345: break;
1346: default:
1.221 schwarze 1347: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1348: ln, ppos, "..");
1.67 kristaps 1349: return(ROFF_IGN);
1.76 kristaps 1350: }
1.67 kristaps 1351:
1.238 schwarze 1352: if (buf->buf[pos] != '\0')
1.217 schwarze 1353: mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
1.238 schwarze 1354: ".. %s", buf->buf + pos);
1.71 kristaps 1355:
1356: roffnode_pop(r);
1.76 kristaps 1357: roffnode_cleanscope(r);
1358: return(ROFF_IGN);
1.71 kristaps 1359:
1.67 kristaps 1360: }
1361:
1.76 kristaps 1362: static void
1363: roffnode_cleanscope(struct roff *r)
1.67 kristaps 1364: {
1365:
1.76 kristaps 1366: while (r->last) {
1.173 schwarze 1367: if (--r->last->endspan != 0)
1.76 kristaps 1368: break;
1369: roffnode_pop(r);
1370: }
1.67 kristaps 1371: }
1372:
1.195 schwarze 1373: static void
1374: roff_ccond(struct roff *r, int ln, int ppos)
1.74 kristaps 1375: {
1376:
1.76 kristaps 1377: if (NULL == r->last) {
1.221 schwarze 1378: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1379: ln, ppos, "\\}");
1.195 schwarze 1380: return;
1.76 kristaps 1381: }
1382:
1.82 kristaps 1383: switch (r->last->tok) {
1.207 schwarze 1384: case ROFF_el:
1.82 kristaps 1385: /* FALLTHROUGH */
1.207 schwarze 1386: case ROFF_ie:
1.82 kristaps 1387: /* FALLTHROUGH */
1.207 schwarze 1388: case ROFF_if:
1.82 kristaps 1389: break;
1390: default:
1.221 schwarze 1391: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1392: ln, ppos, "\\}");
1.195 schwarze 1393: return;
1.75 kristaps 1394: }
1395:
1.76 kristaps 1396: if (r->last->endspan > -1) {
1.221 schwarze 1397: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
1398: ln, ppos, "\\}");
1.195 schwarze 1399: return;
1.76 kristaps 1400: }
1401:
1.75 kristaps 1402: roffnode_pop(r);
1.76 kristaps 1403: roffnode_cleanscope(r);
1.195 schwarze 1404: return;
1.76 kristaps 1405: }
1406:
1407: static enum rofferr
1.80 kristaps 1408: roff_block(ROFF_ARGS)
1.76 kristaps 1409: {
1.220 schwarze 1410: const char *name;
1411: char *iname, *cp;
1.213 schwarze 1412: size_t namesz;
1.106 kristaps 1413:
1.220 schwarze 1414: /* Ignore groff compatibility mode for now. */
1.76 kristaps 1415:
1.238 schwarze 1416: if (tok == ROFF_de1)
1.220 schwarze 1417: tok = ROFF_de;
1.251 schwarze 1418: else if (tok == ROFF_dei1)
1419: tok = ROFF_dei;
1.238 schwarze 1420: else if (tok == ROFF_am1)
1.220 schwarze 1421: tok = ROFF_am;
1.251 schwarze 1422: else if (tok == ROFF_ami1)
1423: tok = ROFF_ami;
1.220 schwarze 1424:
1425: /* Parse the macro name argument. */
1426:
1.238 schwarze 1427: cp = buf->buf + pos;
1428: if (tok == ROFF_ig) {
1.220 schwarze 1429: iname = NULL;
1430: namesz = 0;
1431: } else {
1432: iname = cp;
1433: namesz = roff_getname(r, &cp, ln, ppos);
1434: iname[namesz] = '\0';
1435: }
1.107 kristaps 1436:
1.220 schwarze 1437: /* Resolve the macro name argument if it is indirect. */
1.107 kristaps 1438:
1.238 schwarze 1439: if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1440: if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1.220 schwarze 1441: mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238 schwarze 1442: r->parse, ln, (int)(iname - buf->buf),
1.220 schwarze 1443: "%.*s", (int)namesz, iname);
1444: namesz = 0;
1445: } else
1446: namesz = strlen(name);
1447: } else
1448: name = iname;
1.107 kristaps 1449:
1.238 schwarze 1450: if (namesz == 0 && tok != ROFF_ig) {
1.220 schwarze 1451: mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse,
1452: ln, ppos, roffs[tok].name);
1453: return(ROFF_IGN);
1454: }
1.80 kristaps 1455:
1.106 kristaps 1456: roffnode_push(r, tok, name, ln, ppos);
1457:
1458: /*
1459: * At the beginning of a `de' macro, clear the existing string
1460: * with the same name, if there is one. New content will be
1.193 schwarze 1461: * appended from roff_block_text() in multiline mode.
1.106 kristaps 1462: */
1.107 kristaps 1463:
1.238 schwarze 1464: if (tok == ROFF_de || tok == ROFF_dei)
1.213 schwarze 1465: roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
1.76 kristaps 1466:
1.238 schwarze 1467: if (*cp == '\0')
1.78 kristaps 1468: return(ROFF_IGN);
1469:
1.220 schwarze 1470: /* Get the custom end marker. */
1.107 kristaps 1471:
1.220 schwarze 1472: iname = cp;
1.213 schwarze 1473: namesz = roff_getname(r, &cp, ln, ppos);
1.220 schwarze 1474:
1475: /* Resolve the end marker if it is indirect. */
1476:
1.238 schwarze 1477: if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1478: if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1.220 schwarze 1479: mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238 schwarze 1480: r->parse, ln, (int)(iname - buf->buf),
1.220 schwarze 1481: "%.*s", (int)namesz, iname);
1482: namesz = 0;
1483: } else
1484: namesz = strlen(name);
1485: } else
1486: name = iname;
1487:
1.213 schwarze 1488: if (namesz)
1489: r->last->end = mandoc_strndup(name, namesz);
1.78 kristaps 1490:
1.238 schwarze 1491: if (*cp != '\0')
1.217 schwarze 1492: mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
1493: ln, pos, ".%s ... %s", roffs[tok].name, cp);
1.74 kristaps 1494:
1.78 kristaps 1495: return(ROFF_IGN);
1496: }
1497:
1498: static enum rofferr
1.80 kristaps 1499: roff_block_sub(ROFF_ARGS)
1.79 kristaps 1500: {
1501: enum rofft t;
1502: int i, j;
1503:
1504: /*
1505: * First check whether a custom macro exists at this level. If
1506: * it does, then check against it. This is some of groff's
1507: * stranger behaviours. If we encountered a custom end-scope
1508: * tag and that tag also happens to be a "real" macro, then we
1509: * need to try interpreting it again as a real macro. If it's
1510: * not, then return ignore. Else continue.
1511: */
1512:
1513: if (r->last->end) {
1.130 kristaps 1514: for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.238 schwarze 1515: if (buf->buf[i] != r->last->end[j])
1.79 kristaps 1516: break;
1517:
1.238 schwarze 1518: if (r->last->end[j] == '\0' &&
1519: (buf->buf[i] == '\0' ||
1520: buf->buf[i] == ' ' ||
1521: buf->buf[i] == '\t')) {
1.79 kristaps 1522: roffnode_pop(r);
1523: roffnode_cleanscope(r);
1524:
1.238 schwarze 1525: while (buf->buf[i] == ' ' || buf->buf[i] == '\t')
1.130 kristaps 1526: i++;
1527:
1528: pos = i;
1.238 schwarze 1529: if (roff_parse(r, buf->buf, &pos, ln, ppos) !=
1530: ROFF_MAX)
1.79 kristaps 1531: return(ROFF_RERUN);
1532: return(ROFF_IGN);
1533: }
1534: }
1535:
1536: /*
1537: * If we have no custom end-query or lookup failed, then try
1538: * pulling it out of the hashtable.
1539: */
1540:
1.238 schwarze 1541: t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.79 kristaps 1542:
1.238 schwarze 1543: if (t != ROFF_cblock) {
1544: if (tok != ROFF_ig)
1545: roff_setstr(r, r->last->name, buf->buf + ppos, 2);
1.79 kristaps 1546: return(ROFF_IGN);
1.106 kristaps 1547: }
1.79 kristaps 1548:
1549: assert(roffs[t].proc);
1.238 schwarze 1550: return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.79 kristaps 1551: }
1552:
1553: static enum rofferr
1.80 kristaps 1554: roff_block_text(ROFF_ARGS)
1.78 kristaps 1555: {
1556:
1.238 schwarze 1557: if (tok != ROFF_ig)
1558: roff_setstr(r, r->last->name, buf->buf + pos, 2);
1.106 kristaps 1559:
1.78 kristaps 1560: return(ROFF_IGN);
1561: }
1562:
1563: static enum rofferr
1.82 kristaps 1564: roff_cond_sub(ROFF_ARGS)
1565: {
1566: enum rofft t;
1.139 kristaps 1567: char *ep;
1.198 schwarze 1568: int rr;
1.82 kristaps 1569:
1570: rr = r->last->rule;
1.139 kristaps 1571: roffnode_cleanscope(r);
1.238 schwarze 1572: t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.82 kristaps 1573:
1.139 kristaps 1574: /*
1.177 schwarze 1575: * Fully handle known macros when they are structurally
1576: * required or when the conditional evaluated to true.
1.87 kristaps 1577: */
1578:
1.238 schwarze 1579: if ((t != ROFF_MAX) &&
1580: (rr || roffs[t].flags & ROFFMAC_STRUCT)) {
1.177 schwarze 1581: assert(roffs[t].proc);
1.238 schwarze 1582: return((*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs));
1.177 schwarze 1583: }
1.144 kristaps 1584:
1.196 schwarze 1585: /*
1586: * If `\}' occurs on a macro line without a preceding macro,
1587: * drop the line completely.
1588: */
1589:
1.238 schwarze 1590: ep = buf->buf + pos;
1591: if (ep[0] == '\\' && ep[1] == '}')
1.198 schwarze 1592: rr = 0;
1.196 schwarze 1593:
1.177 schwarze 1594: /* Always check for the closing delimiter `\}'. */
1.144 kristaps 1595:
1.238 schwarze 1596: while ((ep = strchr(ep, '\\')) != NULL) {
1597: if (*(++ep) == '}') {
1.197 schwarze 1598: *ep = '&';
1.238 schwarze 1599: roff_ccond(r, ln, ep - buf->buf - 1);
1.197 schwarze 1600: }
1.247 schwarze 1601: if (*ep != '\0')
1602: ++ep;
1.177 schwarze 1603: }
1.198 schwarze 1604: return(rr ? ROFF_CONT : ROFF_IGN);
1.82 kristaps 1605: }
1606:
1607: static enum rofferr
1608: roff_cond_text(ROFF_ARGS)
1.78 kristaps 1609: {
1.140 kristaps 1610: char *ep;
1.198 schwarze 1611: int rr;
1.82 kristaps 1612:
1613: rr = r->last->rule;
1.140 kristaps 1614: roffnode_cleanscope(r);
1.82 kristaps 1615:
1.238 schwarze 1616: ep = buf->buf + pos;
1617: while ((ep = strchr(ep, '\\')) != NULL) {
1618: if (*(++ep) == '}') {
1.197 schwarze 1619: *ep = '&';
1.238 schwarze 1620: roff_ccond(r, ln, ep - buf->buf - 1);
1.197 schwarze 1621: }
1.247 schwarze 1622: if (*ep != '\0')
1623: ++ep;
1.78 kristaps 1624: }
1.198 schwarze 1625: return(rr ? ROFF_CONT : ROFF_IGN);
1.74 kristaps 1626: }
1627:
1.204 schwarze 1628: /*
1629: * Parse a single signed integer number. Stop at the first non-digit.
1630: * If there is at least one digit, return success and advance the
1631: * parse point, else return failure and let the parse point unchanged.
1632: * Ignore overflows, treat them just like the C language.
1633: */
1.184 schwarze 1634: static int
1.261 ! schwarze 1635: roff_getnum(const char *v, int *pos, int *res, int flags)
1.184 schwarze 1636: {
1.261 ! schwarze 1637: int myres, scaled, n, p;
1.206 schwarze 1638:
1639: if (NULL == res)
1640: res = &myres;
1.184 schwarze 1641:
1642: p = *pos;
1643: n = v[p] == '-';
1.261 ! schwarze 1644: if (n || v[p] == '+')
1.184 schwarze 1645: p++;
1646:
1.261 ! schwarze 1647: if (flags & ROFFNUM_WHITE)
! 1648: while (isspace((unsigned char)v[p]))
! 1649: p++;
! 1650:
1.184 schwarze 1651: for (*res = 0; isdigit((unsigned char)v[p]); p++)
1.204 schwarze 1652: *res = 10 * *res + v[p] - '0';
1.184 schwarze 1653: if (p == *pos + n)
1654: return 0;
1655:
1656: if (n)
1657: *res = -*res;
1658:
1.254 schwarze 1659: /* Each number may be followed by one optional scaling unit. */
1660:
1661: switch (v[p]) {
1662: case 'f':
1.261 ! schwarze 1663: scaled = *res * 65536;
1.254 schwarze 1664: break;
1665: case 'i':
1.261 ! schwarze 1666: scaled = *res * 240;
1.254 schwarze 1667: break;
1668: case 'c':
1.261 ! schwarze 1669: scaled = *res * 240 / 2.54;
1.254 schwarze 1670: break;
1671: case 'v':
1672: /* FALLTROUGH */
1673: case 'P':
1.261 ! schwarze 1674: scaled = *res * 40;
1.254 schwarze 1675: break;
1676: case 'm':
1677: /* FALLTROUGH */
1678: case 'n':
1.261 ! schwarze 1679: scaled = *res * 24;
1.254 schwarze 1680: break;
1681: case 'p':
1.261 ! schwarze 1682: scaled = *res * 10 / 3;
1.254 schwarze 1683: break;
1684: case 'u':
1.261 ! schwarze 1685: scaled = *res;
1.254 schwarze 1686: break;
1687: case 'M':
1.261 ! schwarze 1688: scaled = *res * 6 / 25;
1.254 schwarze 1689: break;
1690: default:
1.261 ! schwarze 1691: scaled = *res;
1.254 schwarze 1692: p--;
1693: break;
1694: }
1.261 ! schwarze 1695: if (flags & ROFFNUM_SCALE)
! 1696: *res = scaled;
1.254 schwarze 1697:
1698: *pos = p + 1;
1699: return(1);
1.184 schwarze 1700: }
1701:
1.198 schwarze 1702: /*
1703: * Evaluate a string comparison condition.
1704: * The first character is the delimiter.
1705: * Succeed if the string up to its second occurrence
1706: * matches the string up to its third occurence.
1707: * Advance the cursor after the third occurrence
1708: * or lacking that, to the end of the line.
1709: */
1710: static int
1711: roff_evalstrcond(const char *v, int *pos)
1712: {
1713: const char *s1, *s2, *s3;
1714: int match;
1715:
1716: match = 0;
1717: s1 = v + *pos; /* initial delimiter */
1718: s2 = s1 + 1; /* for scanning the first string */
1719: s3 = strchr(s2, *s1); /* for scanning the second string */
1720:
1721: if (NULL == s3) /* found no middle delimiter */
1722: goto out;
1723:
1724: while ('\0' != *++s3) {
1725: if (*s2 != *s3) { /* mismatch */
1726: s3 = strchr(s3, *s1);
1727: break;
1728: }
1729: if (*s3 == *s1) { /* found the final delimiter */
1730: match = 1;
1731: break;
1732: }
1733: s2++;
1734: }
1735:
1736: out:
1737: if (NULL == s3)
1738: s3 = strchr(s2, '\0');
1.242 schwarze 1739: else if (*s3 != '\0')
1.198 schwarze 1740: s3++;
1741: *pos = s3 - v;
1742: return(match);
1743: }
1744:
1.204 schwarze 1745: /*
1746: * Evaluate an optionally negated single character, numerical,
1747: * or string condition.
1748: */
1.198 schwarze 1749: static int
1.234 kristaps 1750: roff_evalcond(struct roff *r, int ln, const char *v, int *pos)
1.88 kristaps 1751: {
1.241 schwarze 1752: int number, savepos, wanttrue;
1.88 kristaps 1753:
1.198 schwarze 1754: if ('!' == v[*pos]) {
1755: wanttrue = 0;
1756: (*pos)++;
1757: } else
1758: wanttrue = 1;
1759:
1.88 kristaps 1760: switch (v[*pos]) {
1.240 schwarze 1761: case '\0':
1762: return(0);
1.207 schwarze 1763: case 'n':
1.198 schwarze 1764: /* FALLTHROUGH */
1.207 schwarze 1765: case 'o':
1.88 kristaps 1766: (*pos)++;
1.198 schwarze 1767: return(wanttrue);
1.207 schwarze 1768: case 'c':
1.198 schwarze 1769: /* FALLTHROUGH */
1.207 schwarze 1770: case 'd':
1.198 schwarze 1771: /* FALLTHROUGH */
1.207 schwarze 1772: case 'e':
1.88 kristaps 1773: /* FALLTHROUGH */
1.207 schwarze 1774: case 'r':
1.88 kristaps 1775: /* FALLTHROUGH */
1.207 schwarze 1776: case 't':
1.239 schwarze 1777: /* FALLTHROUGH */
1778: case 'v':
1.88 kristaps 1779: (*pos)++;
1.198 schwarze 1780: return(!wanttrue);
1.88 kristaps 1781: default:
1782: break;
1783: }
1784:
1.241 schwarze 1785: savepos = *pos;
1.261 ! schwarze 1786: if (roff_evalnum(r, ln, v, pos, &number, ROFFNUM_SCALE))
1.204 schwarze 1787: return((number > 0) == wanttrue);
1.241 schwarze 1788: else if (*pos == savepos)
1789: return(roff_evalstrcond(v, pos) == wanttrue);
1.204 schwarze 1790: else
1.241 schwarze 1791: return (0);
1.88 kristaps 1792: }
1793:
1.74 kristaps 1794: static enum rofferr
1.103 kristaps 1795: roff_line_ignore(ROFF_ARGS)
1.89 kristaps 1796: {
1.123 schwarze 1797:
1.89 kristaps 1798: return(ROFF_IGN);
1799: }
1800:
1.104 kristaps 1801: static enum rofferr
1.251 schwarze 1802: roff_insec(ROFF_ARGS)
1803: {
1804:
1805: mandoc_msg(MANDOCERR_REQ_INSEC, r->parse,
1806: ln, ppos, roffs[tok].name);
1807: return(ROFF_IGN);
1808: }
1809:
1810: static enum rofferr
1811: roff_unsupp(ROFF_ARGS)
1812: {
1813:
1814: mandoc_msg(MANDOCERR_REQ_UNSUPP, r->parse,
1815: ln, ppos, roffs[tok].name);
1816: return(ROFF_IGN);
1817: }
1818:
1819: static enum rofferr
1.82 kristaps 1820: roff_cond(ROFF_ARGS)
1.74 kristaps 1821: {
1.173 schwarze 1822:
1823: roffnode_push(r, tok, NULL, ln, ppos);
1.74 kristaps 1824:
1.207 schwarze 1825: /*
1.134 kristaps 1826: * An `.el' has no conditional body: it will consume the value
1827: * of the current rstack entry set in prior `ie' calls or
1.207 schwarze 1828: * defaults to DENY.
1.134 kristaps 1829: *
1830: * If we're not an `el', however, then evaluate the conditional.
1831: */
1.133 kristaps 1832:
1.238 schwarze 1833: r->last->rule = tok == ROFF_el ?
1.207 schwarze 1834: (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
1.238 schwarze 1835: roff_evalcond(r, ln, buf->buf, &pos);
1.77 kristaps 1836:
1.134 kristaps 1837: /*
1838: * An if-else will put the NEGATION of the current evaluated
1839: * conditional into the stack of rules.
1840: */
1841:
1.238 schwarze 1842: if (tok == ROFF_ie) {
1.223 schwarze 1843: if (r->rstackpos + 1 == r->rstacksz) {
1844: r->rstacksz += 16;
1845: r->rstack = mandoc_reallocarray(r->rstack,
1846: r->rstacksz, sizeof(int));
1.134 kristaps 1847: }
1.198 schwarze 1848: r->rstack[++r->rstackpos] = !r->last->rule;
1.82 kristaps 1849: }
1.88 kristaps 1850:
1851: /* If the parent has false as its rule, then so do we. */
1852:
1.198 schwarze 1853: if (r->last->parent && !r->last->parent->rule)
1854: r->last->rule = 0;
1.88 kristaps 1855:
1856: /*
1.173 schwarze 1857: * Determine scope.
1858: * If there is nothing on the line after the conditional,
1859: * not even whitespace, use next-line scope.
1.88 kristaps 1860: */
1.74 kristaps 1861:
1.238 schwarze 1862: if (buf->buf[pos] == '\0') {
1.173 schwarze 1863: r->last->endspan = 2;
1864: goto out;
1865: }
1866:
1.238 schwarze 1867: while (buf->buf[pos] == ' ')
1.173 schwarze 1868: pos++;
1869:
1870: /* An opening brace requests multiline scope. */
1.75 kristaps 1871:
1.238 schwarze 1872: if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
1.75 kristaps 1873: r->last->endspan = -1;
1874: pos += 2;
1.173 schwarze 1875: goto out;
1.207 schwarze 1876: }
1.74 kristaps 1877:
1.77 kristaps 1878: /*
1.173 schwarze 1879: * Anything else following the conditional causes
1880: * single-line scope. Warn if the scope contains
1881: * nothing but trailing whitespace.
1.77 kristaps 1882: */
1883:
1.238 schwarze 1884: if (buf->buf[pos] == '\0')
1.216 schwarze 1885: mandoc_msg(MANDOCERR_COND_EMPTY, r->parse,
1886: ln, ppos, roffs[tok].name);
1.77 kristaps 1887:
1.173 schwarze 1888: r->last->endspan = 1;
1.74 kristaps 1889:
1.173 schwarze 1890: out:
1.75 kristaps 1891: *offs = pos;
1892: return(ROFF_RERUN);
1.83 schwarze 1893: }
1894:
1895: static enum rofferr
1.92 schwarze 1896: roff_ds(ROFF_ARGS)
1897: {
1.212 schwarze 1898: char *string;
1899: const char *name;
1900: size_t namesz;
1.96 kristaps 1901:
1.251 schwarze 1902: /* Ignore groff compatibility mode for now. */
1903:
1904: if (tok == ROFF_ds1)
1905: tok = ROFF_ds;
1906: else if (tok == ROFF_as1)
1907: tok = ROFF_as;
1908:
1.96 kristaps 1909: /*
1.212 schwarze 1910: * The first word is the name of the string.
1911: * If it is empty or terminated by an escape sequence,
1912: * abort the `ds' request without defining anything.
1.96 kristaps 1913: */
1.92 schwarze 1914:
1.238 schwarze 1915: name = string = buf->buf + pos;
1916: if (*name == '\0')
1.92 schwarze 1917: return(ROFF_IGN);
1918:
1.212 schwarze 1919: namesz = roff_getname(r, &string, ln, pos);
1.238 schwarze 1920: if (name[namesz] == '\\')
1.212 schwarze 1921: return(ROFF_IGN);
1922:
1923: /* Read past the initial double-quote, if any. */
1.238 schwarze 1924: if (*string == '"')
1.92 schwarze 1925: string++;
1926:
1.96 kristaps 1927: /* The rest is the value. */
1.212 schwarze 1928: roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
1929: ROFF_as == tok);
1.92 schwarze 1930: return(ROFF_IGN);
1931: }
1932:
1.204 schwarze 1933: /*
1934: * Parse a single operator, one or two characters long.
1935: * If the operator is recognized, return success and advance the
1936: * parse point, else return failure and let the parse point unchanged.
1937: */
1938: static int
1939: roff_getop(const char *v, int *pos, char *res)
1940: {
1941:
1942: *res = v[*pos];
1943:
1944: switch (*res) {
1.207 schwarze 1945: case '+':
1.204 schwarze 1946: /* FALLTHROUGH */
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: break;
1959: case '<':
1960: switch (v[*pos + 1]) {
1.207 schwarze 1961: case '=':
1.204 schwarze 1962: *res = 'l';
1963: (*pos)++;
1964: break;
1.207 schwarze 1965: case '>':
1.204 schwarze 1966: *res = '!';
1967: (*pos)++;
1968: break;
1.207 schwarze 1969: case '?':
1.204 schwarze 1970: *res = 'i';
1971: (*pos)++;
1972: break;
1973: default:
1974: break;
1975: }
1976: break;
1977: case '>':
1978: switch (v[*pos + 1]) {
1.207 schwarze 1979: case '=':
1.204 schwarze 1980: *res = 'g';
1981: (*pos)++;
1982: break;
1.207 schwarze 1983: case '?':
1.204 schwarze 1984: *res = 'a';
1985: (*pos)++;
1986: break;
1987: default:
1988: break;
1989: }
1990: break;
1991: case '=':
1992: if ('=' == v[*pos + 1])
1993: (*pos)++;
1994: break;
1995: default:
1996: return(0);
1997: }
1998: (*pos)++;
1999:
2000: return(*res);
2001: }
2002:
2003: /*
2004: * Evaluate either a parenthesized numeric expression
2005: * or a single signed integer number.
2006: */
2007: static int
1.235 schwarze 2008: roff_evalpar(struct roff *r, int ln,
1.261 ! schwarze 2009: const char *v, int *pos, int *res, int flags)
1.204 schwarze 2010: {
2011:
2012: if ('(' != v[*pos])
1.261 ! schwarze 2013: return(roff_getnum(v, pos, res, flags));
1.204 schwarze 2014:
2015: (*pos)++;
1.261 ! schwarze 2016: if ( ! roff_evalnum(r, ln, v, pos, res, flags | ROFFNUM_WHITE))
1.204 schwarze 2017: return(0);
2018:
1.206 schwarze 2019: /*
2020: * Omission of the closing parenthesis
2021: * is an error in validation mode,
2022: * but ignored in evaluation mode.
2023: */
2024:
1.204 schwarze 2025: if (')' == v[*pos])
2026: (*pos)++;
1.206 schwarze 2027: else if (NULL == res)
2028: return(0);
1.204 schwarze 2029:
2030: return(1);
2031: }
2032:
2033: /*
2034: * Evaluate a complete numeric expression.
2035: * Proceed left to right, there is no concept of precedence.
2036: */
2037: static int
1.235 schwarze 2038: roff_evalnum(struct roff *r, int ln, const char *v,
1.261 ! schwarze 2039: int *pos, int *res, int flags)
1.204 schwarze 2040: {
2041: int mypos, operand2;
2042: char operator;
2043:
2044: if (NULL == pos) {
2045: mypos = 0;
2046: pos = &mypos;
2047: }
2048:
1.261 ! schwarze 2049: if (flags & ROFFNUM_WHITE)
1.204 schwarze 2050: while (isspace((unsigned char)v[*pos]))
2051: (*pos)++;
2052:
1.261 ! schwarze 2053: if ( ! roff_evalpar(r, ln, v, pos, res, flags))
1.204 schwarze 2054: return(0);
2055:
2056: while (1) {
1.261 ! schwarze 2057: if (flags & ROFFNUM_WHITE)
1.204 schwarze 2058: while (isspace((unsigned char)v[*pos]))
2059: (*pos)++;
2060:
2061: if ( ! roff_getop(v, pos, &operator))
2062: break;
2063:
1.261 ! schwarze 2064: if (flags & ROFFNUM_WHITE)
1.204 schwarze 2065: while (isspace((unsigned char)v[*pos]))
2066: (*pos)++;
2067:
1.261 ! schwarze 2068: if ( ! roff_evalpar(r, ln, v, pos, &operand2, flags))
1.204 schwarze 2069: return(0);
2070:
1.261 ! schwarze 2071: if (flags & ROFFNUM_WHITE)
1.204 schwarze 2072: while (isspace((unsigned char)v[*pos]))
2073: (*pos)++;
1.206 schwarze 2074:
2075: if (NULL == res)
2076: continue;
1.204 schwarze 2077:
2078: switch (operator) {
1.207 schwarze 2079: case '+':
1.204 schwarze 2080: *res += operand2;
2081: break;
1.207 schwarze 2082: case '-':
1.204 schwarze 2083: *res -= operand2;
2084: break;
1.207 schwarze 2085: case '*':
1.204 schwarze 2086: *res *= operand2;
2087: break;
1.207 schwarze 2088: case '/':
1.244 schwarze 2089: if (operand2 == 0) {
1.235 schwarze 2090: mandoc_msg(MANDOCERR_DIVZERO,
1.234 kristaps 2091: r->parse, ln, *pos, v);
2092: *res = 0;
2093: break;
2094: }
1.204 schwarze 2095: *res /= operand2;
2096: break;
1.207 schwarze 2097: case '%':
1.244 schwarze 2098: if (operand2 == 0) {
2099: mandoc_msg(MANDOCERR_DIVZERO,
2100: r->parse, ln, *pos, v);
2101: *res = 0;
2102: break;
2103: }
1.204 schwarze 2104: *res %= operand2;
2105: break;
1.207 schwarze 2106: case '<':
1.204 schwarze 2107: *res = *res < operand2;
2108: break;
1.207 schwarze 2109: case '>':
1.204 schwarze 2110: *res = *res > operand2;
2111: break;
1.207 schwarze 2112: case 'l':
1.204 schwarze 2113: *res = *res <= operand2;
2114: break;
1.207 schwarze 2115: case 'g':
1.204 schwarze 2116: *res = *res >= operand2;
2117: break;
1.207 schwarze 2118: case '=':
1.204 schwarze 2119: *res = *res == operand2;
2120: break;
1.207 schwarze 2121: case '!':
1.204 schwarze 2122: *res = *res != operand2;
2123: break;
1.207 schwarze 2124: case '&':
1.204 schwarze 2125: *res = *res && operand2;
2126: break;
1.207 schwarze 2127: case ':':
1.204 schwarze 2128: *res = *res || operand2;
2129: break;
1.207 schwarze 2130: case 'i':
1.204 schwarze 2131: if (operand2 < *res)
2132: *res = operand2;
2133: break;
1.207 schwarze 2134: case 'a':
1.204 schwarze 2135: if (operand2 > *res)
2136: *res = operand2;
2137: break;
2138: default:
2139: abort();
2140: }
2141: }
2142: return(1);
2143: }
2144:
1.180 schwarze 2145: void
1.187 schwarze 2146: roff_setreg(struct roff *r, const char *name, int val, char sign)
1.147 kristaps 2147: {
1.180 schwarze 2148: struct roffreg *reg;
2149:
2150: /* Search for an existing register with the same name. */
2151: reg = r->regtab;
2152:
2153: while (reg && strcmp(name, reg->key.p))
2154: reg = reg->next;
1.147 kristaps 2155:
1.180 schwarze 2156: if (NULL == reg) {
2157: /* Create a new register. */
2158: reg = mandoc_malloc(sizeof(struct roffreg));
2159: reg->key.p = mandoc_strdup(name);
2160: reg->key.sz = strlen(name);
1.187 schwarze 2161: reg->val = 0;
1.180 schwarze 2162: reg->next = r->regtab;
2163: r->regtab = reg;
2164: }
2165:
1.187 schwarze 2166: if ('+' == sign)
2167: reg->val += val;
2168: else if ('-' == sign)
2169: reg->val -= val;
2170: else
2171: reg->val = val;
1.147 kristaps 2172: }
2173:
1.192 schwarze 2174: /*
2175: * Handle some predefined read-only number registers.
2176: * For now, return -1 if the requested register is not predefined;
2177: * in case a predefined read-only register having the value -1
2178: * were to turn up, another special value would have to be chosen.
2179: */
2180: static int
2181: roff_getregro(const char *name)
2182: {
2183:
2184: switch (*name) {
1.207 schwarze 2185: case 'A': /* ASCII approximation mode is always off. */
1.192 schwarze 2186: return(0);
1.207 schwarze 2187: case 'g': /* Groff compatibility mode is always on. */
1.192 schwarze 2188: return(1);
1.207 schwarze 2189: case 'H': /* Fixed horizontal resolution. */
1.192 schwarze 2190: return (24);
1.207 schwarze 2191: case 'j': /* Always adjust left margin only. */
1.192 schwarze 2192: return(0);
1.207 schwarze 2193: case 'T': /* Some output device is always defined. */
1.192 schwarze 2194: return(1);
1.207 schwarze 2195: case 'V': /* Fixed vertical resolution. */
1.192 schwarze 2196: return (40);
2197: default:
2198: return (-1);
2199: }
2200: }
2201:
1.181 schwarze 2202: int
1.180 schwarze 2203: roff_getreg(const struct roff *r, const char *name)
1.147 kristaps 2204: {
1.180 schwarze 2205: struct roffreg *reg;
1.192 schwarze 2206: int val;
2207:
2208: if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) {
2209: val = roff_getregro(name + 1);
2210: if (-1 != val)
2211: return (val);
2212: }
1.180 schwarze 2213:
2214: for (reg = r->regtab; reg; reg = reg->next)
2215: if (0 == strcmp(name, reg->key.p))
1.181 schwarze 2216: return(reg->val);
2217:
2218: return(0);
2219: }
2220:
2221: static int
2222: roff_getregn(const struct roff *r, const char *name, size_t len)
2223: {
2224: struct roffreg *reg;
1.192 schwarze 2225: int val;
2226:
2227: if ('.' == name[0] && 2 == len) {
2228: val = roff_getregro(name + 1);
2229: if (-1 != val)
2230: return (val);
2231: }
1.181 schwarze 2232:
2233: for (reg = r->regtab; reg; reg = reg->next)
2234: if (len == reg->key.sz &&
2235: 0 == strncmp(name, reg->key.p, len))
2236: return(reg->val);
1.147 kristaps 2237:
1.180 schwarze 2238: return(0);
1.147 kristaps 2239: }
2240:
1.180 schwarze 2241: static void
2242: roff_freereg(struct roffreg *reg)
1.147 kristaps 2243: {
1.180 schwarze 2244: struct roffreg *old_reg;
1.147 kristaps 2245:
1.180 schwarze 2246: while (NULL != reg) {
2247: free(reg->key.p);
2248: old_reg = reg;
2249: reg = reg->next;
2250: free(old_reg);
2251: }
1.147 kristaps 2252: }
1.92 schwarze 2253:
2254: static enum rofferr
1.89 kristaps 2255: roff_nr(ROFF_ARGS)
1.83 schwarze 2256: {
1.212 schwarze 2257: char *key, *val;
2258: size_t keysz;
1.138 kristaps 2259: int iv;
1.187 schwarze 2260: char sign;
1.89 kristaps 2261:
1.238 schwarze 2262: key = val = buf->buf + pos;
2263: if (*key == '\0')
1.212 schwarze 2264: return(ROFF_IGN);
2265:
2266: keysz = roff_getname(r, &val, ln, pos);
1.238 schwarze 2267: if (key[keysz] == '\\')
1.212 schwarze 2268: return(ROFF_IGN);
2269: key[keysz] = '\0';
1.89 kristaps 2270:
1.187 schwarze 2271: sign = *val;
1.238 schwarze 2272: if (sign == '+' || sign == '-')
1.187 schwarze 2273: val++;
2274:
1.261 ! schwarze 2275: if (roff_evalnum(r, ln, val, NULL, &iv, ROFFNUM_SCALE))
1.204 schwarze 2276: roff_setreg(r, key, iv, sign);
1.109 kristaps 2277:
1.203 schwarze 2278: return(ROFF_IGN);
2279: }
2280:
2281: static enum rofferr
2282: roff_rr(ROFF_ARGS)
2283: {
2284: struct roffreg *reg, **prev;
1.212 schwarze 2285: char *name, *cp;
2286: size_t namesz;
1.203 schwarze 2287:
1.238 schwarze 2288: name = cp = buf->buf + pos;
2289: if (*name == '\0')
1.212 schwarze 2290: return(ROFF_IGN);
2291: namesz = roff_getname(r, &cp, ln, pos);
2292: name[namesz] = '\0';
1.203 schwarze 2293:
2294: prev = &r->regtab;
2295: while (1) {
2296: reg = *prev;
1.238 schwarze 2297: if (reg == NULL || !strcmp(name, reg->key.p))
1.203 schwarze 2298: break;
2299: prev = ®->next;
2300: }
1.238 schwarze 2301: if (reg != NULL) {
1.203 schwarze 2302: *prev = reg->next;
2303: free(reg->key.p);
2304: free(reg);
2305: }
1.122 schwarze 2306: return(ROFF_IGN);
2307: }
2308:
2309: static enum rofferr
2310: roff_rm(ROFF_ARGS)
2311: {
2312: const char *name;
2313: char *cp;
1.212 schwarze 2314: size_t namesz;
1.122 schwarze 2315:
1.238 schwarze 2316: cp = buf->buf + pos;
2317: while (*cp != '\0') {
1.212 schwarze 2318: name = cp;
1.238 schwarze 2319: namesz = roff_getname(r, &cp, ln, (int)(cp - buf->buf));
1.212 schwarze 2320: roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
1.238 schwarze 2321: if (name[namesz] == '\\')
1.212 schwarze 2322: break;
1.122 schwarze 2323: }
1.178 schwarze 2324: return(ROFF_IGN);
2325: }
2326:
2327: static enum rofferr
2328: roff_it(ROFF_ARGS)
2329: {
2330: int iv;
2331:
2332: /* Parse the number of lines. */
1.261 ! schwarze 2333:
! 2334: if ( ! roff_evalnum(r, ln, buf->buf, &pos, &iv, 0)) {
1.222 schwarze 2335: mandoc_msg(MANDOCERR_IT_NONUM, r->parse,
1.238 schwarze 2336: ln, ppos, buf->buf + 1);
1.178 schwarze 2337: return(ROFF_IGN);
2338: }
2339:
2340: /* Arm the input line trap. */
1.261 ! schwarze 2341:
1.178 schwarze 2342: roffit_lines = iv;
1.261 ! schwarze 2343: roffit_macro = mandoc_strdup(buf->buf + pos);
1.109 kristaps 2344: return(ROFF_IGN);
1.175 schwarze 2345: }
2346:
2347: static enum rofferr
2348: roff_Dd(ROFF_ARGS)
2349: {
2350: const char *const *cp;
2351:
1.227 schwarze 2352: if ((r->options & (MPARSE_MDOC | MPARSE_QUICK)) == 0)
1.175 schwarze 2353: for (cp = __mdoc_reserved; *cp; cp++)
2354: roff_setstr(r, *cp, NULL, 0);
2355:
1.227 schwarze 2356: if (r->format == 0)
2357: r->format = MPARSE_MDOC;
2358:
1.175 schwarze 2359: return(ROFF_CONT);
2360: }
2361:
2362: static enum rofferr
2363: roff_TH(ROFF_ARGS)
2364: {
2365: const char *const *cp;
2366:
1.227 schwarze 2367: if ((r->options & MPARSE_QUICK) == 0)
1.175 schwarze 2368: for (cp = __man_reserved; *cp; cp++)
2369: roff_setstr(r, *cp, NULL, 0);
2370:
1.227 schwarze 2371: if (r->format == 0)
2372: r->format = MPARSE_MAN;
2373:
1.175 schwarze 2374: return(ROFF_CONT);
1.109 kristaps 2375: }
2376:
2377: static enum rofferr
2378: roff_TE(ROFF_ARGS)
2379: {
2380:
2381: if (NULL == r->tbl)
1.221 schwarze 2382: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
2383: ln, ppos, "TE");
1.258 schwarze 2384: else if ( ! tbl_end(&r->tbl)) {
2385: free(buf->buf);
2386: buf->buf = mandoc_strdup(".sp");
2387: buf->sz = 4;
2388: return(ROFF_REPARSE);
2389: }
1.112 kristaps 2390: return(ROFF_IGN);
2391: }
2392:
2393: static enum rofferr
2394: roff_T_(ROFF_ARGS)
2395: {
2396:
2397: if (NULL == r->tbl)
1.221 schwarze 2398: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
2399: ln, ppos, "T&");
1.112 kristaps 2400: else
1.116 kristaps 2401: tbl_restart(ppos, ln, r->tbl);
1.112 kristaps 2402:
1.109 kristaps 2403: return(ROFF_IGN);
2404: }
2405:
1.230 schwarze 2406: /*
2407: * Handle in-line equation delimiters.
2408: */
2409: static enum rofferr
1.238 schwarze 2410: roff_eqndelim(struct roff *r, struct buf *buf, int pos)
1.151 kristaps 2411: {
1.233 schwarze 2412: char *cp1, *cp2;
2413: const char *bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
1.151 kristaps 2414:
1.230 schwarze 2415: /*
2416: * Outside equations, look for an opening delimiter.
2417: * If we are inside an equation, we already know it is
2418: * in-line, or this function wouldn't have been called;
2419: * so look for a closing delimiter.
2420: */
2421:
1.238 schwarze 2422: cp1 = buf->buf + pos;
1.230 schwarze 2423: cp2 = strchr(cp1, r->eqn == NULL ?
2424: r->last_eqn->odelim : r->last_eqn->cdelim);
2425: if (cp2 == NULL)
2426: return(ROFF_CONT);
2427:
1.233 schwarze 2428: *cp2++ = '\0';
2429: bef_pr = bef_nl = aft_nl = aft_pr = "";
2430:
2431: /* Handle preceding text, protecting whitespace. */
2432:
1.238 schwarze 2433: if (*buf->buf != '\0') {
1.233 schwarze 2434: if (r->eqn == NULL)
2435: bef_pr = "\\&";
2436: bef_nl = "\n";
2437: }
2438:
2439: /*
2440: * Prepare replacing the delimiter with an equation macro
2441: * and drop leading white space from the equation.
2442: */
1.230 schwarze 2443:
1.233 schwarze 2444: if (r->eqn == NULL) {
2445: while (*cp2 == ' ')
2446: cp2++;
2447: mac = ".EQ";
2448: } else
2449: mac = ".EN";
2450:
2451: /* Handle following text, protecting whitespace. */
2452:
2453: if (*cp2 != '\0') {
2454: aft_nl = "\n";
2455: if (r->eqn != NULL)
2456: aft_pr = "\\&";
2457: }
2458:
2459: /* Do the actual replacement. */
2460:
1.238 schwarze 2461: buf->sz = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", buf->buf,
1.233 schwarze 2462: bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
1.238 schwarze 2463: free(buf->buf);
2464: buf->buf = cp1;
1.230 schwarze 2465:
2466: /* Toggle the in-line state of the eqn subsystem. */
2467:
2468: r->eqn_inline = r->eqn == NULL;
2469: return(ROFF_REPARSE);
1.151 kristaps 2470: }
2471:
1.235 schwarze 2472: static enum rofferr
2473: roff_EQ(ROFF_ARGS)
1.125 kristaps 2474: {
1.151 kristaps 2475: struct eqn_node *e;
1.125 kristaps 2476:
1.238 schwarze 2477: assert(r->eqn == NULL);
1.235 schwarze 2478: e = eqn_alloc(ppos, ln, r->parse);
1.125 kristaps 2479:
1.230 schwarze 2480: if (r->last_eqn) {
1.125 kristaps 2481: r->last_eqn->next = e;
1.230 schwarze 2482: e->delim = r->last_eqn->delim;
2483: e->odelim = r->last_eqn->odelim;
2484: e->cdelim = r->last_eqn->cdelim;
2485: } else
1.125 kristaps 2486: r->first_eqn = r->last_eqn = e;
2487:
2488: r->eqn = r->last_eqn = e;
1.151 kristaps 2489:
1.238 schwarze 2490: if (buf->buf[pos] != '\0')
1.235 schwarze 2491: mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
1.238 schwarze 2492: ".EQ %s", buf->buf + pos);
1.151 kristaps 2493:
1.125 kristaps 2494: return(ROFF_IGN);
2495: }
2496:
2497: static enum rofferr
2498: roff_EN(ROFF_ARGS)
2499: {
2500:
1.221 schwarze 2501: mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse, ln, ppos, "EN");
1.125 kristaps 2502: return(ROFF_IGN);
2503: }
2504:
2505: static enum rofferr
1.109 kristaps 2506: roff_TS(ROFF_ARGS)
2507: {
1.176 schwarze 2508: struct tbl_node *tbl;
1.89 kristaps 2509:
1.115 kristaps 2510: if (r->tbl) {
1.221 schwarze 2511: mandoc_msg(MANDOCERR_BLK_BROKEN, r->parse,
2512: ln, ppos, "TS breaks TS");
1.151 kristaps 2513: tbl_end(&r->tbl);
1.115 kristaps 2514: }
1.83 schwarze 2515:
1.176 schwarze 2516: tbl = tbl_alloc(ppos, ln, r->parse);
1.113 kristaps 2517:
2518: if (r->last_tbl)
1.176 schwarze 2519: r->last_tbl->next = tbl;
1.113 kristaps 2520: else
1.176 schwarze 2521: r->first_tbl = r->last_tbl = tbl;
1.113 kristaps 2522:
1.176 schwarze 2523: r->tbl = r->last_tbl = tbl;
1.83 schwarze 2524: return(ROFF_IGN);
1.251 schwarze 2525: }
2526:
2527: static enum rofferr
2528: roff_brp(ROFF_ARGS)
2529: {
2530:
2531: buf->buf[pos - 1] = '\0';
2532: return(ROFF_CONT);
1.92 schwarze 2533: }
2534:
1.105 kristaps 2535: static enum rofferr
1.174 kristaps 2536: roff_cc(ROFF_ARGS)
2537: {
2538: const char *p;
2539:
1.238 schwarze 2540: p = buf->buf + pos;
1.174 kristaps 2541:
1.238 schwarze 2542: if (*p == '\0' || (r->control = *p++) == '.')
1.174 kristaps 2543: r->control = 0;
2544:
1.238 schwarze 2545: if (*p != '\0')
1.260 schwarze 2546: mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
2547: ln, p - buf->buf, "cc ... %s", p);
1.174 kristaps 2548:
2549: return(ROFF_IGN);
2550: }
2551:
2552: static enum rofferr
1.164 kristaps 2553: roff_tr(ROFF_ARGS)
2554: {
2555: const char *p, *first, *second;
2556: size_t fsz, ssz;
2557: enum mandoc_esc esc;
2558:
1.238 schwarze 2559: p = buf->buf + pos;
1.164 kristaps 2560:
1.238 schwarze 2561: if (*p == '\0') {
1.260 schwarze 2562: mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse, ln, ppos, "tr");
1.164 kristaps 2563: return(ROFF_IGN);
2564: }
2565:
1.238 schwarze 2566: while (*p != '\0') {
1.164 kristaps 2567: fsz = ssz = 1;
2568:
2569: first = p++;
1.238 schwarze 2570: if (*first == '\\') {
1.164 kristaps 2571: esc = mandoc_escape(&p, NULL, NULL);
1.238 schwarze 2572: if (esc == ESCAPE_ERROR) {
1.219 schwarze 2573: mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238 schwarze 2574: ln, (int)(p - buf->buf), first);
1.164 kristaps 2575: return(ROFF_IGN);
2576: }
2577: fsz = (size_t)(p - first);
2578: }
2579:
2580: second = p++;
1.238 schwarze 2581: if (*second == '\\') {
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), second);
1.164 kristaps 2586: return(ROFF_IGN);
2587: }
2588: ssz = (size_t)(p - second);
1.238 schwarze 2589: } else if (*second == '\0') {
1.260 schwarze 2590: mandoc_vmsg(MANDOCERR_TR_ODD, r->parse,
2591: ln, first - buf->buf, "tr %s", first);
1.164 kristaps 2592: second = " ";
1.165 kristaps 2593: p--;
1.164 kristaps 2594: }
2595:
1.167 kristaps 2596: if (fsz > 1) {
1.207 schwarze 2597: roff_setstrn(&r->xmbtab, first, fsz,
2598: second, ssz, 0);
1.167 kristaps 2599: continue;
2600: }
2601:
1.238 schwarze 2602: if (r->xtab == NULL)
1.207 schwarze 2603: r->xtab = mandoc_calloc(128,
2604: sizeof(struct roffstr));
1.167 kristaps 2605:
2606: free(r->xtab[(int)*first].p);
2607: r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
2608: r->xtab[(int)*first].sz = ssz;
1.164 kristaps 2609: }
2610:
2611: return(ROFF_IGN);
2612: }
2613:
2614: static enum rofferr
1.105 kristaps 2615: roff_so(ROFF_ARGS)
2616: {
1.249 schwarze 2617: char *name, *cp;
1.105 kristaps 2618:
1.238 schwarze 2619: name = buf->buf + pos;
1.224 schwarze 2620: mandoc_vmsg(MANDOCERR_SO, r->parse, ln, ppos, "so %s", name);
1.105 kristaps 2621:
2622: /*
2623: * Handle `so'. Be EXTREMELY careful, as we shouldn't be
2624: * opening anything that's not in our cwd or anything beneath
2625: * it. Thus, explicitly disallow traversing up the file-system
2626: * or using absolute paths.
2627: */
2628:
1.238 schwarze 2629: if (*name == '/' || strstr(name, "../") || strstr(name, "/..")) {
1.210 schwarze 2630: mandoc_vmsg(MANDOCERR_SO_PATH, r->parse, ln, ppos,
2631: ".so %s", name);
1.249 schwarze 2632: buf->sz = mandoc_asprintf(&cp,
2633: ".sp\nSee the file %s.\n.sp", name) + 1;
2634: free(buf->buf);
2635: buf->buf = cp;
2636: *offs = 0;
2637: return(ROFF_REPARSE);
1.105 kristaps 2638: }
2639:
2640: *offs = pos;
2641: return(ROFF_SO);
2642: }
1.92 schwarze 2643:
1.106 kristaps 2644: static enum rofferr
2645: roff_userdef(ROFF_ARGS)
1.99 kristaps 2646: {
1.106 kristaps 2647: const char *arg[9];
2648: char *cp, *n1, *n2;
1.119 schwarze 2649: int i;
1.106 kristaps 2650:
2651: /*
2652: * Collect pointers to macro argument strings
1.188 schwarze 2653: * and NUL-terminate them.
1.106 kristaps 2654: */
1.238 schwarze 2655: cp = buf->buf + pos;
1.119 schwarze 2656: for (i = 0; i < 9; i++)
1.238 schwarze 2657: arg[i] = *cp == '\0' ? "" :
1.136 kristaps 2658: mandoc_getarg(r->parse, &cp, ln, &pos);
1.99 kristaps 2659:
1.106 kristaps 2660: /*
2661: * Expand macro arguments.
1.99 kristaps 2662: */
1.238 schwarze 2663: buf->sz = 0;
1.106 kristaps 2664: n1 = cp = mandoc_strdup(r->current_string);
1.238 schwarze 2665: while ((cp = strstr(cp, "\\$")) != NULL) {
1.106 kristaps 2666: i = cp[2] - '1';
2667: if (0 > i || 8 < i) {
2668: /* Not an argument invocation. */
2669: cp += 2;
2670: continue;
2671: }
1.209 schwarze 2672: *cp = '\0';
1.238 schwarze 2673: buf->sz = mandoc_asprintf(&n2, "%s%s%s",
1.209 schwarze 2674: n1, arg[i], cp + 3) + 1;
1.106 kristaps 2675: cp = n2 + (cp - n1);
2676: free(n1);
2677: n1 = n2;
1.99 kristaps 2678: }
2679:
1.106 kristaps 2680: /*
2681: * Replace the macro invocation
2682: * by the expanded macro.
2683: */
1.238 schwarze 2684: free(buf->buf);
2685: buf->buf = n1;
2686: if (buf->sz == 0)
2687: buf->sz = strlen(buf->buf) + 1;
1.248 schwarze 2688: *offs = 0;
1.106 kristaps 2689:
1.238 schwarze 2690: return(buf->sz > 1 && buf->buf[buf->sz - 2] == '\n' ?
1.106 kristaps 2691: ROFF_REPARSE : ROFF_APPEND);
1.99 kristaps 2692: }
1.121 schwarze 2693:
1.212 schwarze 2694: static size_t
1.121 schwarze 2695: roff_getname(struct roff *r, char **cpp, int ln, int pos)
2696: {
2697: char *name, *cp;
1.212 schwarze 2698: size_t namesz;
1.121 schwarze 2699:
2700: name = *cpp;
2701: if ('\0' == *name)
1.212 schwarze 2702: return(0);
1.121 schwarze 2703:
1.212 schwarze 2704: /* Read until end of name and terminate it with NUL. */
2705: for (cp = name; 1; cp++) {
2706: if ('\0' == *cp || ' ' == *cp) {
2707: namesz = cp - name;
2708: break;
2709: }
1.121 schwarze 2710: if ('\\' != *cp)
2711: continue;
1.215 schwarze 2712: namesz = cp - name;
2713: if ('{' == cp[1] || '}' == cp[1])
2714: break;
1.121 schwarze 2715: cp++;
2716: if ('\\' == *cp)
2717: continue;
1.224 schwarze 2718: mandoc_vmsg(MANDOCERR_NAMESC, r->parse, ln, pos,
2719: "%.*s", (int)(cp - name + 1), name);
1.212 schwarze 2720: mandoc_escape((const char **)&cp, NULL, NULL);
2721: break;
1.121 schwarze 2722: }
2723:
2724: /* Read past spaces. */
2725: while (' ' == *cp)
2726: cp++;
2727:
2728: *cpp = cp;
1.212 schwarze 2729: return(namesz);
1.121 schwarze 2730: }
2731:
1.106 kristaps 2732: /*
2733: * Store *string into the user-defined string called *name.
2734: * To clear an existing entry, call with (*r, *name, NULL, 0).
1.193 schwarze 2735: * append == 0: replace mode
2736: * append == 1: single-line append mode
2737: * append == 2: multiline append mode, append '\n' after each call
1.106 kristaps 2738: */
1.94 kristaps 2739: static void
1.106 kristaps 2740: roff_setstr(struct roff *r, const char *name, const char *string,
1.193 schwarze 2741: int append)
1.92 schwarze 2742: {
1.164 kristaps 2743:
2744: roff_setstrn(&r->strtab, name, strlen(name), string,
1.207 schwarze 2745: string ? strlen(string) : 0, append);
1.164 kristaps 2746: }
2747:
2748: static void
1.166 kristaps 2749: roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1.193 schwarze 2750: const char *string, size_t stringsz, int append)
1.164 kristaps 2751: {
1.166 kristaps 2752: struct roffkv *n;
1.164 kristaps 2753: char *c;
2754: int i;
2755: size_t oldch, newch;
1.92 schwarze 2756:
1.106 kristaps 2757: /* Search for an existing string with the same name. */
1.164 kristaps 2758: n = *r;
2759:
1.211 schwarze 2760: while (n && (namesz != n->key.sz ||
2761: strncmp(n->key.p, name, namesz)))
1.92 schwarze 2762: n = n->next;
1.94 kristaps 2763:
2764: if (NULL == n) {
1.106 kristaps 2765: /* Create a new string table entry. */
1.166 kristaps 2766: n = mandoc_malloc(sizeof(struct roffkv));
2767: n->key.p = mandoc_strndup(name, namesz);
2768: n->key.sz = namesz;
2769: n->val.p = NULL;
2770: n->val.sz = 0;
1.164 kristaps 2771: n->next = *r;
2772: *r = n;
1.193 schwarze 2773: } else if (0 == append) {
1.166 kristaps 2774: free(n->val.p);
2775: n->val.p = NULL;
2776: n->val.sz = 0;
1.106 kristaps 2777: }
2778:
2779: if (NULL == string)
2780: return;
2781:
2782: /*
2783: * One additional byte for the '\n' in multiline mode,
2784: * and one for the terminating '\0'.
2785: */
1.193 schwarze 2786: newch = stringsz + (1 < append ? 2u : 1u);
1.164 kristaps 2787:
1.166 kristaps 2788: if (NULL == n->val.p) {
2789: n->val.p = mandoc_malloc(newch);
2790: *n->val.p = '\0';
1.106 kristaps 2791: oldch = 0;
2792: } else {
1.166 kristaps 2793: oldch = n->val.sz;
2794: n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1.106 kristaps 2795: }
2796:
2797: /* Skip existing content in the destination buffer. */
1.166 kristaps 2798: c = n->val.p + (int)oldch;
1.106 kristaps 2799:
2800: /* Append new content to the destination buffer. */
1.164 kristaps 2801: i = 0;
2802: while (i < (int)stringsz) {
1.106 kristaps 2803: /*
2804: * Rudimentary roff copy mode:
2805: * Handle escaped backslashes.
2806: */
1.164 kristaps 2807: if ('\\' == string[i] && '\\' == string[i + 1])
2808: i++;
2809: *c++ = string[i++];
1.106 kristaps 2810: }
1.94 kristaps 2811:
1.106 kristaps 2812: /* Append terminating bytes. */
1.193 schwarze 2813: if (1 < append)
1.106 kristaps 2814: *c++ = '\n';
1.163 kristaps 2815:
1.106 kristaps 2816: *c = '\0';
1.166 kristaps 2817: n->val.sz = (int)(c - n->val.p);
1.92 schwarze 2818: }
2819:
1.94 kristaps 2820: static const char *
2821: roff_getstrn(const struct roff *r, const char *name, size_t len)
1.92 schwarze 2822: {
1.166 kristaps 2823: const struct roffkv *n;
1.191 schwarze 2824: int i;
1.92 schwarze 2825:
1.164 kristaps 2826: for (n = r->strtab; n; n = n->next)
1.207 schwarze 2827: if (0 == strncmp(name, n->key.p, len) &&
2828: '\0' == n->key.p[(int)len])
1.166 kristaps 2829: return(n->val.p);
1.191 schwarze 2830:
2831: for (i = 0; i < PREDEFS_MAX; i++)
2832: if (0 == strncmp(name, predefs[i].name, len) &&
2833: '\0' == predefs[i].name[(int)len])
2834: return(predefs[i].str);
1.94 kristaps 2835:
1.157 kristaps 2836: return(NULL);
1.92 schwarze 2837: }
2838:
1.94 kristaps 2839: static void
1.167 kristaps 2840: roff_freestr(struct roffkv *r)
1.92 schwarze 2841: {
1.166 kristaps 2842: struct roffkv *n, *nn;
1.92 schwarze 2843:
1.167 kristaps 2844: for (n = r; n; n = nn) {
1.166 kristaps 2845: free(n->key.p);
2846: free(n->val.p);
1.92 schwarze 2847: nn = n->next;
2848: free(n);
2849: }
1.114 kristaps 2850: }
2851:
2852: const struct tbl_span *
2853: roff_span(const struct roff *r)
2854: {
1.207 schwarze 2855:
1.114 kristaps 2856: return(r->tbl ? tbl_span(r->tbl) : NULL);
1.125 kristaps 2857: }
2858:
2859: const struct eqn *
2860: roff_eqn(const struct roff *r)
2861: {
1.207 schwarze 2862:
1.125 kristaps 2863: return(r->last_eqn ? &r->last_eqn->eqn : NULL);
1.164 kristaps 2864: }
2865:
2866: /*
2867: * Duplicate an input string, making the appropriate character
2868: * conversations (as stipulated by `tr') along the way.
2869: * Returns a heap-allocated string with all the replacements made.
2870: */
2871: char *
2872: roff_strdup(const struct roff *r, const char *p)
2873: {
1.166 kristaps 2874: const struct roffkv *cp;
1.164 kristaps 2875: char *res;
2876: const char *pp;
2877: size_t ssz, sz;
2878: enum mandoc_esc esc;
2879:
1.167 kristaps 2880: if (NULL == r->xmbtab && NULL == r->xtab)
1.164 kristaps 2881: return(mandoc_strdup(p));
2882: else if ('\0' == *p)
2883: return(mandoc_strdup(""));
2884:
2885: /*
2886: * Step through each character looking for term matches
2887: * (remember that a `tr' can be invoked with an escape, which is
2888: * a glyph but the escape is multi-character).
2889: * We only do this if the character hash has been initialised
2890: * and the string is >0 length.
2891: */
2892:
2893: res = NULL;
2894: ssz = 0;
2895:
2896: while ('\0' != *p) {
1.167 kristaps 2897: if ('\\' != *p && r->xtab && r->xtab[(int)*p].p) {
2898: sz = r->xtab[(int)*p].sz;
2899: res = mandoc_realloc(res, ssz + sz + 1);
2900: memcpy(res + ssz, r->xtab[(int)*p].p, sz);
2901: ssz += sz;
2902: p++;
2903: continue;
2904: } else if ('\\' != *p) {
2905: res = mandoc_realloc(res, ssz + 2);
2906: res[ssz++] = *p++;
2907: continue;
2908: }
2909:
1.164 kristaps 2910: /* Search for term matches. */
1.167 kristaps 2911: for (cp = r->xmbtab; cp; cp = cp->next)
1.166 kristaps 2912: if (0 == strncmp(p, cp->key.p, cp->key.sz))
1.164 kristaps 2913: break;
2914:
2915: if (NULL != cp) {
2916: /*
2917: * A match has been found.
2918: * Append the match to the array and move
2919: * forward by its keysize.
2920: */
1.207 schwarze 2921: res = mandoc_realloc(res,
2922: ssz + cp->val.sz + 1);
1.166 kristaps 2923: memcpy(res + ssz, cp->val.p, cp->val.sz);
2924: ssz += cp->val.sz;
2925: p += (int)cp->key.sz;
1.164 kristaps 2926: continue;
2927: }
2928:
1.167 kristaps 2929: /*
2930: * Handle escapes carefully: we need to copy
2931: * over just the escape itself, or else we might
2932: * do replacements within the escape itself.
2933: * Make sure to pass along the bogus string.
2934: */
2935: pp = p++;
2936: esc = mandoc_escape(&p, NULL, NULL);
2937: if (ESCAPE_ERROR == esc) {
2938: sz = strlen(pp);
1.164 kristaps 2939: res = mandoc_realloc(res, ssz + sz + 1);
2940: memcpy(res + ssz, pp, sz);
1.167 kristaps 2941: break;
1.164 kristaps 2942: }
1.207 schwarze 2943: /*
2944: * We bail out on bad escapes.
1.167 kristaps 2945: * No need to warn: we already did so when
2946: * roff_res() was called.
2947: */
2948: sz = (int)(p - pp);
2949: res = mandoc_realloc(res, ssz + sz + 1);
2950: memcpy(res + ssz, pp, sz);
2951: ssz += sz;
1.164 kristaps 2952: }
2953:
2954: res[(int)ssz] = '\0';
2955: return(res);
1.227 schwarze 2956: }
2957:
2958: int
2959: roff_getformat(const struct roff *r)
2960: {
2961:
2962: return(r->format);
1.174 kristaps 2963: }
2964:
2965: /*
1.207 schwarze 2966: * Find out whether a line is a macro line or not.
1.174 kristaps 2967: * If it is, adjust the current position and return one; if it isn't,
2968: * return zero and don't change the current position.
2969: * If the control character has been set with `.cc', then let that grain
2970: * precedence.
2971: * This is slighly contrary to groff, where using the non-breaking
2972: * control character when `cc' has been invoked will cause the
2973: * non-breaking macro contents to be printed verbatim.
2974: */
2975: int
2976: roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
2977: {
2978: int pos;
2979:
2980: pos = *ppos;
2981:
2982: if (0 != r->control && cp[pos] == r->control)
2983: pos++;
2984: else if (0 != r->control)
2985: return(0);
2986: else if ('\\' == cp[pos] && '.' == cp[pos + 1])
2987: pos += 2;
2988: else if ('.' == cp[pos] || '\'' == cp[pos])
2989: pos++;
2990: else
2991: return(0);
2992:
2993: while (' ' == cp[pos] || '\t' == cp[pos])
2994: pos++;
2995:
2996: *ppos = pos;
2997: return(1);
1.74 kristaps 2998: }
CVSweb