]> git.cameronkatri.com Git - mandoc.git/blob - man_action.c
7e3cde24a0ae2fca3050ff4636edc81afc983f02
[mandoc.git] / man_action.c
1 /* $Id: man_action.c,v 1.27 2010/03/23 11:30:48 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 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/utsname.h>
22
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "libman.h"
28 #include "libmandoc.h"
29
30 struct actions {
31 int (*post)(struct man *);
32 };
33
34 static int post_TH(struct man *);
35 static int post_fi(struct man *);
36 static int post_nf(struct man *);
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 { NULL }, /* sp */
63 { post_nf }, /* nf */
64 { post_fi }, /* fi */
65 { NULL }, /* r */
66 { NULL }, /* RE */
67 { NULL }, /* RS */
68 { NULL }, /* DT */
69 { NULL }, /* UC */
70 { NULL }, /* PD */
71 { NULL }, /* Sp */
72 { post_nf }, /* Vb */
73 { post_fi }, /* Ve */
74 };
75
76
77 int
78 man_action_post(struct man *m)
79 {
80
81 if (MAN_ACTED & m->last->flags)
82 return(1);
83 m->last->flags |= MAN_ACTED;
84
85 switch (m->last->type) {
86 case (MAN_TEXT):
87 /* FALLTHROUGH */
88 case (MAN_ROOT):
89 return(1);
90 default:
91 break;
92 }
93
94 if (NULL == man_actions[m->last->tok].post)
95 return(1);
96 return((*man_actions[m->last->tok].post)(m));
97 }
98
99
100 static int
101 post_fi(struct man *m)
102 {
103
104 if ( ! (MAN_LITERAL & m->flags))
105 if ( ! man_nwarn(m, m->last, WNLITERAL))
106 return(0);
107 m->flags &= ~MAN_LITERAL;
108 return(1);
109 }
110
111
112 static int
113 post_nf(struct man *m)
114 {
115
116 if (MAN_LITERAL & m->flags)
117 if ( ! man_nwarn(m, m->last, WOLITERAL))
118 return(0);
119 m->flags |= MAN_LITERAL;
120 return(1);
121 }
122
123
124 static int
125 post_TH(struct man *m)
126 {
127 struct man_node *n;
128 char *ep;
129 long lval;
130
131 if (m->meta.title)
132 free(m->meta.title);
133 if (m->meta.vol)
134 free(m->meta.vol);
135 if (m->meta.source)
136 free(m->meta.source);
137
138 m->meta.title = m->meta.vol = m->meta.source = NULL;
139 m->meta.msec = 0;
140 m->meta.date = 0;
141
142 /* ->TITLE<- MSEC DATE SOURCE VOL */
143
144 n = m->last->child;
145 assert(n);
146 m->meta.title = mandoc_strdup(n->string);
147
148 /* TITLE ->MSEC<- DATE SOURCE VOL */
149
150 n = n->next;
151 assert(n);
152
153 lval = strtol(n->string, &ep, 10);
154 if (n->string[0] != '\0' && *ep == '\0')
155 m->meta.msec = (int)lval;
156 else if ( ! man_nwarn(m, n, WMSEC))
157 return(0);
158
159 /* TITLE MSEC ->DATE<- SOURCE VOL */
160
161 n = n->next;
162 if (n) {
163 m->meta.date = mandoc_a2time
164 (MTIME_ISO_8601, n->string);
165
166 if (0 == m->meta.date) {
167 if ( ! man_nwarn(m, n, WDATE))
168 return(0);
169 m->meta.date = time(NULL);
170 }
171 } else
172 m->meta.date = time(NULL);
173
174 /* TITLE MSEC DATE ->SOURCE<- VOL */
175
176 if (n && (n = n->next))
177 m->meta.source = mandoc_strdup(n->string);
178
179 /* TITLE MSEC DATE SOURCE ->VOL<- */
180
181 if (n && (n = n->next))
182 m->meta.vol = mandoc_strdup(n->string);
183
184 n = m->last;
185 man_node_unlink(m, n);
186 man_node_freelist(n);
187 return(1);
188 }