]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.272 2015/06/27 13:29:14 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 char control
; /* control character */
342 enum rofft tok
; /* type of node */
343 struct roffnode
*parent
; /* up one in stack */
344 int line
; /* parse line */
345 int col
; /* parse col */
346 char *name
; /* node name, e.g. macro name */
347 char *end
; /* end-rules: custom token */
348 int endspan
; /* end-rules: next-line or infty */
349 int rule
; /* current evaluation rule */
352 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
353 enum rofft tok, /* tok of macro */ \
354 struct buf *buf, /* input buffer */ \
355 int ln, /* parse line */ \
356 int ppos, /* original pos in buffer */ \
357 int pos, /* current pos in buffer */ \
358 int *offs /* reset offset of buffer data */
360 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
363 const char *name
; /* macro name */
364 roffproc proc
; /* process new macro */
365 roffproc text
; /* process as child text of macro */
366 roffproc sub
; /* process as child of macro */
368 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
369 struct roffmac
*next
;
373 const char *name
; /* predefined input name */
374 const char *str
; /* replacement symbol */
377 #define PREDEF(__name, __str) \
378 { (__name), (__str) },
380 /* --- function prototypes ------------------------------------------------ */
382 static enum rofft
roffhash_find(const char *, size_t);
383 static void roffhash_init(void);
384 static void roffnode_cleanscope(struct roff
*);
385 static void roffnode_pop(struct roff
*);
386 static void roffnode_push(struct roff
*, enum rofft
,
387 const char *, int, int);
388 static enum rofferr
roff_block(ROFF_ARGS
);
389 static enum rofferr
roff_block_text(ROFF_ARGS
);
390 static enum rofferr
roff_block_sub(ROFF_ARGS
);
391 static enum rofferr
roff_brp(ROFF_ARGS
);
392 static enum rofferr
roff_cblock(ROFF_ARGS
);
393 static enum rofferr
roff_cc(ROFF_ARGS
);
394 static void roff_ccond(struct roff
*, int, int);
395 static enum rofferr
roff_cond(ROFF_ARGS
);
396 static enum rofferr
roff_cond_text(ROFF_ARGS
);
397 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
398 static enum rofferr
roff_ds(ROFF_ARGS
);
399 static enum rofferr
roff_eqndelim(struct roff
*, struct buf
*, int);
400 static int roff_evalcond(struct roff
*r
, int, char *, int *);
401 static int roff_evalnum(struct roff
*, int,
402 const char *, int *, int *, int);
403 static int roff_evalpar(struct roff
*, int,
404 const char *, int *, int *, int);
405 static int roff_evalstrcond(const char *, int *);
406 static void roff_free1(struct roff
*);
407 static void roff_freereg(struct roffreg
*);
408 static void roff_freestr(struct roffkv
*);
409 static size_t roff_getname(struct roff
*, char **, int, int);
410 static int roff_getnum(const char *, int *, int *, int);
411 static int roff_getop(const char *, int *, char *);
412 static int roff_getregn(const struct roff
*,
413 const char *, size_t);
414 static int roff_getregro(const char *name
);
415 static const char *roff_getstrn(const struct roff
*,
416 const char *, size_t);
417 static int roff_hasregn(const struct roff
*,
418 const char *, size_t);
419 static enum rofferr
roff_insec(ROFF_ARGS
);
420 static enum rofferr
roff_it(ROFF_ARGS
);
421 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
422 static void roff_man_alloc1(struct roff_man
*);
423 static void roff_man_free1(struct roff_man
*);
424 static enum rofferr
roff_nr(ROFF_ARGS
);
425 static enum rofft
roff_parse(struct roff
*, char *, int *,
427 static enum rofferr
roff_parsetext(struct buf
*, int, int *);
428 static enum rofferr
roff_res(struct roff
*, struct buf
*, int, int);
429 static enum rofferr
roff_rm(ROFF_ARGS
);
430 static enum rofferr
roff_rr(ROFF_ARGS
);
431 static void roff_setstr(struct roff
*,
432 const char *, const char *, int);
433 static void roff_setstrn(struct roffkv
**, const char *,
434 size_t, const char *, size_t, int);
435 static enum rofferr
roff_so(ROFF_ARGS
);
436 static enum rofferr
roff_tr(ROFF_ARGS
);
437 static enum rofferr
roff_Dd(ROFF_ARGS
);
438 static enum rofferr
roff_TH(ROFF_ARGS
);
439 static enum rofferr
roff_TE(ROFF_ARGS
);
440 static enum rofferr
roff_TS(ROFF_ARGS
);
441 static enum rofferr
roff_EQ(ROFF_ARGS
);
442 static enum rofferr
roff_EN(ROFF_ARGS
);
443 static enum rofferr
roff_T_(ROFF_ARGS
);
444 static enum rofferr
roff_unsupp(ROFF_ARGS
);
445 static enum rofferr
roff_userdef(ROFF_ARGS
);
447 /* --- constant data ------------------------------------------------------ */
449 /* See roffhash_find() */
453 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
455 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
456 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
458 static struct roffmac
*hash
[HASHWIDTH
];
460 static struct roffmac roffs
[ROFF_MAX
] = {
461 { "ab", roff_unsupp
, NULL
, NULL
, 0, NULL
},
462 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
463 { "af", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
464 { "aln", roff_unsupp
, NULL
, NULL
, 0, NULL
},
465 { "als", roff_unsupp
, NULL
, NULL
, 0, NULL
},
466 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
467 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
468 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
469 { "ami1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
470 { "as", roff_ds
, NULL
, NULL
, 0, NULL
},
471 { "as1", roff_ds
, NULL
, NULL
, 0, NULL
},
472 { "asciify", roff_unsupp
, NULL
, NULL
, 0, NULL
},
473 { "backtrace", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
474 { "bd", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
475 { "bleedat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
476 { "blm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
477 { "box", roff_unsupp
, NULL
, NULL
, 0, NULL
},
478 { "boxa", roff_unsupp
, NULL
, NULL
, 0, NULL
},
479 { "bp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
480 { "BP", roff_unsupp
, NULL
, NULL
, 0, NULL
},
481 { "break", roff_unsupp
, NULL
, NULL
, 0, NULL
},
482 { "breakchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
483 { "brnl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
484 { "brp", roff_brp
, NULL
, NULL
, 0, NULL
},
485 { "brpnl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
486 { "c2", roff_unsupp
, NULL
, NULL
, 0, NULL
},
487 { "cc", roff_cc
, NULL
, NULL
, 0, NULL
},
488 { "ce", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
489 { "cf", roff_insec
, NULL
, NULL
, 0, NULL
},
490 { "cflags", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
491 { "ch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
492 { "char", roff_unsupp
, NULL
, NULL
, 0, NULL
},
493 { "chop", roff_unsupp
, NULL
, NULL
, 0, NULL
},
494 { "class", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
495 { "close", roff_insec
, NULL
, NULL
, 0, NULL
},
496 { "CL", roff_unsupp
, NULL
, NULL
, 0, NULL
},
497 { "color", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
498 { "composite", roff_unsupp
, NULL
, NULL
, 0, NULL
},
499 { "continue", roff_unsupp
, NULL
, NULL
, 0, NULL
},
500 { "cp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
501 { "cropat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
502 { "cs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
503 { "cu", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
504 { "da", roff_unsupp
, NULL
, NULL
, 0, NULL
},
505 { "dch", roff_unsupp
, NULL
, NULL
, 0, NULL
},
506 { "Dd", roff_Dd
, NULL
, NULL
, 0, NULL
},
507 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
508 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
509 { "defcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
510 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
511 { "dei1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
512 { "device", roff_unsupp
, NULL
, NULL
, 0, NULL
},
513 { "devicem", roff_unsupp
, NULL
, NULL
, 0, NULL
},
514 { "di", roff_unsupp
, NULL
, NULL
, 0, NULL
},
515 { "do", roff_unsupp
, NULL
, NULL
, 0, NULL
},
516 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
517 { "ds1", roff_ds
, NULL
, NULL
, 0, NULL
},
518 { "dwh", roff_unsupp
, NULL
, NULL
, 0, NULL
},
519 { "dt", roff_unsupp
, NULL
, NULL
, 0, NULL
},
520 { "ec", roff_unsupp
, NULL
, NULL
, 0, NULL
},
521 { "ecr", roff_unsupp
, NULL
, NULL
, 0, NULL
},
522 { "ecs", roff_unsupp
, NULL
, NULL
, 0, NULL
},
523 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
524 { "em", roff_unsupp
, NULL
, NULL
, 0, NULL
},
525 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
526 { "eo", roff_unsupp
, NULL
, NULL
, 0, NULL
},
527 { "EP", roff_unsupp
, NULL
, NULL
, 0, NULL
},
528 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
529 { "errprint", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
530 { "ev", roff_unsupp
, NULL
, NULL
, 0, NULL
},
531 { "evc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
532 { "ex", roff_unsupp
, NULL
, NULL
, 0, NULL
},
533 { "fallback", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
534 { "fam", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
535 { "fc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
536 { "fchar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
537 { "fcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
538 { "fdeferlig", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
539 { "feature", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
540 { "fkern", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
541 { "fl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
542 { "flig", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
543 { "fp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
544 { "fps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
545 { "fschar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
546 { "fspacewidth", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
547 { "fspecial", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
548 { "ftr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
549 { "fzoom", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
550 { "gcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
551 { "hc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
552 { "hcode", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
553 { "hidechar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
554 { "hla", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
555 { "hlm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
556 { "hpf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
557 { "hpfa", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
558 { "hpfcode", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
559 { "hw", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
560 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
561 { "hylang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
562 { "hylen", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
563 { "hym", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
564 { "hypp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
565 { "hys", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
566 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
567 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
568 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
569 { "index", roff_unsupp
, NULL
, NULL
, 0, NULL
},
570 { "it", roff_it
, NULL
, NULL
, 0, NULL
},
571 { "itc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
572 { "IX", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
573 { "kern", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
574 { "kernafter", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
575 { "kernbefore", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
576 { "kernpair", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
577 { "lc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
578 { "lc_ctype", roff_unsupp
, NULL
, NULL
, 0, NULL
},
579 { "lds", roff_unsupp
, NULL
, NULL
, 0, NULL
},
580 { "length", roff_unsupp
, NULL
, NULL
, 0, NULL
},
581 { "letadj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
582 { "lf", roff_insec
, NULL
, NULL
, 0, NULL
},
583 { "lg", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
584 { "lhang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
585 { "linetabs", roff_unsupp
, NULL
, NULL
, 0, NULL
},
586 { "lnr", roff_unsupp
, NULL
, NULL
, 0, NULL
},
587 { "lnrf", roff_unsupp
, NULL
, NULL
, 0, NULL
},
588 { "lpfx", roff_unsupp
, NULL
, NULL
, 0, NULL
},
589 { "ls", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
590 { "lsm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
591 { "lt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
592 { "mc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
593 { "mediasize", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
594 { "minss", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
595 { "mk", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
596 { "mso", roff_insec
, NULL
, NULL
, 0, NULL
},
597 { "na", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
598 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
599 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
600 { "nhychar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
601 { "nm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
602 { "nn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
603 { "nop", roff_unsupp
, NULL
, NULL
, 0, NULL
},
604 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
605 { "nrf", roff_unsupp
, NULL
, NULL
, 0, NULL
},
606 { "nroff", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
607 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
608 { "nx", roff_insec
, NULL
, NULL
, 0, NULL
},
609 { "open", roff_insec
, NULL
, NULL
, 0, NULL
},
610 { "opena", roff_insec
, NULL
, NULL
, 0, NULL
},
611 { "os", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
612 { "output", roff_unsupp
, NULL
, NULL
, 0, NULL
},
613 { "padj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
614 { "papersize", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
615 { "pc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
616 { "pev", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
617 { "pi", roff_insec
, NULL
, NULL
, 0, NULL
},
618 { "PI", roff_unsupp
, NULL
, NULL
, 0, NULL
},
619 { "pl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
620 { "pm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
621 { "pn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
622 { "pnr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
623 { "po", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
624 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
625 { "psbb", roff_unsupp
, NULL
, NULL
, 0, NULL
},
626 { "pshape", roff_unsupp
, NULL
, NULL
, 0, NULL
},
627 { "pso", roff_insec
, NULL
, NULL
, 0, NULL
},
628 { "ptr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
629 { "pvs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
630 { "rchar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
631 { "rd", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
632 { "recursionlimit", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
633 { "return", roff_unsupp
, NULL
, NULL
, 0, NULL
},
634 { "rfschar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
635 { "rhang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
636 { "rj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
637 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
638 { "rn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
639 { "rnn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
640 { "rr", roff_rr
, NULL
, NULL
, 0, NULL
},
641 { "rs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
642 { "rt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
643 { "schar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
644 { "sentchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
645 { "shc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
646 { "shift", roff_unsupp
, NULL
, NULL
, 0, NULL
},
647 { "sizes", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
648 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
649 { "spacewidth", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
650 { "special", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
651 { "spreadwarn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
652 { "ss", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
653 { "sty", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
654 { "substring", roff_unsupp
, NULL
, NULL
, 0, NULL
},
655 { "sv", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
656 { "sy", roff_insec
, NULL
, NULL
, 0, NULL
},
657 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
658 { "ta", roff_unsupp
, NULL
, NULL
, 0, NULL
},
659 { "tc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
660 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
661 { "TH", roff_TH
, NULL
, NULL
, 0, NULL
},
662 { "ti", roff_unsupp
, NULL
, NULL
, 0, NULL
},
663 { "tkf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
664 { "tl", roff_unsupp
, NULL
, NULL
, 0, NULL
},
665 { "tm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
666 { "tm1", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
667 { "tmc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
668 { "tr", roff_tr
, NULL
, NULL
, 0, NULL
},
669 { "track", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
670 { "transchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
671 { "trf", roff_insec
, NULL
, NULL
, 0, NULL
},
672 { "trimat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
673 { "trin", roff_unsupp
, NULL
, NULL
, 0, NULL
},
674 { "trnt", roff_unsupp
, NULL
, NULL
, 0, NULL
},
675 { "troff", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
676 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
677 { "uf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
678 { "ul", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
679 { "unformat", roff_unsupp
, NULL
, NULL
, 0, NULL
},
680 { "unwatch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
681 { "unwatchn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
682 { "vpt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
683 { "vs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
684 { "warn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
685 { "warnscale", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
686 { "watch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
687 { "watchlength", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
688 { "watchn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
689 { "wh", roff_unsupp
, NULL
, NULL
, 0, NULL
},
690 { "while", roff_unsupp
, NULL
, NULL
, 0, NULL
},
691 { "write", roff_insec
, NULL
, NULL
, 0, NULL
},
692 { "writec", roff_insec
, NULL
, NULL
, 0, NULL
},
693 { "writem", roff_insec
, NULL
, NULL
, 0, NULL
},
694 { "xflag", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
695 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
696 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
699 /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
700 const char *const __mdoc_reserved
[] = {
701 "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
702 "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
703 "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
704 "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
705 "Dt", "Dv", "Dx", "D1",
706 "Ec", "Ed", "Ef", "Ek", "El", "Em",
707 "En", "Eo", "Er", "Es", "Ev", "Ex",
708 "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
709 "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
710 "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
711 "Oc", "Oo", "Op", "Os", "Ot", "Ox",
712 "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
713 "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
714 "Sc", "Sh", "Sm", "So", "Sq",
715 "Ss", "St", "Sx", "Sy",
716 "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
717 "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
718 "%P", "%Q", "%R", "%T", "%U", "%V",
722 /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
723 const char *const __man_reserved
[] = {
724 "AT", "B", "BI", "BR", "DT",
725 "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
726 "LP", "OP", "P", "PD", "PP",
727 "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
728 "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
732 /* Array of injected predefined strings. */
733 #define PREDEFS_MAX 38
734 static const struct predef predefs
[PREDEFS_MAX
] = {
735 #include "predefs.in"
738 /* See roffhash_find() */
739 #define ROFF_HASH(p) (p[0] - ASCII_LO)
741 static int roffit_lines
; /* number of lines to delay */
742 static char *roffit_macro
; /* nil-terminated macro line */
745 /* --- request table ------------------------------------------------------ */
753 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
754 assert(roffs
[i
].name
[0] >= ASCII_LO
);
755 assert(roffs
[i
].name
[0] <= ASCII_HI
);
757 buc
= ROFF_HASH(roffs
[i
].name
);
759 if (NULL
!= (n
= hash
[buc
])) {
760 for ( ; n
->next
; n
= n
->next
)
764 hash
[buc
] = &roffs
[i
];
769 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
770 * the nil-terminated string name could be found.
773 roffhash_find(const char *p
, size_t s
)
779 * libroff has an extremely simple hashtable, for the time
780 * being, which simply keys on the first character, which must
781 * be printable, then walks a chain. It works well enough until
785 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
790 if (NULL
== (n
= hash
[buc
]))
792 for ( ; n
; n
= n
->next
)
793 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
794 return((enum rofft
)(n
- roffs
));
799 /* --- stack of request blocks -------------------------------------------- */
802 * Pop the current node off of the stack of roff instructions currently
806 roffnode_pop(struct roff
*r
)
813 r
->last
= r
->last
->parent
;
820 * Push a roff node onto the instruction stack. This must later be
821 * removed with roffnode_pop().
824 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
829 p
= mandoc_calloc(1, sizeof(struct roffnode
));
832 p
->name
= mandoc_strdup(name
);
836 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
841 /* --- roff parser state data management ---------------------------------- */
844 roff_free1(struct roff
*r
)
846 struct tbl_node
*tbl
;
850 while (NULL
!= (tbl
= r
->first_tbl
)) {
851 r
->first_tbl
= tbl
->next
;
854 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
856 while (NULL
!= (e
= r
->first_eqn
)) {
857 r
->first_eqn
= e
->next
;
860 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
870 roff_freereg(r
->regtab
);
873 roff_freestr(r
->strtab
);
874 roff_freestr(r
->xmbtab
);
875 r
->strtab
= r
->xmbtab
= NULL
;
878 for (i
= 0; i
< 128; i
++)
885 roff_reset(struct roff
*r
)
889 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
894 roff_free(struct roff
*r
)
902 roff_alloc(struct mparse
*parse
, const struct mchars
*mchars
, int options
)
906 r
= mandoc_calloc(1, sizeof(struct roff
));
909 r
->options
= options
;
910 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
918 /* --- syntax tree state data management ---------------------------------- */
921 roff_man_free1(struct roff_man
*man
)
924 if (man
->first
!= NULL
)
925 roff_node_delete(man
, man
->first
);
926 free(man
->meta
.msec
);
929 free(man
->meta
.arch
);
930 free(man
->meta
.title
);
931 free(man
->meta
.name
);
932 free(man
->meta
.date
);
936 roff_man_alloc1(struct roff_man
*man
)
939 memset(&man
->meta
, 0, sizeof(man
->meta
));
940 man
->first
= mandoc_calloc(1, sizeof(*man
->first
));
941 man
->first
->type
= ROFFT_ROOT
;
942 man
->last
= man
->first
;
945 man
->macroset
= MACROSET_NONE
;
946 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
947 man
->next
= ROFF_NEXT_CHILD
;
951 roff_man_reset(struct roff_man
*man
)
955 roff_man_alloc1(man
);
959 roff_man_free(struct roff_man
*man
)
967 roff_man_alloc(struct roff
*roff
, struct mparse
*parse
,
968 const char *defos
, int quick
)
970 struct roff_man
*man
;
972 man
= mandoc_calloc(1, sizeof(*man
));
977 roff_man_alloc1(man
);
981 /* --- syntax tree handling ----------------------------------------------- */
984 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
985 enum roff_type type
, int tok
)
989 n
= mandoc_calloc(1, sizeof(*n
));
994 n
->sec
= man
->lastsec
;
996 if (man
->flags
& MDOC_SYNOPSIS
)
997 n
->flags
|= MDOC_SYNPRETTY
;
999 n
->flags
&= ~MDOC_SYNPRETTY
;
1000 if (man
->flags
& MDOC_NEWLINE
)
1001 n
->flags
|= MDOC_LINE
;
1002 man
->flags
&= ~MDOC_NEWLINE
;
1008 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
1011 switch (man
->next
) {
1012 case ROFF_NEXT_SIBLING
:
1013 man
->last
->next
= n
;
1014 n
->prev
= man
->last
;
1015 n
->parent
= man
->last
->parent
;
1017 case ROFF_NEXT_CHILD
:
1018 man
->last
->child
= n
;
1019 n
->parent
= man
->last
;
1025 n
->parent
->nchild
++;
1026 n
->parent
->last
= n
;
1029 * Copy over the normalised-data pointer of our parent. Not
1030 * everybody has one, but copying a null pointer is fine.
1035 if (n
->end
!= ENDBODY_NOT
)
1041 n
->norm
= n
->parent
->norm
;
1047 if (man
->macroset
== MACROSET_MDOC
)
1048 mdoc_valid_pre(man
, n
);
1052 assert(n
->parent
->type
== ROFFT_BLOCK
);
1053 n
->parent
->head
= n
;
1058 assert(n
->parent
->type
== ROFFT_BLOCK
);
1059 n
->parent
->body
= n
;
1062 assert(n
->parent
->type
== ROFFT_BLOCK
);
1063 n
->parent
->tail
= n
;
1072 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
1074 struct roff_node
*n
;
1076 n
= roff_node_alloc(man
, line
, pos
, ROFFT_TEXT
, TOKEN_NONE
);
1077 n
->string
= roff_strdup(man
->roff
, word
);
1078 roff_node_append(man
, n
);
1079 if (man
->macroset
== MACROSET_MDOC
)
1080 mdoc_valid_post(man
);
1082 man_valid_post(man
);
1083 man
->next
= ROFF_NEXT_SIBLING
;
1087 roff_word_append(struct roff_man
*man
, const char *word
)
1089 struct roff_node
*n
;
1090 char *addstr
, *newstr
;
1093 addstr
= roff_strdup(man
->roff
, word
);
1094 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
1098 man
->next
= ROFF_NEXT_SIBLING
;
1102 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1104 struct roff_node
*n
;
1106 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
1107 roff_node_append(man
, n
);
1108 man
->next
= ROFF_NEXT_CHILD
;
1112 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1114 struct roff_node
*n
;
1116 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
1117 roff_node_append(man
, n
);
1118 man
->next
= ROFF_NEXT_CHILD
;
1123 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1125 struct roff_node
*n
;
1127 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
1128 roff_node_append(man
, n
);
1129 man
->next
= ROFF_NEXT_CHILD
;
1134 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1136 struct roff_node
*n
;
1138 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
1139 roff_node_append(man
, n
);
1140 man
->next
= ROFF_NEXT_CHILD
;
1145 roff_addeqn(struct roff_man
*man
, const struct eqn
*eqn
)
1147 struct roff_node
*n
;
1149 n
= roff_node_alloc(man
, eqn
->ln
, eqn
->pos
, ROFFT_EQN
, TOKEN_NONE
);
1151 if (eqn
->ln
> man
->last
->line
)
1152 n
->flags
|= MDOC_LINE
;
1153 roff_node_append(man
, n
);
1154 man
->next
= ROFF_NEXT_SIBLING
;
1158 roff_addtbl(struct roff_man
*man
, const struct tbl_span
*tbl
)
1160 struct roff_node
*n
;
1162 if (man
->macroset
== MACROSET_MAN
)
1163 man_breakscope(man
, TOKEN_NONE
);
1164 n
= roff_node_alloc(man
, tbl
->line
, 0, ROFFT_TBL
, TOKEN_NONE
);
1166 roff_node_append(man
, n
);
1167 if (man
->macroset
== MACROSET_MDOC
)
1168 mdoc_valid_post(man
);
1170 man_valid_post(man
);
1171 man
->next
= ROFF_NEXT_SIBLING
;
1175 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1178 /* Adjust siblings. */
1181 n
->prev
->next
= n
->next
;
1183 n
->next
->prev
= n
->prev
;
1185 /* Adjust parent. */
1187 if (n
->parent
!= NULL
) {
1188 n
->parent
->nchild
--;
1189 if (n
->parent
->child
== n
)
1190 n
->parent
->child
= n
->next
;
1191 if (n
->parent
->last
== n
)
1192 n
->parent
->last
= n
->prev
;
1195 /* Adjust parse point. */
1199 if (man
->last
== n
) {
1200 if (n
->prev
== NULL
) {
1201 man
->last
= n
->parent
;
1202 man
->next
= ROFF_NEXT_CHILD
;
1204 man
->last
= n
->prev
;
1205 man
->next
= ROFF_NEXT_SIBLING
;
1208 if (man
->first
== n
)
1213 roff_node_free(struct roff_node
*n
)
1216 if (n
->args
!= NULL
)
1217 mdoc_argv_free(n
->args
);
1218 if (n
->type
== ROFFT_BLOCK
|| n
->type
== ROFFT_ELEM
)
1225 roff_node_delete(struct roff_man
*man
, struct roff_node
*n
)
1228 while (n
->child
!= NULL
)
1229 roff_node_delete(man
, n
->child
);
1230 assert(n
->nchild
== 0);
1231 roff_node_unlink(man
, n
);
1236 deroff(char **dest
, const struct roff_node
*n
)
1241 if (n
->type
!= ROFFT_TEXT
) {
1242 for (n
= n
->child
; n
!= NULL
; n
= n
->next
)
1247 /* Skip leading whitespace and escape sequences. */
1250 while (*cp
!= '\0') {
1253 mandoc_escape((const char **)&cp
, NULL
, NULL
);
1254 } else if (isspace((unsigned char)*cp
))
1260 /* Skip trailing whitespace. */
1262 for (sz
= strlen(cp
); sz
; sz
--)
1263 if ( ! isspace((unsigned char)cp
[sz
-1]))
1266 /* Skip empty strings. */
1271 if (*dest
== NULL
) {
1272 *dest
= mandoc_strndup(cp
, sz
);
1276 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);
1281 /* --- main functions of the roff parser ---------------------------------- */
1284 * In the current line, expand escape sequences that tend to get
1285 * used in numerical expressions and conditional requests.
1286 * Also check the syntax of the remaining escape sequences.
1289 roff_res(struct roff
*r
, struct buf
*buf
, int ln
, int pos
)
1291 char ubuf
[24]; /* buffer to print the number */
1292 const char *start
; /* start of the string to process */
1293 char *stesc
; /* start of an escape sequence ('\\') */
1294 const char *stnam
; /* start of the name, after "[(*" */
1295 const char *cp
; /* end of the name, e.g. before ']' */
1296 const char *res
; /* the string to be substituted */
1297 char *nbuf
; /* new buffer to copy buf->buf to */
1298 size_t maxl
; /* expected length of the escape name */
1299 size_t naml
; /* actual length of the escape name */
1300 enum mandoc_esc esc
; /* type of the escape sequence */
1301 int inaml
; /* length returned from mandoc_escape() */
1302 int expand_count
; /* to avoid infinite loops */
1303 int npos
; /* position in numeric expression */
1304 int arg_complete
; /* argument not interrupted by eol */
1305 char term
; /* character terminating the escape */
1308 start
= buf
->buf
+ pos
;
1309 stesc
= strchr(start
, '\0') - 1;
1310 while (stesc
-- > start
) {
1312 /* Search backwards for the next backslash. */
1317 /* If it is escaped, skip it. */
1319 for (cp
= stesc
- 1; cp
>= start
; cp
--)
1323 if ((stesc
- cp
) % 2 == 0) {
1328 /* Decide whether to expand or to check only. */
1345 esc
= mandoc_escape(&cp
, &stnam
, &inaml
);
1346 if (esc
== ESCAPE_ERROR
||
1347 (esc
== ESCAPE_SPECIAL
&&
1348 mchars_spec2cp(r
->mchars
, stnam
, inaml
) < 0))
1349 mandoc_vmsg(MANDOCERR_ESC_BAD
,
1350 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1351 "%.*s", (int)(cp
- stesc
), stesc
);
1355 if (EXPAND_LIMIT
< ++expand_count
) {
1356 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1357 ln
, (int)(stesc
- buf
->buf
), NULL
);
1362 * The third character decides the length
1363 * of the name of the string or register.
1364 * Save a pointer to the name.
1391 /* Advance to the end of the name. */
1395 while (maxl
== 0 || naml
< maxl
) {
1397 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
1398 ln
, (int)(stesc
- buf
->buf
), stesc
);
1402 if (maxl
== 0 && *cp
== term
) {
1406 if (*cp
++ != '\\' || stesc
[1] != 'w') {
1410 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1411 case ESCAPE_SPECIAL
:
1413 case ESCAPE_UNICODE
:
1415 case ESCAPE_NUMBERED
:
1417 case ESCAPE_OVERSTRIKE
:
1426 * Retrieve the replacement string; if it is
1427 * undefined, resume searching for escapes.
1433 res
= roff_getstrn(r
, stnam
, naml
);
1437 ubuf
[0] = arg_complete
&&
1438 roff_evalnum(r
, ln
, stnam
, &npos
,
1439 NULL
, ROFFNUM_SCALE
) &&
1440 stnam
+ npos
+ 1 == cp
? '1' : '0';
1445 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1446 roff_getregn(r
, stnam
, naml
));
1451 /* use even incomplete args */
1452 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1458 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1459 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1460 "%.*s", (int)naml
, stnam
);
1462 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1463 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1464 ln
, (int)(stesc
- buf
->buf
), NULL
);
1468 /* Replace the escape sequence by the string. */
1471 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1472 buf
->buf
, res
, cp
) + 1;
1474 /* Prepare for the next replacement. */
1477 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1485 * Process text streams:
1486 * Convert all breakable hyphens into ASCII_HYPH.
1487 * Decrement and spring input line trap.
1490 roff_parsetext(struct buf
*buf
, int pos
, int *offs
)
1496 enum mandoc_esc esc
;
1498 start
= p
= buf
->buf
+ pos
;
1500 while (*p
!= '\0') {
1501 sz
= strcspn(p
, "-\\");
1508 /* Skip over escapes. */
1510 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1511 if (esc
== ESCAPE_ERROR
)
1516 } else if (p
== start
) {
1521 if (isalpha((unsigned char)p
[-1]) &&
1522 isalpha((unsigned char)p
[1]))
1527 /* Spring the input line trap. */
1528 if (roffit_lines
== 1) {
1529 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1536 return(ROFF_REPARSE
);
1537 } else if (roffit_lines
> 1)
1543 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
1547 int pos
; /* parse point */
1548 int spos
; /* saved parse point for messages */
1549 int ppos
; /* original offset in buf->buf */
1550 int ctl
; /* macro line (boolean) */
1554 /* Handle in-line equation delimiters. */
1556 if (r
->tbl
== NULL
&&
1557 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1558 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1559 e
= roff_eqndelim(r
, buf
, pos
);
1560 if (e
== ROFF_REPARSE
)
1562 assert(e
== ROFF_CONT
);
1565 /* Expand some escape sequences. */
1567 e
= roff_res(r
, buf
, ln
, pos
);
1570 assert(e
== ROFF_CONT
);
1572 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1575 * First, if a scope is open and we're not a macro, pass the
1576 * text through the macro's filter.
1577 * Equations process all content themselves.
1578 * Tables process almost all content themselves, but we want
1579 * to warn about macros before passing it there.
1582 if (r
->last
!= NULL
&& ! ctl
) {
1584 assert(roffs
[t
].text
);
1585 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1586 assert(e
== ROFF_IGN
|| e
== ROFF_CONT
);
1591 return(eqn_read(&r
->eqn
, ln
, buf
->buf
, ppos
, offs
));
1592 if (r
->tbl
!= NULL
&& ( ! ctl
|| buf
->buf
[pos
] == '\0'))
1593 return(tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
));
1595 return(roff_parsetext(buf
, pos
, offs
));
1597 /* Skip empty request lines. */
1599 if (buf
->buf
[pos
] == '"') {
1600 mandoc_msg(MANDOCERR_COMMENT_BAD
, r
->parse
,
1603 } else if (buf
->buf
[pos
] == '\0')
1607 * If a scope is open, go to the child handler for that macro,
1608 * as it may want to preprocess before doing anything with it.
1609 * Don't do so if an equation is open.
1614 assert(roffs
[t
].sub
);
1615 return((*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1618 /* No scope is open. This is a new request or macro. */
1621 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1623 /* Tables ignore most macros. */
1625 if (r
->tbl
!= NULL
&& (t
== ROFF_MAX
|| t
== ROFF_TS
)) {
1626 mandoc_msg(MANDOCERR_TBLMACRO
, r
->parse
,
1627 ln
, pos
, buf
->buf
+ spos
);
1630 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1632 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] == ' ')
1634 return(tbl_read(r
->tbl
, ln
, buf
->buf
, pos
));
1638 * This is neither a roff request nor a user-defined macro.
1639 * Let the standard macro set parsers handle it.
1645 /* Execute a roff request or a user defined macro. */
1647 assert(roffs
[t
].proc
);
1648 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1652 roff_endparse(struct roff
*r
)
1656 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1657 r
->last
->line
, r
->last
->col
,
1658 roffs
[r
->last
->tok
].name
);
1661 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1662 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, "EQ");
1667 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1668 r
->tbl
->line
, r
->tbl
->pos
, "TS");
1674 * Parse a roff node's type from the input buffer. This must be in the
1675 * form of ".foo xxx" in the usual way.
1678 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1687 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1691 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1693 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
1694 ? ROFF_USERDEF
: roffhash_find(mac
, maclen
);
1702 /* --- handling of request blocks ----------------------------------------- */
1705 roff_cblock(ROFF_ARGS
)
1709 * A block-close `..' should only be invoked as a child of an
1710 * ignore macro, otherwise raise a warning and just ignore it.
1713 if (r
->last
== NULL
) {
1714 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1719 switch (r
->last
->tok
) {
1721 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1726 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1733 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1738 if (buf
->buf
[pos
] != '\0')
1739 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
1740 ".. %s", buf
->buf
+ pos
);
1743 roffnode_cleanscope(r
);
1749 roffnode_cleanscope(struct roff
*r
)
1753 if (--r
->last
->endspan
!= 0)
1760 roff_ccond(struct roff
*r
, int ln
, int ppos
)
1763 if (NULL
== r
->last
) {
1764 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1769 switch (r
->last
->tok
) {
1777 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1782 if (r
->last
->endspan
> -1) {
1783 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1789 roffnode_cleanscope(r
);
1794 roff_block(ROFF_ARGS
)
1800 /* Ignore groff compatibility mode for now. */
1802 if (tok
== ROFF_de1
)
1804 else if (tok
== ROFF_dei1
)
1806 else if (tok
== ROFF_am1
)
1808 else if (tok
== ROFF_ami1
)
1811 /* Parse the macro name argument. */
1813 cp
= buf
->buf
+ pos
;
1814 if (tok
== ROFF_ig
) {
1819 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1820 iname
[namesz
] = '\0';
1823 /* Resolve the macro name argument if it is indirect. */
1825 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1826 if ((name
= roff_getstrn(r
, iname
, namesz
)) == NULL
) {
1827 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1828 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1829 "%.*s", (int)namesz
, iname
);
1832 namesz
= strlen(name
);
1836 if (namesz
== 0 && tok
!= ROFF_ig
) {
1837 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
,
1838 ln
, ppos
, roffs
[tok
].name
);
1842 roffnode_push(r
, tok
, name
, ln
, ppos
);
1845 * At the beginning of a `de' macro, clear the existing string
1846 * with the same name, if there is one. New content will be
1847 * appended from roff_block_text() in multiline mode.
1850 if (tok
== ROFF_de
|| tok
== ROFF_dei
)
1851 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
1856 /* Get the custom end marker. */
1859 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1861 /* Resolve the end marker if it is indirect. */
1863 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1864 if ((name
= roff_getstrn(r
, iname
, namesz
)) == NULL
) {
1865 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1866 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1867 "%.*s", (int)namesz
, iname
);
1870 namesz
= strlen(name
);
1875 r
->last
->end
= mandoc_strndup(name
, namesz
);
1878 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
1879 ln
, pos
, ".%s ... %s", roffs
[tok
].name
, cp
);
1885 roff_block_sub(ROFF_ARGS
)
1891 * First check whether a custom macro exists at this level. If
1892 * it does, then check against it. This is some of groff's
1893 * stranger behaviours. If we encountered a custom end-scope
1894 * tag and that tag also happens to be a "real" macro, then we
1895 * need to try interpreting it again as a real macro. If it's
1896 * not, then return ignore. Else continue.
1900 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
1901 if (buf
->buf
[i
] != r
->last
->end
[j
])
1904 if (r
->last
->end
[j
] == '\0' &&
1905 (buf
->buf
[i
] == '\0' ||
1906 buf
->buf
[i
] == ' ' ||
1907 buf
->buf
[i
] == '\t')) {
1909 roffnode_cleanscope(r
);
1911 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
1915 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
1923 * If we have no custom end-query or lookup failed, then try
1924 * pulling it out of the hashtable.
1927 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1929 if (t
!= ROFF_cblock
) {
1931 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
1935 assert(roffs
[t
].proc
);
1936 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1940 roff_block_text(ROFF_ARGS
)
1944 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
1950 roff_cond_sub(ROFF_ARGS
)
1957 roffnode_cleanscope(r
);
1958 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1961 * Fully handle known macros when they are structurally
1962 * required or when the conditional evaluated to true.
1965 if ((t
!= ROFF_MAX
) &&
1966 (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
)) {
1967 assert(roffs
[t
].proc
);
1968 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1972 * If `\}' occurs on a macro line without a preceding macro,
1973 * drop the line completely.
1976 ep
= buf
->buf
+ pos
;
1977 if (ep
[0] == '\\' && ep
[1] == '}')
1980 /* Always check for the closing delimiter `\}'. */
1982 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1983 if (*(++ep
) == '}') {
1985 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1990 return(rr
? ROFF_CONT
: ROFF_IGN
);
1994 roff_cond_text(ROFF_ARGS
)
2000 roffnode_cleanscope(r
);
2002 ep
= buf
->buf
+ pos
;
2003 while ((ep
= strchr(ep
, '\\')) != NULL
) {
2004 if (*(++ep
) == '}') {
2006 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
2011 return(rr
? ROFF_CONT
: ROFF_IGN
);
2014 /* --- handling of numeric and conditional expressions -------------------- */
2017 * Parse a single signed integer number. Stop at the first non-digit.
2018 * If there is at least one digit, return success and advance the
2019 * parse point, else return failure and let the parse point unchanged.
2020 * Ignore overflows, treat them just like the C language.
2023 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
2025 int myres
, scaled
, n
, p
;
2032 if (n
|| v
[p
] == '+')
2035 if (flags
& ROFFNUM_WHITE
)
2036 while (isspace((unsigned char)v
[p
]))
2039 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
2040 *res
= 10 * *res
+ v
[p
] - '0';
2047 /* Each number may be followed by one optional scaling unit. */
2051 scaled
= *res
* 65536;
2054 scaled
= *res
* 240;
2057 scaled
= *res
* 240 / 2.54;
2070 scaled
= *res
* 10 / 3;
2076 scaled
= *res
* 6 / 25;
2083 if (flags
& ROFFNUM_SCALE
)
2091 * Evaluate a string comparison condition.
2092 * The first character is the delimiter.
2093 * Succeed if the string up to its second occurrence
2094 * matches the string up to its third occurence.
2095 * Advance the cursor after the third occurrence
2096 * or lacking that, to the end of the line.
2099 roff_evalstrcond(const char *v
, int *pos
)
2101 const char *s1
, *s2
, *s3
;
2105 s1
= v
+ *pos
; /* initial delimiter */
2106 s2
= s1
+ 1; /* for scanning the first string */
2107 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2109 if (NULL
== s3
) /* found no middle delimiter */
2112 while ('\0' != *++s3
) {
2113 if (*s2
!= *s3
) { /* mismatch */
2114 s3
= strchr(s3
, *s1
);
2117 if (*s3
== *s1
) { /* found the final delimiter */
2126 s3
= strchr(s2
, '\0');
2127 else if (*s3
!= '\0')
2134 * Evaluate an optionally negated single character, numerical,
2135 * or string condition.
2138 roff_evalcond(struct roff
*r
, int ln
, char *v
, int *pos
)
2142 int number
, savepos
, wanttrue
;
2144 if ('!' == v
[*pos
]) {
2170 cp
= name
= v
+ ++*pos
;
2171 sz
= roff_getname(r
, &cp
, ln
, *pos
);
2173 return((sz
&& roff_hasregn(r
, name
, sz
)) == wanttrue
);
2179 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2180 return((number
> 0) == wanttrue
);
2181 else if (*pos
== savepos
)
2182 return(roff_evalstrcond(v
, pos
) == wanttrue
);
2188 roff_line_ignore(ROFF_ARGS
)
2195 roff_insec(ROFF_ARGS
)
2198 mandoc_msg(MANDOCERR_REQ_INSEC
, r
->parse
,
2199 ln
, ppos
, roffs
[tok
].name
);
2204 roff_unsupp(ROFF_ARGS
)
2207 mandoc_msg(MANDOCERR_REQ_UNSUPP
, r
->parse
,
2208 ln
, ppos
, roffs
[tok
].name
);
2213 roff_cond(ROFF_ARGS
)
2216 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2219 * An `.el' has no conditional body: it will consume the value
2220 * of the current rstack entry set in prior `ie' calls or
2223 * If we're not an `el', however, then evaluate the conditional.
2226 r
->last
->rule
= tok
== ROFF_el
?
2227 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2228 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2231 * An if-else will put the NEGATION of the current evaluated
2232 * conditional into the stack of rules.
2235 if (tok
== ROFF_ie
) {
2236 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2238 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2239 r
->rstacksz
, sizeof(int));
2241 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2244 /* If the parent has false as its rule, then so do we. */
2246 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2251 * If there is nothing on the line after the conditional,
2252 * not even whitespace, use next-line scope.
2255 if (buf
->buf
[pos
] == '\0') {
2256 r
->last
->endspan
= 2;
2260 while (buf
->buf
[pos
] == ' ')
2263 /* An opening brace requests multiline scope. */
2265 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2266 r
->last
->endspan
= -1;
2268 while (buf
->buf
[pos
] == ' ')
2274 * Anything else following the conditional causes
2275 * single-line scope. Warn if the scope contains
2276 * nothing but trailing whitespace.
2279 if (buf
->buf
[pos
] == '\0')
2280 mandoc_msg(MANDOCERR_COND_EMPTY
, r
->parse
,
2281 ln
, ppos
, roffs
[tok
].name
);
2283 r
->last
->endspan
= 1;
2297 /* Ignore groff compatibility mode for now. */
2299 if (tok
== ROFF_ds1
)
2301 else if (tok
== ROFF_as1
)
2305 * The first word is the name of the string.
2306 * If it is empty or terminated by an escape sequence,
2307 * abort the `ds' request without defining anything.
2310 name
= string
= buf
->buf
+ pos
;
2314 namesz
= roff_getname(r
, &string
, ln
, pos
);
2315 if (name
[namesz
] == '\\')
2318 /* Read past the initial double-quote, if any. */
2322 /* The rest is the value. */
2323 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2329 * Parse a single operator, one or two characters long.
2330 * If the operator is recognized, return success and advance the
2331 * parse point, else return failure and let the parse point unchanged.
2334 roff_getop(const char *v
, int *pos
, char *res
)
2355 switch (v
[*pos
+ 1]) {
2373 switch (v
[*pos
+ 1]) {
2387 if ('=' == v
[*pos
+ 1])
2399 * Evaluate either a parenthesized numeric expression
2400 * or a single signed integer number.
2403 roff_evalpar(struct roff
*r
, int ln
,
2404 const char *v
, int *pos
, int *res
, int flags
)
2408 return(roff_getnum(v
, pos
, res
, flags
));
2411 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2415 * Omission of the closing parenthesis
2416 * is an error in validation mode,
2417 * but ignored in evaluation mode.
2422 else if (NULL
== res
)
2429 * Evaluate a complete numeric expression.
2430 * Proceed left to right, there is no concept of precedence.
2433 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2434 int *pos
, int *res
, int flags
)
2436 int mypos
, operand2
;
2444 if (flags
& ROFFNUM_WHITE
)
2445 while (isspace((unsigned char)v
[*pos
]))
2448 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2452 if (flags
& ROFFNUM_WHITE
)
2453 while (isspace((unsigned char)v
[*pos
]))
2456 if ( ! roff_getop(v
, pos
, &operator))
2459 if (flags
& ROFFNUM_WHITE
)
2460 while (isspace((unsigned char)v
[*pos
]))
2463 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2466 if (flags
& ROFFNUM_WHITE
)
2467 while (isspace((unsigned char)v
[*pos
]))
2484 if (operand2
== 0) {
2485 mandoc_msg(MANDOCERR_DIVZERO
,
2486 r
->parse
, ln
, *pos
, v
);
2493 if (operand2
== 0) {
2494 mandoc_msg(MANDOCERR_DIVZERO
,
2495 r
->parse
, ln
, *pos
, v
);
2502 *res
= *res
< operand2
;
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 if (operand2
< *res
)
2530 if (operand2
> *res
)
2540 /* --- register management ------------------------------------------------ */
2543 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2545 struct roffreg
*reg
;
2547 /* Search for an existing register with the same name. */
2550 while (reg
&& strcmp(name
, reg
->key
.p
))
2554 /* Create a new register. */
2555 reg
= mandoc_malloc(sizeof(struct roffreg
));
2556 reg
->key
.p
= mandoc_strdup(name
);
2557 reg
->key
.sz
= strlen(name
);
2559 reg
->next
= r
->regtab
;
2565 else if ('-' == sign
)
2572 * Handle some predefined read-only number registers.
2573 * For now, return -1 if the requested register is not predefined;
2574 * in case a predefined read-only register having the value -1
2575 * were to turn up, another special value would have to be chosen.
2578 roff_getregro(const char *name
)
2582 case 'A': /* ASCII approximation mode is always off. */
2584 case 'g': /* Groff compatibility mode is always on. */
2586 case 'H': /* Fixed horizontal resolution. */
2588 case 'j': /* Always adjust left margin only. */
2590 case 'T': /* Some output device is always defined. */
2592 case 'V': /* Fixed vertical resolution. */
2600 roff_getreg(const struct roff
*r
, const char *name
)
2602 struct roffreg
*reg
;
2605 if ('.' == name
[0] && '\0' != name
[1] && '\0' == name
[2]) {
2606 val
= roff_getregro(name
+ 1);
2611 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2612 if (0 == strcmp(name
, reg
->key
.p
))
2619 roff_getregn(const struct roff
*r
, const char *name
, size_t len
)
2621 struct roffreg
*reg
;
2624 if ('.' == name
[0] && 2 == len
) {
2625 val
= roff_getregro(name
+ 1);
2630 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2631 if (len
== reg
->key
.sz
&&
2632 0 == strncmp(name
, reg
->key
.p
, len
))
2639 roff_hasregn(const struct roff
*r
, const char *name
, size_t len
)
2641 struct roffreg
*reg
;
2644 if ('.' == name
[0] && 2 == len
) {
2645 val
= roff_getregro(name
+ 1);
2650 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2651 if (len
== reg
->key
.sz
&&
2652 0 == strncmp(name
, reg
->key
.p
, len
))
2659 roff_freereg(struct roffreg
*reg
)
2661 struct roffreg
*old_reg
;
2663 while (NULL
!= reg
) {
2679 key
= val
= buf
->buf
+ pos
;
2683 keysz
= roff_getname(r
, &val
, ln
, pos
);
2684 if (key
[keysz
] == '\\')
2689 if (sign
== '+' || sign
== '-')
2692 if (roff_evalnum(r
, ln
, val
, NULL
, &iv
, ROFFNUM_SCALE
))
2693 roff_setreg(r
, key
, iv
, sign
);
2701 struct roffreg
*reg
, **prev
;
2705 name
= cp
= buf
->buf
+ pos
;
2708 namesz
= roff_getname(r
, &cp
, ln
, pos
);
2709 name
[namesz
] = '\0';
2714 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
2726 /* --- handler functions for roff requests -------------------------------- */
2735 cp
= buf
->buf
+ pos
;
2736 while (*cp
!= '\0') {
2738 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
2739 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
2740 if (name
[namesz
] == '\\')
2751 /* Parse the number of lines. */
2753 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
2754 mandoc_msg(MANDOCERR_IT_NONUM
, r
->parse
,
2755 ln
, ppos
, buf
->buf
+ 1);
2759 while (isspace((unsigned char)buf
->buf
[pos
]))
2763 * Arm the input line trap.
2764 * Special-casing "an-trap" is an ugly workaround to cope
2765 * with DocBook stupidly fiddling with man(7) internals.
2769 roffit_macro
= mandoc_strdup(iv
!= 1 ||
2770 strcmp(buf
->buf
+ pos
, "an-trap") ?
2771 buf
->buf
+ pos
: "br");
2778 const char *const *cp
;
2780 if ((r
->options
& (MPARSE_MDOC
| MPARSE_QUICK
)) == 0)
2781 for (cp
= __mdoc_reserved
; *cp
; cp
++)
2782 roff_setstr(r
, *cp
, NULL
, 0);
2785 r
->format
= MPARSE_MDOC
;
2793 const char *const *cp
;
2795 if ((r
->options
& MPARSE_QUICK
) == 0)
2796 for (cp
= __man_reserved
; *cp
; cp
++)
2797 roff_setstr(r
, *cp
, NULL
, 0);
2800 r
->format
= MPARSE_MAN
;
2810 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2812 else if ( ! tbl_end(&r
->tbl
)) {
2814 buf
->buf
= mandoc_strdup(".sp");
2816 return(ROFF_REPARSE
);
2826 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2829 tbl_restart(ppos
, ln
, r
->tbl
);
2835 * Handle in-line equation delimiters.
2838 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
2841 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
2844 * Outside equations, look for an opening delimiter.
2845 * If we are inside an equation, we already know it is
2846 * in-line, or this function wouldn't have been called;
2847 * so look for a closing delimiter.
2850 cp1
= buf
->buf
+ pos
;
2851 cp2
= strchr(cp1
, r
->eqn
== NULL
?
2852 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
2857 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
2859 /* Handle preceding text, protecting whitespace. */
2861 if (*buf
->buf
!= '\0') {
2868 * Prepare replacing the delimiter with an equation macro
2869 * and drop leading white space from the equation.
2872 if (r
->eqn
== NULL
) {
2879 /* Handle following text, protecting whitespace. */
2887 /* Do the actual replacement. */
2889 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
2890 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
2894 /* Toggle the in-line state of the eqn subsystem. */
2896 r
->eqn_inline
= r
->eqn
== NULL
;
2897 return(ROFF_REPARSE
);
2905 assert(r
->eqn
== NULL
);
2906 e
= eqn_alloc(ppos
, ln
, r
->parse
);
2909 r
->last_eqn
->next
= e
;
2910 e
->delim
= r
->last_eqn
->delim
;
2911 e
->odelim
= r
->last_eqn
->odelim
;
2912 e
->cdelim
= r
->last_eqn
->cdelim
;
2914 r
->first_eqn
= r
->last_eqn
= e
;
2916 r
->eqn
= r
->last_eqn
= e
;
2918 if (buf
->buf
[pos
] != '\0')
2919 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
2920 ".EQ %s", buf
->buf
+ pos
);
2929 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
, ln
, ppos
, "EN");
2936 struct tbl_node
*tbl
;
2939 mandoc_msg(MANDOCERR_BLK_BROKEN
, r
->parse
,
2940 ln
, ppos
, "TS breaks TS");
2944 tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
2947 r
->last_tbl
->next
= tbl
;
2949 r
->first_tbl
= r
->last_tbl
= tbl
;
2951 r
->tbl
= r
->last_tbl
= tbl
;
2959 buf
->buf
[pos
- 1] = '\0';
2970 if (*p
== '\0' || (r
->control
= *p
++) == '.')
2974 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
2975 ln
, p
- buf
->buf
, "cc ... %s", p
);
2983 const char *p
, *first
, *second
;
2985 enum mandoc_esc esc
;
2990 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
, ln
, ppos
, "tr");
2994 while (*p
!= '\0') {
2998 if (*first
== '\\') {
2999 esc
= mandoc_escape(&p
, NULL
, NULL
);
3000 if (esc
== ESCAPE_ERROR
) {
3001 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3002 ln
, (int)(p
- buf
->buf
), first
);
3005 fsz
= (size_t)(p
- first
);
3009 if (*second
== '\\') {
3010 esc
= mandoc_escape(&p
, NULL
, NULL
);
3011 if (esc
== ESCAPE_ERROR
) {
3012 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
3013 ln
, (int)(p
- buf
->buf
), second
);
3016 ssz
= (size_t)(p
- second
);
3017 } else if (*second
== '\0') {
3018 mandoc_vmsg(MANDOCERR_TR_ODD
, r
->parse
,
3019 ln
, first
- buf
->buf
, "tr %s", first
);
3025 roff_setstrn(&r
->xmbtab
, first
, fsz
,
3030 if (r
->xtab
== NULL
)
3031 r
->xtab
= mandoc_calloc(128,
3032 sizeof(struct roffstr
));
3034 free(r
->xtab
[(int)*first
].p
);
3035 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
3036 r
->xtab
[(int)*first
].sz
= ssz
;
3047 name
= buf
->buf
+ pos
;
3048 mandoc_vmsg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, "so %s", name
);
3051 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
3052 * opening anything that's not in our cwd or anything beneath
3053 * it. Thus, explicitly disallow traversing up the file-system
3054 * or using absolute paths.
3057 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
3058 mandoc_vmsg(MANDOCERR_SO_PATH
, r
->parse
, ln
, ppos
,
3060 buf
->sz
= mandoc_asprintf(&cp
,
3061 ".sp\nSee the file %s.\n.sp", name
) + 1;
3065 return(ROFF_REPARSE
);
3072 /* --- user defined strings and macros ------------------------------------ */
3075 roff_userdef(ROFF_ARGS
)
3077 const char *arg
[9], *ap
;
3083 * Collect pointers to macro argument strings
3084 * and NUL-terminate them.
3087 cp
= buf
->buf
+ pos
;
3088 for (i
= 0; i
< 9; i
++)
3089 arg
[i
] = *cp
== '\0' ? "" :
3090 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
3093 * Expand macro arguments.
3096 buf
->sz
= strlen(r
->current_string
) + 1;
3097 n1
= cp
= mandoc_malloc(buf
->sz
);
3098 memcpy(n1
, r
->current_string
, buf
->sz
);
3099 while (*cp
!= '\0') {
3101 /* Scan ahead for the next argument invocation. */
3113 * Determine the size of the expanded argument,
3114 * taking escaping of quotes into account.
3118 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3126 * Determine the size of the rest of the
3127 * unexpanded macro, including the NUL.
3130 rsz
= buf
->sz
- (cp
- n1
) - 3;
3133 * When shrinking, move before
3134 * releasing the storage.
3138 memmove(cp
+ asz
, cp
+ 3, rsz
);
3141 * Resize the storage for the macro
3142 * and readjust the parse pointer.
3146 n2
= mandoc_realloc(n1
, buf
->sz
);
3147 cp
= n2
+ (cp
- n1
);
3151 * When growing, make room
3152 * for the expanded argument.
3156 memmove(cp
+ asz
, cp
+ 3, rsz
);
3159 /* Copy the expanded argument, escaping quotes. */
3162 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3164 memcpy(n2
, "\\(dq", 4);
3172 * Replace the macro invocation
3173 * by the expanded macro.
3180 return(buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
3181 ROFF_REPARSE
: ROFF_APPEND
);
3185 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
3194 /* Read until end of name and terminate it with NUL. */
3195 for (cp
= name
; 1; cp
++) {
3196 if ('\0' == *cp
|| ' ' == *cp
) {
3203 if ('{' == cp
[1] || '}' == cp
[1])
3208 mandoc_vmsg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
,
3209 "%.*s", (int)(cp
- name
+ 1), name
);
3210 mandoc_escape((const char **)&cp
, NULL
, NULL
);
3214 /* Read past spaces. */
3223 * Store *string into the user-defined string called *name.
3224 * To clear an existing entry, call with (*r, *name, NULL, 0).
3225 * append == 0: replace mode
3226 * append == 1: single-line append mode
3227 * append == 2: multiline append mode, append '\n' after each call
3230 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
3234 roff_setstrn(&r
->strtab
, name
, strlen(name
), string
,
3235 string
? strlen(string
) : 0, append
);
3239 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
3240 const char *string
, size_t stringsz
, int append
)
3245 size_t oldch
, newch
;
3247 /* Search for an existing string with the same name. */
3250 while (n
&& (namesz
!= n
->key
.sz
||
3251 strncmp(n
->key
.p
, name
, namesz
)))
3255 /* Create a new string table entry. */
3256 n
= mandoc_malloc(sizeof(struct roffkv
));
3257 n
->key
.p
= mandoc_strndup(name
, namesz
);
3263 } else if (0 == append
) {
3273 * One additional byte for the '\n' in multiline mode,
3274 * and one for the terminating '\0'.
3276 newch
= stringsz
+ (1 < append
? 2u : 1u);
3278 if (NULL
== n
->val
.p
) {
3279 n
->val
.p
= mandoc_malloc(newch
);
3284 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
3287 /* Skip existing content in the destination buffer. */
3288 c
= n
->val
.p
+ (int)oldch
;
3290 /* Append new content to the destination buffer. */
3292 while (i
< (int)stringsz
) {
3294 * Rudimentary roff copy mode:
3295 * Handle escaped backslashes.
3297 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
3302 /* Append terminating bytes. */
3307 n
->val
.sz
= (int)(c
- n
->val
.p
);
3311 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
3313 const struct roffkv
*n
;
3316 for (n
= r
->strtab
; n
; n
= n
->next
)
3317 if (0 == strncmp(name
, n
->key
.p
, len
) &&
3318 '\0' == n
->key
.p
[(int)len
])
3321 for (i
= 0; i
< PREDEFS_MAX
; i
++)
3322 if (0 == strncmp(name
, predefs
[i
].name
, len
) &&
3323 '\0' == predefs
[i
].name
[(int)len
])
3324 return(predefs
[i
].str
);
3330 roff_freestr(struct roffkv
*r
)
3332 struct roffkv
*n
, *nn
;
3334 for (n
= r
; n
; n
= nn
) {
3342 /* --- accessors and utility functions ------------------------------------ */
3344 const struct tbl_span
*
3345 roff_span(const struct roff
*r
)
3348 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
3352 roff_eqn(const struct roff
*r
)
3355 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);
3359 * Duplicate an input string, making the appropriate character
3360 * conversations (as stipulated by `tr') along the way.
3361 * Returns a heap-allocated string with all the replacements made.
3364 roff_strdup(const struct roff
*r
, const char *p
)
3366 const struct roffkv
*cp
;
3370 enum mandoc_esc esc
;
3372 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
3373 return(mandoc_strdup(p
));
3374 else if ('\0' == *p
)
3375 return(mandoc_strdup(""));
3378 * Step through each character looking for term matches
3379 * (remember that a `tr' can be invoked with an escape, which is
3380 * a glyph but the escape is multi-character).
3381 * We only do this if the character hash has been initialised
3382 * and the string is >0 length.
3388 while ('\0' != *p
) {
3389 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(int)*p
].p
) {
3390 sz
= r
->xtab
[(int)*p
].sz
;
3391 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3392 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
3396 } else if ('\\' != *p
) {
3397 res
= mandoc_realloc(res
, ssz
+ 2);
3402 /* Search for term matches. */
3403 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
3404 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
3409 * A match has been found.
3410 * Append the match to the array and move
3411 * forward by its keysize.
3413 res
= mandoc_realloc(res
,
3414 ssz
+ cp
->val
.sz
+ 1);
3415 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
3417 p
+= (int)cp
->key
.sz
;
3422 * Handle escapes carefully: we need to copy
3423 * over just the escape itself, or else we might
3424 * do replacements within the escape itself.
3425 * Make sure to pass along the bogus string.
3428 esc
= mandoc_escape(&p
, NULL
, NULL
);
3429 if (ESCAPE_ERROR
== esc
) {
3431 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3432 memcpy(res
+ ssz
, pp
, sz
);
3436 * We bail out on bad escapes.
3437 * No need to warn: we already did so when
3438 * roff_res() was called.
3441 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3442 memcpy(res
+ ssz
, pp
, sz
);
3446 res
[(int)ssz
] = '\0';
3451 roff_getformat(const struct roff
*r
)
3458 * Find out whether a line is a macro line or not.
3459 * If it is, adjust the current position and return one; if it isn't,
3460 * return zero and don't change the current position.
3461 * If the control character has been set with `.cc', then let that grain
3463 * This is slighly contrary to groff, where using the non-breaking
3464 * control character when `cc' has been invoked will cause the
3465 * non-breaking macro contents to be printed verbatim.
3468 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
3474 if (0 != r
->control
&& cp
[pos
] == r
->control
)
3476 else if (0 != r
->control
)
3478 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
3480 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
3485 while (' ' == cp
[pos
] || '\t' == cp
[pos
])