]> git.cameronkatri.com Git - mandoc.git/blob - man.c
Open explicit scope on libman exit now only generates warning.
[mandoc.git] / man.c
1 /* $Id: man.c,v 1.34 2009/08/21 12:12:12 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 #include <assert.h>
18 #include <ctype.h>
19 #include <stdarg.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "libman.h"
25
26 const char *const __man_merrnames[WERRMAX] = {
27 "invalid character", /* WNPRINT */
28 "system: malloc error", /* WNMEM */
29 "invalid manual section", /* WMSEC */
30 "invalid date format", /* WDATE */
31 "scope of prior line violated", /* WLNSCOPE */
32 "trailing whitespace", /* WTSPACE */
33 "unterminated quoted parameter", /* WTQUOTE */
34 "document has no body", /* WNODATA */
35 "document has no title/section", /* WNOTITLE */
36 "invalid escape sequence", /* WESCAPE */
37 "invalid number format", /* WNUMFMT */
38 "expected block head arguments", /* WHEADARGS */
39 "expected block body arguments", /* WBODYARGS */
40 "expected empty block head", /* WNHEADARGS */
41 "unknown macro", /* WMACRO */
42 "ill-formed macro", /* WMACROFORM */
43 "scope open on exit", /* WEXITSCOPE */
44 "no scope context" /* WNOSCOPE */
45 };
46
47 const char *const __man_macronames[MAN_MAX] = {
48 "br", "TH", "SH", "SS",
49 "TP", "LP", "PP", "P",
50 "IP", "HP", "SM", "SB",
51 "BI", "IB", "BR", "RB",
52 "R", "B", "I", "IR",
53 "RI", "na", "i", "sp",
54 "nf", "fi", "r", "RE",
55 "RS", "DT"
56 };
57
58 const char * const *man_macronames = __man_macronames;
59
60 static struct man_node *man_node_alloc(int, int,
61 enum man_type, int);
62 static int man_node_append(struct man *,
63 struct man_node *);
64 static int man_ptext(struct man *, int, char *);
65 static int man_pmacro(struct man *, int, char *);
66 static void man_free1(struct man *);
67 static int man_alloc1(struct man *);
68 static int pstring(struct man *, int, int,
69 const char *, size_t);
70
71 #ifdef __linux__
72 extern size_t strlcpy(char *, const char *, size_t);
73 #endif
74
75
76 const struct man_node *
77 man_node(const struct man *m)
78 {
79
80 return(MAN_HALT & m->flags ? NULL : m->first);
81 }
82
83
84 const struct man_meta *
85 man_meta(const struct man *m)
86 {
87
88 return(MAN_HALT & m->flags ? NULL : &m->meta);
89 }
90
91
92 int
93 man_reset(struct man *man)
94 {
95
96 man_free1(man);
97 return(man_alloc1(man));
98 }
99
100
101 void
102 man_free(struct man *man)
103 {
104
105 man_free1(man);
106
107 if (man->htab)
108 man_hash_free(man->htab);
109 free(man);
110 }
111
112
113 struct man *
114 man_alloc(void *data, int pflags, const struct man_cb *cb)
115 {
116 struct man *p;
117
118 if (NULL == (p = calloc(1, sizeof(struct man))))
119 return(NULL);
120
121 if ( ! man_alloc1(p)) {
122 free(p);
123 return(NULL);
124 }
125
126 p->data = data;
127 p->pflags = pflags;
128 (void)memcpy(&p->cb, cb, sizeof(struct man_cb));
129
130 if (NULL == (p->htab = man_hash_alloc())) {
131 free(p);
132 return(NULL);
133 }
134 return(p);
135 }
136
137
138 int
139 man_endparse(struct man *m)
140 {
141
142 if (MAN_HALT & m->flags)
143 return(0);
144 else if (man_macroend(m))
145 return(1);
146 m->flags |= MAN_HALT;
147 return(0);
148 }
149
150
151 int
152 man_parseln(struct man *m, int ln, char *buf)
153 {
154
155 return('.' == *buf ?
156 man_pmacro(m, ln, buf) :
157 man_ptext(m, ln, buf));
158 }
159
160
161 static void
162 man_free1(struct man *man)
163 {
164
165 if (man->first)
166 man_node_freelist(man->first);
167 if (man->meta.title)
168 free(man->meta.title);
169 if (man->meta.source)
170 free(man->meta.source);
171 if (man->meta.vol)
172 free(man->meta.vol);
173 }
174
175
176 static int
177 man_alloc1(struct man *m)
178 {
179
180 bzero(&m->meta, sizeof(struct man_meta));
181 m->flags = 0;
182 m->last = calloc(1, sizeof(struct man_node));
183 if (NULL == m->last)
184 return(0);
185 m->first = m->last;
186 m->last->type = MAN_ROOT;
187 m->next = MAN_NEXT_CHILD;
188 return(1);
189 }
190
191
192 static int
193 man_node_append(struct man *man, struct man_node *p)
194 {
195
196 assert(man->last);
197 assert(man->first);
198 assert(MAN_ROOT != p->type);
199
200 switch (man->next) {
201 case (MAN_NEXT_SIBLING):
202 man->last->next = p;
203 p->prev = man->last;
204 p->parent = man->last->parent;
205 break;
206 case (MAN_NEXT_CHILD):
207 man->last->child = p;
208 p->parent = man->last;
209 break;
210 default:
211 abort();
212 /* NOTREACHED */
213 }
214
215 p->parent->nchild++;
216
217 if ( ! man_valid_pre(man, p))
218 return(0);
219
220 switch (p->type) {
221 case (MAN_HEAD):
222 assert(MAN_BLOCK == p->parent->type);
223 p->parent->head = p;
224 break;
225 case (MAN_BODY):
226 assert(MAN_BLOCK == p->parent->type);
227 p->parent->body = p;
228 break;
229 default:
230 break;
231 }
232
233 man->last = p;
234
235 switch (p->type) {
236 case (MAN_TEXT):
237 if ( ! man_valid_post(man))
238 return(0);
239 if ( ! man_action_post(man))
240 return(0);
241 break;
242 default:
243 break;
244 }
245
246 return(1);
247 }
248
249
250 static struct man_node *
251 man_node_alloc(int line, int pos, enum man_type type, int tok)
252 {
253 struct man_node *p;
254
255 p = calloc(1, sizeof(struct man_node));
256 if (NULL == p)
257 return(NULL);
258
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, int tok)
269 {
270 struct man_node *p;
271
272 p = man_node_alloc(line, pos, MAN_ELEM, tok);
273 if (NULL == p)
274 return(0);
275 if ( ! man_node_append(m, p))
276 return(0);
277 m->next = MAN_NEXT_CHILD;
278 return(1);
279 }
280
281
282 int
283 man_head_alloc(struct man *m, int line, int pos, int tok)
284 {
285 struct man_node *p;
286
287 p = man_node_alloc(line, pos, MAN_HEAD, tok);
288 if (NULL == p)
289 return(0);
290 if ( ! man_node_append(m, p))
291 return(0);
292 m->next = MAN_NEXT_CHILD;
293 return(1);
294 }
295
296
297 int
298 man_body_alloc(struct man *m, int line, int pos, int tok)
299 {
300 struct man_node *p;
301
302 p = man_node_alloc(line, pos, MAN_BODY, tok);
303 if (NULL == p)
304 return(0);
305 if ( ! man_node_append(m, p))
306 return(0);
307 m->next = MAN_NEXT_CHILD;
308 return(1);
309 }
310
311
312 int
313 man_block_alloc(struct man *m, int line, int pos, int tok)
314 {
315 struct man_node *p;
316
317 p = man_node_alloc(line, pos, MAN_BLOCK, tok);
318 if (NULL == p)
319 return(0);
320 if ( ! man_node_append(m, p))
321 return(0);
322 m->next = MAN_NEXT_CHILD;
323 return(1);
324 }
325
326
327 static int
328 pstring(struct man *m, int line, int pos,
329 const char *p, size_t len)
330 {
331 struct man_node *n;
332 size_t sv;
333
334 n = man_node_alloc(line, pos, MAN_TEXT, -1);
335 if (NULL == n)
336 return(0);
337
338 n->string = malloc(len + 1);
339 if (NULL == n->string) {
340 free(n);
341 return(0);
342 }
343
344 sv = strlcpy(n->string, p, len + 1);
345
346 /* Prohibit truncation. */
347 assert(sv < len + 1);
348
349 if ( ! man_node_append(m, n))
350 return(0);
351 m->next = MAN_NEXT_SIBLING;
352 return(1);
353 }
354
355
356 int
357 man_word_alloc(struct man *m, int line, int pos, const char *word)
358 {
359
360 return(pstring(m, line, pos, word, strlen(word)));
361 }
362
363
364 void
365 man_node_free(struct man_node *p)
366 {
367
368 if (p->string)
369 free(p->string);
370 if (p->parent)
371 p->parent->nchild--;
372 free(p);
373 }
374
375
376 void
377 man_node_freelist(struct man_node *p)
378 {
379
380 if (p->child)
381 man_node_freelist(p->child);
382 if (p->next)
383 man_node_freelist(p->next);
384
385 assert(0 == p->nchild);
386 man_node_free(p);
387 }
388
389
390 static int
391 man_ptext(struct man *m, int line, char *buf)
392 {
393 int i, j;
394
395 /* First de-chunk and allocate words. */
396
397 for (i = 0; ' ' == buf[i]; i++)
398 /* Skip leading whitespace. */ ;
399 if (0 == buf[i]) {
400 if ( ! pstring(m, line, 0, &buf[i], 0))
401 return(0);
402 goto descope;
403 }
404
405 for (j = i; buf[i]; i++) {
406 if (' ' != buf[i])
407 continue;
408
409 /* Escaped whitespace. */
410 if (i && ' ' == buf[i] && '\\' == buf[i - 1])
411 continue;
412
413 buf[i++] = 0;
414 if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
415 return(0);
416
417 for ( ; ' ' == buf[i]; i++)
418 /* Skip trailing whitespace. */ ;
419
420 j = i;
421 if (0 == buf[i])
422 break;
423 }
424
425 if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
426 return(0);
427
428 descope:
429
430 /*
431 * Co-ordinate what happens with having a next-line scope open:
432 * first close out the element scope (if applicable), then close
433 * out the block scope (also if applicable).
434 */
435
436 /* XXX - this should be in man_action.c. */
437
438 if (MAN_ELINE & m->flags) {
439 m->flags &= ~MAN_ELINE;
440 if ( ! man_unscope(m, m->last->parent))
441 return(0);
442 }
443
444 if ( ! (MAN_BLINE & m->flags))
445 return(1);
446 m->flags &= ~MAN_BLINE;
447
448 if ( ! man_unscope(m, m->last->parent))
449 return(0);
450 return(man_body_alloc(m, line, 0, m->last->tok));
451 }
452
453
454 int
455 man_pmacro(struct man *m, int ln, char *buf)
456 {
457 int i, j, c, ppos, fl;
458 char mac[5];
459 struct man_node *n;
460
461 /* Comments and empties are quickly ignored. */
462
463 fl = m->flags;
464
465 if (0 == buf[1])
466 goto out;
467
468 i = 1;
469
470 if (' ' == buf[i]) {
471 i++;
472 while (buf[i] && ' ' == buf[i])
473 i++;
474 if (0 == buf[i])
475 goto out;
476 }
477
478 ppos = i;
479
480 /* Copy the first word into a nil-terminated buffer. */
481
482 for (j = 0; j < 4; j++, i++) {
483 if (0 == (mac[j] = buf[i]))
484 break;
485 else if (' ' == buf[i])
486 break;
487 }
488
489 mac[j] = 0;
490
491 if (j == 4 || j < 1) {
492 if ( ! (MAN_IGN_MACRO & m->pflags)) {
493 (void)man_perr(m, ln, ppos, WMACROFORM);
494 goto err;
495 }
496 if ( ! man_pwarn(m, ln, ppos, WMACROFORM))
497 goto err;
498 return(1);
499 }
500
501 if (MAN_MAX == (c = man_hash_find(m->htab, mac))) {
502 if ( ! (MAN_IGN_MACRO & m->pflags)) {
503 (void)man_perr(m, ln, ppos, WMACRO);
504 goto err;
505 }
506 if ( ! man_pwarn(m, ln, ppos, WMACRO))
507 goto err;
508 return(1);
509 }
510
511 /* The macro is sane. Jump to the next word. */
512
513 while (buf[i] && ' ' == buf[i])
514 i++;
515
516 /* Remove prior ELINE macro, if applicable. */
517
518 if (m->flags & MAN_ELINE) {
519 n = m->last;
520 assert(NULL == n->child);
521 if ( ! man_nwarn(m, n, WLNSCOPE))
522 return(0);
523
524 if (n->prev) {
525 assert(n != n->parent->child);
526 assert(n == n->prev->next);
527 n->prev->next = NULL;
528 m->last = n->prev;
529 } else {
530 assert(n == n->parent->child);
531 n->parent->child = NULL;
532 m->last = n->parent;
533 }
534
535 man_node_free(n);
536 m->flags &= ~MAN_ELINE;
537 }
538
539 /* Begin recursive parse sequence. */
540
541 assert(man_macros[c].fp);
542
543 if ( ! (*man_macros[c].fp)(m, c, ln, ppos, &i, buf))
544 goto err;
545
546 out:
547 if ( ! (MAN_BLINE & fl))
548 return(1);
549
550 /*
551 * If we've opened a new next-line element scope, then return
552 * now, as the next line will close out the block scope.
553 */
554
555 if (MAN_ELINE & m->flags)
556 return(1);
557
558 /* Close out the block scope opened in the prior line. */
559
560 assert(MAN_BLINE & m->flags);
561 m->flags &= ~MAN_BLINE;
562
563 if ( ! man_unscope(m, m->last->parent))
564 return(0);
565 return(man_body_alloc(m, ln, 0, m->last->tok));
566
567 err: /* Error out. */
568
569 m->flags |= MAN_HALT;
570 return(0);
571 }
572
573
574 int
575 man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
576 {
577 char buf[256];
578 va_list ap;
579
580 if (NULL == man->cb.man_err)
581 return(0);
582
583 va_start(ap, fmt);
584 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
585 va_end(ap);
586 return((*man->cb.man_err)(man->data, ln, pos, buf));
587 }
588
589
590 int
591 man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
592 {
593 char buf[256];
594 va_list ap;
595
596 if (NULL == man->cb.man_warn)
597 return(0);
598
599 va_start(ap, fmt);
600 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
601 va_end(ap);
602 return((*man->cb.man_warn)(man->data, ln, pos, buf));
603 }
604
605
606 int
607 man_err(struct man *m, int line, int pos, int iserr, enum merr type)
608 {
609 const char *p;
610
611 p = __man_merrnames[(int)type];
612 assert(p);
613
614 if (iserr)
615 return(man_verr(m, line, pos, p));
616
617 return(man_vwarn(m, line, pos, p));
618 }