]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.40 2009/09/16 14:40:56 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
26 const char *const __man_merrnames
[WERRMAX
] = {
27 "invalid character", /* WNPRINT */
28 "system: malloc error", /* WNMEM */
29 "invalid manual section", /* WMSEC */
30 "invalid date format", /* WDATE */
31 "scope of prior line violated", /* WLNSCOPE */
32 "trailing whitespace", /* WTSPACE */
33 "unterminated quoted parameter", /* WTQUOTE */
34 "document has no body", /* WNODATA */
35 "document has no title/section", /* WNOTITLE */
36 "invalid escape sequence", /* WESCAPE */
37 "invalid number format", /* WNUMFMT */
38 "expected block head arguments", /* WHEADARGS */
39 "expected block body arguments", /* WBODYARGS */
40 "expected empty block head", /* WNHEADARGS */
41 "unknown macro", /* WMACRO */
42 "ill-formed macro", /* WMACROFORM */
43 "scope open on exit", /* WEXITSCOPE */
44 "no scope context", /* WNOSCOPE */
45 "literal context already open", /* WOLITERAL */
46 "no literal context open" /* WNLITERAL */
49 const char *const __man_macronames
[MAN_MAX
] = {
50 "br", "TH", "SH", "SS",
51 "TP", "LP", "PP", "P",
52 "IP", "HP", "SM", "SB",
53 "BI", "IB", "BR", "RB",
55 "RI", "na", "i", "sp",
56 "nf", "fi", "r", "RE",
60 const char * const *man_macronames
= __man_macronames
;
62 static struct man_node
*man_node_alloc(int, int,
64 static int man_node_append(struct man
*,
66 static int man_ptext(struct man
*, int, char *);
67 static int man_pmacro(struct man
*, int, char *);
68 static void man_free1(struct man
*);
69 static int man_alloc1(struct man
*);
70 static int pstring(struct man
*, int, int,
71 const char *, size_t);
74 extern size_t strlcpy(char *, const char *, size_t);
78 const struct man_node
*
79 man_node(const struct man
*m
)
82 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
86 const struct man_meta
*
87 man_meta(const struct man
*m
)
90 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
95 man_reset(struct man
*man
)
99 return(man_alloc1(man
));
104 man_free(struct man
*man
)
113 man_alloc(void *data
, int pflags
, const struct man_cb
*cb
)
117 if (NULL
== (p
= calloc(1, sizeof(struct man
))))
120 if ( ! man_alloc1(p
)) {
129 (void)memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
135 man_endparse(struct man
*m
)
138 if (MAN_HALT
& m
->flags
)
140 else if (man_macroend(m
))
142 m
->flags
|= MAN_HALT
;
148 man_parseln(struct man
*m
, int ln
, char *buf
)
152 man_pmacro(m
, ln
, buf
) :
153 man_ptext(m
, ln
, buf
));
158 man_free1(struct man
*man
)
162 man_node_freelist(man
->first
);
164 free(man
->meta
.title
);
165 if (man
->meta
.source
)
166 free(man
->meta
.source
);
173 man_alloc1(struct man
*m
)
176 bzero(&m
->meta
, sizeof(struct man_meta
));
178 m
->last
= calloc(1, sizeof(struct man_node
));
182 m
->last
->type
= MAN_ROOT
;
183 m
->next
= MAN_NEXT_CHILD
;
189 man_node_append(struct man
*man
, struct man_node
*p
)
194 assert(MAN_ROOT
!= p
->type
);
197 case (MAN_NEXT_SIBLING
):
200 p
->parent
= man
->last
->parent
;
202 case (MAN_NEXT_CHILD
):
203 man
->last
->child
= p
;
204 p
->parent
= man
->last
;
213 if ( ! man_valid_pre(man
, p
))
218 assert(MAN_BLOCK
== p
->parent
->type
);
222 assert(MAN_BLOCK
== p
->parent
->type
);
233 if ( ! man_valid_post(man
))
235 if ( ! man_action_post(man
))
246 static struct man_node
*
247 man_node_alloc(int line
, int pos
, enum man_type type
, int tok
)
251 p
= calloc(1, sizeof(struct man_node
));
264 man_elem_alloc(struct man
*m
, int line
, int pos
, int tok
)
268 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
271 if ( ! man_node_append(m
, p
))
273 m
->next
= MAN_NEXT_CHILD
;
279 man_head_alloc(struct man
*m
, int line
, int pos
, int tok
)
283 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
286 if ( ! man_node_append(m
, p
))
288 m
->next
= MAN_NEXT_CHILD
;
294 man_body_alloc(struct man
*m
, int line
, int pos
, int tok
)
298 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
301 if ( ! man_node_append(m
, p
))
303 m
->next
= MAN_NEXT_CHILD
;
309 man_block_alloc(struct man
*m
, int line
, int pos
, int tok
)
313 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
316 if ( ! man_node_append(m
, p
))
318 m
->next
= MAN_NEXT_CHILD
;
324 pstring(struct man
*m
, int line
, int pos
,
325 const char *p
, size_t len
)
330 n
= man_node_alloc(line
, pos
, MAN_TEXT
, -1);
334 n
->string
= malloc(len
+ 1);
335 if (NULL
== n
->string
) {
340 sv
= strlcpy(n
->string
, p
, len
+ 1);
342 /* Prohibit truncation. */
343 assert(sv
< len
+ 1);
345 if ( ! man_node_append(m
, n
))
347 m
->next
= MAN_NEXT_SIBLING
;
353 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
356 return(pstring(m
, line
, pos
, word
, strlen(word
)));
361 man_node_free(struct man_node
*p
)
373 man_node_freelist(struct man_node
*p
)
378 man_node_freelist(p
->child
);
379 assert(0 == p
->nchild
);
383 man_node_freelist(n
);
388 man_ptext(struct man
*m
, int line
, char *buf
)
392 /* Literal free-form text whitespace is preserved. */
394 if (MAN_LITERAL
& m
->flags
) {
395 if ( ! man_word_alloc(m
, line
, 0, buf
))
400 /* First de-chunk and allocate words. */
402 for (i
= 0; ' ' == buf
[i
]; i
++)
403 /* Skip leading whitespace. */ ;
405 if ( ! pstring(m
, line
, 0, &buf
[i
], 0))
410 for (j
= i
; buf
[i
]; i
++) {
414 /* Escaped whitespace. */
415 if (i
&& ' ' == buf
[i
] && '\\' == buf
[i
- 1])
419 if ( ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
422 for ( ; ' ' == buf
[i
]; i
++)
423 /* Skip trailing whitespace. */ ;
430 if (j
!= i
&& ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
436 * Co-ordinate what happens with having a next-line scope open:
437 * first close out the element scope (if applicable), then close
438 * out the block scope (also if applicable).
441 if (MAN_ELINE
& m
->flags
) {
442 m
->flags
&= ~MAN_ELINE
;
443 if ( ! man_unscope(m
, m
->last
->parent
))
447 if ( ! (MAN_BLINE
& m
->flags
))
449 m
->flags
&= ~MAN_BLINE
;
451 if ( ! man_unscope(m
, m
->last
->parent
))
453 return(man_body_alloc(m
, line
, 0, m
->last
->tok
));
458 man_pmacro(struct man
*m
, int ln
, char *buf
)
460 int i
, j
, c
, ppos
, fl
;
464 /* Comments and empties are quickly ignored. */
475 while (buf
[i
] && ' ' == buf
[i
])
483 /* Copy the first word into a nil-terminated buffer. */
485 for (j
= 0; j
< 4; j
++, i
++) {
486 if (0 == (mac
[j
] = buf
[i
]))
488 else if (' ' == buf
[i
])
491 /* Check for invalid characters. */
493 if (isgraph((u_char
)buf
[i
]))
495 return(man_perr(m
, ln
, i
, WNPRINT
));
500 if (j
== 4 || j
< 1) {
501 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
502 (void)man_perr(m
, ln
, ppos
, WMACROFORM
);
505 if ( ! man_pwarn(m
, ln
, ppos
, WMACROFORM
))
510 if (MAN_MAX
== (c
= man_hash_find(mac
))) {
511 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
512 (void)man_perr(m
, ln
, ppos
, WMACRO
);
515 if ( ! man_pwarn(m
, ln
, ppos
, WMACRO
))
520 /* The macro is sane. Jump to the next word. */
522 while (buf
[i
] && ' ' == buf
[i
])
525 /* Remove prior ELINE macro, if applicable. */
527 if (m
->flags
& MAN_ELINE
) {
529 assert(NULL
== n
->child
);
530 assert(0 == n
->nchild
);
531 if ( ! man_nwarn(m
, n
, WLNSCOPE
))
535 assert(n
!= n
->parent
->child
);
536 assert(n
== n
->prev
->next
);
537 n
->prev
->next
= NULL
;
539 m
->next
= MAN_NEXT_SIBLING
;
541 assert(n
== n
->parent
->child
);
542 n
->parent
->child
= NULL
;
544 m
->next
= MAN_NEXT_CHILD
;
548 m
->flags
&= ~MAN_ELINE
;
551 /* Begin recursive parse sequence. */
553 assert(man_macros
[c
].fp
);
555 if ( ! (*man_macros
[c
].fp
)(m
, c
, ln
, ppos
, &i
, buf
))
559 if ( ! (MAN_BLINE
& fl
))
563 * If we've opened a new next-line element scope, then return
564 * now, as the next line will close out the block scope.
567 if (MAN_ELINE
& m
->flags
)
570 /* Close out the block scope opened in the prior line. */
572 assert(MAN_BLINE
& m
->flags
);
573 m
->flags
&= ~MAN_BLINE
;
575 if ( ! man_unscope(m
, m
->last
->parent
))
577 return(man_body_alloc(m
, ln
, 0, m
->last
->tok
));
579 err
: /* Error out. */
581 m
->flags
|= MAN_HALT
;
587 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
592 if (NULL
== man
->cb
.man_err
)
596 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
598 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
603 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
608 if (NULL
== man
->cb
.man_warn
)
612 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
614 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));
619 man_err(struct man
*m
, int line
, int pos
, int iserr
, enum merr type
)
623 p
= __man_merrnames
[(int)type
];
627 return(man_verr(m
, line
, pos
, p
));
629 return(man_vwarn(m
, line
, pos
, p
));