]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.275 2015/08/29 23:56:01 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 nested if-else conditionals. */
37 #define RSTACK_MAX 128
39 /* Maximum number of string expansions per line, to break infinite loops. */
40 #define EXPAND_LIMIT 1000
42 /* --- data types --------------------------------------------------------- */
125 /* MAN_fi; ignored in mdoc(7) */
134 /* MAN_ft; ignored in mdoc(7) */
156 /* MAN_in; ignored in mdoc(7) */
174 /* MAN_ll, MDOC_ll */
188 /* MAN_nf; ignored in mdoc(7) */
239 /* MAN_sp, MDOC_sp */
292 * An incredibly-simple string buffer.
295 char *p
; /* nil-terminated buffer */
296 size_t sz
; /* saved strlen(p) */
300 * A key-value roffstr pair as part of a singly-linked list.
305 struct roffkv
*next
; /* next in list */
309 * A single number register as part of a singly-linked list.
314 struct roffreg
*next
;
318 struct mparse
*parse
; /* parse point */
319 const struct mchars
*mchars
; /* character table */
320 struct roffnode
*last
; /* leaf of stack */
321 int *rstack
; /* stack of inverted `ie' values */
322 struct roffreg
*regtab
; /* number registers */
323 struct roffkv
*strtab
; /* user-defined strings & macros */
324 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
325 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
326 const char *current_string
; /* value of last called user macro */
327 struct tbl_node
*first_tbl
; /* first table parsed */
328 struct tbl_node
*last_tbl
; /* last table parsed */
329 struct tbl_node
*tbl
; /* current table being parsed */
330 struct eqn_node
*last_eqn
; /* last equation parsed */
331 struct eqn_node
*first_eqn
; /* first equation parsed */
332 struct eqn_node
*eqn
; /* current equation being parsed */
333 int eqn_inline
; /* current equation is inline */
334 int options
; /* parse options */
335 int rstacksz
; /* current size limit of rstack */
336 int rstackpos
; /* position in rstack */
337 int format
; /* current file in mdoc or man format */
338 int argc
; /* number of args of the last macro */
339 char control
; /* control character */
343 enum rofft tok
; /* type of node */
344 struct roffnode
*parent
; /* up one in stack */
345 int line
; /* parse line */
346 int col
; /* parse col */
347 char *name
; /* node name, e.g. macro name */
348 char *end
; /* end-rules: custom token */
349 int endspan
; /* end-rules: next-line or infty */
350 int rule
; /* current evaluation rule */
353 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
354 enum rofft tok, /* tok of macro */ \
355 struct buf *buf, /* input buffer */ \
356 int ln, /* parse line */ \
357 int ppos, /* original pos in buffer */ \
358 int pos, /* current pos in buffer */ \
359 int *offs /* reset offset of buffer data */
361 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
364 const char *name
; /* macro name */
365 roffproc proc
; /* process new macro */
366 roffproc text
; /* process as child text of macro */
367 roffproc sub
; /* process as child of macro */
369 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
370 struct roffmac
*next
;
374 const char *name
; /* predefined input name */
375 const char *str
; /* replacement symbol */
378 #define PREDEF(__name, __str) \
379 { (__name), (__str) },
381 /* --- function prototypes ------------------------------------------------ */
383 static enum rofft
roffhash_find(const char *, size_t);
384 static void roffhash_init(void);
385 static void roffnode_cleanscope(struct roff
*);
386 static void roffnode_pop(struct roff
*);
387 static void roffnode_push(struct roff
*, enum rofft
,
388 const char *, int, int);
389 static enum rofferr
roff_block(ROFF_ARGS
);
390 static enum rofferr
roff_block_text(ROFF_ARGS
);
391 static enum rofferr
roff_block_sub(ROFF_ARGS
);
392 static enum rofferr
roff_brp(ROFF_ARGS
);
393 static enum rofferr
roff_cblock(ROFF_ARGS
);
394 static enum rofferr
roff_cc(ROFF_ARGS
);
395 static void roff_ccond(struct roff
*, int, int);
396 static enum rofferr
roff_cond(ROFF_ARGS
);
397 static enum rofferr
roff_cond_text(ROFF_ARGS
);
398 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
399 static enum rofferr
roff_ds(ROFF_ARGS
);
400 static enum rofferr
roff_eqndelim(struct roff
*, struct buf
*, int);
401 static int roff_evalcond(struct roff
*r
, int, char *, int *);
402 static int roff_evalnum(struct roff
*, int,
403 const char *, int *, int *, int);
404 static int roff_evalpar(struct roff
*, int,
405 const char *, int *, int *, int);
406 static int roff_evalstrcond(const char *, int *);
407 static void roff_free1(struct roff
*);
408 static void roff_freereg(struct roffreg
*);
409 static void roff_freestr(struct roffkv
*);
410 static size_t roff_getname(struct roff
*, char **, int, int);
411 static int roff_getnum(const char *, int *, int *, int);
412 static int roff_getop(const char *, int *, char *);
413 static int roff_getregn(const struct roff
*,
414 const char *, size_t);
415 static int roff_getregro(const struct roff
*,
417 static const char *roff_getstrn(const struct roff
*,
418 const char *, size_t);
419 static int roff_hasregn(const struct roff
*,
420 const char *, size_t);
421 static enum rofferr
roff_insec(ROFF_ARGS
);
422 static enum rofferr
roff_it(ROFF_ARGS
);
423 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
424 static void roff_man_alloc1(struct roff_man
*);
425 static void roff_man_free1(struct roff_man
*);
426 static enum rofferr
roff_nr(ROFF_ARGS
);
427 static enum rofft
roff_parse(struct roff
*, char *, int *,
429 static enum rofferr
roff_parsetext(struct buf
*, int, int *);
430 static enum rofferr
roff_res(struct roff
*, struct buf
*, int, int);
431 static enum rofferr
roff_rm(ROFF_ARGS
);
432 static enum rofferr
roff_rr(ROFF_ARGS
);
433 static void roff_setstr(struct roff
*,
434 const char *, const char *, int);
435 static void roff_setstrn(struct roffkv
**, const char *,
436 size_t, const char *, size_t, int);
437 static enum rofferr
roff_so(ROFF_ARGS
);
438 static enum rofferr
roff_tr(ROFF_ARGS
);
439 static enum rofferr
roff_Dd(ROFF_ARGS
);
440 static enum rofferr
roff_TH(ROFF_ARGS
);
441 static enum rofferr
roff_TE(ROFF_ARGS
);
442 static enum rofferr
roff_TS(ROFF_ARGS
);
443 static enum rofferr
roff_EQ(ROFF_ARGS
);
444 static enum rofferr
roff_EN(ROFF_ARGS
);
445 static enum rofferr
roff_T_(ROFF_ARGS
);
446 static enum rofferr
roff_unsupp(ROFF_ARGS
);
447 static enum rofferr
roff_userdef(ROFF_ARGS
);
449 /* --- constant data ------------------------------------------------------ */
451 /* See roffhash_find() */
455 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
457 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
458 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
460 static struct roffmac
*hash
[HASHWIDTH
];
462 static struct roffmac roffs
[ROFF_MAX
] = {
463 { "ab", roff_unsupp
, NULL
, NULL
, 0, NULL
},
464 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
465 { "af", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
466 { "aln", roff_unsupp
, NULL
, NULL
, 0, NULL
},
467 { "als", roff_unsupp
, NULL
, NULL
, 0, NULL
},
468 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
469 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
470 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
471 { "ami1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
472 { "as", roff_ds
, NULL
, NULL
, 0, NULL
},
473 { "as1", roff_ds
, NULL
, NULL
, 0, NULL
},
474 { "asciify", roff_unsupp
, NULL
, NULL
, 0, NULL
},
475 { "backtrace", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
476 { "bd", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
477 { "bleedat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
478 { "blm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
479 { "box", roff_unsupp
, NULL
, NULL
, 0, NULL
},
480 { "boxa", roff_unsupp
, NULL
, NULL
, 0, NULL
},
481 { "bp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
482 { "BP", roff_unsupp
, NULL
, NULL
, 0, NULL
},
483 { "break", roff_unsupp
, NULL
, NULL
, 0, NULL
},
484 { "breakchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
485 { "brnl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
486 { "brp", roff_brp
, NULL
, NULL
, 0, NULL
},
487 { "brpnl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
488 { "c2", roff_unsupp
, NULL
, NULL
, 0, NULL
},
489 { "cc", roff_cc
, NULL
, NULL
, 0, NULL
},
490 { "ce", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
491 { "cf", roff_insec
, NULL
, NULL
, 0, NULL
},
492 { "cflags", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
493 { "ch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
494 { "char", roff_unsupp
, NULL
, NULL
, 0, NULL
},
495 { "chop", roff_unsupp
, NULL
, NULL
, 0, NULL
},
496 { "class", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
497 { "close", roff_insec
, NULL
, NULL
, 0, NULL
},
498 { "CL", roff_unsupp
, NULL
, NULL
, 0, NULL
},
499 { "color", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
500 { "composite", roff_unsupp
, NULL
, NULL
, 0, NULL
},
501 { "continue", roff_unsupp
, NULL
, NULL
, 0, NULL
},
502 { "cp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
503 { "cropat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
504 { "cs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
505 { "cu", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
506 { "da", roff_unsupp
, NULL
, NULL
, 0, NULL
},
507 { "dch", roff_unsupp
, NULL
, NULL
, 0, NULL
},
508 { "Dd", roff_Dd
, NULL
, NULL
, 0, NULL
},
509 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
510 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
511 { "defcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
512 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
513 { "dei1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
514 { "device", roff_unsupp
, NULL
, NULL
, 0, NULL
},
515 { "devicem", roff_unsupp
, NULL
, NULL
, 0, NULL
},
516 { "di", roff_unsupp
, NULL
, NULL
, 0, NULL
},
517 { "do", roff_unsupp
, NULL
, NULL
, 0, NULL
},
518 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
519 { "ds1", roff_ds
, NULL
, NULL
, 0, NULL
},
520 { "dwh", roff_unsupp
, NULL
, NULL
, 0, NULL
},
521 { "dt", roff_unsupp
, NULL
, NULL
, 0, NULL
},
522 { "ec", roff_unsupp
, NULL
, NULL
, 0, NULL
},
523 { "ecr", roff_unsupp
, NULL
, NULL
, 0, NULL
},
524 { "ecs", roff_unsupp
, NULL
, NULL
, 0, NULL
},
525 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
526 { "em", roff_unsupp
, NULL
, NULL
, 0, NULL
},
527 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
528 { "eo", roff_unsupp
, NULL
, NULL
, 0, NULL
},
529 { "EP", roff_unsupp
, NULL
, NULL
, 0, NULL
},
530 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
531 { "errprint", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
532 { "ev", roff_unsupp
, NULL
, NULL
, 0, NULL
},
533 { "evc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
534 { "ex", roff_unsupp
, NULL
, NULL
, 0, NULL
},
535 { "fallback", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
536 { "fam", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
537 { "fc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
538 { "fchar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
539 { "fcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
540 { "fdeferlig", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
541 { "feature", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
542 { "fkern", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
543 { "fl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
544 { "flig", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
545 { "fp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
546 { "fps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
547 { "fschar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
548 { "fspacewidth", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
549 { "fspecial", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
550 { "ftr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
551 { "fzoom", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
552 { "gcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
553 { "hc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
554 { "hcode", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
555 { "hidechar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
556 { "hla", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
557 { "hlm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
558 { "hpf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
559 { "hpfa", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
560 { "hpfcode", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
561 { "hw", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
562 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
563 { "hylang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
564 { "hylen", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
565 { "hym", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
566 { "hypp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
567 { "hys", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
568 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
569 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
570 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
571 { "index", roff_unsupp
, NULL
, NULL
, 0, NULL
},
572 { "it", roff_it
, NULL
, NULL
, 0, NULL
},
573 { "itc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
574 { "IX", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
575 { "kern", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
576 { "kernafter", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
577 { "kernbefore", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
578 { "kernpair", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
579 { "lc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
580 { "lc_ctype", roff_unsupp
, NULL
, NULL
, 0, NULL
},
581 { "lds", roff_unsupp
, NULL
, NULL
, 0, NULL
},
582 { "length", roff_unsupp
, NULL
, NULL
, 0, NULL
},
583 { "letadj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
584 { "lf", roff_insec
, NULL
, NULL
, 0, NULL
},
585 { "lg", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
586 { "lhang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
587 { "linetabs", roff_unsupp
, NULL
, NULL
, 0, NULL
},
588 { "lnr", roff_unsupp
, NULL
, NULL
, 0, NULL
},
589 { "lnrf", roff_unsupp
, NULL
, NULL
, 0, NULL
},
590 { "lpfx", roff_unsupp
, NULL
, NULL
, 0, NULL
},
591 { "ls", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
592 { "lsm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
593 { "lt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
594 { "mc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
595 { "mediasize", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
596 { "minss", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
597 { "mk", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
598 { "mso", roff_insec
, NULL
, NULL
, 0, NULL
},
599 { "na", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
600 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
601 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
602 { "nhychar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
603 { "nm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
604 { "nn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
605 { "nop", roff_unsupp
, NULL
, NULL
, 0, NULL
},
606 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
607 { "nrf", roff_unsupp
, NULL
, NULL
, 0, NULL
},
608 { "nroff", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
609 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
610 { "nx", roff_insec
, NULL
, NULL
, 0, NULL
},
611 { "open", roff_insec
, NULL
, NULL
, 0, NULL
},
612 { "opena", roff_insec
, NULL
, NULL
, 0, NULL
},
613 { "os", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
614 { "output", roff_unsupp
, NULL
, NULL
, 0, NULL
},
615 { "padj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
616 { "papersize", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
617 { "pc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
618 { "pev", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
619 { "pi", roff_insec
, NULL
, NULL
, 0, NULL
},
620 { "PI", roff_unsupp
, NULL
, NULL
, 0, NULL
},
621 { "pl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
622 { "pm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
623 { "pn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
624 { "pnr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
625 { "po", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
626 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
627 { "psbb", roff_unsupp
, NULL
, NULL
, 0, NULL
},
628 { "pshape", roff_unsupp
, NULL
, NULL
, 0, NULL
},
629 { "pso", roff_insec
, NULL
, NULL
, 0, NULL
},
630 { "ptr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
631 { "pvs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
632 { "rchar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
633 { "rd", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
634 { "recursionlimit", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
635 { "return", roff_unsupp
, NULL
, NULL
, 0, NULL
},
636 { "rfschar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
637 { "rhang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
638 { "rj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
639 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
640 { "rn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
641 { "rnn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
642 { "rr", roff_rr
, NULL
, NULL
, 0, NULL
},
643 { "rs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
644 { "rt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
645 { "schar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
646 { "sentchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
647 { "shc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
648 { "shift", roff_unsupp
, NULL
, NULL
, 0, NULL
},
649 { "sizes", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
650 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
651 { "spacewidth", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
652 { "special", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
653 { "spreadwarn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
654 { "ss", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
655 { "sty", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
656 { "substring", roff_unsupp
, NULL
, NULL
, 0, NULL
},
657 { "sv", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
658 { "sy", roff_insec
, NULL
, NULL
, 0, NULL
},
659 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
660 { "ta", roff_unsupp
, NULL
, NULL
, 0, NULL
},
661 { "tc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
662 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
663 { "TH", roff_TH
, NULL
, NULL
, 0, NULL
},
664 { "ti", roff_unsupp
, NULL
, NULL
, 0, NULL
},
665 { "tkf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
666 { "tl", roff_unsupp
, NULL
, NULL
, 0, NULL
},
667 { "tm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
668 { "tm1", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
669 { "tmc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
670 { "tr", roff_tr
, NULL
, NULL
, 0, NULL
},
671 { "track", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
672 { "transchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
673 { "trf", roff_insec
, NULL
, NULL
, 0, NULL
},
674 { "trimat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
675 { "trin", roff_unsupp
, NULL
, NULL
, 0, NULL
},
676 { "trnt", roff_unsupp
, NULL
, NULL
, 0, NULL
},
677 { "troff", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
678 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
679 { "uf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
680 { "ul", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
681 { "unformat", roff_unsupp
, NULL
, NULL
, 0, NULL
},
682 { "unwatch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
683 { "unwatchn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
684 { "vpt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
685 { "vs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
686 { "warn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
687 { "warnscale", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
688 { "watch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
689 { "watchlength", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
690 { "watchn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
691 { "wh", roff_unsupp
, NULL
, NULL
, 0, NULL
},
692 { "while", roff_unsupp
, NULL
, NULL
, 0, NULL
},
693 { "write", roff_insec
, NULL
, NULL
, 0, NULL
},
694 { "writec", roff_insec
, NULL
, NULL
, 0, NULL
},
695 { "writem", roff_insec
, NULL
, NULL
, 0, NULL
},
696 { "xflag", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
697 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
698 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
701 /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
702 const char *const __mdoc_reserved
[] = {
703 "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
704 "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
705 "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
706 "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
707 "Dt", "Dv", "Dx", "D1",
708 "Ec", "Ed", "Ef", "Ek", "El", "Em",
709 "En", "Eo", "Er", "Es", "Ev", "Ex",
710 "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
711 "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
712 "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
713 "Oc", "Oo", "Op", "Os", "Ot", "Ox",
714 "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
715 "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
716 "Sc", "Sh", "Sm", "So", "Sq",
717 "Ss", "St", "Sx", "Sy",
718 "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
719 "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
720 "%P", "%Q", "%R", "%T", "%U", "%V",
724 /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
725 const char *const __man_reserved
[] = {
726 "AT", "B", "BI", "BR", "DT",
727 "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
728 "LP", "OP", "P", "PD", "PP",
729 "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
730 "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
734 /* Array of injected predefined strings. */
735 #define PREDEFS_MAX 38
736 static const struct predef predefs
[PREDEFS_MAX
] = {
737 #include "predefs.in"
740 /* See roffhash_find() */
741 #define ROFF_HASH(p) (p[0] - ASCII_LO)
743 static int roffit_lines
; /* number of lines to delay */
744 static char *roffit_macro
; /* nil-terminated macro line */
747 /* --- request table ------------------------------------------------------ */
755 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
756 assert(roffs
[i
].name
[0] >= ASCII_LO
);
757 assert(roffs
[i
].name
[0] <= ASCII_HI
);
759 buc
= ROFF_HASH(roffs
[i
].name
);
761 if (NULL
!= (n
= hash
[buc
])) {
762 for ( ; n
->next
; n
= n
->next
)
766 hash
[buc
] = &roffs
[i
];
771 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
772 * the nil-terminated string name could be found.
775 roffhash_find(const char *p
, size_t s
)
781 * libroff has an extremely simple hashtable, for the time
782 * being, which simply keys on the first character, which must
783 * be printable, then walks a chain. It works well enough until
787 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
792 if (NULL
== (n
= hash
[buc
]))
794 for ( ; n
; n
= n
->next
)
795 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
796 return((enum rofft
)(n
- roffs
));
801 /* --- stack of request blocks -------------------------------------------- */
804 * Pop the current node off of the stack of roff instructions currently
808 roffnode_pop(struct roff
*r
)
815 r
->last
= r
->last
->parent
;
822 * Push a roff node onto the instruction stack. This must later be
823 * removed with roffnode_pop().
826 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
831 p
= mandoc_calloc(1, sizeof(struct roffnode
));
834 p
->name
= mandoc_strdup(name
);
838 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
843 /* --- roff parser state data management ---------------------------------- */
846 roff_free1(struct roff
*r
)
848 struct tbl_node
*tbl
;
852 while (NULL
!= (tbl
= r
->first_tbl
)) {
853 r
->first_tbl
= tbl
->next
;
856 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
858 while (NULL
!= (e
= r
->first_eqn
)) {
859 r
->first_eqn
= e
->next
;
862 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
872 roff_freereg(r
->regtab
);
875 roff_freestr(r
->strtab
);
876 roff_freestr(r
->xmbtab
);
877 r
->strtab
= r
->xmbtab
= NULL
;
880 for (i
= 0; i
< 128; i
++)
887 roff_reset(struct roff
*r
)
891 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
896 roff_free(struct roff
*r
)
904 roff_alloc(struct mparse
*parse
, const struct mchars
*mchars
, int options
)
908 r
= mandoc_calloc(1, sizeof(struct roff
));
911 r
->options
= options
;
912 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
920 /* --- syntax tree state data management ---------------------------------- */
923 roff_man_free1(struct roff_man
*man
)
926 if (man
->first
!= NULL
)
927 roff_node_delete(man
, man
->first
);
928 free(man
->meta
.msec
);
931 free(man
->meta
.arch
);
932 free(man
->meta
.title
);
933 free(man
->meta
.name
);
934 free(man
->meta
.date
);
938 roff_man_alloc1(struct roff_man
*man
)
941 memset(&man
->meta
, 0, sizeof(man
->meta
));
942 man
->first
= mandoc_calloc(1, sizeof(*man
->first
));
943 man
->first
->type
= ROFFT_ROOT
;
944 man
->last
= man
->first
;
947 man
->macroset
= MACROSET_NONE
;
948 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
949 man
->next
= ROFF_NEXT_CHILD
;
953 roff_man_reset(struct roff_man
*man
)
957 roff_man_alloc1(man
);
961 roff_man_free(struct roff_man
*man
)
969 roff_man_alloc(struct roff
*roff
, struct mparse
*parse
,
970 const char *defos
, int quick
)
972 struct roff_man
*man
;
974 man
= mandoc_calloc(1, sizeof(*man
));
979 roff_man_alloc1(man
);
983 /* --- syntax tree handling ----------------------------------------------- */
986 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
987 enum roff_type type
, int tok
)
991 n
= mandoc_calloc(1, sizeof(*n
));
996 n
->sec
= man
->lastsec
;
998 if (man
->flags
& MDOC_SYNOPSIS
)
999 n
->flags
|= MDOC_SYNPRETTY
;
1001 n
->flags
&= ~MDOC_SYNPRETTY
;
1002 if (man
->flags
& MDOC_NEWLINE
)
1003 n
->flags
|= MDOC_LINE
;
1004 man
->flags
&= ~MDOC_NEWLINE
;
1010 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
1013 switch (man
->next
) {
1014 case ROFF_NEXT_SIBLING
:
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
;
1027 n
->parent
->nchild
++;
1028 n
->parent
->last
= n
;
1031 * Copy over the normalised-data pointer of our parent. Not
1032 * everybody has one, but copying a null pointer is fine.
1037 if (n
->end
!= ENDBODY_NOT
)
1043 n
->norm
= n
->parent
->norm
;
1049 if (man
->macroset
== MACROSET_MDOC
)
1050 mdoc_valid_pre(man
, n
);
1054 assert(n
->parent
->type
== ROFFT_BLOCK
);
1055 n
->parent
->head
= n
;
1060 assert(n
->parent
->type
== ROFFT_BLOCK
);
1061 n
->parent
->body
= n
;
1064 assert(n
->parent
->type
== ROFFT_BLOCK
);
1065 n
->parent
->tail
= n
;
1074 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
1076 struct roff_node
*n
;
1078 n
= roff_node_alloc(man
, line
, pos
, ROFFT_TEXT
, TOKEN_NONE
);
1079 n
->string
= roff_strdup(man
->roff
, word
);
1080 roff_node_append(man
, n
);
1081 if (man
->macroset
== MACROSET_MDOC
)
1082 mdoc_valid_post(man
);
1084 man_valid_post(man
);
1085 man
->next
= ROFF_NEXT_SIBLING
;
1089 roff_word_append(struct roff_man
*man
, const char *word
)
1091 struct roff_node
*n
;
1092 char *addstr
, *newstr
;
1095 addstr
= roff_strdup(man
->roff
, word
);
1096 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
1100 man
->next
= ROFF_NEXT_SIBLING
;
1104 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1106 struct roff_node
*n
;
1108 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
1109 roff_node_append(man
, n
);
1110 man
->next
= ROFF_NEXT_CHILD
;
1114 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1116 struct roff_node
*n
;
1118 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
1119 roff_node_append(man
, n
);
1120 man
->next
= ROFF_NEXT_CHILD
;
1125 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1127 struct roff_node
*n
;
1129 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
1130 roff_node_append(man
, n
);
1131 man
->next
= ROFF_NEXT_CHILD
;
1136 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1138 struct roff_node
*n
;
1140 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
1141 roff_node_append(man
, n
);
1142 man
->next
= ROFF_NEXT_CHILD
;
1147 roff_addeqn(struct roff_man
*man
, const struct eqn
*eqn
)
1149 struct roff_node
*n
;
1151 n
= roff_node_alloc(man
, eqn
->ln
, eqn
->pos
, ROFFT_EQN
, TOKEN_NONE
);
1153 if (eqn
->ln
> man
->last
->line
)
1154 n
->flags
|= MDOC_LINE
;
1155 roff_node_append(man
, n
);
1156 man
->next
= ROFF_NEXT_SIBLING
;
1160 roff_addtbl(struct roff_man
*man
, const struct tbl_span
*tbl
)
1162 struct roff_node
*n
;
1164 if (man
->macroset
== MACROSET_MAN
)
1165 man_breakscope(man
, TOKEN_NONE
);
1166 n
= roff_node_alloc(man
, tbl
->line
, 0, ROFFT_TBL
, TOKEN_NONE
);
1168 roff_node_append(man
, n
);
1169 if (man
->macroset
== MACROSET_MDOC
)
1170 mdoc_valid_post(man
);
1172 man_valid_post(man
);
1173 man
->next
= ROFF_NEXT_SIBLING
;
1177 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1180 /* Adjust siblings. */
1183 n
->prev
->next
= n
->next
;
1185 n
->next
->prev
= n
->prev
;
1187 /* Adjust parent. */
1189 if (n
->parent
!= NULL
) {
1190 n
->parent
->nchild
--;
1191 if (n
->parent
->child
== n
)
1192 n
->parent
->child
= n
->next
;
1193 if (n
->parent
->last
== n
)
1194 n
->parent
->last
= n
->prev
;
1197 /* Adjust parse point. */
1201 if (man
->last
== n
) {
1202 if (n
->prev
== NULL
) {
1203 man
->last
= n
->parent
;
1204 man
->next
= ROFF_NEXT_CHILD
;
1206 man
->last
= n
->prev
;
1207 man
->next
= ROFF_NEXT_SIBLING
;
1210 if (man
->first
== n
)
1215 roff_node_free(struct roff_node
*n
)
1218 if (n
->args
!= NULL
)
1219 mdoc_argv_free(n
->args
);
1220 if (n
->type
== ROFFT_BLOCK
|| n
->type
== ROFFT_ELEM
)
1227 roff_node_delete(struct roff_man
*man
, struct roff_node
*n
)
1230 while (n
->child
!= NULL
)
1231 roff_node_delete(man
, n
->child
);
1232 assert(n
->nchild
== 0);
1233 roff_node_unlink(man
, n
);
1238 deroff(char **dest
, const struct roff_node
*n
)
1243 if (n
->type
!= ROFFT_TEXT
) {
1244 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1249 /* Skip leading whitespace and escape sequences. */
1252 while (*cp
!= '\0') {
1255 mandoc_escape((const char **)&cp
, NULL
, NULL
);
1256 } else if (isspace((unsigned char)*cp
))
1262 /* Skip trailing whitespace. */
1264 for (sz
= strlen(cp
); sz
; sz
--)
1265 if ( ! isspace((unsigned char)cp
[sz
-1]))
1268 /* Skip empty strings. */
1273 if (*dest
== NULL
) {
1274 *dest
= mandoc_strndup(cp
, sz
);
1278 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);
1283 /* --- main functions of the roff parser ---------------------------------- */
1286 * In the current line, expand escape sequences that tend to get
1287 * used in numerical expressions and conditional requests.
1288 * Also check the syntax of the remaining escape sequences.
1291 roff_res(struct roff
*r
, struct buf
*buf
, int ln
, int pos
)
1293 char ubuf
[24]; /* buffer to print the number */
1294 const char *start
; /* start of the string to process */
1295 char *stesc
; /* start of an escape sequence ('\\') */
1296 const char *stnam
; /* start of the name, after "[(*" */
1297 const char *cp
; /* end of the name, e.g. before ']' */
1298 const char *res
; /* the string to be substituted */
1299 char *nbuf
; /* new buffer to copy buf->buf to */
1300 size_t maxl
; /* expected length of the escape name */
1301 size_t naml
; /* actual length of the escape name */
1302 enum mandoc_esc esc
; /* type of the escape sequence */
1303 int inaml
; /* length returned from mandoc_escape() */
1304 int expand_count
; /* to avoid infinite loops */
1305 int npos
; /* position in numeric expression */
1306 int arg_complete
; /* argument not interrupted by eol */
1307 char term
; /* character terminating the escape */
1310 start
= buf
->buf
+ pos
;
1311 stesc
= strchr(start
, '\0') - 1;
1312 while (stesc
-- > start
) {
1314 /* Search backwards for the next backslash. */
1319 /* If it is escaped, skip it. */
1321 for (cp
= stesc
- 1; cp
>= start
; cp
--)
1325 if ((stesc
- cp
) % 2 == 0) {
1330 /* Decide whether to expand or to check only. */
1347 esc
= mandoc_escape(&cp
, &stnam
, &inaml
);
1348 if (esc
== ESCAPE_ERROR
||
1349 (esc
== ESCAPE_SPECIAL
&&
1350 mchars_spec2cp(r
->mchars
, stnam
, inaml
) < 0))
1351 mandoc_vmsg(MANDOCERR_ESC_BAD
,
1352 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1353 "%.*s", (int)(cp
- stesc
), stesc
);
1357 if (EXPAND_LIMIT
< ++expand_count
) {
1358 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1359 ln
, (int)(stesc
- buf
->buf
), NULL
);
1364 * The third character decides the length
1365 * of the name of the string or register.
1366 * Save a pointer to the name.
1393 /* Advance to the end of the name. */
1397 while (maxl
== 0 || naml
< maxl
) {
1399 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
1400 ln
, (int)(stesc
- buf
->buf
), stesc
);
1404 if (maxl
== 0 && *cp
== term
) {
1408 if (*cp
++ != '\\' || stesc
[1] != 'w') {
1412 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1413 case ESCAPE_SPECIAL
:
1415 case ESCAPE_UNICODE
:
1417 case ESCAPE_NUMBERED
:
1419 case ESCAPE_OVERSTRIKE
:
1428 * Retrieve the replacement string; if it is
1429 * undefined, resume searching for escapes.
1435 res
= roff_getstrn(r
, stnam
, naml
);
1439 ubuf
[0] = arg_complete
&&
1440 roff_evalnum(r
, ln
, stnam
, &npos
,
1441 NULL
, ROFFNUM_SCALE
) &&
1442 stnam
+ npos
+ 1 == cp
? '1' : '0';
1447 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1448 roff_getregn(r
, stnam
, naml
));
1453 /* use even incomplete args */
1454 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1460 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1461 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1462 "%.*s", (int)naml
, stnam
);
1464 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1465 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1466 ln
, (int)(stesc
- buf
->buf
), NULL
);
1470 /* Replace the escape sequence by the string. */
1473 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1474 buf
->buf
, res
, cp
) + 1;
1476 /* Prepare for the next replacement. */
1479 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1487 * Process text streams.
1490 roff_parsetext(struct buf
*buf
, int pos
, int *offs
)
1496 enum mandoc_esc esc
;
1498 /* Spring the input line trap. */
1500 if (roffit_lines
== 1) {
1501 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1508 return(ROFF_REPARSE
);
1509 } else if (roffit_lines
> 1)
1512 /* Convert all breakable hyphens into ASCII_HYPH. */
1514 start
= p
= buf
->buf
+ pos
;
1516 while (*p
!= '\0') {
1517 sz
= strcspn(p
, "-\\");
1524 /* Skip over escapes. */
1526 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1527 if (esc
== ESCAPE_ERROR
)
1532 } else if (p
== start
) {
1537 if (isalpha((unsigned char)p
[-1]) &&
1538 isalpha((unsigned char)p
[1]))
1546 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
1550 int pos
; /* parse point */
1551 int spos
; /* saved parse point for messages */
1552 int ppos
; /* original offset in buf->buf */
1553 int ctl
; /* macro line (boolean) */
1557 /* Handle in-line equation delimiters. */
1559 if (r
->tbl
== NULL
&&
1560 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1561 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1562 e
= roff_eqndelim(r
, buf
, pos
);
1563 if (e
== ROFF_REPARSE
)
1565 assert(e
== ROFF_CONT
);
1568 /* Expand some escape sequences. */
1570 e
= roff_res(r
, buf
, ln
, pos
);
1573 assert(e
== ROFF_CONT
);
1575 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1578 * First, if a scope is open and we're not a macro, pass the
1579 * text through the macro's filter.
1580 * Equations process all content themselves.
1581 * Tables process almost all content themselves, but we want
1582 * to warn about macros before passing it there.
1585 if (r
->last
!= NULL
&& ! ctl
) {
1587 assert(roffs
[t
].text
);
1588 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1589 assert(e
== ROFF_IGN
|| e
== ROFF_CONT
);
1594 return(eqn_read(&r
->eqn
, ln
, buf
->buf
, ppos
, offs
));
1595 if (r
->tbl
!= NULL
&& ( ! ctl
|| buf
->buf
[pos
] == '\0'))
1596 return(tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
));
1598 return(roff_parsetext(buf
, pos
, offs
));
1600 /* Skip empty request lines. */
1602 if (buf
->buf
[pos
] == '"') {
1603 mandoc_msg(MANDOCERR_COMMENT_BAD
, r
->parse
,
1606 } else if (buf
->buf
[pos
] == '\0')
1610 * If a scope is open, go to the child handler for that macro,
1611 * as it may want to preprocess before doing anything with it.
1612 * Don't do so if an equation is open.
1617 assert(roffs
[t
].sub
);
1618 return((*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1621 /* No scope is open. This is a new request or macro. */
1624 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1626 /* Tables ignore most macros. */
1628 if (r
->tbl
!= NULL
&& (t
== ROFF_MAX
|| t
== ROFF_TS
)) {
1629 mandoc_msg(MANDOCERR_TBLMACRO
, r
->parse
,
1630 ln
, pos
, buf
->buf
+ spos
);
1633 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1635 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] == ' ')
1637 return(tbl_read(r
->tbl
, ln
, buf
->buf
, pos
));
1641 * This is neither a roff request nor a user-defined macro.
1642 * Let the standard macro set parsers handle it.
1648 /* Execute a roff request or a user defined macro. */
1650 assert(roffs
[t
].proc
);
1651 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1655 roff_endparse(struct roff
*r
)
1659 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1660 r
->last
->line
, r
->last
->col
,
1661 roffs
[r
->last
->tok
].name
);
1664 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1665 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, "EQ");
1670 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1671 r
->tbl
->line
, r
->tbl
->pos
, "TS");
1677 * Parse a roff node's type from the input buffer. This must be in the
1678 * form of ".foo xxx" in the usual way.
1681 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1690 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1694 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1696 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
1697 ? ROFF_USERDEF
: roffhash_find(mac
, maclen
);
1705 /* --- handling of request blocks ----------------------------------------- */
1708 roff_cblock(ROFF_ARGS
)
1712 * A block-close `..' should only be invoked as a child of an
1713 * ignore macro, otherwise raise a warning and just ignore it.
1716 if (r
->last
== NULL
) {
1717 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1722 switch (r
->last
->tok
) {
1724 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1729 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1736 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1741 if (buf
->buf
[pos
] != '\0')
1742 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
1743 ".. %s", buf
->buf
+ pos
);
1746 roffnode_cleanscope(r
);
1752 roffnode_cleanscope(struct roff
*r
)
1756 if (--r
->last
->endspan
!= 0)
1763 roff_ccond(struct roff
*r
, int ln
, int ppos
)
1766 if (NULL
== r
->last
) {
1767 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1772 switch (r
->last
->tok
) {
1780 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1785 if (r
->last
->endspan
> -1) {
1786 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1792 roffnode_cleanscope(r
);
1797 roff_block(ROFF_ARGS
)
1803 /* Ignore groff compatibility mode for now. */
1805 if (tok
== ROFF_de1
)
1807 else if (tok
== ROFF_dei1
)
1809 else if (tok
== ROFF_am1
)
1811 else if (tok
== ROFF_ami1
)
1814 /* Parse the macro name argument. */
1816 cp
= buf
->buf
+ pos
;
1817 if (tok
== ROFF_ig
) {
1822 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1823 iname
[namesz
] = '\0';
1826 /* Resolve the macro name argument if it is indirect. */
1828 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1829 if ((name
= roff_getstrn(r
, iname
, namesz
)) == NULL
) {
1830 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1831 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1832 "%.*s", (int)namesz
, iname
);
1835 namesz
= strlen(name
);
1839 if (namesz
== 0 && tok
!= ROFF_ig
) {
1840 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
,
1841 ln
, ppos
, roffs
[tok
].name
);
1845 roffnode_push(r
, tok
, name
, ln
, ppos
);
1848 * At the beginning of a `de' macro, clear the existing string
1849 * with the same name, if there is one. New content will be
1850 * appended from roff_block_text() in multiline mode.
1853 if (tok
== ROFF_de
|| tok
== ROFF_dei
)
1854 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
1859 /* Get the custom end marker. */
1862 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1864 /* Resolve the end marker if it is indirect. */
1866 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1867 if ((name
= roff_getstrn(r
, iname
, namesz
)) == NULL
) {
1868 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1869 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1870 "%.*s", (int)namesz
, iname
);
1873 namesz
= strlen(name
);
1878 r
->last
->end
= mandoc_strndup(name
, namesz
);
1881 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
1882 ln
, pos
, ".%s ... %s", roffs
[tok
].name
, cp
);
1888 roff_block_sub(ROFF_ARGS
)
1894 * First check whether a custom macro exists at this level. If
1895 * it does, then check against it. This is some of groff's
1896 * stranger behaviours. If we encountered a custom end-scope
1897 * tag and that tag also happens to be a "real" macro, then we
1898 * need to try interpreting it again as a real macro. If it's
1899 * not, then return ignore. Else continue.
1903 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
1904 if (buf
->buf
[i
] != r
->last
->end
[j
])
1907 if (r
->last
->end
[j
] == '\0' &&
1908 (buf
->buf
[i
] == '\0' ||
1909 buf
->buf
[i
] == ' ' ||
1910 buf
->buf
[i
] == '\t')) {
1912 roffnode_cleanscope(r
);
1914 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
1918 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
1926 * If we have no custom end-query or lookup failed, then try
1927 * pulling it out of the hashtable.
1930 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1932 if (t
!= ROFF_cblock
) {
1934 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
1938 assert(roffs
[t
].proc
);
1939 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1943 roff_block_text(ROFF_ARGS
)
1947 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
1953 roff_cond_sub(ROFF_ARGS
)
1960 roffnode_cleanscope(r
);
1961 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1964 * Fully handle known macros when they are structurally
1965 * required or when the conditional evaluated to true.
1968 if ((t
!= ROFF_MAX
) &&
1969 (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
)) {
1970 assert(roffs
[t
].proc
);
1971 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1975 * If `\}' occurs on a macro line without a preceding macro,
1976 * drop the line completely.
1979 ep
= buf
->buf
+ pos
;
1980 if (ep
[0] == '\\' && ep
[1] == '}')
1983 /* Always check for the closing delimiter `\}'. */
1985 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1986 if (*(++ep
) == '}') {
1988 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1993 return(rr
? ROFF_CONT
: ROFF_IGN
);
1997 roff_cond_text(ROFF_ARGS
)
2003 roffnode_cleanscope(r
);
2005 ep
= buf
->buf
+ pos
;
2006 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2007 if (*(++ep
) == '}') {
2009 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
2014 return(rr
? ROFF_CONT
: ROFF_IGN
);
2017 /* --- handling of numeric and conditional expressions -------------------- */
2020 * Parse a single signed integer number. Stop at the first non-digit.
2021 * If there is at least one digit, return success and advance the
2022 * parse point, else return failure and let the parse point unchanged.
2023 * Ignore overflows, treat them just like the C language.
2026 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
2028 int myres
, scaled
, n
, p
;
2035 if (n
|| v
[p
] == '+')
2038 if (flags
& ROFFNUM_WHITE
)
2039 while (isspace((unsigned char)v
[p
]))
2042 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
2043 *res
= 10 * *res
+ v
[p
] - '0';
2050 /* Each number may be followed by one optional scaling unit. */
2054 scaled
= *res
* 65536;
2057 scaled
= *res
* 240;
2060 scaled
= *res
* 240 / 2.54;
2073 scaled
= *res
* 10 / 3;
2079 scaled
= *res
* 6 / 25;
2086 if (flags
& ROFFNUM_SCALE
)
2094 * Evaluate a string comparison condition.
2095 * The first character is the delimiter.
2096 * Succeed if the string up to its second occurrence
2097 * matches the string up to its third occurence.
2098 * Advance the cursor after the third occurrence
2099 * or lacking that, to the end of the line.
2102 roff_evalstrcond(const char *v
, int *pos
)
2104 const char *s1
, *s2
, *s3
;
2108 s1
= v
+ *pos
; /* initial delimiter */
2109 s2
= s1
+ 1; /* for scanning the first string */
2110 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2112 if (NULL
== s3
) /* found no middle delimiter */
2115 while ('\0' != *++s3
) {
2116 if (*s2
!= *s3
) { /* mismatch */
2117 s3
= strchr(s3
, *s1
);
2120 if (*s3
== *s1
) { /* found the final delimiter */
2129 s3
= strchr(s2
, '\0');
2130 else if (*s3
!= '\0')
2137 * Evaluate an optionally negated single character, numerical,
2138 * or string condition.
2141 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
2145 int number
, savepos
, wanttrue
;
2147 if ('!' == v
[*pos
]) {
2173 cp
= name
= v
+ ++*pos
;
2174 sz
= roff_getname(r
, &cp
, ln
, *pos
);
2176 return((sz
&& roff_hasregn(r
, name
, sz
)) == wanttrue
);
2182 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2183 return((number
> 0) == wanttrue
);
2184 else if (*pos
== savepos
)
2185 return(roff_evalstrcond(v
, pos
) == wanttrue
);
2191 roff_line_ignore(ROFF_ARGS
)
2198 roff_insec(ROFF_ARGS
)
2201 mandoc_msg(MANDOCERR_REQ_INSEC
, r
->parse
,
2202 ln
, ppos
, roffs
[tok
].name
);
2207 roff_unsupp(ROFF_ARGS
)
2210 mandoc_msg(MANDOCERR_REQ_UNSUPP
, r
->parse
,
2211 ln
, ppos
, roffs
[tok
].name
);
2216 roff_cond(ROFF_ARGS
)
2219 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2222 * An `.el' has no conditional body: it will consume the value
2223 * of the current rstack entry set in prior `ie' calls or
2226 * If we're not an `el', however, then evaluate the conditional.
2229 r
->last
->rule
= tok
== ROFF_el
?
2230 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2231 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2234 * An if-else will put the NEGATION of the current evaluated
2235 * conditional into the stack of rules.
2238 if (tok
== ROFF_ie
) {
2239 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2241 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2242 r
->rstacksz
, sizeof(int));
2244 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2247 /* If the parent has false as its rule, then so do we. */
2249 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2254 * If there is nothing on the line after the conditional,
2255 * not even whitespace, use next-line scope.
2258 if (buf
->buf
[pos
] == '\0') {
2259 r
->last
->endspan
= 2;
2263 while (buf
->buf
[pos
] == ' ')
2266 /* An opening brace requests multiline scope. */
2268 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2269 r
->last
->endspan
= -1;
2271 while (buf
->buf
[pos
] == ' ')
2277 * Anything else following the conditional causes
2278 * single-line scope. Warn if the scope contains
2279 * nothing but trailing whitespace.
2282 if (buf
->buf
[pos
] == '\0')
2283 mandoc_msg(MANDOCERR_COND_EMPTY
, r
->parse
,
2284 ln
, ppos
, roffs
[tok
].name
);
2286 r
->last
->endspan
= 1;
2300 /* Ignore groff compatibility mode for now. */
2302 if (tok
== ROFF_ds1
)
2304 else if (tok
== ROFF_as1
)
2308 * The first word is the name of the string.
2309 * If it is empty or terminated by an escape sequence,
2310 * abort the `ds' request without defining anything.
2313 name
= string
= buf
->buf
+ pos
;
2317 namesz
= roff_getname(r
, &string
, ln
, pos
);
2318 if (name
[namesz
] == '\\')
2321 /* Read past the initial double-quote, if any. */
2325 /* The rest is the value. */
2326 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2332 * Parse a single operator, one or two characters long.
2333 * If the operator is recognized, return success and advance the
2334 * parse point, else return failure and let the parse point unchanged.
2337 roff_getop(const char *v
, int *pos
, char *res
)
2358 switch (v
[*pos
+ 1]) {
2376 switch (v
[*pos
+ 1]) {
2390 if ('=' == v
[*pos
+ 1])
2402 * Evaluate either a parenthesized numeric expression
2403 * or a single signed integer number.
2406 roff_evalpar(struct roff
*r
, int ln
,
2407 const char *v
, int *pos
, int *res
, int flags
)
2411 return(roff_getnum(v
, pos
, res
, flags
));
2414 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2418 * Omission of the closing parenthesis
2419 * is an error in validation mode,
2420 * but ignored in evaluation mode.
2425 else if (NULL
== res
)
2432 * Evaluate a complete numeric expression.
2433 * Proceed left to right, there is no concept of precedence.
2436 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2437 int *pos
, int *res
, int flags
)
2439 int mypos
, operand2
;
2447 if (flags
& ROFFNUM_WHITE
)
2448 while (isspace((unsigned char)v
[*pos
]))
2451 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2455 if (flags
& ROFFNUM_WHITE
)
2456 while (isspace((unsigned char)v
[*pos
]))
2459 if ( ! roff_getop(v
, pos
, &operator))
2462 if (flags
& ROFFNUM_WHITE
)
2463 while (isspace((unsigned char)v
[*pos
]))
2466 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2469 if (flags
& ROFFNUM_WHITE
)
2470 while (isspace((unsigned char)v
[*pos
]))
2487 if (operand2
== 0) {
2488 mandoc_msg(MANDOCERR_DIVZERO
,
2489 r
->parse
, ln
, *pos
, v
);
2496 if (operand2
== 0) {
2497 mandoc_msg(MANDOCERR_DIVZERO
,
2498 r
->parse
, ln
, *pos
, v
);
2505 *res
= *res
< operand2
;
2508 *res
= *res
> operand2
;
2511 *res
= *res
<= operand2
;
2514 *res
= *res
>= operand2
;
2517 *res
= *res
== operand2
;
2520 *res
= *res
!= operand2
;
2523 *res
= *res
&& operand2
;
2526 *res
= *res
|| operand2
;
2529 if (operand2
< *res
)
2533 if (operand2
> *res
)
2543 /* --- register management ------------------------------------------------ */
2546 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2548 struct roffreg
*reg
;
2550 /* Search for an existing register with the same name. */
2553 while (reg
&& strcmp(name
, reg
->key
.p
))
2557 /* Create a new register. */
2558 reg
= mandoc_malloc(sizeof(struct roffreg
));
2559 reg
->key
.p
= mandoc_strdup(name
);
2560 reg
->key
.sz
= strlen(name
);
2562 reg
->next
= r
->regtab
;
2568 else if ('-' == sign
)
2575 * Handle some predefined read-only number registers.
2576 * For now, return -1 if the requested register is not predefined;
2577 * in case a predefined read-only register having the value -1
2578 * were to turn up, another special value would have to be chosen.
2581 roff_getregro(const struct roff
*r
, const char *name
)
2585 case '$': /* Number of arguments of the last macro evaluated. */
2587 case 'A': /* ASCII approximation mode is always off. */
2589 case 'g': /* Groff compatibility mode is always on. */
2591 case 'H': /* Fixed horizontal resolution. */
2593 case 'j': /* Always adjust left margin only. */
2595 case 'T': /* Some output device is always defined. */
2597 case 'V': /* Fixed vertical resolution. */
2605 roff_getreg(const struct roff
*r
, const char *name
)
2607 struct roffreg
*reg
;
2610 if ('.' == name
[0] && '\0' != name
[1] && '\0' == name
[2]) {
2611 val
= roff_getregro(r
, name
+ 1);
2616 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2617 if (0 == strcmp(name
, reg
->key
.p
))
2624 roff_getregn(const struct roff
*r
, const char *name
, size_t len
)
2626 struct roffreg
*reg
;
2629 if ('.' == name
[0] && 2 == len
) {
2630 val
= roff_getregro(r
, name
+ 1);
2635 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2636 if (len
== reg
->key
.sz
&&
2637 0 == strncmp(name
, reg
->key
.p
, len
))
2644 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
2646 struct roffreg
*reg
;
2649 if ('.' == name
[0] && 2 == len
) {
2650 val
= roff_getregro(r
, name
+ 1);
2655 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2656 if (len
== reg
->key
.sz
&&
2657 0 == strncmp(name
, reg
->key
.p
, len
))
2664 roff_freereg(struct roffreg
*reg
)
2666 struct roffreg
*old_reg
;
2668 while (NULL
!= reg
) {
2684 key
= val
= buf
->buf
+ pos
;
2688 keysz
= roff_getname(r
, &val
, ln
, pos
);
2689 if (key
[keysz
] == '\\')
2694 if (sign
== '+' || sign
== '-')
2697 if (roff_evalnum(r
, ln
, val
, NULL
, &iv
, ROFFNUM_SCALE
))
2698 roff_setreg(r
, key
, iv
, sign
);
2706 struct roffreg
*reg
, **prev
;
2710 name
= cp
= buf
->buf
+ pos
;
2713 namesz
= roff_getname(r
, &cp
, ln
, pos
);
2714 name
[namesz
] = '\0';
2719 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
2731 /* --- handler functions for roff requests -------------------------------- */
2740 cp
= buf
->buf
+ pos
;
2741 while (*cp
!= '\0') {
2743 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
2744 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
2745 if (name
[namesz
] == '\\')
2756 /* Parse the number of lines. */
2758 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
2759 mandoc_msg(MANDOCERR_IT_NONUM
, r
->parse
,
2760 ln
, ppos
, buf
->buf
+ 1);
2764 while (isspace((unsigned char)buf
->buf
[pos
]))
2768 * Arm the input line trap.
2769 * Special-casing "an-trap" is an ugly workaround to cope
2770 * with DocBook stupidly fiddling with man(7) internals.
2774 roffit_macro
= mandoc_strdup(iv
!= 1 ||
2775 strcmp(buf
->buf
+ pos
, "an-trap") ?
2776 buf
->buf
+ pos
: "br");
2783 const char *const *cp
;
2785 if ((r
->options
& (MPARSE_MDOC
| MPARSE_QUICK
)) == 0)
2786 for (cp
= __mdoc_reserved
; *cp
; cp
++)
2787 roff_setstr(r
, *cp
, NULL
, 0);
2790 r
->format
= MPARSE_MDOC
;
2798 const char *const *cp
;
2800 if ((r
->options
& MPARSE_QUICK
) == 0)
2801 for (cp
= __man_reserved
; *cp
; cp
++)
2802 roff_setstr(r
, *cp
, NULL
, 0);
2805 r
->format
= MPARSE_MAN
;
2815 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2817 else if ( ! tbl_end(&r
->tbl
)) {
2819 buf
->buf
= mandoc_strdup(".sp");
2821 return(ROFF_REPARSE
);
2831 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2834 tbl_restart(ppos
, ln
, r
->tbl
);
2840 * Handle in-line equation delimiters.
2843 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
2846 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
2849 * Outside equations, look for an opening delimiter.
2850 * If we are inside an equation, we already know it is
2851 * in-line, or this function wouldn't have been called;
2852 * so look for a closing delimiter.
2855 cp1
= buf
->buf
+ pos
;
2856 cp2
= strchr(cp1
, r
->eqn
== NULL
?
2857 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
2862 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
2864 /* Handle preceding text, protecting whitespace. */
2866 if (*buf
->buf
!= '\0') {
2873 * Prepare replacing the delimiter with an equation macro
2874 * and drop leading white space from the equation.
2877 if (r
->eqn
== NULL
) {
2884 /* Handle following text, protecting whitespace. */
2892 /* Do the actual replacement. */
2894 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
2895 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
2899 /* Toggle the in-line state of the eqn subsystem. */
2901 r
->eqn_inline
= r
->eqn
== NULL
;
2902 return(ROFF_REPARSE
);
2910 assert(r
->eqn
== NULL
);
2911 e
= eqn_alloc(ppos
, ln
, r
->parse
);
2914 r
->last_eqn
->next
= e
;
2915 e
->delim
= r
->last_eqn
->delim
;
2916 e
->odelim
= r
->last_eqn
->odelim
;
2917 e
->cdelim
= r
->last_eqn
->cdelim
;
2919 r
->first_eqn
= r
->last_eqn
= e
;
2921 r
->eqn
= r
->last_eqn
= e
;
2923 if (buf
->buf
[pos
] != '\0')
2924 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
2925 ".EQ %s", buf
->buf
+ pos
);
2934 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
, ln
, ppos
, "EN");
2941 struct tbl_node
*tbl
;
2944 mandoc_msg(MANDOCERR_BLK_BROKEN
, r
->parse
,
2945 ln
, ppos
, "TS breaks TS");
2949 tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
2952 r
->last_tbl
->next
= tbl
;
2954 r
->first_tbl
= r
->last_tbl
= tbl
;
2956 r
->tbl
= r
->last_tbl
= tbl
;
2964 buf
->buf
[pos
- 1] = '\0';
2975 if (*p
== '\0' || (r
->control
= *p
++) == '.')
2979 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
2980 ln
, p
- buf
->buf
, "cc ... %s", p
);
2988 const char *p
, *first
, *second
;
2990 enum mandoc_esc esc
;
2995 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
, ln
, ppos
, "tr");
2999 while (*p
!= '\0') {
3003 if (*first
== '\\') {
3004 esc
= mandoc_escape(&p
, NULL
, NULL
);
3005 if (esc
== ESCAPE_ERROR
) {
3006 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3007 ln
, (int)(p
- buf
->buf
), first
);
3010 fsz
= (size_t)(p
- first
);
3014 if (*second
== '\\') {
3015 esc
= mandoc_escape(&p
, NULL
, NULL
);
3016 if (esc
== ESCAPE_ERROR
) {
3017 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3018 ln
, (int)(p
- buf
->buf
), second
);
3021 ssz
= (size_t)(p
- second
);
3022 } else if (*second
== '\0') {
3023 mandoc_vmsg(MANDOCERR_TR_ODD
, r
->parse
,
3024 ln
, first
- buf
->buf
, "tr %s", first
);
3030 roff_setstrn(&r
->xmbtab
, first
, fsz
,
3035 if (r
->xtab
== NULL
)
3036 r
->xtab
= mandoc_calloc(128,
3037 sizeof(struct roffstr
));
3039 free(r
->xtab
[(int)*first
].p
);
3040 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
3041 r
->xtab
[(int)*first
].sz
= ssz
;
3052 name
= buf
->buf
+ pos
;
3053 mandoc_vmsg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, "so %s", name
);
3056 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3057 * opening anything that's not in our cwd or anything beneath
3058 * it. Thus, explicitly disallow traversing up the file-system
3059 * or using absolute paths.
3062 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
3063 mandoc_vmsg(MANDOCERR_SO_PATH
, r
->parse
, ln
, ppos
,
3065 buf
->sz
= mandoc_asprintf(&cp
,
3066 ".sp\nSee the file %s.\n.sp", name
) + 1;
3070 return(ROFF_REPARSE
);
3077 /* --- user defined strings and macros ------------------------------------ */
3080 roff_userdef(ROFF_ARGS
)
3082 const char *arg
[9], *ap
;
3088 * Collect pointers to macro argument strings
3089 * and NUL-terminate them.
3093 cp
= buf
->buf
+ pos
;
3094 for (i
= 0; i
< 9; i
++) {
3098 arg
[i
] = mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
3104 * Expand macro arguments.
3107 buf
->sz
= strlen(r
->current_string
) + 1;
3108 n1
= cp
= mandoc_malloc(buf
->sz
);
3109 memcpy(n1
, r
->current_string
, buf
->sz
);
3110 while (*cp
!= '\0') {
3112 /* Scan ahead for the next argument invocation. */
3118 if (*cp
== '*') { /* \\$* inserts all arguments */
3121 } else { /* \\$1 .. \\$9 insert one argument */
3122 ib
= ie
= *cp
- '1';
3123 if (ib
< 0 || ib
> 8)
3129 * Determine the size of the expanded argument,
3130 * taking escaping of quotes into account.
3133 asz
= ie
> ib
? ie
- ib
: 0; /* for blanks */
3134 for (i
= ib
; i
<= ie
; i
++) {
3135 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3144 * Determine the size of the rest of the
3145 * unexpanded macro, including the NUL.
3148 rsz
= buf
->sz
- (cp
- n1
) - 3;
3151 * When shrinking, move before
3152 * releasing the storage.
3156 memmove(cp
+ asz
, cp
+ 3, rsz
);
3159 * Resize the storage for the macro
3160 * and readjust the parse pointer.
3164 n2
= mandoc_realloc(n1
, buf
->sz
);
3165 cp
= n2
+ (cp
- n1
);
3169 * When growing, make room
3170 * for the expanded argument.
3174 memmove(cp
+ asz
, cp
+ 3, rsz
);
3177 /* Copy the expanded argument, escaping quotes. */
3180 for (i
= ib
; i
<= ie
; i
++) {
3181 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3183 memcpy(n2
, "\\(dq", 4);
3194 * Replace the macro invocation
3195 * by the expanded macro.
3202 return(buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
3203 ROFF_REPARSE
: ROFF_APPEND
);
3207 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
3216 /* Read until end of name and terminate it with NUL. */
3217 for (cp
= name
; 1; cp
++) {
3218 if ('\0' == *cp
|| ' ' == *cp
) {
3225 if ('{' == cp
[1] || '}' == cp
[1])
3230 mandoc_vmsg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
,
3231 "%.*s", (int)(cp
- name
+ 1), name
);
3232 mandoc_escape((const char **)&cp
, NULL
, NULL
);
3236 /* Read past spaces. */
3245 * Store *string into the user-defined string called *name.
3246 * To clear an existing entry, call with (*r, *name, NULL, 0).
3247 * append == 0: replace mode
3248 * append == 1: single-line append mode
3249 * append == 2: multiline append mode, append '\n' after each call
3252 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
3256 roff_setstrn(&r
->strtab
, name
, strlen(name
), string
,
3257 string
? strlen(string
) : 0, append
);
3261 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
3262 const char *string
, size_t stringsz
, int append
)
3267 size_t oldch
, newch
;
3269 /* Search for an existing string with the same name. */
3272 while (n
&& (namesz
!= n
->key
.sz
||
3273 strncmp(n
->key
.p
, name
, namesz
)))
3277 /* Create a new string table entry. */
3278 n
= mandoc_malloc(sizeof(struct roffkv
));
3279 n
->key
.p
= mandoc_strndup(name
, namesz
);
3285 } else if (0 == append
) {
3295 * One additional byte for the '\n' in multiline mode,
3296 * and one for the terminating '\0'.
3298 newch
= stringsz
+ (1 < append
? 2u : 1u);
3300 if (NULL
== n
->val
.p
) {
3301 n
->val
.p
= mandoc_malloc(newch
);
3306 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
3309 /* Skip existing content in the destination buffer. */
3310 c
= n
->val
.p
+ (int)oldch
;
3312 /* Append new content to the destination buffer. */
3314 while (i
< (int)stringsz
) {
3316 * Rudimentary roff copy mode:
3317 * Handle escaped backslashes.
3319 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
3324 /* Append terminating bytes. */
3329 n
->val
.sz
= (int)(c
- n
->val
.p
);
3333 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
3335 const struct roffkv
*n
;
3338 for (n
= r
->strtab
; n
; n
= n
->next
)
3339 if (0 == strncmp(name
, n
->key
.p
, len
) &&
3340 '\0' == n
->key
.p
[(int)len
])
3343 for (i
= 0; i
< PREDEFS_MAX
; i
++)
3344 if (0 == strncmp(name
, predefs
[i
].name
, len
) &&
3345 '\0' == predefs
[i
].name
[(int)len
])
3346 return(predefs
[i
].str
);
3352 roff_freestr(struct roffkv
*r
)
3354 struct roffkv
*n
, *nn
;
3356 for (n
= r
; n
; n
= nn
) {
3364 /* --- accessors and utility functions ------------------------------------ */
3366 const struct tbl_span
*
3367 roff_span(const struct roff
*r
)
3370 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
3374 roff_eqn(const struct roff
*r
)
3377 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);
3381 * Duplicate an input string, making the appropriate character
3382 * conversations (as stipulated by `tr') along the way.
3383 * Returns a heap-allocated string with all the replacements made.
3386 roff_strdup(const struct roff
*r
, const char *p
)
3388 const struct roffkv
*cp
;
3392 enum mandoc_esc esc
;
3394 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
3395 return(mandoc_strdup(p
));
3396 else if ('\0' == *p
)
3397 return(mandoc_strdup(""));
3400 * Step through each character looking for term matches
3401 * (remember that a `tr' can be invoked with an escape, which is
3402 * a glyph but the escape is multi-character).
3403 * We only do this if the character hash has been initialised
3404 * and the string is >0 length.
3410 while ('\0' != *p
) {
3411 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(int)*p
].p
) {
3412 sz
= r
->xtab
[(int)*p
].sz
;
3413 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3414 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
3418 } else if ('\\' != *p
) {
3419 res
= mandoc_realloc(res
, ssz
+ 2);
3424 /* Search for term matches. */
3425 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
3426 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
3431 * A match has been found.
3432 * Append the match to the array and move
3433 * forward by its keysize.
3435 res
= mandoc_realloc(res
,
3436 ssz
+ cp
->val
.sz
+ 1);
3437 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
3439 p
+= (int)cp
->key
.sz
;
3444 * Handle escapes carefully: we need to copy
3445 * over just the escape itself, or else we might
3446 * do replacements within the escape itself.
3447 * Make sure to pass along the bogus string.
3450 esc
= mandoc_escape(&p
, NULL
, NULL
);
3451 if (ESCAPE_ERROR
== esc
) {
3453 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3454 memcpy(res
+ ssz
, pp
, sz
);
3458 * We bail out on bad escapes.
3459 * No need to warn: we already did so when
3460 * roff_res() was called.
3463 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3464 memcpy(res
+ ssz
, pp
, sz
);
3468 res
[(int)ssz
] = '\0';
3473 roff_getformat(const struct roff
*r
)
3480 * Find out whether a line is a macro line or not.
3481 * If it is, adjust the current position and return one; if it isn't,
3482 * return zero and don't change the current position.
3483 * If the control character has been set with `.cc', then let that grain
3485 * This is slighly contrary to groff, where using the non-breaking
3486 * control character when `cc' has been invoked will cause the
3487 * non-breaking macro contents to be printed verbatim.
3490 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
3496 if (0 != r
->control
&& cp
[pos
] == r
->control
)
3498 else if (0 != r
->control
)
3500 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
3502 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
3507 while (' ' == cp
[pos
] || '\t' == cp
[pos
])