]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.147 2015/01/15 04:26:40 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 void man_node_append(struct man
*, struct man_node
*);
54 static void man_node_free(struct man_node
*);
55 static void man_node_unlink(struct man
*,
57 static int man_ptext(struct man
*, int, char *, int);
58 static int man_pmacro(struct man
*, int, char *, int);
59 static void man_free1(struct man
*);
60 static void man_alloc1(struct man
*);
61 static void man_descope(struct man
*, int, int);
64 const struct man_node
*
65 man_node(const struct man
*man
)
71 const struct man_meta
*
72 man_meta(const struct man
*man
)
79 man_reset(struct man
*man
)
87 man_free(struct man
*man
)
95 man_alloc(struct roff
*roff
, struct mparse
*parse
,
96 const char *defos
, int quick
)
100 p
= mandoc_calloc(1, sizeof(struct man
));
113 man_endparse(struct man
*man
)
120 man_parseln(struct man
*man
, int ln
, char *buf
, int offs
)
123 if (man
->last
->type
!= MAN_EQN
|| ln
> man
->last
->line
)
124 man
->flags
|= MAN_NEWLINE
;
126 return (roff_getcontrol(man
->roff
, buf
, &offs
) ?
127 man_pmacro(man
, ln
, buf
, offs
) :
128 man_ptext(man
, ln
, buf
, offs
));
132 man_free1(struct man
*man
)
136 man_node_delete(man
, man
->first
);
137 free(man
->meta
.title
);
138 free(man
->meta
.source
);
139 free(man
->meta
.date
);
141 free(man
->meta
.msec
);
145 man_alloc1(struct man
*man
)
148 memset(&man
->meta
, 0, sizeof(struct man_meta
));
150 man
->last
= mandoc_calloc(1, sizeof(struct man_node
));
151 man
->first
= man
->last
;
152 man
->last
->type
= MAN_ROOT
;
153 man
->last
->tok
= MAN_MAX
;
154 man
->next
= MAN_NEXT_CHILD
;
159 man_node_append(struct man
*man
, struct man_node
*p
)
164 assert(p
->type
!= MAN_ROOT
);
167 case MAN_NEXT_SIBLING
:
170 p
->parent
= man
->last
->parent
;
173 man
->last
->child
= p
;
174 p
->parent
= man
->last
;
186 if (p
->tok
== MAN_SH
|| p
->tok
== MAN_SS
)
187 man
->flags
&= ~MAN_LITERAL
;
190 assert(p
->parent
->type
== MAN_BLOCK
);
194 assert(p
->parent
->type
== MAN_BLOCK
);
214 static struct man_node
*
215 man_node_alloc(struct man
*man
, int line
, int pos
,
216 enum man_type type
, enum mant tok
)
220 p
= mandoc_calloc(1, sizeof(struct man_node
));
226 if (man
->flags
& MAN_NEWLINE
)
227 p
->flags
|= MAN_LINE
;
228 man
->flags
&= ~MAN_NEWLINE
;
233 man_elem_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
237 p
= man_node_alloc(man
, line
, pos
, MAN_ELEM
, tok
);
238 man_node_append(man
, p
);
239 man
->next
= MAN_NEXT_CHILD
;
243 man_head_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
247 p
= man_node_alloc(man
, line
, pos
, MAN_HEAD
, tok
);
248 man_node_append(man
, p
);
249 man
->next
= MAN_NEXT_CHILD
;
253 man_body_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
257 p
= man_node_alloc(man
, line
, pos
, MAN_BODY
, tok
);
258 man_node_append(man
, p
);
259 man
->next
= MAN_NEXT_CHILD
;
263 man_block_alloc(struct man
*man
, int line
, int pos
, enum mant tok
)
267 p
= man_node_alloc(man
, line
, pos
, MAN_BLOCK
, tok
);
268 man_node_append(man
, p
);
269 man
->next
= MAN_NEXT_CHILD
;
273 man_word_alloc(struct man
*man
, int line
, int pos
, const char *word
)
277 n
= man_node_alloc(man
, line
, pos
, MAN_TEXT
, MAN_MAX
);
278 n
->string
= roff_strdup(man
->roff
, word
);
279 man_node_append(man
, n
);
280 man
->next
= MAN_NEXT_SIBLING
;
284 man_word_append(struct man
*man
, const char *word
)
287 char *addstr
, *newstr
;
290 addstr
= roff_strdup(man
->roff
, word
);
291 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
295 man
->next
= MAN_NEXT_SIBLING
;
299 * Free all of the resources held by a node. This does NOT unlink a
300 * node from its context; for that, see man_node_unlink().
303 man_node_free(struct man_node
*p
)
311 man_node_delete(struct man
*man
, struct man_node
*p
)
315 man_node_delete(man
, p
->child
);
317 man_node_unlink(man
, p
);
322 man_addeqn(struct man
*man
, const struct eqn
*ep
)
326 n
= man_node_alloc(man
, ep
->ln
, ep
->pos
, MAN_EQN
, MAN_MAX
);
328 if (ep
->ln
> man
->last
->line
)
329 n
->flags
|= MAN_LINE
;
330 man_node_append(man
, n
);
331 man
->next
= MAN_NEXT_SIBLING
;
332 man_descope(man
, ep
->ln
, ep
->pos
);
336 man_addspan(struct man
*man
, const struct tbl_span
*sp
)
340 n
= man_node_alloc(man
, sp
->line
, 0, MAN_TBL
, MAN_MAX
);
342 man_node_append(man
, n
);
343 man
->next
= MAN_NEXT_SIBLING
;
344 man_descope(man
, sp
->line
, 0);
348 man_descope(struct man
*man
, int line
, int offs
)
351 * Co-ordinate what happens with having a next-line scope open:
352 * first close out the element scope (if applicable), then close
353 * out the block scope (also if applicable).
356 if (man
->flags
& MAN_ELINE
) {
357 man
->flags
&= ~MAN_ELINE
;
358 man_unscope(man
, man
->last
->parent
);
360 if ( ! (man
->flags
& MAN_BLINE
))
362 man
->flags
&= ~MAN_BLINE
;
363 man_unscope(man
, man
->last
->parent
);
364 man_body_alloc(man
, line
, offs
, man
->last
->tok
);
368 man_ptext(struct man
*man
, int line
, char *buf
, int offs
)
372 /* Literal free-form text whitespace is preserved. */
374 if (man
->flags
& MAN_LITERAL
) {
375 man_word_alloc(man
, line
, offs
, buf
+ offs
);
376 man_descope(man
, line
, offs
);
380 for (i
= offs
; buf
[i
] == ' '; i
++)
381 /* Skip leading whitespace. */ ;
384 * Blank lines are ignored right after headings
385 * but add a single vertical space elsewhere.
388 if (buf
[i
] == '\0') {
389 /* Allocate a blank entry. */
390 if (man
->last
->tok
!= MAN_SH
&&
391 man
->last
->tok
!= MAN_SS
) {
392 man_elem_alloc(man
, line
, offs
, MAN_sp
);
393 man
->next
= MAN_NEXT_SIBLING
;
399 * Warn if the last un-escaped character is whitespace. Then
400 * strip away the remaining spaces (tabs stay!).
403 i
= (int)strlen(buf
);
406 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
407 if (i
> 1 && '\\' != buf
[i
- 2])
408 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
411 for (--i
; i
&& ' ' == buf
[i
]; i
--)
412 /* Spin back to non-space. */ ;
414 /* Jump ahead of escaped whitespace. */
415 i
+= '\\' == buf
[i
] ? 2 : 1;
419 man_word_alloc(man
, line
, offs
, buf
+ offs
);
422 * End-of-sentence check. If the last character is an unescaped
423 * EOS character, then flag the node as being the end of a
424 * sentence. The front-end will know how to interpret this.
428 if (mandoc_eos(buf
, (size_t)i
))
429 man
->last
->flags
|= MAN_EOS
;
431 man_descope(man
, line
, offs
);
436 man_pmacro(struct man
*man
, int ln
, char *buf
, int offs
)
448 * Copy the first word into a nil-terminated buffer.
449 * Stop when a space, tab, escape, or eoln is encountered.
453 while (i
< 4 && strchr(" \t\\", buf
[offs
]) == NULL
)
454 mac
[i
++] = buf
[offs
++];
458 tok
= (i
> 0 && i
< 4) ? man_hash_find(mac
) : MAN_MAX
;
460 if (tok
== MAN_MAX
) {
461 mandoc_msg(MANDOCERR_MACRO
, man
->parse
,
462 ln
, ppos
, buf
+ ppos
- 1);
466 /* Skip a leading escape sequence or tab. */
471 mandoc_escape(&cp
, NULL
, NULL
);
481 /* Jump to the next non-whitespace word. */
483 while (buf
[offs
] && buf
[offs
] == ' ')
487 * Trailing whitespace. Note that tabs are allowed to be passed
488 * into the parser as "text", so we only warn about spaces here.
491 if (buf
[offs
] == '\0' && buf
[offs
- 1] == ' ')
492 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
496 * Remove prior ELINE macro, as it's being clobbered by a new
497 * macro. Note that NSCOPED macros do not close out ELINE
498 * macros---they don't print text---so we let those slip by.
501 if ( ! (man_macros
[tok
].flags
& MAN_NSCOPED
) &&
502 man
->flags
& MAN_ELINE
) {
504 assert(MAN_TEXT
!= n
->type
);
506 /* Remove repeated NSCOPED macros causing ELINE. */
508 if (man_macros
[n
->tok
].flags
& MAN_NSCOPED
)
511 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
, n
->line
,
512 n
->pos
, "%s breaks %s", man_macronames
[tok
],
513 man_macronames
[n
->tok
]);
515 man_node_delete(man
, n
);
516 man
->flags
&= ~MAN_ELINE
;
520 * Remove prior BLINE macro that is being clobbered.
522 if ((man
->flags
& MAN_BLINE
) &&
523 (man_macros
[tok
].flags
& MAN_BSCOPE
)) {
526 /* Might be a text node like 8 in
530 if (n
->type
== MAN_TEXT
)
533 /* Remove element that didn't end BLINE, if any. */
534 if ( ! (man_macros
[n
->tok
].flags
& MAN_BSCOPE
))
537 assert(n
->type
== MAN_HEAD
);
539 assert(n
->type
== MAN_BLOCK
);
540 assert(man_macros
[n
->tok
].flags
& MAN_SCOPED
);
542 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
, n
->line
,
543 n
->pos
, "%s breaks %s", man_macronames
[tok
],
544 man_macronames
[n
->tok
]);
546 man_node_delete(man
, n
);
547 man
->flags
&= ~MAN_BLINE
;
550 /* Remember whether we are in next-line scope for a block head. */
552 bline
= man
->flags
& MAN_BLINE
;
554 /* Call to handler... */
556 assert(man_macros
[tok
].fp
);
557 (*man_macros
[tok
].fp
)(man
, tok
, ln
, ppos
, &offs
, buf
);
559 /* In quick mode (for mandocdb), abort after the NAME section. */
561 if (man
->quick
&& tok
== MAN_SH
) {
563 if (n
->type
== MAN_BODY
&&
564 strcmp(n
->prev
->child
->string
, "NAME"))
569 * If we are in a next-line scope for a block head,
570 * close it out now and switch to the body,
571 * unless the next-line scope is allowed to continue.
574 if ( ! bline
|| man
->flags
& MAN_ELINE
||
575 man_macros
[tok
].flags
& MAN_NSCOPED
)
578 assert(man
->flags
& MAN_BLINE
);
579 man
->flags
&= ~MAN_BLINE
;
581 man_unscope(man
, man
->last
->parent
);
582 man_body_alloc(man
, ln
, ppos
, man
->last
->tok
);
587 * Unlink a node from its context. If "man" is provided, the last parse
588 * point will also be adjusted accordingly.
591 man_node_unlink(struct man
*man
, struct man_node
*n
)
594 /* Adjust siblings. */
597 n
->prev
->next
= n
->next
;
599 n
->next
->prev
= n
->prev
;
605 if (n
->parent
->child
== n
)
606 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
609 /* Adjust parse point, if applicable. */
611 if (man
&& man
->last
== n
) {
612 /*XXX: this can occur when bailing from validation. */
613 /*assert(NULL == n->next);*/
616 man
->next
= MAN_NEXT_SIBLING
;
618 man
->last
= n
->parent
;
619 man
->next
= MAN_NEXT_CHILD
;
623 if (man
&& man
->first
== n
)
627 const struct mparse
*
628 man_mparse(const struct man
*man
)
631 assert(man
&& man
->parse
);
636 man_deroff(char **dest
, const struct man_node
*n
)
641 if (n
->type
!= MAN_TEXT
) {
642 for (n
= n
->child
; n
; n
= n
->next
)
647 /* Skip leading whitespace and escape sequences. */
650 while ('\0' != *cp
) {
653 mandoc_escape((const char **)&cp
, NULL
, NULL
);
654 } else if (isspace((unsigned char)*cp
))
660 /* Skip trailing whitespace. */
662 for (sz
= strlen(cp
); sz
; sz
--)
663 if (0 == isspace((unsigned char)cp
[sz
-1]))
666 /* Skip empty strings. */
672 *dest
= mandoc_strndup(cp
, sz
);
676 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);