]> git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
Fixed email address in manual AUTHOR reference.
[mandoc.git] / mdoc.c
1 /* $Id: mdoc.c,v 1.76 2009/06/10 20:18:43 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
4 *
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.
8 *
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.
16 */
17 #include <assert.h>
18 #include <ctype.h>
19 #include <stdarg.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "libmdoc.h"
25
26 enum merr {
27 ENOCALL,
28 EBODYPROL,
29 EPROLBODY,
30 ESPACE,
31 ETEXTPROL,
32 ENOBLANK,
33 EMALLOC
34 };
35
36 const char *const __mdoc_macronames[MDOC_MAX] = {
37 "\\\"", "Dd", "Dt", "Os",
38 "Sh", "Ss", "Pp", "D1",
39 "Dl", "Bd", "Ed", "Bl",
40 "El", "It", "Ad", "An",
41 "Ar", "Cd", "Cm", "Dv",
42 "Er", "Ev", "Ex", "Fa",
43 "Fd", "Fl", "Fn", "Ft",
44 "Ic", "In", "Li", "Nd",
45 "Nm", "Op", "Ot", "Pa",
46 "Rv", "St", "Va", "Vt",
47 /* LINTED */
48 "Xr", "\%A", "\%B", "\%D",
49 /* LINTED */
50 "\%I", "\%J", "\%N", "\%O",
51 /* LINTED */
52 "\%P", "\%R", "\%T", "\%V",
53 "Ac", "Ao", "Aq", "At",
54 "Bc", "Bf", "Bo", "Bq",
55 "Bsx", "Bx", "Db", "Dc",
56 "Do", "Dq", "Ec", "Ef",
57 "Em", "Eo", "Fx", "Ms",
58 "No", "Ns", "Nx", "Ox",
59 "Pc", "Pf", "Po", "Pq",
60 "Qc", "Ql", "Qo", "Qq",
61 "Re", "Rs", "Sc", "So",
62 "Sq", "Sm", "Sx", "Sy",
63 "Tn", "Ux", "Xc", "Xo",
64 "Fo", "Fc", "Oo", "Oc",
65 "Bk", "Ek", "Bt", "Hf",
66 "Fr", "Ud", "Lb", "Ap",
67 "Lp", "Lk", "Mt", "Brq",
68 /* LINTED */
69 "Bro", "Brc", "\%C", "Es",
70 /* LINTED */
71 "En", "Dx", "\%Q"
72 };
73
74 const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
75 "split", "nosplit", "ragged",
76 "unfilled", "literal", "file",
77 "offset", "bullet", "dash",
78 "hyphen", "item", "enum",
79 "tag", "diag", "hang",
80 "ohang", "inset", "column",
81 "width", "compact", "std",
82 "filled", "words", "emphasis",
83 "symbolic", "nested"
84 };
85
86 const char * const *mdoc_macronames = __mdoc_macronames;
87 const char * const *mdoc_argnames = __mdoc_argnames;
88
89 static void mdoc_free1(struct mdoc *);
90 static int mdoc_alloc1(struct mdoc *);
91 static struct mdoc_node *node_alloc(struct mdoc *, int, int,
92 int, enum mdoc_type);
93 static int node_append(struct mdoc *,
94 struct mdoc_node *);
95 static int parsetext(struct mdoc *, int, char *);
96 static int parsemacro(struct mdoc *, int, char *);
97 static int macrowarn(struct mdoc *, int, const char *);
98 static int perr(struct mdoc *, int, int, enum merr);
99
100 #define verr(m, t) perr((m), (m)->last->line, (m)->last->pos, (t))
101
102 /*
103 * Get the first (root) node of the parse tree.
104 */
105 const struct mdoc_node *
106 mdoc_node(const struct mdoc *m)
107 {
108
109 return(MDOC_HALT & m->flags ? NULL : m->first);
110 }
111
112
113 const struct mdoc_meta *
114 mdoc_meta(const struct mdoc *m)
115 {
116
117 return(MDOC_HALT & m->flags ? NULL : &m->meta);
118 }
119
120
121 static void
122 mdoc_free1(struct mdoc *mdoc)
123 {
124
125 if (mdoc->first)
126 mdoc_node_freelist(mdoc->first);
127 if (mdoc->meta.title)
128 free(mdoc->meta.title);
129 if (mdoc->meta.os)
130 free(mdoc->meta.os);
131 if (mdoc->meta.name)
132 free(mdoc->meta.name);
133 if (mdoc->meta.arch)
134 free(mdoc->meta.arch);
135 if (mdoc->meta.vol)
136 free(mdoc->meta.vol);
137 }
138
139
140 static int
141 mdoc_alloc1(struct mdoc *mdoc)
142 {
143
144 bzero(&mdoc->meta, sizeof(struct mdoc_meta));
145 mdoc->flags = 0;
146 mdoc->lastnamed = mdoc->lastsec = 0;
147 mdoc->last = calloc(1, sizeof(struct mdoc_node));
148 if (NULL == mdoc->last)
149 return(0);
150
151 mdoc->first = mdoc->last;
152 mdoc->last->type = MDOC_ROOT;
153 mdoc->next = MDOC_NEXT_CHILD;
154 return(1);
155 }
156
157
158 /*
159 * Free up all resources contributed by a parse: the node tree,
160 * meta-data and so on. Then reallocate the root node for another
161 * parse.
162 */
163 int
164 mdoc_reset(struct mdoc *mdoc)
165 {
166
167 mdoc_free1(mdoc);
168 return(mdoc_alloc1(mdoc));
169 }
170
171
172 /*
173 * Completely free up all resources.
174 */
175 void
176 mdoc_free(struct mdoc *mdoc)
177 {
178
179 mdoc_free1(mdoc);
180 if (mdoc->htab)
181 mdoc_hash_free(mdoc->htab);
182 free(mdoc);
183 }
184
185
186 struct mdoc *
187 mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
188 {
189 struct mdoc *p;
190
191 if (NULL == (p = calloc(1, sizeof(struct mdoc))))
192 return(NULL);
193 if (cb)
194 (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
195
196 p->data = data;
197 p->pflags = pflags;
198
199 if (NULL == (p->htab = mdoc_hash_alloc())) {
200 free(p);
201 return(NULL);
202 } else if (mdoc_alloc1(p))
203 return(p);
204
205 free(p);
206 return(NULL);
207 }
208
209
210 /*
211 * Climb back up the parse tree, validating open scopes. Mostly calls
212 * through to macro_end in macro.c.
213 */
214 int
215 mdoc_endparse(struct mdoc *m)
216 {
217
218 if (MDOC_HALT & m->flags)
219 return(0);
220 else if (mdoc_macroend(m))
221 return(1);
222 m->flags |= MDOC_HALT;
223 return(0);
224 }
225
226
227 /*
228 * Main parse routine. Parses a single line -- really just hands off to
229 * the macro or text parser.
230 */
231 int
232 mdoc_parseln(struct mdoc *m, int ln, char *buf)
233 {
234
235 /* If in error-mode, then we parse no more. */
236
237 if (MDOC_HALT & m->flags)
238 return(0);
239
240 return('.' == *buf ? parsemacro(m, ln, buf) :
241 parsetext(m, ln, buf));
242 }
243
244
245 void
246 mdoc_vmsg(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
247 {
248 char buf[256];
249 va_list ap;
250
251 if (NULL == mdoc->cb.mdoc_msg)
252 return;
253
254 va_start(ap, fmt);
255 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
256 va_end(ap);
257 (*mdoc->cb.mdoc_msg)(mdoc->data, ln, pos, buf);
258 }
259
260
261 int
262 mdoc_verr(struct mdoc *mdoc, int ln, int pos,
263 const char *fmt, ...)
264 {
265 char buf[256];
266 va_list ap;
267
268 if (NULL == mdoc->cb.mdoc_err)
269 return(0);
270
271 va_start(ap, fmt);
272 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
273 va_end(ap);
274 return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
275 }
276
277
278 int
279 mdoc_vwarn(struct mdoc *mdoc, int ln, int pos,
280 enum mdoc_warn type, const char *fmt, ...)
281 {
282 char buf[256];
283 va_list ap;
284
285 if (NULL == mdoc->cb.mdoc_warn)
286 return(0);
287
288 va_start(ap, fmt);
289 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
290 va_end(ap);
291 return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, buf));
292 }
293
294
295 int
296 mdoc_macro(struct mdoc *m, int tok,
297 int ln, int pp, int *pos, char *buf)
298 {
299
300 /* FIXME - these should happen during validation. */
301
302 if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
303 SEC_PROLOGUE != m->lastnamed)
304 return(perr(m, ln, pp, EPROLBODY));
305
306 if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
307 SEC_PROLOGUE == m->lastnamed)
308 return(perr(m, ln, pp, EBODYPROL));
309
310 if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
311 return(perr(m, ln, pp, ENOCALL));
312
313 return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
314 }
315
316
317 static int
318 perr(struct mdoc *m, int line, int pos, enum merr type)
319 {
320 char *p;
321
322 p = NULL;
323 switch (type) {
324 case (ENOCALL):
325 p = "not callable";
326 break;
327 case (EPROLBODY):
328 p = "macro disallowed in document body";
329 break;
330 case (EBODYPROL):
331 p = "macro disallowed in document prologue";
332 break;
333 case (EMALLOC):
334 p = "memory exhausted";
335 break;
336 case (ETEXTPROL):
337 p = "text disallowed in document prologue";
338 break;
339 case (ENOBLANK):
340 p = "blank lines disallowed in non-literal contexts";
341 break;
342 case (ESPACE):
343 p = "whitespace disallowed after delimiter";
344 break;
345 }
346 assert(p);
347 return(mdoc_perr(m, line, pos, p));
348 }
349
350
351 static int
352 node_append(struct mdoc *mdoc, struct mdoc_node *p)
353 {
354
355 assert(mdoc->last);
356 assert(mdoc->first);
357 assert(MDOC_ROOT != p->type);
358
359 switch (mdoc->next) {
360 case (MDOC_NEXT_SIBLING):
361 mdoc->last->next = p;
362 p->prev = mdoc->last;
363 p->parent = mdoc->last->parent;
364 break;
365 case (MDOC_NEXT_CHILD):
366 mdoc->last->child = p;
367 p->parent = mdoc->last;
368 break;
369 default:
370 abort();
371 /* NOTREACHED */
372 }
373
374 if ( ! mdoc_valid_pre(mdoc, p))
375 return(0);
376 if ( ! mdoc_action_pre(mdoc, p))
377 return(0);
378
379 switch (p->type) {
380 case (MDOC_HEAD):
381 assert(MDOC_BLOCK == p->parent->type);
382 p->parent->head = p;
383 break;
384 case (MDOC_TAIL):
385 assert(MDOC_BLOCK == p->parent->type);
386 p->parent->tail = p;
387 break;
388 case (MDOC_BODY):
389 assert(MDOC_BLOCK == p->parent->type);
390 p->parent->body = p;
391 break;
392 default:
393 break;
394 }
395
396 mdoc->last = p;
397
398 switch (p->type) {
399 case (MDOC_TEXT):
400 if ( ! mdoc_valid_post(mdoc))
401 return(0);
402 if ( ! mdoc_action_post(mdoc))
403 return(0);
404 break;
405 default:
406 break;
407 }
408
409 return(1);
410 }
411
412
413 static struct mdoc_node *
414 node_alloc(struct mdoc *mdoc, int line,
415 int pos, int tok, enum mdoc_type type)
416 {
417 struct mdoc_node *p;
418
419 if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) {
420 (void)verr(mdoc, EMALLOC);
421 return(NULL);
422 }
423
424 p->sec = mdoc->lastsec;
425 p->line = line;
426 p->pos = pos;
427 p->tok = tok;
428 if (MDOC_TEXT != (p->type = type))
429 assert(p->tok >= 0);
430
431 return(p);
432 }
433
434
435 int
436 mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
437 {
438 struct mdoc_node *p;
439
440 p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL);
441 if (NULL == p)
442 return(0);
443 return(node_append(mdoc, p));
444 }
445
446
447 int
448 mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
449 {
450 struct mdoc_node *p;
451
452 assert(mdoc->first);
453 assert(mdoc->last);
454
455 p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD);
456 if (NULL == p)
457 return(0);
458 return(node_append(mdoc, p));
459 }
460
461
462 int
463 mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
464 {
465 struct mdoc_node *p;
466
467 p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
468 if (NULL == p)
469 return(0);
470 return(node_append(mdoc, p));
471 }
472
473
474 int
475 mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
476 int tok, struct mdoc_arg *args)
477 {
478 struct mdoc_node *p;
479
480 p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK);
481 if (NULL == p)
482 return(0);
483 if ((p->args = args))
484 (args->refcnt)++;
485 return(node_append(mdoc, p));
486 }
487
488
489 int
490 mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
491 int tok, struct mdoc_arg *args)
492 {
493 struct mdoc_node *p;
494
495 p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM);
496 if (NULL == p)
497 return(0);
498 if ((p->args = args))
499 (args->refcnt)++;
500 return(node_append(mdoc, p));
501 }
502
503
504 int
505 mdoc_word_alloc(struct mdoc *mdoc,
506 int line, int pos, const char *word)
507 {
508 struct mdoc_node *p;
509
510 p = node_alloc(mdoc, line, pos, -1, MDOC_TEXT);
511 if (NULL == p)
512 return(0);
513 if (NULL == (p->string = strdup(word))) {
514 (void)verr(mdoc, EMALLOC);
515 return(0);
516 }
517 return(node_append(mdoc, p));
518 }
519
520
521 void
522 mdoc_node_free(struct mdoc_node *p)
523 {
524
525 if (p->string)
526 free(p->string);
527 if (p->args)
528 mdoc_argv_free(p->args);
529 free(p);
530 }
531
532
533 void
534 mdoc_node_freelist(struct mdoc_node *p)
535 {
536
537 if (p->child)
538 mdoc_node_freelist(p->child);
539 if (p->next)
540 mdoc_node_freelist(p->next);
541
542 mdoc_node_free(p);
543 }
544
545
546 /*
547 * Parse free-form text, that is, a line that does not begin with the
548 * control character.
549 */
550 static int
551 parsetext(struct mdoc *m, int line, char *buf)
552 {
553
554 if (SEC_PROLOGUE == m->lastnamed)
555 return(perr(m, line, 0, ETEXTPROL));
556
557 if (0 == buf[0] && ! (MDOC_LITERAL & m->flags))
558 return(perr(m, line, 0, ENOBLANK));
559
560 if ( ! mdoc_word_alloc(m, line, 0, buf))
561 return(0);
562
563 m->next = MDOC_NEXT_SIBLING;
564 return(1);
565 }
566
567
568 static int
569 macrowarn(struct mdoc *m, int ln, const char *buf)
570 {
571 if ( ! (MDOC_IGN_MACRO & m->pflags))
572 return(mdoc_perr(m, ln, 1,
573 "unknown macro: %s%s",
574 buf, strlen(buf) > 3 ? "..." : ""));
575 return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX,
576 "unknown macro: %s%s",
577 buf, strlen(buf) > 3 ? "..." : ""));
578 }
579
580
581
582 /*
583 * Parse a macro line, that is, a line beginning with the control
584 * character.
585 */
586 int
587 parsemacro(struct mdoc *m, int ln, char *buf)
588 {
589 int i, c;
590 char mac[5];
591
592 /* Comments and empties are quickly ignored. */
593
594 if (0 == buf[1])
595 return(1);
596
597 if (' ' == buf[1]) {
598 i = 2;
599 while (buf[i] && ' ' == buf[i])
600 i++;
601 if (0 == buf[i])
602 return(1);
603 return(perr(m, ln, 1, ESPACE));
604 }
605
606 if (buf[1] && '\\' == buf[1])
607 if (buf[2] && '\"' == buf[2])
608 return(1);
609
610 /* Copy the first word into a nil-terminated buffer. */
611
612 for (i = 1; i < 5; i++) {
613 if (0 == (mac[i - 1] = buf[i]))
614 break;
615 else if (' ' == buf[i])
616 break;
617 }
618
619 mac[i - 1] = 0;
620
621 if (i == 5 || i <= 2) {
622 if ( ! macrowarn(m, ln, mac))
623 goto err;
624 return(1);
625 }
626
627 if (MDOC_MAX == (c = mdoc_hash_find(m->htab, mac))) {
628 if ( ! macrowarn(m, ln, mac))
629 goto err;
630 return(1);
631 }
632
633 /* The macro is sane. Jump to the next word. */
634
635 while (buf[i] && ' ' == buf[i])
636 i++;
637
638 /* Begin recursive parse sequence. */
639
640 if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
641 goto err;
642
643 return(1);
644
645 err: /* Error out. */
646
647 m->flags |= MDOC_HALT;
648 return(0);
649 }