1 /* $Id: mdoc.c,v 1.275 2020/04/06 10:16:17 schwarze Exp $ */
3 * Copyright (c) 2010, 2012-2018, 2020 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Top level and utility functions of the mdoc(7) parser for mandoc(1).
22 #include <sys/types.h>
32 #include "mandoc_aux.h"
36 #include "libmandoc.h"
40 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
41 "split", "nosplit", "ragged",
42 "unfilled", "literal", "file",
43 "offset", "bullet", "dash",
44 "hyphen", "item", "enum",
45 "tag", "diag", "hang",
46 "ohang", "inset", "column",
47 "width", "compact", "std",
48 "filled", "words", "emphasis",
49 "symbolic", "nested", "centered"
51 const char * const *mdoc_argnames
= __mdoc_argnames
;
53 static int mdoc_ptext(struct roff_man
*, int, char *, int);
54 static int mdoc_pmacro(struct roff_man
*, int, char *, int);
58 * Main parse routine. Parses a single line -- really just hands off to
59 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
62 mdoc_parseln(struct roff_man
*mdoc
, int ln
, char *buf
, int offs
)
65 if (mdoc
->last
->type
!= ROFFT_EQN
|| ln
> mdoc
->last
->line
)
66 mdoc
->flags
|= MDOC_NEWLINE
;
69 * Let the roff nS register switch SYNOPSIS mode early,
70 * such that the parser knows at all times
71 * whether this mode is on or off.
72 * Note that this mode is also switched by the Sh macro.
74 if (roff_getreg(mdoc
->roff
, "nS"))
75 mdoc
->flags
|= MDOC_SYNOPSIS
;
77 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
79 return roff_getcontrol(mdoc
->roff
, buf
, &offs
) ?
80 mdoc_pmacro(mdoc
, ln
, buf
, offs
) :
81 mdoc_ptext(mdoc
, ln
, buf
, offs
);
85 mdoc_tail_alloc(struct roff_man
*mdoc
, int line
, int pos
, enum roff_tok tok
)
89 p
= roff_node_alloc(mdoc
, line
, pos
, ROFFT_TAIL
, tok
);
90 roff_node_append(mdoc
, p
);
91 mdoc
->next
= ROFF_NEXT_CHILD
;
95 mdoc_endbody_alloc(struct roff_man
*mdoc
, int line
, int pos
,
96 enum roff_tok tok
, struct roff_node
*body
)
100 body
->flags
|= NODE_ENDED
;
101 body
->parent
->flags
|= NODE_ENDED
;
102 p
= roff_node_alloc(mdoc
, line
, pos
, ROFFT_BODY
, tok
);
104 p
->norm
= body
->norm
;
105 p
->end
= ENDBODY_SPACE
;
106 roff_node_append(mdoc
, p
);
107 mdoc
->next
= ROFF_NEXT_SIBLING
;
112 mdoc_block_alloc(struct roff_man
*mdoc
, int line
, int pos
,
113 enum roff_tok tok
, struct mdoc_arg
*args
)
117 p
= roff_node_alloc(mdoc
, line
, pos
, ROFFT_BLOCK
, tok
);
128 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
133 roff_node_append(mdoc
, p
);
134 mdoc
->next
= ROFF_NEXT_CHILD
;
139 mdoc_elem_alloc(struct roff_man
*mdoc
, int line
, int pos
,
140 enum roff_tok tok
, struct mdoc_arg
*args
)
144 p
= roff_node_alloc(mdoc
, line
, pos
, ROFFT_ELEM
, tok
);
151 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
156 roff_node_append(mdoc
, p
);
157 mdoc
->next
= ROFF_NEXT_CHILD
;
161 * Parse free-form text, that is, a line that does not begin with the
165 mdoc_ptext(struct roff_man
*mdoc
, int line
, char *buf
, int offs
)
174 * If a column list contains plain text, assume an implicit item
175 * macro. This can happen one or more times at the beginning
176 * of such a list, intermixed with non-It mdoc macros and with
177 * nodes generated on the roff level, for example by tbl.
180 if ((n
->tok
== MDOC_Bl
&& n
->type
== ROFFT_BODY
&&
181 n
->end
== ENDBODY_NOT
&& n
->norm
->Bl
.type
== LIST_column
) ||
182 (n
->parent
!= NULL
&& n
->parent
->tok
== MDOC_Bl
&&
183 n
->parent
->norm
->Bl
.type
== LIST_column
)) {
184 mdoc
->flags
|= MDOC_FREECOL
;
185 (*mdoc_macro(MDOC_It
)->fp
)(mdoc
, MDOC_It
,
186 line
, offs
, &offs
, buf
);
191 * Search for the beginning of unescaped trailing whitespace (ws)
192 * and for the first character not to be output (end).
195 /* FIXME: replace with strcspn(). */
197 for (c
= end
= buf
+ offs
; *c
; c
++) {
205 * Always warn about trailing tabs,
206 * even outside literal context,
207 * where they should be put on the next line.
212 * Strip trailing tabs in literal context only;
213 * outside, they affect the next line.
215 if (mdoc
->flags
& ROFF_NOFILL
)
219 /* Skip the escaped character, too, if any. */
232 mandoc_msg(MANDOCERR_SPACE_EOL
, line
, (int)(ws
- buf
), NULL
);
235 * Blank lines are allowed in no-fill mode
236 * and cancel preceding \c,
237 * but add a single vertical space elsewhere.
240 if (buf
[offs
] == '\0' && (mdoc
->flags
& ROFF_NOFILL
) == 0) {
241 switch (mdoc
->last
->type
) {
243 sp
= mdoc
->last
->string
;
244 cp
= end
= strchr(sp
, '\0') - 2;
245 if (cp
< sp
|| cp
[0] != '\\' || cp
[1] != 'c')
247 while (cp
> sp
&& cp
[-1] == '\\')
256 mandoc_msg(MANDOCERR_FI_BLANK
, line
, (int)(c
- buf
), NULL
);
257 roff_elem_alloc(mdoc
, line
, offs
, ROFF_sp
);
258 mdoc
->last
->flags
|= NODE_VALID
| NODE_ENDED
;
259 mdoc
->next
= ROFF_NEXT_SIBLING
;
263 roff_word_alloc(mdoc
, line
, offs
, buf
+offs
);
265 if (mdoc
->flags
& ROFF_NOFILL
)
269 * End-of-sentence check. If the last character is an unescaped
270 * EOS character, then flag the node as being the end of a
271 * sentence. The front-end will know how to interpret this.
276 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
)))
277 mdoc
->last
->flags
|= NODE_EOS
;
279 for (c
= buf
+ offs
; c
!= NULL
; c
= strchr(c
+ 1, '.')) {
280 if (c
- buf
< offs
+ 2)
285 isalnum((unsigned char)c
[-2]) == 0 ||
286 isalnum((unsigned char)c
[-1]) == 0 ||
287 (c
[-2] == 'n' && c
[-1] == 'c') ||
288 (c
[-2] == 'v' && c
[-1] == 's'))
295 if (isupper((unsigned char)(*c
)))
296 mandoc_msg(MANDOCERR_EOS
, line
, (int)(c
- buf
), NULL
);
303 * Parse a macro line, that is, a line beginning with the control
307 mdoc_pmacro(struct roff_man
*mdoc
, int ln
, char *buf
, int offs
)
315 /* Determine the line macro. */
319 for (sz
= 0; sz
< 4 && strchr(" \t\\", buf
[offs
]) == NULL
; sz
++)
321 if (sz
== 2 || sz
== 3)
322 tok
= roffhash_find(mdoc
->mdocmac
, buf
+ sv
, sz
);
323 if (tok
== TOKEN_NONE
) {
324 mandoc_msg(MANDOCERR_MACRO
, ln
, sv
, "%s", buf
+ sv
- 1);
328 /* Skip a leading escape sequence or tab. */
333 mandoc_escape(&cp
, NULL
, NULL
);
343 /* Jump to the next non-whitespace word. */
345 while (buf
[offs
] == ' ')
349 * Trailing whitespace. Note that tabs are allowed to be passed
350 * into the parser as "text", so we only warn about spaces here.
353 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
354 mandoc_msg(MANDOCERR_SPACE_EOL
, ln
, offs
- 1, NULL
);
357 * If an initial or transparent macro or a list invocation,
358 * divert directly into macro processing.
362 if (n
== NULL
|| tok
== MDOC_It
|| tok
== MDOC_El
||
363 roff_tok_transparent(tok
)) {
364 (*mdoc_macro(tok
)->fp
)(mdoc
, tok
, ln
, sv
, &offs
, buf
);
369 * If a column list contains a non-It macro, assume an implicit
370 * item macro. This can happen one or more times at the
371 * beginning of such a list, intermixed with text lines and
372 * with nodes generated on the roff level, for example by tbl.
375 if ((n
->tok
== MDOC_Bl
&& n
->type
== ROFFT_BODY
&&
376 n
->end
== ENDBODY_NOT
&& n
->norm
->Bl
.type
== LIST_column
) ||
377 (n
->parent
!= NULL
&& n
->parent
->tok
== MDOC_Bl
&&
378 n
->parent
->norm
->Bl
.type
== LIST_column
)) {
379 mdoc
->flags
|= MDOC_FREECOL
;
380 (*mdoc_macro(MDOC_It
)->fp
)(mdoc
, MDOC_It
, ln
, sv
, &sv
, buf
);
384 /* Normal processing of a macro. */
386 (*mdoc_macro(tok
)->fp
)(mdoc
, tok
, ln
, sv
, &offs
, buf
);
388 /* In quick mode (for mandocdb), abort after the NAME section. */
390 if (mdoc
->quick
&& MDOC_Sh
== tok
&&
391 SEC_NAME
!= mdoc
->last
->sec
)
398 mdoc_isdelim(const char *p
)
427 if (0 == strcmp(p
+ 1, "."))
429 if (0 == strcmp(p
+ 1, "fR|\\fP"))