xref: /openbsd/usr.bin/mandoc/man.c (revision f1c726aa)
1 /* $OpenBSD: man.c,v 1.137 2022/08/16 22:59:48 schwarze Exp $ */
2 /*
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>
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
man_parseln(struct roff_man * man,int ln,char * buf,int offs)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 *
man_hasc(char * start)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 /*
72  * Rewind all open next-line scopes.
73  */
74 void
man_descope(struct roff_man * man,int line,int offs,char * start)75 man_descope(struct roff_man *man, int line, int offs, char *start)
76 {
77 	/* First close out all next-line element scopes, if any. */
78 
79 	if (man->flags & MAN_ELINE) {
80 		while (man->last->parent->type != ROFFT_ROOT &&
81 		    man_macro(man->last->parent->tok)->flags & MAN_ESCOPED)
82 			man_unscope(man, man->last->parent);
83 		man->flags &= ~MAN_ELINE;
84 	}
85 
86 	/* Trailing \c keeps next-line block scope open. */
87 
88 	if (start != NULL && man_hasc(start) != NULL)
89 		return;
90 
91 	/* Close out the next-line block scope, if there is one. */
92 
93 	if ( ! (man->flags & MAN_BLINE))
94 		return;
95 	man_unscope(man, man->last->parent);
96 	roff_body_alloc(man, line, offs, man->last->tok);
97 	man->flags &= ~(MAN_BLINE | ROFF_NONOFILL);
98 }
99 
100 static int
man_ptext(struct roff_man * man,int line,char * buf,int offs)101 man_ptext(struct roff_man *man, int line, char *buf, int offs)
102 {
103 	int		 i;
104 	char		*ep;
105 
106 	/* In no-fill mode, whitespace is preserved on text lines. */
107 
108 	if (man->flags & ROFF_NOFILL) {
109 		roff_word_alloc(man, line, offs, buf + offs);
110 		man_descope(man, line, offs, buf + offs);
111 		return 1;
112 	}
113 
114 	for (i = offs; buf[i] == ' '; i++)
115 		/* Skip leading whitespace. */ ;
116 
117 	/*
118 	 * Blank lines are ignored in next line scope
119 	 * and right after headings and cancel preceding \c,
120 	 * but add a single vertical space elsewhere.
121 	 */
122 
123 	if (buf[i] == '\0') {
124 		if (man->flags & (MAN_ELINE | MAN_BLINE)) {
125 			mandoc_msg(MANDOCERR_BLK_BLANK, line, 0, NULL);
126 			return 1;
127 		}
128 		if (man->last->tok == MAN_SH || man->last->tok == MAN_SS)
129 			return 1;
130 		if (man->last->type == ROFFT_TEXT &&
131 		    ((ep = man_hasc(man->last->string)) != NULL)) {
132 			*ep = '\0';
133 			return 1;
134 		}
135 		roff_elem_alloc(man, line, offs, ROFF_sp);
136 		man->next = ROFF_NEXT_SIBLING;
137 		return 1;
138 	}
139 
140 	/*
141 	 * Warn if the last un-escaped character is whitespace. Then
142 	 * strip away the remaining spaces (tabs stay!).
143 	 */
144 
145 	i = (int)strlen(buf);
146 	assert(i);
147 
148 	if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
149 		if (i > 1 && '\\' != buf[i - 2])
150 			mandoc_msg(MANDOCERR_SPACE_EOL, 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
man_pmacro(struct roff_man * man,int ln,char * buf,int offs)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, ln, ppos, "%s", buf + ppos - 1);
196 		return 1;
197 	}
198 
199 	/* Skip a leading escape sequence or tab. */
200 
201 	switch (buf[offs]) {
202 	case '\\':
203 		cp = buf + offs + 1;
204 		mandoc_escape(&cp, NULL, NULL);
205 		offs = cp - buf;
206 		break;
207 	case '\t':
208 		offs++;
209 		break;
210 	default:
211 		break;
212 	}
213 
214 	/* Jump to the next non-whitespace word. */
215 
216 	while (buf[offs] == ' ')
217 		offs++;
218 
219 	/*
220 	 * Trailing whitespace.  Note that tabs are allowed to be passed
221 	 * into the parser as "text", so we only warn about spaces here.
222 	 */
223 
224 	if (buf[offs] == '\0' && buf[offs - 1] == ' ')
225 		mandoc_msg(MANDOCERR_SPACE_EOL, ln, offs - 1, NULL);
226 
227 	/*
228 	 * Some macros break next-line scopes; otherwise, remember
229 	 * whether we are in next-line scope for a block head.
230 	 */
231 
232 	man_breakscope(man, tok);
233 	bline = man->flags & MAN_BLINE;
234 
235 	/*
236 	 * If the line in next-line scope ends with \c, keep the
237 	 * next-line scope open for the subsequent input line.
238 	 * That is not at all portable, only groff >= 1.22.4
239 	 * does it, but *if* this weird idiom occurs in a manual
240 	 * page, that's very likely what the author intended.
241 	 */
242 
243 	if (bline && man_hasc(buf + offs))
244 		bline = 0;
245 
246 	/* Call to handler... */
247 
248 	(*man_macro(tok)->fp)(man, tok, ln, ppos, &offs, buf);
249 
250 	/* In quick mode (for mandocdb), abort after the NAME section. */
251 
252 	if (man->quick && tok == MAN_SH) {
253 		n = man->last;
254 		if (n->type == ROFFT_BODY &&
255 		    strcmp(n->prev->child->string, "NAME"))
256 			return 2;
257 	}
258 
259 	/*
260 	 * If we are in a next-line scope for a block head,
261 	 * close it out now and switch to the body,
262 	 * unless the next-line scope is allowed to continue.
263 	 */
264 
265 	if (bline == 0 ||
266 	    (man->flags & MAN_BLINE) == 0 ||
267 	    man->flags & MAN_ELINE ||
268 	    man_macro(tok)->flags & MAN_NSCOPED)
269 		return 1;
270 
271 	man_unscope(man, man->last->parent);
272 	roff_body_alloc(man, ln, ppos, man->last->tok);
273 	man->flags &= ~(MAN_BLINE | ROFF_NONOFILL);
274 	return 1;
275 }
276 
277 /*
278  * Rewind open next-line scopes
279  * unless the tok request or macro is allowed inside them.
280  */
281 void
man_breakscope(struct roff_man * man,int tok)282 man_breakscope(struct roff_man *man, int tok)
283 {
284 	struct roff_node *n;
285 
286 	/*
287 	 * An element next line scope is open,
288 	 * and the new macro is not allowed inside elements.
289 	 * Delete the element that is being broken.
290 	 */
291 
292 	if (man->flags & MAN_ELINE && (tok < MAN_TH ||
293 	    (man_macro(tok)->flags & MAN_NSCOPED) == 0)) {
294 		n = man->last;
295 		if (n->type == ROFFT_TEXT)
296 			n = n->parent;
297 		if (n->tok < MAN_TH ||
298 		    (man_macro(n->tok)->flags & (MAN_NSCOPED | MAN_ESCOPED))
299 		     == MAN_NSCOPED)
300 			n = n->parent;
301 		for (;;) {
302 			mandoc_msg(MANDOCERR_BLK_LINE, n->line, n->pos,
303 			    "%s breaks %s", roff_name[tok], roff_name[n->tok]);
304 			if (n->parent->type != ROFFT_ELEM ||
305 			    (man_macro(n->parent->tok)->flags &
306 			     MAN_ESCOPED) == 0)
307 				break;
308 			n = n->parent;
309 		}
310 		roff_node_delete(man, n);
311 		man->flags &= ~MAN_ELINE;
312 	}
313 
314 	/*
315 	 * Weird special case:
316 	 * Switching fill mode closes section headers.
317 	 */
318 
319 	if (man->flags & MAN_BLINE &&
320 	    (tok == ROFF_nf || tok == ROFF_fi) &&
321 	    (man->last->tok == MAN_SH || man->last->tok == MAN_SS)) {
322 		n = man->last;
323 		man_unscope(man, n);
324 		roff_body_alloc(man, n->line, n->pos, n->tok);
325 		man->flags &= ~(MAN_BLINE | ROFF_NONOFILL);
326 	}
327 
328 	/*
329 	 * A block header next line scope is open,
330 	 * and the new macro is not allowed inside block headers.
331 	 * Delete the block that is being broken.
332 	 */
333 
334 	if (man->flags & MAN_BLINE && tok != ROFF_nf && tok != ROFF_fi &&
335 	    (tok < MAN_TH || man_macro(tok)->flags & MAN_XSCOPE)) {
336 		n = man->last;
337 		if (n->type == ROFFT_TEXT)
338 			n = n->parent;
339 		if (n->tok < MAN_TH ||
340 		    (man_macro(n->tok)->flags & MAN_XSCOPE) == 0)
341 			n = n->parent;
342 
343 		assert(n->type == ROFFT_HEAD);
344 		n = n->parent;
345 		assert(n->type == ROFFT_BLOCK);
346 		assert(man_macro(n->tok)->flags & MAN_BSCOPED);
347 
348 		mandoc_msg(MANDOCERR_BLK_LINE, n->line, n->pos,
349 		    "%s breaks %s", roff_name[tok], roff_name[n->tok]);
350 
351 		roff_node_delete(man, n);
352 		man->flags &= ~(MAN_BLINE | ROFF_NONOFILL);
353 	}
354 }
355