Annotation of mandoc/man.c, Revision 1.23
1.23 ! kristaps 1: /* $Id: man.c,v 1.22 2009/06/18 10:32:00 kristaps Exp $ */
1.1 kristaps 2: /*
1.19 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.18 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.18 kristaps 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.
1.1 kristaps 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_macronames[MAN_MAX] = {
1.21 kristaps 27: "br", "TH", "SH", "SS",
1.1 kristaps 28: "TP", "LP", "PP", "P",
29: "IP", "HP", "SM", "SB",
30: "BI", "IB", "BR", "RB",
1.11 kristaps 31: "R", "B", "I", "IR",
1.21 kristaps 32: "RI", "na", "i"
1.1 kristaps 33: };
34:
35: const char * const *man_macronames = __man_macronames;
36:
1.16 kristaps 37: static struct man_node *man_node_alloc(int, int,
38: enum man_type, int);
1.1 kristaps 39: static int man_node_append(struct man *,
40: struct man_node *);
41: static int man_ptext(struct man *, int, char *);
42: static int man_pmacro(struct man *, int, char *);
1.2 kristaps 43: static void man_free1(struct man *);
1.16 kristaps 44: static int man_alloc1(struct man *);
1.1 kristaps 45:
46:
47: const struct man_node *
1.2 kristaps 48: man_node(const struct man *m)
1.1 kristaps 49: {
50:
1.2 kristaps 51: return(MAN_HALT & m->flags ? NULL : m->first);
1.1 kristaps 52: }
53:
54:
55: const struct man_meta *
1.2 kristaps 56: man_meta(const struct man *m)
1.1 kristaps 57: {
58:
1.2 kristaps 59: return(MAN_HALT & m->flags ? NULL : &m->meta);
1.1 kristaps 60: }
61:
62:
1.15 kristaps 63: int
1.1 kristaps 64: man_reset(struct man *man)
65: {
66:
1.2 kristaps 67: man_free1(man);
1.16 kristaps 68: return(man_alloc1(man));
1.1 kristaps 69: }
70:
71:
72: void
73: man_free(struct man *man)
74: {
75:
1.2 kristaps 76: man_free1(man);
77:
1.1 kristaps 78: if (man->htab)
79: man_hash_free(man->htab);
80: free(man);
81: }
82:
83:
84: struct man *
1.7 kristaps 85: man_alloc(void *data, int pflags, const struct man_cb *cb)
1.1 kristaps 86: {
87: struct man *p;
88:
1.16 kristaps 89: if (NULL == (p = calloc(1, sizeof(struct man))))
90: return(NULL);
1.2 kristaps 91:
1.16 kristaps 92: if ( ! man_alloc1(p)) {
93: free(p);
94: return(NULL);
95: }
1.1 kristaps 96:
1.4 kristaps 97: p->data = data;
1.7 kristaps 98: p->pflags = pflags;
1.16 kristaps 99: (void)memcpy(&p->cb, cb, sizeof(struct man_cb));
1.7 kristaps 100:
1.16 kristaps 101: if (NULL == (p->htab = man_hash_alloc())) {
102: free(p);
103: return(NULL);
104: }
1.1 kristaps 105: return(p);
106: }
107:
108:
109: int
110: man_endparse(struct man *m)
111: {
112:
1.3 kristaps 113: if (MAN_HALT & m->flags)
114: return(0);
115: else if (man_macroend(m))
116: return(1);
117: m->flags |= MAN_HALT;
118: return(0);
1.1 kristaps 119: }
120:
121:
122: int
123: man_parseln(struct man *m, int ln, char *buf)
124: {
125:
126: return('.' == *buf ?
127: man_pmacro(m, ln, buf) :
128: man_ptext(m, ln, buf));
129: }
130:
131:
1.2 kristaps 132: static void
133: man_free1(struct man *man)
134: {
135:
136: if (man->first)
137: man_node_freelist(man->first);
138: if (man->meta.title)
139: free(man->meta.title);
1.6 kristaps 140: if (man->meta.source)
141: free(man->meta.source);
1.2 kristaps 142: if (man->meta.vol)
143: free(man->meta.vol);
144: }
145:
146:
1.16 kristaps 147: static int
1.2 kristaps 148: man_alloc1(struct man *m)
149: {
150:
151: bzero(&m->meta, sizeof(struct man_meta));
152: m->flags = 0;
153: m->last = calloc(1, sizeof(struct man_node));
154: if (NULL == m->last)
1.16 kristaps 155: return(0);
1.2 kristaps 156: m->first = m->last;
157: m->last->type = MAN_ROOT;
158: m->next = MAN_NEXT_CHILD;
1.16 kristaps 159: return(1);
1.2 kristaps 160: }
161:
162:
1.1 kristaps 163: static int
164: man_node_append(struct man *man, struct man_node *p)
165: {
166:
167: assert(man->last);
168: assert(man->first);
169: assert(MAN_ROOT != p->type);
170:
171: switch (man->next) {
172: case (MAN_NEXT_SIBLING):
173: man->last->next = p;
174: p->prev = man->last;
175: p->parent = man->last->parent;
176: break;
177: case (MAN_NEXT_CHILD):
178: man->last->child = p;
179: p->parent = man->last;
180: break;
181: default:
182: abort();
183: /* NOTREACHED */
184: }
1.22 kristaps 185:
186: p->parent->nchild++;
1.1 kristaps 187:
1.2 kristaps 188: man->last = p;
189:
1.1 kristaps 190: switch (p->type) {
1.2 kristaps 191: case (MAN_TEXT):
192: if ( ! man_valid_post(man))
193: return(0);
194: if ( ! man_action_post(man))
195: return(0);
1.1 kristaps 196: break;
197: default:
198: break;
199: }
200:
201: return(1);
202: }
203:
204:
205: static struct man_node *
1.16 kristaps 206: man_node_alloc(int line, int pos, enum man_type type, int tok)
1.1 kristaps 207: {
208: struct man_node *p;
209:
1.16 kristaps 210: p = calloc(1, sizeof(struct man_node));
211: if (NULL == p)
212: return(NULL);
213:
1.1 kristaps 214: p->line = line;
215: p->pos = pos;
216: p->type = type;
1.16 kristaps 217: p->tok = tok;
1.1 kristaps 218: return(p);
219: }
220:
221:
222: int
223: man_elem_alloc(struct man *man, int line, int pos, int tok)
224: {
225: struct man_node *p;
226:
1.16 kristaps 227: p = man_node_alloc(line, pos, MAN_ELEM, tok);
228: if (NULL == p)
229: return(0);
1.1 kristaps 230: return(man_node_append(man, p));
231: }
232:
233:
234: int
235: man_word_alloc(struct man *man,
236: int line, int pos, const char *word)
237: {
238: struct man_node *p;
239:
1.16 kristaps 240: p = man_node_alloc(line, pos, MAN_TEXT, -1);
241: if (NULL == p)
242: return(0);
1.1 kristaps 243: if (NULL == (p->string = strdup(word)))
1.16 kristaps 244: return(0);
1.1 kristaps 245: return(man_node_append(man, p));
246: }
247:
248:
249: void
250: man_node_free(struct man_node *p)
251: {
252:
253: if (p->string)
254: free(p->string);
255: free(p);
256: }
257:
258:
259: void
260: man_node_freelist(struct man_node *p)
261: {
262:
263: if (p->child)
264: man_node_freelist(p->child);
265: if (p->next)
266: man_node_freelist(p->next);
267:
268: man_node_free(p);
269: }
270:
271:
272: static int
273: man_ptext(struct man *m, int line, char *buf)
274: {
275:
276: if ( ! man_word_alloc(m, line, 0, buf))
277: return(0);
278: m->next = MAN_NEXT_SIBLING;
1.11 kristaps 279:
280: /*
281: * If this is one of the zany NLINE macros that consumes the
282: * next line of input as being influenced, then close out the
283: * existing macro "scope" and continue processing.
284: */
285:
286: if ( ! (MAN_NLINE & m->flags))
287: return(1);
288:
289: m->flags &= ~MAN_NLINE;
290: m->last = m->last->parent;
291:
292: assert(MAN_ROOT != m->last->type);
293: if ( ! man_valid_post(m))
294: return(0);
295: if ( ! man_action_post(m))
296: return(0);
297:
1.1 kristaps 298: return(1);
299: }
300:
301:
302: int
303: man_pmacro(struct man *m, int ln, char *buf)
304: {
1.11 kristaps 305: int i, j, c, ppos, fl;
1.1 kristaps 306: char mac[5];
1.11 kristaps 307: struct man_node *n;
1.1 kristaps 308:
309: /* Comments and empties are quickly ignored. */
310:
1.11 kristaps 311: n = m->last;
312: fl = MAN_NLINE & m->flags;
313:
1.1 kristaps 314: if (0 == buf[1])
1.11 kristaps 315: goto out;
1.1 kristaps 316:
1.9 kristaps 317: i = 1;
318:
319: if (' ' == buf[i]) {
320: i++;
1.1 kristaps 321: while (buf[i] && ' ' == buf[i])
322: i++;
323: if (0 == buf[i])
1.11 kristaps 324: goto out;
1.1 kristaps 325: }
326:
1.10 kristaps 327: ppos = i;
328:
1.1 kristaps 329: /* Copy the first word into a nil-terminated buffer. */
330:
1.10 kristaps 331: for (j = 0; j < 4; j++, i++) {
332: if (0 == (mac[j] = buf[i]))
1.1 kristaps 333: break;
1.10 kristaps 334: else if (' ' == buf[i])
1.1 kristaps 335: break;
336: }
337:
1.9 kristaps 338: mac[j] = 0;
1.1 kristaps 339:
1.9 kristaps 340: if (j == 4 || j < 1) {
1.7 kristaps 341: if ( ! (MAN_IGN_MACRO & m->pflags)) {
1.10 kristaps 342: (void)man_verr(m, ln, ppos,
1.7 kristaps 343: "ill-formed macro: %s", mac);
344: goto err;
345: }
1.10 kristaps 346: if ( ! man_vwarn(m, ln, ppos,
347: "ill-formed macro: %s", mac))
1.7 kristaps 348: goto err;
1.12 kristaps 349: return(1);
1.7 kristaps 350: }
1.1 kristaps 351:
352: if (MAN_MAX == (c = man_hash_find(m->htab, mac))) {
1.7 kristaps 353: if ( ! (MAN_IGN_MACRO & m->pflags)) {
1.10 kristaps 354: (void)man_verr(m, ln, ppos,
1.7 kristaps 355: "unknown macro: %s", mac);
356: goto err;
357: }
1.10 kristaps 358: if ( ! man_vwarn(m, ln, ppos,
359: "unknown macro: %s", mac))
1.7 kristaps 360: goto err;
1.12 kristaps 361: return(1);
1.1 kristaps 362: }
363:
364: /* The macro is sane. Jump to the next word. */
365:
366: while (buf[i] && ' ' == buf[i])
367: i++;
368:
369: /* Begin recursive parse sequence. */
370:
1.10 kristaps 371: if ( ! man_macro(m, c, ln, ppos, &i, buf))
1.1 kristaps 372: goto err;
373:
1.11 kristaps 374: out:
375: if (fl) {
376: /*
377: * A NLINE macro has been immediately followed with
1.17 kristaps 378: * another. Close out the preceding macro's scope, and
1.11 kristaps 379: * continue.
380: */
381: assert(MAN_ROOT != m->last->type);
382: assert(m->last->parent);
383: assert(MAN_ROOT != m->last->parent->type);
384:
385: if (n != m->last)
386: m->last = m->last->parent;
387:
388: if ( ! man_valid_post(m))
389: return(0);
390: if ( ! man_action_post(m))
391: return(0);
392: m->next = MAN_NEXT_SIBLING;
393: m->flags &= ~MAN_NLINE;
394: }
395:
1.1 kristaps 396: return(1);
397:
398: err: /* Error out. */
399:
1.2 kristaps 400: m->flags |= MAN_HALT;
1.1 kristaps 401: return(0);
402: }
1.3 kristaps 403:
1.4 kristaps 404:
405: int
406: man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
407: {
408: char buf[256];
409: va_list ap;
410:
411: if (NULL == man->cb.man_err)
412: return(0);
413:
414: va_start(ap, fmt);
415: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
416: va_end(ap);
417: return((*man->cb.man_err)(man->data, ln, pos, buf));
418: }
419:
420:
421: int
422: man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
423: {
424: char buf[256];
425: va_list ap;
426:
427: if (NULL == man->cb.man_warn)
428: return(0);
429:
430: va_start(ap, fmt);
431: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
432: va_end(ap);
433: return((*man->cb.man_warn)(man->data, ln, pos, buf));
434: }
435:
436:
1.23 ! kristaps 437: int
! 438: man_err(struct man *m, int line, int pos,
! 439: int iserr, enum merr type)
! 440: {
! 441: const char *p;
! 442:
! 443: p = NULL;
! 444: switch (type) {
! 445: case (WNPRINT):
! 446: p = "invalid character";
! 447: break;
! 448: case (WNMEM):
! 449: p = "memory exhausted";
! 450: break;
! 451: case (WMSEC):
! 452: p = "invalid manual section";
! 453: break;
! 454: case (WDATE):
! 455: p = "invalid date format";
! 456: break;
! 457: case (WLNSCOPE):
! 458: p = "scope of prior line violated";
! 459: break;
! 460: case (WTSPACE):
! 461: p = "trailing whitespace at end of line";
! 462: break;
! 463: case (WTQUOTE):
! 464: p = "unterminated quotation";
! 465: break;
! 466: }
! 467: assert(p);
! 468:
! 469: if (iserr)
! 470: return(man_verr(m, line, pos, p));
! 471:
! 472: return(man_vwarn(m, line, pos, p));
! 473: }
CVSweb