]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.4 2009/03/25 15:17:49 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",
37 const char * const *man_macronames
= __man_macronames
;
39 static struct man_node
*man_node_alloc(int, int, enum man_type
);
40 static int man_node_append(struct man
*,
42 static int man_ptext(struct man
*, int, char *);
43 static int man_pmacro(struct man
*, int, char *);
44 static void man_free1(struct man
*);
45 static void man_alloc1(struct man
*);
48 const struct man_node
*
49 man_node(const struct man
*m
)
52 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
56 const struct man_meta
*
57 man_meta(const struct man
*m
)
60 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
65 man_reset(struct man
*man
)
74 man_free(struct man
*man
)
80 man_hash_free(man
->htab
);
86 man_alloc(void *data
, const struct man_cb
*cb
)
90 p
= calloc(1, sizeof(struct man
));
97 (void)memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
99 p
->htab
= man_hash_alloc();
106 man_endparse(struct man
*m
)
109 if (MAN_HALT
& m
->flags
)
111 else if (man_macroend(m
))
113 m
->flags
|= MAN_HALT
;
119 man_parseln(struct man
*m
, int ln
, char *buf
)
123 man_pmacro(m
, ln
, buf
) :
124 man_ptext(m
, ln
, buf
));
129 man_free1(struct man
*man
)
133 man_node_freelist(man
->first
);
135 free(man
->meta
.title
);
144 man_alloc1(struct man
*m
)
147 bzero(&m
->meta
, sizeof(struct man_meta
));
149 m
->last
= calloc(1, sizeof(struct man_node
));
153 m
->last
->type
= MAN_ROOT
;
154 m
->next
= MAN_NEXT_CHILD
;
159 man_node_append(struct man
*man
, struct man_node
*p
)
164 assert(MAN_ROOT
!= p
->type
);
167 case (MAN_NEXT_SIBLING
):
170 p
->parent
= man
->last
->parent
;
172 case (MAN_NEXT_CHILD
):
173 man
->last
->child
= p
;
174 p
->parent
= man
->last
;
182 if ( ! man_action_pre(man
, p
))
190 if ( ! man_valid_post(man
))
193 if ( ! man_action_post(man
))
205 static struct man_node
*
206 man_node_alloc(int line
, int pos
, enum man_type type
)
210 if (NULL
== (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
);
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
);
239 if (NULL
== (p
->string
= strdup(word
)))
242 return(man_node_append(man
, p
));
247 man_node_free(struct man_node
*p
)
257 man_node_freelist(struct man_node
*p
)
261 man_node_freelist(p
->child
);
263 man_node_freelist(p
->next
);
270 man_ptext(struct man
*m
, int line
, char *buf
)
273 if ( ! man_word_alloc(m
, line
, 0, buf
))
275 m
->next
= MAN_NEXT_SIBLING
;
281 man_pmacro(struct man
*m
, int ln
, char *buf
)
286 /* Comments and empties are quickly ignored. */
293 while (buf
[i
] && ' ' == buf
[i
])
297 warnx("invalid syntax");
301 if (buf
[1] && '\\' == buf
[1])
302 if (buf
[2] && '\"' == buf
[2])
305 /* Copy the first word into a nil-terminated buffer. */
307 for (i
= 1; i
< 5; i
++) {
308 if (0 == (mac
[i
- 1] = buf
[i
]))
310 else if (' ' == buf
[i
])
316 if (i
== 5 || i
<= 1) {
317 warnx("unknown macro: %s", mac
);
321 if (MAN_MAX
== (c
= man_hash_find(m
->htab
, mac
))) {
322 warnx("unknown macro: %s", mac
);
326 /* The macro is sane. Jump to the next word. */
328 while (buf
[i
] && ' ' == buf
[i
])
331 /* Begin recursive parse sequence. */
333 if ( ! man_macro(m
, c
, ln
, 1, &i
, buf
))
338 err
: /* Error out. */
340 m
->flags
|= MAN_HALT
;
346 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
351 if (NULL
== man
->cb
.man_err
)
355 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
357 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
362 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
367 if (NULL
== man
->cb
.man_warn
)
371 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
373 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));