1 /* $Id: man.c,v 1.3 2009/03/23 15:41:09 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
);
90 p
= calloc(1, sizeof(struct man
));
96 p
->htab
= man_hash_alloc();
102 man_endparse(struct man
*m
)
105 if (MAN_HALT
& m
->flags
)
107 else if (man_macroend(m
))
109 m
->flags
|= MAN_HALT
;
115 man_parseln(struct man
*m
, int ln
, char *buf
)
119 man_pmacro(m
, ln
, buf
) :
120 man_ptext(m
, ln
, buf
));
125 man_free1(struct man
*man
)
129 man_node_freelist(man
->first
);
131 free(man
->meta
.title
);
140 man_alloc1(struct man
*m
)
143 bzero(&m
->meta
, sizeof(struct man_meta
));
145 m
->last
= calloc(1, sizeof(struct man_node
));
149 m
->last
->type
= MAN_ROOT
;
150 m
->next
= MAN_NEXT_CHILD
;
155 man_node_append(struct man
*man
, struct man_node
*p
)
160 assert(MAN_ROOT
!= p
->type
);
163 case (MAN_NEXT_SIBLING
):
166 p
->parent
= man
->last
->parent
;
168 case (MAN_NEXT_CHILD
):
169 man
->last
->child
= p
;
170 p
->parent
= man
->last
;
178 if ( ! man_valid_pre(man
, p
))
180 if ( ! man_action_pre(man
, p
))
189 if ( ! man_valid_post(man
))
191 if ( ! man_action_post(man
))
203 static struct man_node
*
204 man_node_alloc(int line
, int pos
, enum man_type type
)
208 if (NULL
== (p
= calloc(1, sizeof(struct man_node
))))
219 man_elem_alloc(struct man
*man
, int line
, int pos
, int tok
)
223 p
= man_node_alloc(line
, pos
, MAN_ELEM
);
226 return(man_node_append(man
, p
));
231 man_word_alloc(struct man
*man
,
232 int line
, int pos
, const char *word
)
236 p
= man_node_alloc(line
, pos
, MAN_TEXT
);
237 if (NULL
== (p
->string
= strdup(word
)))
240 return(man_node_append(man
, p
));
245 man_node_free(struct man_node
*p
)
255 man_node_freelist(struct man_node
*p
)
259 man_node_freelist(p
->child
);
261 man_node_freelist(p
->next
);
268 man_ptext(struct man
*m
, int line
, char *buf
)
271 if ( ! man_word_alloc(m
, line
, 0, buf
))
273 m
->next
= MAN_NEXT_SIBLING
;
279 man_pmacro(struct man
*m
, int ln
, char *buf
)
284 /* Comments and empties are quickly ignored. */
291 while (buf
[i
] && ' ' == buf
[i
])
295 warnx("invalid syntax");
299 if (buf
[1] && '\\' == buf
[1])
300 if (buf
[2] && '\"' == buf
[2])
303 /* Copy the first word into a nil-terminated buffer. */
305 for (i
= 1; i
< 5; i
++) {
306 if (0 == (mac
[i
- 1] = buf
[i
]))
308 else if (' ' == buf
[i
])
314 if (i
== 5 || i
<= 1) {
315 warnx("unknown macro: %s", mac
);
319 if (MAN_MAX
== (c
= man_hash_find(m
->htab
, mac
))) {
320 warnx("unknown macro: %s", mac
);
324 /* The macro is sane. Jump to the next word. */
326 while (buf
[i
] && ' ' == buf
[i
])
329 /* Begin recursive parse sequence. */
331 if ( ! man_macro(m
, c
, ln
, 1, &i
, buf
))
336 err
: /* Error out. */
338 m
->flags
|= MAN_HALT
;