]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
c0b36b26106f2678792903e2ce79c54696d82b79
1 /* $Id: man.c,v 1.16 2009/04/05 16:34:22 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.
28 const char *const __man_macronames
[MAN_MAX
] = {
29 "\\\"", "TH", "SH", "SS",
30 "TP", "LP", "PP", "P",
31 "IP", "HP", "SM", "SB",
32 "BI", "IB", "BR", "RB",
37 const char * const *man_macronames
= __man_macronames
;
39 static struct man_node
*man_node_alloc(int, int,
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 int 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
)
70 return(man_alloc1(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 if (NULL
== (p
= calloc(1, sizeof(struct man
))))
94 if ( ! man_alloc1(p
)) {
101 (void)memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
103 if (NULL
== (p
->htab
= man_hash_alloc())) {
112 man_endparse(struct man
*m
)
115 if (MAN_HALT
& m
->flags
)
117 else if (man_macroend(m
))
119 m
->flags
|= MAN_HALT
;
125 man_parseln(struct man
*m
, int ln
, char *buf
)
129 man_pmacro(m
, ln
, buf
) :
130 man_ptext(m
, ln
, buf
));
135 man_free1(struct man
*man
)
139 man_node_freelist(man
->first
);
141 free(man
->meta
.title
);
142 if (man
->meta
.source
)
143 free(man
->meta
.source
);
150 man_alloc1(struct man
*m
)
153 bzero(&m
->meta
, sizeof(struct man_meta
));
155 m
->last
= calloc(1, sizeof(struct man_node
));
159 m
->last
->type
= MAN_ROOT
;
160 m
->next
= MAN_NEXT_CHILD
;
166 man_node_append(struct man
*man
, struct man_node
*p
)
171 assert(MAN_ROOT
!= p
->type
);
174 case (MAN_NEXT_SIBLING
):
177 p
->parent
= man
->last
->parent
;
179 case (MAN_NEXT_CHILD
):
180 man
->last
->child
= p
;
181 p
->parent
= man
->last
;
192 if ( ! man_valid_post(man
))
194 if ( ! man_action_post(man
))
205 static struct man_node
*
206 man_node_alloc(int line
, int pos
, enum man_type type
, int tok
)
210 p
= calloc(1, sizeof(struct man_node
));
223 man_elem_alloc(struct man
*man
, int line
, int pos
, int tok
)
227 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
230 return(man_node_append(man
, p
));
235 man_word_alloc(struct man
*man
,
236 int line
, int pos
, const char *word
)
240 p
= man_node_alloc(line
, pos
, MAN_TEXT
, -1);
243 if (NULL
== (p
->string
= strdup(word
)))
245 return(man_node_append(man
, p
));
250 man_node_free(struct man_node
*p
)
260 man_node_freelist(struct man_node
*p
)
264 man_node_freelist(p
->child
);
266 man_node_freelist(p
->next
);
273 man_ptext(struct man
*m
, int line
, char *buf
)
276 if ( ! man_word_alloc(m
, line
, 0, buf
))
278 m
->next
= MAN_NEXT_SIBLING
;
281 * If this is one of the zany NLINE macros that consumes the
282 * next line of input as being influenced, then close out the
283 * existing macro "scope" and continue processing.
286 if ( ! (MAN_NLINE
& m
->flags
))
289 m
->flags
&= ~MAN_NLINE
;
290 m
->last
= m
->last
->parent
;
292 assert(MAN_ROOT
!= m
->last
->type
);
293 if ( ! man_valid_post(m
))
295 if ( ! man_action_post(m
))
303 man_pmacro(struct man
*m
, int ln
, char *buf
)
305 int i
, j
, c
, ppos
, fl
;
309 /* Comments and empties are quickly ignored. */
312 fl
= MAN_NLINE
& m
->flags
;
321 while (buf
[i
] && ' ' == buf
[i
])
329 if (buf
[i
] && '\\' == buf
[i
])
330 if (buf
[i
+ 1] && '\"' == buf
[i
+ 1])
333 /* Copy the first word into a nil-terminated buffer. */
335 for (j
= 0; j
< 4; j
++, i
++) {
336 if (0 == (mac
[j
] = buf
[i
]))
338 else if (' ' == buf
[i
])
344 if (j
== 4 || j
< 1) {
345 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
346 (void)man_verr(m
, ln
, ppos
,
347 "ill-formed macro: %s", mac
);
350 if ( ! man_vwarn(m
, ln
, ppos
,
351 "ill-formed macro: %s", mac
))
356 if (MAN_MAX
== (c
= man_hash_find(m
->htab
, mac
))) {
357 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
358 (void)man_verr(m
, ln
, ppos
,
359 "unknown macro: %s", mac
);
362 if ( ! man_vwarn(m
, ln
, ppos
,
363 "unknown macro: %s", mac
))
368 /* The macro is sane. Jump to the next word. */
370 while (buf
[i
] && ' ' == buf
[i
])
373 /* Begin recursive parse sequence. */
375 if ( ! man_macro(m
, c
, ln
, ppos
, &i
, buf
))
381 * A NLINE macro has been immediately followed with
382 * another. Close out the preceeding macro's scope, and
385 assert(MAN_ROOT
!= m
->last
->type
);
386 assert(m
->last
->parent
);
387 assert(MAN_ROOT
!= m
->last
->parent
->type
);
390 m
->last
= m
->last
->parent
;
392 if ( ! man_valid_post(m
))
394 if ( ! man_action_post(m
))
396 m
->next
= MAN_NEXT_SIBLING
;
397 m
->flags
&= ~MAN_NLINE
;
402 err
: /* Error out. */
404 m
->flags
|= MAN_HALT
;
410 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
415 if (NULL
== man
->cb
.man_err
)
419 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
421 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
426 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
431 if (NULL
== man
->cb
.man_warn
)
435 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
437 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));