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