]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.160 2015/04/19 14:25:41 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2011 Joerg Sonnenberger <joerg@netbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
30 #include "mandoc_aux.h"
34 #include "libmandoc.h"
38 const char *const __man_macronames
[MAN_MAX
] = {
39 "br", "TH", "SH", "SS",
40 "TP", "LP", "PP", "P",
41 "IP", "HP", "SM", "SB",
42 "BI", "IB", "BR", "RB",
45 "fi", "RE", "RS", "DT",
46 "UC", "PD", "AT", "in",
47 "ft", "OP", "EX", "EE",
51 const char * const *man_macronames
= __man_macronames
;
53 static void man_descope(struct roff_man
*, int, int);
54 static int man_ptext(struct roff_man
*, int, char *, int);
55 static int man_pmacro(struct roff_man
*, int, char *, int);
59 man_endparse(struct roff_man
*man
)
66 man_parseln(struct roff_man
*man
, int ln
, char *buf
, int offs
)
69 if (man
->last
->type
!= ROFFT_EQN
|| ln
> man
->last
->line
)
70 man
->flags
|= MAN_NEWLINE
;
72 return (roff_getcontrol(man
->roff
, buf
, &offs
) ?
73 man_pmacro(man
, ln
, buf
, offs
) :
74 man_ptext(man
, ln
, buf
, offs
));
78 man_elem_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
82 p
= roff_node_alloc(man
, line
, pos
, ROFFT_ELEM
, tok
);
83 roff_node_append(man
, p
);
84 man
->next
= ROFF_NEXT_CHILD
;
88 man_block_alloc(struct roff_man
*man
, int line
, int pos
, int tok
)
92 p
= roff_node_alloc(man
, line
, pos
, ROFFT_BLOCK
, tok
);
93 roff_node_append(man
, p
);
94 man
->next
= ROFF_NEXT_CHILD
;
98 man_descope(struct roff_man
*man
, int line
, int offs
)
101 * Co-ordinate what happens with having a next-line scope open:
102 * first close out the element scope (if applicable), then close
103 * out the block scope (also if applicable).
106 if (man
->flags
& MAN_ELINE
) {
107 man
->flags
&= ~MAN_ELINE
;
108 man_unscope(man
, man
->last
->parent
);
110 if ( ! (man
->flags
& MAN_BLINE
))
112 man
->flags
&= ~MAN_BLINE
;
113 man_unscope(man
, man
->last
->parent
);
114 roff_body_alloc(man
, line
, offs
, man
->last
->tok
);
118 man_ptext(struct roff_man
*man
, int line
, char *buf
, int offs
)
122 /* Literal free-form text whitespace is preserved. */
124 if (man
->flags
& MAN_LITERAL
) {
125 roff_word_alloc(man
, line
, offs
, buf
+ offs
);
126 man_descope(man
, line
, offs
);
130 for (i
= offs
; buf
[i
] == ' '; i
++)
131 /* Skip leading whitespace. */ ;
134 * Blank lines are ignored right after headings
135 * but add a single vertical space elsewhere.
138 if (buf
[i
] == '\0') {
139 /* Allocate a blank entry. */
140 if (man
->last
->tok
!= MAN_SH
&&
141 man
->last
->tok
!= MAN_SS
) {
142 man_elem_alloc(man
, line
, offs
, MAN_sp
);
143 man
->next
= ROFF_NEXT_SIBLING
;
149 * Warn if the last un-escaped character is whitespace. Then
150 * strip away the remaining spaces (tabs stay!).
153 i
= (int)strlen(buf
);
156 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
157 if (i
> 1 && '\\' != buf
[i
- 2])
158 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
161 for (--i
; i
&& ' ' == buf
[i
]; i
--)
162 /* Spin back to non-space. */ ;
164 /* Jump ahead of escaped whitespace. */
165 i
+= '\\' == buf
[i
] ? 2 : 1;
169 roff_word_alloc(man
, line
, offs
, buf
+ offs
);
172 * End-of-sentence check. If the last character is an unescaped
173 * EOS character, then flag the node as being the end of a
174 * sentence. The front-end will know how to interpret this.
178 if (mandoc_eos(buf
, (size_t)i
))
179 man
->last
->flags
|= MAN_EOS
;
181 man_descope(man
, line
, offs
);
186 man_pmacro(struct roff_man
*man
, int ln
, char *buf
, int offs
)
198 * Copy the first word into a nil-terminated buffer.
199 * Stop when a space, tab, escape, or eoln is encountered.
203 while (i
< 4 && strchr(" \t\\", buf
[offs
]) == NULL
)
204 mac
[i
++] = buf
[offs
++];
208 tok
= (i
> 0 && i
< 4) ? man_hash_find(mac
) : TOKEN_NONE
;
210 if (tok
== TOKEN_NONE
) {
211 mandoc_msg(MANDOCERR_MACRO
, man
->parse
,
212 ln
, ppos
, buf
+ ppos
- 1);
216 /* Skip a leading escape sequence or tab. */
221 mandoc_escape(&cp
, NULL
, NULL
);
231 /* Jump to the next non-whitespace word. */
233 while (buf
[offs
] && buf
[offs
] == ' ')
237 * Trailing whitespace. Note that tabs are allowed to be passed
238 * into the parser as "text", so we only warn about spaces here.
241 if (buf
[offs
] == '\0' && buf
[offs
- 1] == ' ')
242 mandoc_msg(MANDOCERR_SPACE_EOL
, man
->parse
,
246 * Some macros break next-line scopes; otherwise, remember
247 * whether we are in next-line scope for a block head.
250 man_breakscope(man
, tok
);
251 bline
= man
->flags
& MAN_BLINE
;
253 /* Call to handler... */
255 assert(man_macros
[tok
].fp
);
256 (*man_macros
[tok
].fp
)(man
, tok
, ln
, ppos
, &offs
, buf
);
258 /* In quick mode (for mandocdb), abort after the NAME section. */
260 if (man
->quick
&& tok
== MAN_SH
) {
262 if (n
->type
== ROFFT_BODY
&&
263 strcmp(n
->prev
->child
->string
, "NAME"))
268 * If we are in a next-line scope for a block head,
269 * close it out now and switch to the body,
270 * unless the next-line scope is allowed to continue.
273 if ( ! bline
|| man
->flags
& MAN_ELINE
||
274 man_macros
[tok
].flags
& MAN_NSCOPED
)
277 assert(man
->flags
& MAN_BLINE
);
278 man
->flags
&= ~MAN_BLINE
;
280 man_unscope(man
, man
->last
->parent
);
281 roff_body_alloc(man
, ln
, ppos
, man
->last
->tok
);
286 man_breakscope(struct roff_man
*man
, int tok
)
291 * An element next line scope is open,
292 * and the new macro is not allowed inside elements.
293 * Delete the element that is being broken.
296 if (man
->flags
& MAN_ELINE
&& (tok
== TOKEN_NONE
||
297 ! (man_macros
[tok
].flags
& MAN_NSCOPED
))) {
299 assert(n
->type
!= ROFFT_TEXT
);
300 if (man_macros
[n
->tok
].flags
& MAN_NSCOPED
)
303 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
,
304 n
->line
, n
->pos
, "%s breaks %s",
305 tok
== TOKEN_NONE
? "TS" : man_macronames
[tok
],
306 man_macronames
[n
->tok
]);
308 roff_node_delete(man
, n
);
309 man
->flags
&= ~MAN_ELINE
;
313 * A block header next line scope is open,
314 * and the new macro is not allowed inside block headers.
315 * Delete the block that is being broken.
318 if (man
->flags
& MAN_BLINE
&& (tok
== TOKEN_NONE
||
319 man_macros
[tok
].flags
& MAN_BSCOPE
)) {
321 if (n
->type
== ROFFT_TEXT
)
323 if ( ! (man_macros
[n
->tok
].flags
& MAN_BSCOPE
))
326 assert(n
->type
== ROFFT_HEAD
);
328 assert(n
->type
== ROFFT_BLOCK
);
329 assert(man_macros
[n
->tok
].flags
& MAN_SCOPED
);
331 mandoc_vmsg(MANDOCERR_BLK_LINE
, man
->parse
,
332 n
->line
, n
->pos
, "%s breaks %s",
333 tok
== TOKEN_NONE
? "TS" : man_macronames
[tok
],
334 man_macronames
[n
->tok
]);
336 roff_node_delete(man
, n
);
337 man
->flags
&= ~MAN_BLINE
;
341 const struct mparse
*
342 man_mparse(const struct roff_man
*man
)
345 assert(man
&& man
->parse
);
350 man_deroff(char **dest
, const struct roff_node
*n
)
355 if (n
->type
!= ROFFT_TEXT
) {
356 for (n
= n
->child
; n
; n
= n
->next
)
361 /* Skip leading whitespace and escape sequences. */
364 while ('\0' != *cp
) {
367 mandoc_escape((const char **)&cp
, NULL
, NULL
);
368 } else if (isspace((unsigned char)*cp
))
374 /* Skip trailing whitespace. */
376 for (sz
= strlen(cp
); sz
; sz
--)
377 if (0 == isspace((unsigned char)cp
[sz
-1]))
380 /* Skip empty strings. */
386 *dest
= mandoc_strndup(cp
, sz
);
390 mandoc_asprintf(&cp
, "%s %*s", *dest
, (int)sz
, cp
);