Annotation of src/usr.bin/mandoc/man_action.c, Revision 1.10
1.10 ! schwarze 1: /* $Id: man_action.c,v 1.9 2009/10/27 21:40:07 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 <stdlib.h>
21: #include <string.h>
22:
23: #include "libman.h"
1.10 ! schwarze 24: #include "libmandoc.h"
1.1 kristaps 25:
26: struct actions {
27: int (*post)(struct man *);
28: };
29:
30: static int post_TH(struct man *);
1.7 schwarze 31: static int post_fi(struct man *);
32: static int post_nf(struct man *);
1.1 kristaps 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 */
1.5 schwarze 58: { NULL }, /* sp */
1.7 schwarze 59: { post_nf }, /* nf */
60: { post_fi }, /* fi */
61: { NULL }, /* r */
62: { NULL }, /* RE */
63: { NULL }, /* RS */
64: { NULL }, /* DT */
1.8 schwarze 65: { NULL }, /* UC */
1.9 schwarze 66: { NULL }, /* PD */
1.1 kristaps 67: };
68:
1.7 schwarze 69: static time_t man_atotime(const char *);
70:
1.1 kristaps 71:
72: int
73: man_action_post(struct man *m)
74: {
75:
76: if (MAN_ACTED & m->last->flags)
77: return(1);
78: m->last->flags |= MAN_ACTED;
79:
80: switch (m->last->type) {
81: case (MAN_TEXT):
1.7 schwarze 82: /* FALLTHROUGH */
1.1 kristaps 83: case (MAN_ROOT):
1.7 schwarze 84: return(1);
85: default:
1.1 kristaps 86: break;
87: }
1.7 schwarze 88:
89: if (NULL == man_actions[m->last->tok].post)
90: return(1);
91: return((*man_actions[m->last->tok].post)(m));
92: }
93:
94:
95: static int
96: post_fi(struct man *m)
97: {
98:
99: if ( ! (MAN_LITERAL & m->flags))
100: if ( ! man_nwarn(m, m->last, WNLITERAL))
101: return(0);
102: m->flags &= ~MAN_LITERAL;
103: return(1);
104: }
105:
106:
107: static int
108: post_nf(struct man *m)
109: {
110:
111: if (MAN_LITERAL & m->flags)
112: if ( ! man_nwarn(m, m->last, WOLITERAL))
113: return(0);
114: m->flags |= MAN_LITERAL;
1.1 kristaps 115: return(1);
116: }
117:
118:
119: static int
120: post_TH(struct man *m)
121: {
122: struct man_node *n;
123: char *ep;
124: long lval;
125:
126: if (m->meta.title)
127: free(m->meta.title);
128: if (m->meta.vol)
129: free(m->meta.vol);
130: if (m->meta.source)
131: free(m->meta.source);
132:
133: m->meta.title = m->meta.vol = m->meta.source = NULL;
134: m->meta.msec = 0;
135: m->meta.date = 0;
136:
137: /* ->TITLE<- MSEC DATE SOURCE VOL */
138:
139: n = m->last->child;
140: assert(n);
1.10 ! schwarze 141: m->meta.title = mandoc_strdup(n->string);
1.1 kristaps 142:
143: /* TITLE ->MSEC<- DATE SOURCE VOL */
144:
145: n = n->next;
146: assert(n);
147:
148: lval = strtol(n->string, &ep, 10);
149: if (n->string[0] != '\0' && *ep == '\0')
150: m->meta.msec = (int)lval;
1.4 schwarze 151: else if ( ! man_nwarn(m, n, WMSEC))
1.1 kristaps 152: return(0);
153:
154: /* TITLE MSEC ->DATE<- SOURCE VOL */
155:
156: if (NULL == (n = n->next))
157: m->meta.date = time(NULL);
158: else if (0 == (m->meta.date = man_atotime(n->string))) {
1.4 schwarze 159: if ( ! man_nwarn(m, n, WDATE))
1.1 kristaps 160: return(0);
161: m->meta.date = time(NULL);
162: }
163:
164: /* TITLE MSEC DATE ->SOURCE<- VOL */
165:
166: if (n && (n = n->next))
1.10 ! schwarze 167: m->meta.source = mandoc_strdup(n->string);
1.1 kristaps 168:
169: /* TITLE MSEC DATE SOURCE ->VOL<- */
170:
171: if (n && (n = n->next))
1.10 ! schwarze 172: m->meta.vol = mandoc_strdup(n->string);
1.1 kristaps 173:
174: /*
175: * The end document shouldn't have the prologue macros as part
176: * of the syntax tree (they encompass only meta-data).
177: */
178:
179: if (m->last->parent->child == m->last) {
180: m->last->parent->child = NULL;
181: n = m->last;
182: m->last = m->last->parent;
183: m->next = MAN_NEXT_CHILD;
184: } else {
185: assert(m->last->prev);
186: m->last->prev->next = NULL;
187: n = m->last;
188: m->last = m->last->prev;
189: m->next = MAN_NEXT_SIBLING;
190: }
191:
192: man_node_freelist(n);
193: return(1);
194: }
195:
196:
197: static time_t
198: man_atotime(const char *p)
199: {
200: struct tm tm;
201: char *pp;
202:
1.10 ! schwarze 203: memset(&tm, 0, sizeof(struct tm));
1.1 kristaps 204:
205: if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
206: return(mktime(&tm));
207: if ((pp = strptime(p, "%d %b %Y", &tm)) && 0 == *pp)
208: return(mktime(&tm));
209: if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
210: return(mktime(&tm));
211: if ((pp = strptime(p, "%b %Y", &tm)) && 0 == *pp)
212: return(mktime(&tm));
213:
214: return(0);
215: }