Annotation of src/usr.bin/mandoc/man_action.c, Revision 1.4
1.4 ! schwarze 1: /* $Id: man_action.c,v 1.3 2009/06/18 23:34:53 schwarze Exp $ */
1.1 kristaps 2: /*
1.2 schwarze 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.2 schwarze 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.2 schwarze 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 <sys/utsname.h>
18:
19: #include <assert.h>
20: #include <errno.h>
21: #include <stdlib.h>
22: #include <string.h>
23:
24: #include "libman.h"
25:
26: struct actions {
27: int (*post)(struct man *);
28: };
29:
30:
31: static int post_TH(struct man *);
32: static time_t man_atotime(const char *);
33:
34: const struct actions man_actions[MAN_MAX] = {
1.3 schwarze 35: { NULL }, /* br */
1.1 kristaps 36: { post_TH }, /* TH */
37: { NULL }, /* SH */
38: { NULL }, /* SS */
39: { NULL }, /* TP */
40: { NULL }, /* LP */
41: { NULL }, /* PP */
42: { NULL }, /* P */
43: { NULL }, /* IP */
44: { NULL }, /* HP */
45: { NULL }, /* SM */
46: { NULL }, /* SB */
47: { NULL }, /* BI */
48: { NULL }, /* IB */
49: { NULL }, /* BR */
50: { NULL }, /* RB */
51: { NULL }, /* R */
52: { NULL }, /* B */
53: { NULL }, /* I */
54: { NULL }, /* IR */
55: { NULL }, /* RI */
56: { NULL }, /* na */
57: { NULL }, /* i */
58: };
59:
60:
61: int
62: man_action_post(struct man *m)
63: {
64:
65: if (MAN_ACTED & m->last->flags)
66: return(1);
67: m->last->flags |= MAN_ACTED;
68:
69: switch (m->last->type) {
70: case (MAN_TEXT):
71: break;
72: case (MAN_ROOT):
73: break;
74: default:
75: if (NULL == man_actions[m->last->tok].post)
76: break;
77: return((*man_actions[m->last->tok].post)(m));
78: }
79: return(1);
80: }
81:
82:
83: static int
84: post_TH(struct man *m)
85: {
86: struct man_node *n;
87: char *ep;
88: long lval;
89:
90: if (m->meta.title)
91: free(m->meta.title);
92: if (m->meta.vol)
93: free(m->meta.vol);
94: if (m->meta.source)
95: free(m->meta.source);
96:
97: m->meta.title = m->meta.vol = m->meta.source = NULL;
98: m->meta.msec = 0;
99: m->meta.date = 0;
100:
101: /* ->TITLE<- MSEC DATE SOURCE VOL */
102:
103: n = m->last->child;
104: assert(n);
105:
106: if (NULL == (m->meta.title = strdup(n->string)))
1.4 ! schwarze 107: return(man_nerr(m, n, WNMEM));
1.1 kristaps 108:
109: /* TITLE ->MSEC<- DATE SOURCE VOL */
110:
111: n = n->next;
112: assert(n);
113:
114: errno = 0;
115: lval = strtol(n->string, &ep, 10);
116: if (n->string[0] != '\0' && *ep == '\0')
117: m->meta.msec = (int)lval;
1.4 ! schwarze 118: else if ( ! man_nwarn(m, n, WMSEC))
1.1 kristaps 119: return(0);
120:
121: /* TITLE MSEC ->DATE<- SOURCE VOL */
122:
123: if (NULL == (n = n->next))
124: m->meta.date = time(NULL);
125: else if (0 == (m->meta.date = man_atotime(n->string))) {
1.4 ! schwarze 126: if ( ! man_nwarn(m, n, WDATE))
1.1 kristaps 127: return(0);
128: m->meta.date = time(NULL);
129: }
130:
131: /* TITLE MSEC DATE ->SOURCE<- VOL */
132:
133: if (n && (n = n->next))
134: if (NULL == (m->meta.source = strdup(n->string)))
1.4 ! schwarze 135: return(man_nerr(m, n, WNMEM));
1.1 kristaps 136:
137: /* TITLE MSEC DATE SOURCE ->VOL<- */
138:
139: if (n && (n = n->next))
140: if (NULL == (m->meta.vol = strdup(n->string)))
1.4 ! schwarze 141: return(man_nerr(m, n, WNMEM));
1.1 kristaps 142:
143: /*
144: * The end document shouldn't have the prologue macros as part
145: * of the syntax tree (they encompass only meta-data).
146: */
147:
148: if (m->last->parent->child == m->last) {
149: assert(MAN_ROOT == m->last->parent->type);
150: m->last->parent->child = NULL;
151: n = m->last;
152: m->last = m->last->parent;
153: m->next = MAN_NEXT_CHILD;
154: assert(m->last == m->first);
155: } else {
156: assert(m->last->prev);
157: m->last->prev->next = NULL;
158: n = m->last;
159: m->last = m->last->prev;
160: m->next = MAN_NEXT_SIBLING;
161: }
162:
163: man_node_freelist(n);
164: return(1);
165: }
166:
167:
168: static time_t
169: man_atotime(const char *p)
170: {
171: struct tm tm;
172: char *pp;
173:
1.4 ! schwarze 174: bzero(&tm, sizeof(struct tm));
1.1 kristaps 175:
176: if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
177: return(mktime(&tm));
178: if ((pp = strptime(p, "%d %b %Y", &tm)) && 0 == *pp)
179: return(mktime(&tm));
180: if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
181: return(mktime(&tm));
182: if ((pp = strptime(p, "%b %Y", &tm)) && 0 == *pp)
183: return(mktime(&tm));
184:
185: return(0);
186: }