]> git.cameronkatri.com Git - mandoc.git/blob - man.c
simplify the code copying the macro name, and sync the
[mandoc.git] / man.c
1 /* $Id: man.c,v 1.86 2010/08/08 14:51:32 schwarze 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 static int macrowarn(struct man *, int, const char *, int);
60
61
62 const struct man_node *
63 man_node(const struct man *m)
64 {
65
66 return(MAN_HALT & m->flags ? NULL : m->first);
67 }
68
69
70 const struct man_meta *
71 man_meta(const struct man *m)
72 {
73
74 return(MAN_HALT & m->flags ? NULL : &m->meta);
75 }
76
77
78 void
79 man_reset(struct man *man)
80 {
81
82 man_free1(man);
83 man_alloc1(man);
84 }
85
86
87 void
88 man_free(struct man *man)
89 {
90
91 man_free1(man);
92 free(man);
93 }
94
95
96 struct man *
97 man_alloc(struct regset *regs, void *data,
98 int pflags, 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->pflags = pflags;
107 p->msg = msg;
108 p->regs = regs;
109
110 man_alloc1(p);
111 return(p);
112 }
113
114
115 int
116 man_endparse(struct man *m)
117 {
118
119 if (MAN_HALT & m->flags)
120 return(0);
121 else 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 if (MAN_HALT & m->flags)
133 return(0);
134
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_TEXT):
220 if ( ! man_valid_post(man))
221 return(0);
222 if ( ! man_action_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(int line, int pos, enum man_type type, enum mant tok)
235 {
236 struct man_node *p;
237
238 p = mandoc_calloc(1, sizeof(struct man_node));
239 p->line = line;
240 p->pos = pos;
241 p->type = type;
242 p->tok = tok;
243 return(p);
244 }
245
246
247 int
248 man_elem_alloc(struct man *m, int line, int pos, enum mant tok)
249 {
250 struct man_node *p;
251
252 p = man_node_alloc(line, pos, MAN_ELEM, tok);
253 if ( ! man_node_append(m, p))
254 return(0);
255 m->next = MAN_NEXT_CHILD;
256 return(1);
257 }
258
259
260 int
261 man_head_alloc(struct man *m, int line, int pos, enum mant tok)
262 {
263 struct man_node *p;
264
265 p = man_node_alloc(line, pos, MAN_HEAD, tok);
266 if ( ! man_node_append(m, p))
267 return(0);
268 m->next = MAN_NEXT_CHILD;
269 return(1);
270 }
271
272
273 int
274 man_body_alloc(struct man *m, int line, int pos, enum mant tok)
275 {
276 struct man_node *p;
277
278 p = man_node_alloc(line, pos, MAN_BODY, tok);
279 if ( ! man_node_append(m, p))
280 return(0);
281 m->next = MAN_NEXT_CHILD;
282 return(1);
283 }
284
285
286 int
287 man_block_alloc(struct man *m, int line, int pos, enum mant tok)
288 {
289 struct man_node *p;
290
291 p = man_node_alloc(line, pos, MAN_BLOCK, tok);
292 if ( ! man_node_append(m, p))
293 return(0);
294 m->next = MAN_NEXT_CHILD;
295 return(1);
296 }
297
298
299 int
300 man_word_alloc(struct man *m, int line, int pos, const char *word)
301 {
302 struct man_node *n;
303 size_t sv, len;
304
305 len = strlen(word);
306
307 n = man_node_alloc(line, pos, MAN_TEXT, MAN_MAX);
308 n->string = mandoc_malloc(len + 1);
309 sv = strlcpy(n->string, word, len + 1);
310
311 /* Prohibit truncation. */
312 assert(sv < len + 1);
313
314 if ( ! man_node_append(m, n))
315 return(0);
316
317 m->next = MAN_NEXT_SIBLING;
318 return(1);
319 }
320
321
322 /*
323 * Free all of the resources held by a node. This does NOT unlink a
324 * node from its context; for that, see man_node_unlink().
325 */
326 static void
327 man_node_free(struct man_node *p)
328 {
329
330 if (p->string)
331 free(p->string);
332 free(p);
333 }
334
335
336 void
337 man_node_delete(struct man *m, struct man_node *p)
338 {
339
340 while (p->child)
341 man_node_delete(m, p->child);
342
343 man_node_unlink(m, p);
344 man_node_free(p);
345 }
346
347
348 static int
349 man_ptext(struct man *m, int line, char *buf, int offs)
350 {
351 int i;
352
353 /* Ignore bogus comments. */
354
355 if ('\\' == buf[offs] &&
356 '.' == buf[offs + 1] &&
357 '"' == buf[offs + 2])
358 return(man_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
359
360 /* Literal free-form text whitespace is preserved. */
361
362 if (MAN_LITERAL & m->flags) {
363 if ( ! man_word_alloc(m, line, offs, buf + offs))
364 return(0);
365 goto descope;
366 }
367
368 /* Pump blank lines directly into the backend. */
369
370 for (i = offs; ' ' == buf[i]; i++)
371 /* Skip leading whitespace. */ ;
372
373 if ('\0' == buf[i]) {
374 /* Allocate a blank entry. */
375 if ( ! man_word_alloc(m, line, offs, ""))
376 return(0);
377 goto descope;
378 }
379
380 /*
381 * Warn if the last un-escaped character is whitespace. Then
382 * strip away the remaining spaces (tabs stay!).
383 */
384
385 i = (int)strlen(buf);
386 assert(i);
387
388 if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
389 if (i > 1 && '\\' != buf[i - 2])
390 if ( ! man_pmsg(m, line, i - 1, MANDOCERR_EOLNSPACE))
391 return(0);
392
393 for (--i; i && ' ' == buf[i]; i--)
394 /* Spin back to non-space. */ ;
395
396 /* Jump ahead of escaped whitespace. */
397 i += '\\' == buf[i] ? 2 : 1;
398
399 buf[i] = '\0';
400 }
401
402 if ( ! man_word_alloc(m, line, offs, buf + offs))
403 return(0);
404
405 /*
406 * End-of-sentence check. If the last character is an unescaped
407 * EOS character, then flag the node as being the end of a
408 * sentence. The front-end will know how to interpret this.
409 */
410
411 assert(i);
412 if (mandoc_eos(buf, (size_t)i, 0))
413 m->last->flags |= MAN_EOS;
414
415 descope:
416 /*
417 * Co-ordinate what happens with having a next-line scope open:
418 * first close out the element scope (if applicable), then close
419 * out the block scope (also if applicable).
420 */
421
422 if (MAN_ELINE & m->flags) {
423 m->flags &= ~MAN_ELINE;
424 if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
425 return(0);
426 }
427
428 if ( ! (MAN_BLINE & m->flags))
429 return(1);
430 m->flags &= ~MAN_BLINE;
431
432 if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
433 return(0);
434 return(man_body_alloc(m, line, offs, m->last->tok));
435 }
436
437
438 static int
439 macrowarn(struct man *m, int ln, const char *buf, int offs)
440 {
441 int rc;
442
443 rc = man_vmsg(m, MANDOCERR_MACRO, ln, offs,
444 "unknown macro: %s%s",
445 buf, strlen(buf) > 3 ? "..." : "");
446
447 return(MAN_IGN_MACRO & m->pflags ? rc : 0);
448 }
449
450
451 int
452 man_pmacro(struct man *m, int ln, char *buf, int offs)
453 {
454 int i, j, ppos;
455 enum mant tok;
456 char mac[5];
457 struct man_node *n;
458
459 /* Comments and empties are quickly ignored. */
460
461 offs++;
462
463 if ('\0' == buf[offs])
464 return(1);
465
466 i = offs;
467
468 /*
469 * Skip whitespace between the control character and initial
470 * text. "Whitespace" is both spaces and tabs.
471 */
472
473 if (' ' == buf[i] || '\t' == buf[i]) {
474 i++;
475 while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
476 i++;
477 if ('\0' == buf[i])
478 goto out;
479 }
480
481 ppos = i;
482
483 /*
484 * Copy the first word into a nil-terminated buffer.
485 * Stop copying when a tab, space, or eoln is encountered.
486 */
487
488 j = 0;
489 while (j < 4 && '\0' != buf[i] && ' ' != buf[i] && '\t' != buf[i])
490 mac[j++] = buf[i++];
491 mac[j] = '\0';
492
493 if (j == 4 || j < 1) {
494 if ( ! macrowarn(m, ln, mac, ppos))
495 goto err;
496 return(1);
497 }
498
499 if (MAN_MAX == (tok = man_hash_find(mac))) {
500 if ( ! macrowarn(m, ln, mac, ppos))
501 goto err;
502 return(1);
503 }
504
505 /* The macro is sane. Jump to the next word. */
506
507 while (buf[i] && ' ' == buf[i])
508 i++;
509
510 /*
511 * Trailing whitespace. Note that tabs are allowed to be passed
512 * into the parser as "text", so we only warn about spaces here.
513 */
514
515 if ('\0' == buf[i] && ' ' == buf[i - 1])
516 if ( ! man_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
517 goto err;
518
519 /*
520 * Remove prior ELINE macro, as it's being clobbering by a new
521 * macro. Note that NSCOPED macros do not close out ELINE
522 * macros---they don't print text---so we let those slip by.
523 */
524
525 if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
526 m->flags & MAN_ELINE) {
527 assert(MAN_TEXT != m->last->type);
528
529 /*
530 * This occurs in the following construction:
531 * .B
532 * .br
533 * .B
534 * .br
535 * I hate man macros.
536 * Flat-out disallow this madness.
537 */
538 if (MAN_NSCOPED & man_macros[m->last->tok].flags) {
539 man_pmsg(m, ln, ppos, MANDOCERR_SYNTLINESCOPE);
540 return(0);
541 }
542
543 n = m->last;
544
545 assert(n);
546 assert(NULL == n->child);
547 assert(0 == n->nchild);
548
549 if ( ! man_nmsg(m, n, MANDOCERR_LINESCOPE))
550 return(0);
551
552 man_node_delete(m, n);
553 m->flags &= ~MAN_ELINE;
554 }
555
556 /*
557 * Save the fact that we're in the next-line for a block. In
558 * this way, embedded roff instructions can "remember" state
559 * when they exit.
560 */
561
562 if (MAN_BLINE & m->flags)
563 m->flags |= MAN_BPLINE;
564
565 /* Call to handler... */
566
567 assert(man_macros[tok].fp);
568 if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &i, buf))
569 goto err;
570
571 out:
572 /*
573 * We weren't in a block-line scope when entering the
574 * above-parsed macro, so return.
575 */
576
577 if ( ! (MAN_BPLINE & m->flags)) {
578 m->flags &= ~MAN_ILINE;
579 return(1);
580 }
581 m->flags &= ~MAN_BPLINE;
582
583 /*
584 * If we're in a block scope, then allow this macro to slip by
585 * without closing scope around it.
586 */
587
588 if (MAN_ILINE & m->flags) {
589 m->flags &= ~MAN_ILINE;
590 return(1);
591 }
592
593 /*
594 * If we've opened a new next-line element scope, then return
595 * now, as the next line will close out the block scope.
596 */
597
598 if (MAN_ELINE & m->flags)
599 return(1);
600
601 /* Close out the block scope opened in the prior line. */
602
603 assert(MAN_BLINE & m->flags);
604 m->flags &= ~MAN_BLINE;
605
606 if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
607 return(0);
608 return(man_body_alloc(m, ln, offs, m->last->tok));
609
610 err: /* Error out. */
611
612 m->flags |= MAN_HALT;
613 return(0);
614 }
615
616
617 int
618 man_vmsg(struct man *man, enum mandocerr t,
619 int ln, int pos, const char *fmt, ...)
620 {
621 char buf[256];
622 va_list ap;
623
624 va_start(ap, fmt);
625 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
626 va_end(ap);
627 return((*man->msg)(t, man->data, ln, pos, buf));
628 }
629
630
631 /*
632 * Unlink a node from its context. If "m" is provided, the last parse
633 * point will also be adjusted accordingly.
634 */
635 static void
636 man_node_unlink(struct man *m, struct man_node *n)
637 {
638
639 /* Adjust siblings. */
640
641 if (n->prev)
642 n->prev->next = n->next;
643 if (n->next)
644 n->next->prev = n->prev;
645
646 /* Adjust parent. */
647
648 if (n->parent) {
649 n->parent->nchild--;
650 if (n->parent->child == n)
651 n->parent->child = n->prev ? n->prev : n->next;
652 }
653
654 /* Adjust parse point, if applicable. */
655
656 if (m && m->last == n) {
657 /*XXX: this can occur when bailing from validation. */
658 /*assert(NULL == n->next);*/
659 if (n->prev) {
660 m->last = n->prev;
661 m->next = MAN_NEXT_SIBLING;
662 } else {
663 m->last = n->parent;
664 m->next = MAN_NEXT_CHILD;
665 }
666 }
667
668 if (m && m->first == n)
669 m->first = NULL;
670 }