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