]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.29 2009/08/13 11:45:29 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 */
46 const char *const __man_macronames
[MAN_MAX
] = {
47 "br", "TH", "SH", "SS",
48 "TP", "LP", "PP", "P",
49 "IP", "HP", "SM", "SB",
50 "BI", "IB", "BR", "RB",
52 "RI", "na", "i", "sp",
56 const char * const *man_macronames
= __man_macronames
;
58 static struct man_node
*man_node_alloc(int, int,
60 static int man_node_append(struct man
*,
62 static int man_ptext(struct man
*, int, char *);
63 static int man_pmacro(struct man
*, int, char *);
64 static void man_free1(struct man
*);
65 static int man_alloc1(struct man
*);
68 const struct man_node
*
69 man_node(const struct man
*m
)
72 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
76 const struct man_meta
*
77 man_meta(const struct man
*m
)
80 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
85 man_reset(struct man
*man
)
89 return(man_alloc1(man
));
94 man_free(struct man
*man
)
100 man_hash_free(man
->htab
);
106 man_alloc(void *data
, int pflags
, const struct man_cb
*cb
)
110 if (NULL
== (p
= calloc(1, sizeof(struct man
))))
113 if ( ! man_alloc1(p
)) {
120 (void)memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
122 if (NULL
== (p
->htab
= man_hash_alloc())) {
131 man_endparse(struct man
*m
)
134 if (MAN_HALT
& m
->flags
)
136 else if (man_macroend(m
))
138 m
->flags
|= MAN_HALT
;
144 man_parseln(struct man
*m
, int ln
, char *buf
)
148 man_pmacro(m
, ln
, buf
) :
149 man_ptext(m
, ln
, buf
));
154 man_free1(struct man
*man
)
158 man_node_freelist(man
->first
);
160 free(man
->meta
.title
);
161 if (man
->meta
.source
)
162 free(man
->meta
.source
);
169 man_alloc1(struct man
*m
)
172 bzero(&m
->meta
, sizeof(struct man_meta
));
174 m
->last
= calloc(1, sizeof(struct man_node
));
178 m
->last
->type
= MAN_ROOT
;
179 m
->next
= MAN_NEXT_CHILD
;
185 man_node_append(struct man
*man
, struct man_node
*p
)
190 assert(MAN_ROOT
!= p
->type
);
193 case (MAN_NEXT_SIBLING
):
196 p
->parent
= man
->last
->parent
;
198 case (MAN_NEXT_CHILD
):
199 man
->last
->child
= p
;
200 p
->parent
= man
->last
;
209 if ( ! man_valid_pre(man
, p
))
214 assert(MAN_BLOCK
== p
->parent
->type
);
218 assert(MAN_BLOCK
== p
->parent
->type
);
229 if ( ! man_valid_post(man
))
231 if ( ! man_action_post(man
))
242 static struct man_node
*
243 man_node_alloc(int line
, int pos
, enum man_type type
, int tok
)
247 p
= calloc(1, sizeof(struct man_node
));
260 man_elem_alloc(struct man
*man
, int line
, int pos
, int tok
)
264 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
267 return(man_node_append(man
, p
));
272 man_head_alloc(struct man
*m
, int line
, int pos
, int tok
)
276 p
= man_node_alloc(line
, pos
, MAN_HEAD
, tok
);
279 if ( ! man_node_append(m
, p
))
281 m
->next
= MAN_NEXT_CHILD
;
287 man_body_alloc(struct man
*m
, int line
, int pos
, int tok
)
291 p
= man_node_alloc(line
, pos
, MAN_BODY
, tok
);
294 if ( ! man_node_append(m
, p
))
296 m
->next
= MAN_NEXT_CHILD
;
302 man_block_alloc(struct man
*m
, int line
, int pos
, int tok
)
306 p
= man_node_alloc(line
, pos
, MAN_BLOCK
, tok
);
309 if ( ! man_node_append(m
, p
))
311 m
->next
= MAN_NEXT_CHILD
;
317 man_word_alloc(struct man
*man
,
318 int line
, int pos
, const char *word
)
322 p
= man_node_alloc(line
, pos
, MAN_TEXT
, -1);
325 if (NULL
== (p
->string
= strdup(word
)))
327 return(man_node_append(man
, p
));
332 man_node_free(struct man_node
*p
)
344 man_node_freelist(struct man_node
*p
)
348 man_node_freelist(p
->child
);
350 man_node_freelist(p
->next
);
352 assert(0 == p
->nchild
);
358 man_ptext(struct man
*m
, int line
, char *buf
)
361 /* First allocate word. */
363 if ( ! man_word_alloc(m
, line
, 0, buf
))
365 m
->next
= MAN_NEXT_SIBLING
;
368 * Co-ordinate what happens with having a next-line scope open:
369 * first close out the element scope (if applicable), then close
370 * out the block scope (also if applicable).
373 /* XXX - this should be in man_action.c. */
375 if (MAN_ELINE
& m
->flags
) {
376 m
->flags
&= ~MAN_ELINE
;
377 if ( ! man_unscope(m
, m
->last
->parent
))
381 if ( ! (MAN_BLINE
& m
->flags
))
383 m
->flags
&= ~MAN_BLINE
;
385 if ( ! man_unscope(m
, m
->last
->parent
))
387 return(man_body_alloc(m
, line
, 0, m
->last
->tok
));
392 man_pmacro(struct man
*m
, int ln
, char *buf
)
394 int i
, j
, c
, ppos
, fl
;
397 /* Comments and empties are quickly ignored. */
408 while (buf
[i
] && ' ' == buf
[i
])
416 /* Copy the first word into a nil-terminated buffer. */
418 for (j
= 0; j
< 4; j
++, i
++) {
419 if (0 == (mac
[j
] = buf
[i
]))
421 else if (' ' == buf
[i
])
427 if (j
== 4 || j
< 1) {
428 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
429 (void)man_perr(m
, ln
, ppos
, WMACROFORM
);
432 if ( ! man_pwarn(m
, ln
, ppos
, WMACROFORM
))
437 if (MAN_MAX
== (c
= man_hash_find(m
->htab
, mac
))) {
438 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
439 (void)man_perr(m
, ln
, ppos
, WMACRO
);
442 if ( ! man_pwarn(m
, ln
, ppos
, WMACRO
))
447 /* The macro is sane. Jump to the next word. */
449 while (buf
[i
] && ' ' == buf
[i
])
452 /* Begin recursive parse sequence. */
454 assert(man_macros
[c
].fp
);
456 if ( ! (*man_macros
[c
].fp
)(m
, c
, ln
, ppos
, &i
, buf
))
460 if ( ! (MAN_BLINE
& fl
))
464 * If we've opened a new next-line element scope, then return
465 * now, as the next line will close out the block scope.
468 if (MAN_ELINE
& m
->flags
)
471 /* Close out the block scope opened in the prior line. */
473 /* XXX - this should be in man_action.c. */
475 assert(MAN_BLINE
& m
->flags
);
476 m
->flags
&= ~MAN_BLINE
;
478 if ( ! man_unscope(m
, m
->last
->parent
))
480 return(man_body_alloc(m
, ln
, 0, m
->last
->tok
));
482 err
: /* Error out. */
484 m
->flags
|= MAN_HALT
;
490 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
495 if (NULL
== man
->cb
.man_err
)
499 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
501 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
506 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
511 if (NULL
== man
->cb
.man_warn
)
515 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
517 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));
522 man_err(struct man
*m
, int line
, int pos
, int iserr
, enum merr type
)
526 p
= __man_merrnames
[(int)type
];
530 return(man_verr(m
, line
, pos
, p
));
532 return(man_vwarn(m
, line
, pos
, p
));