1 /* $Id: mdoc_state.c,v 1.10 2018/08/17 20:33:38 schwarze Exp $ */
3 * Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
17 #include <sys/types.h>
26 #include "libmandoc.h"
29 #define STATE_ARGS struct roff_man *mdoc, struct roff_node *n
31 typedef void (*state_handler
)(STATE_ARGS
);
33 static void state_bd(STATE_ARGS
);
34 static void state_bl(STATE_ARGS
);
35 static void state_dl(STATE_ARGS
);
36 static void state_sh(STATE_ARGS
);
37 static void state_sm(STATE_ARGS
);
39 static const state_handler state_handlers
[MDOC_MAX
- MDOC_Dd
] = {
164 mdoc_state(struct roff_man
*mdoc
, struct roff_node
*n
)
166 state_handler handler
;
168 if (n
->tok
== TOKEN_NONE
|| n
->tok
< ROFF_MAX
)
171 assert(n
->tok
>= MDOC_Dd
&& n
->tok
< MDOC_MAX
);
172 if ((mdoc_macro(n
->tok
)->flags
& MDOC_PROLOGUE
) == 0)
173 mdoc
->flags
|= MDOC_PBODY
;
175 handler
= state_handlers
[n
->tok
- MDOC_Dd
];
181 mdoc_state_reset(struct roff_man
*mdoc
)
184 roff_setreg(mdoc
->roff
, "nS", 0, '=');
193 if (n
->type
!= ROFFT_HEAD
&&
194 (n
->type
!= ROFFT_BODY
|| n
->end
!= ENDBODY_NOT
))
197 if (n
->parent
->args
== NULL
)
200 arg
= n
->parent
->args
->argv
[0].arg
;
201 if (arg
!= MDOC_Literal
&& arg
!= MDOC_Unfilled
)
210 struct mdoc_arg
*args
;
213 if (n
->type
!= ROFFT_HEAD
|| n
->parent
->args
== NULL
)
216 args
= n
->parent
->args
;
217 for (i
= 0; i
< args
->argc
; i
++) {
218 switch(args
->argv
[i
].arg
) {
220 n
->norm
->Bl
.type
= LIST_diag
;
223 n
->norm
->Bl
.type
= LIST_column
;
237 mdoc
->flags
|= MDOC_LITERAL
;
240 mdoc
->flags
&= ~MDOC_LITERAL
;
250 struct roff_node
*nch
;
253 if (n
->type
!= ROFFT_HEAD
)
256 if ( ! (n
->flags
& NODE_VALID
)) {
261 * Set the section attribute for the BLOCK, HEAD,
262 * and HEAD children; the latter can only be TEXT
263 * nodes, so no recursion is needed. For other
264 * nodes, including the .Sh BODY, this is done
265 * when allocating the node data structures, but
266 * for .Sh BLOCK and HEAD, the section is still
267 * unknown at that time.
270 n
->sec
= n
->parent
->sec
= secname
== NULL
?
271 SEC_CUSTOM
: mdoc_a2sec(secname
);
272 for (nch
= n
->child
; nch
!= NULL
; nch
= nch
->next
)
277 if ((mdoc
->lastsec
= n
->sec
) == SEC_SYNOPSIS
) {
278 roff_setreg(mdoc
->roff
, "nS", 1, '=');
279 mdoc
->flags
|= MDOC_SYNOPSIS
;
281 roff_setreg(mdoc
->roff
, "nS", 0, '=');
282 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
290 if (n
->child
== NULL
)
291 mdoc
->flags
^= MDOC_SMOFF
;
292 else if ( ! strcmp(n
->child
->string
, "on"))
293 mdoc
->flags
&= ~MDOC_SMOFF
;
294 else if ( ! strcmp(n
->child
->string
, "off"))
295 mdoc
->flags
|= MDOC_SMOFF
;