Annotation of mandoc/man_action.c, Revision 1.9
1.9 ! kristaps 1: /* $Id: man_action.c,v 1.8 2009/04/02 06:51:44 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
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
7: * above copyright notice and this permission notice appear in all
8: * copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12: * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13: * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16: * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17: * PERFORMANCE OF THIS SOFTWARE.
18: */
19: #include <sys/utsname.h>
20:
21: #include <assert.h>
22: #include <errno.h>
23: #include <stdlib.h>
24: #include <string.h>
25:
26: #include "libman.h"
27:
1.3 kristaps 28: #ifdef __linux__
29: extern char *strptime(const char *, const char *, struct tm *);
30: #endif
1.1 kristaps 31:
32: struct actions {
33: int (*post)(struct man *);
34: };
35:
36:
1.2 kristaps 37: static int post_TH(struct man *);
1.5 kristaps 38: static time_t man_atotime(const char *);
1.2 kristaps 39:
1.1 kristaps 40: const struct actions man_actions[MAN_MAX] = {
41: { NULL }, /* __ */
1.2 kristaps 42: { post_TH }, /* TH */
1.1 kristaps 43: { NULL }, /* SH */
44: { NULL }, /* SS */
45: { NULL }, /* TP */
46: { NULL }, /* LP */
47: { NULL }, /* PP */
48: { NULL }, /* P */
49: { NULL }, /* IP */
50: { NULL }, /* HP */
51: { NULL }, /* SM */
52: { NULL }, /* SB */
53: { NULL }, /* BI */
54: { NULL }, /* IB */
55: { NULL }, /* BR */
56: { NULL }, /* RB */
57: { NULL }, /* R */
58: { NULL }, /* B */
59: { NULL }, /* I */
60: { NULL }, /* IR */
1.5 kristaps 61: { NULL }, /* RI */
1.6 kristaps 62: { NULL }, /* br */
1.8 kristaps 63: { NULL }, /* na */
1.9 ! kristaps 64: { NULL }, /* i */
1.1 kristaps 65: };
66:
67:
68: int
69: man_action_post(struct man *m)
70: {
71:
72: if (MAN_ACTED & m->last->flags)
73: return(1);
74: m->last->flags |= MAN_ACTED;
75:
76: switch (m->last->type) {
77: case (MAN_TEXT):
78: break;
79: case (MAN_ROOT):
80: break;
81: default:
82: if (NULL == man_actions[m->last->tok].post)
83: break;
84: return((*man_actions[m->last->tok].post)(m));
85: }
86: return(1);
87: }
88:
1.2 kristaps 89:
1.1 kristaps 90: static int
1.2 kristaps 91: post_TH(struct man *m)
1.1 kristaps 92: {
1.2 kristaps 93: struct man_node *n;
94: char *ep;
95: long lval;
1.1 kristaps 96:
97: if (m->meta.title)
98: free(m->meta.title);
99: if (m->meta.vol)
100: free(m->meta.vol);
1.2 kristaps 101: if (m->meta.source)
102: free(m->meta.source);
1.1 kristaps 103:
1.2 kristaps 104: m->meta.title = m->meta.vol = m->meta.source = NULL;
1.1 kristaps 105: m->meta.msec = 0;
1.2 kristaps 106: m->meta.date = 0;
107:
108: /* ->TITLE<- MSEC DATE SOURCE VOL */
109:
110: n = m->last->child;
111: assert(n);
1.1 kristaps 112:
1.2 kristaps 113: if (NULL == (m->meta.title = strdup(n->string)))
1.7 kristaps 114: return(man_verr(m, n->line, n->pos,
115: "memory exhausted"));
1.2 kristaps 116:
117: /* TITLE ->MSEC<- DATE SOURCE VOL */
1.1 kristaps 118:
1.2 kristaps 119: n = n->next;
120: assert(n);
1.1 kristaps 121:
1.2 kristaps 122: errno = 0;
123: lval = strtol(n->string, &ep, 10);
124: if (n->string[0] != '\0' && *ep == '\0')
125: m->meta.msec = (int)lval;
126: else if ( ! man_vwarn(m, n->line, n->pos, "invalid section"))
127: return(0);
1.1 kristaps 128:
1.2 kristaps 129: /* TITLE MSEC ->DATE<- SOURCE VOL */
1.1 kristaps 130:
1.5 kristaps 131: if (NULL == (n = n->next))
1.2 kristaps 132: m->meta.date = time(NULL);
1.5 kristaps 133: else if (0 == (m->meta.date = man_atotime(n->string))) {
1.2 kristaps 134: if ( ! man_vwarn(m, n->line, n->pos, "invalid date"))
135: return(0);
136: m->meta.date = time(NULL);
137: }
1.1 kristaps 138:
1.2 kristaps 139: /* TITLE MSEC DATE ->SOURCE<- VOL */
1.1 kristaps 140:
1.5 kristaps 141: if (n && (n = n->next))
1.2 kristaps 142: if (NULL == (m->meta.source = strdup(n->string)))
1.7 kristaps 143: return(man_verr(m, n->line, n->pos,
144: "memory exhausted"));
1.1 kristaps 145:
1.2 kristaps 146: /* TITLE MSEC DATE SOURCE ->VOL<- */
1.1 kristaps 147:
1.5 kristaps 148: if (n && (n = n->next))
1.2 kristaps 149: if (NULL == (m->meta.vol = strdup(n->string)))
1.7 kristaps 150: return(man_verr(m, n->line, n->pos,
151: "memory exhausted"));
1.1 kristaps 152:
153: /*
154: * The end document shouldn't have the prologue macros as part
155: * of the syntax tree (they encompass only meta-data).
156: */
157:
1.4 kristaps 158: if (m->last->parent->child == m->last) {
159: assert(MAN_ROOT == m->last->parent->type);
160: m->last->parent->child = NULL;
161: n = m->last;
162: m->last = m->last->parent;
163: m->next = MAN_NEXT_CHILD;
164: assert(m->last == m->first);
165: } else {
166: assert(m->last->prev);
167: m->last->prev->next = NULL;
168: n = m->last;
169: m->last = m->last->prev;
170: m->next = MAN_NEXT_SIBLING;
171: }
1.2 kristaps 172:
173: man_node_freelist(n);
174: return(1);
175: }
1.1 kristaps 176:
177:
1.2 kristaps 178: static time_t
179: man_atotime(const char *p)
180: {
181: struct tm tm;
182: char *pp;
183:
184: (void)memset(&tm, 0, sizeof(struct tm));
185:
186: if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
187: return(mktime(&tm));
188: if ((pp = strptime(p, "%d %b %Y", &tm)) && 0 == *pp)
189: return(mktime(&tm));
190: if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
191: return(mktime(&tm));
192: if ((pp = strptime(p, "%b %Y", &tm)) && 0 == *pp)
193: return(mktime(&tm));
1.1 kristaps 194:
1.2 kristaps 195: return(0);
1.1 kristaps 196: }
CVSweb