]> git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
Clean-up: in-source documentation.
[mandoc.git] / mdoc.c
1 /* $Id: mdoc.c,v 1.83 2009/06/16 20:22:23 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 "Ap", "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", "Lp",
67 "Lk", "Mt", "Brq", "Bro",
68 /* LINTED */
69 "Brc", "\%C", "Es", "En",
70 /* LINTED */
71 "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 const struct mdoc_node *
103 mdoc_node(const struct mdoc *m)
104 {
105
106 return(MDOC_HALT & m->flags ? NULL : m->first);
107 }
108
109
110 const struct mdoc_meta *
111 mdoc_meta(const struct mdoc *m)
112 {
113
114 return(MDOC_HALT & m->flags ? NULL : &m->meta);
115 }
116
117
118 static void
119 mdoc_free1(struct mdoc *mdoc)
120 {
121
122 if (mdoc->first)
123 mdoc_node_freelist(mdoc->first);
124 if (mdoc->meta.title)
125 free(mdoc->meta.title);
126 if (mdoc->meta.os)
127 free(mdoc->meta.os);
128 if (mdoc->meta.name)
129 free(mdoc->meta.name);
130 if (mdoc->meta.arch)
131 free(mdoc->meta.arch);
132 if (mdoc->meta.vol)
133 free(mdoc->meta.vol);
134 }
135
136
137 static int
138 mdoc_alloc1(struct mdoc *mdoc)
139 {
140
141 bzero(&mdoc->meta, sizeof(struct mdoc_meta));
142 mdoc->flags = 0;
143 mdoc->lastnamed = mdoc->lastsec = 0;
144 mdoc->last = calloc(1, sizeof(struct mdoc_node));
145 if (NULL == mdoc->last)
146 return(0);
147
148 mdoc->first = mdoc->last;
149 mdoc->last->type = MDOC_ROOT;
150 mdoc->next = MDOC_NEXT_CHILD;
151 return(1);
152 }
153
154
155 /*
156 * Free up all resources contributed by a parse: the node tree,
157 * meta-data and so on. Then reallocate the root node for another
158 * parse.
159 */
160 int
161 mdoc_reset(struct mdoc *mdoc)
162 {
163
164 mdoc_free1(mdoc);
165 return(mdoc_alloc1(mdoc));
166 }
167
168
169 /*
170 * Completely free up all resources.
171 */
172 void
173 mdoc_free(struct mdoc *mdoc)
174 {
175
176 mdoc_free1(mdoc);
177 if (mdoc->htab)
178 mdoc_hash_free(mdoc->htab);
179 free(mdoc);
180 }
181
182
183 struct mdoc *
184 mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
185 {
186 struct mdoc *p;
187
188 if (NULL == (p = calloc(1, sizeof(struct mdoc))))
189 return(NULL);
190 if (cb)
191 (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
192
193 p->data = data;
194 p->pflags = pflags;
195
196 if (NULL == (p->htab = mdoc_hash_alloc())) {
197 free(p);
198 return(NULL);
199 } else if (mdoc_alloc1(p))
200 return(p);
201
202 free(p);
203 return(NULL);
204 }
205
206
207 /*
208 * Climb back up the parse tree, validating open scopes. Mostly calls
209 * through to macro_end in macro.c.
210 */
211 int
212 mdoc_endparse(struct mdoc *m)
213 {
214
215 if (MDOC_HALT & m->flags)
216 return(0);
217 else if (mdoc_macroend(m))
218 return(1);
219 m->flags |= MDOC_HALT;
220 return(0);
221 }
222
223
224 /*
225 * Main parse routine. Parses a single line -- really just hands off to
226 * the macro or text parser.
227 */
228 int
229 mdoc_parseln(struct mdoc *m, int ln, char *buf)
230 {
231
232 /* If in error-mode, then we parse no more. */
233
234 if (MDOC_HALT & m->flags)
235 return(0);
236
237 return('.' == *buf ? parsemacro(m, ln, buf) :
238 parsetext(m, ln, buf));
239 }
240
241
242 int
243 mdoc_verr(struct mdoc *mdoc, int ln, int pos,
244 const char *fmt, ...)
245 {
246 char buf[256];
247 va_list ap;
248
249 if (NULL == mdoc->cb.mdoc_err)
250 return(0);
251
252 va_start(ap, fmt);
253 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
254 va_end(ap);
255 return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
256 }
257
258
259 int
260 mdoc_vwarn(struct mdoc *mdoc, int ln, int pos,
261 enum mdoc_warn type, const char *fmt, ...)
262 {
263 char buf[256];
264 va_list ap;
265
266 if (NULL == mdoc->cb.mdoc_warn)
267 return(0);
268
269 va_start(ap, fmt);
270 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
271 va_end(ap);
272 return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, buf));
273 }
274
275
276 int
277 mdoc_nerr(struct mdoc *mdoc, const struct mdoc_node *node, const char *fmt, ...)
278 {
279 char buf[256];
280 va_list ap;
281
282 if (NULL == mdoc->cb.mdoc_err)
283 return(0);
284
285 va_start(ap, fmt);
286 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
287 va_end(ap);
288 return((*mdoc->cb.mdoc_err)(mdoc->data, node->line, node->pos, buf));
289 }
290
291
292 int
293 mdoc_warn(struct mdoc *mdoc, enum mdoc_warn type, const char *fmt, ...)
294 {
295 char buf[256];
296 va_list ap;
297
298 if (NULL == mdoc->cb.mdoc_warn)
299 return(0);
300
301 va_start(ap, fmt);
302 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
303 va_end(ap);
304 return((*mdoc->cb.mdoc_warn)(mdoc->data, mdoc->last->line,
305 mdoc->last->pos, type, buf));
306 }
307
308
309 int
310 mdoc_err(struct mdoc *mdoc, const char *fmt, ...)
311 {
312 char buf[256];
313 va_list ap;
314
315 if (NULL == mdoc->cb.mdoc_err)
316 return(0);
317
318 va_start(ap, fmt);
319 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
320 va_end(ap);
321 return((*mdoc->cb.mdoc_err)(mdoc->data, mdoc->last->line,
322 mdoc->last->pos, buf));
323 }
324
325
326 int
327 mdoc_pwarn(struct mdoc *mdoc, int line, int pos, enum mdoc_warn type,
328 const char *fmt, ...)
329 {
330 char buf[256];
331 va_list ap;
332
333 if (NULL == mdoc->cb.mdoc_warn)
334 return(0);
335
336 va_start(ap, fmt);
337 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
338 va_end(ap);
339 return((*mdoc->cb.mdoc_warn)(mdoc->data, line, pos, type, buf));
340 }
341
342 int
343 mdoc_perr(struct mdoc *mdoc, int line, int pos, const char *fmt, ...)
344 {
345 char buf[256];
346 va_list ap;
347
348 if (NULL == mdoc->cb.mdoc_err)
349 return(0);
350
351 va_start(ap, fmt);
352 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
353 va_end(ap);
354 return((*mdoc->cb.mdoc_err)(mdoc->data, line, pos, buf));
355 }
356
357
358 int
359 mdoc_macro(struct mdoc *m, int tok,
360 int ln, int pp, int *pos, char *buf)
361 {
362
363 /* FIXME - these should happen during validation. */
364
365 if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
366 SEC_PROLOGUE != m->lastnamed)
367 return(perr(m, ln, pp, EPROLBODY));
368
369 if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
370 SEC_PROLOGUE == m->lastnamed)
371 return(perr(m, ln, pp, EBODYPROL));
372
373 if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
374 return(perr(m, ln, pp, ENOCALL));
375
376 return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
377 }
378
379
380 static int
381 perr(struct mdoc *m, int line, int pos, enum merr type)
382 {
383 char *p;
384
385 p = NULL;
386 switch (type) {
387 case (ENOCALL):
388 p = "not callable";
389 break;
390 case (EPROLBODY):
391 p = "macro disallowed in document body";
392 break;
393 case (EBODYPROL):
394 p = "macro disallowed in document prologue";
395 break;
396 case (EMALLOC):
397 p = "memory exhausted";
398 break;
399 case (ETEXTPROL):
400 p = "text disallowed in document prologue";
401 break;
402 case (ENOBLANK):
403 p = "blank lines disallowed in non-literal contexts";
404 break;
405 case (ESPACE):
406 p = "whitespace disallowed after delimiter";
407 break;
408 }
409 assert(p);
410 return(mdoc_perr(m, line, pos, p));
411 }
412
413
414 static int
415 node_append(struct mdoc *mdoc, struct mdoc_node *p)
416 {
417
418 assert(mdoc->last);
419 assert(mdoc->first);
420 assert(MDOC_ROOT != p->type);
421
422 switch (mdoc->next) {
423 case (MDOC_NEXT_SIBLING):
424 mdoc->last->next = p;
425 p->prev = mdoc->last;
426 p->parent = mdoc->last->parent;
427 break;
428 case (MDOC_NEXT_CHILD):
429 mdoc->last->child = p;
430 p->parent = mdoc->last;
431 break;
432 default:
433 abort();
434 /* NOTREACHED */
435 }
436
437 if ( ! mdoc_valid_pre(mdoc, p))
438 return(0);
439 if ( ! mdoc_action_pre(mdoc, p))
440 return(0);
441
442 switch (p->type) {
443 case (MDOC_HEAD):
444 assert(MDOC_BLOCK == p->parent->type);
445 p->parent->head = p;
446 break;
447 case (MDOC_TAIL):
448 assert(MDOC_BLOCK == p->parent->type);
449 p->parent->tail = p;
450 break;
451 case (MDOC_BODY):
452 assert(MDOC_BLOCK == p->parent->type);
453 p->parent->body = p;
454 break;
455 default:
456 break;
457 }
458
459 mdoc->last = p;
460
461 switch (p->type) {
462 case (MDOC_TEXT):
463 if ( ! mdoc_valid_post(mdoc))
464 return(0);
465 if ( ! mdoc_action_post(mdoc))
466 return(0);
467 break;
468 default:
469 break;
470 }
471
472 return(1);
473 }
474
475
476 static struct mdoc_node *
477 node_alloc(struct mdoc *mdoc, int line,
478 int pos, int tok, enum mdoc_type type)
479 {
480 struct mdoc_node *p;
481
482 if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) {
483 (void)verr(mdoc, EMALLOC);
484 return(NULL);
485 }
486
487 p->sec = mdoc->lastsec;
488 p->line = line;
489 p->pos = pos;
490 p->tok = tok;
491 if (MDOC_TEXT != (p->type = type))
492 assert(p->tok >= 0);
493
494 return(p);
495 }
496
497
498 int
499 mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
500 {
501 struct mdoc_node *p;
502
503 p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL);
504 if (NULL == p)
505 return(0);
506 return(node_append(mdoc, p));
507 }
508
509
510 int
511 mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
512 {
513 struct mdoc_node *p;
514
515 assert(mdoc->first);
516 assert(mdoc->last);
517
518 p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD);
519 if (NULL == p)
520 return(0);
521 return(node_append(mdoc, p));
522 }
523
524
525 int
526 mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
527 {
528 struct mdoc_node *p;
529
530 p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
531 if (NULL == p)
532 return(0);
533 return(node_append(mdoc, p));
534 }
535
536
537 int
538 mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
539 int tok, struct mdoc_arg *args)
540 {
541 struct mdoc_node *p;
542
543 p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK);
544 if (NULL == p)
545 return(0);
546 p->args = args;
547 if (p->args)
548 (args->refcnt)++;
549 return(node_append(mdoc, p));
550 }
551
552
553 int
554 mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
555 int tok, struct mdoc_arg *args)
556 {
557 struct mdoc_node *p;
558
559 p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM);
560 if (NULL == p)
561 return(0);
562 p->args = args;
563 if (p->args)
564 (args->refcnt)++;
565 return(node_append(mdoc, p));
566 }
567
568
569 int
570 mdoc_word_alloc(struct mdoc *mdoc,
571 int line, int pos, const char *word)
572 {
573 struct mdoc_node *p;
574
575 p = node_alloc(mdoc, line, pos, -1, MDOC_TEXT);
576 if (NULL == p)
577 return(0);
578 if (NULL == (p->string = strdup(word))) {
579 (void)verr(mdoc, EMALLOC);
580 return(0);
581 }
582 return(node_append(mdoc, p));
583 }
584
585
586 void
587 mdoc_node_free(struct mdoc_node *p)
588 {
589
590 if (p->string)
591 free(p->string);
592 if (p->args)
593 mdoc_argv_free(p->args);
594 free(p);
595 }
596
597
598 void
599 mdoc_node_freelist(struct mdoc_node *p)
600 {
601
602 if (p->child)
603 mdoc_node_freelist(p->child);
604 if (p->next)
605 mdoc_node_freelist(p->next);
606
607 mdoc_node_free(p);
608 }
609
610
611 /*
612 * Parse free-form text, that is, a line that does not begin with the
613 * control character.
614 */
615 static int
616 parsetext(struct mdoc *m, int line, char *buf)
617 {
618
619 if (SEC_PROLOGUE == m->lastnamed)
620 return(perr(m, line, 0, ETEXTPROL));
621
622 if (0 == buf[0] && ! (MDOC_LITERAL & m->flags))
623 return(perr(m, line, 0, ENOBLANK));
624
625 if ( ! mdoc_word_alloc(m, line, 0, buf))
626 return(0);
627
628 m->next = MDOC_NEXT_SIBLING;
629 return(1);
630 }
631
632
633 static int
634 macrowarn(struct mdoc *m, int ln, const char *buf)
635 {
636 if ( ! (MDOC_IGN_MACRO & m->pflags))
637 return(mdoc_perr(m, ln, 1,
638 "unknown macro: %s%s",
639 buf, strlen(buf) > 3 ? "..." : ""));
640 return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX,
641 "unknown macro: %s%s",
642 buf, strlen(buf) > 3 ? "..." : ""));
643 }
644
645
646 /*
647 * Parse a macro line, that is, a line beginning with the control
648 * character.
649 */
650 int
651 parsemacro(struct mdoc *m, int ln, char *buf)
652 {
653 int i, c;
654 char mac[5];
655
656 /* Empty lines are ignored. */
657
658 if (0 == buf[1])
659 return(1);
660
661 if (' ' == buf[1]) {
662 i = 2;
663 while (buf[i] && ' ' == buf[i])
664 i++;
665 if (0 == buf[i])
666 return(1);
667 return(perr(m, ln, 1, ESPACE));
668 }
669
670 /* Copy the first word into a nil-terminated buffer. */
671
672 for (i = 1; i < 5; i++) {
673 if (0 == (mac[i - 1] = buf[i]))
674 break;
675 else if (' ' == buf[i])
676 break;
677 }
678
679 mac[i - 1] = 0;
680
681 if (i == 5 || i <= 2) {
682 if ( ! macrowarn(m, ln, mac))
683 goto err;
684 return(1);
685 }
686
687 if (MDOC_MAX == (c = mdoc_hash_find(m->htab, mac))) {
688 if ( ! macrowarn(m, ln, mac))
689 goto err;
690 return(1);
691 }
692
693 /* The macro is sane. Jump to the next word. */
694
695 while (buf[i] && ' ' == buf[i])
696 i++;
697
698 /* Begin recursive parse sequence. */
699
700 if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
701 goto err;
702
703 return(1);
704
705 err: /* Error out. */
706
707 m->flags |= MDOC_HALT;
708 return(0);
709 }