]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.146 2014/12/28 14:42:27 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
)
121 man_parseln(struct man
*man
, int ln
, char *buf
, int offs
)
124 if (man
->last
->type
!= MAN_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
);
139 free(man
->meta
.source
);
140 free(man
->meta
.date
);
142 free(man
->meta
.msec
);
146 man_alloc1(struct man
*man
)
149 memset(&man
->meta
, 0, sizeof(struct man_meta
));
151 man
->last
= mandoc_calloc(1, sizeof(struct man_node
));
152 man
->first
= man
->last
;
153 man
->last
->type
= MAN_ROOT
;
154 man
->last
->tok
= MAN_MAX
;
155 man
->next
= MAN_NEXT_CHILD
;
160 man_node_append(struct man
*man
, struct man_node
*p
)
165 assert(p
->type
!= MAN_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
== MAN_BLOCK
);
195 assert(p
->parent
->type
== MAN_BLOCK
);
215 static struct man_node
*
216 man_node_alloc(struct man
*man
, int line
, int pos
,
217 enum man_type type
, enum mant tok
)
221 p
= mandoc_calloc(1, sizeof(struct man_node
));
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
, enum mant tok
)
238 p
= man_node_alloc(man
, line
, pos
, MAN_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
, enum mant tok
)
248 p
= man_node_alloc(man
, line
, pos
, MAN_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
, enum mant tok
)
258 p
= man_node_alloc(man
, line
, pos
, MAN_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
, enum mant tok
)
268 p
= man_node_alloc(man
, line
, pos
, MAN_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
, MAN_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 man_node
*p
)
312 man_node_delete(struct man
*man
, struct man_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
, MAN_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 n
= man_node_alloc(man
, sp
->line
, 0, MAN_TBL
, MAN_MAX
);
343 man_node_append(man
, n
);
344 man
->next
= MAN_NEXT_SIBLING
;
345 man_descope(man
, sp
->line
, 0);
349 man_descope(struct man
*man
, int line
, int offs
)
352 * Co-ordinate what happens with having a next-line scope open:
353 * first close out the element scope (if applicable), then close
354 * out the block scope (also if applicable).
357 if (man
->flags
& MAN_ELINE
) {
358 man
->flags
&= ~MAN_ELINE
;
359 man_unscope(man
, man
->last
->parent
);
361 if ( ! (man
->flags
& MAN_BLINE
))
363 man
->flags
&= ~MAN_BLINE
;
364 man_unscope(man
, man
->last
->parent
);
365 man_body_alloc(man
, line
, offs
, man
->last
->tok
);
369 man_ptext(struct man
*man
, int line
, char *buf
, int offs
)
373 /* Literal free-form text whitespace is preserved. */
375 if (man
->flags
& MAN_LITERAL
) {
376 man_word_alloc(man
, line
, offs
, buf
+ offs
);
377 man_descope(man
, line
, offs
);
381 for (i
= offs
; buf
[i
] == ' '; i
++)
382 /* Skip leading whitespace. */ ;
385 * Blank lines are ignored right after headings
386 * but add a single vertical space elsewhere.
389 if (buf
[i
] == '\0') {
390 /* Allocate a blank entry. */
391 if (man
->last
->tok
!= MAN_SH
&&
392 man
->last
->tok
!= MAN_SS
) {
393 man_elem_alloc(man
, line
, offs
, MAN_sp
);
394 man
->next
= MAN_NEXT_SIBLING
;
400 * Warn if the last un-escaped character is whitespace. Then
401 * strip away the remaining spaces (tabs stay!).
404 i
= (int)strlen(buf
);
407 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
408 if (i
> 1 && '\\' != buf
[i
- 2])
409 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
412 for (--i
; i
&& ' ' == buf
[i
]; i
--)
413 /* Spin back to non-space. */ ;
415 /* Jump ahead of escaped whitespace. */
416 i
+= '\\' == buf
[i
] ? 2 : 1;
420 man_word_alloc(man
, line
, offs
, buf
+ offs
);
423 * End-of-sentence check. If the last character is an unescaped
424 * EOS character, then flag the node as being the end of a
425 * sentence. The front-end will know how to interpret this.
429 if (mandoc_eos(buf
, (size_t)i
))
430 man
->last
->flags
|= MAN_EOS
;
432 man_descope(man
, line
, offs
);
437 man_pmacro(struct man
*man
, int ln
, char *buf
, int offs
)
449 * Copy the first word into a nil-terminated buffer.
450 * Stop when a space, tab, escape, or eoln is encountered.
454 while (i
< 4 && strchr(" \t\\", buf
[offs
]) == NULL
)
455 mac
[i
++] = buf
[offs
++];
459 tok
= (i
> 0 && i
< 4) ? man_hash_find(mac
) : MAN_MAX
;
461 if (tok
== MAN_MAX
) {
462 mandoc_msg(MANDOCERR_MACRO
, man
->parse
,
463 ln
, ppos
, buf
+ ppos
- 1);
467 /* Skip a leading escape sequence or tab. */
472 mandoc_escape(&cp
, NULL
, NULL
);
482 /* Jump to the next non-whitespace word. */
484 while (buf
[offs
] && buf
[offs
] == ' ')
488 * Trailing whitespace. Note that tabs are allowed to be passed
489 * into the parser as "text", so we only warn about spaces here.
492 if (buf
[offs
] == '\0' && buf
[offs
- 1] == ' ')
493 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
497 * Remove prior ELINE macro, as it's being clobbered by a new
498 * macro. Note that NSCOPED macros do not close out ELINE
499 * macros---they don't print text---so we let those slip by.
502 if ( ! (man_macros
[tok
].flags
& MAN_NSCOPED
) &&
503 man
->flags
& MAN_ELINE
) {
505 assert(MAN_TEXT
!= n
->type
);
507 /* Remove repeated NSCOPED macros causing ELINE. */
509 if (man_macros
[n
->tok
].flags
& MAN_NSCOPED
)
512 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
, n
->line
,
513 n
->pos
, "%s breaks %s", man_macronames
[tok
],
514 man_macronames
[n
->tok
]);
516 man_node_delete(man
, n
);
517 man
->flags
&= ~MAN_ELINE
;
521 * Remove prior BLINE macro that is being clobbered.
523 if ((man
->flags
& MAN_BLINE
) &&
524 (man_macros
[tok
].flags
& MAN_BSCOPE
)) {
527 /* Might be a text node like 8 in
531 if (n
->type
== MAN_TEXT
)
534 /* Remove element that didn't end BLINE, if any. */
535 if ( ! (man_macros
[n
->tok
].flags
& MAN_BSCOPE
))
538 assert(n
->type
== MAN_HEAD
);
540 assert(n
->type
== MAN_BLOCK
);
541 assert(man_macros
[n
->tok
].flags
& MAN_SCOPED
);
543 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
, n
->line
,
544 n
->pos
, "%s breaks %s", man_macronames
[tok
],
545 man_macronames
[n
->tok
]);
547 man_node_delete(man
, n
);
548 man
->flags
&= ~MAN_BLINE
;
551 /* Remember whether we are in next-line scope for a block head. */
553 bline
= man
->flags
& MAN_BLINE
;
555 /* Call to handler... */
557 assert(man_macros
[tok
].fp
);
558 (*man_macros
[tok
].fp
)(man
, tok
, ln
, ppos
, &offs
, buf
);
560 /* In quick mode (for mandocdb), abort after the NAME section. */
562 if (man
->quick
&& tok
== MAN_SH
) {
564 if (n
->type
== MAN_BODY
&&
565 strcmp(n
->prev
->child
->string
, "NAME"))
570 * If we are in a next-line scope for a block head,
571 * close it out now and switch to the body,
572 * unless the next-line scope is allowed to continue.
575 if ( ! bline
|| man
->flags
& MAN_ELINE
||
576 man_macros
[tok
].flags
& MAN_NSCOPED
)
579 assert(man
->flags
& MAN_BLINE
);
580 man
->flags
&= ~MAN_BLINE
;
582 man_unscope(man
, man
->last
->parent
);
583 man_body_alloc(man
, ln
, ppos
, man
->last
->tok
);
588 * Unlink a node from its context. If "man" is provided, the last parse
589 * point will also be adjusted accordingly.
592 man_node_unlink(struct man
*man
, struct man_node
*n
)
595 /* Adjust siblings. */
598 n
->prev
->next
= n
->next
;
600 n
->next
->prev
= n
->prev
;
606 if (n
->parent
->child
== n
)
607 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
610 /* Adjust parse point, if applicable. */
612 if (man
&& man
->last
== n
) {
613 /*XXX: this can occur when bailing from validation. */
614 /*assert(NULL == n->next);*/
617 man
->next
= MAN_NEXT_SIBLING
;
619 man
->last
= n
->parent
;
620 man
->next
= MAN_NEXT_CHILD
;
624 if (man
&& man
->first
== n
)
628 const struct mparse
*
629 man_mparse(const struct man
*man
)
632 assert(man
&& man
->parse
);
637 man_deroff(char **dest
, const struct man_node
*n
)
642 if (n
->type
!= MAN_TEXT
) {
643 for (n
= n
->child
; n
; n
= n
->next
)
648 /* Skip leading whitespace and escape sequences. */
651 while ('\0' != *cp
) {
654 mandoc_escape((const char **)&cp
, NULL
, NULL
);
655 } else if (isspace((unsigned char)*cp
))
661 /* Skip trailing whitespace. */
663 for (sz
= strlen(cp
); sz
; sz
--)
664 if (0 == isspace((unsigned char)cp
[sz
-1]))
667 /* Skip empty strings. */
673 *dest
= mandoc_strndup(cp
, sz
);
677 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);