]> git.cameronkatri.com Git - mandoc.git/blob - man.c
2792921d10a617106ed6f8ce20c6b2a487d4a7e2
[mandoc.git] / man.c
1 /* $Id: man.c,v 1.93 2011/01/01 10:51:30 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(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
59
60 const struct man_node *
61 man_node(const struct man *m)
62 {
63
64 return(MAN_HALT & m->flags ? NULL : m->first);
65 }
66
67
68 const struct man_meta *
69 man_meta(const struct man *m)
70 {
71
72 return(MAN_HALT & m->flags ? NULL : &m->meta);
73 }
74
75
76 void
77 man_reset(struct man *man)
78 {
79
80 man_free1(man);
81 man_alloc1(man);
82 }
83
84
85 void
86 man_free(struct man *man)
87 {
88
89 man_free1(man);
90 free(man);
91 }
92
93
94 struct man *
95 man_alloc(struct regset *regs, void *data, mandocmsg msg)
96 {
97 struct man *p;
98
99 p = mandoc_calloc(1, sizeof(struct man));
100
101 man_hash_init();
102 p->data = data;
103 p->msg = msg;
104 p->regs = regs;
105
106 man_alloc1(p);
107 return(p);
108 }
109
110
111 int
112 man_endparse(struct man *m)
113 {
114
115 if (MAN_HALT & m->flags)
116 return(0);
117 else if (man_macroend(m))
118 return(1);
119 m->flags |= MAN_HALT;
120 return(0);
121 }
122
123
124 int
125 man_parseln(struct man *m, int ln, char *buf, int offs)
126 {
127
128 if (MAN_HALT & m->flags)
129 return(0);
130
131 return(('.' == buf[offs] || '\'' == buf[offs]) ?
132 man_pmacro(m, ln, buf, offs) :
133 man_ptext(m, ln, buf, offs));
134 }
135
136
137 static void
138 man_free1(struct man *man)
139 {
140
141 if (man->first)
142 man_node_delete(man, man->first);
143 if (man->meta.title)
144 free(man->meta.title);
145 if (man->meta.source)
146 free(man->meta.source);
147 if (man->meta.rawdate)
148 free(man->meta.rawdate);
149 if (man->meta.vol)
150 free(man->meta.vol);
151 if (man->meta.msec)
152 free(man->meta.msec);
153 }
154
155
156 static void
157 man_alloc1(struct man *m)
158 {
159
160 memset(&m->meta, 0, sizeof(struct man_meta));
161 m->flags = 0;
162 m->last = mandoc_calloc(1, sizeof(struct man_node));
163 m->first = m->last;
164 m->last->type = MAN_ROOT;
165 m->last->tok = MAN_MAX;
166 m->next = MAN_NEXT_CHILD;
167 }
168
169
170 static int
171 man_node_append(struct man *man, struct man_node *p)
172 {
173
174 assert(man->last);
175 assert(man->first);
176 assert(MAN_ROOT != p->type);
177
178 switch (man->next) {
179 case (MAN_NEXT_SIBLING):
180 man->last->next = p;
181 p->prev = man->last;
182 p->parent = man->last->parent;
183 break;
184 case (MAN_NEXT_CHILD):
185 man->last->child = p;
186 p->parent = man->last;
187 break;
188 default:
189 abort();
190 /* NOTREACHED */
191 }
192
193 assert(p->parent);
194 p->parent->nchild++;
195
196 if ( ! man_valid_pre(man, p))
197 return(0);
198
199 switch (p->type) {
200 case (MAN_HEAD):
201 assert(MAN_BLOCK == p->parent->type);
202 p->parent->head = p;
203 break;
204 case (MAN_BODY):
205 assert(MAN_BLOCK == p->parent->type);
206 p->parent->body = p;
207 break;
208 default:
209 break;
210 }
211
212 man->last = p;
213
214 switch (p->type) {
215 case (MAN_TEXT):
216 if ( ! man_valid_post(man))
217 return(0);
218 break;
219 default:
220 break;
221 }
222
223 return(1);
224 }
225
226
227 static struct man_node *
228 man_node_alloc(int line, int pos, enum man_type type, enum mant tok)
229 {
230 struct man_node *p;
231
232 p = mandoc_calloc(1, sizeof(struct man_node));
233 p->line = line;
234 p->pos = pos;
235 p->type = type;
236 p->tok = tok;
237 return(p);
238 }
239
240
241 int
242 man_elem_alloc(struct man *m, int line, int pos, enum mant tok)
243 {
244 struct man_node *p;
245
246 p = man_node_alloc(line, pos, MAN_ELEM, tok);
247 if ( ! man_node_append(m, p))
248 return(0);
249 m->next = MAN_NEXT_CHILD;
250 return(1);
251 }
252
253
254 int
255 man_head_alloc(struct man *m, int line, int pos, enum mant tok)
256 {
257 struct man_node *p;
258
259 p = man_node_alloc(line, pos, MAN_HEAD, tok);
260 if ( ! man_node_append(m, p))
261 return(0);
262 m->next = MAN_NEXT_CHILD;
263 return(1);
264 }
265
266
267 int
268 man_body_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_BODY, 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_block_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_BLOCK, 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_word_alloc(struct man *m, int line, int pos, const char *word)
295 {
296 struct man_node *n;
297 size_t sv, len;
298
299 len = strlen(word);
300
301 n = man_node_alloc(line, pos, MAN_TEXT, MAN_MAX);
302 n->string = mandoc_malloc(len + 1);
303 sv = strlcpy(n->string, word, len + 1);
304
305 /* Prohibit truncation. */
306 assert(sv < len + 1);
307
308 if ( ! man_node_append(m, n))
309 return(0);
310
311 m->next = MAN_NEXT_SIBLING;
312 return(1);
313 }
314
315
316 /*
317 * Free all of the resources held by a node. This does NOT unlink a
318 * node from its context; for that, see man_node_unlink().
319 */
320 static void
321 man_node_free(struct man_node *p)
322 {
323
324 if (p->string)
325 free(p->string);
326 free(p);
327 }
328
329
330 void
331 man_node_delete(struct man *m, struct man_node *p)
332 {
333
334 while (p->child)
335 man_node_delete(m, p->child);
336
337 man_node_unlink(m, p);
338 man_node_free(p);
339 }
340
341
342 static int
343 man_ptext(struct man *m, int line, char *buf, int offs)
344 {
345 int i;
346
347 /* Ignore bogus comments. */
348
349 if ('\\' == buf[offs] &&
350 '.' == buf[offs + 1] &&
351 '"' == buf[offs + 2]) {
352 man_pmsg(m, line, offs, MANDOCERR_BADCOMMENT);
353 return(1);
354 }
355
356 /* Literal free-form text whitespace is preserved. */
357
358 if (MAN_LITERAL & m->flags) {
359 if ( ! man_word_alloc(m, line, offs, buf + offs))
360 return(0);
361 goto descope;
362 }
363
364 /* Pump blank lines directly into the backend. */
365
366 for (i = offs; ' ' == buf[i]; i++)
367 /* Skip leading whitespace. */ ;
368
369 if ('\0' == buf[i]) {
370 /* Allocate a blank entry. */
371 if ( ! man_word_alloc(m, line, offs, ""))
372 return(0);
373 goto descope;
374 }
375
376 /*
377 * Warn if the last un-escaped character is whitespace. Then
378 * strip away the remaining spaces (tabs stay!).
379 */
380
381 i = (int)strlen(buf);
382 assert(i);
383
384 if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
385 if (i > 1 && '\\' != buf[i - 2])
386 man_pmsg(m, line, i - 1, MANDOCERR_EOLNSPACE);
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, "%s", buf + ppos - 1);
478 return(1);
479 }
480
481 /* The macro is sane. Jump to the next word. */
482
483 while (buf[i] && ' ' == buf[i])
484 i++;
485
486 /*
487 * Trailing whitespace. Note that tabs are allowed to be passed
488 * into the parser as "text", so we only warn about spaces here.
489 */
490
491 if ('\0' == buf[i] && ' ' == buf[i - 1])
492 man_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE);
493
494 /*
495 * Remove prior ELINE macro, as it's being clobbered by a new
496 * macro. Note that NSCOPED macros do not close out ELINE
497 * macros---they don't print text---so we let those slip by.
498 */
499
500 if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
501 m->flags & MAN_ELINE) {
502 n = m->last;
503 assert(MAN_TEXT != n->type);
504
505 /* Remove repeated NSCOPED macros causing ELINE. */
506
507 if (MAN_NSCOPED & man_macros[n->tok].flags)
508 n = n->parent;
509
510 man_vmsg(m, MANDOCERR_LINESCOPE, n->line, n->pos,
511 "%s", man_macronames[n->tok]);
512
513 man_node_delete(m, n);
514 m->flags &= ~MAN_ELINE;
515 }
516
517 /*
518 * Save the fact that we're in the next-line for a block. In
519 * this way, embedded roff instructions can "remember" state
520 * when they exit.
521 */
522
523 if (MAN_BLINE & m->flags)
524 m->flags |= MAN_BPLINE;
525
526 /* Call to handler... */
527
528 assert(man_macros[tok].fp);
529 if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &i, buf))
530 goto err;
531
532 out:
533 /*
534 * We weren't in a block-line scope when entering the
535 * above-parsed macro, so return.
536 */
537
538 if ( ! (MAN_BPLINE & m->flags)) {
539 m->flags &= ~MAN_ILINE;
540 return(1);
541 }
542 m->flags &= ~MAN_BPLINE;
543
544 /*
545 * If we're in a block scope, then allow this macro to slip by
546 * without closing scope around it.
547 */
548
549 if (MAN_ILINE & m->flags) {
550 m->flags &= ~MAN_ILINE;
551 return(1);
552 }
553
554 /*
555 * If we've opened a new next-line element scope, then return
556 * now, as the next line will close out the block scope.
557 */
558
559 if (MAN_ELINE & m->flags)
560 return(1);
561
562 /* Close out the block scope opened in the prior line. */
563
564 assert(MAN_BLINE & m->flags);
565 m->flags &= ~MAN_BLINE;
566
567 if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
568 return(0);
569 return(man_body_alloc(m, ln, offs, m->last->tok));
570
571 err: /* Error out. */
572
573 m->flags |= MAN_HALT;
574 return(0);
575 }
576
577
578 int
579 man_vmsg(struct man *man, enum mandocerr t,
580 int ln, int pos, const char *fmt, ...)
581 {
582 char buf[256];
583 va_list ap;
584
585 va_start(ap, fmt);
586 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
587 va_end(ap);
588 return((*man->msg)(t, man->data, ln, pos, buf));
589 }
590
591
592 /*
593 * Unlink a node from its context. If "m" is provided, the last parse
594 * point will also be adjusted accordingly.
595 */
596 static void
597 man_node_unlink(struct man *m, struct man_node *n)
598 {
599
600 /* Adjust siblings. */
601
602 if (n->prev)
603 n->prev->next = n->next;
604 if (n->next)
605 n->next->prev = n->prev;
606
607 /* Adjust parent. */
608
609 if (n->parent) {
610 n->parent->nchild--;
611 if (n->parent->child == n)
612 n->parent->child = n->prev ? n->prev : n->next;
613 }
614
615 /* Adjust parse point, if applicable. */
616
617 if (m && m->last == n) {
618 /*XXX: this can occur when bailing from validation. */
619 /*assert(NULL == n->next);*/
620 if (n->prev) {
621 m->last = n->prev;
622 m->next = MAN_NEXT_SIBLING;
623 } else {
624 m->last = n->parent;
625 m->next = MAN_NEXT_CHILD;
626 }
627 }
628
629 if (m && m->first == n)
630 m->first = NULL;
631 }