]> git.cameronkatri.com Git - mandoc.git/blob - man_action.c
Portability: replaced queue macros in html.c (Joerg Sonnenberger).
[mandoc.git] / man_action.c
1 /* $Id: man_action.c,v 1.20 2009/10/24 05:45:04 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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 above
7 * copyright notice and this permission notice appear in all copies.
8 *
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.
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 static int post_TH(struct man *);
31 static int post_fi(struct man *);
32 static int post_nf(struct man *);
33
34 const struct actions man_actions[MAN_MAX] = {
35 { NULL }, /* br */
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 { NULL }, /* sp */
59 { post_nf }, /* nf */
60 { post_fi }, /* fi */
61 { NULL }, /* r */
62 { NULL }, /* RE */
63 { NULL }, /* RS */
64 { NULL }, /* DT */
65 { NULL }, /* UC */
66 { NULL }, /* PD */
67 };
68
69 static time_t man_atotime(const char *);
70 #ifdef __linux__
71 extern char *strptime(const char *, const char *, struct tm *);
72 #endif
73
74
75 int
76 man_action_post(struct man *m)
77 {
78
79 if (MAN_ACTED & m->last->flags)
80 return(1);
81 m->last->flags |= MAN_ACTED;
82
83 switch (m->last->type) {
84 case (MAN_TEXT):
85 /* FALLTHROUGH */
86 case (MAN_ROOT):
87 return(1);
88 default:
89 break;
90 }
91
92 if (NULL == man_actions[m->last->tok].post)
93 return(1);
94 return((*man_actions[m->last->tok].post)(m));
95 }
96
97
98 static int
99 post_fi(struct man *m)
100 {
101
102 if ( ! (MAN_LITERAL & m->flags))
103 if ( ! man_nwarn(m, m->last, WNLITERAL))
104 return(0);
105 m->flags &= ~MAN_LITERAL;
106 return(1);
107 }
108
109
110 static int
111 post_nf(struct man *m)
112 {
113
114 if (MAN_LITERAL & m->flags)
115 if ( ! man_nwarn(m, m->last, WOLITERAL))
116 return(0);
117 m->flags |= MAN_LITERAL;
118 return(1);
119 }
120
121
122 static int
123 post_TH(struct man *m)
124 {
125 struct man_node *n;
126 char *ep;
127 long lval;
128
129 if (m->meta.title)
130 free(m->meta.title);
131 if (m->meta.vol)
132 free(m->meta.vol);
133 if (m->meta.source)
134 free(m->meta.source);
135
136 m->meta.title = m->meta.vol = m->meta.source = NULL;
137 m->meta.msec = 0;
138 m->meta.date = 0;
139
140 /* ->TITLE<- MSEC DATE SOURCE VOL */
141
142 n = m->last->child;
143 assert(n);
144
145 if (NULL == (m->meta.title = strdup(n->string)))
146 return(man_nerr(m, n, WNMEM));
147
148 /* TITLE ->MSEC<- DATE SOURCE VOL */
149
150 n = n->next;
151 assert(n);
152
153 errno = 0;
154 lval = strtol(n->string, &ep, 10);
155 if (n->string[0] != '\0' && *ep == '\0')
156 m->meta.msec = (int)lval;
157 else if ( ! man_nwarn(m, n, WMSEC))
158 return(0);
159
160 /* TITLE MSEC ->DATE<- SOURCE VOL */
161
162 if (NULL == (n = n->next))
163 m->meta.date = time(NULL);
164 else if (0 == (m->meta.date = man_atotime(n->string))) {
165 if ( ! man_nwarn(m, n, WDATE))
166 return(0);
167 m->meta.date = time(NULL);
168 }
169
170 /* TITLE MSEC DATE ->SOURCE<- VOL */
171
172 if (n && (n = n->next))
173 if (NULL == (m->meta.source = strdup(n->string)))
174 return(man_nerr(m, n, WNMEM));
175
176 /* TITLE MSEC DATE SOURCE ->VOL<- */
177
178 if (n && (n = n->next))
179 if (NULL == (m->meta.vol = strdup(n->string)))
180 return(man_nerr(m, n, WNMEM));
181
182 /*
183 * The end document shouldn't have the prologue macros as part
184 * of the syntax tree (they encompass only meta-data).
185 */
186
187 if (m->last->parent->child == m->last) {
188 m->last->parent->child = NULL;
189 n = m->last;
190 m->last = m->last->parent;
191 m->next = MAN_NEXT_CHILD;
192 } else {
193 assert(m->last->prev);
194 m->last->prev->next = NULL;
195 n = m->last;
196 m->last = m->last->prev;
197 m->next = MAN_NEXT_SIBLING;
198 }
199
200 man_node_freelist(n);
201 return(1);
202 }
203
204
205 static time_t
206 man_atotime(const char *p)
207 {
208 struct tm tm;
209 char *pp;
210
211 bzero(&tm, sizeof(struct tm));
212
213 if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
214 return(mktime(&tm));
215 if ((pp = strptime(p, "%d %b %Y", &tm)) && 0 == *pp)
216 return(mktime(&tm));
217 if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
218 return(mktime(&tm));
219 if ((pp = strptime(p, "%b %Y", &tm)) && 0 == *pp)
220 return(mktime(&tm));
221
222 return(0);
223 }