]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.255 2015/10/20 02:01:31 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012-2015 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_macronames
[MDOC_MAX
+ 1] = {
40 "Ap", "Dd", "Dt", "Os",
41 "Sh", "Ss", "Pp", "D1",
42 "Dl", "Bd", "Ed", "Bl",
43 "El", "It", "Ad", "An",
44 "Ar", "Cd", "Cm", "Dv",
45 "Er", "Ev", "Ex", "Fa",
46 "Fd", "Fl", "Fn", "Ft",
47 "Ic", "In", "Li", "Nd",
48 "Nm", "Op", "Ot", "Pa",
49 "Rv", "St", "Va", "Vt",
50 "Xr", "%A", "%B", "%D",
51 "%I", "%J", "%N", "%O",
52 "%P", "%R", "%T", "%V",
53 "Ac", "Ao", "Aq", "At",
54 "Bc", "Bf", "Bo", "Bq",
55 "Bsx", "Bx", "Db", "Dc",
56 "Do", "Dq", "Ec", "Ef",
57 "Em", "Eo", "Fx", "Ms",
58 "No", "Ns", "Nx", "Ox",
59 "Pc", "Pf", "Po", "Pq",
60 "Qc", "Ql", "Qo", "Qq",
61 "Re", "Rs", "Sc", "So",
62 "Sq", "Sm", "Sx", "Sy",
63 "Tn", "Ux", "Xc", "Xo",
64 "Fo", "Fc", "Oo", "Oc",
65 "Bk", "Ek", "Bt", "Hf",
66 "Fr", "Ud", "Lb", "Lp",
67 "Lk", "Mt", "Brq", "Bro",
68 "Brc", "%C", "Es", "En",
69 "Dx", "%Q", "br", "sp",
73 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
74 "split", "nosplit", "ragged",
75 "unfilled", "literal", "file",
76 "offset", "bullet", "dash",
77 "hyphen", "item", "enum",
78 "tag", "diag", "hang",
79 "ohang", "inset", "column",
80 "width", "compact", "std",
81 "filled", "words", "emphasis",
82 "symbolic", "nested", "centered"
85 const char * const *mdoc_macronames
= __mdoc_macronames
+ 1;
86 const char * const *mdoc_argnames
= __mdoc_argnames
;
88 static int mdoc_ptext(struct roff_man
*, int, char *, int);
89 static int mdoc_pmacro(struct roff_man
*, int, char *, int);
93 * Main parse routine. Parses a single line -- really just hands off to
94 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
97 mdoc_parseln(struct roff_man
*mdoc
, int ln
, char *buf
, int offs
)
100 if (mdoc
->last
->type
!= ROFFT_EQN
|| ln
> mdoc
->last
->line
)
101 mdoc
->flags
|= MDOC_NEWLINE
;
104 * Let the roff nS register switch SYNOPSIS mode early,
105 * such that the parser knows at all times
106 * whether this mode is on or off.
107 * Note that this mode is also switched by the Sh macro.
109 if (roff_getreg(mdoc
->roff
, "nS"))
110 mdoc
->flags
|= MDOC_SYNOPSIS
;
112 mdoc
->flags
&= ~MDOC_SYNOPSIS
;
114 return roff_getcontrol(mdoc
->roff
, buf
, &offs
) ?
115 mdoc_pmacro(mdoc
, ln
, buf
, offs
) :
116 mdoc_ptext(mdoc
, ln
, buf
, offs
);
120 mdoc_macro(MACRO_PROT_ARGS
)
122 assert(tok
> TOKEN_NONE
&& tok
< MDOC_MAX
);
124 (*mdoc_macros
[tok
].fp
)(mdoc
, tok
, line
, ppos
, pos
, buf
);
128 mdoc_tail_alloc(struct roff_man
*mdoc
, int line
, int pos
, int tok
)
132 p
= roff_node_alloc(mdoc
, line
, pos
, ROFFT_TAIL
, tok
);
133 roff_node_append(mdoc
, p
);
134 mdoc
->next
= ROFF_NEXT_CHILD
;
138 mdoc_endbody_alloc(struct roff_man
*mdoc
, int line
, int pos
, int tok
,
139 struct roff_node
*body
, enum mdoc_endbody end
)
143 body
->flags
|= MDOC_ENDED
;
144 body
->parent
->flags
|= MDOC_ENDED
;
145 p
= roff_node_alloc(mdoc
, line
, pos
, ROFFT_BODY
, tok
);
147 p
->norm
= body
->norm
;
149 roff_node_append(mdoc
, p
);
150 mdoc
->next
= ROFF_NEXT_SIBLING
;
155 mdoc_block_alloc(struct roff_man
*mdoc
, int line
, int pos
,
156 int tok
, struct mdoc_arg
*args
)
160 p
= roff_node_alloc(mdoc
, line
, pos
, ROFFT_BLOCK
, tok
);
171 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
176 roff_node_append(mdoc
, p
);
177 mdoc
->next
= ROFF_NEXT_CHILD
;
182 mdoc_elem_alloc(struct roff_man
*mdoc
, int line
, int pos
,
183 int tok
, struct mdoc_arg
*args
)
187 p
= roff_node_alloc(mdoc
, line
, pos
, ROFFT_ELEM
, tok
);
194 p
->norm
= mandoc_calloc(1, sizeof(union mdoc_data
));
199 roff_node_append(mdoc
, p
);
200 mdoc
->next
= ROFF_NEXT_CHILD
;
204 mdoc_node_relink(struct roff_man
*mdoc
, struct roff_node
*p
)
207 roff_node_unlink(mdoc
, p
);
208 roff_node_append(mdoc
, p
);
212 * Parse free-form text, that is, a line that does not begin with the
216 mdoc_ptext(struct roff_man
*mdoc
, int line
, char *buf
, int offs
)
225 * Divert directly to list processing if we're encountering a
226 * columnar ROFFT_BLOCK with or without a prior ROFFT_BLOCK entry
227 * (a ROFFT_BODY means it's already open, in which case we should
228 * process within its context in the normal way).
231 if (n
->tok
== MDOC_Bl
&& n
->type
== ROFFT_BODY
&&
232 n
->end
== ENDBODY_NOT
&& n
->norm
->Bl
.type
== LIST_column
) {
233 /* `Bl' is open without any children. */
234 mdoc
->flags
|= MDOC_FREECOL
;
235 mdoc_macro(mdoc
, MDOC_It
, line
, offs
, &offs
, buf
);
239 if (n
->tok
== MDOC_It
&& n
->type
== ROFFT_BLOCK
&&
241 MDOC_Bl
== n
->parent
->tok
&&
242 LIST_column
== n
->parent
->norm
->Bl
.type
) {
243 /* `Bl' has block-level `It' children. */
244 mdoc
->flags
|= MDOC_FREECOL
;
245 mdoc_macro(mdoc
, MDOC_It
, line
, offs
, &offs
, buf
);
250 * Search for the beginning of unescaped trailing whitespace (ws)
251 * and for the first character not to be output (end).
254 /* FIXME: replace with strcspn(). */
256 for (c
= end
= buf
+ offs
; *c
; c
++) {
264 * Always warn about trailing tabs,
265 * even outside literal context,
266 * where they should be put on the next line.
271 * Strip trailing tabs in literal context only;
272 * outside, they affect the next line.
274 if (MDOC_LITERAL
& mdoc
->flags
)
278 /* Skip the escaped character, too, if any. */
291 mandoc_msg(MANDOCERR_SPACE_EOL
, mdoc
->parse
,
292 line
, (int)(ws
-buf
), NULL
);
294 if (buf
[offs
] == '\0' && ! (mdoc
->flags
& MDOC_LITERAL
)) {
295 mandoc_msg(MANDOCERR_FI_BLANK
, mdoc
->parse
,
296 line
, (int)(c
- buf
), NULL
);
299 * Insert a `sp' in the case of a blank line. Technically,
300 * blank lines aren't allowed, but enough manuals assume this
301 * behaviour that we want to work around it.
303 roff_elem_alloc(mdoc
, line
, offs
, MDOC_sp
);
304 mdoc
->last
->flags
|= MDOC_VALID
| MDOC_ENDED
;
305 mdoc
->next
= ROFF_NEXT_SIBLING
;
309 roff_word_alloc(mdoc
, line
, offs
, buf
+offs
);
311 if (mdoc
->flags
& MDOC_LITERAL
)
315 * End-of-sentence check. If the last character is an unescaped
316 * EOS character, then flag the node as being the end of a
317 * sentence. The front-end will know how to interpret this.
322 if (mandoc_eos(buf
+offs
, (size_t)(end
-buf
-offs
)))
323 mdoc
->last
->flags
|= MDOC_EOS
;
328 * Parse a macro line, that is, a line beginning with the control
332 mdoc_pmacro(struct roff_man
*mdoc
, int ln
, char *buf
, int offs
)
343 * Copy the first word into a nil-terminated buffer.
344 * Stop when a space, tab, escape, or eoln is encountered.
348 while (i
< 4 && strchr(" \t\\", buf
[offs
]) == NULL
)
349 mac
[i
++] = buf
[offs
++];
353 tok
= (i
> 1 && i
< 4) ? mdoc_hash_find(mac
) : TOKEN_NONE
;
355 if (tok
== TOKEN_NONE
) {
356 mandoc_msg(MANDOCERR_MACRO
, mdoc
->parse
,
357 ln
, sv
, buf
+ sv
- 1);
361 /* Skip a leading escape sequence or tab. */
366 mandoc_escape(&cp
, NULL
, NULL
);
376 /* Jump to the next non-whitespace word. */
378 while (buf
[offs
] && ' ' == buf
[offs
])
382 * Trailing whitespace. Note that tabs are allowed to be passed
383 * into the parser as "text", so we only warn about spaces here.
386 if ('\0' == buf
[offs
] && ' ' == buf
[offs
- 1])
387 mandoc_msg(MANDOCERR_SPACE_EOL
, mdoc
->parse
,
391 * If an initial macro or a list invocation, divert directly
392 * into macro processing.
395 if (NULL
== mdoc
->last
|| MDOC_It
== tok
|| MDOC_El
== tok
) {
396 mdoc_macro(mdoc
, tok
, ln
, sv
, &offs
, buf
);
404 * If the first macro of a `Bl -column', open an `It' block
405 * context around the parsed macro.
408 if (n
->tok
== MDOC_Bl
&& n
->type
== ROFFT_BODY
&&
409 n
->end
== ENDBODY_NOT
&& n
->norm
->Bl
.type
== LIST_column
) {
410 mdoc
->flags
|= MDOC_FREECOL
;
411 mdoc_macro(mdoc
, MDOC_It
, ln
, sv
, &sv
, buf
);
416 * If we're following a block-level `It' within a `Bl -column'
417 * context (perhaps opened in the above block or in ptext()),
418 * then open an `It' block context around the parsed macro.
421 if (n
->tok
== MDOC_It
&& n
->type
== ROFFT_BLOCK
&&
423 MDOC_Bl
== n
->parent
->tok
&&
424 LIST_column
== n
->parent
->norm
->Bl
.type
) {
425 mdoc
->flags
|= MDOC_FREECOL
;
426 mdoc_macro(mdoc
, MDOC_It
, ln
, sv
, &sv
, buf
);
430 /* Normal processing of a macro. */
432 mdoc_macro(mdoc
, tok
, ln
, sv
, &offs
, buf
);
434 /* In quick mode (for mandocdb), abort after the NAME section. */
436 if (mdoc
->quick
&& MDOC_Sh
== tok
&&
437 SEC_NAME
!= mdoc
->last
->sec
)
444 mdoc_isdelim(const char *p
)
473 if (0 == strcmp(p
+ 1, "."))
475 if (0 == strcmp(p
+ 1, "fR|\\fP"))
482 mdoc_validate(struct roff_man
*mdoc
)
485 mdoc
->last
= mdoc
->first
;
486 mdoc_node_validate(mdoc
);
487 mdoc_state_reset(mdoc
);