]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.269 2018/08/17 20:33:37 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012-2018 Ingo Schwarze <schwarze@openbsd.org>
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.
20 #include <sys/types.h>
30 #include "mandoc_aux.h"
34 #include "libmandoc.h"
38 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
39 "split", "nosplit", "ragged",
40 "unfilled", "literal", "file",
41 "offset", "bullet", "dash",
42 "hyphen", "item", "enum",
43 "tag", "diag", "hang",
44 "ohang", "inset", "column",
45 "width", "compact", "std",
46 "filled", "words", "emphasis",
47 "symbolic", "nested", "centered"
49 const char * const *mdoc_argnames
= __mdoc_argnames
;
51 static int mdoc_ptext(struct roff_man
*, int, char *, int);
52 static int mdoc_pmacro(struct roff_man
*, int, char *, int);
56 * Main parse routine. Parses a single line -- really just hands off to
57 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
60 mdoc_parseln(struct roff_man
*mdoc
, int ln
, char *buf
, int offs
)
63 if (mdoc
->last
->type
!= ROFFT_EQN
|| ln
> mdoc
->last
->line
)
64 mdoc
->flags
|= MDOC_NEWLINE
;
67 * Let the roff nS register switch SYNOPSIS mode early,
68 * such that the parser knows at all times
69 * whether this mode is on or off.
70 * Note that this mode is also switched by the Sh macro.
72 if (roff_getreg(mdoc
->roff
, "nS"))
73 mdoc
->flags
|= MDOC_SYNOPSIS
;
75 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
77 return roff_getcontrol(mdoc
->roff
, buf
, &offs
) ?
78 mdoc_pmacro(mdoc
, ln
, buf
, offs
) :
79 mdoc_ptext(mdoc
, ln
, buf
, offs
);
83 mdoc_tail_alloc(struct roff_man
*mdoc
, int line
, int pos
, enum roff_tok tok
)
87 p
= roff_node_alloc(mdoc
, line
, pos
, ROFFT_TAIL
, tok
);
88 roff_node_append(mdoc
, p
);
89 mdoc
->next
= ROFF_NEXT_CHILD
;
93 mdoc_endbody_alloc(struct roff_man
*mdoc
, int line
, int pos
,
94 enum roff_tok tok
, struct roff_node
*body
)
98 body
->flags
|= NODE_ENDED
;
99 body
->parent
->flags
|= NODE_ENDED
;
100 p
= roff_node_alloc(mdoc
, line
, pos
, ROFFT_BODY
, tok
);
102 p
->norm
= body
->norm
;
103 p
->end
= ENDBODY_SPACE
;
104 roff_node_append(mdoc
, p
);
105 mdoc
->next
= ROFF_NEXT_SIBLING
;
110 mdoc_block_alloc(struct roff_man
*mdoc
, int line
, int pos
,
111 enum roff_tok tok
, struct mdoc_arg
*args
)
115 p
= roff_node_alloc(mdoc
, line
, pos
, ROFFT_BLOCK
, tok
);
126 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
131 roff_node_append(mdoc
, p
);
132 mdoc
->next
= ROFF_NEXT_CHILD
;
137 mdoc_elem_alloc(struct roff_man
*mdoc
, int line
, int pos
,
138 enum roff_tok tok
, struct mdoc_arg
*args
)
142 p
= roff_node_alloc(mdoc
, line
, pos
, ROFFT_ELEM
, tok
);
149 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
154 roff_node_append(mdoc
, p
);
155 mdoc
->next
= ROFF_NEXT_CHILD
;
159 mdoc_node_relink(struct roff_man
*mdoc
, struct roff_node
*p
)
162 roff_node_unlink(mdoc
, p
);
163 p
->prev
= p
->next
= NULL
;
164 roff_node_append(mdoc
, p
);
168 * Parse free-form text, that is, a line that does not begin with the
172 mdoc_ptext(struct roff_man
*mdoc
, int line
, char *buf
, int offs
)
181 * If a column list contains plain text, assume an implicit item
182 * macro. This can happen one or more times at the beginning
183 * of such a list, intermixed with non-It mdoc macros and with
184 * nodes generated on the roff level, for example by tbl.
187 if ((n
->tok
== MDOC_Bl
&& n
->type
== ROFFT_BODY
&&
188 n
->end
== ENDBODY_NOT
&& n
->norm
->Bl
.type
== LIST_column
) ||
189 (n
->parent
!= NULL
&& n
->parent
->tok
== MDOC_Bl
&&
190 n
->parent
->norm
->Bl
.type
== LIST_column
)) {
191 mdoc
->flags
|= MDOC_FREECOL
;
192 (*mdoc_macro(MDOC_It
)->fp
)(mdoc
, MDOC_It
,
193 line
, offs
, &offs
, buf
);
198 * Search for the beginning of unescaped trailing whitespace (ws)
199 * and for the first character not to be output (end).
202 /* FIXME: replace with strcspn(). */
204 for (c
= end
= buf
+ offs
; *c
; c
++) {
212 * Always warn about trailing tabs,
213 * even outside literal context,
214 * where they should be put on the next line.
219 * Strip trailing tabs in literal context only;
220 * outside, they affect the next line.
222 if (MDOC_LITERAL
& mdoc
->flags
)
226 /* Skip the escaped character, too, if any. */
239 mandoc_msg(MANDOCERR_SPACE_EOL
, mdoc
->parse
,
240 line
, (int)(ws
-buf
), NULL
);
243 * Blank lines are allowed in no-fill mode
244 * and cancel preceding \c,
245 * but add a single vertical space elsewhere.
248 if (buf
[offs
] == '\0' && ! (mdoc
->flags
& MDOC_LITERAL
)) {
249 switch (mdoc
->last
->type
) {
251 sp
= mdoc
->last
->string
;
252 cp
= end
= strchr(sp
, '\0') - 2;
253 if (cp
< sp
|| cp
[0] != '\\' || cp
[1] != 'c')
255 while (cp
> sp
&& cp
[-1] == '\\')
264 mandoc_msg(MANDOCERR_FI_BLANK
, mdoc
->parse
,
265 line
, (int)(c
- buf
), NULL
);
266 roff_elem_alloc(mdoc
, line
, offs
, ROFF_sp
);
267 mdoc
->last
->flags
|= NODE_VALID
| NODE_ENDED
;
268 mdoc
->next
= ROFF_NEXT_SIBLING
;
272 roff_word_alloc(mdoc
, line
, offs
, buf
+offs
);
274 if (mdoc
->flags
& MDOC_LITERAL
)
278 * End-of-sentence check. If the last character is an unescaped
279 * EOS character, then flag the node as being the end of a
280 * sentence. The front-end will know how to interpret this.
285 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
)))
286 mdoc
->last
->flags
|= NODE_EOS
;
288 for (c
= buf
+ offs
; c
!= NULL
; c
= strchr(c
+ 1, '.')) {
289 if (c
- buf
< offs
+ 2)
294 isalnum((unsigned char)c
[-2]) == 0 ||
295 isalnum((unsigned char)c
[-1]) == 0 ||
296 (c
[-2] == 'n' && c
[-1] == 'c') ||
297 (c
[-2] == 'v' && c
[-1] == 's'))
304 if (isupper((unsigned char)(*c
)))
305 mandoc_msg(MANDOCERR_EOS
, mdoc
->parse
,
306 line
, (int)(c
- buf
), NULL
);
313 * Parse a macro line, that is, a line beginning with the control
317 mdoc_pmacro(struct roff_man
*mdoc
, int ln
, char *buf
, int offs
)
325 /* Determine the line macro. */
329 for (sz
= 0; sz
< 4 && strchr(" \t\\", buf
[offs
]) == NULL
; sz
++)
331 if (sz
== 2 || sz
== 3)
332 tok
= roffhash_find(mdoc
->mdocmac
, buf
+ sv
, sz
);
333 if (tok
== TOKEN_NONE
) {
334 mandoc_msg(MANDOCERR_MACRO
, mdoc
->parse
,
335 ln
, sv
, buf
+ sv
- 1);
339 /* Skip a leading escape sequence or tab. */
344 mandoc_escape(&cp
, NULL
, NULL
);
354 /* Jump to the next non-whitespace word. */
356 while (buf
[offs
] == ' ')
360 * Trailing whitespace. Note that tabs are allowed to be passed
361 * into the parser as "text", so we only warn about spaces here.
364 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
365 mandoc_msg(MANDOCERR_SPACE_EOL
, mdoc
->parse
,
369 * If an initial macro or a list invocation, divert directly
370 * into macro processing.
374 if (n
== NULL
|| tok
== MDOC_It
|| tok
== MDOC_El
) {
375 (*mdoc_macro(tok
)->fp
)(mdoc
, tok
, ln
, sv
, &offs
, buf
);
380 * If a column list contains a non-It macro, assume an implicit
381 * item macro. This can happen one or more times at the
382 * beginning of such a list, intermixed with text lines and
383 * with nodes generated on the roff level, for example by tbl.
386 if ((n
->tok
== MDOC_Bl
&& n
->type
== ROFFT_BODY
&&
387 n
->end
== ENDBODY_NOT
&& n
->norm
->Bl
.type
== LIST_column
) ||
388 (n
->parent
!= NULL
&& n
->parent
->tok
== MDOC_Bl
&&
389 n
->parent
->norm
->Bl
.type
== LIST_column
)) {
390 mdoc
->flags
|= MDOC_FREECOL
;
391 (*mdoc_macro(MDOC_It
)->fp
)(mdoc
, MDOC_It
, ln
, sv
, &sv
, buf
);
395 /* Normal processing of a macro. */
397 (*mdoc_macro(tok
)->fp
)(mdoc
, tok
, ln
, sv
, &offs
, buf
);
399 /* In quick mode (for mandocdb), abort after the NAME section. */
401 if (mdoc
->quick
&& MDOC_Sh
== tok
&&
402 SEC_NAME
!= mdoc
->last
->sec
)
409 mdoc_isdelim(const char *p
)
438 if (0 == strcmp(p
+ 1, "."))
440 if (0 == strcmp(p
+ 1, "fR|\\fP"))
447 mdoc_validate(struct roff_man
*mdoc
)
450 mdoc
->last
= mdoc
->first
;
451 mdoc_node_validate(mdoc
);
452 mdoc_state_reset(mdoc
);