]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.18 2009/04/12 19:45:26 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
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_macronames
[MAN_MAX
] = {
27 "\\\"", "TH", "SH", "SS",
28 "TP", "LP", "PP", "P",
29 "IP", "HP", "SM", "SB",
30 "BI", "IB", "BR", "RB",
35 const char * const *man_macronames
= __man_macronames
;
37 static struct man_node
*man_node_alloc(int, int,
39 static int man_node_append(struct man
*,
41 static int man_ptext(struct man
*, int, char *);
42 static int man_pmacro(struct man
*, int, char *);
43 static void man_free1(struct man
*);
44 static int man_alloc1(struct man
*);
47 const struct man_node
*
48 man_node(const struct man
*m
)
51 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
55 const struct man_meta
*
56 man_meta(const struct man
*m
)
59 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
64 man_reset(struct man
*man
)
68 return(man_alloc1(man
));
73 man_free(struct man
*man
)
79 man_hash_free(man
->htab
);
85 man_alloc(void *data
, int pflags
, const struct man_cb
*cb
)
89 if (NULL
== (p
= calloc(1, sizeof(struct man
))))
92 if ( ! man_alloc1(p
)) {
99 (void)memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
101 if (NULL
== (p
->htab
= man_hash_alloc())) {
110 man_endparse(struct man
*m
)
113 if (MAN_HALT
& m
->flags
)
115 else if (man_macroend(m
))
117 m
->flags
|= MAN_HALT
;
123 man_parseln(struct man
*m
, int ln
, char *buf
)
127 man_pmacro(m
, ln
, buf
) :
128 man_ptext(m
, ln
, buf
));
133 man_free1(struct man
*man
)
137 man_node_freelist(man
->first
);
139 free(man
->meta
.title
);
140 if (man
->meta
.source
)
141 free(man
->meta
.source
);
148 man_alloc1(struct man
*m
)
151 bzero(&m
->meta
, sizeof(struct man_meta
));
153 m
->last
= calloc(1, sizeof(struct man_node
));
157 m
->last
->type
= MAN_ROOT
;
158 m
->next
= MAN_NEXT_CHILD
;
164 man_node_append(struct man
*man
, struct man_node
*p
)
169 assert(MAN_ROOT
!= p
->type
);
172 case (MAN_NEXT_SIBLING
):
175 p
->parent
= man
->last
->parent
;
177 case (MAN_NEXT_CHILD
):
178 man
->last
->child
= p
;
179 p
->parent
= man
->last
;
190 if ( ! man_valid_post(man
))
192 if ( ! man_action_post(man
))
203 static struct man_node
*
204 man_node_alloc(int line
, int pos
, enum man_type type
, int tok
)
208 p
= calloc(1, sizeof(struct man_node
));
221 man_elem_alloc(struct man
*man
, int line
, int pos
, int tok
)
225 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
228 return(man_node_append(man
, p
));
233 man_word_alloc(struct man
*man
,
234 int line
, int pos
, const char *word
)
238 p
= man_node_alloc(line
, pos
, MAN_TEXT
, -1);
241 if (NULL
== (p
->string
= strdup(word
)))
243 return(man_node_append(man
, p
));
248 man_node_free(struct man_node
*p
)
258 man_node_freelist(struct man_node
*p
)
262 man_node_freelist(p
->child
);
264 man_node_freelist(p
->next
);
271 man_ptext(struct man
*m
, int line
, char *buf
)
274 if ( ! man_word_alloc(m
, line
, 0, buf
))
276 m
->next
= MAN_NEXT_SIBLING
;
279 * If this is one of the zany NLINE macros that consumes the
280 * next line of input as being influenced, then close out the
281 * existing macro "scope" and continue processing.
284 if ( ! (MAN_NLINE
& m
->flags
))
287 m
->flags
&= ~MAN_NLINE
;
288 m
->last
= m
->last
->parent
;
290 assert(MAN_ROOT
!= m
->last
->type
);
291 if ( ! man_valid_post(m
))
293 if ( ! man_action_post(m
))
301 man_pmacro(struct man
*m
, int ln
, char *buf
)
303 int i
, j
, c
, ppos
, fl
;
307 /* Comments and empties are quickly ignored. */
310 fl
= MAN_NLINE
& m
->flags
;
319 while (buf
[i
] && ' ' == buf
[i
])
327 if (buf
[i
] && '\\' == buf
[i
])
328 if (buf
[i
+ 1] && '\"' == buf
[i
+ 1])
331 /* Copy the first word into a nil-terminated buffer. */
333 for (j
= 0; j
< 4; j
++, i
++) {
334 if (0 == (mac
[j
] = buf
[i
]))
336 else if (' ' == buf
[i
])
342 if (j
== 4 || j
< 1) {
343 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
344 (void)man_verr(m
, ln
, ppos
,
345 "ill-formed macro: %s", mac
);
348 if ( ! man_vwarn(m
, ln
, ppos
,
349 "ill-formed macro: %s", mac
))
354 if (MAN_MAX
== (c
= man_hash_find(m
->htab
, mac
))) {
355 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
356 (void)man_verr(m
, ln
, ppos
,
357 "unknown macro: %s", mac
);
360 if ( ! man_vwarn(m
, ln
, ppos
,
361 "unknown macro: %s", mac
))
366 /* The macro is sane. Jump to the next word. */
368 while (buf
[i
] && ' ' == buf
[i
])
371 /* Begin recursive parse sequence. */
373 if ( ! man_macro(m
, c
, ln
, ppos
, &i
, buf
))
379 * A NLINE macro has been immediately followed with
380 * another. Close out the preceding macro's scope, and
383 assert(MAN_ROOT
!= m
->last
->type
);
384 assert(m
->last
->parent
);
385 assert(MAN_ROOT
!= m
->last
->parent
->type
);
388 m
->last
= m
->last
->parent
;
390 if ( ! man_valid_post(m
))
392 if ( ! man_action_post(m
))
394 m
->next
= MAN_NEXT_SIBLING
;
395 m
->flags
&= ~MAN_NLINE
;
400 err
: /* Error out. */
402 m
->flags
|= MAN_HALT
;
408 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
413 if (NULL
== man
->cb
.man_err
)
417 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
419 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
424 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
429 if (NULL
== man
->cb
.man_warn
)
433 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
435 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));