]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.28 2009/07/24 20:22:24 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 const char *const __man_merrnames
[WERRMAX
] = {
27 "invalid character", /* WNPRINT */
28 "system: malloc error", /* WNMEM */
29 "invalid manual section", /* WMSEC */
30 "invalid date format", /* WDATE */
31 "scope of prior line violated", /* WLNSCOPE */
32 "trailing whitespace", /* WTSPACE */
33 "unterminated quoted parameter", /* WTQUOTE */
34 "document has no body", /* WNODATA */
35 "document has no title/section", /* WNOTITLE */
36 "invalid escape sequence", /* WESCAPE */
37 "invalid number format", /* WNUMFMT */
40 const char *const __man_macronames
[MAN_MAX
] = {
41 "br", "TH", "SH", "SS",
42 "TP", "LP", "PP", "P",
43 "IP", "HP", "SM", "SB",
44 "BI", "IB", "BR", "RB",
49 const char * const *man_macronames
= __man_macronames
;
51 static struct man_node
*man_node_alloc(int, int,
53 static int man_node_append(struct man
*,
55 static int man_ptext(struct man
*, int, char *);
56 static int man_pmacro(struct man
*, int, char *);
57 static void man_free1(struct man
*);
58 static int man_alloc1(struct man
*);
61 const struct man_node
*
62 man_node(const struct man
*m
)
65 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
69 const struct man_meta
*
70 man_meta(const struct man
*m
)
73 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
78 man_reset(struct man
*man
)
82 return(man_alloc1(man
));
87 man_free(struct man
*man
)
93 man_hash_free(man
->htab
);
99 man_alloc(void *data
, int pflags
, const struct man_cb
*cb
)
103 if (NULL
== (p
= calloc(1, sizeof(struct man
))))
106 if ( ! man_alloc1(p
)) {
113 (void)memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
115 if (NULL
== (p
->htab
= man_hash_alloc())) {
124 man_endparse(struct man
*m
)
127 if (MAN_HALT
& m
->flags
)
129 else if (man_macroend(m
))
131 m
->flags
|= MAN_HALT
;
137 man_parseln(struct man
*m
, int ln
, char *buf
)
141 man_pmacro(m
, ln
, buf
) :
142 man_ptext(m
, ln
, buf
));
147 man_free1(struct man
*man
)
151 man_node_freelist(man
->first
);
153 free(man
->meta
.title
);
154 if (man
->meta
.source
)
155 free(man
->meta
.source
);
162 man_alloc1(struct man
*m
)
165 bzero(&m
->meta
, sizeof(struct man_meta
));
167 m
->last
= calloc(1, sizeof(struct man_node
));
171 m
->last
->type
= MAN_ROOT
;
172 m
->next
= MAN_NEXT_CHILD
;
178 man_node_append(struct man
*man
, struct man_node
*p
)
183 assert(MAN_ROOT
!= p
->type
);
186 case (MAN_NEXT_SIBLING
):
189 p
->parent
= man
->last
->parent
;
191 case (MAN_NEXT_CHILD
):
192 man
->last
->child
= p
;
193 p
->parent
= man
->last
;
206 if ( ! man_valid_post(man
))
208 if ( ! man_action_post(man
))
219 static struct man_node
*
220 man_node_alloc(int line
, int pos
, enum man_type type
, int tok
)
224 p
= calloc(1, sizeof(struct man_node
));
237 man_elem_alloc(struct man
*man
, int line
, int pos
, int tok
)
241 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
244 return(man_node_append(man
, p
));
249 man_word_alloc(struct man
*man
,
250 int line
, int pos
, const char *word
)
254 p
= man_node_alloc(line
, pos
, MAN_TEXT
, -1);
257 if (NULL
== (p
->string
= strdup(word
)))
259 return(man_node_append(man
, p
));
264 man_node_free(struct man_node
*p
)
276 man_node_freelist(struct man_node
*p
)
280 man_node_freelist(p
->child
);
282 man_node_freelist(p
->next
);
284 assert(0 == p
->nchild
);
290 man_ptext(struct man
*m
, int line
, char *buf
)
293 if ( ! man_word_alloc(m
, line
, 0, buf
))
295 m
->next
= MAN_NEXT_SIBLING
;
298 * If this is one of the zany NLINE macros that consumes the
299 * next line of input as being influenced, then close out the
300 * existing macro "scope" and continue processing.
303 if ( ! (MAN_NLINE
& m
->flags
))
306 m
->flags
&= ~MAN_NLINE
;
307 m
->last
= m
->last
->parent
;
309 assert(MAN_ROOT
!= m
->last
->type
);
310 if ( ! man_valid_post(m
))
312 if ( ! man_action_post(m
))
320 man_pmacro(struct man
*m
, int ln
, char *buf
)
322 int i
, j
, c
, ppos
, fl
;
326 /* Comments and empties are quickly ignored. */
329 fl
= MAN_NLINE
& m
->flags
;
338 while (buf
[i
] && ' ' == buf
[i
])
346 /* Copy the first word into a nil-terminated buffer. */
348 for (j
= 0; j
< 4; j
++, i
++) {
349 if (0 == (mac
[j
] = buf
[i
]))
351 else if (' ' == buf
[i
])
357 if (j
== 4 || j
< 1) {
358 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
359 (void)man_verr(m
, ln
, ppos
,
360 "ill-formed macro: %s", mac
);
363 if ( ! man_vwarn(m
, ln
, ppos
,
364 "ill-formed macro: %s", mac
))
369 if (MAN_MAX
== (c
= man_hash_find(m
->htab
, mac
))) {
370 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
371 (void)man_verr(m
, ln
, ppos
,
372 "unknown macro: %s", mac
);
375 if ( ! man_vwarn(m
, ln
, ppos
,
376 "unknown macro: %s", mac
))
381 /* The macro is sane. Jump to the next word. */
383 while (buf
[i
] && ' ' == buf
[i
])
386 /* Begin recursive parse sequence. */
388 if ( ! man_macro(m
, c
, ln
, ppos
, &i
, buf
))
394 * A NLINE macro has been immediately followed with
395 * another. Close out the preceding macro's scope, and
398 assert(MAN_ROOT
!= m
->last
->type
);
399 assert(m
->last
->parent
);
400 assert(MAN_ROOT
!= m
->last
->parent
->type
);
403 m
->last
= m
->last
->parent
;
405 if ( ! man_valid_post(m
))
407 if ( ! man_action_post(m
))
409 m
->next
= MAN_NEXT_SIBLING
;
410 m
->flags
&= ~MAN_NLINE
;
415 err
: /* Error out. */
417 m
->flags
|= MAN_HALT
;
423 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
428 if (NULL
== man
->cb
.man_err
)
432 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
434 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
439 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
444 if (NULL
== man
->cb
.man_warn
)
448 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
450 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));
455 man_err(struct man
*m
, int line
, int pos
, int iserr
, enum merr type
)
459 p
= __man_merrnames
[(int)type
];
463 return(man_verr(m
, line
, pos
, p
));
465 return(man_vwarn(m
, line
, pos
, p
));