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