]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
55f9522f38e850415dbafb8085223efbc2f2d968
1 /* $Id: man.c,v 1.143 2014/11/19 03:08:17 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2011 Joerg Sonnenberger <joerg@netbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
32 #include "mandoc_aux.h"
34 #include "libmandoc.h"
36 const char *const __man_macronames
[MAN_MAX
] = {
37 "br", "TH", "SH", "SS",
38 "TP", "LP", "PP", "P",
39 "IP", "HP", "SM", "SB",
40 "BI", "IB", "BR", "RB",
42 "RI", "na", "sp", "nf",
43 "fi", "RE", "RS", "DT",
44 "UC", "PD", "AT", "in",
45 "ft", "OP", "EX", "EE",
49 const char * const *man_macronames
= __man_macronames
;
51 static struct man_node
*man_node_alloc(struct man
*, int, int,
52 enum man_type
, enum mant
);
53 static int man_node_append(struct man
*,
55 static void man_node_free(struct man_node
*);
56 static void man_node_unlink(struct man
*,
58 static int man_ptext(struct man
*, int, char *, int);
59 static int man_pmacro(struct man
*, int, char *, int);
60 static void man_free1(struct man
*);
61 static void man_alloc1(struct man
*);
62 static int man_descope(struct man
*, int, int);
65 const struct man_node
*
66 man_node(const struct man
*man
)
72 const struct man_meta
*
73 man_meta(const struct man
*man
)
80 man_reset(struct man
*man
)
88 man_free(struct man
*man
)
96 man_alloc(struct roff
*roff
, struct mparse
*parse
, int quick
)
100 p
= mandoc_calloc(1, sizeof(struct man
));
112 man_endparse(struct man
*man
)
115 return(man_macroend(man
));
119 man_parseln(struct man
*man
, int ln
, char *buf
, int offs
)
122 if (man
->last
->type
!= MAN_EQN
|| ln
> man
->last
->line
)
123 man
->flags
|= MAN_NEWLINE
;
125 return (roff_getcontrol(man
->roff
, buf
, &offs
) ?
126 man_pmacro(man
, ln
, buf
, offs
) :
127 man_ptext(man
, ln
, buf
, offs
));
131 man_free1(struct man
*man
)
135 man_node_delete(man
, man
->first
);
137 free(man
->meta
.title
);
138 if (man
->meta
.source
)
139 free(man
->meta
.source
);
141 free(man
->meta
.date
);
145 free(man
->meta
.msec
);
149 man_alloc1(struct man
*man
)
152 memset(&man
->meta
, 0, sizeof(struct man_meta
));
154 man
->last
= mandoc_calloc(1, sizeof(struct man_node
));
155 man
->first
= man
->last
;
156 man
->last
->type
= MAN_ROOT
;
157 man
->last
->tok
= MAN_MAX
;
158 man
->next
= MAN_NEXT_CHILD
;
163 man_node_append(struct man
*man
, struct man_node
*p
)
168 assert(MAN_ROOT
!= p
->type
);
171 case MAN_NEXT_SIBLING
:
174 p
->parent
= man
->last
->parent
;
177 man
->last
->child
= p
;
178 p
->parent
= man
->last
;
190 if (p
->tok
== MAN_SH
|| p
->tok
== MAN_SS
)
191 man
->flags
&= ~MAN_LITERAL
;
194 assert(MAN_BLOCK
== p
->parent
->type
);
198 assert(MAN_BLOCK
== p
->parent
->type
);
202 assert(MAN_BLOCK
== p
->parent
->type
);
215 if ( ! man_valid_post(man
))
225 static struct man_node
*
226 man_node_alloc(struct man
*man
, int line
, int pos
,
227 enum man_type type
, enum mant tok
)
231 p
= mandoc_calloc(1, sizeof(struct man_node
));
237 if (MAN_NEWLINE
& man
->flags
)
238 p
->flags
|= MAN_LINE
;
239 man
->flags
&= ~MAN_NEWLINE
;
244 man_elem_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
248 p
= man_node_alloc(man
, line
, pos
, MAN_ELEM
, tok
);
249 if ( ! man_node_append(man
, p
))
251 man
->next
= MAN_NEXT_CHILD
;
256 man_tail_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
260 p
= man_node_alloc(man
, line
, pos
, MAN_TAIL
, tok
);
261 if ( ! man_node_append(man
, p
))
263 man
->next
= MAN_NEXT_CHILD
;
268 man_head_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
272 p
= man_node_alloc(man
, line
, pos
, MAN_HEAD
, tok
);
273 if ( ! man_node_append(man
, p
))
275 man
->next
= MAN_NEXT_CHILD
;
280 man_body_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
284 p
= man_node_alloc(man
, line
, pos
, MAN_BODY
, tok
);
285 if ( ! man_node_append(man
, p
))
287 man
->next
= MAN_NEXT_CHILD
;
292 man_block_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
296 p
= man_node_alloc(man
, line
, pos
, MAN_BLOCK
, tok
);
297 if ( ! man_node_append(man
, p
))
299 man
->next
= MAN_NEXT_CHILD
;
304 man_word_alloc(struct man
*man
, int line
, int pos
, const char *word
)
308 n
= man_node_alloc(man
, line
, pos
, MAN_TEXT
, MAN_MAX
);
309 n
->string
= roff_strdup(man
->roff
, word
);
311 if ( ! man_node_append(man
, n
))
314 man
->next
= MAN_NEXT_SIBLING
;
319 man_word_append(struct man
*man
, const char *word
)
322 char *addstr
, *newstr
;
325 addstr
= roff_strdup(man
->roff
, word
);
326 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
330 man
->next
= MAN_NEXT_SIBLING
;
334 * Free all of the resources held by a node. This does NOT unlink a
335 * node from its context; for that, see man_node_unlink().
338 man_node_free(struct man_node
*p
)
347 man_node_delete(struct man
*man
, struct man_node
*p
)
351 man_node_delete(man
, p
->child
);
353 man_node_unlink(man
, p
);
358 man_addeqn(struct man
*man
, const struct eqn
*ep
)
362 n
= man_node_alloc(man
, ep
->ln
, ep
->pos
, MAN_EQN
, MAN_MAX
);
364 if (ep
->ln
> man
->last
->line
)
365 n
->flags
|= MAN_LINE
;
367 if ( ! man_node_append(man
, n
))
370 man
->next
= MAN_NEXT_SIBLING
;
371 return(man_descope(man
, ep
->ln
, ep
->pos
));
375 man_addspan(struct man
*man
, const struct tbl_span
*sp
)
379 n
= man_node_alloc(man
, sp
->line
, 0, MAN_TBL
, MAN_MAX
);
382 if ( ! man_node_append(man
, n
))
385 man
->next
= MAN_NEXT_SIBLING
;
386 return(man_descope(man
, sp
->line
, 0));
390 man_descope(struct man
*man
, int line
, int offs
)
393 * Co-ordinate what happens with having a next-line scope open:
394 * first close out the element scope (if applicable), then close
395 * out the block scope (also if applicable).
398 if (MAN_ELINE
& man
->flags
) {
399 man
->flags
&= ~MAN_ELINE
;
400 if ( ! man_unscope(man
, man
->last
->parent
))
404 if ( ! (MAN_BLINE
& man
->flags
))
406 man
->flags
&= ~MAN_BLINE
;
408 if ( ! man_unscope(man
, man
->last
->parent
))
410 return(man_body_alloc(man
, line
, offs
, man
->last
->tok
));
414 man_ptext(struct man
*man
, int line
, char *buf
, int offs
)
418 /* Literal free-form text whitespace is preserved. */
420 if (MAN_LITERAL
& man
->flags
) {
421 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
423 return(man_descope(man
, line
, offs
));
426 for (i
= offs
; ' ' == buf
[i
]; i
++)
427 /* Skip leading whitespace. */ ;
430 * Blank lines are ignored right after headings
431 * but add a single vertical space elsewhere.
434 if ('\0' == buf
[i
]) {
435 /* Allocate a blank entry. */
436 if (MAN_SH
!= man
->last
->tok
&&
437 MAN_SS
!= man
->last
->tok
) {
438 if ( ! man_elem_alloc(man
, line
, offs
, MAN_sp
))
440 man
->next
= MAN_NEXT_SIBLING
;
446 * Warn if the last un-escaped character is whitespace. Then
447 * strip away the remaining spaces (tabs stay!).
450 i
= (int)strlen(buf
);
453 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
454 if (i
> 1 && '\\' != buf
[i
- 2])
455 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
458 for (--i
; i
&& ' ' == buf
[i
]; i
--)
459 /* Spin back to non-space. */ ;
461 /* Jump ahead of escaped whitespace. */
462 i
+= '\\' == buf
[i
] ? 2 : 1;
467 if ( ! man_word_alloc(man
, line
, offs
, buf
+ offs
))
471 * End-of-sentence check. If the last character is an unescaped
472 * EOS character, then flag the node as being the end of a
473 * sentence. The front-end will know how to interpret this.
477 if (mandoc_eos(buf
, (size_t)i
))
478 man
->last
->flags
|= MAN_EOS
;
480 return(man_descope(man
, line
, offs
));
484 man_pmacro(struct man
*man
, int ln
, char *buf
, int offs
)
496 * Copy the first word into a nil-terminated buffer.
497 * Stop when a space, tab, escape, or eoln is encountered.
501 while (i
< 4 && strchr(" \t\\", buf
[offs
]) == NULL
)
502 mac
[i
++] = buf
[offs
++];
506 tok
= (i
> 0 && i
< 4) ? man_hash_find(mac
) : MAN_MAX
;
508 if (tok
== MAN_MAX
) {
509 mandoc_msg(MANDOCERR_MACRO
, man
->parse
,
510 ln
, ppos
, buf
+ ppos
- 1);
514 /* Skip a leading escape sequence or tab. */
519 mandoc_escape(&cp
, NULL
, NULL
);
529 /* Jump to the next non-whitespace word. */
531 while (buf
[offs
] && ' ' == buf
[offs
])
535 * Trailing whitespace. Note that tabs are allowed to be passed
536 * into the parser as "text", so we only warn about spaces here.
539 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
540 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
544 * Remove prior ELINE macro, as it's being clobbered by a new
545 * macro. Note that NSCOPED macros do not close out ELINE
546 * macros---they don't print text---so we let those slip by.
549 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
550 man
->flags
& MAN_ELINE
) {
552 assert(MAN_TEXT
!= n
->type
);
554 /* Remove repeated NSCOPED macros causing ELINE. */
556 if (MAN_NSCOPED
& man_macros
[n
->tok
].flags
)
559 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
, n
->line
,
560 n
->pos
, "%s breaks %s", man_macronames
[tok
],
561 man_macronames
[n
->tok
]);
563 man_node_delete(man
, n
);
564 man
->flags
&= ~MAN_ELINE
;
568 * Remove prior BLINE macro that is being clobbered.
570 if ((man
->flags
& MAN_BLINE
) &&
571 (MAN_BSCOPE
& man_macros
[tok
].flags
)) {
574 /* Might be a text node like 8 in
578 if (MAN_TEXT
== n
->type
)
581 /* Remove element that didn't end BLINE, if any. */
582 if ( ! (MAN_BSCOPE
& man_macros
[n
->tok
].flags
))
585 assert(MAN_HEAD
== n
->type
);
587 assert(MAN_BLOCK
== n
->type
);
588 assert(MAN_SCOPED
& man_macros
[n
->tok
].flags
);
590 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
, n
->line
,
591 n
->pos
, "%s breaks %s", man_macronames
[tok
],
592 man_macronames
[n
->tok
]);
594 man_node_delete(man
, n
);
595 man
->flags
&= ~MAN_BLINE
;
598 /* Remember whether we are in next-line scope for a block head. */
600 bline
= man
->flags
& MAN_BLINE
;
602 /* Call to handler... */
604 assert(man_macros
[tok
].fp
);
605 if ( ! (*man_macros
[tok
].fp
)(man
, tok
, ln
, ppos
, &offs
, buf
))
608 /* In quick mode (for mandocdb), abort after the NAME section. */
610 if (man
->quick
&& MAN_SH
== tok
) {
612 if (MAN_BODY
== n
->type
&&
613 strcmp(n
->prev
->child
->string
, "NAME"))
618 * If we are in a next-line scope for a block head,
619 * close it out now and switch to the body,
620 * unless the next-line scope is allowed to continue.
623 if ( ! bline
|| man
->flags
& MAN_ELINE
||
624 man_macros
[tok
].flags
& MAN_NSCOPED
)
627 assert(MAN_BLINE
& man
->flags
);
628 man
->flags
&= ~MAN_BLINE
;
630 if ( ! man_unscope(man
, man
->last
->parent
))
632 return(man_body_alloc(man
, ln
, ppos
, man
->last
->tok
));
636 * Unlink a node from its context. If "man" is provided, the last parse
637 * point will also be adjusted accordingly.
640 man_node_unlink(struct man
*man
, struct man_node
*n
)
643 /* Adjust siblings. */
646 n
->prev
->next
= n
->next
;
648 n
->next
->prev
= n
->prev
;
654 if (n
->parent
->child
== n
)
655 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
658 /* Adjust parse point, if applicable. */
660 if (man
&& man
->last
== n
) {
661 /*XXX: this can occur when bailing from validation. */
662 /*assert(NULL == n->next);*/
665 man
->next
= MAN_NEXT_SIBLING
;
667 man
->last
= n
->parent
;
668 man
->next
= MAN_NEXT_CHILD
;
672 if (man
&& man
->first
== n
)
676 const struct mparse
*
677 man_mparse(const struct man
*man
)
680 assert(man
&& man
->parse
);
685 man_deroff(char **dest
, const struct man_node
*n
)
690 if (MAN_TEXT
!= n
->type
) {
691 for (n
= n
->child
; n
; n
= n
->next
)
696 /* Skip leading whitespace and escape sequences. */
699 while ('\0' != *cp
) {
702 mandoc_escape((const char **)&cp
, NULL
, NULL
);
703 } else if (isspace((unsigned char)*cp
))
709 /* Skip trailing whitespace. */
711 for (sz
= strlen(cp
); sz
; sz
--)
712 if (0 == isspace((unsigned char)cp
[sz
-1]))
715 /* Skip empty strings. */
721 *dest
= mandoc_strndup(cp
, sz
);
725 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);