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