]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.268 2015/04/19 14:57:38 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,
401 const 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 char *name
);
416 static const char *roff_getstrn(const struct roff
*,
417 const char *, size_t);
418 static enum rofferr
roff_insec(ROFF_ARGS
);
419 static enum rofferr
roff_it(ROFF_ARGS
);
420 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
421 static void roff_man_alloc1(struct roff_man
*);
422 static void roff_man_free1(struct roff_man
*);
423 static enum rofferr
roff_nr(ROFF_ARGS
);
424 static enum rofft
roff_parse(struct roff
*, char *, int *,
426 static enum rofferr
roff_parsetext(struct buf
*, int, int *);
427 static enum rofferr
roff_res(struct roff
*, struct buf
*, int, int);
428 static enum rofferr
roff_rm(ROFF_ARGS
);
429 static enum rofferr
roff_rr(ROFF_ARGS
);
430 static void roff_setstr(struct roff
*,
431 const char *, const char *, int);
432 static void roff_setstrn(struct roffkv
**, const char *,
433 size_t, const char *, size_t, int);
434 static enum rofferr
roff_so(ROFF_ARGS
);
435 static enum rofferr
roff_tr(ROFF_ARGS
);
436 static enum rofferr
roff_Dd(ROFF_ARGS
);
437 static enum rofferr
roff_TH(ROFF_ARGS
);
438 static enum rofferr
roff_TE(ROFF_ARGS
);
439 static enum rofferr
roff_TS(ROFF_ARGS
);
440 static enum rofferr
roff_EQ(ROFF_ARGS
);
441 static enum rofferr
roff_EN(ROFF_ARGS
);
442 static enum rofferr
roff_T_(ROFF_ARGS
);
443 static enum rofferr
roff_unsupp(ROFF_ARGS
);
444 static enum rofferr
roff_userdef(ROFF_ARGS
);
446 /* --- constant data ------------------------------------------------------ */
448 /* See roffhash_find() */
452 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
454 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
455 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
457 static struct roffmac
*hash
[HASHWIDTH
];
459 static struct roffmac roffs
[ROFF_MAX
] = {
460 { "ab", roff_unsupp
, NULL
, NULL
, 0, NULL
},
461 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
462 { "af", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
463 { "aln", roff_unsupp
, NULL
, NULL
, 0, NULL
},
464 { "als", roff_unsupp
, NULL
, NULL
, 0, NULL
},
465 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
466 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
467 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
468 { "ami1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
469 { "as", roff_ds
, NULL
, NULL
, 0, NULL
},
470 { "as1", roff_ds
, NULL
, NULL
, 0, NULL
},
471 { "asciify", roff_unsupp
, NULL
, NULL
, 0, NULL
},
472 { "backtrace", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
473 { "bd", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
474 { "bleedat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
475 { "blm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
476 { "box", roff_unsupp
, NULL
, NULL
, 0, NULL
},
477 { "boxa", roff_unsupp
, NULL
, NULL
, 0, NULL
},
478 { "bp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
479 { "BP", roff_unsupp
, NULL
, NULL
, 0, NULL
},
480 { "break", roff_unsupp
, NULL
, NULL
, 0, NULL
},
481 { "breakchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
482 { "brnl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
483 { "brp", roff_brp
, NULL
, NULL
, 0, NULL
},
484 { "brpnl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
485 { "c2", roff_unsupp
, NULL
, NULL
, 0, NULL
},
486 { "cc", roff_cc
, NULL
, NULL
, 0, NULL
},
487 { "ce", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
488 { "cf", roff_insec
, NULL
, NULL
, 0, NULL
},
489 { "cflags", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
490 { "ch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
491 { "char", roff_unsupp
, NULL
, NULL
, 0, NULL
},
492 { "chop", roff_unsupp
, NULL
, NULL
, 0, NULL
},
493 { "class", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
494 { "close", roff_insec
, NULL
, NULL
, 0, NULL
},
495 { "CL", roff_unsupp
, NULL
, NULL
, 0, NULL
},
496 { "color", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
497 { "composite", roff_unsupp
, NULL
, NULL
, 0, NULL
},
498 { "continue", roff_unsupp
, NULL
, NULL
, 0, NULL
},
499 { "cp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
500 { "cropat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
501 { "cs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
502 { "cu", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
503 { "da", roff_unsupp
, NULL
, NULL
, 0, NULL
},
504 { "dch", roff_unsupp
, NULL
, NULL
, 0, NULL
},
505 { "Dd", roff_Dd
, NULL
, NULL
, 0, NULL
},
506 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
507 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
508 { "defcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
509 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
510 { "dei1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
511 { "device", roff_unsupp
, NULL
, NULL
, 0, NULL
},
512 { "devicem", roff_unsupp
, NULL
, NULL
, 0, NULL
},
513 { "di", roff_unsupp
, NULL
, NULL
, 0, NULL
},
514 { "do", roff_unsupp
, NULL
, NULL
, 0, NULL
},
515 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
516 { "ds1", roff_ds
, NULL
, NULL
, 0, NULL
},
517 { "dwh", roff_unsupp
, NULL
, NULL
, 0, NULL
},
518 { "dt", roff_unsupp
, NULL
, NULL
, 0, NULL
},
519 { "ec", roff_unsupp
, NULL
, NULL
, 0, NULL
},
520 { "ecr", roff_unsupp
, NULL
, NULL
, 0, NULL
},
521 { "ecs", roff_unsupp
, NULL
, NULL
, 0, NULL
},
522 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
523 { "em", roff_unsupp
, NULL
, NULL
, 0, NULL
},
524 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
525 { "eo", roff_unsupp
, NULL
, NULL
, 0, NULL
},
526 { "EP", roff_unsupp
, NULL
, NULL
, 0, NULL
},
527 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
528 { "errprint", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
529 { "ev", roff_unsupp
, NULL
, NULL
, 0, NULL
},
530 { "evc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
531 { "ex", roff_unsupp
, NULL
, NULL
, 0, NULL
},
532 { "fallback", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
533 { "fam", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
534 { "fc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
535 { "fchar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
536 { "fcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
537 { "fdeferlig", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
538 { "feature", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
539 { "fkern", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
540 { "fl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
541 { "flig", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
542 { "fp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
543 { "fps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
544 { "fschar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
545 { "fspacewidth", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
546 { "fspecial", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
547 { "ftr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
548 { "fzoom", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
549 { "gcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
550 { "hc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
551 { "hcode", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
552 { "hidechar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
553 { "hla", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
554 { "hlm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
555 { "hpf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
556 { "hpfa", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
557 { "hpfcode", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
558 { "hw", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
559 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
560 { "hylang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
561 { "hylen", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
562 { "hym", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
563 { "hypp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
564 { "hys", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
565 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
566 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
567 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
568 { "index", roff_unsupp
, NULL
, NULL
, 0, NULL
},
569 { "it", roff_it
, NULL
, NULL
, 0, NULL
},
570 { "itc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
571 { "IX", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
572 { "kern", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
573 { "kernafter", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
574 { "kernbefore", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
575 { "kernpair", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
576 { "lc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
577 { "lc_ctype", roff_unsupp
, NULL
, NULL
, 0, NULL
},
578 { "lds", roff_unsupp
, NULL
, NULL
, 0, NULL
},
579 { "length", roff_unsupp
, NULL
, NULL
, 0, NULL
},
580 { "letadj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
581 { "lf", roff_insec
, NULL
, NULL
, 0, NULL
},
582 { "lg", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
583 { "lhang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
584 { "linetabs", roff_unsupp
, NULL
, NULL
, 0, NULL
},
585 { "lnr", roff_unsupp
, NULL
, NULL
, 0, NULL
},
586 { "lnrf", roff_unsupp
, NULL
, NULL
, 0, NULL
},
587 { "lpfx", roff_unsupp
, NULL
, NULL
, 0, NULL
},
588 { "ls", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
589 { "lsm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
590 { "lt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
591 { "mc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
592 { "mediasize", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
593 { "minss", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
594 { "mk", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
595 { "mso", roff_insec
, NULL
, NULL
, 0, NULL
},
596 { "na", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
597 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
598 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
599 { "nhychar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
600 { "nm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
601 { "nn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
602 { "nop", roff_unsupp
, NULL
, NULL
, 0, NULL
},
603 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
604 { "nrf", roff_unsupp
, NULL
, NULL
, 0, NULL
},
605 { "nroff", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
606 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
607 { "nx", roff_insec
, NULL
, NULL
, 0, NULL
},
608 { "open", roff_insec
, NULL
, NULL
, 0, NULL
},
609 { "opena", roff_insec
, NULL
, NULL
, 0, NULL
},
610 { "os", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
611 { "output", roff_unsupp
, NULL
, NULL
, 0, NULL
},
612 { "padj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
613 { "papersize", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
614 { "pc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
615 { "pev", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
616 { "pi", roff_insec
, NULL
, NULL
, 0, NULL
},
617 { "PI", roff_unsupp
, NULL
, NULL
, 0, NULL
},
618 { "pl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
619 { "pm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
620 { "pn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
621 { "pnr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
622 { "po", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
623 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
624 { "psbb", roff_unsupp
, NULL
, NULL
, 0, NULL
},
625 { "pshape", roff_unsupp
, NULL
, NULL
, 0, NULL
},
626 { "pso", roff_insec
, NULL
, NULL
, 0, NULL
},
627 { "ptr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
628 { "pvs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
629 { "rchar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
630 { "rd", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
631 { "recursionlimit", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
632 { "return", roff_unsupp
, NULL
, NULL
, 0, NULL
},
633 { "rfschar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
634 { "rhang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
635 { "rj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
636 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
637 { "rn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
638 { "rnn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
639 { "rr", roff_rr
, NULL
, NULL
, 0, NULL
},
640 { "rs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
641 { "rt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
642 { "schar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
643 { "sentchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
644 { "shc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
645 { "shift", roff_unsupp
, NULL
, NULL
, 0, NULL
},
646 { "sizes", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
647 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
648 { "spacewidth", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
649 { "special", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
650 { "spreadwarn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
651 { "ss", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
652 { "sty", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
653 { "substring", roff_unsupp
, NULL
, NULL
, 0, NULL
},
654 { "sv", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
655 { "sy", roff_insec
, NULL
, NULL
, 0, NULL
},
656 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
657 { "ta", roff_unsupp
, NULL
, NULL
, 0, NULL
},
658 { "tc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
659 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
660 { "TH", roff_TH
, NULL
, NULL
, 0, NULL
},
661 { "ti", roff_unsupp
, NULL
, NULL
, 0, NULL
},
662 { "tkf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
663 { "tl", roff_unsupp
, NULL
, NULL
, 0, NULL
},
664 { "tm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
665 { "tm1", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
666 { "tmc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
667 { "tr", roff_tr
, NULL
, NULL
, 0, NULL
},
668 { "track", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
669 { "transchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
670 { "trf", roff_insec
, NULL
, NULL
, 0, NULL
},
671 { "trimat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
672 { "trin", roff_unsupp
, NULL
, NULL
, 0, NULL
},
673 { "trnt", roff_unsupp
, NULL
, NULL
, 0, NULL
},
674 { "troff", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
675 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
676 { "uf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
677 { "ul", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
678 { "unformat", roff_unsupp
, NULL
, NULL
, 0, NULL
},
679 { "unwatch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
680 { "unwatchn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
681 { "vpt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
682 { "vs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
683 { "warn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
684 { "warnscale", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
685 { "watch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
686 { "watchlength", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
687 { "watchn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
688 { "wh", roff_unsupp
, NULL
, NULL
, 0, NULL
},
689 { "while", roff_unsupp
, NULL
, NULL
, 0, NULL
},
690 { "write", roff_insec
, NULL
, NULL
, 0, NULL
},
691 { "writec", roff_insec
, NULL
, NULL
, 0, NULL
},
692 { "writem", roff_insec
, NULL
, NULL
, 0, NULL
},
693 { "xflag", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
694 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
695 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
698 /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
699 const char *const __mdoc_reserved
[] = {
700 "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
701 "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
702 "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
703 "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
704 "Dt", "Dv", "Dx", "D1",
705 "Ec", "Ed", "Ef", "Ek", "El", "Em",
706 "En", "Eo", "Er", "Es", "Ev", "Ex",
707 "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
708 "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
709 "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
710 "Oc", "Oo", "Op", "Os", "Ot", "Ox",
711 "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
712 "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
713 "Sc", "Sh", "Sm", "So", "Sq",
714 "Ss", "St", "Sx", "Sy",
715 "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
716 "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
717 "%P", "%Q", "%R", "%T", "%U", "%V",
721 /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
722 const char *const __man_reserved
[] = {
723 "AT", "B", "BI", "BR", "DT",
724 "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
725 "LP", "OP", "P", "PD", "PP",
726 "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
727 "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
731 /* Array of injected predefined strings. */
732 #define PREDEFS_MAX 38
733 static const struct predef predefs
[PREDEFS_MAX
] = {
734 #include "predefs.in"
737 /* See roffhash_find() */
738 #define ROFF_HASH(p) (p[0] - ASCII_LO)
740 static int roffit_lines
; /* number of lines to delay */
741 static char *roffit_macro
; /* nil-terminated macro line */
744 /* --- request table ------------------------------------------------------ */
752 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
753 assert(roffs
[i
].name
[0] >= ASCII_LO
);
754 assert(roffs
[i
].name
[0] <= ASCII_HI
);
756 buc
= ROFF_HASH(roffs
[i
].name
);
758 if (NULL
!= (n
= hash
[buc
])) {
759 for ( ; n
->next
; n
= n
->next
)
763 hash
[buc
] = &roffs
[i
];
768 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
769 * the nil-terminated string name could be found.
772 roffhash_find(const char *p
, size_t s
)
778 * libroff has an extremely simple hashtable, for the time
779 * being, which simply keys on the first character, which must
780 * be printable, then walks a chain. It works well enough until
784 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
789 if (NULL
== (n
= hash
[buc
]))
791 for ( ; n
; n
= n
->next
)
792 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
793 return((enum rofft
)(n
- roffs
));
798 /* --- stack of request blocks -------------------------------------------- */
801 * Pop the current node off of the stack of roff instructions currently
805 roffnode_pop(struct roff
*r
)
812 r
->last
= r
->last
->parent
;
819 * Push a roff node onto the instruction stack. This must later be
820 * removed with roffnode_pop().
823 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
828 p
= mandoc_calloc(1, sizeof(struct roffnode
));
831 p
->name
= mandoc_strdup(name
);
835 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
840 /* --- roff parser state data management ---------------------------------- */
843 roff_free1(struct roff
*r
)
845 struct tbl_node
*tbl
;
849 while (NULL
!= (tbl
= r
->first_tbl
)) {
850 r
->first_tbl
= tbl
->next
;
853 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
855 while (NULL
!= (e
= r
->first_eqn
)) {
856 r
->first_eqn
= e
->next
;
859 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
869 roff_freereg(r
->regtab
);
872 roff_freestr(r
->strtab
);
873 roff_freestr(r
->xmbtab
);
874 r
->strtab
= r
->xmbtab
= NULL
;
877 for (i
= 0; i
< 128; i
++)
884 roff_reset(struct roff
*r
)
888 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
893 roff_free(struct roff
*r
)
901 roff_alloc(struct mparse
*parse
, const struct mchars
*mchars
, int options
)
905 r
= mandoc_calloc(1, sizeof(struct roff
));
908 r
->options
= options
;
909 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
917 /* --- syntax tree state data management ---------------------------------- */
920 roff_man_free1(struct roff_man
*man
)
923 if (man
->first
!= NULL
)
924 roff_node_delete(man
, man
->first
);
925 free(man
->meta
.msec
);
928 free(man
->meta
.arch
);
929 free(man
->meta
.title
);
930 free(man
->meta
.name
);
931 free(man
->meta
.date
);
935 roff_man_alloc1(struct roff_man
*man
)
938 memset(&man
->meta
, 0, sizeof(man
->meta
));
939 man
->first
= mandoc_calloc(1, sizeof(*man
->first
));
940 man
->first
->type
= ROFFT_ROOT
;
941 man
->last
= man
->first
;
944 man
->macroset
= MACROSET_NONE
;
945 man
->lastsec
= man
->lastnamed
= SEC_NONE
;
946 man
->next
= ROFF_NEXT_CHILD
;
950 roff_man_reset(struct roff_man
*man
)
954 roff_man_alloc1(man
);
958 roff_man_free(struct roff_man
*man
)
966 roff_man_alloc(struct roff
*roff
, struct mparse
*parse
,
967 const char *defos
, int quick
)
969 struct roff_man
*man
;
971 man
= mandoc_calloc(1, sizeof(*man
));
976 roff_man_alloc1(man
);
980 /* --- syntax tree handling ----------------------------------------------- */
983 roff_node_alloc(struct roff_man
*man
, int line
, int pos
,
984 enum roff_type type
, int tok
)
988 n
= mandoc_calloc(1, sizeof(*n
));
993 n
->sec
= man
->lastsec
;
995 if (man
->flags
& MDOC_SYNOPSIS
)
996 n
->flags
|= MDOC_SYNPRETTY
;
998 n
->flags
&= ~MDOC_SYNPRETTY
;
999 if (man
->flags
& MDOC_NEWLINE
)
1000 n
->flags
|= MDOC_LINE
;
1001 man
->flags
&= ~MDOC_NEWLINE
;
1007 roff_node_append(struct roff_man
*man
, struct roff_node
*n
)
1010 switch (man
->next
) {
1011 case ROFF_NEXT_SIBLING
:
1012 man
->last
->next
= n
;
1013 n
->prev
= man
->last
;
1014 n
->parent
= man
->last
->parent
;
1016 case ROFF_NEXT_CHILD
:
1017 man
->last
->child
= n
;
1018 n
->parent
= man
->last
;
1024 n
->parent
->nchild
++;
1027 * Copy over the normalised-data pointer of our parent. Not
1028 * everybody has one, but copying a null pointer is fine.
1033 if (n
->end
!= ENDBODY_NOT
)
1039 n
->norm
= n
->parent
->norm
;
1045 if (man
->macroset
== MACROSET_MDOC
)
1046 mdoc_valid_pre(man
, n
);
1050 assert(n
->parent
->type
== ROFFT_BLOCK
);
1051 n
->parent
->head
= n
;
1056 assert(n
->parent
->type
== ROFFT_BLOCK
);
1057 n
->parent
->body
= n
;
1060 assert(n
->parent
->type
== ROFFT_BLOCK
);
1061 n
->parent
->tail
= n
;
1070 roff_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
1072 struct roff_node
*n
;
1074 n
= roff_node_alloc(man
, line
, pos
, ROFFT_TEXT
, TOKEN_NONE
);
1075 n
->string
= roff_strdup(man
->roff
, word
);
1076 roff_node_append(man
, n
);
1077 if (man
->macroset
== MACROSET_MDOC
)
1078 mdoc_valid_post(man
);
1080 man_valid_post(man
);
1081 man
->next
= ROFF_NEXT_SIBLING
;
1085 roff_word_append(struct roff_man
*man
, const char *word
)
1087 struct roff_node
*n
;
1088 char *addstr
, *newstr
;
1091 addstr
= roff_strdup(man
->roff
, word
);
1092 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
1096 man
->next
= ROFF_NEXT_SIBLING
;
1100 roff_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1102 struct roff_node
*n
;
1104 n
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
1105 roff_node_append(man
, n
);
1106 man
->next
= ROFF_NEXT_CHILD
;
1110 roff_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1112 struct roff_node
*n
;
1114 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
1115 roff_node_append(man
, n
);
1116 man
->next
= ROFF_NEXT_CHILD
;
1121 roff_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1123 struct roff_node
*n
;
1125 n
= roff_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
1126 roff_node_append(man
, n
);
1127 man
->next
= ROFF_NEXT_CHILD
;
1132 roff_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
1134 struct roff_node
*n
;
1136 n
= roff_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
1137 roff_node_append(man
, n
);
1138 man
->next
= ROFF_NEXT_CHILD
;
1143 roff_addeqn(struct roff_man
*man
, const struct eqn
*eqn
)
1145 struct roff_node
*n
;
1147 n
= roff_node_alloc(man
, eqn
->ln
, eqn
->pos
, ROFFT_EQN
, TOKEN_NONE
);
1149 if (eqn
->ln
> man
->last
->line
)
1150 n
->flags
|= MDOC_LINE
;
1151 roff_node_append(man
, n
);
1152 man
->next
= ROFF_NEXT_SIBLING
;
1156 roff_addtbl(struct roff_man
*man
, const struct tbl_span
*tbl
)
1158 struct roff_node
*n
;
1160 if (man
->macroset
== MACROSET_MAN
)
1161 man_breakscope(man
, TOKEN_NONE
);
1162 n
= roff_node_alloc(man
, tbl
->line
, 0, ROFFT_TBL
, TOKEN_NONE
);
1164 roff_node_append(man
, n
);
1165 if (man
->macroset
== MACROSET_MDOC
)
1166 mdoc_valid_post(man
);
1168 man_valid_post(man
);
1169 man
->next
= ROFF_NEXT_SIBLING
;
1173 roff_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
1176 /* Adjust siblings. */
1179 n
->prev
->next
= n
->next
;
1181 n
->next
->prev
= n
->prev
;
1183 /* Adjust parent. */
1185 if (n
->parent
!= NULL
) {
1186 n
->parent
->nchild
--;
1187 if (n
->parent
->child
== n
)
1188 n
->parent
->child
= n
->next
;
1189 if (n
->parent
->last
== n
)
1190 n
->parent
->last
= n
->prev
;
1193 /* Adjust parse point. */
1197 if (man
->last
== n
) {
1198 if (n
->prev
== NULL
) {
1199 man
->last
= n
->parent
;
1200 man
->next
= ROFF_NEXT_CHILD
;
1202 man
->last
= n
->prev
;
1203 man
->next
= ROFF_NEXT_SIBLING
;
1206 if (man
->first
== n
)
1211 roff_node_free(struct roff_node
*n
)
1214 if (n
->args
!= NULL
)
1215 mdoc_argv_free(n
->args
);
1216 if (n
->type
== ROFFT_BLOCK
|| n
->type
== ROFFT_ELEM
)
1223 roff_node_delete(struct roff_man
*man
, struct roff_node
*n
)
1226 while (n
->child
!= NULL
)
1227 roff_node_delete(man
, n
->child
);
1228 assert(n
->nchild
== 0);
1229 roff_node_unlink(man
, n
);
1233 /* --- main functions of the roff parser ---------------------------------- */
1236 * In the current line, expand escape sequences that tend to get
1237 * used in numerical expressions and conditional requests.
1238 * Also check the syntax of the remaining escape sequences.
1241 roff_res(struct roff
*r
, struct buf
*buf
, int ln
, int pos
)
1243 char ubuf
[24]; /* buffer to print the number */
1244 const char *start
; /* start of the string to process */
1245 char *stesc
; /* start of an escape sequence ('\\') */
1246 const char *stnam
; /* start of the name, after "[(*" */
1247 const char *cp
; /* end of the name, e.g. before ']' */
1248 const char *res
; /* the string to be substituted */
1249 char *nbuf
; /* new buffer to copy buf->buf to */
1250 size_t maxl
; /* expected length of the escape name */
1251 size_t naml
; /* actual length of the escape name */
1252 enum mandoc_esc esc
; /* type of the escape sequence */
1253 int inaml
; /* length returned from mandoc_escape() */
1254 int expand_count
; /* to avoid infinite loops */
1255 int npos
; /* position in numeric expression */
1256 int arg_complete
; /* argument not interrupted by eol */
1257 char term
; /* character terminating the escape */
1260 start
= buf
->buf
+ pos
;
1261 stesc
= strchr(start
, '\0') - 1;
1262 while (stesc
-- > start
) {
1264 /* Search backwards for the next backslash. */
1269 /* If it is escaped, skip it. */
1271 for (cp
= stesc
- 1; cp
>= start
; cp
--)
1275 if ((stesc
- cp
) % 2 == 0) {
1280 /* Decide whether to expand or to check only. */
1297 esc
= mandoc_escape(&cp
, &stnam
, &inaml
);
1298 if (esc
== ESCAPE_ERROR
||
1299 (esc
== ESCAPE_SPECIAL
&&
1300 mchars_spec2cp(r
->mchars
, stnam
, inaml
) < 0))
1301 mandoc_vmsg(MANDOCERR_ESC_BAD
,
1302 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1303 "%.*s", (int)(cp
- stesc
), stesc
);
1307 if (EXPAND_LIMIT
< ++expand_count
) {
1308 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1309 ln
, (int)(stesc
- buf
->buf
), NULL
);
1314 * The third character decides the length
1315 * of the name of the string or register.
1316 * Save a pointer to the name.
1343 /* Advance to the end of the name. */
1347 while (maxl
== 0 || naml
< maxl
) {
1349 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
1350 ln
, (int)(stesc
- buf
->buf
), stesc
);
1354 if (maxl
== 0 && *cp
== term
) {
1358 if (*cp
++ != '\\' || stesc
[1] != 'w') {
1362 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1363 case ESCAPE_SPECIAL
:
1365 case ESCAPE_UNICODE
:
1367 case ESCAPE_NUMBERED
:
1369 case ESCAPE_OVERSTRIKE
:
1378 * Retrieve the replacement string; if it is
1379 * undefined, resume searching for escapes.
1385 res
= roff_getstrn(r
, stnam
, naml
);
1389 ubuf
[0] = arg_complete
&&
1390 roff_evalnum(r
, ln
, stnam
, &npos
,
1391 NULL
, ROFFNUM_SCALE
) &&
1392 stnam
+ npos
+ 1 == cp
? '1' : '0';
1397 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1398 roff_getregn(r
, stnam
, naml
));
1403 /* use even incomplete args */
1404 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1410 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1411 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1412 "%.*s", (int)naml
, stnam
);
1414 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1415 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1416 ln
, (int)(stesc
- buf
->buf
), NULL
);
1420 /* Replace the escape sequence by the string. */
1423 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1424 buf
->buf
, res
, cp
) + 1;
1426 /* Prepare for the next replacement. */
1429 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1437 * Process text streams:
1438 * Convert all breakable hyphens into ASCII_HYPH.
1439 * Decrement and spring input line trap.
1442 roff_parsetext(struct buf
*buf
, int pos
, int *offs
)
1448 enum mandoc_esc esc
;
1450 start
= p
= buf
->buf
+ pos
;
1452 while (*p
!= '\0') {
1453 sz
= strcspn(p
, "-\\");
1460 /* Skip over escapes. */
1462 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1463 if (esc
== ESCAPE_ERROR
)
1468 } else if (p
== start
) {
1473 if (isalpha((unsigned char)p
[-1]) &&
1474 isalpha((unsigned char)p
[1]))
1479 /* Spring the input line trap. */
1480 if (roffit_lines
== 1) {
1481 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1488 return(ROFF_REPARSE
);
1489 } else if (roffit_lines
> 1)
1495 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
1499 int pos
; /* parse point */
1500 int spos
; /* saved parse point for messages */
1501 int ppos
; /* original offset in buf->buf */
1502 int ctl
; /* macro line (boolean) */
1506 /* Handle in-line equation delimiters. */
1508 if (r
->tbl
== NULL
&&
1509 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1510 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1511 e
= roff_eqndelim(r
, buf
, pos
);
1512 if (e
== ROFF_REPARSE
)
1514 assert(e
== ROFF_CONT
);
1517 /* Expand some escape sequences. */
1519 e
= roff_res(r
, buf
, ln
, pos
);
1522 assert(e
== ROFF_CONT
);
1524 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1527 * First, if a scope is open and we're not a macro, pass the
1528 * text through the macro's filter.
1529 * Equations process all content themselves.
1530 * Tables process almost all content themselves, but we want
1531 * to warn about macros before passing it there.
1534 if (r
->last
!= NULL
&& ! ctl
) {
1536 assert(roffs
[t
].text
);
1537 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1538 assert(e
== ROFF_IGN
|| e
== ROFF_CONT
);
1543 return(eqn_read(&r
->eqn
, ln
, buf
->buf
, ppos
, offs
));
1544 if (r
->tbl
!= NULL
&& ( ! ctl
|| buf
->buf
[pos
] == '\0'))
1545 return(tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
));
1547 return(roff_parsetext(buf
, pos
, offs
));
1549 /* Skip empty request lines. */
1551 if (buf
->buf
[pos
] == '"') {
1552 mandoc_msg(MANDOCERR_COMMENT_BAD
, r
->parse
,
1555 } else if (buf
->buf
[pos
] == '\0')
1559 * If a scope is open, go to the child handler for that macro,
1560 * as it may want to preprocess before doing anything with it.
1561 * Don't do so if an equation is open.
1566 assert(roffs
[t
].sub
);
1567 return((*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1570 /* No scope is open. This is a new request or macro. */
1573 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1575 /* Tables ignore most macros. */
1577 if (r
->tbl
!= NULL
&& (t
== ROFF_MAX
|| t
== ROFF_TS
)) {
1578 mandoc_msg(MANDOCERR_TBLMACRO
, r
->parse
,
1579 ln
, pos
, buf
->buf
+ spos
);
1582 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1584 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] == ' ')
1586 return(tbl_read(r
->tbl
, ln
, buf
->buf
, pos
));
1590 * This is neither a roff request nor a user-defined macro.
1591 * Let the standard macro set parsers handle it.
1597 /* Execute a roff request or a user defined macro. */
1599 assert(roffs
[t
].proc
);
1600 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1604 roff_endparse(struct roff
*r
)
1608 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1609 r
->last
->line
, r
->last
->col
,
1610 roffs
[r
->last
->tok
].name
);
1613 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1614 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, "EQ");
1619 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1620 r
->tbl
->line
, r
->tbl
->pos
, "TS");
1626 * Parse a roff node's type from the input buffer. This must be in the
1627 * form of ".foo xxx" in the usual way.
1630 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1639 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1643 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1645 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
1646 ? ROFF_USERDEF
: roffhash_find(mac
, maclen
);
1654 /* --- handling of request blocks ----------------------------------------- */
1657 roff_cblock(ROFF_ARGS
)
1661 * A block-close `..' should only be invoked as a child of an
1662 * ignore macro, otherwise raise a warning and just ignore it.
1665 if (r
->last
== NULL
) {
1666 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1671 switch (r
->last
->tok
) {
1673 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1678 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1685 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1690 if (buf
->buf
[pos
] != '\0')
1691 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
1692 ".. %s", buf
->buf
+ pos
);
1695 roffnode_cleanscope(r
);
1701 roffnode_cleanscope(struct roff
*r
)
1705 if (--r
->last
->endspan
!= 0)
1712 roff_ccond(struct roff
*r
, int ln
, int ppos
)
1715 if (NULL
== r
->last
) {
1716 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1721 switch (r
->last
->tok
) {
1729 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1734 if (r
->last
->endspan
> -1) {
1735 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1741 roffnode_cleanscope(r
);
1746 roff_block(ROFF_ARGS
)
1752 /* Ignore groff compatibility mode for now. */
1754 if (tok
== ROFF_de1
)
1756 else if (tok
== ROFF_dei1
)
1758 else if (tok
== ROFF_am1
)
1760 else if (tok
== ROFF_ami1
)
1763 /* Parse the macro name argument. */
1765 cp
= buf
->buf
+ pos
;
1766 if (tok
== ROFF_ig
) {
1771 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1772 iname
[namesz
] = '\0';
1775 /* Resolve the macro name argument if it is indirect. */
1777 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1778 if ((name
= roff_getstrn(r
, iname
, namesz
)) == NULL
) {
1779 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1780 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1781 "%.*s", (int)namesz
, iname
);
1784 namesz
= strlen(name
);
1788 if (namesz
== 0 && tok
!= ROFF_ig
) {
1789 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
,
1790 ln
, ppos
, roffs
[tok
].name
);
1794 roffnode_push(r
, tok
, name
, ln
, ppos
);
1797 * At the beginning of a `de' macro, clear the existing string
1798 * with the same name, if there is one. New content will be
1799 * appended from roff_block_text() in multiline mode.
1802 if (tok
== ROFF_de
|| tok
== ROFF_dei
)
1803 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
1808 /* Get the custom end marker. */
1811 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1813 /* Resolve the end marker if it is indirect. */
1815 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1816 if ((name
= roff_getstrn(r
, iname
, namesz
)) == NULL
) {
1817 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1818 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1819 "%.*s", (int)namesz
, iname
);
1822 namesz
= strlen(name
);
1827 r
->last
->end
= mandoc_strndup(name
, namesz
);
1830 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
1831 ln
, pos
, ".%s ... %s", roffs
[tok
].name
, cp
);
1837 roff_block_sub(ROFF_ARGS
)
1843 * First check whether a custom macro exists at this level. If
1844 * it does, then check against it. This is some of groff's
1845 * stranger behaviours. If we encountered a custom end-scope
1846 * tag and that tag also happens to be a "real" macro, then we
1847 * need to try interpreting it again as a real macro. If it's
1848 * not, then return ignore. Else continue.
1852 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
1853 if (buf
->buf
[i
] != r
->last
->end
[j
])
1856 if (r
->last
->end
[j
] == '\0' &&
1857 (buf
->buf
[i
] == '\0' ||
1858 buf
->buf
[i
] == ' ' ||
1859 buf
->buf
[i
] == '\t')) {
1861 roffnode_cleanscope(r
);
1863 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
1867 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
1875 * If we have no custom end-query or lookup failed, then try
1876 * pulling it out of the hashtable.
1879 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1881 if (t
!= ROFF_cblock
) {
1883 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
1887 assert(roffs
[t
].proc
);
1888 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1892 roff_block_text(ROFF_ARGS
)
1896 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
1902 roff_cond_sub(ROFF_ARGS
)
1909 roffnode_cleanscope(r
);
1910 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1913 * Fully handle known macros when they are structurally
1914 * required or when the conditional evaluated to true.
1917 if ((t
!= ROFF_MAX
) &&
1918 (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
)) {
1919 assert(roffs
[t
].proc
);
1920 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1924 * If `\}' occurs on a macro line without a preceding macro,
1925 * drop the line completely.
1928 ep
= buf
->buf
+ pos
;
1929 if (ep
[0] == '\\' && ep
[1] == '}')
1932 /* Always check for the closing delimiter `\}'. */
1934 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1935 if (*(++ep
) == '}') {
1937 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1942 return(rr
? ROFF_CONT
: ROFF_IGN
);
1946 roff_cond_text(ROFF_ARGS
)
1952 roffnode_cleanscope(r
);
1954 ep
= buf
->buf
+ pos
;
1955 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1956 if (*(++ep
) == '}') {
1958 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1963 return(rr
? ROFF_CONT
: ROFF_IGN
);
1966 /* --- handling of numeric and conditional expressions -------------------- */
1969 * Parse a single signed integer number. Stop at the first non-digit.
1970 * If there is at least one digit, return success and advance the
1971 * parse point, else return failure and let the parse point unchanged.
1972 * Ignore overflows, treat them just like the C language.
1975 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
1977 int myres
, scaled
, n
, p
;
1984 if (n
|| v
[p
] == '+')
1987 if (flags
& ROFFNUM_WHITE
)
1988 while (isspace((unsigned char)v
[p
]))
1991 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
1992 *res
= 10 * *res
+ v
[p
] - '0';
1999 /* Each number may be followed by one optional scaling unit. */
2003 scaled
= *res
* 65536;
2006 scaled
= *res
* 240;
2009 scaled
= *res
* 240 / 2.54;
2022 scaled
= *res
* 10 / 3;
2028 scaled
= *res
* 6 / 25;
2035 if (flags
& ROFFNUM_SCALE
)
2043 * Evaluate a string comparison condition.
2044 * The first character is the delimiter.
2045 * Succeed if the string up to its second occurrence
2046 * matches the string up to its third occurence.
2047 * Advance the cursor after the third occurrence
2048 * or lacking that, to the end of the line.
2051 roff_evalstrcond(const char *v
, int *pos
)
2053 const char *s1
, *s2
, *s3
;
2057 s1
= v
+ *pos
; /* initial delimiter */
2058 s2
= s1
+ 1; /* for scanning the first string */
2059 s3
= strchr(s2
, *s1
); /* for scanning the second string */
2061 if (NULL
== s3
) /* found no middle delimiter */
2064 while ('\0' != *++s3
) {
2065 if (*s2
!= *s3
) { /* mismatch */
2066 s3
= strchr(s3
, *s1
);
2069 if (*s3
== *s1
) { /* found the final delimiter */
2078 s3
= strchr(s2
, '\0');
2079 else if (*s3
!= '\0')
2086 * Evaluate an optionally negated single character, numerical,
2087 * or string condition.
2090 roff_evalcond(struct roff
*r
, int ln
, const char *v
, int *pos
)
2092 int number
, savepos
, wanttrue
;
2094 if ('!' == v
[*pos
]) {
2126 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
2127 return((number
> 0) == wanttrue
);
2128 else if (*pos
== savepos
)
2129 return(roff_evalstrcond(v
, pos
) == wanttrue
);
2135 roff_line_ignore(ROFF_ARGS
)
2142 roff_insec(ROFF_ARGS
)
2145 mandoc_msg(MANDOCERR_REQ_INSEC
, r
->parse
,
2146 ln
, ppos
, roffs
[tok
].name
);
2151 roff_unsupp(ROFF_ARGS
)
2154 mandoc_msg(MANDOCERR_REQ_UNSUPP
, r
->parse
,
2155 ln
, ppos
, roffs
[tok
].name
);
2160 roff_cond(ROFF_ARGS
)
2163 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
2166 * An `.el' has no conditional body: it will consume the value
2167 * of the current rstack entry set in prior `ie' calls or
2170 * If we're not an `el', however, then evaluate the conditional.
2173 r
->last
->rule
= tok
== ROFF_el
?
2174 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
2175 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
2178 * An if-else will put the NEGATION of the current evaluated
2179 * conditional into the stack of rules.
2182 if (tok
== ROFF_ie
) {
2183 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
2185 r
->rstack
= mandoc_reallocarray(r
->rstack
,
2186 r
->rstacksz
, sizeof(int));
2188 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
2191 /* If the parent has false as its rule, then so do we. */
2193 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
2198 * If there is nothing on the line after the conditional,
2199 * not even whitespace, use next-line scope.
2202 if (buf
->buf
[pos
] == '\0') {
2203 r
->last
->endspan
= 2;
2207 while (buf
->buf
[pos
] == ' ')
2210 /* An opening brace requests multiline scope. */
2212 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
2213 r
->last
->endspan
= -1;
2219 * Anything else following the conditional causes
2220 * single-line scope. Warn if the scope contains
2221 * nothing but trailing whitespace.
2224 if (buf
->buf
[pos
] == '\0')
2225 mandoc_msg(MANDOCERR_COND_EMPTY
, r
->parse
,
2226 ln
, ppos
, roffs
[tok
].name
);
2228 r
->last
->endspan
= 1;
2242 /* Ignore groff compatibility mode for now. */
2244 if (tok
== ROFF_ds1
)
2246 else if (tok
== ROFF_as1
)
2250 * The first word is the name of the string.
2251 * If it is empty or terminated by an escape sequence,
2252 * abort the `ds' request without defining anything.
2255 name
= string
= buf
->buf
+ pos
;
2259 namesz
= roff_getname(r
, &string
, ln
, pos
);
2260 if (name
[namesz
] == '\\')
2263 /* Read past the initial double-quote, if any. */
2267 /* The rest is the value. */
2268 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
2274 * Parse a single operator, one or two characters long.
2275 * If the operator is recognized, return success and advance the
2276 * parse point, else return failure and let the parse point unchanged.
2279 roff_getop(const char *v
, int *pos
, char *res
)
2300 switch (v
[*pos
+ 1]) {
2318 switch (v
[*pos
+ 1]) {
2332 if ('=' == v
[*pos
+ 1])
2344 * Evaluate either a parenthesized numeric expression
2345 * or a single signed integer number.
2348 roff_evalpar(struct roff
*r
, int ln
,
2349 const char *v
, int *pos
, int *res
, int flags
)
2353 return(roff_getnum(v
, pos
, res
, flags
));
2356 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2360 * Omission of the closing parenthesis
2361 * is an error in validation mode,
2362 * but ignored in evaluation mode.
2367 else if (NULL
== res
)
2374 * Evaluate a complete numeric expression.
2375 * Proceed left to right, there is no concept of precedence.
2378 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2379 int *pos
, int *res
, int flags
)
2381 int mypos
, operand2
;
2389 if (flags
& ROFFNUM_WHITE
)
2390 while (isspace((unsigned char)v
[*pos
]))
2393 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2397 if (flags
& ROFFNUM_WHITE
)
2398 while (isspace((unsigned char)v
[*pos
]))
2401 if ( ! roff_getop(v
, pos
, &operator))
2404 if (flags
& ROFFNUM_WHITE
)
2405 while (isspace((unsigned char)v
[*pos
]))
2408 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2411 if (flags
& ROFFNUM_WHITE
)
2412 while (isspace((unsigned char)v
[*pos
]))
2429 if (operand2
== 0) {
2430 mandoc_msg(MANDOCERR_DIVZERO
,
2431 r
->parse
, ln
, *pos
, v
);
2438 if (operand2
== 0) {
2439 mandoc_msg(MANDOCERR_DIVZERO
,
2440 r
->parse
, ln
, *pos
, v
);
2447 *res
= *res
< operand2
;
2450 *res
= *res
> operand2
;
2453 *res
= *res
<= operand2
;
2456 *res
= *res
>= operand2
;
2459 *res
= *res
== operand2
;
2462 *res
= *res
!= operand2
;
2465 *res
= *res
&& operand2
;
2468 *res
= *res
|| operand2
;
2471 if (operand2
< *res
)
2475 if (operand2
> *res
)
2485 /* --- register management ------------------------------------------------ */
2488 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2490 struct roffreg
*reg
;
2492 /* Search for an existing register with the same name. */
2495 while (reg
&& strcmp(name
, reg
->key
.p
))
2499 /* Create a new register. */
2500 reg
= mandoc_malloc(sizeof(struct roffreg
));
2501 reg
->key
.p
= mandoc_strdup(name
);
2502 reg
->key
.sz
= strlen(name
);
2504 reg
->next
= r
->regtab
;
2510 else if ('-' == sign
)
2517 * Handle some predefined read-only number registers.
2518 * For now, return -1 if the requested register is not predefined;
2519 * in case a predefined read-only register having the value -1
2520 * were to turn up, another special value would have to be chosen.
2523 roff_getregro(const char *name
)
2527 case 'A': /* ASCII approximation mode is always off. */
2529 case 'g': /* Groff compatibility mode is always on. */
2531 case 'H': /* Fixed horizontal resolution. */
2533 case 'j': /* Always adjust left margin only. */
2535 case 'T': /* Some output device is always defined. */
2537 case 'V': /* Fixed vertical resolution. */
2545 roff_getreg(const struct roff
*r
, const char *name
)
2547 struct roffreg
*reg
;
2550 if ('.' == name
[0] && '\0' != name
[1] && '\0' == name
[2]) {
2551 val
= roff_getregro(name
+ 1);
2556 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2557 if (0 == strcmp(name
, reg
->key
.p
))
2564 roff_getregn(const struct roff
*r
, const char *name
, size_t len
)
2566 struct roffreg
*reg
;
2569 if ('.' == name
[0] && 2 == len
) {
2570 val
= roff_getregro(name
+ 1);
2575 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2576 if (len
== reg
->key
.sz
&&
2577 0 == strncmp(name
, reg
->key
.p
, len
))
2584 roff_freereg(struct roffreg
*reg
)
2586 struct roffreg
*old_reg
;
2588 while (NULL
!= reg
) {
2604 key
= val
= buf
->buf
+ pos
;
2608 keysz
= roff_getname(r
, &val
, ln
, pos
);
2609 if (key
[keysz
] == '\\')
2614 if (sign
== '+' || sign
== '-')
2617 if (roff_evalnum(r
, ln
, val
, NULL
, &iv
, ROFFNUM_SCALE
))
2618 roff_setreg(r
, key
, iv
, sign
);
2626 struct roffreg
*reg
, **prev
;
2630 name
= cp
= buf
->buf
+ pos
;
2633 namesz
= roff_getname(r
, &cp
, ln
, pos
);
2634 name
[namesz
] = '\0';
2639 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
2651 /* --- handler functions for roff requests -------------------------------- */
2660 cp
= buf
->buf
+ pos
;
2661 while (*cp
!= '\0') {
2663 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
2664 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
2665 if (name
[namesz
] == '\\')
2676 /* Parse the number of lines. */
2678 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
2679 mandoc_msg(MANDOCERR_IT_NONUM
, r
->parse
,
2680 ln
, ppos
, buf
->buf
+ 1);
2684 while (isspace((unsigned char)buf
->buf
[pos
]))
2688 * Arm the input line trap.
2689 * Special-casing "an-trap" is an ugly workaround to cope
2690 * with DocBook stupidly fiddling with man(7) internals.
2694 roffit_macro
= mandoc_strdup(iv
!= 1 ||
2695 strcmp(buf
->buf
+ pos
, "an-trap") ?
2696 buf
->buf
+ pos
: "br");
2703 const char *const *cp
;
2705 if ((r
->options
& (MPARSE_MDOC
| MPARSE_QUICK
)) == 0)
2706 for (cp
= __mdoc_reserved
; *cp
; cp
++)
2707 roff_setstr(r
, *cp
, NULL
, 0);
2710 r
->format
= MPARSE_MDOC
;
2718 const char *const *cp
;
2720 if ((r
->options
& MPARSE_QUICK
) == 0)
2721 for (cp
= __man_reserved
; *cp
; cp
++)
2722 roff_setstr(r
, *cp
, NULL
, 0);
2725 r
->format
= MPARSE_MAN
;
2735 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2737 else if ( ! tbl_end(&r
->tbl
)) {
2739 buf
->buf
= mandoc_strdup(".sp");
2741 return(ROFF_REPARSE
);
2751 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2754 tbl_restart(ppos
, ln
, r
->tbl
);
2760 * Handle in-line equation delimiters.
2763 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
2766 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
2769 * Outside equations, look for an opening delimiter.
2770 * If we are inside an equation, we already know it is
2771 * in-line, or this function wouldn't have been called;
2772 * so look for a closing delimiter.
2775 cp1
= buf
->buf
+ pos
;
2776 cp2
= strchr(cp1
, r
->eqn
== NULL
?
2777 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
2782 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
2784 /* Handle preceding text, protecting whitespace. */
2786 if (*buf
->buf
!= '\0') {
2793 * Prepare replacing the delimiter with an equation macro
2794 * and drop leading white space from the equation.
2797 if (r
->eqn
== NULL
) {
2804 /* Handle following text, protecting whitespace. */
2812 /* Do the actual replacement. */
2814 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
2815 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
2819 /* Toggle the in-line state of the eqn subsystem. */
2821 r
->eqn_inline
= r
->eqn
== NULL
;
2822 return(ROFF_REPARSE
);
2830 assert(r
->eqn
== NULL
);
2831 e
= eqn_alloc(ppos
, ln
, r
->parse
);
2834 r
->last_eqn
->next
= e
;
2835 e
->delim
= r
->last_eqn
->delim
;
2836 e
->odelim
= r
->last_eqn
->odelim
;
2837 e
->cdelim
= r
->last_eqn
->cdelim
;
2839 r
->first_eqn
= r
->last_eqn
= e
;
2841 r
->eqn
= r
->last_eqn
= e
;
2843 if (buf
->buf
[pos
] != '\0')
2844 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
2845 ".EQ %s", buf
->buf
+ pos
);
2854 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
, ln
, ppos
, "EN");
2861 struct tbl_node
*tbl
;
2864 mandoc_msg(MANDOCERR_BLK_BROKEN
, r
->parse
,
2865 ln
, ppos
, "TS breaks TS");
2869 tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
2872 r
->last_tbl
->next
= tbl
;
2874 r
->first_tbl
= r
->last_tbl
= tbl
;
2876 r
->tbl
= r
->last_tbl
= tbl
;
2884 buf
->buf
[pos
- 1] = '\0';
2895 if (*p
== '\0' || (r
->control
= *p
++) == '.')
2899 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
2900 ln
, p
- buf
->buf
, "cc ... %s", p
);
2908 const char *p
, *first
, *second
;
2910 enum mandoc_esc esc
;
2915 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
, ln
, ppos
, "tr");
2919 while (*p
!= '\0') {
2923 if (*first
== '\\') {
2924 esc
= mandoc_escape(&p
, NULL
, NULL
);
2925 if (esc
== ESCAPE_ERROR
) {
2926 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
2927 ln
, (int)(p
- buf
->buf
), first
);
2930 fsz
= (size_t)(p
- first
);
2934 if (*second
== '\\') {
2935 esc
= mandoc_escape(&p
, NULL
, NULL
);
2936 if (esc
== ESCAPE_ERROR
) {
2937 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
2938 ln
, (int)(p
- buf
->buf
), second
);
2941 ssz
= (size_t)(p
- second
);
2942 } else if (*second
== '\0') {
2943 mandoc_vmsg(MANDOCERR_TR_ODD
, r
->parse
,
2944 ln
, first
- buf
->buf
, "tr %s", first
);
2950 roff_setstrn(&r
->xmbtab
, first
, fsz
,
2955 if (r
->xtab
== NULL
)
2956 r
->xtab
= mandoc_calloc(128,
2957 sizeof(struct roffstr
));
2959 free(r
->xtab
[(int)*first
].p
);
2960 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
2961 r
->xtab
[(int)*first
].sz
= ssz
;
2972 name
= buf
->buf
+ pos
;
2973 mandoc_vmsg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, "so %s", name
);
2976 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
2977 * opening anything that's not in our cwd or anything beneath
2978 * it. Thus, explicitly disallow traversing up the file-system
2979 * or using absolute paths.
2982 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
2983 mandoc_vmsg(MANDOCERR_SO_PATH
, r
->parse
, ln
, ppos
,
2985 buf
->sz
= mandoc_asprintf(&cp
,
2986 ".sp\nSee the file %s.\n.sp", name
) + 1;
2990 return(ROFF_REPARSE
);
2997 /* --- user defined strings and macros ------------------------------------ */
3000 roff_userdef(ROFF_ARGS
)
3002 const char *arg
[9], *ap
;
3008 * Collect pointers to macro argument strings
3009 * and NUL-terminate them.
3012 cp
= buf
->buf
+ pos
;
3013 for (i
= 0; i
< 9; i
++)
3014 arg
[i
] = *cp
== '\0' ? "" :
3015 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
3018 * Expand macro arguments.
3021 buf
->sz
= strlen(r
->current_string
) + 1;
3022 n1
= cp
= mandoc_malloc(buf
->sz
);
3023 memcpy(n1
, r
->current_string
, buf
->sz
);
3024 while (*cp
!= '\0') {
3026 /* Scan ahead for the next argument invocation. */
3038 * Determine the size of the expanded argument,
3039 * taking escaping of quotes into account.
3043 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3051 * Determine the size of the rest of the
3052 * unexpanded macro, including the NUL.
3055 rsz
= buf
->sz
- (cp
- n1
) - 3;
3058 * When shrinking, move before
3059 * releasing the storage.
3063 memmove(cp
+ asz
, cp
+ 3, rsz
);
3066 * Resize the storage for the macro
3067 * and readjust the parse pointer.
3071 n2
= mandoc_realloc(n1
, buf
->sz
);
3072 cp
= n2
+ (cp
- n1
);
3076 * When growing, make room
3077 * for the expanded argument.
3081 memmove(cp
+ asz
, cp
+ 3, rsz
);
3084 /* Copy the expanded argument, escaping quotes. */
3087 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
3089 memcpy(n2
, "\\(dq", 4);
3097 * Replace the macro invocation
3098 * by the expanded macro.
3105 return(buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
3106 ROFF_REPARSE
: ROFF_APPEND
);
3110 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
3119 /* Read until end of name and terminate it with NUL. */
3120 for (cp
= name
; 1; cp
++) {
3121 if ('\0' == *cp
|| ' ' == *cp
) {
3128 if ('{' == cp
[1] || '}' == cp
[1])
3133 mandoc_vmsg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
,
3134 "%.*s", (int)(cp
- name
+ 1), name
);
3135 mandoc_escape((const char **)&cp
, NULL
, NULL
);
3139 /* Read past spaces. */
3148 * Store *string into the user-defined string called *name.
3149 * To clear an existing entry, call with (*r, *name, NULL, 0).
3150 * append == 0: replace mode
3151 * append == 1: single-line append mode
3152 * append == 2: multiline append mode, append '\n' after each call
3155 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
3159 roff_setstrn(&r
->strtab
, name
, strlen(name
), string
,
3160 string
? strlen(string
) : 0, append
);
3164 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
3165 const char *string
, size_t stringsz
, int append
)
3170 size_t oldch
, newch
;
3172 /* Search for an existing string with the same name. */
3175 while (n
&& (namesz
!= n
->key
.sz
||
3176 strncmp(n
->key
.p
, name
, namesz
)))
3180 /* Create a new string table entry. */
3181 n
= mandoc_malloc(sizeof(struct roffkv
));
3182 n
->key
.p
= mandoc_strndup(name
, namesz
);
3188 } else if (0 == append
) {
3198 * One additional byte for the '\n' in multiline mode,
3199 * and one for the terminating '\0'.
3201 newch
= stringsz
+ (1 < append
? 2u : 1u);
3203 if (NULL
== n
->val
.p
) {
3204 n
->val
.p
= mandoc_malloc(newch
);
3209 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
3212 /* Skip existing content in the destination buffer. */
3213 c
= n
->val
.p
+ (int)oldch
;
3215 /* Append new content to the destination buffer. */
3217 while (i
< (int)stringsz
) {
3219 * Rudimentary roff copy mode:
3220 * Handle escaped backslashes.
3222 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
3227 /* Append terminating bytes. */
3232 n
->val
.sz
= (int)(c
- n
->val
.p
);
3236 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
3238 const struct roffkv
*n
;
3241 for (n
= r
->strtab
; n
; n
= n
->next
)
3242 if (0 == strncmp(name
, n
->key
.p
, len
) &&
3243 '\0' == n
->key
.p
[(int)len
])
3246 for (i
= 0; i
< PREDEFS_MAX
; i
++)
3247 if (0 == strncmp(name
, predefs
[i
].name
, len
) &&
3248 '\0' == predefs
[i
].name
[(int)len
])
3249 return(predefs
[i
].str
);
3255 roff_freestr(struct roffkv
*r
)
3257 struct roffkv
*n
, *nn
;
3259 for (n
= r
; n
; n
= nn
) {
3267 /* --- accessors and utility functions ------------------------------------ */
3269 const struct tbl_span
*
3270 roff_span(const struct roff
*r
)
3273 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
3277 roff_eqn(const struct roff
*r
)
3280 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);
3284 * Duplicate an input string, making the appropriate character
3285 * conversations (as stipulated by `tr') along the way.
3286 * Returns a heap-allocated string with all the replacements made.
3289 roff_strdup(const struct roff
*r
, const char *p
)
3291 const struct roffkv
*cp
;
3295 enum mandoc_esc esc
;
3297 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
3298 return(mandoc_strdup(p
));
3299 else if ('\0' == *p
)
3300 return(mandoc_strdup(""));
3303 * Step through each character looking for term matches
3304 * (remember that a `tr' can be invoked with an escape, which is
3305 * a glyph but the escape is multi-character).
3306 * We only do this if the character hash has been initialised
3307 * and the string is >0 length.
3313 while ('\0' != *p
) {
3314 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(int)*p
].p
) {
3315 sz
= r
->xtab
[(int)*p
].sz
;
3316 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3317 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
3321 } else if ('\\' != *p
) {
3322 res
= mandoc_realloc(res
, ssz
+ 2);
3327 /* Search for term matches. */
3328 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
3329 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
3334 * A match has been found.
3335 * Append the match to the array and move
3336 * forward by its keysize.
3338 res
= mandoc_realloc(res
,
3339 ssz
+ cp
->val
.sz
+ 1);
3340 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
3342 p
+= (int)cp
->key
.sz
;
3347 * Handle escapes carefully: we need to copy
3348 * over just the escape itself, or else we might
3349 * do replacements within the escape itself.
3350 * Make sure to pass along the bogus string.
3353 esc
= mandoc_escape(&p
, NULL
, NULL
);
3354 if (ESCAPE_ERROR
== esc
) {
3356 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3357 memcpy(res
+ ssz
, pp
, sz
);
3361 * We bail out on bad escapes.
3362 * No need to warn: we already did so when
3363 * roff_res() was called.
3366 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3367 memcpy(res
+ ssz
, pp
, sz
);
3371 res
[(int)ssz
] = '\0';
3376 roff_getformat(const struct roff
*r
)
3383 * Find out whether a line is a macro line or not.
3384 * If it is, adjust the current position and return one; if it isn't,
3385 * return zero and don't change the current position.
3386 * If the control character has been set with `.cc', then let that grain
3388 * This is slighly contrary to groff, where using the non-breaking
3389 * control character when `cc' has been invoked will cause the
3390 * non-breaking macro contents to be printed verbatim.
3393 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
3399 if (0 != r
->control
&& cp
[pos
] == r
->control
)
3401 else if (0 != r
->control
)
3403 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
3405 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
3410 while (' ' == cp
[pos
] || '\t' == cp
[pos
])