mdoc_state.c revision 1.1.1.1.2.3 1 1.1.1.1.2.3 pgoyette /* Id: mdoc_state.c,v 1.4 2017/01/10 13:47:00 schwarze Exp */
2 1.1.1.1.2.2 pgoyette /*
3 1.1.1.1.2.2 pgoyette * Copyright (c) 2014, 2015 Ingo Schwarze <schwarze (at) openbsd.org>
4 1.1.1.1.2.2 pgoyette *
5 1.1.1.1.2.2 pgoyette * Permission to use, copy, modify, and distribute this software for any
6 1.1.1.1.2.2 pgoyette * purpose with or without fee is hereby granted, provided that the above
7 1.1.1.1.2.2 pgoyette * copyright notice and this permission notice appear in all copies.
8 1.1.1.1.2.2 pgoyette *
9 1.1.1.1.2.2 pgoyette * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 1.1.1.1.2.2 pgoyette * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 1.1.1.1.2.2 pgoyette * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 1.1.1.1.2.2 pgoyette * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 1.1.1.1.2.2 pgoyette * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 1.1.1.1.2.2 pgoyette * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 1.1.1.1.2.2 pgoyette * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 1.1.1.1.2.2 pgoyette */
17 1.1.1.1.2.2 pgoyette #include <sys/types.h>
18 1.1.1.1.2.2 pgoyette
19 1.1.1.1.2.2 pgoyette #include <stdlib.h>
20 1.1.1.1.2.2 pgoyette #include <string.h>
21 1.1.1.1.2.2 pgoyette
22 1.1.1.1.2.2 pgoyette #include "mandoc.h"
23 1.1.1.1.2.2 pgoyette #include "roff.h"
24 1.1.1.1.2.2 pgoyette #include "mdoc.h"
25 1.1.1.1.2.2 pgoyette #include "libmandoc.h"
26 1.1.1.1.2.2 pgoyette #include "libmdoc.h"
27 1.1.1.1.2.2 pgoyette
28 1.1.1.1.2.2 pgoyette #define STATE_ARGS struct roff_man *mdoc, struct roff_node *n
29 1.1.1.1.2.2 pgoyette
30 1.1.1.1.2.2 pgoyette typedef void (*state_handler)(STATE_ARGS);
31 1.1.1.1.2.2 pgoyette
32 1.1.1.1.2.2 pgoyette static void state_bd(STATE_ARGS);
33 1.1.1.1.2.2 pgoyette static void state_bl(STATE_ARGS);
34 1.1.1.1.2.2 pgoyette static void state_dl(STATE_ARGS);
35 1.1.1.1.2.2 pgoyette static void state_sh(STATE_ARGS);
36 1.1.1.1.2.2 pgoyette static void state_sm(STATE_ARGS);
37 1.1.1.1.2.2 pgoyette
38 1.1.1.1.2.2 pgoyette static const state_handler state_handlers[MDOC_MAX] = {
39 1.1.1.1.2.2 pgoyette NULL, /* Ap */
40 1.1.1.1.2.2 pgoyette NULL, /* Dd */
41 1.1.1.1.2.2 pgoyette NULL, /* Dt */
42 1.1.1.1.2.2 pgoyette NULL, /* Os */
43 1.1.1.1.2.2 pgoyette state_sh, /* Sh */
44 1.1.1.1.2.2 pgoyette NULL, /* Ss */
45 1.1.1.1.2.2 pgoyette NULL, /* Pp */
46 1.1.1.1.2.2 pgoyette NULL, /* D1 */
47 1.1.1.1.2.2 pgoyette state_dl, /* Dl */
48 1.1.1.1.2.2 pgoyette state_bd, /* Bd */
49 1.1.1.1.2.2 pgoyette NULL, /* Ed */
50 1.1.1.1.2.2 pgoyette state_bl, /* Bl */
51 1.1.1.1.2.2 pgoyette NULL, /* El */
52 1.1.1.1.2.2 pgoyette NULL, /* It */
53 1.1.1.1.2.2 pgoyette NULL, /* Ad */
54 1.1.1.1.2.2 pgoyette NULL, /* An */
55 1.1.1.1.2.2 pgoyette NULL, /* Ar */
56 1.1.1.1.2.2 pgoyette NULL, /* Cd */
57 1.1.1.1.2.2 pgoyette NULL, /* Cm */
58 1.1.1.1.2.2 pgoyette NULL, /* Dv */
59 1.1.1.1.2.2 pgoyette NULL, /* Er */
60 1.1.1.1.2.2 pgoyette NULL, /* Ev */
61 1.1.1.1.2.2 pgoyette NULL, /* Ex */
62 1.1.1.1.2.2 pgoyette NULL, /* Fa */
63 1.1.1.1.2.2 pgoyette NULL, /* Fd */
64 1.1.1.1.2.2 pgoyette NULL, /* Fl */
65 1.1.1.1.2.2 pgoyette NULL, /* Fn */
66 1.1.1.1.2.2 pgoyette NULL, /* Ft */
67 1.1.1.1.2.2 pgoyette NULL, /* Ic */
68 1.1.1.1.2.2 pgoyette NULL, /* In */
69 1.1.1.1.2.2 pgoyette NULL, /* Li */
70 1.1.1.1.2.2 pgoyette NULL, /* Nd */
71 1.1.1.1.2.2 pgoyette NULL, /* Nm */
72 1.1.1.1.2.2 pgoyette NULL, /* Op */
73 1.1.1.1.2.2 pgoyette NULL, /* Ot */
74 1.1.1.1.2.2 pgoyette NULL, /* Pa */
75 1.1.1.1.2.2 pgoyette NULL, /* Rv */
76 1.1.1.1.2.2 pgoyette NULL, /* St */
77 1.1.1.1.2.2 pgoyette NULL, /* Va */
78 1.1.1.1.2.2 pgoyette NULL, /* Vt */
79 1.1.1.1.2.2 pgoyette NULL, /* Xr */
80 1.1.1.1.2.2 pgoyette NULL, /* %A */
81 1.1.1.1.2.2 pgoyette NULL, /* %B */
82 1.1.1.1.2.2 pgoyette NULL, /* %D */
83 1.1.1.1.2.2 pgoyette NULL, /* %I */
84 1.1.1.1.2.2 pgoyette NULL, /* %J */
85 1.1.1.1.2.2 pgoyette NULL, /* %N */
86 1.1.1.1.2.2 pgoyette NULL, /* %O */
87 1.1.1.1.2.2 pgoyette NULL, /* %P */
88 1.1.1.1.2.2 pgoyette NULL, /* %R */
89 1.1.1.1.2.2 pgoyette NULL, /* %T */
90 1.1.1.1.2.2 pgoyette NULL, /* %V */
91 1.1.1.1.2.2 pgoyette NULL, /* Ac */
92 1.1.1.1.2.2 pgoyette NULL, /* Ao */
93 1.1.1.1.2.2 pgoyette NULL, /* Aq */
94 1.1.1.1.2.2 pgoyette NULL, /* At */
95 1.1.1.1.2.2 pgoyette NULL, /* Bc */
96 1.1.1.1.2.2 pgoyette NULL, /* Bf */
97 1.1.1.1.2.2 pgoyette NULL, /* Bo */
98 1.1.1.1.2.2 pgoyette NULL, /* Bq */
99 1.1.1.1.2.2 pgoyette NULL, /* Bsx */
100 1.1.1.1.2.2 pgoyette NULL, /* Bx */
101 1.1.1.1.2.2 pgoyette NULL, /* Db */
102 1.1.1.1.2.2 pgoyette NULL, /* Dc */
103 1.1.1.1.2.2 pgoyette NULL, /* Do */
104 1.1.1.1.2.2 pgoyette NULL, /* Dq */
105 1.1.1.1.2.2 pgoyette NULL, /* Ec */
106 1.1.1.1.2.2 pgoyette NULL, /* Ef */
107 1.1.1.1.2.2 pgoyette NULL, /* Em */
108 1.1.1.1.2.2 pgoyette NULL, /* Eo */
109 1.1.1.1.2.2 pgoyette NULL, /* Fx */
110 1.1.1.1.2.2 pgoyette NULL, /* Ms */
111 1.1.1.1.2.2 pgoyette NULL, /* No */
112 1.1.1.1.2.2 pgoyette NULL, /* Ns */
113 1.1.1.1.2.2 pgoyette NULL, /* Nx */
114 1.1.1.1.2.2 pgoyette NULL, /* Ox */
115 1.1.1.1.2.2 pgoyette NULL, /* Pc */
116 1.1.1.1.2.2 pgoyette NULL, /* Pf */
117 1.1.1.1.2.2 pgoyette NULL, /* Po */
118 1.1.1.1.2.2 pgoyette NULL, /* Pq */
119 1.1.1.1.2.2 pgoyette NULL, /* Qc */
120 1.1.1.1.2.2 pgoyette NULL, /* Ql */
121 1.1.1.1.2.2 pgoyette NULL, /* Qo */
122 1.1.1.1.2.2 pgoyette NULL, /* Qq */
123 1.1.1.1.2.2 pgoyette NULL, /* Re */
124 1.1.1.1.2.2 pgoyette NULL, /* Rs */
125 1.1.1.1.2.2 pgoyette NULL, /* Sc */
126 1.1.1.1.2.2 pgoyette NULL, /* So */
127 1.1.1.1.2.2 pgoyette NULL, /* Sq */
128 1.1.1.1.2.2 pgoyette state_sm, /* Sm */
129 1.1.1.1.2.2 pgoyette NULL, /* Sx */
130 1.1.1.1.2.2 pgoyette NULL, /* Sy */
131 1.1.1.1.2.2 pgoyette NULL, /* Tn */
132 1.1.1.1.2.2 pgoyette NULL, /* Ux */
133 1.1.1.1.2.2 pgoyette NULL, /* Xc */
134 1.1.1.1.2.2 pgoyette NULL, /* Xo */
135 1.1.1.1.2.2 pgoyette NULL, /* Fo */
136 1.1.1.1.2.2 pgoyette NULL, /* Fc */
137 1.1.1.1.2.2 pgoyette NULL, /* Oo */
138 1.1.1.1.2.2 pgoyette NULL, /* Oc */
139 1.1.1.1.2.2 pgoyette NULL, /* Bk */
140 1.1.1.1.2.2 pgoyette NULL, /* Ek */
141 1.1.1.1.2.2 pgoyette NULL, /* Bt */
142 1.1.1.1.2.2 pgoyette NULL, /* Hf */
143 1.1.1.1.2.2 pgoyette NULL, /* Fr */
144 1.1.1.1.2.2 pgoyette NULL, /* Ud */
145 1.1.1.1.2.2 pgoyette NULL, /* Lb */
146 1.1.1.1.2.2 pgoyette NULL, /* Lp */
147 1.1.1.1.2.2 pgoyette NULL, /* Lk */
148 1.1.1.1.2.2 pgoyette NULL, /* Mt */
149 1.1.1.1.2.2 pgoyette NULL, /* Brq */
150 1.1.1.1.2.2 pgoyette NULL, /* Bro */
151 1.1.1.1.2.2 pgoyette NULL, /* Brc */
152 1.1.1.1.2.2 pgoyette NULL, /* %C */
153 1.1.1.1.2.2 pgoyette NULL, /* Es */
154 1.1.1.1.2.2 pgoyette NULL, /* En */
155 1.1.1.1.2.2 pgoyette NULL, /* Dx */
156 1.1.1.1.2.2 pgoyette NULL, /* %Q */
157 1.1.1.1.2.2 pgoyette NULL, /* br */
158 1.1.1.1.2.2 pgoyette NULL, /* sp */
159 1.1.1.1.2.2 pgoyette NULL, /* %U */
160 1.1.1.1.2.2 pgoyette NULL, /* Ta */
161 1.1.1.1.2.2 pgoyette NULL, /* ll */
162 1.1.1.1.2.2 pgoyette };
163 1.1.1.1.2.2 pgoyette
164 1.1.1.1.2.2 pgoyette
165 1.1.1.1.2.2 pgoyette void
166 1.1.1.1.2.2 pgoyette mdoc_state(struct roff_man *mdoc, struct roff_node *n)
167 1.1.1.1.2.2 pgoyette {
168 1.1.1.1.2.2 pgoyette state_handler handler;
169 1.1.1.1.2.2 pgoyette
170 1.1.1.1.2.2 pgoyette if (n->tok == TOKEN_NONE)
171 1.1.1.1.2.2 pgoyette return;
172 1.1.1.1.2.2 pgoyette
173 1.1.1.1.2.2 pgoyette if ( ! (mdoc_macros[n->tok].flags & MDOC_PROLOGUE))
174 1.1.1.1.2.2 pgoyette mdoc->flags |= MDOC_PBODY;
175 1.1.1.1.2.2 pgoyette
176 1.1.1.1.2.2 pgoyette handler = state_handlers[n->tok];
177 1.1.1.1.2.2 pgoyette if (*handler)
178 1.1.1.1.2.2 pgoyette (*handler)(mdoc, n);
179 1.1.1.1.2.2 pgoyette }
180 1.1.1.1.2.2 pgoyette
181 1.1.1.1.2.2 pgoyette void
182 1.1.1.1.2.2 pgoyette mdoc_state_reset(struct roff_man *mdoc)
183 1.1.1.1.2.2 pgoyette {
184 1.1.1.1.2.2 pgoyette
185 1.1.1.1.2.2 pgoyette roff_setreg(mdoc->roff, "nS", 0, '=');
186 1.1.1.1.2.2 pgoyette mdoc->flags = 0;
187 1.1.1.1.2.2 pgoyette }
188 1.1.1.1.2.2 pgoyette
189 1.1.1.1.2.2 pgoyette static void
190 1.1.1.1.2.2 pgoyette state_bd(STATE_ARGS)
191 1.1.1.1.2.2 pgoyette {
192 1.1.1.1.2.2 pgoyette enum mdocargt arg;
193 1.1.1.1.2.2 pgoyette
194 1.1.1.1.2.2 pgoyette if (n->type != ROFFT_HEAD &&
195 1.1.1.1.2.2 pgoyette (n->type != ROFFT_BODY || n->end != ENDBODY_NOT))
196 1.1.1.1.2.2 pgoyette return;
197 1.1.1.1.2.2 pgoyette
198 1.1.1.1.2.2 pgoyette if (n->parent->args == NULL)
199 1.1.1.1.2.2 pgoyette return;
200 1.1.1.1.2.2 pgoyette
201 1.1.1.1.2.2 pgoyette arg = n->parent->args->argv[0].arg;
202 1.1.1.1.2.2 pgoyette if (arg != MDOC_Literal && arg != MDOC_Unfilled)
203 1.1.1.1.2.2 pgoyette return;
204 1.1.1.1.2.2 pgoyette
205 1.1.1.1.2.2 pgoyette state_dl(mdoc, n);
206 1.1.1.1.2.2 pgoyette }
207 1.1.1.1.2.2 pgoyette
208 1.1.1.1.2.2 pgoyette static void
209 1.1.1.1.2.2 pgoyette state_bl(STATE_ARGS)
210 1.1.1.1.2.2 pgoyette {
211 1.1.1.1.2.2 pgoyette
212 1.1.1.1.2.2 pgoyette if (n->type != ROFFT_HEAD || n->parent->args == NULL)
213 1.1.1.1.2.2 pgoyette return;
214 1.1.1.1.2.2 pgoyette
215 1.1.1.1.2.2 pgoyette switch(n->parent->args->argv[0].arg) {
216 1.1.1.1.2.2 pgoyette case MDOC_Diag:
217 1.1.1.1.2.2 pgoyette n->norm->Bl.type = LIST_diag;
218 1.1.1.1.2.2 pgoyette break;
219 1.1.1.1.2.2 pgoyette case MDOC_Column:
220 1.1.1.1.2.2 pgoyette n->norm->Bl.type = LIST_column;
221 1.1.1.1.2.2 pgoyette break;
222 1.1.1.1.2.2 pgoyette default:
223 1.1.1.1.2.2 pgoyette break;
224 1.1.1.1.2.2 pgoyette }
225 1.1.1.1.2.2 pgoyette }
226 1.1.1.1.2.2 pgoyette
227 1.1.1.1.2.2 pgoyette static void
228 1.1.1.1.2.2 pgoyette state_dl(STATE_ARGS)
229 1.1.1.1.2.2 pgoyette {
230 1.1.1.1.2.2 pgoyette
231 1.1.1.1.2.2 pgoyette switch (n->type) {
232 1.1.1.1.2.2 pgoyette case ROFFT_HEAD:
233 1.1.1.1.2.2 pgoyette mdoc->flags |= MDOC_LITERAL;
234 1.1.1.1.2.2 pgoyette break;
235 1.1.1.1.2.2 pgoyette case ROFFT_BODY:
236 1.1.1.1.2.2 pgoyette mdoc->flags &= ~MDOC_LITERAL;
237 1.1.1.1.2.2 pgoyette break;
238 1.1.1.1.2.2 pgoyette default:
239 1.1.1.1.2.2 pgoyette break;
240 1.1.1.1.2.2 pgoyette }
241 1.1.1.1.2.2 pgoyette }
242 1.1.1.1.2.2 pgoyette
243 1.1.1.1.2.2 pgoyette static void
244 1.1.1.1.2.2 pgoyette state_sh(STATE_ARGS)
245 1.1.1.1.2.2 pgoyette {
246 1.1.1.1.2.2 pgoyette struct roff_node *nch;
247 1.1.1.1.2.2 pgoyette char *secname;
248 1.1.1.1.2.2 pgoyette
249 1.1.1.1.2.2 pgoyette if (n->type != ROFFT_HEAD)
250 1.1.1.1.2.2 pgoyette return;
251 1.1.1.1.2.2 pgoyette
252 1.1.1.1.2.3 pgoyette if ( ! (n->flags & NODE_VALID)) {
253 1.1.1.1.2.2 pgoyette secname = NULL;
254 1.1.1.1.2.2 pgoyette deroff(&secname, n);
255 1.1.1.1.2.2 pgoyette
256 1.1.1.1.2.2 pgoyette /*
257 1.1.1.1.2.2 pgoyette * Set the section attribute for the BLOCK, HEAD,
258 1.1.1.1.2.2 pgoyette * and HEAD children; the latter can only be TEXT
259 1.1.1.1.2.2 pgoyette * nodes, so no recursion is needed. For other
260 1.1.1.1.2.2 pgoyette * nodes, including the .Sh BODY, this is done
261 1.1.1.1.2.2 pgoyette * when allocating the node data structures, but
262 1.1.1.1.2.2 pgoyette * for .Sh BLOCK and HEAD, the section is still
263 1.1.1.1.2.2 pgoyette * unknown at that time.
264 1.1.1.1.2.2 pgoyette */
265 1.1.1.1.2.2 pgoyette
266 1.1.1.1.2.2 pgoyette n->sec = n->parent->sec = secname == NULL ?
267 1.1.1.1.2.2 pgoyette SEC_CUSTOM : mdoc_a2sec(secname);
268 1.1.1.1.2.2 pgoyette for (nch = n->child; nch != NULL; nch = nch->next)
269 1.1.1.1.2.2 pgoyette nch->sec = n->sec;
270 1.1.1.1.2.2 pgoyette free(secname);
271 1.1.1.1.2.2 pgoyette }
272 1.1.1.1.2.2 pgoyette
273 1.1.1.1.2.2 pgoyette if ((mdoc->lastsec = n->sec) == SEC_SYNOPSIS) {
274 1.1.1.1.2.2 pgoyette roff_setreg(mdoc->roff, "nS", 1, '=');
275 1.1.1.1.2.2 pgoyette mdoc->flags |= MDOC_SYNOPSIS;
276 1.1.1.1.2.2 pgoyette } else {
277 1.1.1.1.2.2 pgoyette roff_setreg(mdoc->roff, "nS", 0, '=');
278 1.1.1.1.2.2 pgoyette mdoc->flags &= ~MDOC_SYNOPSIS;
279 1.1.1.1.2.2 pgoyette }
280 1.1.1.1.2.2 pgoyette }
281 1.1.1.1.2.2 pgoyette
282 1.1.1.1.2.2 pgoyette static void
283 1.1.1.1.2.2 pgoyette state_sm(STATE_ARGS)
284 1.1.1.1.2.2 pgoyette {
285 1.1.1.1.2.2 pgoyette
286 1.1.1.1.2.2 pgoyette if (n->child == NULL)
287 1.1.1.1.2.2 pgoyette mdoc->flags ^= MDOC_SMOFF;
288 1.1.1.1.2.2 pgoyette else if ( ! strcmp(n->child->string, "on"))
289 1.1.1.1.2.2 pgoyette mdoc->flags &= ~MDOC_SMOFF;
290 1.1.1.1.2.2 pgoyette else if ( ! strcmp(n->child->string, "off"))
291 1.1.1.1.2.2 pgoyette mdoc->flags |= MDOC_SMOFF;
292 1.1.1.1.2.2 pgoyette }
293