]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.11 2009/03/26 14:38:11 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
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
29 const char *const __man_macronames
[MAN_MAX
] = {
30 "\\\"", "TH", "SH", "SS",
31 "TP", "LP", "PP", "P",
32 "IP", "HP", "SM", "SB",
33 "BI", "IB", "BR", "RB",
38 const char * const *man_macronames
= __man_macronames
;
40 static struct man_node
*man_node_alloc(int, int, enum man_type
);
41 static int man_node_append(struct man
*,
43 static int man_ptext(struct man
*, int, char *);
44 static int man_pmacro(struct man
*, int, char *);
45 static void man_free1(struct man
*);
46 static void man_alloc1(struct man
*);
49 const struct man_node
*
50 man_node(const struct man
*m
)
53 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
57 const struct man_meta
*
58 man_meta(const struct man
*m
)
61 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
66 man_reset(struct man
*man
)
75 man_free(struct man
*man
)
81 man_hash_free(man
->htab
);
87 man_alloc(void *data
, int pflags
, const struct man_cb
*cb
)
91 p
= calloc(1, sizeof(struct man
));
98 (void)memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
100 p
->htab
= man_hash_alloc();
109 man_endparse(struct man
*m
)
112 if (MAN_HALT
& m
->flags
)
114 else if (man_macroend(m
))
116 m
->flags
|= MAN_HALT
;
122 man_parseln(struct man
*m
, int ln
, char *buf
)
126 man_pmacro(m
, ln
, buf
) :
127 man_ptext(m
, ln
, buf
));
132 man_free1(struct man
*man
)
136 man_node_freelist(man
->first
);
138 free(man
->meta
.title
);
139 if (man
->meta
.source
)
140 free(man
->meta
.source
);
147 man_alloc1(struct man
*m
)
150 bzero(&m
->meta
, sizeof(struct man_meta
));
152 m
->last
= calloc(1, sizeof(struct man_node
));
156 m
->last
->type
= MAN_ROOT
;
157 m
->next
= MAN_NEXT_CHILD
;
162 man_node_append(struct man
*man
, struct man_node
*p
)
167 assert(MAN_ROOT
!= p
->type
);
170 case (MAN_NEXT_SIBLING
):
173 p
->parent
= man
->last
->parent
;
175 case (MAN_NEXT_CHILD
):
176 man
->last
->child
= p
;
177 p
->parent
= man
->last
;
188 if ( ! man_valid_post(man
))
190 if ( ! man_action_post(man
))
201 static struct man_node
*
202 man_node_alloc(int line
, int pos
, enum man_type type
)
206 if (NULL
== (p
= calloc(1, sizeof(struct man_node
))))
217 man_elem_alloc(struct man
*man
, int line
, int pos
, int tok
)
221 p
= man_node_alloc(line
, pos
, MAN_ELEM
);
224 return(man_node_append(man
, p
));
229 man_word_alloc(struct man
*man
,
230 int line
, int pos
, const char *word
)
234 p
= man_node_alloc(line
, pos
, MAN_TEXT
);
235 if (NULL
== (p
->string
= strdup(word
)))
238 return(man_node_append(man
, p
));
243 man_node_free(struct man_node
*p
)
253 man_node_freelist(struct man_node
*p
)
257 man_node_freelist(p
->child
);
259 man_node_freelist(p
->next
);
266 man_ptext(struct man
*m
, int line
, char *buf
)
269 if ( ! man_word_alloc(m
, line
, 0, buf
))
271 m
->next
= MAN_NEXT_SIBLING
;
274 * If this is one of the zany NLINE macros that consumes the
275 * next line of input as being influenced, then close out the
276 * existing macro "scope" and continue processing.
279 if ( ! (MAN_NLINE
& m
->flags
))
282 m
->flags
&= ~MAN_NLINE
;
283 m
->last
= m
->last
->parent
;
285 assert(MAN_ROOT
!= m
->last
->type
);
286 if ( ! man_valid_post(m
))
288 if ( ! man_action_post(m
))
296 man_pmacro(struct man
*m
, int ln
, char *buf
)
298 int i
, j
, c
, ppos
, fl
;
302 /* Comments and empties are quickly ignored. */
305 fl
= MAN_NLINE
& m
->flags
;
314 while (buf
[i
] && ' ' == buf
[i
])
322 if (buf
[i
] && '\\' == buf
[i
])
323 if (buf
[i
+ 1] && '\"' == buf
[i
+ 1])
326 /* Copy the first word into a nil-terminated buffer. */
328 for (j
= 0; j
< 4; j
++, i
++) {
329 if (0 == (mac
[j
] = buf
[i
]))
331 else if (' ' == buf
[i
])
337 if (j
== 4 || j
< 1) {
338 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
339 (void)man_verr(m
, ln
, ppos
,
340 "ill-formed macro: %s", mac
);
343 if ( ! man_vwarn(m
, ln
, ppos
,
344 "ill-formed macro: %s", mac
))
349 if (MAN_MAX
== (c
= man_hash_find(m
->htab
, mac
))) {
350 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
351 (void)man_verr(m
, ln
, ppos
,
352 "unknown macro: %s", mac
);
355 if ( ! man_vwarn(m
, ln
, ppos
,
356 "unknown macro: %s", mac
))
361 /* The macro is sane. Jump to the next word. */
363 while (buf
[i
] && ' ' == buf
[i
])
366 /* Begin recursive parse sequence. */
368 if ( ! man_macro(m
, c
, ln
, ppos
, &i
, buf
))
374 * A NLINE macro has been immediately followed with
375 * another. Close out the preceeding macro's scope, and
378 assert(MAN_ROOT
!= m
->last
->type
);
379 assert(m
->last
->parent
);
380 assert(MAN_ROOT
!= m
->last
->parent
->type
);
383 m
->last
= m
->last
->parent
;
385 if ( ! man_valid_post(m
))
387 if ( ! man_action_post(m
))
389 m
->next
= MAN_NEXT_SIBLING
;
390 m
->flags
&= ~MAN_NLINE
;
395 err
: /* Error out. */
397 m
->flags
|= MAN_HALT
;
403 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
408 if (NULL
== man
->cb
.man_err
)
412 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
414 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
419 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
424 if (NULL
== man
->cb
.man_warn
)
428 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
430 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));