]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.154 2015/04/18 16:34:25 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 roff_man
*);
53 static void man_breakscope(struct roff_man
*, int);
54 static void man_descope(struct roff_man
*, int, int);
55 static void man_free1(struct roff_man
*);
56 static struct roff_node
*man_node_alloc(struct roff_man
*, int, int,
58 static void man_node_append(struct roff_man
*,
60 static void man_node_free(struct roff_node
*);
61 static void man_node_unlink(struct roff_man
*,
63 static int man_ptext(struct roff_man
*, int, char *, int);
64 static int man_pmacro(struct roff_man
*, int, char *, int);
67 const struct roff_node
*
68 man_node(const struct roff_man
*man
)
74 const struct roff_meta
*
75 man_meta(const struct roff_man
*man
)
82 man_reset(struct roff_man
*man
)
90 man_free(struct roff_man
*man
)
98 man_alloc(struct roff
*roff
, struct mparse
*parse
,
99 const char *defos
, int quick
)
103 p
= mandoc_calloc(1, sizeof(*p
));
116 man_endparse(struct roff_man
*man
)
123 man_parseln(struct roff_man
*man
, int ln
, char *buf
, int offs
)
126 if (man
->last
->type
!= ROFFT_EQN
|| ln
> man
->last
->line
)
127 man
->flags
|= MAN_NEWLINE
;
129 return (roff_getcontrol(man
->roff
, buf
, &offs
) ?
130 man_pmacro(man
, ln
, buf
, offs
) :
131 man_ptext(man
, ln
, buf
, offs
));
135 man_free1(struct roff_man
*man
)
139 man_node_delete(man
, man
->first
);
140 free(man
->meta
.title
);
142 free(man
->meta
.date
);
144 free(man
->meta
.msec
);
148 man_alloc1(struct roff_man
*man
)
151 memset(&man
->meta
, 0, sizeof(man
->meta
));
152 man
->macroset
= MACROSET_MAN
;
154 man
->last
= mandoc_calloc(1, sizeof(*man
->last
));
155 man
->first
= man
->last
;
156 man
->last
->type
= ROFFT_ROOT
;
157 man
->last
->tok
= MAN_MAX
;
158 man
->next
= ROFF_NEXT_CHILD
;
163 man_node_append(struct roff_man
*man
, struct roff_node
*p
)
168 assert(p
->type
!= ROFFT_ROOT
);
171 case ROFF_NEXT_SIBLING
:
174 p
->parent
= man
->last
->parent
;
176 case ROFF_NEXT_CHILD
:
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(p
->parent
->type
== ROFFT_BLOCK
);
198 assert(p
->parent
->type
== ROFFT_BLOCK
);
218 static struct roff_node
*
219 man_node_alloc(struct roff_man
*man
, int line
, int pos
,
220 enum roff_type type
, int tok
)
224 p
= mandoc_calloc(1, sizeof(*p
));
230 if (man
->flags
& MAN_NEWLINE
)
231 p
->flags
|= MAN_LINE
;
232 man
->flags
&= ~MAN_NEWLINE
;
237 man_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
241 p
= man_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
242 man_node_append(man
, p
);
243 man
->next
= ROFF_NEXT_CHILD
;
247 man_head_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
251 p
= man_node_alloc(man
, line
, pos
, ROFFT_HEAD
, tok
);
252 man_node_append(man
, p
);
253 man
->next
= ROFF_NEXT_CHILD
;
257 man_body_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
261 p
= man_node_alloc(man
, line
, pos
, ROFFT_BODY
, tok
);
262 man_node_append(man
, p
);
263 man
->next
= ROFF_NEXT_CHILD
;
267 man_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
271 p
= man_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
272 man_node_append(man
, p
);
273 man
->next
= ROFF_NEXT_CHILD
;
277 man_word_alloc(struct roff_man
*man
, int line
, int pos
, const char *word
)
281 n
= man_node_alloc(man
, line
, pos
, ROFFT_TEXT
, MAN_MAX
);
282 n
->string
= roff_strdup(man
->roff
, word
);
283 man_node_append(man
, n
);
284 man
->next
= ROFF_NEXT_SIBLING
;
288 man_word_append(struct roff_man
*man
, const char *word
)
291 char *addstr
, *newstr
;
294 addstr
= roff_strdup(man
->roff
, word
);
295 mandoc_asprintf(&newstr
, "%s %s", n
->string
, addstr
);
299 man
->next
= ROFF_NEXT_SIBLING
;
303 * Free all of the resources held by a node. This does NOT unlink a
304 * node from its context; for that, see man_node_unlink().
307 man_node_free(struct roff_node
*p
)
315 man_node_delete(struct roff_man
*man
, struct roff_node
*p
)
319 man_node_delete(man
, p
->child
);
321 man_node_unlink(man
, p
);
326 man_addeqn(struct roff_man
*man
, const struct eqn
*ep
)
330 n
= man_node_alloc(man
, ep
->ln
, ep
->pos
, ROFFT_EQN
, MAN_MAX
);
332 if (ep
->ln
> man
->last
->line
)
333 n
->flags
|= MAN_LINE
;
334 man_node_append(man
, n
);
335 man
->next
= ROFF_NEXT_SIBLING
;
336 man_descope(man
, ep
->ln
, ep
->pos
);
340 man_addspan(struct roff_man
*man
, const struct tbl_span
*sp
)
344 man_breakscope(man
, MAN_MAX
);
345 n
= man_node_alloc(man
, sp
->line
, 0, ROFFT_TBL
, MAN_MAX
);
347 man_node_append(man
, n
);
348 man
->next
= ROFF_NEXT_SIBLING
;
349 man_descope(man
, sp
->line
, 0);
353 man_descope(struct roff_man
*man
, int line
, int offs
)
356 * Co-ordinate what happens with having a next-line scope open:
357 * first close out the element scope (if applicable), then close
358 * out the block scope (also if applicable).
361 if (man
->flags
& MAN_ELINE
) {
362 man
->flags
&= ~MAN_ELINE
;
363 man_unscope(man
, man
->last
->parent
);
365 if ( ! (man
->flags
& MAN_BLINE
))
367 man
->flags
&= ~MAN_BLINE
;
368 man_unscope(man
, man
->last
->parent
);
369 man_body_alloc(man
, line
, offs
, man
->last
->tok
);
373 man_ptext(struct roff_man
*man
, int line
, char *buf
, int offs
)
377 /* Literal free-form text whitespace is preserved. */
379 if (man
->flags
& MAN_LITERAL
) {
380 man_word_alloc(man
, line
, offs
, buf
+ offs
);
381 man_descope(man
, line
, offs
);
385 for (i
= offs
; buf
[i
] == ' '; i
++)
386 /* Skip leading whitespace. */ ;
389 * Blank lines are ignored right after headings
390 * but add a single vertical space elsewhere.
393 if (buf
[i
] == '\0') {
394 /* Allocate a blank entry. */
395 if (man
->last
->tok
!= MAN_SH
&&
396 man
->last
->tok
!= MAN_SS
) {
397 man_elem_alloc(man
, line
, offs
, MAN_sp
);
398 man
->next
= ROFF_NEXT_SIBLING
;
404 * Warn if the last un-escaped character is whitespace. Then
405 * strip away the remaining spaces (tabs stay!).
408 i
= (int)strlen(buf
);
411 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
412 if (i
> 1 && '\\' != buf
[i
- 2])
413 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
416 for (--i
; i
&& ' ' == buf
[i
]; i
--)
417 /* Spin back to non-space. */ ;
419 /* Jump ahead of escaped whitespace. */
420 i
+= '\\' == buf
[i
] ? 2 : 1;
424 man_word_alloc(man
, line
, offs
, buf
+ offs
);
427 * End-of-sentence check. If the last character is an unescaped
428 * EOS character, then flag the node as being the end of a
429 * sentence. The front-end will know how to interpret this.
433 if (mandoc_eos(buf
, (size_t)i
))
434 man
->last
->flags
|= MAN_EOS
;
436 man_descope(man
, line
, offs
);
441 man_pmacro(struct roff_man
*man
, int ln
, char *buf
, int offs
)
453 * Copy the first word into a nil-terminated buffer.
454 * Stop when a space, tab, escape, or eoln is encountered.
458 while (i
< 4 && strchr(" \t\\", buf
[offs
]) == NULL
)
459 mac
[i
++] = buf
[offs
++];
463 tok
= (i
> 0 && i
< 4) ? man_hash_find(mac
) : MAN_MAX
;
465 if (tok
== MAN_MAX
) {
466 mandoc_msg(MANDOCERR_MACRO
, man
->parse
,
467 ln
, ppos
, buf
+ ppos
- 1);
471 /* Skip a leading escape sequence or tab. */
476 mandoc_escape(&cp
, NULL
, NULL
);
486 /* Jump to the next non-whitespace word. */
488 while (buf
[offs
] && buf
[offs
] == ' ')
492 * Trailing whitespace. Note that tabs are allowed to be passed
493 * into the parser as "text", so we only warn about spaces here.
496 if (buf
[offs
] == '\0' && buf
[offs
- 1] == ' ')
497 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
501 * Some macros break next-line scopes; otherwise, remember
502 * whether we are in next-line scope for a block head.
505 man_breakscope(man
, tok
);
506 bline
= man
->flags
& MAN_BLINE
;
508 /* Call to handler... */
510 assert(man_macros
[tok
].fp
);
511 (*man_macros
[tok
].fp
)(man
, tok
, ln
, ppos
, &offs
, buf
);
513 /* In quick mode (for mandocdb), abort after the NAME section. */
515 if (man
->quick
&& tok
== MAN_SH
) {
517 if (n
->type
== ROFFT_BODY
&&
518 strcmp(n
->prev
->child
->string
, "NAME"))
523 * If we are in a next-line scope for a block head,
524 * close it out now and switch to the body,
525 * unless the next-line scope is allowed to continue.
528 if ( ! bline
|| man
->flags
& MAN_ELINE
||
529 man_macros
[tok
].flags
& MAN_NSCOPED
)
532 assert(man
->flags
& MAN_BLINE
);
533 man
->flags
&= ~MAN_BLINE
;
535 man_unscope(man
, man
->last
->parent
);
536 man_body_alloc(man
, ln
, ppos
, man
->last
->tok
);
541 man_breakscope(struct roff_man
*man
, int tok
)
546 * An element next line scope is open,
547 * and the new macro is not allowed inside elements.
548 * Delete the element that is being broken.
551 if (man
->flags
& MAN_ELINE
&& (tok
== MAN_MAX
||
552 ! (man_macros
[tok
].flags
& MAN_NSCOPED
))) {
554 assert(n
->type
!= ROFFT_TEXT
);
555 if (man_macros
[n
->tok
].flags
& MAN_NSCOPED
)
558 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
,
559 n
->line
, n
->pos
, "%s breaks %s",
560 tok
== MAN_MAX
? "TS" : man_macronames
[tok
],
561 man_macronames
[n
->tok
]);
563 man_node_delete(man
, n
);
564 man
->flags
&= ~MAN_ELINE
;
568 * A block header next line scope is open,
569 * and the new macro is not allowed inside block headers.
570 * Delete the block that is being broken.
573 if (man
->flags
& MAN_BLINE
&& (tok
== MAN_MAX
||
574 man_macros
[tok
].flags
& MAN_BSCOPE
)) {
576 if (n
->type
== ROFFT_TEXT
)
578 if ( ! (man_macros
[n
->tok
].flags
& MAN_BSCOPE
))
581 assert(n
->type
== ROFFT_HEAD
);
583 assert(n
->type
== ROFFT_BLOCK
);
584 assert(man_macros
[n
->tok
].flags
& MAN_SCOPED
);
586 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
,
587 n
->line
, n
->pos
, "%s breaks %s",
588 tok
== MAN_MAX
? "TS" : man_macronames
[tok
],
589 man_macronames
[n
->tok
]);
591 man_node_delete(man
, n
);
592 man
->flags
&= ~MAN_BLINE
;
597 * Unlink a node from its context. If "man" is provided, the last parse
598 * point will also be adjusted accordingly.
601 man_node_unlink(struct roff_man
*man
, struct roff_node
*n
)
604 /* Adjust siblings. */
607 n
->prev
->next
= n
->next
;
609 n
->next
->prev
= n
->prev
;
615 if (n
->parent
->child
== n
)
616 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
619 /* Adjust parse point, if applicable. */
621 if (man
&& man
->last
== n
) {
622 /*XXX: this can occur when bailing from validation. */
623 /*assert(NULL == n->next);*/
626 man
->next
= ROFF_NEXT_SIBLING
;
628 man
->last
= n
->parent
;
629 man
->next
= ROFF_NEXT_CHILD
;
633 if (man
&& man
->first
== n
)
637 const struct mparse
*
638 man_mparse(const struct roff_man
*man
)
641 assert(man
&& man
->parse
);
646 man_deroff(char **dest
, const struct roff_node
*n
)
651 if (n
->type
!= ROFFT_TEXT
) {
652 for (n
= n
->child
; n
; n
= n
->next
)
657 /* Skip leading whitespace and escape sequences. */
660 while ('\0' != *cp
) {
663 mandoc_escape((const char **)&cp
, NULL
, NULL
);
664 } else if (isspace((unsigned char)*cp
))
670 /* Skip trailing whitespace. */
672 for (sz
= strlen(cp
); sz
; sz
--)
673 if (0 == isspace((unsigned char)cp
[sz
-1]))
676 /* Skip empty strings. */
682 *dest
= mandoc_strndup(cp
, sz
);
686 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);