Annotation of src/usr.bin/mandoc/man_action.c, Revision 1.2
1.2 ! schwarze 1: /* $Id: man_action.c,v 1.11 2009/06/10 20:18:43 kristaps 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] = {
35: { NULL }, /* __ */
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 }, /* br */
57: { NULL }, /* na */
58: { NULL }, /* i */
59: };
60:
61:
62: int
63: man_action_post(struct man *m)
64: {
65:
66: if (MAN_ACTED & m->last->flags)
67: return(1);
68: m->last->flags |= MAN_ACTED;
69:
70: switch (m->last->type) {
71: case (MAN_TEXT):
72: break;
73: case (MAN_ROOT):
74: break;
75: default:
76: if (NULL == man_actions[m->last->tok].post)
77: break;
78: return((*man_actions[m->last->tok].post)(m));
79: }
80: return(1);
81: }
82:
83:
84: static int
85: post_TH(struct man *m)
86: {
87: struct man_node *n;
88: char *ep;
89: long lval;
90:
91: if (m->meta.title)
92: free(m->meta.title);
93: if (m->meta.vol)
94: free(m->meta.vol);
95: if (m->meta.source)
96: free(m->meta.source);
97:
98: m->meta.title = m->meta.vol = m->meta.source = NULL;
99: m->meta.msec = 0;
100: m->meta.date = 0;
101:
102: /* ->TITLE<- MSEC DATE SOURCE VOL */
103:
104: n = m->last->child;
105: assert(n);
106:
107: if (NULL == (m->meta.title = strdup(n->string)))
108: return(man_verr(m, n->line, n->pos,
109: "memory exhausted"));
110:
111: /* TITLE ->MSEC<- DATE SOURCE VOL */
112:
113: n = n->next;
114: assert(n);
115:
116: errno = 0;
117: lval = strtol(n->string, &ep, 10);
118: if (n->string[0] != '\0' && *ep == '\0')
119: m->meta.msec = (int)lval;
120: else if ( ! man_vwarn(m, n->line, n->pos, "invalid section"))
121: return(0);
122:
123: /* TITLE MSEC ->DATE<- SOURCE VOL */
124:
125: if (NULL == (n = n->next))
126: m->meta.date = time(NULL);
127: else if (0 == (m->meta.date = man_atotime(n->string))) {
128: if ( ! man_vwarn(m, n->line, n->pos, "invalid date"))
129: return(0);
130: m->meta.date = time(NULL);
131: }
132:
133: /* TITLE MSEC DATE ->SOURCE<- VOL */
134:
135: if (n && (n = n->next))
136: if (NULL == (m->meta.source = strdup(n->string)))
137: return(man_verr(m, n->line, n->pos,
138: "memory exhausted"));
139:
140: /* TITLE MSEC DATE SOURCE ->VOL<- */
141:
142: if (n && (n = n->next))
143: if (NULL == (m->meta.vol = strdup(n->string)))
144: return(man_verr(m, n->line, n->pos,
145: "memory exhausted"));
146:
147: /*
148: * The end document shouldn't have the prologue macros as part
149: * of the syntax tree (they encompass only meta-data).
150: */
151:
152: if (m->last->parent->child == m->last) {
153: assert(MAN_ROOT == m->last->parent->type);
154: m->last->parent->child = NULL;
155: n = m->last;
156: m->last = m->last->parent;
157: m->next = MAN_NEXT_CHILD;
158: assert(m->last == m->first);
159: } else {
160: assert(m->last->prev);
161: m->last->prev->next = NULL;
162: n = m->last;
163: m->last = m->last->prev;
164: m->next = MAN_NEXT_SIBLING;
165: }
166:
167: man_node_freelist(n);
168: return(1);
169: }
170:
171:
172: static time_t
173: man_atotime(const char *p)
174: {
175: struct tm tm;
176: char *pp;
177:
178: (void)memset(&tm, 0, sizeof(struct tm));
179:
180: if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
181: return(mktime(&tm));
182: if ((pp = strptime(p, "%d %b %Y", &tm)) && 0 == *pp)
183: return(mktime(&tm));
184: if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
185: return(mktime(&tm));
186: if ((pp = strptime(p, "%b %Y", &tm)) && 0 == *pp)
187: return(mktime(&tm));
188:
189: return(0);
190: }