1 /* $Id: mdoc.c,v 1.275 2020/04/06 10:16:17 schwarze Exp $ */
2 /*
3 * Copyright (c) 2010, 2012-2018, 2020 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * Top level and utility functions of the mdoc(7) parser for mandoc(1).
19 */
20 #include "config.h"
21
22 #include <sys/types.h>
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31
32 #include "mandoc_aux.h"
33 #include "mandoc.h"
34 #include "roff.h"
35 #include "mdoc.h"
36 #include "libmandoc.h"
37 #include "roff_int.h"
38 #include "libmdoc.h"
39
40 const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
41 "split", "nosplit", "ragged",
42 "unfilled", "literal", "file",
43 "offset", "bullet", "dash",
44 "hyphen", "item", "enum",
45 "tag", "diag", "hang",
46 "ohang", "inset", "column",
47 "width", "compact", "std",
48 "filled", "words", "emphasis",
49 "symbolic", "nested", "centered"
50 };
51 const char * const *mdoc_argnames = __mdoc_argnames;
52
53 static int mdoc_ptext(struct roff_man *, int, char *, int);
54 static int mdoc_pmacro(struct roff_man *, int, char *, int);
55
56
57 /*
58 * Main parse routine. Parses a single line -- really just hands off to
59 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
60 */
61 int
mdoc_parseln(struct roff_man * mdoc,int ln,char * buf,int offs)62 mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, int offs)
63 {
64
65 if (mdoc->last->type != ROFFT_EQN || ln > mdoc->last->line)
66 mdoc->flags |= MDOC_NEWLINE;
67
68 /*
69 * Let the roff nS register switch SYNOPSIS mode early,
70 * such that the parser knows at all times
71 * whether this mode is on or off.
72 * Note that this mode is also switched by the Sh macro.
73 */
74 if (roff_getreg(mdoc->roff, "nS"))
75 mdoc->flags |= MDOC_SYNOPSIS;
76 else
77 mdoc->flags &= ~MDOC_SYNOPSIS;
78
79 return roff_getcontrol(mdoc->roff, buf, &offs) ?
80 mdoc_pmacro(mdoc, ln, buf, offs) :
81 mdoc_ptext(mdoc, ln, buf, offs);
82 }
83
84 void
mdoc_tail_alloc(struct roff_man * mdoc,int line,int pos,enum roff_tok tok)85 mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, enum roff_tok tok)
86 {
87 struct roff_node *p;
88
89 p = roff_node_alloc(mdoc, line, pos, ROFFT_TAIL, tok);
90 roff_node_append(mdoc, p);
91 mdoc->next = ROFF_NEXT_CHILD;
92 }
93
94 struct roff_node *
mdoc_endbody_alloc(struct roff_man * mdoc,int line,int pos,enum roff_tok tok,struct roff_node * body)95 mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos,
96 enum roff_tok tok, struct roff_node *body)
97 {
98 struct roff_node *p;
99
100 body->flags |= NODE_ENDED;
101 body->parent->flags |= NODE_ENDED;
102 p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok);
103 p->body = body;
104 p->norm = body->norm;
105 p->end = ENDBODY_SPACE;
106 roff_node_append(mdoc, p);
107 mdoc->next = ROFF_NEXT_SIBLING;
108 return p;
109 }
110
111 struct roff_node *
mdoc_block_alloc(struct roff_man * mdoc,int line,int pos,enum roff_tok tok,struct mdoc_arg * args)112 mdoc_block_alloc(struct roff_man *mdoc, int line, int pos,
113 enum roff_tok tok, struct mdoc_arg *args)
114 {
115 struct roff_node *p;
116
117 p = roff_node_alloc(mdoc, line, pos, ROFFT_BLOCK, tok);
118 p->args = args;
119 if (p->args)
120 (args->refcnt)++;
121
122 switch (tok) {
123 case MDOC_Bd:
124 case MDOC_Bf:
125 case MDOC_Bl:
126 case MDOC_En:
127 case MDOC_Rs:
128 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
129 break;
130 default:
131 break;
132 }
133 roff_node_append(mdoc, p);
134 mdoc->next = ROFF_NEXT_CHILD;
135 return p;
136 }
137
138 void
mdoc_elem_alloc(struct roff_man * mdoc,int line,int pos,enum roff_tok tok,struct mdoc_arg * args)139 mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos,
140 enum roff_tok tok, struct mdoc_arg *args)
141 {
142 struct roff_node *p;
143
144 p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok);
145 p->args = args;
146 if (p->args)
147 (args->refcnt)++;
148
149 switch (tok) {
150 case MDOC_An:
151 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
152 break;
153 default:
154 break;
155 }
156 roff_node_append(mdoc, p);
157 mdoc->next = ROFF_NEXT_CHILD;
158 }
159
160 /*
161 * Parse free-form text, that is, a line that does not begin with the
162 * control character.
163 */
164 static int
mdoc_ptext(struct roff_man * mdoc,int line,char * buf,int offs)165 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs)
166 {
167 struct roff_node *n;
168 char *c, *ws, *end, *cp, *sp;
169
170 n = mdoc->last;
171
172 /*
173 * If a column list contains plain text, assume an implicit item
174 * macro. This can happen one or more times at the beginning
175 * of such a list, intermixed with non-It mdoc macros and with
176 * nodes generated on the roff level, for example by tbl.
177 */
178
179 if ((n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
180 n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) ||
181 (n->parent != NULL && n->parent->tok == MDOC_Bl &&
182 n->parent->norm->Bl.type == LIST_column)) {
183 mdoc->flags |= MDOC_FREECOL;
184 (*mdoc_macro(MDOC_It)->fp)(mdoc, MDOC_It,
185 line, offs, &offs, buf);
186 return 1;
187 }
188
189 /*
190 * Search for the beginning of unescaped trailing whitespace (ws)
191 * and for the first character not to be output (end).
192 */
193
194 /* FIXME: replace with strcspn(). */
195 ws = NULL;
196 for (c = end = buf + offs; *c; c++) {
197 switch (*c) {
198 case ' ':
199 if (NULL == ws)
200 ws = c;
201 continue;
202 case '\t':
203 /*
204 * Always warn about trailing tabs,
205 * even outside literal context,
206 * where they should be put on the next line.
207 */
208 if (NULL == ws)
209 ws = c;
210 /*
211 * Strip trailing tabs in literal context only;
212 * outside, they affect the next line.
213 */
214 if (mdoc->flags & ROFF_NOFILL)
215 continue;
216 break;
217 case '\\':
218 /* Skip the escaped character, too, if any. */
219 if (c[1])
220 c++;
221 /* FALLTHROUGH */
222 default:
223 ws = NULL;
224 break;
225 }
226 end = c + 1;
227 }
228 *end = '\0';
229
230 if (ws)
231 mandoc_msg(MANDOCERR_SPACE_EOL, line, (int)(ws - buf), NULL);
232
233 /*
234 * Blank lines are allowed in no-fill mode
235 * and cancel preceding \c,
236 * but add a single vertical space elsewhere.
237 */
238
239 if (buf[offs] == '\0' && (mdoc->flags & ROFF_NOFILL) == 0) {
240 switch (mdoc->last->type) {
241 case ROFFT_TEXT:
242 sp = mdoc->last->string;
243 cp = end = strchr(sp, '\0') - 2;
244 if (cp < sp || cp[0] != '\\' || cp[1] != 'c')
245 break;
246 while (cp > sp && cp[-1] == '\\')
247 cp--;
248 if ((end - cp) % 2)
249 break;
250 *end = '\0';
251 return 1;
252 default:
253 break;
254 }
255 mandoc_msg(MANDOCERR_FI_BLANK, line, (int)(c - buf), NULL);
256 roff_elem_alloc(mdoc, line, offs, ROFF_sp);
257 mdoc->last->flags |= NODE_VALID | NODE_ENDED;
258 mdoc->next = ROFF_NEXT_SIBLING;
259 return 1;
260 }
261
262 roff_word_alloc(mdoc, line, offs, buf+offs);
263
264 if (mdoc->flags & ROFF_NOFILL)
265 return 1;
266
267 /*
268 * End-of-sentence check. If the last character is an unescaped
269 * EOS character, then flag the node as being the end of a
270 * sentence. The front-end will know how to interpret this.
271 */
272
273 assert(buf < end);
274
275 if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
276 mdoc->last->flags |= NODE_EOS;
277
278 for (c = buf + offs; c != NULL; c = strchr(c + 1, '.')) {
279 if (c - buf < offs + 2)
280 continue;
281 if (end - c < 3)
282 break;
283 if (c[1] != ' ' ||
284 isalnum((unsigned char)c[-2]) == 0 ||
285 isalnum((unsigned char)c[-1]) == 0 ||
286 (c[-2] == 'n' && c[-1] == 'c') ||
287 (c[-2] == 'v' && c[-1] == 's'))
288 continue;
289 c += 2;
290 if (*c == ' ')
291 c++;
292 if (*c == ' ')
293 c++;
294 if (isupper((unsigned char)(*c)))
295 mandoc_msg(MANDOCERR_EOS, line, (int)(c - buf), NULL);
296 }
297
298 return 1;
299 }
300
301 /*
302 * Parse a macro line, that is, a line beginning with the control
303 * character.
304 */
305 static int
mdoc_pmacro(struct roff_man * mdoc,int ln,char * buf,int offs)306 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs)
307 {
308 struct roff_node *n;
309 const char *cp;
310 size_t sz;
311 enum roff_tok tok;
312 int sv;
313
314 /* Determine the line macro. */
315
316 sv = offs;
317 tok = TOKEN_NONE;
318 for (sz = 0; sz < 4 && strchr(" \t\\", buf[offs]) == NULL; sz++)
319 offs++;
320 if (sz == 2 || sz == 3)
321 tok = roffhash_find(mdoc->mdocmac, buf + sv, sz);
322 if (tok == TOKEN_NONE) {
323 mandoc_msg(MANDOCERR_MACRO, ln, sv, "%s", buf + sv - 1);
324 return 1;
325 }
326
327 /* Skip a leading escape sequence or tab. */
328
329 switch (buf[offs]) {
330 case '\\':
331 cp = buf + offs + 1;
332 mandoc_escape(&cp, NULL, NULL);
333 offs = cp - buf;
334 break;
335 case '\t':
336 offs++;
337 break;
338 default:
339 break;
340 }
341
342 /* Jump to the next non-whitespace word. */
343
344 while (buf[offs] == ' ')
345 offs++;
346
347 /*
348 * Trailing whitespace. Note that tabs are allowed to be passed
349 * into the parser as "text", so we only warn about spaces here.
350 */
351
352 if ('\0' == buf[offs] && ' ' == buf[offs - 1])
353 mandoc_msg(MANDOCERR_SPACE_EOL, ln, offs - 1, NULL);
354
355 /*
356 * If an initial or transparent macro or a list invocation,
357 * divert directly into macro processing.
358 */
359
360 n = mdoc->last;
361 if (n == NULL || tok == MDOC_It || tok == MDOC_El ||
362 roff_tok_transparent(tok)) {
363 (*mdoc_macro(tok)->fp)(mdoc, tok, ln, sv, &offs, buf);
364 return 1;
365 }
366
367 /*
368 * If a column list contains a non-It macro, assume an implicit
369 * item macro. This can happen one or more times at the
370 * beginning of such a list, intermixed with text lines and
371 * with nodes generated on the roff level, for example by tbl.
372 */
373
374 if ((n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
375 n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) ||
376 (n->parent != NULL && n->parent->tok == MDOC_Bl &&
377 n->parent->norm->Bl.type == LIST_column)) {
378 mdoc->flags |= MDOC_FREECOL;
379 (*mdoc_macro(MDOC_It)->fp)(mdoc, MDOC_It, ln, sv, &sv, buf);
380 return 1;
381 }
382
383 /* Normal processing of a macro. */
384
385 (*mdoc_macro(tok)->fp)(mdoc, tok, ln, sv, &offs, buf);
386
387 /* In quick mode (for mandocdb), abort after the NAME section. */
388
389 if (mdoc->quick && MDOC_Sh == tok &&
390 SEC_NAME != mdoc->last->sec)
391 return 2;
392
393 return 1;
394 }
395
396 enum mdelim
mdoc_isdelim(const char * p)397 mdoc_isdelim(const char *p)
398 {
399
400 if ('\0' == p[0])
401 return DELIM_NONE;
402
403 if ('\0' == p[1])
404 switch (p[0]) {
405 case '(':
406 case '[':
407 return DELIM_OPEN;
408 case '|':
409 return DELIM_MIDDLE;
410 case '.':
411 case ',':
412 case ';':
413 case ':':
414 case '?':
415 case '!':
416 case ')':
417 case ']':
418 return DELIM_CLOSE;
419 default:
420 return DELIM_NONE;
421 }
422
423 if ('\\' != p[0])
424 return DELIM_NONE;
425
426 if (0 == strcmp(p + 1, "."))
427 return DELIM_CLOSE;
428 if (0 == strcmp(p + 1, "fR|\\fP"))
429 return DELIM_MIDDLE;
430
431 return DELIM_NONE;
432 }
433