]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.10 2008/12/29 19:25:29 kristaps Exp $ */
3 * Copyright (c) 2008 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
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 __mdoc_macronames
[MDOC_MAX
] = {
30 "\\\"", "Dd", "Dt", "Os",
31 "Sh", "Ss", "Pp", "D1",
32 "Dl", "Bd", "Ed", "Bl",
33 "El", "It", "Ad", "An",
34 "Ar", "Cd", "Cm", "Dv",
35 "Er", "Ev", "Ex", "Fa",
36 "Fd", "Fl", "Fn", "Ft",
37 "Ic", "In", "Li", "Nd",
38 "Nm", "Op", "Ot", "Pa",
39 "Rv", "St", "Va", "Vt",
41 "Xr", "\%A", "\%B", "\%D",
43 "\%I", "\%J", "\%N", "\%O",
45 "\%P", "\%R", "\%T", "\%V",
46 "Ac", "Ao", "Aq", "At",
47 "Bc", "Bf", "Bo", "Bq",
48 "Bsx", "Bx", "Db", "Dc",
49 "Do", "Dq", "Ec", "Ef",
50 "Em", "Eo", "Fx", "Ms",
51 "No", "Ns", "Nx", "Ox",
52 "Pc", "Pf", "Po", "Pq",
53 "Qc", "Ql", "Qo", "Qq",
54 "Re", "Rs", "Sc", "So",
55 "Sq", "Sm", "Sx", "Sy",
56 "Tn", "Ux", "Xc", "Xo",
57 "Fo", "Fc", "Oo", "Oc",
58 "Bk", "Ek", "Bt", "Hf",
62 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
63 "split", "nosplit", "ragged",
64 "unfilled", "literal", "file",
65 "offset", "bullet", "dash",
66 "hyphen", "item", "enum",
67 "tag", "diag", "hang",
68 "ohang", "inset", "column",
69 "width", "compact", "std",
70 "p1003.1-88", "p1003.1-90", "p1003.1-96",
71 "p1003.1-2001", "p1003.1-2004", "p1003.1",
72 "p1003.1b", "p1003.1b-93", "p1003.1c-95",
73 "p1003.1g-2000", "p1003.2-92", "p1387.2-95",
74 "p1003.2", "p1387.2", "isoC-90",
75 "isoC-amd1", "isoC-tcor1", "isoC-tcor2",
76 "isoC-99", "ansiC", "ansiC-89",
77 "ansiC-99", "ieee754", "iso8802-3",
78 "xpg3", "xpg4", "xpg4.2",
79 "xpg4.3", "xbd5", "xcu5",
80 "xsh5", "xns5", "xns5.2d2.0",
81 "xcurses4.2", "susv2", "susv3",
82 "svid4", "filled", "words",
85 const struct mdoc_macro __mdoc_macros
[MDOC_MAX
] = {
87 { macro_prologue_ddate
, 0 }, /* Dd */
88 { macro_prologue_dtitle
, 0 }, /* Dt */
89 { macro_prologue_os
, 0 }, /* Os */
90 { macro_scoped_implicit
, 0 }, /* Sh */
91 { macro_scoped_implicit
, 0 }, /* Ss */
92 { macro_text
, 0 }, /* Pp */
93 { macro_scoped_line
, 0 }, /* D1 */
94 { macro_scoped_line
, 0 }, /* Dl */
95 { macro_scoped_explicit
, MDOC_EXPLICIT
}, /* Bd */
96 { macro_scoped_explicit
, 0 }, /* Ed */
97 { macro_scoped_explicit
, MDOC_EXPLICIT
}, /* Bl */
98 { macro_scoped_explicit
, 0 }, /* El */
100 { macro_text
, MDOC_CALLABLE
}, /* Ad */
101 { NULL
, 0 }, /* An */
102 { macro_text
, MDOC_CALLABLE
}, /* Ar */
103 { NULL
, 0 }, /* Cd */
104 { macro_text
, MDOC_CALLABLE
}, /* Cm */
105 { macro_text
, MDOC_CALLABLE
}, /* Dv */
106 { macro_text
, MDOC_CALLABLE
}, /* Er */
107 { macro_text
, MDOC_CALLABLE
}, /* Ev */
108 { NULL
, 0 }, /* Ex */
109 { macro_text
, MDOC_CALLABLE
}, /* Fa */
110 { NULL
, 0 }, /* Fd */
111 { macro_text
, MDOC_CALLABLE
}, /* Fl */
112 { NULL
, 0 }, /* Fn */
113 { macro_text
, 0 }, /* Ft */
114 { macro_text
, MDOC_CALLABLE
}, /* Ic */
115 { NULL
, 0 }, /* In */
116 { macro_text
, MDOC_CALLABLE
}, /* Li */
117 { macro_constant
, 0 }, /* Nd */
118 { NULL
, 0 }, /* Nm */
119 { NULL
, 0 }, /* Op */
120 { NULL
, 0 }, /* Ot */
121 { macro_text
, MDOC_CALLABLE
}, /* Pa */
122 { NULL
, 0 }, /* Rv */
123 { NULL
, 0 }, /* St */
124 { macro_text
, MDOC_CALLABLE
}, /* Va */
125 { macro_text
, MDOC_CALLABLE
}, /* Vt */
126 { NULL
, 0 }, /* Xr */
127 { NULL
, 0 }, /* %A */
128 { NULL
, 0 }, /* %B */
129 { NULL
, 0 }, /* %D */
130 { NULL
, 0 }, /* %I */
131 { NULL
, 0 }, /* %J */
132 { NULL
, 0 }, /* %N */
133 { NULL
, 0 }, /* %O */
134 { NULL
, 0 }, /* %P */
135 { NULL
, 0 }, /* %R */
136 { NULL
, 0 }, /* %T */
137 { NULL
, 0 }, /* %V */
138 { NULL
, 0 }, /* Ac */
139 { NULL
, 0 }, /* Ao */
140 { macro_scoped_pline
, MDOC_CALLABLE
}, /* Aq */
141 { macro_constant
, 0 }, /* At */
142 { NULL
, 0 }, /* Bc */
143 { NULL
, 0 }, /* Bf */
144 { NULL
, 0 }, /* Bo */
145 { macro_scoped_pline
, MDOC_CALLABLE
}, /* Bq */
146 { macro_constant_delimited
, 0 }, /* Bsx */
147 { macro_constant_delimited
, 0 }, /* Bx */
148 { NULL
, 0 }, /* Db */
149 { NULL
, 0 }, /* Dc */
150 { NULL
, 0 }, /* Do */
151 { macro_scoped_pline
, MDOC_CALLABLE
}, /* Dq */
152 { NULL
, 0 }, /* Ec */
153 { NULL
, 0 }, /* Ef */
154 { macro_text
, MDOC_CALLABLE
}, /* Em */
155 { NULL
, 0 }, /* Eo */
156 { macro_constant_delimited
, 0 }, /* Fx */
157 { macro_text
, 0 }, /* Ms */
158 { NULL
, 0 }, /* No */
159 { NULL
, 0 }, /* Ns */
160 { macro_constant_delimited
, 0 }, /* Nx */
161 { macro_constant_delimited
, 0 }, /* Ox */
162 { NULL
, 0 }, /* Pc */
163 { NULL
, 0 }, /* Pf */
164 { NULL
, 0 }, /* Po */
165 { macro_scoped_pline
, MDOC_CALLABLE
}, /* Pq */
166 { NULL
, 0 }, /* Qc */
167 { macro_scoped_pline
, MDOC_CALLABLE
}, /* Ql */
168 { NULL
, 0 }, /* Qo */
169 { macro_scoped_pline
, MDOC_CALLABLE
}, /* Qq */
170 { NULL
, 0 }, /* Re */
171 { NULL
, 0 }, /* Rs */
172 { NULL
, 0 }, /* Sc */
173 { NULL
, 0 }, /* So */
174 { macro_scoped_pline
, MDOC_CALLABLE
}, /* Sq */
175 { NULL
, 0 }, /* Sm */
176 { macro_text
, MDOC_CALLABLE
}, /* Sx */
177 { macro_text
, MDOC_CALLABLE
}, /* Sy */
178 { macro_text
, MDOC_CALLABLE
}, /* Tn */
179 { macro_constant_delimited
, 0 }, /* Ux */
180 { NULL
, 0 }, /* Xc */
181 { NULL
, 0 }, /* Xo */
182 { NULL
, 0 }, /* Fo */
183 { NULL
, 0 }, /* Fc */
184 { NULL
, 0 }, /* Oo */
185 { NULL
, 0 }, /* Oc */
186 { NULL
, 0 }, /* Bk */
187 { NULL
, 0 }, /* Ek */
188 { macro_constant
, 0 }, /* Bt */
189 { macro_constant
, 0 }, /* Hf */
190 { NULL
, 0 }, /* Fr */
191 { macro_constant
, 0 }, /* Ud */
194 const char * const *mdoc_macronames
= __mdoc_macronames
;
195 const char * const *mdoc_argnames
= __mdoc_argnames
;
196 const struct mdoc_macro
* const mdoc_macros
= __mdoc_macros
;
199 static struct mdoc_arg
*argdup(size_t, const struct mdoc_arg
*);
200 static void argfree(size_t, struct mdoc_arg
*);
201 static void argcpy(struct mdoc_arg
*,
202 const struct mdoc_arg
*);
203 static char **paramdup(size_t, const char **);
204 static void paramfree(size_t, char **);
206 static void mdoc_node_freelist(struct mdoc_node
*);
207 static void mdoc_node_append(struct mdoc
*, int,
209 static void mdoc_elem_free(struct mdoc_elem
*);
210 static void mdoc_text_free(struct mdoc_text
*);
213 const struct mdoc_node
*
214 mdoc_result(struct mdoc
*mdoc
)
222 mdoc_free(struct mdoc
*mdoc
)
226 mdoc_node_freelist(mdoc
->first
);
228 mdoc_tokhash_free(mdoc
->htab
);
235 mdoc_alloc(void *data
, const struct mdoc_cb
*cb
)
239 p
= xcalloc(1, sizeof(struct mdoc
));
242 (void)memcpy(&p
->cb
, cb
, sizeof(struct mdoc_cb
));
244 p
->htab
= mdoc_tokhash_alloc();
250 mdoc_parseln(struct mdoc
*mdoc
, char *buf
)
256 mdoc_word_alloc(mdoc
, 0, buf
);
260 if (buf
[1] && '\\' == buf
[1])
261 if (buf
[2] && '\"' == buf
[2])
265 while (buf
[i
] && ! isspace(buf
[i
]) && i
< (int)sizeof(tmp
))
268 if (i
== (int)sizeof(tmp
))
269 return(mdoc_err(mdoc
, -1, 1, ERR_MACRO_NOTSUP
));
271 return(mdoc_err(mdoc
, -1, 1, ERR_MACRO_NOTSUP
));
275 (void)memcpy(tmp
, buf
+ 1, (size_t)i
);
278 if (MDOC_MAX
== (c
= mdoc_find(mdoc
, tmp
)))
279 return(mdoc_err(mdoc
, c
, 1, ERR_MACRO_NOTSUP
));
281 while (buf
[i
] && isspace(buf
[i
]))
284 return(mdoc_macro(mdoc
, c
, 1, &i
, buf
));
289 mdoc_msg(struct mdoc
*mdoc
, int pos
, const char *fmt
, ...)
294 if (NULL
== mdoc
->cb
.mdoc_msg
)
298 (void)vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
301 (*mdoc
->cb
.mdoc_msg
)(mdoc
->data
, pos
, buf
);
306 mdoc_err(struct mdoc
*mdoc
, int tok
, int pos
, enum mdoc_err type
)
309 if (NULL
== mdoc
->cb
.mdoc_err
)
311 return((*mdoc
->cb
.mdoc_err
)(mdoc
->data
, tok
, pos
, type
));
316 mdoc_warn(struct mdoc
*mdoc
, int tok
, int pos
, enum mdoc_warn type
)
319 if (NULL
== mdoc
->cb
.mdoc_warn
)
321 return((*mdoc
->cb
.mdoc_warn
)(mdoc
->data
, tok
, pos
, type
));
326 mdoc_macro(struct mdoc
*mdoc
, int tok
, int ppos
, int *pos
, char *buf
)
329 if (NULL
== (mdoc_macros
[tok
].fp
)) {
330 (void)mdoc_err(mdoc
, tok
, ppos
, ERR_MACRO_NOTSUP
);
334 if (1 != ppos
&& ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
)) {
335 (void)mdoc_err(mdoc
, tok
, ppos
, ERR_MACRO_NOTCALL
);
339 /*mdoc_msg(mdoc, ppos, "calling `%s'", mdoc_macronames[tok]);*/
341 return((*mdoc_macros
[tok
].fp
)(mdoc
, tok
, ppos
, pos
, buf
));
346 mdoc_node_append(struct mdoc
*mdoc
, int pos
, struct mdoc_node
*p
)
348 const char *nn
, *on
, *nt
, *ot
, *act
;
352 nn
= p
->data
.text
.string
;
356 nn
= mdoc_macronames
[p
->data
.body
.tok
];
360 nn
= mdoc_macronames
[p
->data
.elem
.tok
];
364 nn
= mdoc_macronames
[p
->data
.head
.tok
];
368 nn
= mdoc_macronames
[p
->data
.block
.tok
];
376 if (NULL
== mdoc
->first
) {
377 assert(NULL
== mdoc
->last
);
380 mdoc_msg(mdoc
, pos
, "parse: root %s `%s'", nt
, nn
);
384 switch (mdoc
->last
->type
) {
390 on
= mdoc_macronames
[mdoc
->last
->data
.body
.tok
];
394 on
= mdoc_macronames
[mdoc
->last
->data
.elem
.tok
];
398 on
= mdoc_macronames
[mdoc
->last
->data
.head
.tok
];
402 on
= mdoc_macronames
[mdoc
->last
->data
.block
.tok
];
412 p
->parent
= mdoc
->last
->parent
;
413 mdoc
->last
->next
= p
;
414 p
->prev
= mdoc
->last
;
419 assert(mdoc
->last
->type
== MDOC_BLOCK
);
420 p
->parent
= mdoc
->last
;
421 mdoc
->last
->child
= p
;
426 switch (mdoc
->last
->type
) {
430 p
->parent
= mdoc
->last
;
431 mdoc
->last
->child
= p
;
435 p
->parent
= mdoc
->last
->parent
;
436 p
->prev
= mdoc
->last
;
437 mdoc
->last
->next
= p
;
444 mdoc_msg(mdoc
, pos
, "parse: %s `%s' %s %s `%s'",
445 nt
, nn
, act
, ot
, on
);
450 /* FIXME: deprecate paramsz, params. */
452 mdoc_head_alloc(struct mdoc
*mdoc
, int pos
, int tok
,
453 size_t paramsz
, const char **params
)
459 assert(mdoc
->last
->type
== MDOC_BLOCK
);
460 assert(mdoc
->last
->data
.block
.tok
== tok
);
462 p
= xcalloc(1, sizeof(struct mdoc_node
));
464 p
->data
.head
.tok
= tok
;
465 p
->data
.head
.sz
= paramsz
;
466 p
->data
.head
.args
= paramdup(paramsz
, params
);
468 mdoc_node_append(mdoc
, pos
, p
);
473 mdoc_body_alloc(struct mdoc
*mdoc
, int pos
, int tok
)
479 assert((mdoc
->last
->type
== MDOC_BLOCK
) ||
480 (mdoc
->last
->type
== MDOC_HEAD
));
481 if (mdoc
->last
->type
== MDOC_BLOCK
)
482 assert(mdoc
->last
->data
.block
.tok
== tok
);
484 assert(mdoc
->last
->data
.head
.tok
== tok
);
486 p
= xcalloc(1, sizeof(struct mdoc_node
));
489 p
->data
.body
.tok
= tok
;
491 mdoc_node_append(mdoc
, pos
, p
);
496 mdoc_block_alloc(struct mdoc
*mdoc
, int pos
, int tok
,
497 size_t argsz
, const struct mdoc_arg
*args
)
501 p
= xcalloc(1, sizeof(struct mdoc_node
));
503 p
->type
= MDOC_BLOCK
;
504 p
->data
.block
.tok
= tok
;
505 p
->data
.block
.argc
= argsz
;
506 p
->data
.block
.argv
= argdup(argsz
, args
);
508 mdoc_node_append(mdoc
, pos
, p
);
513 mdoc_elem_alloc(struct mdoc
*mdoc
, int pos
, int tok
,
514 size_t argsz
, const struct mdoc_arg
*args
,
515 size_t paramsz
, const char **params
)
519 p
= xcalloc(1, sizeof(struct mdoc_node
));
521 p
->data
.elem
.tok
= tok
;
522 p
->data
.elem
.sz
= paramsz
;
523 p
->data
.elem
.args
= paramdup(paramsz
, params
);
524 p
->data
.elem
.argc
= argsz
;
525 p
->data
.elem
.argv
= argdup(argsz
, args
);
527 mdoc_node_append(mdoc
, pos
, p
);
532 mdoc_word_alloc(struct mdoc
*mdoc
, int pos
, const char *word
)
536 p
= xcalloc(1, sizeof(struct mdoc_node
));
538 p
->data
.text
.string
= xstrdup(word
);
540 mdoc_node_append(mdoc
, pos
, p
);
545 argfree(size_t sz
, struct mdoc_arg
*p
)
554 for (i
= 0; i
< (int)sz
; i
++)
558 for (j
= 0; j
< (int)p
[i
].sz
; j
++)
566 mdoc_elem_free(struct mdoc_elem
*p
)
569 paramfree(p
->sz
, p
->args
);
570 argfree(p
->argc
, p
->argv
);
575 mdoc_block_free(struct mdoc_block
*p
)
578 argfree(p
->argc
, p
->argv
);
583 mdoc_text_free(struct mdoc_text
*p
)
592 mdoc_head_free(struct mdoc_head
*p
)
595 paramfree(p
->sz
, p
->args
);
600 mdoc_node_free(struct mdoc_node
*p
)
605 mdoc_text_free(&p
->data
.text
);
608 mdoc_elem_free(&p
->data
.elem
);
611 mdoc_block_free(&p
->data
.block
);
614 mdoc_head_free(&p
->data
.head
);
625 mdoc_node_freelist(struct mdoc_node
*p
)
629 mdoc_node_freelist(p
->child
);
631 mdoc_node_freelist(p
->next
);
638 mdoc_find(const struct mdoc
*mdoc
, const char *key
)
641 return(mdoc_tokhash_find(mdoc
->htab
, key
));
646 argcpy(struct mdoc_arg
*dst
, const struct mdoc_arg
*src
)
651 if (0 == (dst
->sz
= src
->sz
))
653 dst
->value
= xcalloc(dst
->sz
, sizeof(char *));
654 for (i
= 0; i
< (int)dst
->sz
; i
++)
655 dst
->value
[i
] = xstrdup(src
->value
[i
]);
659 static struct mdoc_arg
*
660 argdup(size_t argsz
, const struct mdoc_arg
*args
)
668 pp
= xcalloc((size_t)argsz
, sizeof(struct mdoc_arg
));
669 for (i
= 0; i
< (int)argsz
; i
++)
670 argcpy(&pp
[i
], &args
[i
]);
677 paramfree(size_t sz
, char **p
)
686 for (i
= 0; i
< (int)sz
; i
++)
693 paramdup(size_t sz
, const char **p
)
701 pp
= xcalloc(sz
, sizeof(char *));
702 for (i
= 0; i
< (int)sz
; i
++)
703 pp
[i
] = xstrdup(p
[i
]);