]>
git.cameronkatri.com Git - mandoc.git/blob - roff.c
1 /* $Id: roff.c,v 1.263 2015/02/21 14:46:58 schwarze Exp $ */
3 * Copyright (c) 2010, 2011, 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"
31 #include "libmandoc.h"
34 /* Maximum number of nested if-else conditionals. */
35 #define RSTACK_MAX 128
37 /* Maximum number of string expansions per line, to break infinite loops. */
38 #define EXPAND_LIMIT 1000
121 /* MAN_fi; ignored in mdoc(7) */
130 /* MAN_ft; ignored in mdoc(7) */
152 /* MAN_in; ignored in mdoc(7) */
170 /* MAN_ll, MDOC_ll */
184 /* MAN_nf; ignored in mdoc(7) */
235 /* MAN_sp, MDOC_sp */
288 * An incredibly-simple string buffer.
291 char *p
; /* nil-terminated buffer */
292 size_t sz
; /* saved strlen(p) */
296 * A key-value roffstr pair as part of a singly-linked list.
301 struct roffkv
*next
; /* next in list */
305 * A single number register as part of a singly-linked list.
310 struct roffreg
*next
;
314 struct mparse
*parse
; /* parse point */
315 const struct mchars
*mchars
; /* character table */
316 struct roffnode
*last
; /* leaf of stack */
317 int *rstack
; /* stack of inverted `ie' values */
318 struct roffreg
*regtab
; /* number registers */
319 struct roffkv
*strtab
; /* user-defined strings & macros */
320 struct roffkv
*xmbtab
; /* multi-byte trans table (`tr') */
321 struct roffstr
*xtab
; /* single-byte trans table (`tr') */
322 const char *current_string
; /* value of last called user macro */
323 struct tbl_node
*first_tbl
; /* first table parsed */
324 struct tbl_node
*last_tbl
; /* last table parsed */
325 struct tbl_node
*tbl
; /* current table being parsed */
326 struct eqn_node
*last_eqn
; /* last equation parsed */
327 struct eqn_node
*first_eqn
; /* first equation parsed */
328 struct eqn_node
*eqn
; /* current equation being parsed */
329 int eqn_inline
; /* current equation is inline */
330 int options
; /* parse options */
331 int rstacksz
; /* current size limit of rstack */
332 int rstackpos
; /* position in rstack */
333 int format
; /* current file in mdoc or man format */
334 char control
; /* control character */
338 enum rofft tok
; /* type of node */
339 struct roffnode
*parent
; /* up one in stack */
340 int line
; /* parse line */
341 int col
; /* parse col */
342 char *name
; /* node name, e.g. macro name */
343 char *end
; /* end-rules: custom token */
344 int endspan
; /* end-rules: next-line or infty */
345 int rule
; /* current evaluation rule */
348 #define ROFF_ARGS struct roff *r, /* parse ctx */ \
349 enum rofft tok, /* tok of macro */ \
350 struct buf *buf, /* input buffer */ \
351 int ln, /* parse line */ \
352 int ppos, /* original pos in buffer */ \
353 int pos, /* current pos in buffer */ \
354 int *offs /* reset offset of buffer data */
356 typedef enum rofferr (*roffproc
)(ROFF_ARGS
);
359 const char *name
; /* macro name */
360 roffproc proc
; /* process new macro */
361 roffproc text
; /* process as child text of macro */
362 roffproc sub
; /* process as child of macro */
364 #define ROFFMAC_STRUCT (1 << 0) /* always interpret */
365 struct roffmac
*next
;
369 const char *name
; /* predefined input name */
370 const char *str
; /* replacement symbol */
373 #define PREDEF(__name, __str) \
374 { (__name), (__str) },
376 static enum rofft
roffhash_find(const char *, size_t);
377 static void roffhash_init(void);
378 static void roffnode_cleanscope(struct roff
*);
379 static void roffnode_pop(struct roff
*);
380 static void roffnode_push(struct roff
*, enum rofft
,
381 const char *, int, int);
382 static enum rofferr
roff_block(ROFF_ARGS
);
383 static enum rofferr
roff_block_text(ROFF_ARGS
);
384 static enum rofferr
roff_block_sub(ROFF_ARGS
);
385 static enum rofferr
roff_brp(ROFF_ARGS
);
386 static enum rofferr
roff_cblock(ROFF_ARGS
);
387 static enum rofferr
roff_cc(ROFF_ARGS
);
388 static void roff_ccond(struct roff
*, int, int);
389 static enum rofferr
roff_cond(ROFF_ARGS
);
390 static enum rofferr
roff_cond_text(ROFF_ARGS
);
391 static enum rofferr
roff_cond_sub(ROFF_ARGS
);
392 static enum rofferr
roff_ds(ROFF_ARGS
);
393 static enum rofferr
roff_eqndelim(struct roff
*, struct buf
*, int);
394 static int roff_evalcond(struct roff
*r
, int,
395 const char *, int *);
396 static int roff_evalnum(struct roff
*, int,
397 const char *, int *, int *, int);
398 static int roff_evalpar(struct roff
*, int,
399 const char *, int *, int *, int);
400 static int roff_evalstrcond(const char *, int *);
401 static void roff_free1(struct roff
*);
402 static void roff_freereg(struct roffreg
*);
403 static void roff_freestr(struct roffkv
*);
404 static size_t roff_getname(struct roff
*, char **, int, int);
405 static int roff_getnum(const char *, int *, int *, int);
406 static int roff_getop(const char *, int *, char *);
407 static int roff_getregn(const struct roff
*,
408 const char *, size_t);
409 static int roff_getregro(const char *name
);
410 static const char *roff_getstrn(const struct roff
*,
411 const char *, size_t);
412 static enum rofferr
roff_insec(ROFF_ARGS
);
413 static enum rofferr
roff_it(ROFF_ARGS
);
414 static enum rofferr
roff_line_ignore(ROFF_ARGS
);
415 static enum rofferr
roff_nr(ROFF_ARGS
);
416 static enum rofft
roff_parse(struct roff
*, char *, int *,
418 static enum rofferr
roff_parsetext(struct buf
*, int, int *);
419 static enum rofferr
roff_res(struct roff
*, struct buf
*, int, int);
420 static enum rofferr
roff_rm(ROFF_ARGS
);
421 static enum rofferr
roff_rr(ROFF_ARGS
);
422 static void roff_setstr(struct roff
*,
423 const char *, const char *, int);
424 static void roff_setstrn(struct roffkv
**, const char *,
425 size_t, const char *, size_t, int);
426 static enum rofferr
roff_so(ROFF_ARGS
);
427 static enum rofferr
roff_tr(ROFF_ARGS
);
428 static enum rofferr
roff_Dd(ROFF_ARGS
);
429 static enum rofferr
roff_TH(ROFF_ARGS
);
430 static enum rofferr
roff_TE(ROFF_ARGS
);
431 static enum rofferr
roff_TS(ROFF_ARGS
);
432 static enum rofferr
roff_EQ(ROFF_ARGS
);
433 static enum rofferr
roff_EN(ROFF_ARGS
);
434 static enum rofferr
roff_T_(ROFF_ARGS
);
435 static enum rofferr
roff_unsupp(ROFF_ARGS
);
436 static enum rofferr
roff_userdef(ROFF_ARGS
);
438 /* See roffhash_find() */
442 #define HASHWIDTH (ASCII_HI - ASCII_LO + 1)
444 #define ROFFNUM_SCALE (1 << 0) /* Honour scaling in roff_getnum(). */
445 #define ROFFNUM_WHITE (1 << 1) /* Skip whitespace in roff_evalnum(). */
447 static struct roffmac
*hash
[HASHWIDTH
];
449 static struct roffmac roffs
[ROFF_MAX
] = {
450 { "ab", roff_unsupp
, NULL
, NULL
, 0, NULL
},
451 { "ad", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
452 { "af", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
453 { "aln", roff_unsupp
, NULL
, NULL
, 0, NULL
},
454 { "als", roff_unsupp
, NULL
, NULL
, 0, NULL
},
455 { "am", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
456 { "am1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
457 { "ami", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
458 { "ami1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
459 { "as", roff_ds
, NULL
, NULL
, 0, NULL
},
460 { "as1", roff_ds
, NULL
, NULL
, 0, NULL
},
461 { "asciify", roff_unsupp
, NULL
, NULL
, 0, NULL
},
462 { "backtrace", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
463 { "bd", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
464 { "bleedat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
465 { "blm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
466 { "box", roff_unsupp
, NULL
, NULL
, 0, NULL
},
467 { "boxa", roff_unsupp
, NULL
, NULL
, 0, NULL
},
468 { "bp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
469 { "BP", roff_unsupp
, NULL
, NULL
, 0, NULL
},
470 { "break", roff_unsupp
, NULL
, NULL
, 0, NULL
},
471 { "breakchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
472 { "brnl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
473 { "brp", roff_brp
, NULL
, NULL
, 0, NULL
},
474 { "brpnl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
475 { "c2", roff_unsupp
, NULL
, NULL
, 0, NULL
},
476 { "cc", roff_cc
, NULL
, NULL
, 0, NULL
},
477 { "ce", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
478 { "cf", roff_insec
, NULL
, NULL
, 0, NULL
},
479 { "cflags", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
480 { "ch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
481 { "char", roff_unsupp
, NULL
, NULL
, 0, NULL
},
482 { "chop", roff_unsupp
, NULL
, NULL
, 0, NULL
},
483 { "class", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
484 { "close", roff_insec
, NULL
, NULL
, 0, NULL
},
485 { "CL", roff_unsupp
, NULL
, NULL
, 0, NULL
},
486 { "color", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
487 { "composite", roff_unsupp
, NULL
, NULL
, 0, NULL
},
488 { "continue", roff_unsupp
, NULL
, NULL
, 0, NULL
},
489 { "cp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
490 { "cropat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
491 { "cs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
492 { "cu", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
493 { "da", roff_unsupp
, NULL
, NULL
, 0, NULL
},
494 { "dch", roff_unsupp
, NULL
, NULL
, 0, NULL
},
495 { "Dd", roff_Dd
, NULL
, NULL
, 0, NULL
},
496 { "de", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
497 { "de1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
498 { "defcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
499 { "dei", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
500 { "dei1", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
501 { "device", roff_unsupp
, NULL
, NULL
, 0, NULL
},
502 { "devicem", roff_unsupp
, NULL
, NULL
, 0, NULL
},
503 { "di", roff_unsupp
, NULL
, NULL
, 0, NULL
},
504 { "do", roff_unsupp
, NULL
, NULL
, 0, NULL
},
505 { "ds", roff_ds
, NULL
, NULL
, 0, NULL
},
506 { "ds1", roff_ds
, NULL
, NULL
, 0, NULL
},
507 { "dwh", roff_unsupp
, NULL
, NULL
, 0, NULL
},
508 { "dt", roff_unsupp
, NULL
, NULL
, 0, NULL
},
509 { "ec", roff_unsupp
, NULL
, NULL
, 0, NULL
},
510 { "ecr", roff_unsupp
, NULL
, NULL
, 0, NULL
},
511 { "ecs", roff_unsupp
, NULL
, NULL
, 0, NULL
},
512 { "el", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
513 { "em", roff_unsupp
, NULL
, NULL
, 0, NULL
},
514 { "EN", roff_EN
, NULL
, NULL
, 0, NULL
},
515 { "eo", roff_unsupp
, NULL
, NULL
, 0, NULL
},
516 { "EP", roff_unsupp
, NULL
, NULL
, 0, NULL
},
517 { "EQ", roff_EQ
, NULL
, NULL
, 0, NULL
},
518 { "errprint", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
519 { "ev", roff_unsupp
, NULL
, NULL
, 0, NULL
},
520 { "evc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
521 { "ex", roff_unsupp
, NULL
, NULL
, 0, NULL
},
522 { "fallback", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
523 { "fam", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
524 { "fc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
525 { "fchar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
526 { "fcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
527 { "fdeferlig", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
528 { "feature", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
529 { "fkern", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
530 { "fl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
531 { "flig", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
532 { "fp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
533 { "fps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
534 { "fschar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
535 { "fspacewidth", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
536 { "fspecial", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
537 { "ftr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
538 { "fzoom", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
539 { "gcolor", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
540 { "hc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
541 { "hcode", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
542 { "hidechar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
543 { "hla", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
544 { "hlm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
545 { "hpf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
546 { "hpfa", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
547 { "hpfcode", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
548 { "hw", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
549 { "hy", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
550 { "hylang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
551 { "hylen", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
552 { "hym", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
553 { "hypp", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
554 { "hys", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
555 { "ie", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
556 { "if", roff_cond
, roff_cond_text
, roff_cond_sub
, ROFFMAC_STRUCT
, NULL
},
557 { "ig", roff_block
, roff_block_text
, roff_block_sub
, 0, NULL
},
558 { "index", roff_unsupp
, NULL
, NULL
, 0, NULL
},
559 { "it", roff_it
, NULL
, NULL
, 0, NULL
},
560 { "itc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
561 { "IX", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
562 { "kern", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
563 { "kernafter", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
564 { "kernbefore", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
565 { "kernpair", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
566 { "lc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
567 { "lc_ctype", roff_unsupp
, NULL
, NULL
, 0, NULL
},
568 { "lds", roff_unsupp
, NULL
, NULL
, 0, NULL
},
569 { "length", roff_unsupp
, NULL
, NULL
, 0, NULL
},
570 { "letadj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
571 { "lf", roff_insec
, NULL
, NULL
, 0, NULL
},
572 { "lg", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
573 { "lhang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
574 { "linetabs", roff_unsupp
, NULL
, NULL
, 0, NULL
},
575 { "lnr", roff_unsupp
, NULL
, NULL
, 0, NULL
},
576 { "lnrf", roff_unsupp
, NULL
, NULL
, 0, NULL
},
577 { "lpfx", roff_unsupp
, NULL
, NULL
, 0, NULL
},
578 { "ls", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
579 { "lsm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
580 { "lt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
581 { "mc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
582 { "mediasize", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
583 { "minss", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
584 { "mk", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
585 { "mso", roff_insec
, NULL
, NULL
, 0, NULL
},
586 { "na", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
587 { "ne", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
588 { "nh", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
589 { "nhychar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
590 { "nm", roff_unsupp
, NULL
, NULL
, 0, NULL
},
591 { "nn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
592 { "nop", roff_unsupp
, NULL
, NULL
, 0, NULL
},
593 { "nr", roff_nr
, NULL
, NULL
, 0, NULL
},
594 { "nrf", roff_unsupp
, NULL
, NULL
, 0, NULL
},
595 { "nroff", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
596 { "ns", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
597 { "nx", roff_insec
, NULL
, NULL
, 0, NULL
},
598 { "open", roff_insec
, NULL
, NULL
, 0, NULL
},
599 { "opena", roff_insec
, NULL
, NULL
, 0, NULL
},
600 { "os", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
601 { "output", roff_unsupp
, NULL
, NULL
, 0, NULL
},
602 { "padj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
603 { "papersize", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
604 { "pc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
605 { "pev", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
606 { "pi", roff_insec
, NULL
, NULL
, 0, NULL
},
607 { "PI", roff_unsupp
, NULL
, NULL
, 0, NULL
},
608 { "pl", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
609 { "pm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
610 { "pn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
611 { "pnr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
612 { "po", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
613 { "ps", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
614 { "psbb", roff_unsupp
, NULL
, NULL
, 0, NULL
},
615 { "pshape", roff_unsupp
, NULL
, NULL
, 0, NULL
},
616 { "pso", roff_insec
, NULL
, NULL
, 0, NULL
},
617 { "ptr", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
618 { "pvs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
619 { "rchar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
620 { "rd", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
621 { "recursionlimit", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
622 { "return", roff_unsupp
, NULL
, NULL
, 0, NULL
},
623 { "rfschar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
624 { "rhang", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
625 { "rj", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
626 { "rm", roff_rm
, NULL
, NULL
, 0, NULL
},
627 { "rn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
628 { "rnn", roff_unsupp
, NULL
, NULL
, 0, NULL
},
629 { "rr", roff_rr
, NULL
, NULL
, 0, NULL
},
630 { "rs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
631 { "rt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
632 { "schar", roff_unsupp
, NULL
, NULL
, 0, NULL
},
633 { "sentchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
634 { "shc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
635 { "shift", roff_unsupp
, NULL
, NULL
, 0, NULL
},
636 { "sizes", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
637 { "so", roff_so
, NULL
, NULL
, 0, NULL
},
638 { "spacewidth", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
639 { "special", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
640 { "spreadwarn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
641 { "ss", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
642 { "sty", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
643 { "substring", roff_unsupp
, NULL
, NULL
, 0, NULL
},
644 { "sv", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
645 { "sy", roff_insec
, NULL
, NULL
, 0, NULL
},
646 { "T&", roff_T_
, NULL
, NULL
, 0, NULL
},
647 { "ta", roff_unsupp
, NULL
, NULL
, 0, NULL
},
648 { "tc", roff_unsupp
, NULL
, NULL
, 0, NULL
},
649 { "TE", roff_TE
, NULL
, NULL
, 0, NULL
},
650 { "TH", roff_TH
, NULL
, NULL
, 0, NULL
},
651 { "ti", roff_unsupp
, NULL
, NULL
, 0, NULL
},
652 { "tkf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
653 { "tl", roff_unsupp
, NULL
, NULL
, 0, NULL
},
654 { "tm", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
655 { "tm1", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
656 { "tmc", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
657 { "tr", roff_tr
, NULL
, NULL
, 0, NULL
},
658 { "track", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
659 { "transchar", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
660 { "trf", roff_insec
, NULL
, NULL
, 0, NULL
},
661 { "trimat", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
662 { "trin", roff_unsupp
, NULL
, NULL
, 0, NULL
},
663 { "trnt", roff_unsupp
, NULL
, NULL
, 0, NULL
},
664 { "troff", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
665 { "TS", roff_TS
, NULL
, NULL
, 0, NULL
},
666 { "uf", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
667 { "ul", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
668 { "unformat", roff_unsupp
, NULL
, NULL
, 0, NULL
},
669 { "unwatch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
670 { "unwatchn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
671 { "vpt", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
672 { "vs", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
673 { "warn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
674 { "warnscale", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
675 { "watch", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
676 { "watchlength", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
677 { "watchn", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
678 { "wh", roff_unsupp
, NULL
, NULL
, 0, NULL
},
679 { "while", roff_unsupp
, NULL
, NULL
, 0, NULL
},
680 { "write", roff_insec
, NULL
, NULL
, 0, NULL
},
681 { "writec", roff_insec
, NULL
, NULL
, 0, NULL
},
682 { "writem", roff_insec
, NULL
, NULL
, 0, NULL
},
683 { "xflag", roff_line_ignore
, NULL
, NULL
, 0, NULL
},
684 { ".", roff_cblock
, NULL
, NULL
, 0, NULL
},
685 { NULL
, roff_userdef
, NULL
, NULL
, 0, NULL
},
688 /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
689 const char *const __mdoc_reserved
[] = {
690 "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
691 "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
692 "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
693 "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
694 "Dt", "Dv", "Dx", "D1",
695 "Ec", "Ed", "Ef", "Ek", "El", "Em",
696 "En", "Eo", "Er", "Es", "Ev", "Ex",
697 "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
698 "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
699 "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
700 "Oc", "Oo", "Op", "Os", "Ot", "Ox",
701 "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
702 "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
703 "Sc", "Sh", "Sm", "So", "Sq",
704 "Ss", "St", "Sx", "Sy",
705 "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
706 "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
707 "%P", "%Q", "%R", "%T", "%U", "%V",
711 /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
712 const char *const __man_reserved
[] = {
713 "AT", "B", "BI", "BR", "DT",
714 "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
715 "LP", "OP", "P", "PD", "PP",
716 "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
717 "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
721 /* Array of injected predefined strings. */
722 #define PREDEFS_MAX 38
723 static const struct predef predefs
[PREDEFS_MAX
] = {
724 #include "predefs.in"
727 /* See roffhash_find() */
728 #define ROFF_HASH(p) (p[0] - ASCII_LO)
730 static int roffit_lines
; /* number of lines to delay */
731 static char *roffit_macro
; /* nil-terminated macro line */
740 for (i
= 0; i
< (int)ROFF_USERDEF
; i
++) {
741 assert(roffs
[i
].name
[0] >= ASCII_LO
);
742 assert(roffs
[i
].name
[0] <= ASCII_HI
);
744 buc
= ROFF_HASH(roffs
[i
].name
);
746 if (NULL
!= (n
= hash
[buc
])) {
747 for ( ; n
->next
; n
= n
->next
)
751 hash
[buc
] = &roffs
[i
];
756 * Look up a roff token by its name. Returns ROFF_MAX if no macro by
757 * the nil-terminated string name could be found.
760 roffhash_find(const char *p
, size_t s
)
766 * libroff has an extremely simple hashtable, for the time
767 * being, which simply keys on the first character, which must
768 * be printable, then walks a chain. It works well enough until
772 if (p
[0] < ASCII_LO
|| p
[0] > ASCII_HI
)
777 if (NULL
== (n
= hash
[buc
]))
779 for ( ; n
; n
= n
->next
)
780 if (0 == strncmp(n
->name
, p
, s
) && '\0' == n
->name
[(int)s
])
781 return((enum rofft
)(n
- roffs
));
787 * Pop the current node off of the stack of roff instructions currently
791 roffnode_pop(struct roff
*r
)
798 r
->last
= r
->last
->parent
;
805 * Push a roff node onto the instruction stack. This must later be
806 * removed with roffnode_pop().
809 roffnode_push(struct roff
*r
, enum rofft tok
, const char *name
,
814 p
= mandoc_calloc(1, sizeof(struct roffnode
));
817 p
->name
= mandoc_strdup(name
);
821 p
->rule
= p
->parent
? p
->parent
->rule
: 0;
827 roff_free1(struct roff
*r
)
829 struct tbl_node
*tbl
;
833 while (NULL
!= (tbl
= r
->first_tbl
)) {
834 r
->first_tbl
= tbl
->next
;
837 r
->first_tbl
= r
->last_tbl
= r
->tbl
= NULL
;
839 while (NULL
!= (e
= r
->first_eqn
)) {
840 r
->first_eqn
= e
->next
;
843 r
->first_eqn
= r
->last_eqn
= r
->eqn
= NULL
;
853 roff_freereg(r
->regtab
);
856 roff_freestr(r
->strtab
);
857 roff_freestr(r
->xmbtab
);
858 r
->strtab
= r
->xmbtab
= NULL
;
861 for (i
= 0; i
< 128; i
++)
868 roff_reset(struct roff
*r
)
872 r
->format
= r
->options
& (MPARSE_MDOC
| MPARSE_MAN
);
877 roff_free(struct roff
*r
)
885 roff_alloc(struct mparse
*parse
, const struct mchars
*mchars
, int options
)
889 r
= mandoc_calloc(1, sizeof(struct roff
));
892 r
->options
= options
;
893 r
->format
= options
& (MPARSE_MDOC
| MPARSE_MAN
);
902 * In the current line, expand escape sequences that tend to get
903 * used in numerical expressions and conditional requests.
904 * Also check the syntax of the remaining escape sequences.
907 roff_res(struct roff
*r
, struct buf
*buf
, int ln
, int pos
)
909 char ubuf
[24]; /* buffer to print the number */
910 const char *start
; /* start of the string to process */
911 char *stesc
; /* start of an escape sequence ('\\') */
912 const char *stnam
; /* start of the name, after "[(*" */
913 const char *cp
; /* end of the name, e.g. before ']' */
914 const char *res
; /* the string to be substituted */
915 char *nbuf
; /* new buffer to copy buf->buf to */
916 size_t maxl
; /* expected length of the escape name */
917 size_t naml
; /* actual length of the escape name */
918 enum mandoc_esc esc
; /* type of the escape sequence */
919 int inaml
; /* length returned from mandoc_escape() */
920 int expand_count
; /* to avoid infinite loops */
921 int npos
; /* position in numeric expression */
922 int arg_complete
; /* argument not interrupted by eol */
923 char term
; /* character terminating the escape */
926 start
= buf
->buf
+ pos
;
927 stesc
= strchr(start
, '\0') - 1;
928 while (stesc
-- > start
) {
930 /* Search backwards for the next backslash. */
935 /* If it is escaped, skip it. */
937 for (cp
= stesc
- 1; cp
>= start
; cp
--)
941 if ((stesc
- cp
) % 2 == 0) {
946 /* Decide whether to expand or to check only. */
963 esc
= mandoc_escape(&cp
, &stnam
, &inaml
);
964 if (esc
== ESCAPE_ERROR
||
965 (esc
== ESCAPE_SPECIAL
&&
966 mchars_spec2cp(r
->mchars
, stnam
, inaml
) < 0))
967 mandoc_vmsg(MANDOCERR_ESC_BAD
,
968 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
969 "%.*s", (int)(cp
- stesc
), stesc
);
973 if (EXPAND_LIMIT
< ++expand_count
) {
974 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
975 ln
, (int)(stesc
- buf
->buf
), NULL
);
980 * The third character decides the length
981 * of the name of the string or register.
982 * Save a pointer to the name.
1009 /* Advance to the end of the name. */
1013 while (maxl
== 0 || naml
< maxl
) {
1015 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
1016 ln
, (int)(stesc
- buf
->buf
), stesc
);
1020 if (maxl
== 0 && *cp
== term
) {
1024 if (*cp
++ != '\\' || stesc
[1] != 'w') {
1028 switch (mandoc_escape(&cp
, NULL
, NULL
)) {
1029 case ESCAPE_SPECIAL
:
1031 case ESCAPE_UNICODE
:
1033 case ESCAPE_NUMBERED
:
1035 case ESCAPE_OVERSTRIKE
:
1044 * Retrieve the replacement string; if it is
1045 * undefined, resume searching for escapes.
1051 res
= roff_getstrn(r
, stnam
, naml
);
1055 ubuf
[0] = arg_complete
&&
1056 roff_evalnum(r
, ln
, stnam
, &npos
,
1057 NULL
, ROFFNUM_SCALE
) &&
1058 stnam
+ npos
+ 1 == cp
? '1' : '0';
1063 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1064 roff_getregn(r
, stnam
, naml
));
1069 /* use even incomplete args */
1070 (void)snprintf(ubuf
, sizeof(ubuf
), "%d",
1076 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1077 r
->parse
, ln
, (int)(stesc
- buf
->buf
),
1078 "%.*s", (int)naml
, stnam
);
1080 } else if (buf
->sz
+ strlen(res
) > SHRT_MAX
) {
1081 mandoc_msg(MANDOCERR_ROFFLOOP
, r
->parse
,
1082 ln
, (int)(stesc
- buf
->buf
), NULL
);
1086 /* Replace the escape sequence by the string. */
1089 buf
->sz
= mandoc_asprintf(&nbuf
, "%s%s%s",
1090 buf
->buf
, res
, cp
) + 1;
1092 /* Prepare for the next replacement. */
1095 stesc
= nbuf
+ (stesc
- buf
->buf
) + strlen(res
);
1103 * Process text streams:
1104 * Convert all breakable hyphens into ASCII_HYPH.
1105 * Decrement and spring input line trap.
1108 roff_parsetext(struct buf
*buf
, int pos
, int *offs
)
1114 enum mandoc_esc esc
;
1116 start
= p
= buf
->buf
+ pos
;
1118 while (*p
!= '\0') {
1119 sz
= strcspn(p
, "-\\");
1126 /* Skip over escapes. */
1128 esc
= mandoc_escape((const char **)&p
, NULL
, NULL
);
1129 if (esc
== ESCAPE_ERROR
)
1132 } else if (p
== start
) {
1137 if (isalpha((unsigned char)p
[-1]) &&
1138 isalpha((unsigned char)p
[1]))
1143 /* Spring the input line trap. */
1144 if (roffit_lines
== 1) {
1145 isz
= mandoc_asprintf(&p
, "%s\n.%s", buf
->buf
, roffit_macro
);
1152 return(ROFF_REPARSE
);
1153 } else if (roffit_lines
> 1)
1159 roff_parseln(struct roff
*r
, int ln
, struct buf
*buf
, int *offs
)
1163 int pos
; /* parse point */
1164 int spos
; /* saved parse point for messages */
1165 int ppos
; /* original offset in buf->buf */
1166 int ctl
; /* macro line (boolean) */
1170 /* Handle in-line equation delimiters. */
1172 if (r
->tbl
== NULL
&&
1173 r
->last_eqn
!= NULL
&& r
->last_eqn
->delim
&&
1174 (r
->eqn
== NULL
|| r
->eqn_inline
)) {
1175 e
= roff_eqndelim(r
, buf
, pos
);
1176 if (e
== ROFF_REPARSE
)
1178 assert(e
== ROFF_CONT
);
1181 /* Expand some escape sequences. */
1183 e
= roff_res(r
, buf
, ln
, pos
);
1186 assert(e
== ROFF_CONT
);
1188 ctl
= roff_getcontrol(r
, buf
->buf
, &pos
);
1191 * First, if a scope is open and we're not a macro, pass the
1192 * text through the macro's filter.
1193 * Equations process all content themselves.
1194 * Tables process almost all content themselves, but we want
1195 * to warn about macros before passing it there.
1198 if (r
->last
!= NULL
&& ! ctl
) {
1200 assert(roffs
[t
].text
);
1201 e
= (*roffs
[t
].text
)(r
, t
, buf
, ln
, pos
, pos
, offs
);
1202 assert(e
== ROFF_IGN
|| e
== ROFF_CONT
);
1207 return(eqn_read(&r
->eqn
, ln
, buf
->buf
, ppos
, offs
));
1208 if (r
->tbl
!= NULL
&& ( ! ctl
|| buf
->buf
[pos
] == '\0'))
1209 return(tbl_read(r
->tbl
, ln
, buf
->buf
, ppos
));
1211 return(roff_parsetext(buf
, pos
, offs
));
1213 /* Skip empty request lines. */
1215 if (buf
->buf
[pos
] == '"') {
1216 mandoc_msg(MANDOCERR_COMMENT_BAD
, r
->parse
,
1219 } else if (buf
->buf
[pos
] == '\0')
1223 * If a scope is open, go to the child handler for that macro,
1224 * as it may want to preprocess before doing anything with it.
1225 * Don't do so if an equation is open.
1230 assert(roffs
[t
].sub
);
1231 return((*roffs
[t
].sub
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1234 /* No scope is open. This is a new request or macro. */
1237 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1239 /* Tables ignore most macros. */
1241 if (r
->tbl
!= NULL
&& (t
== ROFF_MAX
|| t
== ROFF_TS
)) {
1242 mandoc_msg(MANDOCERR_TBLMACRO
, r
->parse
,
1243 ln
, pos
, buf
->buf
+ spos
);
1246 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] != ' ')
1248 while (buf
->buf
[pos
] != '\0' && buf
->buf
[pos
] == ' ')
1250 return(tbl_read(r
->tbl
, ln
, buf
->buf
, pos
));
1254 * This is neither a roff request nor a user-defined macro.
1255 * Let the standard macro set parsers handle it.
1261 /* Execute a roff request or a user defined macro. */
1263 assert(roffs
[t
].proc
);
1264 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1268 roff_endparse(struct roff
*r
)
1272 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1273 r
->last
->line
, r
->last
->col
,
1274 roffs
[r
->last
->tok
].name
);
1277 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1278 r
->eqn
->eqn
.ln
, r
->eqn
->eqn
.pos
, "EQ");
1283 mandoc_msg(MANDOCERR_BLK_NOEND
, r
->parse
,
1284 r
->tbl
->line
, r
->tbl
->pos
, "TS");
1290 * Parse a roff node's type from the input buffer. This must be in the
1291 * form of ".foo xxx" in the usual way.
1294 roff_parse(struct roff
*r
, char *buf
, int *pos
, int ln
, int ppos
)
1303 if ('\0' == *cp
|| '"' == *cp
|| '\t' == *cp
|| ' ' == *cp
)
1307 maclen
= roff_getname(r
, &cp
, ln
, ppos
);
1309 t
= (r
->current_string
= roff_getstrn(r
, mac
, maclen
))
1310 ? ROFF_USERDEF
: roffhash_find(mac
, maclen
);
1319 roff_cblock(ROFF_ARGS
)
1323 * A block-close `..' should only be invoked as a child of an
1324 * ignore macro, otherwise raise a warning and just ignore it.
1327 if (r
->last
== NULL
) {
1328 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1333 switch (r
->last
->tok
) {
1335 /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1340 /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1347 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1352 if (buf
->buf
[pos
] != '\0')
1353 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
1354 ".. %s", buf
->buf
+ pos
);
1357 roffnode_cleanscope(r
);
1363 roffnode_cleanscope(struct roff
*r
)
1367 if (--r
->last
->endspan
!= 0)
1374 roff_ccond(struct roff
*r
, int ln
, int ppos
)
1377 if (NULL
== r
->last
) {
1378 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1383 switch (r
->last
->tok
) {
1391 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1396 if (r
->last
->endspan
> -1) {
1397 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
1403 roffnode_cleanscope(r
);
1408 roff_block(ROFF_ARGS
)
1414 /* Ignore groff compatibility mode for now. */
1416 if (tok
== ROFF_de1
)
1418 else if (tok
== ROFF_dei1
)
1420 else if (tok
== ROFF_am1
)
1422 else if (tok
== ROFF_ami1
)
1425 /* Parse the macro name argument. */
1427 cp
= buf
->buf
+ pos
;
1428 if (tok
== ROFF_ig
) {
1433 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1434 iname
[namesz
] = '\0';
1437 /* Resolve the macro name argument if it is indirect. */
1439 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1440 if ((name
= roff_getstrn(r
, iname
, namesz
)) == NULL
) {
1441 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1442 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1443 "%.*s", (int)namesz
, iname
);
1446 namesz
= strlen(name
);
1450 if (namesz
== 0 && tok
!= ROFF_ig
) {
1451 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
,
1452 ln
, ppos
, roffs
[tok
].name
);
1456 roffnode_push(r
, tok
, name
, ln
, ppos
);
1459 * At the beginning of a `de' macro, clear the existing string
1460 * with the same name, if there is one. New content will be
1461 * appended from roff_block_text() in multiline mode.
1464 if (tok
== ROFF_de
|| tok
== ROFF_dei
)
1465 roff_setstrn(&r
->strtab
, name
, namesz
, "", 0, 0);
1470 /* Get the custom end marker. */
1473 namesz
= roff_getname(r
, &cp
, ln
, ppos
);
1475 /* Resolve the end marker if it is indirect. */
1477 if (namesz
&& (tok
== ROFF_dei
|| tok
== ROFF_ami
)) {
1478 if ((name
= roff_getstrn(r
, iname
, namesz
)) == NULL
) {
1479 mandoc_vmsg(MANDOCERR_STR_UNDEF
,
1480 r
->parse
, ln
, (int)(iname
- buf
->buf
),
1481 "%.*s", (int)namesz
, iname
);
1484 namesz
= strlen(name
);
1489 r
->last
->end
= mandoc_strndup(name
, namesz
);
1492 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
1493 ln
, pos
, ".%s ... %s", roffs
[tok
].name
, cp
);
1499 roff_block_sub(ROFF_ARGS
)
1505 * First check whether a custom macro exists at this level. If
1506 * it does, then check against it. This is some of groff's
1507 * stranger behaviours. If we encountered a custom end-scope
1508 * tag and that tag also happens to be a "real" macro, then we
1509 * need to try interpreting it again as a real macro. If it's
1510 * not, then return ignore. Else continue.
1514 for (i
= pos
, j
= 0; r
->last
->end
[j
]; j
++, i
++)
1515 if (buf
->buf
[i
] != r
->last
->end
[j
])
1518 if (r
->last
->end
[j
] == '\0' &&
1519 (buf
->buf
[i
] == '\0' ||
1520 buf
->buf
[i
] == ' ' ||
1521 buf
->buf
[i
] == '\t')) {
1523 roffnode_cleanscope(r
);
1525 while (buf
->buf
[i
] == ' ' || buf
->buf
[i
] == '\t')
1529 if (roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
) !=
1537 * If we have no custom end-query or lookup failed, then try
1538 * pulling it out of the hashtable.
1541 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1543 if (t
!= ROFF_cblock
) {
1545 roff_setstr(r
, r
->last
->name
, buf
->buf
+ ppos
, 2);
1549 assert(roffs
[t
].proc
);
1550 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1554 roff_block_text(ROFF_ARGS
)
1558 roff_setstr(r
, r
->last
->name
, buf
->buf
+ pos
, 2);
1564 roff_cond_sub(ROFF_ARGS
)
1571 roffnode_cleanscope(r
);
1572 t
= roff_parse(r
, buf
->buf
, &pos
, ln
, ppos
);
1575 * Fully handle known macros when they are structurally
1576 * required or when the conditional evaluated to true.
1579 if ((t
!= ROFF_MAX
) &&
1580 (rr
|| roffs
[t
].flags
& ROFFMAC_STRUCT
)) {
1581 assert(roffs
[t
].proc
);
1582 return((*roffs
[t
].proc
)(r
, t
, buf
, ln
, ppos
, pos
, offs
));
1586 * If `\}' occurs on a macro line without a preceding macro,
1587 * drop the line completely.
1590 ep
= buf
->buf
+ pos
;
1591 if (ep
[0] == '\\' && ep
[1] == '}')
1594 /* Always check for the closing delimiter `\}'. */
1596 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1597 if (*(++ep
) == '}') {
1599 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1604 return(rr
? ROFF_CONT
: ROFF_IGN
);
1608 roff_cond_text(ROFF_ARGS
)
1614 roffnode_cleanscope(r
);
1616 ep
= buf
->buf
+ pos
;
1617 while ((ep
= strchr(ep
, '\\')) != NULL
) {
1618 if (*(++ep
) == '}') {
1620 roff_ccond(r
, ln
, ep
- buf
->buf
- 1);
1625 return(rr
? ROFF_CONT
: ROFF_IGN
);
1629 * Parse a single signed integer number. Stop at the first non-digit.
1630 * If there is at least one digit, return success and advance the
1631 * parse point, else return failure and let the parse point unchanged.
1632 * Ignore overflows, treat them just like the C language.
1635 roff_getnum(const char *v
, int *pos
, int *res
, int flags
)
1637 int myres
, scaled
, n
, p
;
1644 if (n
|| v
[p
] == '+')
1647 if (flags
& ROFFNUM_WHITE
)
1648 while (isspace((unsigned char)v
[p
]))
1651 for (*res
= 0; isdigit((unsigned char)v
[p
]); p
++)
1652 *res
= 10 * *res
+ v
[p
] - '0';
1659 /* Each number may be followed by one optional scaling unit. */
1663 scaled
= *res
* 65536;
1666 scaled
= *res
* 240;
1669 scaled
= *res
* 240 / 2.54;
1682 scaled
= *res
* 10 / 3;
1688 scaled
= *res
* 6 / 25;
1695 if (flags
& ROFFNUM_SCALE
)
1703 * Evaluate a string comparison condition.
1704 * The first character is the delimiter.
1705 * Succeed if the string up to its second occurrence
1706 * matches the string up to its third occurence.
1707 * Advance the cursor after the third occurrence
1708 * or lacking that, to the end of the line.
1711 roff_evalstrcond(const char *v
, int *pos
)
1713 const char *s1
, *s2
, *s3
;
1717 s1
= v
+ *pos
; /* initial delimiter */
1718 s2
= s1
+ 1; /* for scanning the first string */
1719 s3
= strchr(s2
, *s1
); /* for scanning the second string */
1721 if (NULL
== s3
) /* found no middle delimiter */
1724 while ('\0' != *++s3
) {
1725 if (*s2
!= *s3
) { /* mismatch */
1726 s3
= strchr(s3
, *s1
);
1729 if (*s3
== *s1
) { /* found the final delimiter */
1738 s3
= strchr(s2
, '\0');
1739 else if (*s3
!= '\0')
1746 * Evaluate an optionally negated single character, numerical,
1747 * or string condition.
1750 roff_evalcond(struct roff
*r
, int ln
, const char *v
, int *pos
)
1752 int number
, savepos
, wanttrue
;
1754 if ('!' == v
[*pos
]) {
1786 if (roff_evalnum(r
, ln
, v
, pos
, &number
, ROFFNUM_SCALE
))
1787 return((number
> 0) == wanttrue
);
1788 else if (*pos
== savepos
)
1789 return(roff_evalstrcond(v
, pos
) == wanttrue
);
1795 roff_line_ignore(ROFF_ARGS
)
1802 roff_insec(ROFF_ARGS
)
1805 mandoc_msg(MANDOCERR_REQ_INSEC
, r
->parse
,
1806 ln
, ppos
, roffs
[tok
].name
);
1811 roff_unsupp(ROFF_ARGS
)
1814 mandoc_msg(MANDOCERR_REQ_UNSUPP
, r
->parse
,
1815 ln
, ppos
, roffs
[tok
].name
);
1820 roff_cond(ROFF_ARGS
)
1823 roffnode_push(r
, tok
, NULL
, ln
, ppos
);
1826 * An `.el' has no conditional body: it will consume the value
1827 * of the current rstack entry set in prior `ie' calls or
1830 * If we're not an `el', however, then evaluate the conditional.
1833 r
->last
->rule
= tok
== ROFF_el
?
1834 (r
->rstackpos
< 0 ? 0 : r
->rstack
[r
->rstackpos
--]) :
1835 roff_evalcond(r
, ln
, buf
->buf
, &pos
);
1838 * An if-else will put the NEGATION of the current evaluated
1839 * conditional into the stack of rules.
1842 if (tok
== ROFF_ie
) {
1843 if (r
->rstackpos
+ 1 == r
->rstacksz
) {
1845 r
->rstack
= mandoc_reallocarray(r
->rstack
,
1846 r
->rstacksz
, sizeof(int));
1848 r
->rstack
[++r
->rstackpos
] = !r
->last
->rule
;
1851 /* If the parent has false as its rule, then so do we. */
1853 if (r
->last
->parent
&& !r
->last
->parent
->rule
)
1858 * If there is nothing on the line after the conditional,
1859 * not even whitespace, use next-line scope.
1862 if (buf
->buf
[pos
] == '\0') {
1863 r
->last
->endspan
= 2;
1867 while (buf
->buf
[pos
] == ' ')
1870 /* An opening brace requests multiline scope. */
1872 if (buf
->buf
[pos
] == '\\' && buf
->buf
[pos
+ 1] == '{') {
1873 r
->last
->endspan
= -1;
1879 * Anything else following the conditional causes
1880 * single-line scope. Warn if the scope contains
1881 * nothing but trailing whitespace.
1884 if (buf
->buf
[pos
] == '\0')
1885 mandoc_msg(MANDOCERR_COND_EMPTY
, r
->parse
,
1886 ln
, ppos
, roffs
[tok
].name
);
1888 r
->last
->endspan
= 1;
1902 /* Ignore groff compatibility mode for now. */
1904 if (tok
== ROFF_ds1
)
1906 else if (tok
== ROFF_as1
)
1910 * The first word is the name of the string.
1911 * If it is empty or terminated by an escape sequence,
1912 * abort the `ds' request without defining anything.
1915 name
= string
= buf
->buf
+ pos
;
1919 namesz
= roff_getname(r
, &string
, ln
, pos
);
1920 if (name
[namesz
] == '\\')
1923 /* Read past the initial double-quote, if any. */
1927 /* The rest is the value. */
1928 roff_setstrn(&r
->strtab
, name
, namesz
, string
, strlen(string
),
1934 * Parse a single operator, one or two characters long.
1935 * If the operator is recognized, return success and advance the
1936 * parse point, else return failure and let the parse point unchanged.
1939 roff_getop(const char *v
, int *pos
, char *res
)
1960 switch (v
[*pos
+ 1]) {
1978 switch (v
[*pos
+ 1]) {
1992 if ('=' == v
[*pos
+ 1])
2004 * Evaluate either a parenthesized numeric expression
2005 * or a single signed integer number.
2008 roff_evalpar(struct roff
*r
, int ln
,
2009 const char *v
, int *pos
, int *res
, int flags
)
2013 return(roff_getnum(v
, pos
, res
, flags
));
2016 if ( ! roff_evalnum(r
, ln
, v
, pos
, res
, flags
| ROFFNUM_WHITE
))
2020 * Omission of the closing parenthesis
2021 * is an error in validation mode,
2022 * but ignored in evaluation mode.
2027 else if (NULL
== res
)
2034 * Evaluate a complete numeric expression.
2035 * Proceed left to right, there is no concept of precedence.
2038 roff_evalnum(struct roff
*r
, int ln
, const char *v
,
2039 int *pos
, int *res
, int flags
)
2041 int mypos
, operand2
;
2049 if (flags
& ROFFNUM_WHITE
)
2050 while (isspace((unsigned char)v
[*pos
]))
2053 if ( ! roff_evalpar(r
, ln
, v
, pos
, res
, flags
))
2057 if (flags
& ROFFNUM_WHITE
)
2058 while (isspace((unsigned char)v
[*pos
]))
2061 if ( ! roff_getop(v
, pos
, &operator))
2064 if (flags
& ROFFNUM_WHITE
)
2065 while (isspace((unsigned char)v
[*pos
]))
2068 if ( ! roff_evalpar(r
, ln
, v
, pos
, &operand2
, flags
))
2071 if (flags
& ROFFNUM_WHITE
)
2072 while (isspace((unsigned char)v
[*pos
]))
2089 if (operand2
== 0) {
2090 mandoc_msg(MANDOCERR_DIVZERO
,
2091 r
->parse
, ln
, *pos
, v
);
2098 if (operand2
== 0) {
2099 mandoc_msg(MANDOCERR_DIVZERO
,
2100 r
->parse
, ln
, *pos
, v
);
2107 *res
= *res
< operand2
;
2110 *res
= *res
> operand2
;
2113 *res
= *res
<= operand2
;
2116 *res
= *res
>= operand2
;
2119 *res
= *res
== operand2
;
2122 *res
= *res
!= operand2
;
2125 *res
= *res
&& operand2
;
2128 *res
= *res
|| operand2
;
2131 if (operand2
< *res
)
2135 if (operand2
> *res
)
2146 roff_setreg(struct roff
*r
, const char *name
, int val
, char sign
)
2148 struct roffreg
*reg
;
2150 /* Search for an existing register with the same name. */
2153 while (reg
&& strcmp(name
, reg
->key
.p
))
2157 /* Create a new register. */
2158 reg
= mandoc_malloc(sizeof(struct roffreg
));
2159 reg
->key
.p
= mandoc_strdup(name
);
2160 reg
->key
.sz
= strlen(name
);
2162 reg
->next
= r
->regtab
;
2168 else if ('-' == sign
)
2175 * Handle some predefined read-only number registers.
2176 * For now, return -1 if the requested register is not predefined;
2177 * in case a predefined read-only register having the value -1
2178 * were to turn up, another special value would have to be chosen.
2181 roff_getregro(const char *name
)
2185 case 'A': /* ASCII approximation mode is always off. */
2187 case 'g': /* Groff compatibility mode is always on. */
2189 case 'H': /* Fixed horizontal resolution. */
2191 case 'j': /* Always adjust left margin only. */
2193 case 'T': /* Some output device is always defined. */
2195 case 'V': /* Fixed vertical resolution. */
2203 roff_getreg(const struct roff
*r
, const char *name
)
2205 struct roffreg
*reg
;
2208 if ('.' == name
[0] && '\0' != name
[1] && '\0' == name
[2]) {
2209 val
= roff_getregro(name
+ 1);
2214 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2215 if (0 == strcmp(name
, reg
->key
.p
))
2222 roff_getregn(const struct roff
*r
, const char *name
, size_t len
)
2224 struct roffreg
*reg
;
2227 if ('.' == name
[0] && 2 == len
) {
2228 val
= roff_getregro(name
+ 1);
2233 for (reg
= r
->regtab
; reg
; reg
= reg
->next
)
2234 if (len
== reg
->key
.sz
&&
2235 0 == strncmp(name
, reg
->key
.p
, len
))
2242 roff_freereg(struct roffreg
*reg
)
2244 struct roffreg
*old_reg
;
2246 while (NULL
!= reg
) {
2262 key
= val
= buf
->buf
+ pos
;
2266 keysz
= roff_getname(r
, &val
, ln
, pos
);
2267 if (key
[keysz
] == '\\')
2272 if (sign
== '+' || sign
== '-')
2275 if (roff_evalnum(r
, ln
, val
, NULL
, &iv
, ROFFNUM_SCALE
))
2276 roff_setreg(r
, key
, iv
, sign
);
2284 struct roffreg
*reg
, **prev
;
2288 name
= cp
= buf
->buf
+ pos
;
2291 namesz
= roff_getname(r
, &cp
, ln
, pos
);
2292 name
[namesz
] = '\0';
2297 if (reg
== NULL
|| !strcmp(name
, reg
->key
.p
))
2316 cp
= buf
->buf
+ pos
;
2317 while (*cp
!= '\0') {
2319 namesz
= roff_getname(r
, &cp
, ln
, (int)(cp
- buf
->buf
));
2320 roff_setstrn(&r
->strtab
, name
, namesz
, NULL
, 0, 0);
2321 if (name
[namesz
] == '\\')
2332 /* Parse the number of lines. */
2334 if ( ! roff_evalnum(r
, ln
, buf
->buf
, &pos
, &iv
, 0)) {
2335 mandoc_msg(MANDOCERR_IT_NONUM
, r
->parse
,
2336 ln
, ppos
, buf
->buf
+ 1);
2340 while (isspace((unsigned char)buf
->buf
[pos
]))
2344 * Arm the input line trap.
2345 * Special-casing "an-trap" is an ugly workaround to cope
2346 * with DocBook stupidly fiddling with man(7) internals.
2350 roffit_macro
= mandoc_strdup(iv
!= 1 ||
2351 strcmp(buf
->buf
+ pos
, "an-trap") ?
2352 buf
->buf
+ pos
: "br");
2359 const char *const *cp
;
2361 if ((r
->options
& (MPARSE_MDOC
| MPARSE_QUICK
)) == 0)
2362 for (cp
= __mdoc_reserved
; *cp
; cp
++)
2363 roff_setstr(r
, *cp
, NULL
, 0);
2366 r
->format
= MPARSE_MDOC
;
2374 const char *const *cp
;
2376 if ((r
->options
& MPARSE_QUICK
) == 0)
2377 for (cp
= __man_reserved
; *cp
; cp
++)
2378 roff_setstr(r
, *cp
, NULL
, 0);
2381 r
->format
= MPARSE_MAN
;
2391 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2393 else if ( ! tbl_end(&r
->tbl
)) {
2395 buf
->buf
= mandoc_strdup(".sp");
2397 return(ROFF_REPARSE
);
2407 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
,
2410 tbl_restart(ppos
, ln
, r
->tbl
);
2416 * Handle in-line equation delimiters.
2419 roff_eqndelim(struct roff
*r
, struct buf
*buf
, int pos
)
2422 const char *bef_pr
, *bef_nl
, *mac
, *aft_nl
, *aft_pr
;
2425 * Outside equations, look for an opening delimiter.
2426 * If we are inside an equation, we already know it is
2427 * in-line, or this function wouldn't have been called;
2428 * so look for a closing delimiter.
2431 cp1
= buf
->buf
+ pos
;
2432 cp2
= strchr(cp1
, r
->eqn
== NULL
?
2433 r
->last_eqn
->odelim
: r
->last_eqn
->cdelim
);
2438 bef_pr
= bef_nl
= aft_nl
= aft_pr
= "";
2440 /* Handle preceding text, protecting whitespace. */
2442 if (*buf
->buf
!= '\0') {
2449 * Prepare replacing the delimiter with an equation macro
2450 * and drop leading white space from the equation.
2453 if (r
->eqn
== NULL
) {
2460 /* Handle following text, protecting whitespace. */
2468 /* Do the actual replacement. */
2470 buf
->sz
= mandoc_asprintf(&cp1
, "%s%s%s%s%s%s%s", buf
->buf
,
2471 bef_pr
, bef_nl
, mac
, aft_nl
, aft_pr
, cp2
) + 1;
2475 /* Toggle the in-line state of the eqn subsystem. */
2477 r
->eqn_inline
= r
->eqn
== NULL
;
2478 return(ROFF_REPARSE
);
2486 assert(r
->eqn
== NULL
);
2487 e
= eqn_alloc(ppos
, ln
, r
->parse
);
2490 r
->last_eqn
->next
= e
;
2491 e
->delim
= r
->last_eqn
->delim
;
2492 e
->odelim
= r
->last_eqn
->odelim
;
2493 e
->cdelim
= r
->last_eqn
->cdelim
;
2495 r
->first_eqn
= r
->last_eqn
= e
;
2497 r
->eqn
= r
->last_eqn
= e
;
2499 if (buf
->buf
[pos
] != '\0')
2500 mandoc_vmsg(MANDOCERR_ARG_SKIP
, r
->parse
, ln
, pos
,
2501 ".EQ %s", buf
->buf
+ pos
);
2510 mandoc_msg(MANDOCERR_BLK_NOTOPEN
, r
->parse
, ln
, ppos
, "EN");
2517 struct tbl_node
*tbl
;
2520 mandoc_msg(MANDOCERR_BLK_BROKEN
, r
->parse
,
2521 ln
, ppos
, "TS breaks TS");
2525 tbl
= tbl_alloc(ppos
, ln
, r
->parse
);
2528 r
->last_tbl
->next
= tbl
;
2530 r
->first_tbl
= r
->last_tbl
= tbl
;
2532 r
->tbl
= r
->last_tbl
= tbl
;
2540 buf
->buf
[pos
- 1] = '\0';
2551 if (*p
== '\0' || (r
->control
= *p
++) == '.')
2555 mandoc_vmsg(MANDOCERR_ARG_EXCESS
, r
->parse
,
2556 ln
, p
- buf
->buf
, "cc ... %s", p
);
2564 const char *p
, *first
, *second
;
2566 enum mandoc_esc esc
;
2571 mandoc_msg(MANDOCERR_REQ_EMPTY
, r
->parse
, ln
, ppos
, "tr");
2575 while (*p
!= '\0') {
2579 if (*first
== '\\') {
2580 esc
= mandoc_escape(&p
, NULL
, NULL
);
2581 if (esc
== ESCAPE_ERROR
) {
2582 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
2583 ln
, (int)(p
- buf
->buf
), first
);
2586 fsz
= (size_t)(p
- first
);
2590 if (*second
== '\\') {
2591 esc
= mandoc_escape(&p
, NULL
, NULL
);
2592 if (esc
== ESCAPE_ERROR
) {
2593 mandoc_msg(MANDOCERR_ESC_BAD
, r
->parse
,
2594 ln
, (int)(p
- buf
->buf
), second
);
2597 ssz
= (size_t)(p
- second
);
2598 } else if (*second
== '\0') {
2599 mandoc_vmsg(MANDOCERR_TR_ODD
, r
->parse
,
2600 ln
, first
- buf
->buf
, "tr %s", first
);
2606 roff_setstrn(&r
->xmbtab
, first
, fsz
,
2611 if (r
->xtab
== NULL
)
2612 r
->xtab
= mandoc_calloc(128,
2613 sizeof(struct roffstr
));
2615 free(r
->xtab
[(int)*first
].p
);
2616 r
->xtab
[(int)*first
].p
= mandoc_strndup(second
, ssz
);
2617 r
->xtab
[(int)*first
].sz
= ssz
;
2628 name
= buf
->buf
+ pos
;
2629 mandoc_vmsg(MANDOCERR_SO
, r
->parse
, ln
, ppos
, "so %s", name
);
2632 * Handle `so'. Be EXTREMELY careful, as we shouldn't be
2633 * opening anything that's not in our cwd or anything beneath
2634 * it. Thus, explicitly disallow traversing up the file-system
2635 * or using absolute paths.
2638 if (*name
== '/' || strstr(name
, "../") || strstr(name
, "/..")) {
2639 mandoc_vmsg(MANDOCERR_SO_PATH
, r
->parse
, ln
, ppos
,
2641 buf
->sz
= mandoc_asprintf(&cp
,
2642 ".sp\nSee the file %s.\n.sp", name
) + 1;
2646 return(ROFF_REPARSE
);
2654 roff_userdef(ROFF_ARGS
)
2656 const char *arg
[9], *ap
;
2662 * Collect pointers to macro argument strings
2663 * and NUL-terminate them.
2666 cp
= buf
->buf
+ pos
;
2667 for (i
= 0; i
< 9; i
++)
2668 arg
[i
] = *cp
== '\0' ? "" :
2669 mandoc_getarg(r
->parse
, &cp
, ln
, &pos
);
2672 * Expand macro arguments.
2675 buf
->sz
= strlen(r
->current_string
) + 1;
2676 n1
= cp
= mandoc_malloc(buf
->sz
);
2677 memcpy(n1
, r
->current_string
, buf
->sz
);
2678 while (*cp
!= '\0') {
2680 /* Scan ahead for the next argument invocation. */
2692 * Determine the size of the expanded argument,
2693 * taking escaping of quotes into account.
2697 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
2705 * Determine the size of the rest of the
2706 * unexpanded macro, including the NUL.
2709 rsz
= buf
->sz
- (cp
- n1
) - 3;
2712 * When shrinking, move before
2713 * releasing the storage.
2717 memmove(cp
+ asz
, cp
+ 3, rsz
);
2720 * Resize the storage for the macro
2721 * and readjust the parse pointer.
2725 n2
= mandoc_realloc(n1
, buf
->sz
);
2726 cp
= n2
+ (cp
- n1
);
2730 * When growing, make room
2731 * for the expanded argument.
2735 memmove(cp
+ asz
, cp
+ 3, rsz
);
2738 /* Copy the expanded argument, escaping quotes. */
2741 for (ap
= arg
[i
]; *ap
!= '\0'; ap
++) {
2743 memcpy(n2
, "\\(dq", 4);
2751 * Replace the macro invocation
2752 * by the expanded macro.
2759 return(buf
->sz
> 1 && buf
->buf
[buf
->sz
- 2] == '\n' ?
2760 ROFF_REPARSE
: ROFF_APPEND
);
2764 roff_getname(struct roff
*r
, char **cpp
, int ln
, int pos
)
2773 /* Read until end of name and terminate it with NUL. */
2774 for (cp
= name
; 1; cp
++) {
2775 if ('\0' == *cp
|| ' ' == *cp
) {
2782 if ('{' == cp
[1] || '}' == cp
[1])
2787 mandoc_vmsg(MANDOCERR_NAMESC
, r
->parse
, ln
, pos
,
2788 "%.*s", (int)(cp
- name
+ 1), name
);
2789 mandoc_escape((const char **)&cp
, NULL
, NULL
);
2793 /* Read past spaces. */
2802 * Store *string into the user-defined string called *name.
2803 * To clear an existing entry, call with (*r, *name, NULL, 0).
2804 * append == 0: replace mode
2805 * append == 1: single-line append mode
2806 * append == 2: multiline append mode, append '\n' after each call
2809 roff_setstr(struct roff
*r
, const char *name
, const char *string
,
2813 roff_setstrn(&r
->strtab
, name
, strlen(name
), string
,
2814 string
? strlen(string
) : 0, append
);
2818 roff_setstrn(struct roffkv
**r
, const char *name
, size_t namesz
,
2819 const char *string
, size_t stringsz
, int append
)
2824 size_t oldch
, newch
;
2826 /* Search for an existing string with the same name. */
2829 while (n
&& (namesz
!= n
->key
.sz
||
2830 strncmp(n
->key
.p
, name
, namesz
)))
2834 /* Create a new string table entry. */
2835 n
= mandoc_malloc(sizeof(struct roffkv
));
2836 n
->key
.p
= mandoc_strndup(name
, namesz
);
2842 } else if (0 == append
) {
2852 * One additional byte for the '\n' in multiline mode,
2853 * and one for the terminating '\0'.
2855 newch
= stringsz
+ (1 < append
? 2u : 1u);
2857 if (NULL
== n
->val
.p
) {
2858 n
->val
.p
= mandoc_malloc(newch
);
2863 n
->val
.p
= mandoc_realloc(n
->val
.p
, oldch
+ newch
);
2866 /* Skip existing content in the destination buffer. */
2867 c
= n
->val
.p
+ (int)oldch
;
2869 /* Append new content to the destination buffer. */
2871 while (i
< (int)stringsz
) {
2873 * Rudimentary roff copy mode:
2874 * Handle escaped backslashes.
2876 if ('\\' == string
[i
] && '\\' == string
[i
+ 1])
2881 /* Append terminating bytes. */
2886 n
->val
.sz
= (int)(c
- n
->val
.p
);
2890 roff_getstrn(const struct roff
*r
, const char *name
, size_t len
)
2892 const struct roffkv
*n
;
2895 for (n
= r
->strtab
; n
; n
= n
->next
)
2896 if (0 == strncmp(name
, n
->key
.p
, len
) &&
2897 '\0' == n
->key
.p
[(int)len
])
2900 for (i
= 0; i
< PREDEFS_MAX
; i
++)
2901 if (0 == strncmp(name
, predefs
[i
].name
, len
) &&
2902 '\0' == predefs
[i
].name
[(int)len
])
2903 return(predefs
[i
].str
);
2909 roff_freestr(struct roffkv
*r
)
2911 struct roffkv
*n
, *nn
;
2913 for (n
= r
; n
; n
= nn
) {
2921 const struct tbl_span
*
2922 roff_span(const struct roff
*r
)
2925 return(r
->tbl
? tbl_span(r
->tbl
) : NULL
);
2929 roff_eqn(const struct roff
*r
)
2932 return(r
->last_eqn
? &r
->last_eqn
->eqn
: NULL
);
2936 * Duplicate an input string, making the appropriate character
2937 * conversations (as stipulated by `tr') along the way.
2938 * Returns a heap-allocated string with all the replacements made.
2941 roff_strdup(const struct roff
*r
, const char *p
)
2943 const struct roffkv
*cp
;
2947 enum mandoc_esc esc
;
2949 if (NULL
== r
->xmbtab
&& NULL
== r
->xtab
)
2950 return(mandoc_strdup(p
));
2951 else if ('\0' == *p
)
2952 return(mandoc_strdup(""));
2955 * Step through each character looking for term matches
2956 * (remember that a `tr' can be invoked with an escape, which is
2957 * a glyph but the escape is multi-character).
2958 * We only do this if the character hash has been initialised
2959 * and the string is >0 length.
2965 while ('\0' != *p
) {
2966 if ('\\' != *p
&& r
->xtab
&& r
->xtab
[(int)*p
].p
) {
2967 sz
= r
->xtab
[(int)*p
].sz
;
2968 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
2969 memcpy(res
+ ssz
, r
->xtab
[(int)*p
].p
, sz
);
2973 } else if ('\\' != *p
) {
2974 res
= mandoc_realloc(res
, ssz
+ 2);
2979 /* Search for term matches. */
2980 for (cp
= r
->xmbtab
; cp
; cp
= cp
->next
)
2981 if (0 == strncmp(p
, cp
->key
.p
, cp
->key
.sz
))
2986 * A match has been found.
2987 * Append the match to the array and move
2988 * forward by its keysize.
2990 res
= mandoc_realloc(res
,
2991 ssz
+ cp
->val
.sz
+ 1);
2992 memcpy(res
+ ssz
, cp
->val
.p
, cp
->val
.sz
);
2994 p
+= (int)cp
->key
.sz
;
2999 * Handle escapes carefully: we need to copy
3000 * over just the escape itself, or else we might
3001 * do replacements within the escape itself.
3002 * Make sure to pass along the bogus string.
3005 esc
= mandoc_escape(&p
, NULL
, NULL
);
3006 if (ESCAPE_ERROR
== esc
) {
3008 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3009 memcpy(res
+ ssz
, pp
, sz
);
3013 * We bail out on bad escapes.
3014 * No need to warn: we already did so when
3015 * roff_res() was called.
3018 res
= mandoc_realloc(res
, ssz
+ sz
+ 1);
3019 memcpy(res
+ ssz
, pp
, sz
);
3023 res
[(int)ssz
] = '\0';
3028 roff_getformat(const struct roff
*r
)
3035 * Find out whether a line is a macro line or not.
3036 * If it is, adjust the current position and return one; if it isn't,
3037 * return zero and don't change the current position.
3038 * If the control character has been set with `.cc', then let that grain
3040 * This is slighly contrary to groff, where using the non-breaking
3041 * control character when `cc' has been invoked will cause the
3042 * non-breaking macro contents to be printed verbatim.
3045 roff_getcontrol(const struct roff
*r
, const char *cp
, int *ppos
)
3051 if (0 != r
->control
&& cp
[pos
] == r
->control
)
3053 else if (0 != r
->control
)
3055 else if ('\\' == cp
[pos
] && '.' == cp
[pos
+ 1])
3057 else if ('.' == cp
[pos
] || '\'' == cp
[pos
])
3062 while (' ' == cp
[pos
] || '\t' == cp
[pos
])