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