]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.282 2015/10/21 23:51:11 schwarze Exp $ */
3 * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2015 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
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.
20 #include <sys/types.h>
30 #include "mandoc_aux.h"
32 #include "libmandoc.h"
36 /* Maximum number of string expansions per line, to break infinite loops. */
37 #define EXPAND_LIMIT 1000
39 /* --- data types --------------------------------------------------------- */
122 /* MAN_fi; ignored in mdoc(7) */
131 /* MAN_ft; ignored in mdoc(7) */
153 /* MAN_in; ignored in mdoc(7) */
171 /* MAN_ll, MDOC_ll */
185 /* MAN_nf; ignored in mdoc(7) */
236 /* MAN_sp, MDOC_sp */
289 * An incredibly-simple string buffer.
292 char *p
; /* nil-terminated buffer */
293 size_t sz
; /* saved strlen(p) */
297 * A key-value roffstr pair as part of a singly-linked list.
302 struct roffkv
*next
; /* next in list */
306 * A single number register as part of a singly-linked list.
311 struct roffreg
*next
;
315 struct mparse
*parse
; /* parse point */
316 struct roffnode
*last
; /* leaf of stack */
317 int *rstack
; /* stack of inverted `ie' values */
318 struct roffreg
*regtab
; /* number registers */
319 struct roffkv
*strtab
; /* user-defined strings & macros */
320 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
321 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
322 const char *current_string
; /* value of last called user macro */
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 */
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 */
329 int eqn_inline
; /* current equation is inline */
330 int options
; /* parse options */
331 int rstacksz
; /* current size limit of rstack */
332 int rstackpos
; /* position in rstack */
333 int format
; /* current file in mdoc or man format */
334 int argc
; /* number of args of the last macro */
335 char control
; /* control character */
339 enum rofft tok
; /* type of node */
340 struct roffnode
*parent
; /* up one in stack */
341 int line
; /* parse line */
342 int col
; /* parse col */
343 char *name
; /* node name, e.g. macro name */
344 char *end
; /* end-rules: custom token */
345 int endspan
; /* end-rules: next-line or infty */
346 int rule
; /* current evaluation rule */
349 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
350 enum rofft tok, /* tok of macro */ \
351 struct buf *buf, /* input buffer */ \
352 int ln, /* parse line */ \
353 int ppos, /* original pos in buffer */ \
354 int pos, /* current pos in buffer */ \
355 int *offs /* reset offset of buffer data */
357 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
360 const char *name
; /* macro name */
361 roffproc proc
; /* process new macro */
362 roffproc text
; /* process as child text of macro */
363 roffproc sub
; /* process as child of macro */
365 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
366 struct roffmac
*next
;
370 const char *name
; /* predefined input name */
371 const char *str
; /* replacement symbol */
374 #define PREDEF(__name, __str) \
375 { (__name), (__str) },
377 /* --- function prototypes ------------------------------------------------ */
379 static enum rofft
roffhash_find(const char *, size_t);
380 static void roffhash_init(void);
381 static void roffnode_cleanscope(struct roff
*);
382 static void roffnode_pop(struct roff
*);
383 static void roffnode_push(struct roff
*, enum rofft
,
384 const char *, int, int);
385 static enum rofferr
roff_block(ROFF_ARGS
);
386 static enum rofferr
roff_block_text(ROFF_ARGS
);
387 static enum rofferr
roff_block_sub(ROFF_ARGS
);
388 static enum rofferr
roff_brp(ROFF_ARGS
);
389 static enum rofferr
roff_cblock(ROFF_ARGS
);
390 static enum rofferr
roff_cc(ROFF_ARGS
);
391 static void roff_ccond(struct roff
*, int, int);
392 static enum rofferr
roff_cond(ROFF_ARGS
);
393 static enum rofferr
roff_cond_text(ROFF_ARGS
);
394 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
395 static enum rofferr
roff_ds(ROFF_ARGS
);
396 static enum rofferr
roff_eqndelim(struct roff
*, struct buf
*, int);
397 static int roff_evalcond(struct roff
*r
, int, char *, int *);
398 static int roff_evalnum(struct roff
*, int,
399 const char *, int *, int *, int);
400 static int roff_evalpar(struct roff
*, int,
401 const char *, int *, int *, int);
402 static int roff_evalstrcond(const char *, int *);
403 static void roff_free1(struct roff
*);
404 static void roff_freereg(struct roffreg
*);
405 static void roff_freestr(struct roffkv
*);
406 static size_t roff_getname(struct roff
*, char **, int, int);
407 static int roff_getnum(const char *, int *, int *, int);
408 static int roff_getop(const char *, int *, char *);
409 static int roff_getregn(const struct roff
*,
410 const char *, size_t);
411 static int roff_getregro(const struct roff
*,
413 static const char *roff_getstrn(const struct roff
*,
414 const char *, size_t);
415 static int roff_hasregn(const struct roff
*,
416 const char *, size_t);
417 static enum rofferr
roff_insec(ROFF_ARGS
);
418 static enum rofferr
roff_it(ROFF_ARGS
);
419 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
420 static void roff_man_alloc1(struct roff_man
*);
421 static void roff_man_free1(struct roff_man
*);
422 static enum rofferr
roff_nr(ROFF_ARGS
);
423 static enum rofft
roff_parse(struct roff
*, char *, int *,
425 static enum rofferr
roff_parsetext(struct buf
*, int, int *);
426 static enum rofferr
roff_res(struct roff
*, struct buf
*, int, int);
427 static enum rofferr
roff_rm(ROFF_ARGS
);
428 static enum rofferr
roff_rr(ROFF_ARGS
);
429 static void roff_setstr(struct roff
*,
430 const char *, const char *, int);
431 static void roff_setstrn(struct roffkv
**, const char *,
432 size_t, const char *, size_t, int);
433 static enum rofferr
roff_so(ROFF_ARGS
);
434 static enum rofferr
roff_tr(ROFF_ARGS
);
435 static enum rofferr
roff_Dd(ROFF_ARGS
);
436 static enum rofferr
roff_TH(ROFF_ARGS
);
437 static enum rofferr
roff_TE(ROFF_ARGS
);
438 static enum rofferr
roff_TS(ROFF_ARGS
);
439 static enum rofferr
roff_EQ(ROFF_ARGS
);
440 static enum rofferr
roff_EN(ROFF_ARGS
);
441 static enum rofferr
roff_T_(ROFF_ARGS
);
442 static enum rofferr
roff_unsupp(ROFF_ARGS
);
443 static enum rofferr
roff_userdef(ROFF_ARGS
);
445 /* --- constant data ------------------------------------------------------ */
447 /* See roffhash_find() */
451 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
453 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
454 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
456 static struct roffmac
*hash
[HASHWIDTH
];
458 static struct roffmac roffs
[ROFF_MAX
] = {
459 { "ab", roff_unsupp
, NULL
, NULL
, 0, NULL
},
460 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
461 { "af", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
462 { "aln", roff_unsupp
, NULL
, NULL
, 0, NULL
},
463 { "als", roff_unsupp
, NULL
, NULL
, 0, NULL
},
464 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
465 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
466 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
467 { "ami1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
468 { "as", roff_ds
, NULL
, NULL
, 0, NULL
},
469 { "as1", roff_ds
, NULL
, NULL
, 0, NULL
},
470 { "asciify", roff_unsupp
, NULL
, NULL
, 0, NULL
},
471 { "backtrace", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
472 { "bd", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
473 { "bleedat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
474 { "blm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
475 { "box", roff_unsupp
, NULL
, NULL
, 0, NULL
},
476 { "boxa", roff_unsupp
, NULL
, NULL
, 0, NULL
},
477 { "bp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
478 { "BP", roff_unsupp
, NULL
, NULL
, 0, NULL
},
479 { "break", roff_unsupp
, NULL
, NULL
, 0, NULL
},
480 { "breakchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
481 { "brnl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
482 { "brp", roff_brp
, NULL
, NULL
, 0, NULL
},
483 { "brpnl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
484 { "c2", roff_unsupp
, NULL
, NULL
, 0, NULL
},
485 { "cc", roff_cc
, NULL
, NULL
, 0, NULL
},
486 { "ce", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
487 { "cf", roff_insec
, NULL
, NULL
, 0, NULL
},
488 { "cflags", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
489 { "ch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
490 { "char", roff_unsupp
, NULL
, NULL
, 0, NULL
},
491 { "chop", roff_unsupp
, NULL
, NULL
, 0, NULL
},
492 { "class", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
493 { "close", roff_insec
, NULL
, NULL
, 0, NULL
},
494 { "CL", roff_unsupp
, NULL
, NULL
, 0, NULL
},
495 { "color", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
496 { "composite", roff_unsupp
, NULL
, NULL
, 0, NULL
},
497 { "continue", roff_unsupp
, NULL
, NULL
, 0, NULL
},
498 { "cp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
499 { "cropat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
500 { "cs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
501 { "cu", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
502 { "da", roff_unsupp
, NULL
, NULL
, 0, NULL
},
503 { "dch", roff_unsupp
, NULL
, NULL
, 0, NULL
},
504 { "Dd", roff_Dd
, NULL
, NULL
, 0, NULL
},
505 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
506 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
507 { "defcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
508 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
509 { "dei1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
510 { "device", roff_unsupp
, NULL
, NULL
, 0, NULL
},
511 { "devicem", roff_unsupp
, NULL
, NULL
, 0, NULL
},
512 { "di", roff_unsupp
, NULL
, NULL
, 0, NULL
},
513 { "do", roff_unsupp
, NULL
, NULL
, 0, NULL
},
514 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
515 { "ds1", roff_ds
, NULL
, NULL
, 0, NULL
},
516 { "dwh", roff_unsupp
, NULL
, NULL
, 0, NULL
},
517 { "dt", roff_unsupp
, NULL
, NULL
, 0, NULL
},
518 { "ec", roff_unsupp
, NULL
, NULL
, 0, NULL
},
519 { "ecr", roff_unsupp
, NULL
, NULL
, 0, NULL
},
520 { "ecs", roff_unsupp
, NULL
, NULL
, 0, NULL
},
521 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
522 { "em", roff_unsupp
, NULL
, NULL
, 0, NULL
},
523 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
524 { "eo", roff_unsupp
, NULL
, NULL
, 0, NULL
},
525 { "EP", roff_unsupp
, NULL
, NULL
, 0, NULL
},
526 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
527 { "errprint", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
528 { "ev", roff_unsupp
, NULL
, NULL
, 0, NULL
},
529 { "evc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
530 { "ex", roff_unsupp
, NULL
, NULL
, 0, NULL
},
531 { "fallback", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
532 { "fam", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
533 { "fc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
534 { "fchar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
535 { "fcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
536 { "fdeferlig", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
537 { "feature", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
538 { "fkern", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
539 { "fl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
540 { "flig", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
541 { "fp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
542 { "fps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
543 { "fschar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
544 { "fspacewidth", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
545 { "fspecial", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
546 { "ftr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
547 { "fzoom", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
548 { "gcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
549 { "hc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
550 { "hcode", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
551 { "hidechar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
552 { "hla", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
553 { "hlm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
554 { "hpf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
555 { "hpfa", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
556 { "hpfcode", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
557 { "hw", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
558 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
559 { "hylang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
560 { "hylen", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
561 { "hym", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
562 { "hypp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
563 { "hys", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
564 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
565 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
566 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
567 { "index", roff_unsupp
, NULL
, NULL
, 0, NULL
},
568 { "it", roff_it
, NULL
, NULL
, 0, NULL
},
569 { "itc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
570 { "IX", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
571 { "kern", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
572 { "kernafter", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
573 { "kernbefore", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
574 { "kernpair", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
575 { "lc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
576 { "lc_ctype", roff_unsupp
, NULL
, NULL
, 0, NULL
},
577 { "lds", roff_unsupp
, NULL
, NULL
, 0, NULL
},
578 { "length", roff_unsupp
, NULL
, NULL
, 0, NULL
},
579 { "letadj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
580 { "lf", roff_insec
, NULL
, NULL
, 0, NULL
},
581 { "lg", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
582 { "lhang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
583 { "linetabs", roff_unsupp
, NULL
, NULL
, 0, NULL
},
584 { "lnr", roff_unsupp
, NULL
, NULL
, 0, NULL
},
585 { "lnrf", roff_unsupp
, NULL
, NULL
, 0, NULL
},
586 { "lpfx", roff_unsupp
, NULL
, NULL
, 0, NULL
},
587 { "ls", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
588 { "lsm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
589 { "lt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
590 { "mc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
591 { "mediasize", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
592 { "minss", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
593 { "mk", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
594 { "mso", roff_insec
, NULL
, NULL
, 0, NULL
},
595 { "na", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
596 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
597 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
598 { "nhychar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
599 { "nm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
600 { "nn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
601 { "nop", roff_unsupp
, NULL
, NULL
, 0, NULL
},
602 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
603 { "nrf", roff_unsupp
, NULL
, NULL
, 0, NULL
},
604 { "nroff", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
605 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
606 { "nx", roff_insec
, NULL
, NULL
, 0, NULL
},
607 { "open", roff_insec
, NULL
, NULL
, 0, NULL
},
608 { "opena", roff_insec
, NULL
, NULL
, 0, NULL
},
609 { "os", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
610 { "output", roff_unsupp
, NULL
, NULL
, 0, NULL
},
611 { "padj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
612 { "papersize", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
613 { "pc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
614 { "pev", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
615 { "pi", roff_insec
, NULL
, NULL
, 0, NULL
},
616 { "PI", roff_unsupp
, NULL
, NULL
, 0, NULL
},
617 { "pl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
618 { "pm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
619 { "pn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
620 { "pnr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
621 { "po", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
622 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
623 { "psbb", roff_unsupp
, NULL
, NULL
, 0, NULL
},
624 { "pshape", roff_unsupp
, NULL
, NULL
, 0, NULL
},
625 { "pso", roff_insec
, NULL
, NULL
, 0, NULL
},
626 { "ptr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
627 { "pvs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
628 { "rchar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
629 { "rd", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
630 { "recursionlimit", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
631 { "return", roff_unsupp
, NULL
, NULL
, 0, NULL
},
632 { "rfschar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
633 { "rhang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
634 { "rj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
635 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
636 { "rn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
637 { "rnn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
638 { "rr", roff_rr
, NULL
, NULL
, 0, NULL
},
639 { "rs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
640 { "rt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
641 { "schar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
642 { "sentchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
643 { "shc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
644 { "shift", roff_unsupp
, NULL
, NULL
, 0, NULL
},
645 { "sizes", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
646 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
647 { "spacewidth", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
648 { "special", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
649 { "spreadwarn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
650 { "ss", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
651 { "sty", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
652 { "substring", roff_unsupp
, NULL
, NULL
, 0, NULL
},
653 { "sv", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
654 { "sy", roff_insec
, NULL
, NULL
, 0, NULL
},
655 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
656 { "ta", roff_unsupp
, NULL
, NULL
, 0, NULL
},
657 { "tc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
658 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
659 { "TH", roff_TH
, NULL
, NULL
, 0, NULL
},
660 { "ti", roff_unsupp
, NULL
, NULL
, 0, NULL
},
661 { "tkf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
662 { "tl", roff_unsupp
, NULL
, NULL
, 0, NULL
},
663 { "tm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
664 { "tm1", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
665 { "tmc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
666 { "tr", roff_tr
, NULL
, NULL
, 0, NULL
},
667 { "track", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
668 { "transchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
669 { "trf", roff_insec
, NULL
, NULL
, 0, NULL
},
670 { "trimat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
671 { "trin", roff_unsupp
, NULL
, NULL
, 0, NULL
},
672 { "trnt", roff_unsupp
, NULL
, NULL
, 0, NULL
},
673 { "troff", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
674 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
675 { "uf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
676 { "ul", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
677 { "unformat", roff_unsupp
, NULL
, NULL
, 0, NULL
},
678 { "unwatch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
679 { "unwatchn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
680 { "vpt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
681 { "vs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
682 { "warn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
683 { "warnscale", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
684 { "watch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
685 { "watchlength", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
686 { "watchn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
687 { "wh", roff_unsupp
, NULL
, NULL
, 0, NULL
},
688 { "while", roff_unsupp
, NULL
, NULL
, 0, NULL
},
689 { "write", roff_insec
, NULL
, NULL
, 0, NULL
},
690 { "writec", roff_insec
, NULL
, NULL
, 0, NULL
},
691 { "writem", roff_insec
, NULL
, NULL
, 0, NULL
},
692 { "xflag", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
693 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
694 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
697 /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
698 const char *const __mdoc_reserved
[] = {
699 "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
700 "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
701 "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
702 "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
703 "Dt", "Dv", "Dx", "D1",
704 "Ec", "Ed", "Ef", "Ek", "El", "Em",
705 "En", "Eo", "Er", "Es", "Ev", "Ex",
706 "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
707 "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
708 "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
709 "Oc", "Oo", "Op", "Os", "Ot", "Ox",
710 "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
711 "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
712 "Sc", "Sh", "Sm", "So", "Sq",
713 "Ss", "St", "Sx", "Sy",
714 "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
715 "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
716 "%P", "%Q", "%R", "%T", "%U", "%V",
720 /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
721 const char *const __man_reserved
[] = {
722 "AT", "B", "BI", "BR", "DT",
723 "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
724 "LP", "OP", "P", "PD", "PP",
725 "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
726 "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
730 /* Array of injected predefined strings. */
731 #define PREDEFS_MAX 38
732 static const struct predef predefs
[PREDEFS_MAX
] = {
733 #include "predefs.in"
736 /* See roffhash_find() */
737 #define ROFF_HASH(p) (p[0] - ASCII_LO)
739 static int roffit_lines
; /* number of lines to delay */
740 static char *roffit_macro
; /* nil-terminated macro line */
743 /* --- request table ------------------------------------------------------ */
751 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
752 assert(roffs
[i
].name
[0] >= ASCII_LO
);
753 assert(roffs
[i
].name
[0] <= ASCII_HI
);
755 buc
= ROFF_HASH(roffs
[i
].name
);
757 if (NULL
!= (n
= hash
[buc
])) {
758 for ( ; n
->next
; n
= n
->next
)
762 hash
[buc
] = &roffs
[i
];
767 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
768 * the nil-terminated string name could be found.
771 roffhash_find(const char *p
, size_t s
)
777 * libroff has an extremely simple hashtable, for the time
778 * being, which simply keys on the first character, which must
779 * be printable, then walks a chain. It works well enough until
783 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
788 if (NULL
== (n
= hash
[buc
]))
790 for ( ; n
; n
= n
->next
)
791 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
792 return (enum rofft
)(n
- roffs
);
797 /* --- stack of request blocks -------------------------------------------- */
800 * Pop the current node off of the stack of roff instructions currently
804 roffnode_pop(struct roff
*r
)
811 r
->last
= r
->last
->parent
;
818 * Push a roff node onto the instruction stack. This must later be
819 * removed with roffnode_pop().
822 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
827 p
= mandoc_calloc(1, sizeof(struct roffnode
));
830 p
->name
= mandoc_strdup(name
);
834 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
839 /* --- roff parser state data management ---------------------------------- */
842 roff_free1(struct roff
*r
)
844 struct tbl_node
*tbl
;
848 while (NULL
!= (tbl
= r
->first_tbl
)) {
849 r
->first_tbl
= tbl
->next
;
852 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
854 while (NULL
!= (e
= r
->first_eqn
)) {
855 r
->first_eqn
= e
->next
;
858 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
868 roff_freereg(r
->regtab
);
871 roff_freestr(r
->strtab
);
872 roff_freestr(r
->xmbtab
);
873 r
->strtab
= r
->xmbtab
= NULL
;
876 for (i
= 0; i
< 128; i
++)
883 roff_reset(struct roff
*r
)
887 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
892 roff_free(struct roff
*r
)
900 roff_alloc(struct mparse
*parse
, int options
)
904 r
= mandoc_calloc(1, sizeof(struct roff
));
906 r
->options
= options
;
907 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
915 /* --- syntax tree state data management ---------------------------------- */
918 roff_man_free1(struct roff_man
*man
)
921 if (man
->first
!= NULL
)
922 roff_node_delete(man
, man
->first
);
923 free(man
->meta
.msec
);
926 free(man
->meta
.arch
);
927 free(man
->meta
.title
);
928 free(man
->meta
.name
);
929 free(man
->meta
.date
);
933 roff_man_alloc1(struct roff_man
*man
)
936 memset(&man
->meta
, 0, sizeof(man
->meta
));
937 man
->first
= mandoc_calloc(1, sizeof(*man
->first
));
938 man
->first
->type
= ROFFT_ROOT
;
939 man
->last
= man
->first
;
942 man
->macroset
= MACROSET_NONE
;
943 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
944 man
->next
= ROFF_NEXT_CHILD
;
948 roff_man_reset(struct roff_man
*man
)
952 roff_man_alloc1(man
);
956 roff_man_free(struct roff_man
*man
)
964 roff_man_alloc(struct roff
*roff
, struct mparse
*parse
,
965 const char *defos
, int quick
)
967 struct roff_man
*man
;
969 man
= mandoc_calloc(1, sizeof(*man
));
974 roff_man_alloc1(man
);
978 /* --- syntax tree handling ----------------------------------------------- */
981 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
982 enum roff_type type
, int tok
)
986 n
= mandoc_calloc(1, sizeof(*n
));
991 n
->sec
= man
->lastsec
;
993 if (man
->flags
& MDOC_SYNOPSIS
)
994 n
->flags
|= MDOC_SYNPRETTY
;
996 n
->flags
&= ~MDOC_SYNPRETTY
;
997 if (man
->flags
& MDOC_NEWLINE
)
998 n
->flags
|= MDOC_LINE
;
999 man
->flags
&= ~MDOC_NEWLINE
;
1005 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
1008 switch (man
->next
) {
1009 case ROFF_NEXT_SIBLING
:
1010 if (man
->last
->next
!= NULL
) {
1011 n
->next
= man
->last
->next
;
1012 man
->last
->next
->prev
= n
;
1014 man
->last
->parent
->last
= n
;
1015 man
->last
->next
= n
;
1016 n
->prev
= man
->last
;
1017 n
->parent
= man
->last
->parent
;
1019 case ROFF_NEXT_CHILD
:
1020 man
->last
->child
= n
;
1021 n
->parent
= man
->last
;
1022 n
->parent
->last
= n
;
1027 n
->parent
->nchild
++;
1032 n
->parent
->head
= n
;
1035 if (n
->end
!= ENDBODY_NOT
)
1037 n
->parent
->body
= n
;
1040 n
->parent
->tail
= n
;
1047 * Copy over the normalised-data pointer of our parent. Not
1048 * everybody has one, but copying a null pointer is fine.
1051 n
->norm
= n
->parent
->norm
;
1052 assert(n
->parent
->type
== ROFFT_BLOCK
);
1056 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
1058 struct roff_node
*n
;
1060 n
= roff_node_alloc(man
, line
, pos
, ROFFT_TEXT
, TOKEN_NONE
);
1061 n
->string
= roff_strdup(man
->roff
, word
);
1062 roff_node_append(man
, n
);
1063 if (man
->macroset
== MACROSET_MDOC
)
1064 n
->flags
|= MDOC_VALID
| MDOC_ENDED
;
1066 man_valid_post(man
);
1067 man
->next
= ROFF_NEXT_SIBLING
;
1071 roff_word_append(struct roff_man
*man
, const char *word
)
1073 struct roff_node
*n
;
1074 char *addstr
, *newstr
;
1077 addstr
= roff_strdup(man
->roff
, word
);
1078 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
1082 man
->next
= ROFF_NEXT_SIBLING
;
1086 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1088 struct roff_node
*n
;
1090 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
1091 roff_node_append(man
, n
);
1092 man
->next
= ROFF_NEXT_CHILD
;
1096 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1098 struct roff_node
*n
;
1100 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
1101 roff_node_append(man
, n
);
1102 man
->next
= ROFF_NEXT_CHILD
;
1107 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1109 struct roff_node
*n
;
1111 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
1112 roff_node_append(man
, n
);
1113 man
->next
= ROFF_NEXT_CHILD
;
1118 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1120 struct roff_node
*n
;
1122 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
1123 roff_node_append(man
, n
);
1124 man
->next
= ROFF_NEXT_CHILD
;
1129 roff_addeqn(struct roff_man
*man
, const struct eqn
*eqn
)
1131 struct roff_node
*n
;
1133 n
= roff_node_alloc(man
, eqn
->ln
, eqn
->pos
, ROFFT_EQN
, TOKEN_NONE
);
1135 if (eqn
->ln
> man
->last
->line
)
1136 n
->flags
|= MDOC_LINE
;
1137 roff_node_append(man
, n
);
1138 man
->next
= ROFF_NEXT_SIBLING
;
1142 roff_addtbl(struct roff_man
*man
, const struct tbl_span
*tbl
)
1144 struct roff_node
*n
;
1146 if (man
->macroset
== MACROSET_MAN
)
1147 man_breakscope(man
, TOKEN_NONE
);
1148 n
= roff_node_alloc(man
, tbl
->line
, 0, ROFFT_TBL
, TOKEN_NONE
);
1150 roff_node_append(man
, n
);
1151 if (man
->macroset
== MACROSET_MDOC
)
1152 n
->flags
|= MDOC_VALID
| MDOC_ENDED
;
1154 man_valid_post(man
);
1155 man
->next
= ROFF_NEXT_SIBLING
;
1159 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1162 /* Adjust siblings. */
1165 n
->prev
->next
= n
->next
;
1167 n
->next
->prev
= n
->prev
;
1169 /* Adjust parent. */
1171 if (n
->parent
!= NULL
) {
1172 n
->parent
->nchild
--;
1173 if (n
->parent
->child
== n
)
1174 n
->parent
->child
= n
->next
;
1175 if (n
->parent
->last
== n
)
1176 n
->parent
->last
= n
->prev
;
1179 /* Adjust parse point. */
1183 if (man
->last
== n
) {
1184 if (n
->prev
== NULL
) {
1185 man
->last
= n
->parent
;
1186 man
->next
= ROFF_NEXT_CHILD
;
1188 man
->last
= n
->prev
;
1189 man
->next
= ROFF_NEXT_SIBLING
;
1192 if (man
->first
== n
)
1197 roff_node_free(struct roff_node
*n
)
1200 if (n
->args
!= NULL
)
1201 mdoc_argv_free(n
->args
);
1202 if (n
->type
== ROFFT_BLOCK
|| n
->type
== ROFFT_ELEM
)
1209 roff_node_delete(struct roff_man
*man
, struct roff_node
*n
)
1212 while (n
->child
!= NULL
)
1213 roff_node_delete(man
, n
->child
);
1214 assert(n
->nchild
== 0);
1215 roff_node_unlink(man
, n
);
1220 deroff(char **dest
, const struct roff_node
*n
)
1225 if (n
->type
!= ROFFT_TEXT
) {
1226 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1231 /* Skip leading whitespace and escape sequences. */
1234 while (*cp
!= '\0') {
1237 mandoc_escape((const char **)&cp
, NULL
, NULL
);
1238 } else if (isspace((unsigned char)*cp
))
1244 /* Skip trailing whitespace. */
1246 for (sz
= strlen(cp
); sz
; sz
--)
1247 if ( ! isspace((unsigned char)cp
[sz
-1]))
1250 /* Skip empty strings. */
1255 if (*dest
== NULL
) {
1256 *dest
= mandoc_strndup(cp
, sz
);
1260 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);
1265 /* --- main functions of the roff parser ---------------------------------- */
1268 * In the current line, expand escape sequences that tend to get
1269 * used in numerical expressions and conditional requests.
1270 * Also check the syntax of the remaining escape sequences.
1273 roff_res(struct roff
*r
, struct buf
*buf
, int ln
, int pos
)
1275 char ubuf
[24]; /* buffer to print the number */
1276 const char *start
; /* start of the string to process */
1277 char *stesc
; /* start of an escape sequence ('\\') */
1278 const char *stnam
; /* start of the name, after "[(*" */
1279 const char *cp
; /* end of the name, e.g. before ']' */
1280 const char *res
; /* the string to be substituted */
1281 char *nbuf
; /* new buffer to copy buf->buf to */
1282 size_t maxl
; /* expected length of the escape name */
1283 size_t naml
; /* actual length of the escape name */
1284 enum mandoc_esc esc
; /* type of the escape sequence */
1285 int inaml
; /* length returned from mandoc_escape() */
1286 int expand_count
; /* to avoid infinite loops */
1287 int npos
; /* position in numeric expression */
1288 int arg_complete
; /* argument not interrupted by eol */
1289 char term
; /* character terminating the escape */
1292 start
= buf
->buf
+ pos
;
1293 stesc
= strchr(start
, '\0') - 1;
1294 while (stesc
-- > start
) {
1296 /* Search backwards for the next backslash. */
1301 /* If it is escaped, skip it. */
1303 for (cp
= stesc
- 1; cp
>= start
; cp
--)
1307 if ((stesc
- cp
) % 2 == 0) {
1312 /* Decide whether to expand or to check only. */
1328 esc
= mandoc_escape(&cp
, &stnam
, &inaml
);
1329 if (esc
== ESCAPE_ERROR
||
1330 (esc
== ESCAPE_SPECIAL
&&
1331 mchars_spec2cp(stnam
, inaml
) < 0))
1332 mandoc_vmsg(MANDOCERR_ESC_BAD
,
1333 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1334 "%.*s", (int)(cp
- stesc
), stesc
);
1338 if (EXPAND_LIMIT
< ++expand_count
) {
1339 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1340 ln
, (int)(stesc
- buf
->buf
), NULL
);
1345 * The third character decides the length
1346 * of the name of the string or register.
1347 * Save a pointer to the name.
1374 /* Advance to the end of the name. */
1378 while (maxl
== 0 || naml
< maxl
) {
1380 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
1381 ln
, (int)(stesc
- buf
->buf
), stesc
);
1385 if (maxl
== 0 && *cp
== term
) {
1389 if (*cp
++ != '\\' || stesc
[1] != 'w') {
1393 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1394 case ESCAPE_SPECIAL
:
1395 case ESCAPE_UNICODE
:
1396 case ESCAPE_NUMBERED
:
1397 case ESCAPE_OVERSTRIKE
:
1406 * Retrieve the replacement string; if it is
1407 * undefined, resume searching for escapes.
1413 res
= roff_getstrn(r
, stnam
, naml
);
1417 ubuf
[0] = arg_complete
&&
1418 roff_evalnum(r
, ln
, stnam
, &npos
,
1419 NULL
, ROFFNUM_SCALE
) &&
1420 stnam
+ npos
+ 1 == cp
? '1' : '0';
1425 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1426 roff_getregn(r
, stnam
, naml
));
1431 /* use even incomplete args */
1432 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1438 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1439 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1440 "%.*s", (int)naml
, stnam
);
1442 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1443 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1444 ln
, (int)(stesc
- buf
->buf
), NULL
);
1448 /* Replace the escape sequence by the string. */
1451 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1452 buf
->buf
, res
, cp
) + 1;
1454 /* Prepare for the next replacement. */
1457 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1465 * Process text streams.
1468 roff_parsetext(struct buf
*buf
, int pos
, int *offs
)
1474 enum mandoc_esc esc
;
1476 /* Spring the input line trap. */
1478 if (roffit_lines
== 1) {
1479 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1486 return ROFF_REPARSE
;
1487 } else if (roffit_lines
> 1)
1490 /* Convert all breakable hyphens into ASCII_HYPH. */
1492 start
= p
= buf
->buf
+ pos
;
1494 while (*p
!= '\0') {
1495 sz
= strcspn(p
, "-\\");
1502 /* Skip over escapes. */
1504 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1505 if (esc
== ESCAPE_ERROR
)
1510 } else if (p
== start
) {
1515 if (isalpha((unsigned char)p
[-1]) &&
1516 isalpha((unsigned char)p
[1]))
1524 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
1528 int pos
; /* parse point */
1529 int spos
; /* saved parse point for messages */
1530 int ppos
; /* original offset in buf->buf */
1531 int ctl
; /* macro line (boolean) */
1535 /* Handle in-line equation delimiters. */
1537 if (r
->tbl
== NULL
&&
1538 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1539 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1540 e
= roff_eqndelim(r
, buf
, pos
);
1541 if (e
== ROFF_REPARSE
)
1543 assert(e
== ROFF_CONT
);
1546 /* Expand some escape sequences. */
1548 e
= roff_res(r
, buf
, ln
, pos
);
1551 assert(e
== ROFF_CONT
);
1553 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1556 * First, if a scope is open and we're not a macro, pass the
1557 * text through the macro's filter.
1558 * Equations process all content themselves.
1559 * Tables process almost all content themselves, but we want
1560 * to warn about macros before passing it there.
1563 if (r
->last
!= NULL
&& ! ctl
) {
1565 assert(roffs
[t
].text
);
1566 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1567 assert(e
== ROFF_IGN
|| e
== ROFF_CONT
);
1572 return eqn_read(&r
->eqn
, ln
, buf
->buf
, ppos
, offs
);
1573 if (r
->tbl
!= NULL
&& ( ! ctl
|| buf
->buf
[pos
] == '\0'))
1574 return tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
);
1576 return roff_parsetext(buf
, pos
, offs
);
1578 /* Skip empty request lines. */
1580 if (buf
->buf
[pos
] == '"') {
1581 mandoc_msg(MANDOCERR_COMMENT_BAD
, r
->parse
,
1584 } else if (buf
->buf
[pos
] == '\0')
1588 * If a scope is open, go to the child handler for that macro,
1589 * as it may want to preprocess before doing anything with it.
1590 * Don't do so if an equation is open.
1595 assert(roffs
[t
].sub
);
1596 return (*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1599 /* No scope is open. This is a new request or macro. */
1602 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1604 /* Tables ignore most macros. */
1606 if (r
->tbl
!= NULL
&& (t
== ROFF_MAX
|| t
== ROFF_TS
)) {
1607 mandoc_msg(MANDOCERR_TBLMACRO
, r
->parse
,
1608 ln
, pos
, buf
->buf
+ spos
);
1611 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1613 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] == ' ')
1615 return tbl_read(r
->tbl
, ln
, buf
->buf
, pos
);
1619 * This is neither a roff request nor a user-defined macro.
1620 * Let the standard macro set parsers handle it.
1626 /* Execute a roff request or a user defined macro. */
1628 assert(roffs
[t
].proc
);
1629 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1633 roff_endparse(struct roff
*r
)
1637 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1638 r
->last
->line
, r
->last
->col
,
1639 roffs
[r
->last
->tok
].name
);
1642 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1643 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, "EQ");
1648 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1649 r
->tbl
->line
, r
->tbl
->pos
, "TS");
1655 * Parse a roff node's type from the input buffer. This must be in the
1656 * form of ".foo xxx" in the usual way.
1659 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1668 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1672 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1674 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
1675 ? ROFF_USERDEF
: roffhash_find(mac
, maclen
);
1683 /* --- handling of request blocks ----------------------------------------- */
1686 roff_cblock(ROFF_ARGS
)
1690 * A block-close `..' should only be invoked as a child of an
1691 * ignore macro, otherwise raise a warning and just ignore it.
1694 if (r
->last
== NULL
) {
1695 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1700 switch (r
->last
->tok
) {
1702 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1705 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1710 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1715 if (buf
->buf
[pos
] != '\0')
1716 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
1717 ".. %s", buf
->buf
+ pos
);
1720 roffnode_cleanscope(r
);
1726 roffnode_cleanscope(struct roff
*r
)
1730 if (--r
->last
->endspan
!= 0)
1737 roff_ccond(struct roff
*r
, int ln
, int ppos
)
1740 if (NULL
== r
->last
) {
1741 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1746 switch (r
->last
->tok
) {
1752 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1757 if (r
->last
->endspan
> -1) {
1758 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1764 roffnode_cleanscope(r
);
1769 roff_block(ROFF_ARGS
)
1775 /* Ignore groff compatibility mode for now. */
1777 if (tok
== ROFF_de1
)
1779 else if (tok
== ROFF_dei1
)
1781 else if (tok
== ROFF_am1
)
1783 else if (tok
== ROFF_ami1
)
1786 /* Parse the macro name argument. */
1788 cp
= buf
->buf
+ pos
;
1789 if (tok
== ROFF_ig
) {
1794 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1795 iname
[namesz
] = '\0';
1798 /* Resolve the macro name argument if it is indirect. */
1800 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1801 if ((name
= roff_getstrn(r
, iname
, namesz
)) == NULL
) {
1802 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1803 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1804 "%.*s", (int)namesz
, iname
);
1807 namesz
= strlen(name
);
1811 if (namesz
== 0 && tok
!= ROFF_ig
) {
1812 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
,
1813 ln
, ppos
, roffs
[tok
].name
);
1817 roffnode_push(r
, tok
, name
, ln
, ppos
);
1820 * At the beginning of a `de' macro, clear the existing string
1821 * with the same name, if there is one. New content will be
1822 * appended from roff_block_text() in multiline mode.
1825 if (tok
== ROFF_de
|| tok
== ROFF_dei
)
1826 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
1831 /* Get the custom end marker. */
1834 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1836 /* Resolve the end marker if it is indirect. */
1838 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1839 if ((name
= roff_getstrn(r
, iname
, namesz
)) == NULL
) {
1840 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1841 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1842 "%.*s", (int)namesz
, iname
);
1845 namesz
= strlen(name
);
1850 r
->last
->end
= mandoc_strndup(name
, namesz
);
1853 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
1854 ln
, pos
, ".%s ... %s", roffs
[tok
].name
, cp
);
1860 roff_block_sub(ROFF_ARGS
)
1866 * First check whether a custom macro exists at this level. If
1867 * it does, then check against it. This is some of groff's
1868 * stranger behaviours. If we encountered a custom end-scope
1869 * tag and that tag also happens to be a "real" macro, then we
1870 * need to try interpreting it again as a real macro. If it's
1871 * not, then return ignore. Else continue.
1875 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
1876 if (buf
->buf
[i
] != r
->last
->end
[j
])
1879 if (r
->last
->end
[j
] == '\0' &&
1880 (buf
->buf
[i
] == '\0' ||
1881 buf
->buf
[i
] == ' ' ||
1882 buf
->buf
[i
] == '\t')) {
1884 roffnode_cleanscope(r
);
1886 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
1890 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
1898 * If we have no custom end-query or lookup failed, then try
1899 * pulling it out of the hashtable.
1902 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1904 if (t
!= ROFF_cblock
) {
1906 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
1910 assert(roffs
[t
].proc
);
1911 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1915 roff_block_text(ROFF_ARGS
)
1919 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
1925 roff_cond_sub(ROFF_ARGS
)
1932 roffnode_cleanscope(r
);
1933 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1936 * Fully handle known macros when they are structurally
1937 * required or when the conditional evaluated to true.
1940 if ((t
!= ROFF_MAX
) &&
1941 (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
)) {
1942 assert(roffs
[t
].proc
);
1943 return (*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
);
1947 * If `\}' occurs on a macro line without a preceding macro,
1948 * drop the line completely.
1951 ep
= buf
->buf
+ pos
;
1952 if (ep
[0] == '\\' && ep
[1] == '}')
1955 /* Always check for the closing delimiter `\}'. */
1957 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1958 if (*(++ep
) == '}') {
1960 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1965 return rr
? ROFF_CONT
: ROFF_IGN
;
1969 roff_cond_text(ROFF_ARGS
)
1975 roffnode_cleanscope(r
);
1977 ep
= buf
->buf
+ pos
;
1978 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1979 if (*(++ep
) == '}') {
1981 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1986 return rr
? ROFF_CONT
: ROFF_IGN
;
1989 /* --- handling of numeric and conditional expressions -------------------- */
1992 * Parse a single signed integer number. Stop at the first non-digit.
1993 * If there is at least one digit, return success and advance the
1994 * parse point, else return failure and let the parse point unchanged.
1995 * Ignore overflows, treat them just like the C language.
1998 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
2000 int myres
, scaled
, n
, p
;
2007 if (n
|| v
[p
] == '+')
2010 if (flags
& ROFFNUM_WHITE
)
2011 while (isspace((unsigned char)v
[p
]))
2014 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
2015 *res
= 10 * *res
+ v
[p
] - '0';
2022 /* Each number may be followed by one optional scaling unit. */
2026 scaled
= *res
* 65536;
2029 scaled
= *res
* 240;
2032 scaled
= *res
* 240 / 2.54;
2043 scaled
= *res
* 10 / 3;
2049 scaled
= *res
* 6 / 25;
2056 if (flags
& ROFFNUM_SCALE
)
2064 * Evaluate a string comparison condition.
2065 * The first character is the delimiter.
2066 * Succeed if the string up to its second occurrence
2067 * matches the string up to its third occurence.
2068 * Advance the cursor after the third occurrence
2069 * or lacking that, to the end of the line.
2072 roff_evalstrcond(const char *v
, int *pos
)
2074 const char *s1
, *s2
, *s3
;
2078 s1
= v
+ *pos
; /* initial delimiter */
2079 s2
= s1
+ 1; /* for scanning the first string */
2080 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2082 if (NULL
== s3
) /* found no middle delimiter */
2085 while ('\0' != *++s3
) {
2086 if (*s2
!= *s3
) { /* mismatch */
2087 s3
= strchr(s3
, *s1
);
2090 if (*s3
== *s1
) { /* found the final delimiter */
2099 s3
= strchr(s2
, '\0');
2100 else if (*s3
!= '\0')
2107 * Evaluate an optionally negated single character, numerical,
2108 * or string condition.
2111 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
2115 int number
, savepos
, wanttrue
;
2117 if ('!' == v
[*pos
]) {
2138 cp
= name
= v
+ ++*pos
;
2139 sz
= roff_getname(r
, &cp
, ln
, *pos
);
2141 return (sz
&& roff_hasregn(r
, name
, sz
)) == wanttrue
;
2147 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2148 return (number
> 0) == wanttrue
;
2149 else if (*pos
== savepos
)
2150 return roff_evalstrcond(v
, pos
) == wanttrue
;
2156 roff_line_ignore(ROFF_ARGS
)
2163 roff_insec(ROFF_ARGS
)
2166 mandoc_msg(MANDOCERR_REQ_INSEC
, r
->parse
,
2167 ln
, ppos
, roffs
[tok
].name
);
2172 roff_unsupp(ROFF_ARGS
)
2175 mandoc_msg(MANDOCERR_REQ_UNSUPP
, r
->parse
,
2176 ln
, ppos
, roffs
[tok
].name
);
2181 roff_cond(ROFF_ARGS
)
2184 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2187 * An `.el' has no conditional body: it will consume the value
2188 * of the current rstack entry set in prior `ie' calls or
2191 * If we're not an `el', however, then evaluate the conditional.
2194 r
->last
->rule
= tok
== ROFF_el
?
2195 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2196 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2199 * An if-else will put the NEGATION of the current evaluated
2200 * conditional into the stack of rules.
2203 if (tok
== ROFF_ie
) {
2204 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2206 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2207 r
->rstacksz
, sizeof(int));
2209 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2212 /* If the parent has false as its rule, then so do we. */
2214 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2219 * If there is nothing on the line after the conditional,
2220 * not even whitespace, use next-line scope.
2223 if (buf
->buf
[pos
] == '\0') {
2224 r
->last
->endspan
= 2;
2228 while (buf
->buf
[pos
] == ' ')
2231 /* An opening brace requests multiline scope. */
2233 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2234 r
->last
->endspan
= -1;
2236 while (buf
->buf
[pos
] == ' ')
2242 * Anything else following the conditional causes
2243 * single-line scope. Warn if the scope contains
2244 * nothing but trailing whitespace.
2247 if (buf
->buf
[pos
] == '\0')
2248 mandoc_msg(MANDOCERR_COND_EMPTY
, r
->parse
,
2249 ln
, ppos
, roffs
[tok
].name
);
2251 r
->last
->endspan
= 1;
2265 /* Ignore groff compatibility mode for now. */
2267 if (tok
== ROFF_ds1
)
2269 else if (tok
== ROFF_as1
)
2273 * The first word is the name of the string.
2274 * If it is empty or terminated by an escape sequence,
2275 * abort the `ds' request without defining anything.
2278 name
= string
= buf
->buf
+ pos
;
2282 namesz
= roff_getname(r
, &string
, ln
, pos
);
2283 if (name
[namesz
] == '\\')
2286 /* Read past the initial double-quote, if any. */
2290 /* The rest is the value. */
2291 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2297 * Parse a single operator, one or two characters long.
2298 * If the operator is recognized, return success and advance the
2299 * parse point, else return failure and let the parse point unchanged.
2302 roff_getop(const char *v
, int *pos
, char *res
)
2317 switch (v
[*pos
+ 1]) {
2335 switch (v
[*pos
+ 1]) {
2349 if ('=' == v
[*pos
+ 1])
2361 * Evaluate either a parenthesized numeric expression
2362 * or a single signed integer number.
2365 roff_evalpar(struct roff
*r
, int ln
,
2366 const char *v
, int *pos
, int *res
, int flags
)
2370 return roff_getnum(v
, pos
, res
, flags
);
2373 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2377 * Omission of the closing parenthesis
2378 * is an error in validation mode,
2379 * but ignored in evaluation mode.
2384 else if (NULL
== res
)
2391 * Evaluate a complete numeric expression.
2392 * Proceed left to right, there is no concept of precedence.
2395 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2396 int *pos
, int *res
, int flags
)
2398 int mypos
, operand2
;
2406 if (flags
& ROFFNUM_WHITE
)
2407 while (isspace((unsigned char)v
[*pos
]))
2410 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2414 if (flags
& ROFFNUM_WHITE
)
2415 while (isspace((unsigned char)v
[*pos
]))
2418 if ( ! roff_getop(v
, pos
, &operator))
2421 if (flags
& ROFFNUM_WHITE
)
2422 while (isspace((unsigned char)v
[*pos
]))
2425 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2428 if (flags
& ROFFNUM_WHITE
)
2429 while (isspace((unsigned char)v
[*pos
]))
2446 if (operand2
== 0) {
2447 mandoc_msg(MANDOCERR_DIVZERO
,
2448 r
->parse
, ln
, *pos
, v
);
2455 if (operand2
== 0) {
2456 mandoc_msg(MANDOCERR_DIVZERO
,
2457 r
->parse
, ln
, *pos
, v
);
2464 *res
= *res
< operand2
;
2467 *res
= *res
> operand2
;
2470 *res
= *res
<= operand2
;
2473 *res
= *res
>= operand2
;
2476 *res
= *res
== operand2
;
2479 *res
= *res
!= operand2
;
2482 *res
= *res
&& operand2
;
2485 *res
= *res
|| operand2
;
2488 if (operand2
< *res
)
2492 if (operand2
> *res
)
2502 /* --- register management ------------------------------------------------ */
2505 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2507 struct roffreg
*reg
;
2509 /* Search for an existing register with the same name. */
2512 while (reg
&& strcmp(name
, reg
->key
.p
))
2516 /* Create a new register. */
2517 reg
= mandoc_malloc(sizeof(struct roffreg
));
2518 reg
->key
.p
= mandoc_strdup(name
);
2519 reg
->key
.sz
= strlen(name
);
2521 reg
->next
= r
->regtab
;
2527 else if ('-' == sign
)
2534 * Handle some predefined read-only number registers.
2535 * For now, return -1 if the requested register is not predefined;
2536 * in case a predefined read-only register having the value -1
2537 * were to turn up, another special value would have to be chosen.
2540 roff_getregro(const struct roff
*r
, const char *name
)
2544 case '$': /* Number of arguments of the last macro evaluated. */
2546 case 'A': /* ASCII approximation mode is always off. */
2548 case 'g': /* Groff compatibility mode is always on. */
2550 case 'H': /* Fixed horizontal resolution. */
2552 case 'j': /* Always adjust left margin only. */
2554 case 'T': /* Some output device is always defined. */
2556 case 'V': /* Fixed vertical resolution. */
2564 roff_getreg(const struct roff
*r
, const char *name
)
2566 struct roffreg
*reg
;
2569 if ('.' == name
[0] && '\0' != name
[1] && '\0' == name
[2]) {
2570 val
= roff_getregro(r
, name
+ 1);
2575 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2576 if (0 == strcmp(name
, reg
->key
.p
))
2583 roff_getregn(const struct roff
*r
, const char *name
, size_t len
)
2585 struct roffreg
*reg
;
2588 if ('.' == name
[0] && 2 == len
) {
2589 val
= roff_getregro(r
, name
+ 1);
2594 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2595 if (len
== reg
->key
.sz
&&
2596 0 == strncmp(name
, reg
->key
.p
, len
))
2603 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
2605 struct roffreg
*reg
;
2608 if ('.' == name
[0] && 2 == len
) {
2609 val
= roff_getregro(r
, name
+ 1);
2614 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2615 if (len
== reg
->key
.sz
&&
2616 0 == strncmp(name
, reg
->key
.p
, len
))
2623 roff_freereg(struct roffreg
*reg
)
2625 struct roffreg
*old_reg
;
2627 while (NULL
!= reg
) {
2643 key
= val
= buf
->buf
+ pos
;
2647 keysz
= roff_getname(r
, &val
, ln
, pos
);
2648 if (key
[keysz
] == '\\')
2653 if (sign
== '+' || sign
== '-')
2656 if (roff_evalnum(r
, ln
, val
, NULL
, &iv
, ROFFNUM_SCALE
))
2657 roff_setreg(r
, key
, iv
, sign
);
2665 struct roffreg
*reg
, **prev
;
2669 name
= cp
= buf
->buf
+ pos
;
2672 namesz
= roff_getname(r
, &cp
, ln
, pos
);
2673 name
[namesz
] = '\0';
2678 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
2690 /* --- handler functions for roff requests -------------------------------- */
2699 cp
= buf
->buf
+ pos
;
2700 while (*cp
!= '\0') {
2702 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
2703 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
2704 if (name
[namesz
] == '\\')
2715 /* Parse the number of lines. */
2717 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
2718 mandoc_msg(MANDOCERR_IT_NONUM
, r
->parse
,
2719 ln
, ppos
, buf
->buf
+ 1);
2723 while (isspace((unsigned char)buf
->buf
[pos
]))
2727 * Arm the input line trap.
2728 * Special-casing "an-trap" is an ugly workaround to cope
2729 * with DocBook stupidly fiddling with man(7) internals.
2733 roffit_macro
= mandoc_strdup(iv
!= 1 ||
2734 strcmp(buf
->buf
+ pos
, "an-trap") ?
2735 buf
->buf
+ pos
: "br");
2742 const char *const *cp
;
2744 if ((r
->options
& (MPARSE_MDOC
| MPARSE_QUICK
)) == 0)
2745 for (cp
= __mdoc_reserved
; *cp
; cp
++)
2746 roff_setstr(r
, *cp
, NULL
, 0);
2749 r
->format
= MPARSE_MDOC
;
2757 const char *const *cp
;
2759 if ((r
->options
& MPARSE_QUICK
) == 0)
2760 for (cp
= __man_reserved
; *cp
; cp
++)
2761 roff_setstr(r
, *cp
, NULL
, 0);
2764 r
->format
= MPARSE_MAN
;
2774 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2776 else if ( ! tbl_end(&r
->tbl
)) {
2778 buf
->buf
= mandoc_strdup(".sp");
2780 return ROFF_REPARSE
;
2790 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2793 tbl_restart(ppos
, ln
, r
->tbl
);
2799 * Handle in-line equation delimiters.
2802 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
2805 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
2808 * Outside equations, look for an opening delimiter.
2809 * If we are inside an equation, we already know it is
2810 * in-line, or this function wouldn't have been called;
2811 * so look for a closing delimiter.
2814 cp1
= buf
->buf
+ pos
;
2815 cp2
= strchr(cp1
, r
->eqn
== NULL
?
2816 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
2821 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
2823 /* Handle preceding text, protecting whitespace. */
2825 if (*buf
->buf
!= '\0') {
2832 * Prepare replacing the delimiter with an equation macro
2833 * and drop leading white space from the equation.
2836 if (r
->eqn
== NULL
) {
2843 /* Handle following text, protecting whitespace. */
2851 /* Do the actual replacement. */
2853 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
2854 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
2858 /* Toggle the in-line state of the eqn subsystem. */
2860 r
->eqn_inline
= r
->eqn
== NULL
;
2861 return ROFF_REPARSE
;
2869 assert(r
->eqn
== NULL
);
2870 e
= eqn_alloc(ppos
, ln
, r
->parse
);
2873 r
->last_eqn
->next
= e
;
2874 e
->delim
= r
->last_eqn
->delim
;
2875 e
->odelim
= r
->last_eqn
->odelim
;
2876 e
->cdelim
= r
->last_eqn
->cdelim
;
2878 r
->first_eqn
= r
->last_eqn
= e
;
2880 r
->eqn
= r
->last_eqn
= e
;
2882 if (buf
->buf
[pos
] != '\0')
2883 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
2884 ".EQ %s", buf
->buf
+ pos
);
2893 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
, ln
, ppos
, "EN");
2900 struct tbl_node
*tbl
;
2903 mandoc_msg(MANDOCERR_BLK_BROKEN
, r
->parse
,
2904 ln
, ppos
, "TS breaks TS");
2908 tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
2911 r
->last_tbl
->next
= tbl
;
2913 r
->first_tbl
= r
->last_tbl
= tbl
;
2915 r
->tbl
= r
->last_tbl
= tbl
;
2923 buf
->buf
[pos
- 1] = '\0';
2934 if (*p
== '\0' || (r
->control
= *p
++) == '.')
2938 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
2939 ln
, p
- buf
->buf
, "cc ... %s", p
);
2947 const char *p
, *first
, *second
;
2949 enum mandoc_esc esc
;
2954 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
, ln
, ppos
, "tr");
2958 while (*p
!= '\0') {
2962 if (*first
== '\\') {
2963 esc
= mandoc_escape(&p
, NULL
, NULL
);
2964 if (esc
== ESCAPE_ERROR
) {
2965 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
2966 ln
, (int)(p
- buf
->buf
), first
);
2969 fsz
= (size_t)(p
- first
);
2973 if (*second
== '\\') {
2974 esc
= mandoc_escape(&p
, NULL
, NULL
);
2975 if (esc
== ESCAPE_ERROR
) {
2976 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
2977 ln
, (int)(p
- buf
->buf
), second
);
2980 ssz
= (size_t)(p
- second
);
2981 } else if (*second
== '\0') {
2982 mandoc_vmsg(MANDOCERR_TR_ODD
, r
->parse
,
2983 ln
, first
- buf
->buf
, "tr %s", first
);
2989 roff_setstrn(&r
->xmbtab
, first
, fsz
,
2994 if (r
->xtab
== NULL
)
2995 r
->xtab
= mandoc_calloc(128,
2996 sizeof(struct roffstr
));
2998 free(r
->xtab
[(int)*first
].p
);
2999 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
3000 r
->xtab
[(int)*first
].sz
= ssz
;
3011 name
= buf
->buf
+ pos
;
3012 mandoc_vmsg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, "so %s", name
);
3015 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3016 * opening anything that's not in our cwd or anything beneath
3017 * it. Thus, explicitly disallow traversing up the file-system
3018 * or using absolute paths.
3021 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
3022 mandoc_vmsg(MANDOCERR_SO_PATH
, r
->parse
, ln
, ppos
,
3024 buf
->sz
= mandoc_asprintf(&cp
,
3025 ".sp\nSee the file %s.\n.sp", name
) + 1;
3029 return ROFF_REPARSE
;
3036 /* --- user defined strings and macros ------------------------------------ */
3039 roff_userdef(ROFF_ARGS
)
3041 const char *arg
[9], *ap
;
3047 * Collect pointers to macro argument strings
3048 * and NUL-terminate them.
3052 cp
= buf
->buf
+ pos
;
3053 for (i
= 0; i
< 9; i
++) {
3057 arg
[i
] = mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
3063 * Expand macro arguments.
3066 buf
->sz
= strlen(r
->current_string
) + 1;
3067 n1
= cp
= mandoc_malloc(buf
->sz
);
3068 memcpy(n1
, r
->current_string
, buf
->sz
);
3069 while (*cp
!= '\0') {
3071 /* Scan ahead for the next argument invocation. */
3077 if (*cp
== '*') { /* \\$* inserts all arguments */
3080 } else { /* \\$1 .. \\$9 insert one argument */
3081 ib
= ie
= *cp
- '1';
3082 if (ib
< 0 || ib
> 8)
3088 * Determine the size of the expanded argument,
3089 * taking escaping of quotes into account.
3092 asz
= ie
> ib
? ie
- ib
: 0; /* for blanks */
3093 for (i
= ib
; i
<= ie
; i
++) {
3094 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3103 * Determine the size of the rest of the
3104 * unexpanded macro, including the NUL.
3107 rsz
= buf
->sz
- (cp
- n1
) - 3;
3110 * When shrinking, move before
3111 * releasing the storage.
3115 memmove(cp
+ asz
, cp
+ 3, rsz
);
3118 * Resize the storage for the macro
3119 * and readjust the parse pointer.
3123 n2
= mandoc_realloc(n1
, buf
->sz
);
3124 cp
= n2
+ (cp
- n1
);
3128 * When growing, make room
3129 * for the expanded argument.
3133 memmove(cp
+ asz
, cp
+ 3, rsz
);
3136 /* Copy the expanded argument, escaping quotes. */
3139 for (i
= ib
; i
<= ie
; i
++) {
3140 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3142 memcpy(n2
, "\\(dq", 4);
3153 * Replace the macro invocation
3154 * by the expanded macro.
3161 return buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
3162 ROFF_REPARSE
: ROFF_APPEND
;
3166 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
3175 /* Read until end of name and terminate it with NUL. */
3176 for (cp
= name
; 1; cp
++) {
3177 if ('\0' == *cp
|| ' ' == *cp
) {
3184 if ('{' == cp
[1] || '}' == cp
[1])
3189 mandoc_vmsg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
,
3190 "%.*s", (int)(cp
- name
+ 1), name
);
3191 mandoc_escape((const char **)&cp
, NULL
, NULL
);
3195 /* Read past spaces. */
3204 * Store *string into the user-defined string called *name.
3205 * To clear an existing entry, call with (*r, *name, NULL, 0).
3206 * append == 0: replace mode
3207 * append == 1: single-line append mode
3208 * append == 2: multiline append mode, append '\n' after each call
3211 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
3215 roff_setstrn(&r
->strtab
, name
, strlen(name
), string
,
3216 string
? strlen(string
) : 0, append
);
3220 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
3221 const char *string
, size_t stringsz
, int append
)
3226 size_t oldch
, newch
;
3228 /* Search for an existing string with the same name. */
3231 while (n
&& (namesz
!= n
->key
.sz
||
3232 strncmp(n
->key
.p
, name
, namesz
)))
3236 /* Create a new string table entry. */
3237 n
= mandoc_malloc(sizeof(struct roffkv
));
3238 n
->key
.p
= mandoc_strndup(name
, namesz
);
3244 } else if (0 == append
) {
3254 * One additional byte for the '\n' in multiline mode,
3255 * and one for the terminating '\0'.
3257 newch
= stringsz
+ (1 < append
? 2u : 1u);
3259 if (NULL
== n
->val
.p
) {
3260 n
->val
.p
= mandoc_malloc(newch
);
3265 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
3268 /* Skip existing content in the destination buffer. */
3269 c
= n
->val
.p
+ (int)oldch
;
3271 /* Append new content to the destination buffer. */
3273 while (i
< (int)stringsz
) {
3275 * Rudimentary roff copy mode:
3276 * Handle escaped backslashes.
3278 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
3283 /* Append terminating bytes. */
3288 n
->val
.sz
= (int)(c
- n
->val
.p
);
3292 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
3294 const struct roffkv
*n
;
3297 for (n
= r
->strtab
; n
; n
= n
->next
)
3298 if (0 == strncmp(name
, n
->key
.p
, len
) &&
3299 '\0' == n
->key
.p
[(int)len
])
3302 for (i
= 0; i
< PREDEFS_MAX
; i
++)
3303 if (0 == strncmp(name
, predefs
[i
].name
, len
) &&
3304 '\0' == predefs
[i
].name
[(int)len
])
3305 return predefs
[i
].str
;
3311 roff_freestr(struct roffkv
*r
)
3313 struct roffkv
*n
, *nn
;
3315 for (n
= r
; n
; n
= nn
) {
3323 /* --- accessors and utility functions ------------------------------------ */
3325 const struct tbl_span
*
3326 roff_span(const struct roff
*r
)
3329 return r
->tbl
? tbl_span(r
->tbl
) : NULL
;
3333 roff_eqn(const struct roff
*r
)
3336 return r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
;
3340 * Duplicate an input string, making the appropriate character
3341 * conversations (as stipulated by `tr') along the way.
3342 * Returns a heap-allocated string with all the replacements made.
3345 roff_strdup(const struct roff
*r
, const char *p
)
3347 const struct roffkv
*cp
;
3351 enum mandoc_esc esc
;
3353 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
3354 return mandoc_strdup(p
);
3355 else if ('\0' == *p
)
3356 return mandoc_strdup("");
3359 * Step through each character looking for term matches
3360 * (remember that a `tr' can be invoked with an escape, which is
3361 * a glyph but the escape is multi-character).
3362 * We only do this if the character hash has been initialised
3363 * and the string is >0 length.
3369 while ('\0' != *p
) {
3370 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(int)*p
].p
) {
3371 sz
= r
->xtab
[(int)*p
].sz
;
3372 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3373 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
3377 } else if ('\\' != *p
) {
3378 res
= mandoc_realloc(res
, ssz
+ 2);
3383 /* Search for term matches. */
3384 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
3385 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
3390 * A match has been found.
3391 * Append the match to the array and move
3392 * forward by its keysize.
3394 res
= mandoc_realloc(res
,
3395 ssz
+ cp
->val
.sz
+ 1);
3396 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
3398 p
+= (int)cp
->key
.sz
;
3403 * Handle escapes carefully: we need to copy
3404 * over just the escape itself, or else we might
3405 * do replacements within the escape itself.
3406 * Make sure to pass along the bogus string.
3409 esc
= mandoc_escape(&p
, NULL
, NULL
);
3410 if (ESCAPE_ERROR
== esc
) {
3412 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3413 memcpy(res
+ ssz
, pp
, sz
);
3417 * We bail out on bad escapes.
3418 * No need to warn: we already did so when
3419 * roff_res() was called.
3422 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3423 memcpy(res
+ ssz
, pp
, sz
);
3427 res
[(int)ssz
] = '\0';
3432 roff_getformat(const struct roff
*r
)
3439 * Find out whether a line is a macro line or not.
3440 * If it is, adjust the current position and return one; if it isn't,
3441 * return zero and don't change the current position.
3442 * If the control character has been set with `.cc', then let that grain
3444 * This is slighly contrary to groff, where using the non-breaking
3445 * control character when `cc' has been invoked will cause the
3446 * non-breaking macro contents to be printed verbatim.
3449 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
3455 if (0 != r
->control
&& cp
[pos
] == r
->control
)
3457 else if (0 != r
->control
)
3459 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
3461 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
3466 while (' ' == cp
[pos
] || '\t' == cp
[pos
])