]> git.cameronkatri.com Git - mandoc.git/blob - man.c
Use tbl_span line number for warnings/errors.
[mandoc.git] / man.c
1 /* $Id: man.c,v 1.99 2011/02/06 22:05:20 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "mandoc.h"
30 #include "libman.h"
31 #include "libmandoc.h"
32
33 const char *const __man_macronames[MAN_MAX] = {
34 "br", "TH", "SH", "SS",
35 "TP", "LP", "PP", "P",
36 "IP", "HP", "SM", "SB",
37 "BI", "IB", "BR", "RB",
38 "R", "B", "I", "IR",
39 "RI", "na", "sp", "nf",
40 "fi", "RE", "RS", "DT",
41 "UC", "PD", "AT", "in",
42 "ft"
43 };
44
45 const char * const *man_macronames = __man_macronames;
46
47 static struct man_node *man_node_alloc(struct man *, int, int,
48 enum man_type, enum mant);
49 static int man_node_append(struct man *,
50 struct man_node *);
51 static int man_span_alloc(struct man *,
52 const struct tbl_span *);
53 static void man_node_free(struct man_node *);
54 static void man_node_unlink(struct man *,
55 struct man_node *);
56 static int man_ptext(struct man *, int, char *, int);
57 static int man_pmacro(struct man *, int, char *, int);
58 static void man_free1(struct man *);
59 static void man_alloc1(struct man *);
60 static int man_descope(struct man *, int, int);
61
62
63 const struct man_node *
64 man_node(const struct man *m)
65 {
66
67 assert( ! (MAN_HALT & m->flags));
68 return(m->first);
69 }
70
71
72 const struct man_meta *
73 man_meta(const struct man *m)
74 {
75
76 assert( ! (MAN_HALT & m->flags));
77 return(&m->meta);
78 }
79
80
81 void
82 man_reset(struct man *man)
83 {
84
85 man_free1(man);
86 man_alloc1(man);
87 }
88
89
90 void
91 man_free(struct man *man)
92 {
93
94 man_free1(man);
95 free(man);
96 }
97
98
99 struct man *
100 man_alloc(struct regset *regs, void *data, mandocmsg msg)
101 {
102 struct man *p;
103
104 p = mandoc_calloc(1, sizeof(struct man));
105
106 man_hash_init();
107 p->data = data;
108 p->msg = msg;
109 p->regs = regs;
110
111 man_alloc1(p);
112 return(p);
113 }
114
115
116 int
117 man_endparse(struct man *m)
118 {
119
120 assert( ! (MAN_HALT & m->flags));
121 if (man_macroend(m))
122 return(1);
123 m->flags |= MAN_HALT;
124 return(0);
125 }
126
127
128 int
129 man_parseln(struct man *m, int ln, char *buf, int offs)
130 {
131
132 m->flags |= MAN_NEWLINE;
133
134 assert( ! (MAN_HALT & m->flags));
135 return(('.' == buf[offs] || '\'' == buf[offs]) ?
136 man_pmacro(m, ln, buf, offs) :
137 man_ptext(m, ln, buf, offs));
138 }
139
140
141 static void
142 man_free1(struct man *man)
143 {
144
145 if (man->first)
146 man_node_delete(man, man->first);
147 if (man->meta.title)
148 free(man->meta.title);
149 if (man->meta.source)
150 free(man->meta.source);
151 if (man->meta.rawdate)
152 free(man->meta.rawdate);
153 if (man->meta.vol)
154 free(man->meta.vol);
155 if (man->meta.msec)
156 free(man->meta.msec);
157 }
158
159
160 static void
161 man_alloc1(struct man *m)
162 {
163
164 memset(&m->meta, 0, sizeof(struct man_meta));
165 m->flags = 0;
166 m->last = mandoc_calloc(1, sizeof(struct man_node));
167 m->first = m->last;
168 m->last->type = MAN_ROOT;
169 m->last->tok = MAN_MAX;
170 m->next = MAN_NEXT_CHILD;
171 }
172
173
174 static int
175 man_node_append(struct man *man, struct man_node *p)
176 {
177
178 assert(man->last);
179 assert(man->first);
180 assert(MAN_ROOT != p->type);
181
182 switch (man->next) {
183 case (MAN_NEXT_SIBLING):
184 man->last->next = p;
185 p->prev = man->last;
186 p->parent = man->last->parent;
187 break;
188 case (MAN_NEXT_CHILD):
189 man->last->child = p;
190 p->parent = man->last;
191 break;
192 default:
193 abort();
194 /* NOTREACHED */
195 }
196
197 assert(p->parent);
198 p->parent->nchild++;
199
200 if ( ! man_valid_pre(man, p))
201 return(0);
202
203 switch (p->type) {
204 case (MAN_HEAD):
205 assert(MAN_BLOCK == p->parent->type);
206 p->parent->head = p;
207 break;
208 case (MAN_BODY):
209 assert(MAN_BLOCK == p->parent->type);
210 p->parent->body = p;
211 break;
212 default:
213 break;
214 }
215
216 man->last = p;
217
218 switch (p->type) {
219 case (MAN_TBL):
220 /* FALLTHROUGH */
221 case (MAN_TEXT):
222 if ( ! man_valid_post(man))
223 return(0);
224 break;
225 default:
226 break;
227 }
228
229 return(1);
230 }
231
232
233 static struct man_node *
234 man_node_alloc(struct man *m, int line, int pos,
235 enum man_type type, enum mant tok)
236 {
237 struct man_node *p;
238
239 p = mandoc_calloc(1, sizeof(struct man_node));
240 p->line = line;
241 p->pos = pos;
242 p->type = type;
243 p->tok = tok;
244
245 if (MAN_NEWLINE & m->flags)
246 p->flags |= MAN_LINE;
247 m->flags &= ~MAN_NEWLINE;
248 return(p);
249 }
250
251
252 int
253 man_elem_alloc(struct man *m, int line, int pos, enum mant tok)
254 {
255 struct man_node *p;
256
257 p = man_node_alloc(m, line, pos, MAN_ELEM, tok);
258 if ( ! man_node_append(m, p))
259 return(0);
260 m->next = MAN_NEXT_CHILD;
261 return(1);
262 }
263
264
265 int
266 man_head_alloc(struct man *m, int line, int pos, enum mant tok)
267 {
268 struct man_node *p;
269
270 p = man_node_alloc(m, line, pos, MAN_HEAD, tok);
271 if ( ! man_node_append(m, p))
272 return(0);
273 m->next = MAN_NEXT_CHILD;
274 return(1);
275 }
276
277
278 int
279 man_body_alloc(struct man *m, int line, int pos, enum mant tok)
280 {
281 struct man_node *p;
282
283 p = man_node_alloc(m, line, pos, MAN_BODY, tok);
284 if ( ! man_node_append(m, p))
285 return(0);
286 m->next = MAN_NEXT_CHILD;
287 return(1);
288 }
289
290
291 int
292 man_block_alloc(struct man *m, int line, int pos, enum mant tok)
293 {
294 struct man_node *p;
295
296 p = man_node_alloc(m, line, pos, MAN_BLOCK, tok);
297 if ( ! man_node_append(m, p))
298 return(0);
299 m->next = MAN_NEXT_CHILD;
300 return(1);
301 }
302
303 static int
304 man_span_alloc(struct man *m, const struct tbl_span *span)
305 {
306 struct man_node *n;
307
308 n = man_node_alloc(m, span->line, 0, MAN_TBL, MAN_MAX);
309 n->span = span;
310
311 if ( ! man_node_append(m, n))
312 return(0);
313
314 m->next = MAN_NEXT_SIBLING;
315 return(1);
316 }
317
318 int
319 man_word_alloc(struct man *m, int line, int pos, const char *word)
320 {
321 struct man_node *n;
322 size_t sv, len;
323
324 len = strlen(word);
325
326 n = man_node_alloc(m, line, pos, MAN_TEXT, MAN_MAX);
327 n->string = mandoc_malloc(len + 1);
328 sv = strlcpy(n->string, word, len + 1);
329
330 /* Prohibit truncation. */
331 assert(sv < len + 1);
332
333 if ( ! man_node_append(m, n))
334 return(0);
335
336 m->next = MAN_NEXT_SIBLING;
337 return(1);
338 }
339
340
341 /*
342 * Free all of the resources held by a node. This does NOT unlink a
343 * node from its context; for that, see man_node_unlink().
344 */
345 static void
346 man_node_free(struct man_node *p)
347 {
348
349 if (p->string)
350 free(p->string);
351 free(p);
352 }
353
354
355 void
356 man_node_delete(struct man *m, struct man_node *p)
357 {
358
359 while (p->child)
360 man_node_delete(m, p->child);
361
362 man_node_unlink(m, p);
363 man_node_free(p);
364 }
365
366
367 int
368 man_addspan(struct man *m, const struct tbl_span *sp)
369 {
370
371 assert( ! (MAN_HALT & m->flags));
372 if ( ! man_span_alloc(m, sp))
373 return(0);
374 return(man_descope(m, sp->line, 0));
375 }
376
377 static int
378 man_descope(struct man *m, int line, int offs)
379 {
380 /*
381 * Co-ordinate what happens with having a next-line scope open:
382 * first close out the element scope (if applicable), then close
383 * out the block scope (also if applicable).
384 */
385
386 if (MAN_ELINE & m->flags) {
387 m->flags &= ~MAN_ELINE;
388 if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
389 return(0);
390 }
391
392 if ( ! (MAN_BLINE & m->flags))
393 return(1);
394 m->flags &= ~MAN_BLINE;
395
396 if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
397 return(0);
398 return(man_body_alloc(m, line, offs, m->last->tok));
399 }
400
401
402 static int
403 man_ptext(struct man *m, int line, char *buf, int offs)
404 {
405 int i;
406
407 /* Ignore bogus comments. */
408
409 if ('\\' == buf[offs] &&
410 '.' == buf[offs + 1] &&
411 '"' == buf[offs + 2]) {
412 man_pmsg(m, line, offs, MANDOCERR_BADCOMMENT);
413 return(1);
414 }
415
416 /* Literal free-form text whitespace is preserved. */
417
418 if (MAN_LITERAL & m->flags) {
419 if ( ! man_word_alloc(m, line, offs, buf + offs))
420 return(0);
421 return(man_descope(m, line, offs));
422 }
423
424 /* Pump blank lines directly into the backend. */
425
426 for (i = offs; ' ' == buf[i]; i++)
427 /* Skip leading whitespace. */ ;
428
429 if ('\0' == buf[i]) {
430 /* Allocate a blank entry. */
431 if ( ! man_word_alloc(m, line, offs, ""))
432 return(0);
433 return(man_descope(m, line, offs));
434 }
435
436 /*
437 * Warn if the last un-escaped character is whitespace. Then
438 * strip away the remaining spaces (tabs stay!).
439 */
440
441 i = (int)strlen(buf);
442 assert(i);
443
444 if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
445 if (i > 1 && '\\' != buf[i - 2])
446 man_pmsg(m, line, i - 1, MANDOCERR_EOLNSPACE);
447
448 for (--i; i && ' ' == buf[i]; i--)
449 /* Spin back to non-space. */ ;
450
451 /* Jump ahead of escaped whitespace. */
452 i += '\\' == buf[i] ? 2 : 1;
453
454 buf[i] = '\0';
455 }
456
457 if ( ! man_word_alloc(m, line, offs, buf + offs))
458 return(0);
459
460 /*
461 * End-of-sentence check. If the last character is an unescaped
462 * EOS character, then flag the node as being the end of a
463 * sentence. The front-end will know how to interpret this.
464 */
465
466 assert(i);
467 if (mandoc_eos(buf, (size_t)i, 0))
468 m->last->flags |= MAN_EOS;
469
470 return(man_descope(m, line, offs));
471 }
472
473
474 static int
475 man_pmacro(struct man *m, int ln, char *buf, int offs)
476 {
477 int i, j, ppos;
478 enum mant tok;
479 char mac[5];
480 struct man_node *n;
481
482 /* Comments and empties are quickly ignored. */
483
484 offs++;
485
486 if ('\0' == buf[offs])
487 return(1);
488
489 i = offs;
490
491 /*
492 * Skip whitespace between the control character and initial
493 * text. "Whitespace" is both spaces and tabs.
494 */
495
496 if (' ' == buf[i] || '\t' == buf[i]) {
497 i++;
498 while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
499 i++;
500 if ('\0' == buf[i])
501 goto out;
502 }
503
504 ppos = i;
505
506 /*
507 * Copy the first word into a nil-terminated buffer.
508 * Stop copying when a tab, space, or eoln is encountered.
509 */
510
511 j = 0;
512 while (j < 4 && '\0' != buf[i] && ' ' != buf[i] && '\t' != buf[i])
513 mac[j++] = buf[i++];
514 mac[j] = '\0';
515
516 tok = (j > 0 && j < 4) ? man_hash_find(mac) : MAN_MAX;
517 if (MAN_MAX == tok) {
518 man_vmsg(m, MANDOCERR_MACRO, ln, ppos, "%s", buf + ppos - 1);
519 return(1);
520 }
521
522 /* The macro is sane. Jump to the next word. */
523
524 while (buf[i] && ' ' == buf[i])
525 i++;
526
527 /*
528 * Trailing whitespace. Note that tabs are allowed to be passed
529 * into the parser as "text", so we only warn about spaces here.
530 */
531
532 if ('\0' == buf[i] && ' ' == buf[i - 1])
533 man_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE);
534
535 /*
536 * Remove prior ELINE macro, as it's being clobbered by a new
537 * macro. Note that NSCOPED macros do not close out ELINE
538 * macros---they don't print text---so we let those slip by.
539 */
540
541 if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
542 m->flags & MAN_ELINE) {
543 n = m->last;
544 assert(MAN_TEXT != n->type);
545
546 /* Remove repeated NSCOPED macros causing ELINE. */
547
548 if (MAN_NSCOPED & man_macros[n->tok].flags)
549 n = n->parent;
550
551 man_vmsg(m, MANDOCERR_LINESCOPE, n->line, n->pos,
552 "%s", man_macronames[n->tok]);
553
554 man_node_delete(m, n);
555 m->flags &= ~MAN_ELINE;
556 }
557
558 /*
559 * Save the fact that we're in the next-line for a block. In
560 * this way, embedded roff instructions can "remember" state
561 * when they exit.
562 */
563
564 if (MAN_BLINE & m->flags)
565 m->flags |= MAN_BPLINE;
566
567 /* Call to handler... */
568
569 assert(man_macros[tok].fp);
570 if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &i, buf))
571 goto err;
572
573 out:
574 /*
575 * We weren't in a block-line scope when entering the
576 * above-parsed macro, so return.
577 */
578
579 if ( ! (MAN_BPLINE & m->flags)) {
580 m->flags &= ~MAN_ILINE;
581 return(1);
582 }
583 m->flags &= ~MAN_BPLINE;
584
585 /*
586 * If we're in a block scope, then allow this macro to slip by
587 * without closing scope around it.
588 */
589
590 if (MAN_ILINE & m->flags) {
591 m->flags &= ~MAN_ILINE;
592 return(1);
593 }
594
595 /*
596 * If we've opened a new next-line element scope, then return
597 * now, as the next line will close out the block scope.
598 */
599
600 if (MAN_ELINE & m->flags)
601 return(1);
602
603 /* Close out the block scope opened in the prior line. */
604
605 assert(MAN_BLINE & m->flags);
606 m->flags &= ~MAN_BLINE;
607
608 if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
609 return(0);
610 return(man_body_alloc(m, ln, offs, m->last->tok));
611
612 err: /* Error out. */
613
614 m->flags |= MAN_HALT;
615 return(0);
616 }
617
618
619 int
620 man_vmsg(struct man *man, enum mandocerr t,
621 int ln, int pos, const char *fmt, ...)
622 {
623 char buf[256];
624 va_list ap;
625
626 va_start(ap, fmt);
627 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
628 va_end(ap);
629 return((*man->msg)(t, man->data, ln, pos, buf));
630 }
631
632
633 /*
634 * Unlink a node from its context. If "m" is provided, the last parse
635 * point will also be adjusted accordingly.
636 */
637 static void
638 man_node_unlink(struct man *m, struct man_node *n)
639 {
640
641 /* Adjust siblings. */
642
643 if (n->prev)
644 n->prev->next = n->next;
645 if (n->next)
646 n->next->prev = n->prev;
647
648 /* Adjust parent. */
649
650 if (n->parent) {
651 n->parent->nchild--;
652 if (n->parent->child == n)
653 n->parent->child = n->prev ? n->prev : n->next;
654 }
655
656 /* Adjust parse point, if applicable. */
657
658 if (m && m->last == n) {
659 /*XXX: this can occur when bailing from validation. */
660 /*assert(NULL == n->next);*/
661 if (n->prev) {
662 m->last = n->prev;
663 m->next = MAN_NEXT_SIBLING;
664 } else {
665 m->last = n->parent;
666 m->next = MAN_NEXT_CHILD;
667 }
668 }
669
670 if (m && m->first == n)
671 m->first = NULL;
672 }