]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.9 2009/03/26 09:55: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",
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
, int pflags
, 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();
108 man_endparse(struct man
*m
)
111 if (MAN_HALT
& m
->flags
)
113 else if (man_macroend(m
))
115 m
->flags
|= MAN_HALT
;
121 man_parseln(struct man
*m
, int ln
, char *buf
)
125 man_pmacro(m
, ln
, buf
) :
126 man_ptext(m
, ln
, buf
));
131 man_free1(struct man
*man
)
135 man_node_freelist(man
->first
);
137 free(man
->meta
.title
);
138 if (man
->meta
.source
)
139 free(man
->meta
.source
);
146 man_alloc1(struct man
*m
)
149 bzero(&m
->meta
, sizeof(struct man_meta
));
151 m
->last
= calloc(1, sizeof(struct man_node
));
155 m
->last
->type
= MAN_ROOT
;
156 m
->next
= MAN_NEXT_CHILD
;
161 man_node_append(struct man
*man
, struct man_node
*p
)
166 assert(MAN_ROOT
!= p
->type
);
169 case (MAN_NEXT_SIBLING
):
172 p
->parent
= man
->last
->parent
;
174 case (MAN_NEXT_CHILD
):
175 man
->last
->child
= p
;
176 p
->parent
= man
->last
;
187 if ( ! man_valid_post(man
))
189 if ( ! man_action_post(man
))
200 static struct man_node
*
201 man_node_alloc(int line
, int pos
, enum man_type type
)
205 if (NULL
== (p
= calloc(1, sizeof(struct man_node
))))
216 man_elem_alloc(struct man
*man
, int line
, int pos
, int tok
)
220 p
= man_node_alloc(line
, pos
, MAN_ELEM
);
223 return(man_node_append(man
, p
));
228 man_word_alloc(struct man
*man
,
229 int line
, int pos
, const char *word
)
233 p
= man_node_alloc(line
, pos
, MAN_TEXT
);
234 if (NULL
== (p
->string
= strdup(word
)))
237 return(man_node_append(man
, p
));
242 man_node_free(struct man_node
*p
)
252 man_node_freelist(struct man_node
*p
)
256 man_node_freelist(p
->child
);
258 man_node_freelist(p
->next
);
265 man_ptext(struct man
*m
, int line
, char *buf
)
268 if ( ! man_word_alloc(m
, line
, 0, buf
))
270 m
->next
= MAN_NEXT_SIBLING
;
276 man_pmacro(struct man
*m
, int ln
, char *buf
)
281 /* Comments and empties are quickly ignored. */
290 while (buf
[i
] && ' ' == buf
[i
])
296 if (buf
[i
] && '\\' == buf
[i
])
297 if (buf
[i
+ 1] && '\"' == buf
[i
+ 1])
300 /* Copy the first word into a nil-terminated buffer. */
302 for (j
= 0; j
< 4; j
++) {
303 if (0 == (mac
[j
] = buf
[j
+ i
]))
305 else if (' ' == buf
[j
+ i
])
311 if (j
== 4 || j
< 1) {
312 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
313 (void)man_verr(m
, ln
, i
,
314 "ill-formed macro: %s", mac
);
317 if ( ! man_vwarn(m
, ln
, 0, "ill-formed macro: %s", mac
))
322 if (MAN_MAX
== (c
= man_hash_find(m
->htab
, mac
))) {
323 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
324 (void)man_verr(m
, ln
, i
,
325 "unknown macro: %s", mac
);
328 if ( ! man_vwarn(m
, ln
, i
, "unknown macro: %s", mac
))
333 /* The macro is sane. Jump to the next word. */
335 while (buf
[i
] && ' ' == buf
[i
])
338 /* Begin recursive parse sequence. */
340 if ( ! man_macro(m
, c
, ln
, 1, &i
, buf
))
345 err
: /* Error out. */
347 m
->flags
|= MAN_HALT
;
353 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
358 if (NULL
== man
->cb
.man_err
)
362 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
364 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
369 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
374 if (NULL
== man
->cb
.man_warn
)
378 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
380 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));