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