Annotation of src/usr.bin/mandoc/man_action.c, Revision 1.3
1.3 ! schwarze 1: /* $Id: man_action.c,v 1.2 2009/06/14 23:00:57 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)))
107: return(man_verr(m, n->line, n->pos,
108: "memory exhausted"));
109:
110: /* TITLE ->MSEC<- DATE SOURCE VOL */
111:
112: n = n->next;
113: assert(n);
114:
115: errno = 0;
116: lval = strtol(n->string, &ep, 10);
117: if (n->string[0] != '\0' && *ep == '\0')
118: m->meta.msec = (int)lval;
119: else if ( ! man_vwarn(m, n->line, n->pos, "invalid section"))
120: return(0);
121:
122: /* TITLE MSEC ->DATE<- SOURCE VOL */
123:
124: if (NULL == (n = n->next))
125: m->meta.date = time(NULL);
126: else if (0 == (m->meta.date = man_atotime(n->string))) {
127: if ( ! man_vwarn(m, n->line, n->pos, "invalid date"))
128: return(0);
129: m->meta.date = time(NULL);
130: }
131:
132: /* TITLE MSEC DATE ->SOURCE<- VOL */
133:
134: if (n && (n = n->next))
135: if (NULL == (m->meta.source = strdup(n->string)))
136: return(man_verr(m, n->line, n->pos,
137: "memory exhausted"));
138:
139: /* TITLE MSEC DATE SOURCE ->VOL<- */
140:
141: if (n && (n = n->next))
142: if (NULL == (m->meta.vol = strdup(n->string)))
143: return(man_verr(m, n->line, n->pos,
144: "memory exhausted"));
145:
146: /*
147: * The end document shouldn't have the prologue macros as part
148: * of the syntax tree (they encompass only meta-data).
149: */
150:
151: if (m->last->parent->child == m->last) {
152: assert(MAN_ROOT == m->last->parent->type);
153: m->last->parent->child = NULL;
154: n = m->last;
155: m->last = m->last->parent;
156: m->next = MAN_NEXT_CHILD;
157: assert(m->last == m->first);
158: } else {
159: assert(m->last->prev);
160: m->last->prev->next = NULL;
161: n = m->last;
162: m->last = m->last->prev;
163: m->next = MAN_NEXT_SIBLING;
164: }
165:
166: man_node_freelist(n);
167: return(1);
168: }
169:
170:
171: static time_t
172: man_atotime(const char *p)
173: {
174: struct tm tm;
175: char *pp;
176:
177: (void)memset(&tm, 0, sizeof(struct tm));
178:
179: if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
180: return(mktime(&tm));
181: if ((pp = strptime(p, "%d %b %Y", &tm)) && 0 == *pp)
182: return(mktime(&tm));
183: if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
184: return(mktime(&tm));
185: if ((pp = strptime(p, "%b %Y", &tm)) && 0 == *pp)
186: return(mktime(&tm));
187:
188: return(0);
189: }