Annotation of mandoc/man.c, Revision 1.16
1.16 ! kristaps 1: /* $Id: man.c,v 1.15 2009/04/03 11:08:39 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
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
7: * above copyright notice and this permission notice appear in all
8: * copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12: * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13: * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16: * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17: * PERFORMANCE OF THIS SOFTWARE.
18: */
19: #include <assert.h>
20: #include <ctype.h>
21: #include <stdarg.h>
22: #include <stdlib.h>
23: #include <stdio.h>
24: #include <string.h>
25:
26: #include "libman.h"
27:
28: const char *const __man_macronames[MAN_MAX] = {
29: "\\\"", "TH", "SH", "SS",
30: "TP", "LP", "PP", "P",
31: "IP", "HP", "SM", "SB",
32: "BI", "IB", "BR", "RB",
1.11 kristaps 33: "R", "B", "I", "IR",
1.16 ! kristaps 34: "RI", "br", "na", "i"
1.1 kristaps 35: };
36:
37: const char * const *man_macronames = __man_macronames;
38:
1.16 ! kristaps 39: static struct man_node *man_node_alloc(int, int,
! 40: enum man_type, int);
1.1 kristaps 41: static int man_node_append(struct man *,
42: struct man_node *);
43: static int man_ptext(struct man *, int, char *);
44: static int man_pmacro(struct man *, int, char *);
1.2 kristaps 45: static void man_free1(struct man *);
1.16 ! kristaps 46: static int man_alloc1(struct man *);
1.1 kristaps 47:
48:
49: const struct man_node *
1.2 kristaps 50: man_node(const struct man *m)
1.1 kristaps 51: {
52:
1.2 kristaps 53: return(MAN_HALT & m->flags ? NULL : m->first);
1.1 kristaps 54: }
55:
56:
57: const struct man_meta *
1.2 kristaps 58: man_meta(const struct man *m)
1.1 kristaps 59: {
60:
1.2 kristaps 61: return(MAN_HALT & m->flags ? NULL : &m->meta);
1.1 kristaps 62: }
63:
64:
1.15 kristaps 65: int
1.1 kristaps 66: man_reset(struct man *man)
67: {
68:
1.2 kristaps 69: man_free1(man);
1.16 ! kristaps 70: return(man_alloc1(man));
1.1 kristaps 71: }
72:
73:
74: void
75: man_free(struct man *man)
76: {
77:
1.2 kristaps 78: man_free1(man);
79:
1.1 kristaps 80: if (man->htab)
81: man_hash_free(man->htab);
82: free(man);
83: }
84:
85:
86: struct man *
1.7 kristaps 87: man_alloc(void *data, int pflags, const struct man_cb *cb)
1.1 kristaps 88: {
89: struct man *p;
90:
1.16 ! kristaps 91: if (NULL == (p = calloc(1, sizeof(struct man))))
! 92: return(NULL);
1.2 kristaps 93:
1.16 ! kristaps 94: if ( ! man_alloc1(p)) {
! 95: free(p);
! 96: return(NULL);
! 97: }
1.1 kristaps 98:
1.4 kristaps 99: p->data = data;
1.7 kristaps 100: p->pflags = pflags;
1.16 ! kristaps 101: (void)memcpy(&p->cb, cb, sizeof(struct man_cb));
1.7 kristaps 102:
1.16 ! kristaps 103: if (NULL == (p->htab = man_hash_alloc())) {
! 104: free(p);
! 105: return(NULL);
! 106: }
1.1 kristaps 107: return(p);
108: }
109:
110:
111: int
112: man_endparse(struct man *m)
113: {
114:
1.3 kristaps 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);
1.1 kristaps 121: }
122:
123:
124: int
125: man_parseln(struct man *m, int ln, char *buf)
126: {
127:
128: return('.' == *buf ?
129: man_pmacro(m, ln, buf) :
130: man_ptext(m, ln, buf));
131: }
132:
133:
1.2 kristaps 134: static void
135: man_free1(struct man *man)
136: {
137:
138: if (man->first)
139: man_node_freelist(man->first);
140: if (man->meta.title)
141: free(man->meta.title);
1.6 kristaps 142: if (man->meta.source)
143: free(man->meta.source);
1.2 kristaps 144: if (man->meta.vol)
145: free(man->meta.vol);
146: }
147:
148:
1.16 ! kristaps 149: static int
1.2 kristaps 150: man_alloc1(struct man *m)
151: {
152:
153: bzero(&m->meta, sizeof(struct man_meta));
154: m->flags = 0;
155: m->last = calloc(1, sizeof(struct man_node));
156: if (NULL == m->last)
1.16 ! kristaps 157: return(0);
1.2 kristaps 158: m->first = m->last;
159: m->last->type = MAN_ROOT;
160: m->next = MAN_NEXT_CHILD;
1.16 ! kristaps 161: return(1);
1.2 kristaps 162: }
163:
164:
1.1 kristaps 165: static int
166: man_node_append(struct man *man, struct man_node *p)
167: {
168:
169: assert(man->last);
170: assert(man->first);
171: assert(MAN_ROOT != p->type);
172:
173: switch (man->next) {
174: case (MAN_NEXT_SIBLING):
175: man->last->next = p;
176: p->prev = man->last;
177: p->parent = man->last->parent;
178: break;
179: case (MAN_NEXT_CHILD):
180: man->last->child = p;
181: p->parent = man->last;
182: break;
183: default:
184: abort();
185: /* NOTREACHED */
186: }
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.9 kristaps 329: if (buf[i] && '\\' == buf[i])
330: if (buf[i + 1] && '\"' == buf[i + 1])
1.11 kristaps 331: goto out;
1.1 kristaps 332:
333: /* Copy the first word into a nil-terminated buffer. */
334:
1.10 kristaps 335: for (j = 0; j < 4; j++, i++) {
336: if (0 == (mac[j] = buf[i]))
1.1 kristaps 337: break;
1.10 kristaps 338: else if (' ' == buf[i])
1.1 kristaps 339: break;
340: }
341:
1.9 kristaps 342: mac[j] = 0;
1.1 kristaps 343:
1.9 kristaps 344: if (j == 4 || j < 1) {
1.7 kristaps 345: if ( ! (MAN_IGN_MACRO & m->pflags)) {
1.10 kristaps 346: (void)man_verr(m, ln, ppos,
1.7 kristaps 347: "ill-formed macro: %s", mac);
348: goto err;
349: }
1.10 kristaps 350: if ( ! man_vwarn(m, ln, ppos,
351: "ill-formed macro: %s", mac))
1.7 kristaps 352: goto err;
1.12 kristaps 353: return(1);
1.7 kristaps 354: }
1.1 kristaps 355:
356: if (MAN_MAX == (c = man_hash_find(m->htab, mac))) {
1.7 kristaps 357: if ( ! (MAN_IGN_MACRO & m->pflags)) {
1.10 kristaps 358: (void)man_verr(m, ln, ppos,
1.7 kristaps 359: "unknown macro: %s", mac);
360: goto err;
361: }
1.10 kristaps 362: if ( ! man_vwarn(m, ln, ppos,
363: "unknown macro: %s", mac))
1.7 kristaps 364: goto err;
1.12 kristaps 365: return(1);
1.1 kristaps 366: }
367:
368: /* The macro is sane. Jump to the next word. */
369:
370: while (buf[i] && ' ' == buf[i])
371: i++;
372:
373: /* Begin recursive parse sequence. */
374:
1.10 kristaps 375: if ( ! man_macro(m, c, ln, ppos, &i, buf))
1.1 kristaps 376: goto err;
377:
1.11 kristaps 378: out:
379: if (fl) {
380: /*
381: * A NLINE macro has been immediately followed with
382: * another. Close out the preceeding macro's scope, and
383: * continue.
384: */
385: assert(MAN_ROOT != m->last->type);
386: assert(m->last->parent);
387: assert(MAN_ROOT != m->last->parent->type);
388:
389: if (n != m->last)
390: m->last = m->last->parent;
391:
392: if ( ! man_valid_post(m))
393: return(0);
394: if ( ! man_action_post(m))
395: return(0);
396: m->next = MAN_NEXT_SIBLING;
397: m->flags &= ~MAN_NLINE;
398: }
399:
1.1 kristaps 400: return(1);
401:
402: err: /* Error out. */
403:
1.2 kristaps 404: m->flags |= MAN_HALT;
1.1 kristaps 405: return(0);
406: }
1.3 kristaps 407:
1.4 kristaps 408:
409: int
410: man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
411: {
412: char buf[256];
413: va_list ap;
414:
415: if (NULL == man->cb.man_err)
416: return(0);
417:
418: va_start(ap, fmt);
419: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
420: va_end(ap);
421: return((*man->cb.man_err)(man->data, ln, pos, buf));
422: }
423:
424:
425: int
426: man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
427: {
428: char buf[256];
429: va_list ap;
430:
431: if (NULL == man->cb.man_warn)
432: return(0);
433:
434: va_start(ap, fmt);
435: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
436: va_end(ap);
437: return((*man->cb.man_warn)(man->data, ln, pos, buf));
438: }
439:
440:
CVSweb