]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
c27ddaf8002c4e527f5984907d2f21be24a31858
1 /* $Id: man.c,v 1.27 2009/07/07 09:35:40 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 */
39 const char *const __man_macronames
[MAN_MAX
] = {
40 "br", "TH", "SH", "SS",
41 "TP", "LP", "PP", "P",
42 "IP", "HP", "SM", "SB",
43 "BI", "IB", "BR", "RB",
48 const char * const *man_macronames
= __man_macronames
;
50 static struct man_node
*man_node_alloc(int, int,
52 static int man_node_append(struct man
*,
54 static int man_ptext(struct man
*, int, char *);
55 static int man_pmacro(struct man
*, int, char *);
56 static void man_free1(struct man
*);
57 static int man_alloc1(struct man
*);
60 const struct man_node
*
61 man_node(const struct man
*m
)
64 return(MAN_HALT
& m
->flags
? NULL
: m
->first
);
68 const struct man_meta
*
69 man_meta(const struct man
*m
)
72 return(MAN_HALT
& m
->flags
? NULL
: &m
->meta
);
77 man_reset(struct man
*man
)
81 return(man_alloc1(man
));
86 man_free(struct man
*man
)
92 man_hash_free(man
->htab
);
98 man_alloc(void *data
, int pflags
, const struct man_cb
*cb
)
102 if (NULL
== (p
= calloc(1, sizeof(struct man
))))
105 if ( ! man_alloc1(p
)) {
112 (void)memcpy(&p
->cb
, cb
, sizeof(struct man_cb
));
114 if (NULL
== (p
->htab
= man_hash_alloc())) {
123 man_endparse(struct man
*m
)
126 if (MAN_HALT
& m
->flags
)
128 else if (man_macroend(m
))
130 m
->flags
|= MAN_HALT
;
136 man_parseln(struct man
*m
, int ln
, char *buf
)
140 man_pmacro(m
, ln
, buf
) :
141 man_ptext(m
, ln
, buf
));
146 man_free1(struct man
*man
)
150 man_node_freelist(man
->first
);
152 free(man
->meta
.title
);
153 if (man
->meta
.source
)
154 free(man
->meta
.source
);
161 man_alloc1(struct man
*m
)
164 bzero(&m
->meta
, sizeof(struct man_meta
));
166 m
->last
= calloc(1, sizeof(struct man_node
));
170 m
->last
->type
= MAN_ROOT
;
171 m
->next
= MAN_NEXT_CHILD
;
177 man_node_append(struct man
*man
, struct man_node
*p
)
182 assert(MAN_ROOT
!= p
->type
);
185 case (MAN_NEXT_SIBLING
):
188 p
->parent
= man
->last
->parent
;
190 case (MAN_NEXT_CHILD
):
191 man
->last
->child
= p
;
192 p
->parent
= man
->last
;
205 if ( ! man_valid_post(man
))
207 if ( ! man_action_post(man
))
218 static struct man_node
*
219 man_node_alloc(int line
, int pos
, enum man_type type
, int tok
)
223 p
= calloc(1, sizeof(struct man_node
));
236 man_elem_alloc(struct man
*man
, int line
, int pos
, int tok
)
240 p
= man_node_alloc(line
, pos
, MAN_ELEM
, tok
);
243 return(man_node_append(man
, p
));
248 man_word_alloc(struct man
*man
,
249 int line
, int pos
, const char *word
)
253 p
= man_node_alloc(line
, pos
, MAN_TEXT
, -1);
256 if (NULL
== (p
->string
= strdup(word
)))
258 return(man_node_append(man
, p
));
263 man_node_free(struct man_node
*p
)
275 man_node_freelist(struct man_node
*p
)
279 man_node_freelist(p
->child
);
281 man_node_freelist(p
->next
);
283 assert(0 == p
->nchild
);
289 man_ptext(struct man
*m
, int line
, char *buf
)
292 if ( ! man_word_alloc(m
, line
, 0, buf
))
294 m
->next
= MAN_NEXT_SIBLING
;
297 * If this is one of the zany NLINE macros that consumes the
298 * next line of input as being influenced, then close out the
299 * existing macro "scope" and continue processing.
302 if ( ! (MAN_NLINE
& m
->flags
))
305 m
->flags
&= ~MAN_NLINE
;
306 m
->last
= m
->last
->parent
;
308 assert(MAN_ROOT
!= m
->last
->type
);
309 if ( ! man_valid_post(m
))
311 if ( ! man_action_post(m
))
319 man_pmacro(struct man
*m
, int ln
, char *buf
)
321 int i
, j
, c
, ppos
, fl
;
325 /* Comments and empties are quickly ignored. */
328 fl
= MAN_NLINE
& m
->flags
;
337 while (buf
[i
] && ' ' == buf
[i
])
345 /* Copy the first word into a nil-terminated buffer. */
347 for (j
= 0; j
< 4; j
++, i
++) {
348 if (0 == (mac
[j
] = buf
[i
]))
350 else if (' ' == buf
[i
])
356 if (j
== 4 || j
< 1) {
357 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
358 (void)man_verr(m
, ln
, ppos
,
359 "ill-formed macro: %s", mac
);
362 if ( ! man_vwarn(m
, ln
, ppos
,
363 "ill-formed macro: %s", mac
))
368 if (MAN_MAX
== (c
= man_hash_find(m
->htab
, mac
))) {
369 if ( ! (MAN_IGN_MACRO
& m
->pflags
)) {
370 (void)man_verr(m
, ln
, ppos
,
371 "unknown macro: %s", mac
);
374 if ( ! man_vwarn(m
, ln
, ppos
,
375 "unknown macro: %s", mac
))
380 /* The macro is sane. Jump to the next word. */
382 while (buf
[i
] && ' ' == buf
[i
])
385 /* Begin recursive parse sequence. */
387 if ( ! man_macro(m
, c
, ln
, ppos
, &i
, buf
))
393 * A NLINE macro has been immediately followed with
394 * another. Close out the preceding macro's scope, and
397 assert(MAN_ROOT
!= m
->last
->type
);
398 assert(m
->last
->parent
);
399 assert(MAN_ROOT
!= m
->last
->parent
->type
);
402 m
->last
= m
->last
->parent
;
404 if ( ! man_valid_post(m
))
406 if ( ! man_action_post(m
))
408 m
->next
= MAN_NEXT_SIBLING
;
409 m
->flags
&= ~MAN_NLINE
;
414 err
: /* Error out. */
416 m
->flags
|= MAN_HALT
;
422 man_verr(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
427 if (NULL
== man
->cb
.man_err
)
431 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
433 return((*man
->cb
.man_err
)(man
->data
, ln
, pos
, buf
));
438 man_vwarn(struct man
*man
, int ln
, int pos
, const char *fmt
, ...)
443 if (NULL
== man
->cb
.man_warn
)
447 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
449 return((*man
->cb
.man_warn
)(man
->data
, ln
, pos
, buf
));
454 man_err(struct man
*m
, int line
, int pos
, int iserr
, enum merr type
)
458 p
= __man_merrnames
[(int)type
];
462 return(man_verr(m
, line
, pos
, p
));
464 return(man_vwarn(m
, line
, pos
, p
));