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