Annotation of src/usr.bin/mandoc/man_action.c, Revision 1.12
1.12 ! schwarze 1: /* $Id: man_action.c,v 1.11 2009/12/23 22:30:17 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.12 ! schwarze 67: { NULL }, /* Sp */
! 68: { post_nf }, /* Vb */
! 69: { post_fi }, /* Ve */
1.1 kristaps 70: };
71:
72:
73: int
74: man_action_post(struct man *m)
75: {
76:
77: if (MAN_ACTED & m->last->flags)
78: return(1);
79: m->last->flags |= MAN_ACTED;
80:
81: switch (m->last->type) {
82: case (MAN_TEXT):
1.7 schwarze 83: /* FALLTHROUGH */
1.1 kristaps 84: case (MAN_ROOT):
1.7 schwarze 85: return(1);
86: default:
1.1 kristaps 87: break;
88: }
1.7 schwarze 89:
90: if (NULL == man_actions[m->last->tok].post)
91: return(1);
92: return((*man_actions[m->last->tok].post)(m));
93: }
94:
95:
96: static int
97: post_fi(struct man *m)
98: {
99:
100: if ( ! (MAN_LITERAL & m->flags))
101: if ( ! man_nwarn(m, m->last, WNLITERAL))
102: return(0);
103: m->flags &= ~MAN_LITERAL;
104: return(1);
105: }
106:
107:
108: static int
109: post_nf(struct man *m)
110: {
111:
112: if (MAN_LITERAL & m->flags)
113: if ( ! man_nwarn(m, m->last, WOLITERAL))
114: return(0);
115: m->flags |= MAN_LITERAL;
1.1 kristaps 116: return(1);
117: }
118:
119:
120: static int
121: post_TH(struct man *m)
122: {
123: struct man_node *n;
124: char *ep;
125: long lval;
126:
127: if (m->meta.title)
128: free(m->meta.title);
129: if (m->meta.vol)
130: free(m->meta.vol);
131: if (m->meta.source)
132: free(m->meta.source);
133:
134: m->meta.title = m->meta.vol = m->meta.source = NULL;
135: m->meta.msec = 0;
136: m->meta.date = 0;
137:
138: /* ->TITLE<- MSEC DATE SOURCE VOL */
139:
140: n = m->last->child;
141: assert(n);
1.10 schwarze 142: m->meta.title = mandoc_strdup(n->string);
1.1 kristaps 143:
144: /* TITLE ->MSEC<- DATE SOURCE VOL */
145:
146: n = n->next;
147: assert(n);
148:
149: lval = strtol(n->string, &ep, 10);
150: if (n->string[0] != '\0' && *ep == '\0')
151: m->meta.msec = (int)lval;
1.4 schwarze 152: else if ( ! man_nwarn(m, n, WMSEC))
1.1 kristaps 153: return(0);
154:
155: /* TITLE MSEC ->DATE<- SOURCE VOL */
156:
1.11 schwarze 157: n = n->next;
158: if (n) {
159: m->meta.date = mandoc_a2time
160: (MTIME_ISO_8601, n->string);
161:
162: if (0 == m->meta.date) {
163: if ( ! man_nwarn(m, n, WDATE))
164: return(0);
165: m->meta.date = time(NULL);
166: }
167: } else
1.1 kristaps 168: m->meta.date = time(NULL);
169:
170: /* TITLE MSEC DATE ->SOURCE<- VOL */
171:
172: if (n && (n = n->next))
1.10 schwarze 173: m->meta.source = mandoc_strdup(n->string);
1.1 kristaps 174:
175: /* TITLE MSEC DATE SOURCE ->VOL<- */
176:
177: if (n && (n = n->next))
1.10 schwarze 178: m->meta.vol = mandoc_strdup(n->string);
1.1 kristaps 179:
180: /*
181: * The end document shouldn't have the prologue macros as part
182: * of the syntax tree (they encompass only meta-data).
183: */
184:
185: if (m->last->parent->child == m->last) {
186: m->last->parent->child = NULL;
187: n = m->last;
188: m->last = m->last->parent;
189: m->next = MAN_NEXT_CHILD;
190: } else {
191: assert(m->last->prev);
192: m->last->prev->next = NULL;
193: n = m->last;
194: m->last = m->last->prev;
195: m->next = MAN_NEXT_SIBLING;
196: }
197:
198: man_node_freelist(n);
199: return(1);
200: }