]>
git.cameronkatri.com Git - mandoc.git/blob - man.c
1 /* $Id: man.c,v 1.188 2022/04/28 10:26:37 schwarze Exp $ */
3 * Copyright (c) 2013-2015,2017-2019,2022 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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 static char *man_hasc(char *);
39 static int man_ptext(struct roff_man
*, int, char *, int);
40 static int man_pmacro(struct roff_man
*, int, char *, int);
44 man_parseln(struct roff_man
*man
, int ln
, char *buf
, int offs
)
47 if (man
->last
->type
!= ROFFT_EQN
|| ln
> man
->last
->line
)
48 man
->flags
|= MAN_NEWLINE
;
50 return roff_getcontrol(man
->roff
, buf
, &offs
) ?
51 man_pmacro(man
, ln
, buf
, offs
) :
52 man_ptext(man
, ln
, buf
, offs
);
56 * If the string ends with \c, return a pointer to the backslash.
57 * Otherwise, return NULL.
64 ep
= strchr(start
, '\0') - 2;
65 if (ep
< start
|| ep
[0] != '\\' || ep
[1] != 'c')
67 for (cp
= ep
; cp
> start
; cp
--)
70 return (ep
- cp
) % 2 ? NULL
: ep
;
74 * Rewind all open next-line scopes.
77 man_descope(struct roff_man
*man
, int line
, int offs
, char *start
)
79 /* Trailing \c keeps next-line scope open. */
81 if (start
!= NULL
&& man_hasc(start
) != NULL
)
85 * Co-ordinate what happens with having a next-line scope open:
86 * first close out the element scopes (if applicable),
87 * then close out the block scope (also if applicable).
90 if (man
->flags
& MAN_ELINE
) {
91 while (man
->last
->parent
->type
!= ROFFT_ROOT
&&
92 man_macro(man
->last
->parent
->tok
)->flags
& MAN_ESCOPED
)
93 man_unscope(man
, man
->last
->parent
);
94 man
->flags
&= ~MAN_ELINE
;
96 if ( ! (man
->flags
& MAN_BLINE
))
98 man_unscope(man
, man
->last
->parent
);
99 roff_body_alloc(man
, line
, offs
, man
->last
->tok
);
100 man
->flags
&= ~(MAN_BLINE
| ROFF_NONOFILL
);
104 man_ptext(struct roff_man
*man
, int line
, char *buf
, int offs
)
109 /* In no-fill mode, whitespace is preserved on text lines. */
111 if (man
->flags
& ROFF_NOFILL
) {
112 roff_word_alloc(man
, line
, offs
, buf
+ offs
);
113 man_descope(man
, line
, offs
, buf
+ offs
);
117 for (i
= offs
; buf
[i
] == ' '; i
++)
118 /* Skip leading whitespace. */ ;
121 * Blank lines are ignored in next line scope
122 * and right after headings and cancel preceding \c,
123 * but add a single vertical space elsewhere.
126 if (buf
[i
] == '\0') {
127 if (man
->flags
& (MAN_ELINE
| MAN_BLINE
)) {
128 mandoc_msg(MANDOCERR_BLK_BLANK
, line
, 0, NULL
);
131 if (man
->last
->tok
== MAN_SH
|| man
->last
->tok
== MAN_SS
)
133 if (man
->last
->type
== ROFFT_TEXT
&&
134 ((ep
= man_hasc(man
->last
->string
)) != NULL
)) {
138 roff_elem_alloc(man
, line
, offs
, ROFF_sp
);
139 man
->next
= ROFF_NEXT_SIBLING
;
144 * Warn if the last un-escaped character is whitespace. Then
145 * strip away the remaining spaces (tabs stay!).
148 i
= (int)strlen(buf
);
151 if (' ' == buf
[i
- 1] || '\t' == buf
[i
- 1]) {
152 if (i
> 1 && '\\' != buf
[i
- 2])
153 mandoc_msg(MANDOCERR_SPACE_EOL
, line
, i
- 1, NULL
);
155 for (--i
; i
&& ' ' == buf
[i
]; i
--)
156 /* Spin back to non-space. */ ;
158 /* Jump ahead of escaped whitespace. */
159 i
+= '\\' == buf
[i
] ? 2 : 1;
163 roff_word_alloc(man
, line
, offs
, buf
+ offs
);
166 * End-of-sentence check. If the last character is an unescaped
167 * EOS character, then flag the node as being the end of a
168 * sentence. The front-end will know how to interpret this.
172 if (mandoc_eos(buf
, (size_t)i
))
173 man
->last
->flags
|= NODE_EOS
;
175 man_descope(man
, line
, offs
, buf
+ offs
);
180 man_pmacro(struct roff_man
*man
, int ln
, char *buf
, int offs
)
189 /* Determine the line macro. */
193 for (sz
= 0; sz
< 4 && strchr(" \t\\", buf
[offs
]) == NULL
; sz
++)
195 if (sz
> 0 && sz
< 4)
196 tok
= roffhash_find(man
->manmac
, buf
+ ppos
, sz
);
197 if (tok
== TOKEN_NONE
) {
198 mandoc_msg(MANDOCERR_MACRO
, ln
, ppos
, "%s", buf
+ ppos
- 1);
202 /* Skip a leading escape sequence or tab. */
207 mandoc_escape(&cp
, NULL
, NULL
);
217 /* Jump to the next non-whitespace word. */
219 while (buf
[offs
] == ' ')
223 * Trailing whitespace. Note that tabs are allowed to be passed
224 * into the parser as "text", so we only warn about spaces here.
227 if (buf
[offs
] == '\0' && buf
[offs
- 1] == ' ')
228 mandoc_msg(MANDOCERR_SPACE_EOL
, ln
, offs
- 1, NULL
);
231 * Some macros break next-line scopes; otherwise, remember
232 * whether we are in next-line scope for a block head.
235 man_breakscope(man
, tok
);
236 bline
= man
->flags
& MAN_BLINE
;
239 * If the line in next-line scope ends with \c, keep the
240 * next-line scope open for the subsequent input line.
241 * That is not at all portable, only groff >= 1.22.4
242 * does it, but *if* this weird idiom occurs in a manual
243 * page, that's very likely what the author intended.
246 if (bline
&& man_hasc(buf
+ offs
))
249 /* Call to handler... */
251 (*man_macro(tok
)->fp
)(man
, tok
, ln
, ppos
, &offs
, buf
);
253 /* In quick mode (for mandocdb), abort after the NAME section. */
255 if (man
->quick
&& tok
== MAN_SH
) {
257 if (n
->type
== ROFFT_BODY
&&
258 strcmp(n
->prev
->child
->string
, "NAME"))
263 * If we are in a next-line scope for a block head,
264 * close it out now and switch to the body,
265 * unless the next-line scope is allowed to continue.
269 (man
->flags
& MAN_BLINE
) == 0 ||
270 man
->flags
& MAN_ELINE
||
271 man_macro(tok
)->flags
& MAN_NSCOPED
)
274 man_unscope(man
, man
->last
->parent
);
275 roff_body_alloc(man
, ln
, ppos
, man
->last
->tok
);
276 man
->flags
&= ~(MAN_BLINE
| ROFF_NONOFILL
);
281 * Rewind open next-line scopes
282 * unless the tok request or macro is allowed inside them.
285 man_breakscope(struct roff_man
*man
, int tok
)
290 * An element next line scope is open,
291 * and the new macro is not allowed inside elements.
292 * Delete the element that is being broken.
295 if (man
->flags
& MAN_ELINE
&& (tok
< MAN_TH
||
296 (man_macro(tok
)->flags
& MAN_NSCOPED
) == 0)) {
298 if (n
->type
== ROFFT_TEXT
)
300 if (n
->tok
< MAN_TH
||
301 (man_macro(n
->tok
)->flags
& (MAN_NSCOPED
| MAN_ESCOPED
))
305 mandoc_msg(MANDOCERR_BLK_LINE
, n
->line
, n
->pos
,
306 "%s breaks %s", roff_name
[tok
], roff_name
[n
->tok
]);
307 if (n
->parent
->type
!= ROFFT_ELEM
||
308 (man_macro(n
->parent
->tok
)->flags
&
313 roff_node_delete(man
, n
);
314 man
->flags
&= ~MAN_ELINE
;
318 * Weird special case:
319 * Switching fill mode closes section headers.
322 if (man
->flags
& MAN_BLINE
&&
323 (tok
== ROFF_nf
|| tok
== ROFF_fi
) &&
324 (man
->last
->tok
== MAN_SH
|| man
->last
->tok
== MAN_SS
)) {
327 roff_body_alloc(man
, n
->line
, n
->pos
, n
->tok
);
328 man
->flags
&= ~(MAN_BLINE
| ROFF_NONOFILL
);
332 * A block header next line scope is open,
333 * and the new macro is not allowed inside block headers.
334 * Delete the block that is being broken.
337 if (man
->flags
& MAN_BLINE
&& tok
!= ROFF_nf
&& tok
!= ROFF_fi
&&
338 (tok
< MAN_TH
|| man_macro(tok
)->flags
& MAN_XSCOPE
)) {
340 if (n
->type
== ROFFT_TEXT
)
342 if (n
->tok
< MAN_TH
||
343 (man_macro(n
->tok
)->flags
& MAN_XSCOPE
) == 0)
346 assert(n
->type
== ROFFT_HEAD
);
348 assert(n
->type
== ROFFT_BLOCK
);
349 assert(man_macro(n
->tok
)->flags
& MAN_BSCOPED
);
351 mandoc_msg(MANDOCERR_BLK_LINE
, n
->line
, n
->pos
,
352 "%s breaks %s", roff_name
[tok
], roff_name
[n
->tok
]);
354 roff_node_delete(man
, n
);
355 man
->flags
&= ~(MAN_BLINE
| ROFF_NONOFILL
);