1*cec8643bSMichal Nowak /* $Id: man.c,v 1.187 2019/01/05 00:36:50 schwarze Exp $ */
295c635efSGarrett D'Amore /*
395c635efSGarrett D'Amore * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4*cec8643bSMichal Nowak * Copyright (c) 2013-2015, 2017-2019 Ingo Schwarze <schwarze@openbsd.org>
5260e9a87SYuri Pankov * Copyright (c) 2011 Joerg Sonnenberger <joerg@netbsd.org>
695c635efSGarrett D'Amore *
795c635efSGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
895c635efSGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
995c635efSGarrett D'Amore * copyright notice and this permission notice appear in all copies.
1095c635efSGarrett D'Amore *
11371584c2SYuri Pankov * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1295c635efSGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13371584c2SYuri Pankov * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1495c635efSGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1595c635efSGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1695c635efSGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1795c635efSGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1895c635efSGarrett D'Amore */
1995c635efSGarrett D'Amore #include "config.h"
2095c635efSGarrett D'Amore
2195c635efSGarrett D'Amore #include <sys/types.h>
2295c635efSGarrett D'Amore
2395c635efSGarrett D'Amore #include <assert.h>
24260e9a87SYuri Pankov #include <ctype.h>
2595c635efSGarrett D'Amore #include <stdarg.h>
2695c635efSGarrett D'Amore #include <stdlib.h>
2795c635efSGarrett D'Amore #include <stdio.h>
2895c635efSGarrett D'Amore #include <string.h>
2995c635efSGarrett D'Amore
30260e9a87SYuri Pankov #include "mandoc_aux.h"
31371584c2SYuri Pankov #include "mandoc.h"
32371584c2SYuri Pankov #include "roff.h"
33371584c2SYuri Pankov #include "man.h"
3495c635efSGarrett D'Amore #include "libmandoc.h"
35371584c2SYuri Pankov #include "roff_int.h"
36371584c2SYuri Pankov #include "libman.h"
3795c635efSGarrett D'Amore
38*cec8643bSMichal Nowak static char *man_hasc(char *);
39371584c2SYuri Pankov static int man_ptext(struct roff_man *, int, char *, int);
40371584c2SYuri Pankov static int man_pmacro(struct roff_man *, int, char *, int);
4195c635efSGarrett D'Amore
4295c635efSGarrett D'Amore
4395c635efSGarrett D'Amore int
man_parseln(struct roff_man * man,int ln,char * buf,int offs)44371584c2SYuri Pankov man_parseln(struct roff_man *man, int ln, char *buf, int offs)
4595c635efSGarrett D'Amore {
4695c635efSGarrett D'Amore
47371584c2SYuri Pankov if (man->last->type != ROFFT_EQN || ln > man->last->line)
48260e9a87SYuri Pankov man->flags |= MAN_NEWLINE;
4995c635efSGarrett D'Amore
50371584c2SYuri Pankov return roff_getcontrol(man->roff, buf, &offs) ?
51260e9a87SYuri Pankov man_pmacro(man, ln, buf, offs) :
52371584c2SYuri Pankov man_ptext(man, ln, buf, offs);
5395c635efSGarrett D'Amore }
5495c635efSGarrett D'Amore
55*cec8643bSMichal Nowak /*
56*cec8643bSMichal Nowak * If the string ends with \c, return a pointer to the backslash.
57*cec8643bSMichal Nowak * Otherwise, return NULL.
58*cec8643bSMichal Nowak */
59*cec8643bSMichal Nowak static char *
man_hasc(char * start)60*cec8643bSMichal Nowak man_hasc(char *start)
6195c635efSGarrett D'Amore {
62*cec8643bSMichal Nowak char *cp, *ep;
63*cec8643bSMichal Nowak
64*cec8643bSMichal Nowak ep = strchr(start, '\0') - 2;
65*cec8643bSMichal Nowak if (ep < start || ep[0] != '\\' || ep[1] != 'c')
66*cec8643bSMichal Nowak return NULL;
67*cec8643bSMichal Nowak for (cp = ep; cp > start; cp--)
68*cec8643bSMichal Nowak if (cp[-1] != '\\')
69*cec8643bSMichal Nowak break;
70*cec8643bSMichal Nowak return (ep - cp) % 2 ? NULL : ep;
71*cec8643bSMichal Nowak }
72*cec8643bSMichal Nowak
73*cec8643bSMichal Nowak void
man_descope(struct roff_man * man,int line,int offs,char * start)74*cec8643bSMichal Nowak man_descope(struct roff_man *man, int line, int offs, char *start)
75*cec8643bSMichal Nowak {
76*cec8643bSMichal Nowak /* Trailing \c keeps next-line scope open. */
77*cec8643bSMichal Nowak
78*cec8643bSMichal Nowak if (start != NULL && man_hasc(start) != NULL)
79*cec8643bSMichal Nowak return;
80*cec8643bSMichal Nowak
8195c635efSGarrett D'Amore /*
8295c635efSGarrett D'Amore * Co-ordinate what happens with having a next-line scope open:
83*cec8643bSMichal Nowak * first close out the element scopes (if applicable),
84*cec8643bSMichal Nowak * then close out the block scope (also if applicable).
8595c635efSGarrett D'Amore */
8695c635efSGarrett D'Amore
87260e9a87SYuri Pankov if (man->flags & MAN_ELINE) {
88*cec8643bSMichal Nowak while (man->last->parent->type != ROFFT_ROOT &&
89*cec8643bSMichal Nowak man_macro(man->last->parent->tok)->flags & MAN_ESCOPED)
90*cec8643bSMichal Nowak man_unscope(man, man->last->parent);
91698f87a4SGarrett D'Amore man->flags &= ~MAN_ELINE;
9295c635efSGarrett D'Amore }
93260e9a87SYuri Pankov if ( ! (man->flags & MAN_BLINE))
94260e9a87SYuri Pankov return;
95260e9a87SYuri Pankov man_unscope(man, man->last->parent);
96371584c2SYuri Pankov roff_body_alloc(man, line, offs, man->last->tok);
97*cec8643bSMichal Nowak man->flags &= ~(MAN_BLINE | ROFF_NONOFILL);
9895c635efSGarrett D'Amore }
9995c635efSGarrett D'Amore
10095c635efSGarrett D'Amore static int
man_ptext(struct roff_man * man,int line,char * buf,int offs)101371584c2SYuri Pankov man_ptext(struct roff_man *man, int line, char *buf, int offs)
10295c635efSGarrett D'Amore {
10395c635efSGarrett D'Amore int i;
104c66b8046SYuri Pankov char *ep;
10595c635efSGarrett D'Amore
106*cec8643bSMichal Nowak /* In no-fill mode, whitespace is preserved on text lines. */
10795c635efSGarrett D'Amore
108*cec8643bSMichal Nowak if (man->flags & ROFF_NOFILL) {
109371584c2SYuri Pankov roff_word_alloc(man, line, offs, buf + offs);
110*cec8643bSMichal Nowak man_descope(man, line, offs, buf + offs);
111371584c2SYuri Pankov return 1;
11295c635efSGarrett D'Amore }
11395c635efSGarrett D'Amore
114260e9a87SYuri Pankov for (i = offs; buf[i] == ' '; i++)
11595c635efSGarrett D'Amore /* Skip leading whitespace. */ ;
11695c635efSGarrett D'Amore
117698f87a4SGarrett D'Amore /*
118c66b8046SYuri Pankov * Blank lines are ignored in next line scope
119c66b8046SYuri Pankov * and right after headings and cancel preceding \c,
120698f87a4SGarrett D'Amore * but add a single vertical space elsewhere.
121698f87a4SGarrett D'Amore */
122698f87a4SGarrett D'Amore
123260e9a87SYuri Pankov if (buf[i] == '\0') {
124c66b8046SYuri Pankov if (man->flags & (MAN_ELINE | MAN_BLINE)) {
125*cec8643bSMichal Nowak mandoc_msg(MANDOCERR_BLK_BLANK, line, 0, NULL);
126c66b8046SYuri Pankov return 1;
127c66b8046SYuri Pankov }
128c66b8046SYuri Pankov if (man->last->tok == MAN_SH || man->last->tok == MAN_SS)
129c66b8046SYuri Pankov return 1;
130*cec8643bSMichal Nowak if (man->last->type == ROFFT_TEXT &&
131*cec8643bSMichal Nowak ((ep = man_hasc(man->last->string)) != NULL)) {
132c66b8046SYuri Pankov *ep = '\0';
133c66b8046SYuri Pankov return 1;
134698f87a4SGarrett D'Amore }
135c66b8046SYuri Pankov roff_elem_alloc(man, line, offs, ROFF_sp);
136c66b8046SYuri Pankov man->next = ROFF_NEXT_SIBLING;
137371584c2SYuri Pankov return 1;
13895c635efSGarrett D'Amore }
13995c635efSGarrett D'Amore
140260e9a87SYuri Pankov /*
14195c635efSGarrett D'Amore * Warn if the last un-escaped character is whitespace. Then
142260e9a87SYuri Pankov * strip away the remaining spaces (tabs stay!).
14395c635efSGarrett D'Amore */
14495c635efSGarrett D'Amore
14595c635efSGarrett D'Amore i = (int)strlen(buf);
14695c635efSGarrett D'Amore assert(i);
14795c635efSGarrett D'Amore
14895c635efSGarrett D'Amore if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
14995c635efSGarrett D'Amore if (i > 1 && '\\' != buf[i - 2])
150*cec8643bSMichal Nowak mandoc_msg(MANDOCERR_SPACE_EOL, line, i - 1, NULL);
15195c635efSGarrett D'Amore
15295c635efSGarrett D'Amore for (--i; i && ' ' == buf[i]; i--)
15395c635efSGarrett D'Amore /* Spin back to non-space. */ ;
15495c635efSGarrett D'Amore
15595c635efSGarrett D'Amore /* Jump ahead of escaped whitespace. */
15695c635efSGarrett D'Amore i += '\\' == buf[i] ? 2 : 1;
15795c635efSGarrett D'Amore
15895c635efSGarrett D'Amore buf[i] = '\0';
15995c635efSGarrett D'Amore }
160371584c2SYuri Pankov roff_word_alloc(man, line, offs, buf + offs);
16195c635efSGarrett D'Amore
16295c635efSGarrett D'Amore /*
16395c635efSGarrett D'Amore * End-of-sentence check. If the last character is an unescaped
16495c635efSGarrett D'Amore * EOS character, then flag the node as being the end of a
16595c635efSGarrett D'Amore * sentence. The front-end will know how to interpret this.
16695c635efSGarrett D'Amore */
16795c635efSGarrett D'Amore
16895c635efSGarrett D'Amore assert(i);
169260e9a87SYuri Pankov if (mandoc_eos(buf, (size_t)i))
170a40ea1a7SYuri Pankov man->last->flags |= NODE_EOS;
17195c635efSGarrett D'Amore
172*cec8643bSMichal Nowak man_descope(man, line, offs, buf + offs);
173371584c2SYuri Pankov return 1;
17495c635efSGarrett D'Amore }
17595c635efSGarrett D'Amore
17695c635efSGarrett D'Amore static int
man_pmacro(struct roff_man * man,int ln,char * buf,int offs)177371584c2SYuri Pankov man_pmacro(struct roff_man *man, int ln, char *buf, int offs)
17895c635efSGarrett D'Amore {
179371584c2SYuri Pankov struct roff_node *n;
180260e9a87SYuri Pankov const char *cp;
181c66b8046SYuri Pankov size_t sz;
182c66b8046SYuri Pankov enum roff_tok tok;
183c66b8046SYuri Pankov int ppos;
184260e9a87SYuri Pankov int bline;
18595c635efSGarrett D'Amore
186c66b8046SYuri Pankov /* Determine the line macro. */
18795c635efSGarrett D'Amore
188c66b8046SYuri Pankov ppos = offs;
189c66b8046SYuri Pankov tok = TOKEN_NONE;
190c66b8046SYuri Pankov for (sz = 0; sz < 4 && strchr(" \t\\", buf[offs]) == NULL; sz++)
191c66b8046SYuri Pankov offs++;
192c66b8046SYuri Pankov if (sz > 0 && sz < 4)
193c66b8046SYuri Pankov tok = roffhash_find(man->manmac, buf + ppos, sz);
194371584c2SYuri Pankov if (tok == TOKEN_NONE) {
195*cec8643bSMichal Nowak mandoc_msg(MANDOCERR_MACRO, ln, ppos, "%s", buf + ppos - 1);
196371584c2SYuri Pankov return 1;
19795c635efSGarrett D'Amore }
19895c635efSGarrett D'Amore
199260e9a87SYuri Pankov /* Skip a leading escape sequence or tab. */
20095c635efSGarrett D'Amore
201260e9a87SYuri Pankov switch (buf[offs]) {
202260e9a87SYuri Pankov case '\\':
203260e9a87SYuri Pankov cp = buf + offs + 1;
204260e9a87SYuri Pankov mandoc_escape(&cp, NULL, NULL);
205260e9a87SYuri Pankov offs = cp - buf;
206260e9a87SYuri Pankov break;
207260e9a87SYuri Pankov case '\t':
20895c635efSGarrett D'Amore offs++;
209260e9a87SYuri Pankov break;
210260e9a87SYuri Pankov default:
211260e9a87SYuri Pankov break;
212260e9a87SYuri Pankov }
213260e9a87SYuri Pankov
214260e9a87SYuri Pankov /* Jump to the next non-whitespace word. */
21595c635efSGarrett D'Amore
216c66b8046SYuri Pankov while (buf[offs] == ' ')
217260e9a87SYuri Pankov offs++;
218260e9a87SYuri Pankov
219260e9a87SYuri Pankov /*
22095c635efSGarrett D'Amore * Trailing whitespace. Note that tabs are allowed to be passed
22195c635efSGarrett D'Amore * into the parser as "text", so we only warn about spaces here.
22295c635efSGarrett D'Amore */
22395c635efSGarrett D'Amore
224260e9a87SYuri Pankov if (buf[offs] == '\0' && buf[offs - 1] == ' ')
225*cec8643bSMichal Nowak mandoc_msg(MANDOCERR_SPACE_EOL, ln, offs - 1, NULL);
22695c635efSGarrett D'Amore
227260e9a87SYuri Pankov /*
228260e9a87SYuri Pankov * Some macros break next-line scopes; otherwise, remember
229260e9a87SYuri Pankov * whether we are in next-line scope for a block head.
23095c635efSGarrett D'Amore */
23195c635efSGarrett D'Amore
232260e9a87SYuri Pankov man_breakscope(man, tok);
233260e9a87SYuri Pankov bline = man->flags & MAN_BLINE;
23495c635efSGarrett D'Amore
235c66b8046SYuri Pankov /*
236c66b8046SYuri Pankov * If the line in next-line scope ends with \c, keep the
237c66b8046SYuri Pankov * next-line scope open for the subsequent input line.
238c66b8046SYuri Pankov * That is not at all portable, only groff >= 1.22.4
239c66b8046SYuri Pankov * does it, but *if* this weird idiom occurs in a manual
240c66b8046SYuri Pankov * page, that's very likely what the author intended.
241c66b8046SYuri Pankov */
242c66b8046SYuri Pankov
243*cec8643bSMichal Nowak if (bline && man_hasc(buf + offs))
244*cec8643bSMichal Nowak bline = 0;
245c66b8046SYuri Pankov
246260e9a87SYuri Pankov /* Call to handler... */
24795c635efSGarrett D'Amore
248*cec8643bSMichal Nowak (*man_macro(tok)->fp)(man, tok, ln, ppos, &offs, buf);
24995c635efSGarrett D'Amore
250260e9a87SYuri Pankov /* In quick mode (for mandocdb), abort after the NAME section. */
25195c635efSGarrett D'Amore
252260e9a87SYuri Pankov if (man->quick && tok == MAN_SH) {
253260e9a87SYuri Pankov n = man->last;
254371584c2SYuri Pankov if (n->type == ROFFT_BODY &&
255260e9a87SYuri Pankov strcmp(n->prev->child->string, "NAME"))
256371584c2SYuri Pankov return 2;
25795c635efSGarrett D'Amore }
25895c635efSGarrett D'Amore
25995c635efSGarrett D'Amore /*
260260e9a87SYuri Pankov * If we are in a next-line scope for a block head,
261260e9a87SYuri Pankov * close it out now and switch to the body,
262260e9a87SYuri Pankov * unless the next-line scope is allowed to continue.
26395c635efSGarrett D'Amore */
26495c635efSGarrett D'Amore
265*cec8643bSMichal Nowak if (bline == 0 ||
266*cec8643bSMichal Nowak (man->flags & MAN_BLINE) == 0 ||
267*cec8643bSMichal Nowak man->flags & MAN_ELINE ||
268*cec8643bSMichal Nowak man_macro(tok)->flags & MAN_NSCOPED)
269371584c2SYuri Pankov return 1;
27095c635efSGarrett D'Amore
271260e9a87SYuri Pankov man_unscope(man, man->last->parent);
272371584c2SYuri Pankov roff_body_alloc(man, ln, ppos, man->last->tok);
273*cec8643bSMichal Nowak man->flags &= ~(MAN_BLINE | ROFF_NONOFILL);
274371584c2SYuri Pankov return 1;
275260e9a87SYuri Pankov }
27695c635efSGarrett D'Amore
277260e9a87SYuri Pankov void
man_breakscope(struct roff_man * man,int tok)278371584c2SYuri Pankov man_breakscope(struct roff_man *man, int tok)
279260e9a87SYuri Pankov {
280371584c2SYuri Pankov struct roff_node *n;
28195c635efSGarrett D'Amore
28295c635efSGarrett D'Amore /*
283260e9a87SYuri Pankov * An element next line scope is open,
284260e9a87SYuri Pankov * and the new macro is not allowed inside elements.
285260e9a87SYuri Pankov * Delete the element that is being broken.
28695c635efSGarrett D'Amore */
28795c635efSGarrett D'Amore
288c66b8046SYuri Pankov if (man->flags & MAN_ELINE && (tok < MAN_TH ||
289*cec8643bSMichal Nowak (man_macro(tok)->flags & MAN_NSCOPED) == 0)) {
290260e9a87SYuri Pankov n = man->last;
291c66b8046SYuri Pankov if (n->type == ROFFT_TEXT)
292c66b8046SYuri Pankov n = n->parent;
293c66b8046SYuri Pankov if (n->tok < MAN_TH ||
294*cec8643bSMichal Nowak (man_macro(n->tok)->flags & (MAN_NSCOPED | MAN_ESCOPED))
295*cec8643bSMichal Nowak == MAN_NSCOPED)
296260e9a87SYuri Pankov n = n->parent;
29795c635efSGarrett D'Amore
298*cec8643bSMichal Nowak mandoc_msg(MANDOCERR_BLK_LINE, n->line, n->pos,
299*cec8643bSMichal Nowak "%s breaks %s", roff_name[tok], roff_name[n->tok]);
30095c635efSGarrett D'Amore
301371584c2SYuri Pankov roff_node_delete(man, n);
302260e9a87SYuri Pankov man->flags &= ~MAN_ELINE;
30395c635efSGarrett D'Amore }
30495c635efSGarrett D'Amore
305371584c2SYuri Pankov /*
306371584c2SYuri Pankov * Weird special case:
307371584c2SYuri Pankov * Switching fill mode closes section headers.
308371584c2SYuri Pankov */
309371584c2SYuri Pankov
310371584c2SYuri Pankov if (man->flags & MAN_BLINE &&
311*cec8643bSMichal Nowak (tok == ROFF_nf || tok == ROFF_fi) &&
312371584c2SYuri Pankov (man->last->tok == MAN_SH || man->last->tok == MAN_SS)) {
313371584c2SYuri Pankov n = man->last;
314371584c2SYuri Pankov man_unscope(man, n);
315371584c2SYuri Pankov roff_body_alloc(man, n->line, n->pos, n->tok);
316*cec8643bSMichal Nowak man->flags &= ~(MAN_BLINE | ROFF_NONOFILL);
317371584c2SYuri Pankov }
318371584c2SYuri Pankov
31995c635efSGarrett D'Amore /*
320260e9a87SYuri Pankov * A block header next line scope is open,
321260e9a87SYuri Pankov * and the new macro is not allowed inside block headers.
322260e9a87SYuri Pankov * Delete the block that is being broken.
32395c635efSGarrett D'Amore */
32495c635efSGarrett D'Amore
325*cec8643bSMichal Nowak if (man->flags & MAN_BLINE && tok != ROFF_nf && tok != ROFF_fi &&
326*cec8643bSMichal Nowak (tok < MAN_TH || man_macro(tok)->flags & MAN_XSCOPE)) {
327260e9a87SYuri Pankov n = man->last;
328371584c2SYuri Pankov if (n->type == ROFFT_TEXT)
329260e9a87SYuri Pankov n = n->parent;
330c66b8046SYuri Pankov if (n->tok < MAN_TH ||
331*cec8643bSMichal Nowak (man_macro(n->tok)->flags & MAN_XSCOPE) == 0)
332260e9a87SYuri Pankov n = n->parent;
33395c635efSGarrett D'Amore
334371584c2SYuri Pankov assert(n->type == ROFFT_HEAD);
335260e9a87SYuri Pankov n = n->parent;
336371584c2SYuri Pankov assert(n->type == ROFFT_BLOCK);
337*cec8643bSMichal Nowak assert(man_macro(n->tok)->flags & MAN_BSCOPED);
33895c635efSGarrett D'Amore
339*cec8643bSMichal Nowak mandoc_msg(MANDOCERR_BLK_LINE, n->line, n->pos,
340*cec8643bSMichal Nowak "%s breaks %s", roff_name[tok], roff_name[n->tok]);
34195c635efSGarrett D'Amore
342371584c2SYuri Pankov roff_node_delete(man, n);
343*cec8643bSMichal Nowak man->flags &= ~(MAN_BLINE | ROFF_NONOFILL);
344260e9a87SYuri Pankov }
34595c635efSGarrett D'Amore }
346