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