]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.152 2015/04/02 23:48:19 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014, 2015 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 AUTHORS DISCLAIM ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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>
30 #include "mandoc_aux.h"
34 #include "libmandoc.h"
37 const char *const __man_macronames
[MAN_MAX
] = {
38 "br", "TH", "SH", "SS",
39 "TP", "LP", "PP", "P",
40 "IP", "HP", "SM", "SB",
41 "BI", "IB", "BR", "RB",
44 "fi", "RE", "RS", "DT",
45 "UC", "PD", "AT", "in",
46 "ft", "OP", "EX", "EE",
50 const char * const *man_macronames
= __man_macronames
;
52 static void man_alloc1(struct man
*);
53 static void man_breakscope(struct man
*, int);
54 static void man_descope(struct man
*, int, int);
55 static void man_free1(struct man
*);
56 static struct roff_node
*man_node_alloc(struct man
*, int, int,
58 static void man_node_append(struct man
*, struct roff_node
*);
59 static void man_node_free(struct roff_node
*);
60 static void man_node_unlink(struct man
*, struct roff_node
*);
61 static int man_ptext(struct man
*, int, char *, int);
62 static int man_pmacro(struct man
*, int, char *, int);
65 const struct roff_node
*
66 man_node(const struct man
*man
)
72 const struct roff_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
,
97 const char *defos
, int quick
)
101 p
= mandoc_calloc(1, sizeof(struct man
));
114 man_endparse(struct man
*man
)
121 man_parseln(struct man
*man
, int ln
, char *buf
, int offs
)
124 if (man
->last
->type
!= ROFFT_EQN
|| ln
> man
->last
->line
)
125 man
->flags
|= MAN_NEWLINE
;
127 return (roff_getcontrol(man
->roff
, buf
, &offs
) ?
128 man_pmacro(man
, ln
, buf
, offs
) :
129 man_ptext(man
, ln
, buf
, offs
));
133 man_free1(struct man
*man
)
137 man_node_delete(man
, man
->first
);
138 free(man
->meta
.title
);
140 free(man
->meta
.date
);
142 free(man
->meta
.msec
);
146 man_alloc1(struct man
*man
)
149 memset(&man
->meta
, 0, sizeof(man
->meta
));
151 man
->last
= mandoc_calloc(1, sizeof(*man
->last
));
152 man
->first
= man
->last
;
153 man
->last
->type
= ROFFT_ROOT
;
154 man
->last
->tok
= MAN_MAX
;
155 man
->next
= MAN_NEXT_CHILD
;
160 man_node_append(struct man
*man
, struct roff_node
*p
)
165 assert(p
->type
!= ROFFT_ROOT
);
168 case MAN_NEXT_SIBLING
:
171 p
->parent
= man
->last
->parent
;
174 man
->last
->child
= p
;
175 p
->parent
= man
->last
;
187 if (p
->tok
== MAN_SH
|| p
->tok
== MAN_SS
)
188 man
->flags
&= ~MAN_LITERAL
;
191 assert(p
->parent
->type
== ROFFT_BLOCK
);
195 assert(p
->parent
->type
== ROFFT_BLOCK
);
215 static struct roff_node
*
216 man_node_alloc(struct man
*man
, int line
, int pos
,
217 enum roff_type type
, int tok
)
221 p
= mandoc_calloc(1, sizeof(*p
));
227 if (man
->flags
& MAN_NEWLINE
)
228 p
->flags
|= MAN_LINE
;
229 man
->flags
&= ~MAN_NEWLINE
;
234 man_elem_alloc(struct man
*man
, int line
, int pos
, int tok
)
238 p
= man_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
239 man_node_append(man
, p
);
240 man
->next
= MAN_NEXT_CHILD
;
244 man_head_alloc(struct man
*man
, int line
, int pos
, int tok
)
248 p
= man_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
249 man_node_append(man
, p
);
250 man
->next
= MAN_NEXT_CHILD
;
254 man_body_alloc(struct man
*man
, int line
, int pos
, int tok
)
258 p
= man_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
259 man_node_append(man
, p
);
260 man
->next
= MAN_NEXT_CHILD
;
264 man_block_alloc(struct man
*man
, int line
, int pos
, int tok
)
268 p
= man_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
269 man_node_append(man
, p
);
270 man
->next
= MAN_NEXT_CHILD
;
274 man_word_alloc(struct man
*man
, int line
, int pos
, const char *word
)
278 n
= man_node_alloc(man
, line
, pos
, ROFFT_TEXT
, MAN_MAX
);
279 n
->string
= roff_strdup(man
->roff
, word
);
280 man_node_append(man
, n
);
281 man
->next
= MAN_NEXT_SIBLING
;
285 man_word_append(struct man
*man
, const char *word
)
288 char *addstr
, *newstr
;
291 addstr
= roff_strdup(man
->roff
, word
);
292 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
296 man
->next
= MAN_NEXT_SIBLING
;
300 * Free all of the resources held by a node. This does NOT unlink a
301 * node from its context; for that, see man_node_unlink().
304 man_node_free(struct roff_node
*p
)
312 man_node_delete(struct man
*man
, struct roff_node
*p
)
316 man_node_delete(man
, p
->child
);
318 man_node_unlink(man
, p
);
323 man_addeqn(struct man
*man
, const struct eqn
*ep
)
327 n
= man_node_alloc(man
, ep
->ln
, ep
->pos
, ROFFT_EQN
, MAN_MAX
);
329 if (ep
->ln
> man
->last
->line
)
330 n
->flags
|= MAN_LINE
;
331 man_node_append(man
, n
);
332 man
->next
= MAN_NEXT_SIBLING
;
333 man_descope(man
, ep
->ln
, ep
->pos
);
337 man_addspan(struct man
*man
, const struct tbl_span
*sp
)
341 man_breakscope(man
, MAN_MAX
);
342 n
= man_node_alloc(man
, sp
->line
, 0, ROFFT_TBL
, MAN_MAX
);
344 man_node_append(man
, n
);
345 man
->next
= MAN_NEXT_SIBLING
;
346 man_descope(man
, sp
->line
, 0);
350 man_descope(struct man
*man
, int line
, int offs
)
353 * Co-ordinate what happens with having a next-line scope open:
354 * first close out the element scope (if applicable), then close
355 * out the block scope (also if applicable).
358 if (man
->flags
& MAN_ELINE
) {
359 man
->flags
&= ~MAN_ELINE
;
360 man_unscope(man
, man
->last
->parent
);
362 if ( ! (man
->flags
& MAN_BLINE
))
364 man
->flags
&= ~MAN_BLINE
;
365 man_unscope(man
, man
->last
->parent
);
366 man_body_alloc(man
, line
, offs
, man
->last
->tok
);
370 man_ptext(struct man
*man
, int line
, char *buf
, int offs
)
374 /* Literal free-form text whitespace is preserved. */
376 if (man
->flags
& MAN_LITERAL
) {
377 man_word_alloc(man
, line
, offs
, buf
+ offs
);
378 man_descope(man
, line
, offs
);
382 for (i
= offs
; buf
[i
] == ' '; i
++)
383 /* Skip leading whitespace. */ ;
386 * Blank lines are ignored right after headings
387 * but add a single vertical space elsewhere.
390 if (buf
[i
] == '\0') {
391 /* Allocate a blank entry. */
392 if (man
->last
->tok
!= MAN_SH
&&
393 man
->last
->tok
!= MAN_SS
) {
394 man_elem_alloc(man
, line
, offs
, MAN_sp
);
395 man
->next
= MAN_NEXT_SIBLING
;
401 * Warn if the last un-escaped character is whitespace. Then
402 * strip away the remaining spaces (tabs stay!).
405 i
= (int)strlen(buf
);
408 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
409 if (i
> 1 && '\\' != buf
[i
- 2])
410 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
413 for (--i
; i
&& ' ' == buf
[i
]; i
--)
414 /* Spin back to non-space. */ ;
416 /* Jump ahead of escaped whitespace. */
417 i
+= '\\' == buf
[i
] ? 2 : 1;
421 man_word_alloc(man
, line
, offs
, buf
+ offs
);
424 * End-of-sentence check. If the last character is an unescaped
425 * EOS character, then flag the node as being the end of a
426 * sentence. The front-end will know how to interpret this.
430 if (mandoc_eos(buf
, (size_t)i
))
431 man
->last
->flags
|= MAN_EOS
;
433 man_descope(man
, line
, offs
);
438 man_pmacro(struct man
*man
, int ln
, char *buf
, int offs
)
450 * Copy the first word into a nil-terminated buffer.
451 * Stop when a space, tab, escape, or eoln is encountered.
455 while (i
< 4 && strchr(" \t\\", buf
[offs
]) == NULL
)
456 mac
[i
++] = buf
[offs
++];
460 tok
= (i
> 0 && i
< 4) ? man_hash_find(mac
) : MAN_MAX
;
462 if (tok
== MAN_MAX
) {
463 mandoc_msg(MANDOCERR_MACRO
, man
->parse
,
464 ln
, ppos
, buf
+ ppos
- 1);
468 /* Skip a leading escape sequence or tab. */
473 mandoc_escape(&cp
, NULL
, NULL
);
483 /* Jump to the next non-whitespace word. */
485 while (buf
[offs
] && buf
[offs
] == ' ')
489 * Trailing whitespace. Note that tabs are allowed to be passed
490 * into the parser as "text", so we only warn about spaces here.
493 if (buf
[offs
] == '\0' && buf
[offs
- 1] == ' ')
494 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
498 * Some macros break next-line scopes; otherwise, remember
499 * whether we are in next-line scope for a block head.
502 man_breakscope(man
, tok
);
503 bline
= man
->flags
& MAN_BLINE
;
505 /* Call to handler... */
507 assert(man_macros
[tok
].fp
);
508 (*man_macros
[tok
].fp
)(man
, tok
, ln
, ppos
, &offs
, buf
);
510 /* In quick mode (for mandocdb), abort after the NAME section. */
512 if (man
->quick
&& tok
== MAN_SH
) {
514 if (n
->type
== ROFFT_BODY
&&
515 strcmp(n
->prev
->child
->string
, "NAME"))
520 * If we are in a next-line scope for a block head,
521 * close it out now and switch to the body,
522 * unless the next-line scope is allowed to continue.
525 if ( ! bline
|| man
->flags
& MAN_ELINE
||
526 man_macros
[tok
].flags
& MAN_NSCOPED
)
529 assert(man
->flags
& MAN_BLINE
);
530 man
->flags
&= ~MAN_BLINE
;
532 man_unscope(man
, man
->last
->parent
);
533 man_body_alloc(man
, ln
, ppos
, man
->last
->tok
);
538 man_breakscope(struct man
*man
, int tok
)
543 * An element next line scope is open,
544 * and the new macro is not allowed inside elements.
545 * Delete the element that is being broken.
548 if (man
->flags
& MAN_ELINE
&& (tok
== MAN_MAX
||
549 ! (man_macros
[tok
].flags
& MAN_NSCOPED
))) {
551 assert(n
->type
!= ROFFT_TEXT
);
552 if (man_macros
[n
->tok
].flags
& MAN_NSCOPED
)
555 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
,
556 n
->line
, n
->pos
, "%s breaks %s",
557 tok
== MAN_MAX
? "TS" : man_macronames
[tok
],
558 man_macronames
[n
->tok
]);
560 man_node_delete(man
, n
);
561 man
->flags
&= ~MAN_ELINE
;
565 * A block header next line scope is open,
566 * and the new macro is not allowed inside block headers.
567 * Delete the block that is being broken.
570 if (man
->flags
& MAN_BLINE
&& (tok
== MAN_MAX
||
571 man_macros
[tok
].flags
& MAN_BSCOPE
)) {
573 if (n
->type
== ROFFT_TEXT
)
575 if ( ! (man_macros
[n
->tok
].flags
& MAN_BSCOPE
))
578 assert(n
->type
== ROFFT_HEAD
);
580 assert(n
->type
== ROFFT_BLOCK
);
581 assert(man_macros
[n
->tok
].flags
& MAN_SCOPED
);
583 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
,
584 n
->line
, n
->pos
, "%s breaks %s",
585 tok
== MAN_MAX
? "TS" : man_macronames
[tok
],
586 man_macronames
[n
->tok
]);
588 man_node_delete(man
, n
);
589 man
->flags
&= ~MAN_BLINE
;
594 * Unlink a node from its context. If "man" is provided, the last parse
595 * point will also be adjusted accordingly.
598 man_node_unlink(struct man
*man
, struct roff_node
*n
)
601 /* Adjust siblings. */
604 n
->prev
->next
= n
->next
;
606 n
->next
->prev
= n
->prev
;
612 if (n
->parent
->child
== n
)
613 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
616 /* Adjust parse point, if applicable. */
618 if (man
&& man
->last
== n
) {
619 /*XXX: this can occur when bailing from validation. */
620 /*assert(NULL == n->next);*/
623 man
->next
= MAN_NEXT_SIBLING
;
625 man
->last
= n
->parent
;
626 man
->next
= MAN_NEXT_CHILD
;
630 if (man
&& man
->first
== n
)
634 const struct mparse
*
635 man_mparse(const struct man
*man
)
638 assert(man
&& man
->parse
);
643 man_deroff(char **dest
, const struct roff_node
*n
)
648 if (n
->type
!= ROFFT_TEXT
) {
649 for (n
= n
->child
; n
; n
= n
->next
)
654 /* Skip leading whitespace and escape sequences. */
657 while ('\0' != *cp
) {
660 mandoc_escape((const char **)&cp
, NULL
, NULL
);
661 } else if (isspace((unsigned char)*cp
))
667 /* Skip trailing whitespace. */
669 for (sz
= strlen(cp
); sz
; sz
--)
670 if (0 == isspace((unsigned char)cp
[sz
-1]))
673 /* Skip empty strings. */
679 *dest
= mandoc_strndup(cp
, sz
);
683 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);