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