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