xref: /openbsd-src/usr.bin/mandoc/man.c (revision d1df930ffab53da22f3324c32bed7ac5709915e6)
1 /*	$OpenBSD: man.c,v 1.128 2018/08/26 16:18:38 schwarze Exp $ */
2 /*
3  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2013,2014,2015,2017,2018 Ingo Schwarze <schwarze@openbsd.org>
5  * Copyright (c) 2011 Joerg Sonnenberger <joerg@netbsd.org>
6  *
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.
10  *
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.
18  */
19 #include <sys/types.h>
20 
21 #include <assert.h>
22 #include <ctype.h>
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 
28 #include "mandoc_aux.h"
29 #include "mandoc.h"
30 #include "roff.h"
31 #include "man.h"
32 #include "libmandoc.h"
33 #include "roff_int.h"
34 #include "libman.h"
35 
36 static	char		*man_hasc(char *);
37 static	int		 man_ptext(struct roff_man *, int, char *, int);
38 static	int		 man_pmacro(struct roff_man *, int, char *, int);
39 
40 
41 int
42 man_parseln(struct roff_man *man, int ln, char *buf, int offs)
43 {
44 
45 	if (man->last->type != ROFFT_EQN || ln > man->last->line)
46 		man->flags |= MAN_NEWLINE;
47 
48 	return roff_getcontrol(man->roff, buf, &offs) ?
49 	    man_pmacro(man, ln, buf, offs) :
50 	    man_ptext(man, ln, buf, offs);
51 }
52 
53 /*
54  * If the string ends with \c, return a pointer to the backslash.
55  * Otherwise, return NULL.
56  */
57 static char *
58 man_hasc(char *start)
59 {
60 	char	*cp, *ep;
61 
62 	ep = strchr(start, '\0') - 2;
63 	if (ep < start || ep[0] != '\\' || ep[1] != 'c')
64 		return NULL;
65 	for (cp = ep; cp > start; cp--)
66 		if (cp[-1] != '\\')
67 			break;
68 	return (ep - cp) % 2 ? NULL : ep;
69 }
70 
71 void
72 man_descope(struct roff_man *man, int line, int offs, char *start)
73 {
74 	/* Trailing \c keeps next-line scope open. */
75 
76 	if (start != NULL && man_hasc(start) != NULL)
77 		return;
78 
79 	/*
80 	 * Co-ordinate what happens with having a next-line scope open:
81 	 * first close out the element scopes (if applicable),
82 	 * then close out the block scope (also if applicable).
83 	 */
84 
85 	if (man->flags & MAN_ELINE) {
86 		while (man->last->parent->type != ROFFT_ROOT &&
87 		    man_macro(man->last->parent->tok)->flags & MAN_ESCOPED)
88 			man_unscope(man, man->last->parent);
89 		man->flags &= ~MAN_ELINE;
90 	}
91 	if ( ! (man->flags & MAN_BLINE))
92 		return;
93 	man->flags &= ~MAN_BLINE;
94 	man_unscope(man, man->last->parent);
95 	roff_body_alloc(man, line, offs, man->last->tok);
96 }
97 
98 static int
99 man_ptext(struct roff_man *man, int line, char *buf, int offs)
100 {
101 	int		 i;
102 	char		*ep;
103 
104 	/* Literal free-form text whitespace is preserved. */
105 
106 	if (man->flags & MAN_LITERAL) {
107 		roff_word_alloc(man, line, offs, buf + offs);
108 		man_descope(man, line, offs, buf + offs);
109 		return 1;
110 	}
111 
112 	for (i = offs; buf[i] == ' '; i++)
113 		/* Skip leading whitespace. */ ;
114 
115 	/*
116 	 * Blank lines are ignored in next line scope
117 	 * and right after headings and cancel preceding \c,
118 	 * but add a single vertical space elsewhere.
119 	 */
120 
121 	if (buf[i] == '\0') {
122 		if (man->flags & (MAN_ELINE | MAN_BLINE)) {
123 			mandoc_msg(MANDOCERR_BLK_BLANK, man->parse,
124 			    line, 0, NULL);
125 			return 1;
126 		}
127 		if (man->last->tok == MAN_SH || man->last->tok == MAN_SS)
128 			return 1;
129 		if (man->last->type == ROFFT_TEXT &&
130 		    ((ep = man_hasc(man->last->string)) != NULL)) {
131 			*ep = '\0';
132 			return 1;
133 		}
134 		roff_elem_alloc(man, line, offs, ROFF_sp);
135 		man->next = ROFF_NEXT_SIBLING;
136 		return 1;
137 	}
138 
139 	/*
140 	 * Warn if the last un-escaped character is whitespace. Then
141 	 * strip away the remaining spaces (tabs stay!).
142 	 */
143 
144 	i = (int)strlen(buf);
145 	assert(i);
146 
147 	if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
148 		if (i > 1 && '\\' != buf[i - 2])
149 			mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
150 			    line, i - 1, NULL);
151 
152 		for (--i; i && ' ' == buf[i]; i--)
153 			/* Spin back to non-space. */ ;
154 
155 		/* Jump ahead of escaped whitespace. */
156 		i += '\\' == buf[i] ? 2 : 1;
157 
158 		buf[i] = '\0';
159 	}
160 	roff_word_alloc(man, line, offs, buf + offs);
161 
162 	/*
163 	 * End-of-sentence check.  If the last character is an unescaped
164 	 * EOS character, then flag the node as being the end of a
165 	 * sentence.  The front-end will know how to interpret this.
166 	 */
167 
168 	assert(i);
169 	if (mandoc_eos(buf, (size_t)i))
170 		man->last->flags |= NODE_EOS;
171 
172 	man_descope(man, line, offs, buf + offs);
173 	return 1;
174 }
175 
176 static int
177 man_pmacro(struct roff_man *man, int ln, char *buf, int offs)
178 {
179 	struct roff_node *n;
180 	const char	*cp;
181 	size_t		 sz;
182 	enum roff_tok	 tok;
183 	int		 ppos;
184 	int		 bline;
185 
186 	/* Determine the line macro. */
187 
188 	ppos = offs;
189 	tok = TOKEN_NONE;
190 	for (sz = 0; sz < 4 && strchr(" \t\\", buf[offs]) == NULL; sz++)
191 		offs++;
192 	if (sz > 0 && sz < 4)
193 		tok = roffhash_find(man->manmac, buf + ppos, sz);
194 	if (tok == TOKEN_NONE) {
195 		mandoc_msg(MANDOCERR_MACRO, man->parse,
196 		    ln, ppos, buf + ppos - 1);
197 		return 1;
198 	}
199 
200 	/* Skip a leading escape sequence or tab. */
201 
202 	switch (buf[offs]) {
203 	case '\\':
204 		cp = buf + offs + 1;
205 		mandoc_escape(&cp, NULL, NULL);
206 		offs = cp - buf;
207 		break;
208 	case '\t':
209 		offs++;
210 		break;
211 	default:
212 		break;
213 	}
214 
215 	/* Jump to the next non-whitespace word. */
216 
217 	while (buf[offs] == ' ')
218 		offs++;
219 
220 	/*
221 	 * Trailing whitespace.  Note that tabs are allowed to be passed
222 	 * into the parser as "text", so we only warn about spaces here.
223 	 */
224 
225 	if (buf[offs] == '\0' && buf[offs - 1] == ' ')
226 		mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
227 		    ln, offs - 1, NULL);
228 
229 	/*
230 	 * Some macros break next-line scopes; otherwise, remember
231 	 * whether we are in next-line scope for a block head.
232 	 */
233 
234 	man_breakscope(man, tok);
235 	bline = man->flags & MAN_BLINE;
236 
237 	/*
238 	 * If the line in next-line scope ends with \c, keep the
239 	 * next-line scope open for the subsequent input line.
240 	 * That is not at all portable, only groff >= 1.22.4
241 	 * does it, but *if* this weird idiom occurs in a manual
242 	 * page, that's very likely what the author intended.
243 	 */
244 
245 	if (bline && man_hasc(buf + offs))
246 		bline = 0;
247 
248 	/* Call to handler... */
249 
250 	(*man_macro(tok)->fp)(man, tok, ln, ppos, &offs, buf);
251 
252 	/* In quick mode (for mandocdb), abort after the NAME section. */
253 
254 	if (man->quick && tok == MAN_SH) {
255 		n = man->last;
256 		if (n->type == ROFFT_BODY &&
257 		    strcmp(n->prev->child->string, "NAME"))
258 			return 2;
259 	}
260 
261 	/*
262 	 * If we are in a next-line scope for a block head,
263 	 * close it out now and switch to the body,
264 	 * unless the next-line scope is allowed to continue.
265 	 */
266 
267 	if (bline == 0 ||
268 	    (man->flags & MAN_BLINE) == 0 ||
269 	    man->flags & MAN_ELINE ||
270 	    man_macro(tok)->flags & MAN_NSCOPED)
271 		return 1;
272 
273 	man->flags &= ~MAN_BLINE;
274 	man_unscope(man, man->last->parent);
275 	roff_body_alloc(man, ln, ppos, man->last->tok);
276 	return 1;
277 }
278 
279 void
280 man_breakscope(struct roff_man *man, int tok)
281 {
282 	struct roff_node *n;
283 
284 	/*
285 	 * An element next line scope is open,
286 	 * and the new macro is not allowed inside elements.
287 	 * Delete the element that is being broken.
288 	 */
289 
290 	if (man->flags & MAN_ELINE && (tok < MAN_TH ||
291 	    (man_macro(tok)->flags & MAN_NSCOPED) == 0)) {
292 		n = man->last;
293 		if (n->type == ROFFT_TEXT)
294 			n = n->parent;
295 		if (n->tok < MAN_TH ||
296 		    (man_macro(n->tok)->flags & (MAN_NSCOPED | MAN_ESCOPED))
297 		     == MAN_NSCOPED)
298 			n = n->parent;
299 
300 		mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse,
301 		    n->line, n->pos, "%s breaks %s",
302 		    roff_name[tok], roff_name[n->tok]);
303 
304 		roff_node_delete(man, n);
305 		man->flags &= ~MAN_ELINE;
306 	}
307 
308 	/*
309 	 * Weird special case:
310 	 * Switching fill mode closes section headers.
311 	 */
312 
313 	if (man->flags & MAN_BLINE &&
314 	    (tok == MAN_nf || tok == MAN_fi) &&
315 	    (man->last->tok == MAN_SH || man->last->tok == MAN_SS)) {
316 		n = man->last;
317 		man_unscope(man, n);
318 		roff_body_alloc(man, n->line, n->pos, n->tok);
319 		man->flags &= ~MAN_BLINE;
320 	}
321 
322 	/*
323 	 * A block header next line scope is open,
324 	 * and the new macro is not allowed inside block headers.
325 	 * Delete the block that is being broken.
326 	 */
327 
328 	if (man->flags & MAN_BLINE && (tok < MAN_TH ||
329 	    man_macro(tok)->flags & MAN_XSCOPE)) {
330 		n = man->last;
331 		if (n->type == ROFFT_TEXT)
332 			n = n->parent;
333 		if (n->tok < MAN_TH ||
334 		    (man_macro(n->tok)->flags & MAN_XSCOPE) == 0)
335 			n = n->parent;
336 
337 		assert(n->type == ROFFT_HEAD);
338 		n = n->parent;
339 		assert(n->type == ROFFT_BLOCK);
340 		assert(man_macro(n->tok)->flags & MAN_BSCOPED);
341 
342 		mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse,
343 		    n->line, n->pos, "%s breaks %s",
344 		    roff_name[tok], roff_name[n->tok]);
345 
346 		roff_node_delete(man, n);
347 		man->flags &= ~MAN_BLINE;
348 	}
349 }
350 
351 void
352 man_state(struct roff_man *man, struct roff_node *n)
353 {
354 
355 	switch(n->tok) {
356 	case MAN_nf:
357 	case MAN_EX:
358 		if (man->flags & MAN_LITERAL && ! (n->flags & NODE_VALID))
359 			mandoc_msg(MANDOCERR_NF_SKIP, man->parse,
360 			    n->line, n->pos, "nf");
361 		man->flags |= MAN_LITERAL;
362 		break;
363 	case MAN_fi:
364 	case MAN_EE:
365 		if ( ! (man->flags & MAN_LITERAL) &&
366 		     ! (n->flags & NODE_VALID))
367 			mandoc_msg(MANDOCERR_FI_SKIP, man->parse,
368 			    n->line, n->pos, "fi");
369 		man->flags &= ~MAN_LITERAL;
370 		break;
371 	default:
372 		break;
373 	}
374 	man->last->flags |= NODE_VALID;
375 }
376 
377 void
378 man_validate(struct roff_man *man)
379 {
380 
381 	man->last = man->first;
382 	man_node_validate(man);
383 	man->flags &= ~MAN_LITERAL;
384 }
385