]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
2792921d10a617106ed6f8ce20c6b2a487d4a7e2
1 /* $Id: man.c,v 1.93 2011/01/01 10:51:30 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
31 #include "libmandoc.h"
33 const char *const __man_macronames
[MAN_MAX
] = {
34 "br", "TH", "SH", "SS",
35 "TP", "LP", "PP", "P",
36 "IP", "HP", "SM", "SB",
37 "BI", "IB", "BR", "RB",
39 "RI", "na", "sp", "nf",
40 "fi", "RE", "RS", "DT",
41 "UC", "PD", "AT", "in",
45 const char * const *man_macronames
= __man_macronames
;
47 static struct man_node
*man_node_alloc(int, int,
48 enum man_type
, enum mant
);
49 static int man_node_append(struct man
*,
51 static void man_node_free(struct man_node
*);
52 static void man_node_unlink(struct man
*,
54 static int man_ptext(struct man
*, int, char *, int);
55 static int man_pmacro(struct man
*, int, char *, int);
56 static void man_free1(struct man
*);
57 static void man_alloc1(struct man
*);
60 const struct man_node
*
61 man_node(const struct man
*m
)
64 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
68 const struct man_meta
*
69 man_meta(const struct man
*m
)
72 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
77 man_reset(struct man
*man
)
86 man_free(struct man
*man
)
95 man_alloc(struct regset
*regs
, void *data
, mandocmsg msg
)
99 p
= mandoc_calloc(1, sizeof(struct man
));
112 man_endparse(struct man
*m
)
115 if (MAN_HALT
& m
->flags
)
117 else if (man_macroend(m
))
119 m
->flags
|= MAN_HALT
;
125 man_parseln(struct man
*m
, int ln
, char *buf
, int offs
)
128 if (MAN_HALT
& m
->flags
)
131 return(('.' == buf
[offs
] || '\'' == buf
[offs
]) ?
132 man_pmacro(m
, ln
, buf
, offs
) :
133 man_ptext(m
, ln
, buf
, offs
));
138 man_free1(struct man
*man
)
142 man_node_delete(man
, man
->first
);
144 free(man
->meta
.title
);
145 if (man
->meta
.source
)
146 free(man
->meta
.source
);
147 if (man
->meta
.rawdate
)
148 free(man
->meta
.rawdate
);
152 free(man
->meta
.msec
);
157 man_alloc1(struct man
*m
)
160 memset(&m
->meta
, 0, sizeof(struct man_meta
));
162 m
->last
= mandoc_calloc(1, sizeof(struct man_node
));
164 m
->last
->type
= MAN_ROOT
;
165 m
->last
->tok
= MAN_MAX
;
166 m
->next
= MAN_NEXT_CHILD
;
171 man_node_append(struct man
*man
, struct man_node
*p
)
176 assert(MAN_ROOT
!= p
->type
);
179 case (MAN_NEXT_SIBLING
):
182 p
->parent
= man
->last
->parent
;
184 case (MAN_NEXT_CHILD
):
185 man
->last
->child
= p
;
186 p
->parent
= man
->last
;
196 if ( ! man_valid_pre(man
, p
))
201 assert(MAN_BLOCK
== p
->parent
->type
);
205 assert(MAN_BLOCK
== p
->parent
->type
);
216 if ( ! man_valid_post(man
))
227 static struct man_node
*
228 man_node_alloc(int line
, int pos
, enum man_type type
, enum mant tok
)
232 p
= mandoc_calloc(1, sizeof(struct man_node
));
242 man_elem_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
246 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
247 if ( ! man_node_append(m
, p
))
249 m
->next
= MAN_NEXT_CHILD
;
255 man_head_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
259 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
260 if ( ! man_node_append(m
, p
))
262 m
->next
= MAN_NEXT_CHILD
;
268 man_body_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
272 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
273 if ( ! man_node_append(m
, p
))
275 m
->next
= MAN_NEXT_CHILD
;
281 man_block_alloc(struct man
*m
, int line
, int pos
, enum mant tok
)
285 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
286 if ( ! man_node_append(m
, p
))
288 m
->next
= MAN_NEXT_CHILD
;
294 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
301 n
= man_node_alloc(line
, pos
, MAN_TEXT
, MAN_MAX
);
302 n
->string
= mandoc_malloc(len
+ 1);
303 sv
= strlcpy(n
->string
, word
, len
+ 1);
305 /* Prohibit truncation. */
306 assert(sv
< len
+ 1);
308 if ( ! man_node_append(m
, n
))
311 m
->next
= MAN_NEXT_SIBLING
;
317 * Free all of the resources held by a node. This does NOT unlink a
318 * node from its context; for that, see man_node_unlink().
321 man_node_free(struct man_node
*p
)
331 man_node_delete(struct man
*m
, struct man_node
*p
)
335 man_node_delete(m
, p
->child
);
337 man_node_unlink(m
, p
);
343 man_ptext(struct man
*m
, int line
, char *buf
, int offs
)
347 /* Ignore bogus comments. */
349 if ('\\' == buf
[offs
] &&
350 '.' == buf
[offs
+ 1] &&
351 '"' == buf
[offs
+ 2]) {
352 man_pmsg(m
, line
, offs
, MANDOCERR_BADCOMMENT
);
356 /* Literal free-form text whitespace is preserved. */
358 if (MAN_LITERAL
& m
->flags
) {
359 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
364 /* Pump blank lines directly into the backend. */
366 for (i
= offs
; ' ' == buf
[i
]; i
++)
367 /* Skip leading whitespace. */ ;
369 if ('\0' == buf
[i
]) {
370 /* Allocate a blank entry. */
371 if ( ! man_word_alloc(m
, line
, offs
, ""))
377 * Warn if the last un-escaped character is whitespace. Then
378 * strip away the remaining spaces (tabs stay!).
381 i
= (int)strlen(buf
);
384 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
385 if (i
> 1 && '\\' != buf
[i
- 2])
386 man_pmsg(m
, line
, i
- 1, MANDOCERR_EOLNSPACE
);
388 for (--i
; i
&& ' ' == buf
[i
]; i
--)
389 /* Spin back to non-space. */ ;
391 /* Jump ahead of escaped whitespace. */
392 i
+= '\\' == buf
[i
] ? 2 : 1;
397 if ( ! man_word_alloc(m
, line
, offs
, buf
+ offs
))
401 * End-of-sentence check. If the last character is an unescaped
402 * EOS character, then flag the node as being the end of a
403 * sentence. The front-end will know how to interpret this.
407 if (mandoc_eos(buf
, (size_t)i
, 0))
408 m
->last
->flags
|= MAN_EOS
;
412 * Co-ordinate what happens with having a next-line scope open:
413 * first close out the element scope (if applicable), then close
414 * out the block scope (also if applicable).
417 if (MAN_ELINE
& m
->flags
) {
418 m
->flags
&= ~MAN_ELINE
;
419 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
423 if ( ! (MAN_BLINE
& m
->flags
))
425 m
->flags
&= ~MAN_BLINE
;
427 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
429 return(man_body_alloc(m
, line
, offs
, m
->last
->tok
));
434 man_pmacro(struct man
*m
, int ln
, char *buf
, int offs
)
441 /* Comments and empties are quickly ignored. */
445 if ('\0' == buf
[offs
])
451 * Skip whitespace between the control character and initial
452 * text. "Whitespace" is both spaces and tabs.
455 if (' ' == buf
[i
] || '\t' == buf
[i
]) {
457 while (buf
[i
] && (' ' == buf
[i
] || '\t' == buf
[i
]))
466 * Copy the first word into a nil-terminated buffer.
467 * Stop copying when a tab, space, or eoln is encountered.
471 while (j
< 4 && '\0' != buf
[i
] && ' ' != buf
[i
] && '\t' != buf
[i
])
475 tok
= (j
> 0 && j
< 4) ? man_hash_find(mac
) : MAN_MAX
;
476 if (MAN_MAX
== tok
) {
477 man_vmsg(m
, MANDOCERR_MACRO
, ln
, ppos
, "%s", buf
+ ppos
- 1);
481 /* The macro is sane. Jump to the next word. */
483 while (buf
[i
] && ' ' == buf
[i
])
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 ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
492 man_pmsg(m
, ln
, i
- 1, MANDOCERR_EOLNSPACE
);
495 * Remove prior ELINE macro, as it's being clobbered by a new
496 * macro. Note that NSCOPED macros do not close out ELINE
497 * macros---they don't print text---so we let those slip by.
500 if ( ! (MAN_NSCOPED
& man_macros
[tok
].flags
) &&
501 m
->flags
& MAN_ELINE
) {
503 assert(MAN_TEXT
!= n
->type
);
505 /* Remove repeated NSCOPED macros causing ELINE. */
507 if (MAN_NSCOPED
& man_macros
[n
->tok
].flags
)
510 man_vmsg(m
, MANDOCERR_LINESCOPE
, n
->line
, n
->pos
,
511 "%s", man_macronames
[n
->tok
]);
513 man_node_delete(m
, n
);
514 m
->flags
&= ~MAN_ELINE
;
518 * Save the fact that we're in the next-line for a block. In
519 * this way, embedded roff instructions can "remember" state
523 if (MAN_BLINE
& m
->flags
)
524 m
->flags
|= MAN_BPLINE
;
526 /* Call to handler... */
528 assert(man_macros
[tok
].fp
);
529 if ( ! (*man_macros
[tok
].fp
)(m
, tok
, ln
, ppos
, &i
, buf
))
534 * We weren't in a block-line scope when entering the
535 * above-parsed macro, so return.
538 if ( ! (MAN_BPLINE
& m
->flags
)) {
539 m
->flags
&= ~MAN_ILINE
;
542 m
->flags
&= ~MAN_BPLINE
;
545 * If we're in a block scope, then allow this macro to slip by
546 * without closing scope around it.
549 if (MAN_ILINE
& m
->flags
) {
550 m
->flags
&= ~MAN_ILINE
;
555 * If we've opened a new next-line element scope, then return
556 * now, as the next line will close out the block scope.
559 if (MAN_ELINE
& m
->flags
)
562 /* Close out the block scope opened in the prior line. */
564 assert(MAN_BLINE
& m
->flags
);
565 m
->flags
&= ~MAN_BLINE
;
567 if ( ! man_unscope(m
, m
->last
->parent
, MANDOCERR_MAX
))
569 return(man_body_alloc(m
, ln
, offs
, m
->last
->tok
));
571 err
: /* Error out. */
573 m
->flags
|= MAN_HALT
;
579 man_vmsg(struct man
*man
, enum mandocerr t
,
580 int ln
, int pos
, const char *fmt
, ...)
586 vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
588 return((*man
->msg
)(t
, man
->data
, ln
, pos
, buf
));
593 * Unlink a node from its context. If "m" is provided, the last parse
594 * point will also be adjusted accordingly.
597 man_node_unlink(struct man
*m
, struct man_node
*n
)
600 /* Adjust siblings. */
603 n
->prev
->next
= n
->next
;
605 n
->next
->prev
= n
->prev
;
611 if (n
->parent
->child
== n
)
612 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
615 /* Adjust parse point, if applicable. */
617 if (m
&& m
->last
== n
) {
618 /*XXX: this can occur when bailing from validation. */
619 /*assert(NULL == n->next);*/
622 m
->next
= MAN_NEXT_SIBLING
;
625 m
->next
= MAN_NEXT_CHILD
;
629 if (m
&& m
->first
== n
)