]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.34 2009/08/21 12:12:12 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 */
47 const char *const __man_macronames
[MAN_MAX
] = {
48 "br", "TH", "SH", "SS",
49 "TP", "LP", "PP", "P",
50 "IP", "HP", "SM", "SB",
51 "BI", "IB", "BR", "RB",
53 "RI", "na", "i", "sp",
54 "nf", "fi", "r", "RE",
58 const char * const *man_macronames
= __man_macronames
;
60 static struct man_node
*man_node_alloc(int, int,
62 static int man_node_append(struct man
*,
64 static int man_ptext(struct man
*, int, char *);
65 static int man_pmacro(struct man
*, int, char *);
66 static void man_free1(struct man
*);
67 static int man_alloc1(struct man
*);
68 static int pstring(struct man
*, int, int,
69 const char *, size_t);
72 extern size_t strlcpy(char *, const char *, size_t);
76 const struct man_node
*
77 man_node(const struct man
*m
)
80 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
84 const struct man_meta
*
85 man_meta(const struct man
*m
)
88 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
93 man_reset(struct man
*man
)
97 return(man_alloc1(man
));
102 man_free(struct man
*man
)
108 man_hash_free(man
->htab
);
114 man_alloc(void *data
, int pflags
, const struct man_cb
*cb
)
118 if (NULL
== (p
= calloc(1, sizeof(struct man
))))
121 if ( ! man_alloc1(p
)) {
128 (void)memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
130 if (NULL
== (p
->htab
= man_hash_alloc())) {
139 man_endparse(struct man
*m
)
142 if (MAN_HALT
& m
->flags
)
144 else if (man_macroend(m
))
146 m
->flags
|= MAN_HALT
;
152 man_parseln(struct man
*m
, int ln
, char *buf
)
156 man_pmacro(m
, ln
, buf
) :
157 man_ptext(m
, ln
, buf
));
162 man_free1(struct man
*man
)
166 man_node_freelist(man
->first
);
168 free(man
->meta
.title
);
169 if (man
->meta
.source
)
170 free(man
->meta
.source
);
177 man_alloc1(struct man
*m
)
180 bzero(&m
->meta
, sizeof(struct man_meta
));
182 m
->last
= calloc(1, sizeof(struct man_node
));
186 m
->last
->type
= MAN_ROOT
;
187 m
->next
= MAN_NEXT_CHILD
;
193 man_node_append(struct man
*man
, struct man_node
*p
)
198 assert(MAN_ROOT
!= p
->type
);
201 case (MAN_NEXT_SIBLING
):
204 p
->parent
= man
->last
->parent
;
206 case (MAN_NEXT_CHILD
):
207 man
->last
->child
= p
;
208 p
->parent
= man
->last
;
217 if ( ! man_valid_pre(man
, p
))
222 assert(MAN_BLOCK
== p
->parent
->type
);
226 assert(MAN_BLOCK
== p
->parent
->type
);
237 if ( ! man_valid_post(man
))
239 if ( ! man_action_post(man
))
250 static struct man_node
*
251 man_node_alloc(int line
, int pos
, enum man_type type
, int tok
)
255 p
= calloc(1, sizeof(struct man_node
));
268 man_elem_alloc(struct man
*m
, int line
, int pos
, int tok
)
272 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
275 if ( ! man_node_append(m
, p
))
277 m
->next
= MAN_NEXT_CHILD
;
283 man_head_alloc(struct man
*m
, int line
, int pos
, int tok
)
287 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
290 if ( ! man_node_append(m
, p
))
292 m
->next
= MAN_NEXT_CHILD
;
298 man_body_alloc(struct man
*m
, int line
, int pos
, int tok
)
302 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
305 if ( ! man_node_append(m
, p
))
307 m
->next
= MAN_NEXT_CHILD
;
313 man_block_alloc(struct man
*m
, int line
, int pos
, int tok
)
317 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
320 if ( ! man_node_append(m
, p
))
322 m
->next
= MAN_NEXT_CHILD
;
328 pstring(struct man
*m
, int line
, int pos
,
329 const char *p
, size_t len
)
334 n
= man_node_alloc(line
, pos
, MAN_TEXT
, -1);
338 n
->string
= malloc(len
+ 1);
339 if (NULL
== n
->string
) {
344 sv
= strlcpy(n
->string
, p
, len
+ 1);
346 /* Prohibit truncation. */
347 assert(sv
< len
+ 1);
349 if ( ! man_node_append(m
, n
))
351 m
->next
= MAN_NEXT_SIBLING
;
357 man_word_alloc(struct man
*m
, int line
, int pos
, const char *word
)
360 return(pstring(m
, line
, pos
, word
, strlen(word
)));
365 man_node_free(struct man_node
*p
)
377 man_node_freelist(struct man_node
*p
)
381 man_node_freelist(p
->child
);
383 man_node_freelist(p
->next
);
385 assert(0 == p
->nchild
);
391 man_ptext(struct man
*m
, int line
, char *buf
)
395 /* First de-chunk and allocate words. */
397 for (i
= 0; ' ' == buf
[i
]; i
++)
398 /* Skip leading whitespace. */ ;
400 if ( ! pstring(m
, line
, 0, &buf
[i
], 0))
405 for (j
= i
; buf
[i
]; i
++) {
409 /* Escaped whitespace. */
410 if (i
&& ' ' == buf
[i
] && '\\' == buf
[i
- 1])
414 if ( ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
417 for ( ; ' ' == buf
[i
]; i
++)
418 /* Skip trailing whitespace. */ ;
425 if (j
!= i
&& ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
431 * Co-ordinate what happens with having a next-line scope open:
432 * first close out the element scope (if applicable), then close
433 * out the block scope (also if applicable).
436 /* XXX - this should be in man_action.c. */
438 if (MAN_ELINE
& m
->flags
) {
439 m
->flags
&= ~MAN_ELINE
;
440 if ( ! man_unscope(m
, m
->last
->parent
))
444 if ( ! (MAN_BLINE
& m
->flags
))
446 m
->flags
&= ~MAN_BLINE
;
448 if ( ! man_unscope(m
, m
->last
->parent
))
450 return(man_body_alloc(m
, line
, 0, m
->last
->tok
));
455 man_pmacro(struct man
*m
, int ln
, char *buf
)
457 int i
, j
, c
, ppos
, fl
;
461 /* Comments and empties are quickly ignored. */
472 while (buf
[i
] && ' ' == buf
[i
])
480 /* Copy the first word into a nil-terminated buffer. */
482 for (j
= 0; j
< 4; j
++, i
++) {
483 if (0 == (mac
[j
] = buf
[i
]))
485 else if (' ' == buf
[i
])
491 if (j
== 4 || j
< 1) {
492 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
493 (void)man_perr(m
, ln
, ppos
, WMACROFORM
);
496 if ( ! man_pwarn(m
, ln
, ppos
, WMACROFORM
))
501 if (MAN_MAX
== (c
= man_hash_find(m
->htab
, mac
))) {
502 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
503 (void)man_perr(m
, ln
, ppos
, WMACRO
);
506 if ( ! man_pwarn(m
, ln
, ppos
, WMACRO
))
511 /* The macro is sane. Jump to the next word. */
513 while (buf
[i
] && ' ' == buf
[i
])
516 /* Remove prior ELINE macro, if applicable. */
518 if (m
->flags
& MAN_ELINE
) {
520 assert(NULL
== n
->child
);
521 if ( ! man_nwarn(m
, n
, WLNSCOPE
))
525 assert(n
!= n
->parent
->child
);
526 assert(n
== n
->prev
->next
);
527 n
->prev
->next
= NULL
;
530 assert(n
== n
->parent
->child
);
531 n
->parent
->child
= NULL
;
536 m
->flags
&= ~MAN_ELINE
;
539 /* Begin recursive parse sequence. */
541 assert(man_macros
[c
].fp
);
543 if ( ! (*man_macros
[c
].fp
)(m
, c
, ln
, ppos
, &i
, buf
))
547 if ( ! (MAN_BLINE
& fl
))
551 * If we've opened a new next-line element scope, then return
552 * now, as the next line will close out the block scope.
555 if (MAN_ELINE
& m
->flags
)
558 /* Close out the block scope opened in the prior line. */
560 assert(MAN_BLINE
& m
->flags
);
561 m
->flags
&= ~MAN_BLINE
;
563 if ( ! man_unscope(m
, m
->last
->parent
))
565 return(man_body_alloc(m
, ln
, 0, m
->last
->tok
));
567 err
: /* Error out. */
569 m
->flags
|= MAN_HALT
;
575 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
580 if (NULL
== man
->cb
.man_err
)
584 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
586 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
591 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
596 if (NULL
== man
->cb
.man_warn
)
600 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
602 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));
607 man_err(struct man
*m
, int line
, int pos
, int iserr
, enum merr type
)
611 p
= __man_merrnames
[(int)type
];
615 return(man_verr(m
, line
, pos
, p
));
617 return(man_vwarn(m
, line
, pos
, p
));