]> git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
Fixed lint invocation in Makefile.
[mandoc.git] / mdoc.c
1 /* $Id: mdoc.c,v 1.84 2009/06/17 07:59:47 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 const struct mdoc_node *
101 mdoc_node(const struct mdoc *m)
102 {
103
104 return(MDOC_HALT & m->flags ? NULL : m->first);
105 }
106
107
108 const struct mdoc_meta *
109 mdoc_meta(const struct mdoc *m)
110 {
111
112 return(MDOC_HALT & m->flags ? NULL : &m->meta);
113 }
114
115
116 static void
117 mdoc_free1(struct mdoc *mdoc)
118 {
119
120 if (mdoc->first)
121 mdoc_node_freelist(mdoc->first);
122 if (mdoc->meta.title)
123 free(mdoc->meta.title);
124 if (mdoc->meta.os)
125 free(mdoc->meta.os);
126 if (mdoc->meta.name)
127 free(mdoc->meta.name);
128 if (mdoc->meta.arch)
129 free(mdoc->meta.arch);
130 if (mdoc->meta.vol)
131 free(mdoc->meta.vol);
132 }
133
134
135 static int
136 mdoc_alloc1(struct mdoc *mdoc)
137 {
138
139 bzero(&mdoc->meta, sizeof(struct mdoc_meta));
140 mdoc->flags = 0;
141 mdoc->lastnamed = mdoc->lastsec = 0;
142 mdoc->last = calloc(1, sizeof(struct mdoc_node));
143 if (NULL == mdoc->last)
144 return(0);
145
146 mdoc->first = mdoc->last;
147 mdoc->last->type = MDOC_ROOT;
148 mdoc->next = MDOC_NEXT_CHILD;
149 return(1);
150 }
151
152
153 /*
154 * Free up all resources contributed by a parse: the node tree,
155 * meta-data and so on. Then reallocate the root node for another
156 * parse.
157 */
158 int
159 mdoc_reset(struct mdoc *mdoc)
160 {
161
162 mdoc_free1(mdoc);
163 return(mdoc_alloc1(mdoc));
164 }
165
166
167 /*
168 * Completely free up all resources.
169 */
170 void
171 mdoc_free(struct mdoc *mdoc)
172 {
173
174 mdoc_free1(mdoc);
175 if (mdoc->htab)
176 mdoc_hash_free(mdoc->htab);
177 free(mdoc);
178 }
179
180
181 struct mdoc *
182 mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
183 {
184 struct mdoc *p;
185
186 if (NULL == (p = calloc(1, sizeof(struct mdoc))))
187 return(NULL);
188 if (cb)
189 (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
190
191 p->data = data;
192 p->pflags = pflags;
193
194 if (NULL == (p->htab = mdoc_hash_alloc())) {
195 free(p);
196 return(NULL);
197 } else if (mdoc_alloc1(p))
198 return(p);
199
200 free(p);
201 return(NULL);
202 }
203
204
205 /*
206 * Climb back up the parse tree, validating open scopes. Mostly calls
207 * through to macro_end in macro.c.
208 */
209 int
210 mdoc_endparse(struct mdoc *m)
211 {
212
213 if (MDOC_HALT & m->flags)
214 return(0);
215 else if (mdoc_macroend(m))
216 return(1);
217 m->flags |= MDOC_HALT;
218 return(0);
219 }
220
221
222 /*
223 * Main parse routine. Parses a single line -- really just hands off to
224 * the macro or text parser.
225 */
226 int
227 mdoc_parseln(struct mdoc *m, int ln, char *buf)
228 {
229
230 if (MDOC_HALT & m->flags)
231 return(0);
232
233 return('.' == *buf ? parsemacro(m, ln, buf) :
234 parsetext(m, ln, buf));
235 }
236
237
238 int
239 mdoc_verr(struct mdoc *mdoc, int ln, int pos,
240 const char *fmt, ...)
241 {
242 char buf[256];
243 va_list ap;
244
245 if (NULL == mdoc->cb.mdoc_err)
246 return(0);
247
248 va_start(ap, fmt);
249 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
250 va_end(ap);
251 return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
252 }
253
254
255 int
256 mdoc_vwarn(struct mdoc *mdoc, int ln, int pos,
257 enum mdoc_warn type, const char *fmt, ...)
258 {
259 char buf[256];
260 va_list ap;
261
262 if (NULL == mdoc->cb.mdoc_warn)
263 return(0);
264
265 va_start(ap, fmt);
266 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
267 va_end(ap);
268 return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, buf));
269 }
270
271
272 int
273 mdoc_nerr(struct mdoc *mdoc, const struct mdoc_node *node,
274 const char *fmt, ...)
275 {
276 char buf[256];
277 va_list ap;
278
279 if (NULL == mdoc->cb.mdoc_err)
280 return(0);
281
282 va_start(ap, fmt);
283 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
284 va_end(ap);
285 return((*mdoc->cb.mdoc_err)(mdoc->data,
286 node->line, node->pos, buf));
287 }
288
289
290 int
291 mdoc_warn(struct mdoc *mdoc, enum mdoc_warn type,
292 const char *fmt, ...)
293 {
294 char buf[256];
295 va_list ap;
296
297 if (NULL == mdoc->cb.mdoc_warn)
298 return(0);
299
300 va_start(ap, fmt);
301 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
302 va_end(ap);
303 return((*mdoc->cb.mdoc_warn)(mdoc->data, mdoc->last->line,
304 mdoc->last->pos, type, buf));
305 }
306
307
308 int
309 mdoc_err(struct mdoc *mdoc, const char *fmt, ...)
310 {
311 char buf[256];
312 va_list ap;
313
314 if (NULL == mdoc->cb.mdoc_err)
315 return(0);
316
317 va_start(ap, fmt);
318 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
319 va_end(ap);
320 return((*mdoc->cb.mdoc_err)(mdoc->data, mdoc->last->line,
321 mdoc->last->pos, buf));
322 }
323
324
325 int
326 mdoc_pwarn(struct mdoc *mdoc, int line, int pos, enum mdoc_warn type,
327 const char *fmt, ...)
328 {
329 char buf[256];
330 va_list ap;
331
332 if (NULL == mdoc->cb.mdoc_warn)
333 return(0);
334
335 va_start(ap, fmt);
336 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
337 va_end(ap);
338 return((*mdoc->cb.mdoc_warn)(mdoc->data,
339 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)perr(mdoc, (mdoc)->last->line,
484 (mdoc)->last->pos, EMALLOC);
485 return(NULL);
486 }
487
488 p->sec = mdoc->lastsec;
489 p->line = line;
490 p->pos = pos;
491 p->tok = tok;
492 if (MDOC_TEXT != (p->type = type))
493 assert(p->tok >= 0);
494
495 return(p);
496 }
497
498
499 int
500 mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
501 {
502 struct mdoc_node *p;
503
504 p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL);
505 if (NULL == p)
506 return(0);
507 return(node_append(mdoc, p));
508 }
509
510
511 int
512 mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
513 {
514 struct mdoc_node *p;
515
516 assert(mdoc->first);
517 assert(mdoc->last);
518
519 p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD);
520 if (NULL == p)
521 return(0);
522 return(node_append(mdoc, p));
523 }
524
525
526 int
527 mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
528 {
529 struct mdoc_node *p;
530
531 p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
532 if (NULL == p)
533 return(0);
534 return(node_append(mdoc, p));
535 }
536
537
538 int
539 mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
540 int tok, struct mdoc_arg *args)
541 {
542 struct mdoc_node *p;
543
544 p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK);
545 if (NULL == p)
546 return(0);
547 p->args = args;
548 if (p->args)
549 (args->refcnt)++;
550 return(node_append(mdoc, p));
551 }
552
553
554 int
555 mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
556 int tok, struct mdoc_arg *args)
557 {
558 struct mdoc_node *p;
559
560 p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM);
561 if (NULL == p)
562 return(0);
563 p->args = args;
564 if (p->args)
565 (args->refcnt)++;
566 return(node_append(mdoc, p));
567 }
568
569
570 int
571 mdoc_word_alloc(struct mdoc *mdoc,
572 int line, int pos, const char *word)
573 {
574 struct mdoc_node *p;
575
576 p = node_alloc(mdoc, line, pos, -1, MDOC_TEXT);
577 if (NULL == p)
578 return(0);
579 if (NULL == (p->string = strdup(word))) {
580 (void)perr(mdoc, (mdoc)->last->line,
581 (mdoc)->last->pos, EMALLOC);
582 return(0);
583 }
584 return(node_append(mdoc, p));
585 }
586
587
588 void
589 mdoc_node_free(struct mdoc_node *p)
590 {
591
592 if (p->string)
593 free(p->string);
594 if (p->args)
595 mdoc_argv_free(p->args);
596 free(p);
597 }
598
599
600 void
601 mdoc_node_freelist(struct mdoc_node *p)
602 {
603
604 if (p->child)
605 mdoc_node_freelist(p->child);
606 if (p->next)
607 mdoc_node_freelist(p->next);
608
609 mdoc_node_free(p);
610 }
611
612
613 /*
614 * Parse free-form text, that is, a line that does not begin with the
615 * control character.
616 */
617 static int
618 parsetext(struct mdoc *m, int line, char *buf)
619 {
620
621 if (SEC_PROLOGUE == m->lastnamed)
622 return(perr(m, line, 0, ETEXTPROL));
623
624 if (0 == buf[0] && ! (MDOC_LITERAL & m->flags))
625 return(perr(m, line, 0, ENOBLANK));
626
627 if ( ! mdoc_word_alloc(m, line, 0, buf))
628 return(0);
629
630 m->next = MDOC_NEXT_SIBLING;
631 return(1);
632 }
633
634
635 static int
636 macrowarn(struct mdoc *m, int ln, const char *buf)
637 {
638 if ( ! (MDOC_IGN_MACRO & m->pflags))
639 return(mdoc_perr(m, ln, 1,
640 "unknown macro: %s%s",
641 buf, strlen(buf) > 3 ? "..." : ""));
642 return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX,
643 "unknown macro: %s%s",
644 buf, strlen(buf) > 3 ? "..." : ""));
645 }
646
647
648 /*
649 * Parse a macro line, that is, a line beginning with the control
650 * character.
651 */
652 int
653 parsemacro(struct mdoc *m, int ln, char *buf)
654 {
655 int i, c;
656 char mac[5];
657
658 /* Empty lines are ignored. */
659
660 if (0 == buf[1])
661 return(1);
662
663 if (' ' == buf[1]) {
664 i = 2;
665 while (buf[i] && ' ' == buf[i])
666 i++;
667 if (0 == buf[i])
668 return(1);
669 return(perr(m, ln, 1, ESPACE));
670 }
671
672 /* Copy the first word into a nil-terminated buffer. */
673
674 for (i = 1; i < 5; i++) {
675 if (0 == (mac[i - 1] = buf[i]))
676 break;
677 else if (' ' == buf[i])
678 break;
679 }
680
681 mac[i - 1] = 0;
682
683 if (i == 5 || i <= 2) {
684 if ( ! macrowarn(m, ln, mac))
685 goto err;
686 return(1);
687 }
688
689 if (MDOC_MAX == (c = mdoc_hash_find(m->htab, mac))) {
690 if ( ! macrowarn(m, ln, mac))
691 goto err;
692 return(1);
693 }
694
695 /* The macro is sane. Jump to the next word. */
696
697 while (buf[i] && ' ' == buf[i])
698 i++;
699
700 /* Begin recursive parse sequence. */
701
702 if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
703 goto err;
704
705 return(1);
706
707 err: /* Error out. */
708
709 m->flags |= MDOC_HALT;
710 return(0);
711 }