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