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