Annotation of mandoc/man_action.c, Revision 1.11
1.11 ! kristaps 1: /* $Id: man_action.c,v 1.10 2009/04/12 19:45:26 kristaps Exp $ */
1.1 kristaps 2: /*
1.11 ! kristaps 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.10 kristaps 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.10 kristaps 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 <errno.h>
21: #include <stdlib.h>
22: #include <string.h>
23:
24: #include "libman.h"
25:
1.3 kristaps 26: #ifdef __linux__
27: extern char *strptime(const char *, const char *, struct tm *);
28: #endif
1.1 kristaps 29:
30: struct actions {
31: int (*post)(struct man *);
32: };
33:
34:
1.2 kristaps 35: static int post_TH(struct man *);
1.5 kristaps 36: static time_t man_atotime(const char *);
1.2 kristaps 37:
1.1 kristaps 38: const struct actions man_actions[MAN_MAX] = {
39: { NULL }, /* __ */
1.2 kristaps 40: { post_TH }, /* TH */
1.1 kristaps 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 */
1.5 kristaps 59: { NULL }, /* RI */
1.6 kristaps 60: { NULL }, /* br */
1.8 kristaps 61: { NULL }, /* na */
1.9 kristaps 62: { NULL }, /* i */
1.1 kristaps 63: };
64:
65:
66: int
67: man_action_post(struct man *m)
68: {
69:
70: if (MAN_ACTED & m->last->flags)
71: return(1);
72: m->last->flags |= MAN_ACTED;
73:
74: switch (m->last->type) {
75: case (MAN_TEXT):
76: break;
77: case (MAN_ROOT):
78: break;
79: default:
80: if (NULL == man_actions[m->last->tok].post)
81: break;
82: return((*man_actions[m->last->tok].post)(m));
83: }
84: return(1);
85: }
86:
1.2 kristaps 87:
1.1 kristaps 88: static int
1.2 kristaps 89: post_TH(struct man *m)
1.1 kristaps 90: {
1.2 kristaps 91: struct man_node *n;
92: char *ep;
93: long lval;
1.1 kristaps 94:
95: if (m->meta.title)
96: free(m->meta.title);
97: if (m->meta.vol)
98: free(m->meta.vol);
1.2 kristaps 99: if (m->meta.source)
100: free(m->meta.source);
1.1 kristaps 101:
1.2 kristaps 102: m->meta.title = m->meta.vol = m->meta.source = NULL;
1.1 kristaps 103: m->meta.msec = 0;
1.2 kristaps 104: m->meta.date = 0;
105:
106: /* ->TITLE<- MSEC DATE SOURCE VOL */
107:
108: n = m->last->child;
109: assert(n);
1.1 kristaps 110:
1.2 kristaps 111: if (NULL == (m->meta.title = strdup(n->string)))
1.7 kristaps 112: return(man_verr(m, n->line, n->pos,
113: "memory exhausted"));
1.2 kristaps 114:
115: /* TITLE ->MSEC<- DATE SOURCE VOL */
1.1 kristaps 116:
1.2 kristaps 117: n = n->next;
118: assert(n);
1.1 kristaps 119:
1.2 kristaps 120: errno = 0;
121: lval = strtol(n->string, &ep, 10);
122: if (n->string[0] != '\0' && *ep == '\0')
123: m->meta.msec = (int)lval;
124: else if ( ! man_vwarn(m, n->line, n->pos, "invalid section"))
125: return(0);
1.1 kristaps 126:
1.2 kristaps 127: /* TITLE MSEC ->DATE<- SOURCE VOL */
1.1 kristaps 128:
1.5 kristaps 129: if (NULL == (n = n->next))
1.2 kristaps 130: m->meta.date = time(NULL);
1.5 kristaps 131: else if (0 == (m->meta.date = man_atotime(n->string))) {
1.2 kristaps 132: if ( ! man_vwarn(m, n->line, n->pos, "invalid date"))
133: return(0);
134: m->meta.date = time(NULL);
135: }
1.1 kristaps 136:
1.2 kristaps 137: /* TITLE MSEC DATE ->SOURCE<- VOL */
1.1 kristaps 138:
1.5 kristaps 139: if (n && (n = n->next))
1.2 kristaps 140: if (NULL == (m->meta.source = strdup(n->string)))
1.7 kristaps 141: return(man_verr(m, n->line, n->pos,
142: "memory exhausted"));
1.1 kristaps 143:
1.2 kristaps 144: /* TITLE MSEC DATE SOURCE ->VOL<- */
1.1 kristaps 145:
1.5 kristaps 146: if (n && (n = n->next))
1.2 kristaps 147: if (NULL == (m->meta.vol = strdup(n->string)))
1.7 kristaps 148: return(man_verr(m, n->line, n->pos,
149: "memory exhausted"));
1.1 kristaps 150:
151: /*
152: * The end document shouldn't have the prologue macros as part
153: * of the syntax tree (they encompass only meta-data).
154: */
155:
1.4 kristaps 156: if (m->last->parent->child == m->last) {
157: assert(MAN_ROOT == m->last->parent->type);
158: m->last->parent->child = NULL;
159: n = m->last;
160: m->last = m->last->parent;
161: m->next = MAN_NEXT_CHILD;
162: assert(m->last == m->first);
163: } else {
164: assert(m->last->prev);
165: m->last->prev->next = NULL;
166: n = m->last;
167: m->last = m->last->prev;
168: m->next = MAN_NEXT_SIBLING;
169: }
1.2 kristaps 170:
171: man_node_freelist(n);
172: return(1);
173: }
1.1 kristaps 174:
175:
1.2 kristaps 176: static time_t
177: man_atotime(const char *p)
178: {
179: struct tm tm;
180: char *pp;
181:
182: (void)memset(&tm, 0, sizeof(struct tm));
183:
184: if ((pp = strptime(p, "%b %d %Y", &tm)) && 0 == *pp)
185: return(mktime(&tm));
186: if ((pp = strptime(p, "%d %b %Y", &tm)) && 0 == *pp)
187: return(mktime(&tm));
188: if ((pp = strptime(p, "%b %d, %Y", &tm)) && 0 == *pp)
189: return(mktime(&tm));
190: if ((pp = strptime(p, "%b %Y", &tm)) && 0 == *pp)
191: return(mktime(&tm));
1.1 kristaps 192:
1.2 kristaps 193: return(0);
1.1 kristaps 194: }
CVSweb