]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.15 2009/04/03 11:08:39 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
)
77 man_free(struct man
*man
)
83 man_hash_free(man
->htab
);
89 man_alloc(void *data
, int pflags
, const struct man_cb
*cb
)
93 p
= calloc(1, sizeof(struct man
));
100 (void)memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
102 p
->htab
= man_hash_alloc();
111 man_endparse(struct man
*m
)
114 if (MAN_HALT
& m
->flags
)
116 else if (man_macroend(m
))
118 m
->flags
|= MAN_HALT
;
124 man_parseln(struct man
*m
, int ln
, char *buf
)
128 man_pmacro(m
, ln
, buf
) :
129 man_ptext(m
, ln
, buf
));
134 man_free1(struct man
*man
)
138 man_node_freelist(man
->first
);
140 free(man
->meta
.title
);
141 if (man
->meta
.source
)
142 free(man
->meta
.source
);
149 man_alloc1(struct man
*m
)
152 bzero(&m
->meta
, sizeof(struct man_meta
));
154 m
->last
= calloc(1, sizeof(struct man_node
));
158 m
->last
->type
= MAN_ROOT
;
159 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
)
208 if (NULL
== (p
= calloc(1, sizeof(struct man_node
))))
219 man_elem_alloc(struct man
*man
, int line
, int pos
, int tok
)
223 p
= man_node_alloc(line
, pos
, MAN_ELEM
);
226 return(man_node_append(man
, p
));
231 man_word_alloc(struct man
*man
,
232 int line
, int pos
, const char *word
)
236 p
= man_node_alloc(line
, pos
, MAN_TEXT
);
237 if (NULL
== (p
->string
= strdup(word
)))
240 return(man_node_append(man
, p
));
245 man_node_free(struct man_node
*p
)
255 man_node_freelist(struct man_node
*p
)
259 man_node_freelist(p
->child
);
261 man_node_freelist(p
->next
);
268 man_ptext(struct man
*m
, int line
, char *buf
)
271 if ( ! man_word_alloc(m
, line
, 0, buf
))
273 m
->next
= MAN_NEXT_SIBLING
;
276 * If this is one of the zany NLINE macros that consumes the
277 * next line of input as being influenced, then close out the
278 * existing macro "scope" and continue processing.
281 if ( ! (MAN_NLINE
& m
->flags
))
284 m
->flags
&= ~MAN_NLINE
;
285 m
->last
= m
->last
->parent
;
287 assert(MAN_ROOT
!= m
->last
->type
);
288 if ( ! man_valid_post(m
))
290 if ( ! man_action_post(m
))
298 man_pmacro(struct man
*m
, int ln
, char *buf
)
300 int i
, j
, c
, ppos
, fl
;
304 /* Comments and empties are quickly ignored. */
307 fl
= MAN_NLINE
& m
->flags
;
316 while (buf
[i
] && ' ' == buf
[i
])
324 if (buf
[i
] && '\\' == buf
[i
])
325 if (buf
[i
+ 1] && '\"' == buf
[i
+ 1])
328 /* Copy the first word into a nil-terminated buffer. */
330 for (j
= 0; j
< 4; j
++, i
++) {
331 if (0 == (mac
[j
] = buf
[i
]))
333 else if (' ' == buf
[i
])
339 if (j
== 4 || j
< 1) {
340 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
341 (void)man_verr(m
, ln
, ppos
,
342 "ill-formed macro: %s", mac
);
345 if ( ! man_vwarn(m
, ln
, ppos
,
346 "ill-formed macro: %s", mac
))
351 if (MAN_MAX
== (c
= man_hash_find(m
->htab
, mac
))) {
352 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
353 (void)man_verr(m
, ln
, ppos
,
354 "unknown macro: %s", mac
);
357 if ( ! man_vwarn(m
, ln
, ppos
,
358 "unknown macro: %s", mac
))
363 /* The macro is sane. Jump to the next word. */
365 while (buf
[i
] && ' ' == buf
[i
])
368 /* Begin recursive parse sequence. */
370 if ( ! man_macro(m
, c
, ln
, ppos
, &i
, buf
))
376 * A NLINE macro has been immediately followed with
377 * another. Close out the preceeding macro's scope, and
380 assert(MAN_ROOT
!= m
->last
->type
);
381 assert(m
->last
->parent
);
382 assert(MAN_ROOT
!= m
->last
->parent
->type
);
385 m
->last
= m
->last
->parent
;
387 if ( ! man_valid_post(m
))
389 if ( ! man_action_post(m
))
391 m
->next
= MAN_NEXT_SIBLING
;
392 m
->flags
&= ~MAN_NLINE
;
397 err
: /* Error out. */
399 m
->flags
|= MAN_HALT
;
405 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
410 if (NULL
== man
->cb
.man_err
)
414 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
416 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
421 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
426 if (NULL
== man
->cb
.man_warn
)
430 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
432 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));