]> git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
Decide whether to use_pager as early as possible,
[mandoc.git] / mdoc.c
1 /* $Id: mdoc.c,v 1.253 2015/10/06 18:32:19 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012-2015 Ingo Schwarze <schwarze@openbsd.org>
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 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <time.h>
29
30 #include "mandoc_aux.h"
31 #include "mandoc.h"
32 #include "roff.h"
33 #include "mdoc.h"
34 #include "libmandoc.h"
35 #include "roff_int.h"
36 #include "libmdoc.h"
37
38 const char *const __mdoc_macronames[MDOC_MAX + 1] = {
39 "text",
40 "Ap", "Dd", "Dt", "Os",
41 "Sh", "Ss", "Pp", "D1",
42 "Dl", "Bd", "Ed", "Bl",
43 "El", "It", "Ad", "An",
44 "Ar", "Cd", "Cm", "Dv",
45 "Er", "Ev", "Ex", "Fa",
46 "Fd", "Fl", "Fn", "Ft",
47 "Ic", "In", "Li", "Nd",
48 "Nm", "Op", "Ot", "Pa",
49 "Rv", "St", "Va", "Vt",
50 "Xr", "%A", "%B", "%D",
51 "%I", "%J", "%N", "%O",
52 "%P", "%R", "%T", "%V",
53 "Ac", "Ao", "Aq", "At",
54 "Bc", "Bf", "Bo", "Bq",
55 "Bsx", "Bx", "Db", "Dc",
56 "Do", "Dq", "Ec", "Ef",
57 "Em", "Eo", "Fx", "Ms",
58 "No", "Ns", "Nx", "Ox",
59 "Pc", "Pf", "Po", "Pq",
60 "Qc", "Ql", "Qo", "Qq",
61 "Re", "Rs", "Sc", "So",
62 "Sq", "Sm", "Sx", "Sy",
63 "Tn", "Ux", "Xc", "Xo",
64 "Fo", "Fc", "Oo", "Oc",
65 "Bk", "Ek", "Bt", "Hf",
66 "Fr", "Ud", "Lb", "Lp",
67 "Lk", "Mt", "Brq", "Bro",
68 "Brc", "%C", "Es", "En",
69 "Dx", "%Q", "br", "sp",
70 "%U", "Ta", "ll",
71 };
72
73 const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
74 "split", "nosplit", "ragged",
75 "unfilled", "literal", "file",
76 "offset", "bullet", "dash",
77 "hyphen", "item", "enum",
78 "tag", "diag", "hang",
79 "ohang", "inset", "column",
80 "width", "compact", "std",
81 "filled", "words", "emphasis",
82 "symbolic", "nested", "centered"
83 };
84
85 const char * const *mdoc_macronames = __mdoc_macronames + 1;
86 const char * const *mdoc_argnames = __mdoc_argnames;
87
88 static int mdoc_ptext(struct roff_man *, int, char *, int);
89 static int mdoc_pmacro(struct roff_man *, int, char *, int);
90
91
92 /*
93 * Main parse routine. Parses a single line -- really just hands off to
94 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
95 */
96 int
97 mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, int offs)
98 {
99
100 if (mdoc->last->type != ROFFT_EQN || ln > mdoc->last->line)
101 mdoc->flags |= MDOC_NEWLINE;
102
103 /*
104 * Let the roff nS register switch SYNOPSIS mode early,
105 * such that the parser knows at all times
106 * whether this mode is on or off.
107 * Note that this mode is also switched by the Sh macro.
108 */
109 if (roff_getreg(mdoc->roff, "nS"))
110 mdoc->flags |= MDOC_SYNOPSIS;
111 else
112 mdoc->flags &= ~MDOC_SYNOPSIS;
113
114 return roff_getcontrol(mdoc->roff, buf, &offs) ?
115 mdoc_pmacro(mdoc, ln, buf, offs) :
116 mdoc_ptext(mdoc, ln, buf, offs);
117 }
118
119 void
120 mdoc_macro(MACRO_PROT_ARGS)
121 {
122 assert(tok > TOKEN_NONE && tok < MDOC_MAX);
123
124 if (mdoc->flags & MDOC_PBODY) {
125 if (tok == MDOC_Dt) {
126 mandoc_vmsg(MANDOCERR_DT_LATE,
127 mdoc->parse, line, ppos,
128 "Dt %s", buf + *pos);
129 return;
130 }
131 } else if ( ! (mdoc_macros[tok].flags & MDOC_PROLOGUE)) {
132 if (mdoc->meta.title == NULL) {
133 mandoc_vmsg(MANDOCERR_DT_NOTITLE,
134 mdoc->parse, line, ppos, "%s %s",
135 mdoc_macronames[tok], buf + *pos);
136 mdoc->meta.title = mandoc_strdup("UNTITLED");
137 }
138 if (NULL == mdoc->meta.vol)
139 mdoc->meta.vol = mandoc_strdup("LOCAL");
140 mdoc->flags |= MDOC_PBODY;
141 }
142 (*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf);
143 }
144
145 void
146 mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, int tok)
147 {
148 struct roff_node *p;
149
150 p = roff_node_alloc(mdoc, line, pos, ROFFT_TAIL, tok);
151 roff_node_append(mdoc, p);
152 mdoc->next = ROFF_NEXT_CHILD;
153 }
154
155 struct roff_node *
156 mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos, int tok,
157 struct roff_node *body, enum mdoc_endbody end)
158 {
159 struct roff_node *p;
160
161 body->flags |= MDOC_ENDED;
162 body->parent->flags |= MDOC_ENDED;
163 p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok);
164 p->body = body;
165 p->norm = body->norm;
166 p->end = end;
167 roff_node_append(mdoc, p);
168 mdoc->next = ROFF_NEXT_SIBLING;
169 return p;
170 }
171
172 struct roff_node *
173 mdoc_block_alloc(struct roff_man *mdoc, int line, int pos,
174 int tok, struct mdoc_arg *args)
175 {
176 struct roff_node *p;
177
178 p = roff_node_alloc(mdoc, line, pos, ROFFT_BLOCK, tok);
179 p->args = args;
180 if (p->args)
181 (args->refcnt)++;
182
183 switch (tok) {
184 case MDOC_Bd:
185 /* FALLTHROUGH */
186 case MDOC_Bf:
187 /* FALLTHROUGH */
188 case MDOC_Bl:
189 /* FALLTHROUGH */
190 case MDOC_En:
191 /* FALLTHROUGH */
192 case MDOC_Rs:
193 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
194 break;
195 default:
196 break;
197 }
198 roff_node_append(mdoc, p);
199 mdoc->next = ROFF_NEXT_CHILD;
200 return p;
201 }
202
203 void
204 mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos,
205 int tok, struct mdoc_arg *args)
206 {
207 struct roff_node *p;
208
209 p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok);
210 p->args = args;
211 if (p->args)
212 (args->refcnt)++;
213
214 switch (tok) {
215 case MDOC_An:
216 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
217 break;
218 default:
219 break;
220 }
221 roff_node_append(mdoc, p);
222 mdoc->next = ROFF_NEXT_CHILD;
223 }
224
225 void
226 mdoc_node_relink(struct roff_man *mdoc, struct roff_node *p)
227 {
228
229 roff_node_unlink(mdoc, p);
230 roff_node_append(mdoc, p);
231 }
232
233 /*
234 * Parse free-form text, that is, a line that does not begin with the
235 * control character.
236 */
237 static int
238 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs)
239 {
240 struct roff_node *n;
241 char *c, *ws, *end;
242
243 assert(mdoc->last);
244 n = mdoc->last;
245
246 /*
247 * Divert directly to list processing if we're encountering a
248 * columnar ROFFT_BLOCK with or without a prior ROFFT_BLOCK entry
249 * (a ROFFT_BODY means it's already open, in which case we should
250 * process within its context in the normal way).
251 */
252
253 if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
254 n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
255 /* `Bl' is open without any children. */
256 mdoc->flags |= MDOC_FREECOL;
257 mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
258 return 1;
259 }
260
261 if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
262 NULL != n->parent &&
263 MDOC_Bl == n->parent->tok &&
264 LIST_column == n->parent->norm->Bl.type) {
265 /* `Bl' has block-level `It' children. */
266 mdoc->flags |= MDOC_FREECOL;
267 mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
268 return 1;
269 }
270
271 /*
272 * Search for the beginning of unescaped trailing whitespace (ws)
273 * and for the first character not to be output (end).
274 */
275
276 /* FIXME: replace with strcspn(). */
277 ws = NULL;
278 for (c = end = buf + offs; *c; c++) {
279 switch (*c) {
280 case ' ':
281 if (NULL == ws)
282 ws = c;
283 continue;
284 case '\t':
285 /*
286 * Always warn about trailing tabs,
287 * even outside literal context,
288 * where they should be put on the next line.
289 */
290 if (NULL == ws)
291 ws = c;
292 /*
293 * Strip trailing tabs in literal context only;
294 * outside, they affect the next line.
295 */
296 if (MDOC_LITERAL & mdoc->flags)
297 continue;
298 break;
299 case '\\':
300 /* Skip the escaped character, too, if any. */
301 if (c[1])
302 c++;
303 /* FALLTHROUGH */
304 default:
305 ws = NULL;
306 break;
307 }
308 end = c + 1;
309 }
310 *end = '\0';
311
312 if (ws)
313 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
314 line, (int)(ws-buf), NULL);
315
316 if (buf[offs] == '\0' && ! (mdoc->flags & MDOC_LITERAL)) {
317 mandoc_msg(MANDOCERR_FI_BLANK, mdoc->parse,
318 line, (int)(c - buf), NULL);
319
320 /*
321 * Insert a `sp' in the case of a blank line. Technically,
322 * blank lines aren't allowed, but enough manuals assume this
323 * behaviour that we want to work around it.
324 */
325 roff_elem_alloc(mdoc, line, offs, MDOC_sp);
326 mdoc->next = ROFF_NEXT_SIBLING;
327 mdoc_valid_post(mdoc);
328 return 1;
329 }
330
331 roff_word_alloc(mdoc, line, offs, buf+offs);
332
333 if (mdoc->flags & MDOC_LITERAL)
334 return 1;
335
336 /*
337 * End-of-sentence check. If the last character is an unescaped
338 * EOS character, then flag the node as being the end of a
339 * sentence. The front-end will know how to interpret this.
340 */
341
342 assert(buf < end);
343
344 if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
345 mdoc->last->flags |= MDOC_EOS;
346 return 1;
347 }
348
349 /*
350 * Parse a macro line, that is, a line beginning with the control
351 * character.
352 */
353 static int
354 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs)
355 {
356 struct roff_node *n;
357 const char *cp;
358 int tok;
359 int i, sv;
360 char mac[5];
361
362 sv = offs;
363
364 /*
365 * Copy the first word into a nil-terminated buffer.
366 * Stop when a space, tab, escape, or eoln is encountered.
367 */
368
369 i = 0;
370 while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
371 mac[i++] = buf[offs++];
372
373 mac[i] = '\0';
374
375 tok = (i > 1 && i < 4) ? mdoc_hash_find(mac) : TOKEN_NONE;
376
377 if (tok == TOKEN_NONE) {
378 mandoc_msg(MANDOCERR_MACRO, mdoc->parse,
379 ln, sv, buf + sv - 1);
380 return 1;
381 }
382
383 /* Skip a leading escape sequence or tab. */
384
385 switch (buf[offs]) {
386 case '\\':
387 cp = buf + offs + 1;
388 mandoc_escape(&cp, NULL, NULL);
389 offs = cp - buf;
390 break;
391 case '\t':
392 offs++;
393 break;
394 default:
395 break;
396 }
397
398 /* Jump to the next non-whitespace word. */
399
400 while (buf[offs] && ' ' == buf[offs])
401 offs++;
402
403 /*
404 * Trailing whitespace. Note that tabs are allowed to be passed
405 * into the parser as "text", so we only warn about spaces here.
406 */
407
408 if ('\0' == buf[offs] && ' ' == buf[offs - 1])
409 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
410 ln, offs - 1, NULL);
411
412 /*
413 * If an initial macro or a list invocation, divert directly
414 * into macro processing.
415 */
416
417 if (NULL == mdoc->last || MDOC_It == tok || MDOC_El == tok) {
418 mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
419 return 1;
420 }
421
422 n = mdoc->last;
423 assert(mdoc->last);
424
425 /*
426 * If the first macro of a `Bl -column', open an `It' block
427 * context around the parsed macro.
428 */
429
430 if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
431 n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
432 mdoc->flags |= MDOC_FREECOL;
433 mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
434 return 1;
435 }
436
437 /*
438 * If we're following a block-level `It' within a `Bl -column'
439 * context (perhaps opened in the above block or in ptext()),
440 * then open an `It' block context around the parsed macro.
441 */
442
443 if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
444 NULL != n->parent &&
445 MDOC_Bl == n->parent->tok &&
446 LIST_column == n->parent->norm->Bl.type) {
447 mdoc->flags |= MDOC_FREECOL;
448 mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
449 return 1;
450 }
451
452 /* Normal processing of a macro. */
453
454 mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
455
456 /* In quick mode (for mandocdb), abort after the NAME section. */
457
458 if (mdoc->quick && MDOC_Sh == tok &&
459 SEC_NAME != mdoc->last->sec)
460 return 2;
461
462 return 1;
463 }
464
465 enum mdelim
466 mdoc_isdelim(const char *p)
467 {
468
469 if ('\0' == p[0])
470 return DELIM_NONE;
471
472 if ('\0' == p[1])
473 switch (p[0]) {
474 case '(':
475 /* FALLTHROUGH */
476 case '[':
477 return DELIM_OPEN;
478 case '|':
479 return DELIM_MIDDLE;
480 case '.':
481 /* FALLTHROUGH */
482 case ',':
483 /* FALLTHROUGH */
484 case ';':
485 /* FALLTHROUGH */
486 case ':':
487 /* FALLTHROUGH */
488 case '?':
489 /* FALLTHROUGH */
490 case '!':
491 /* FALLTHROUGH */
492 case ')':
493 /* FALLTHROUGH */
494 case ']':
495 return DELIM_CLOSE;
496 default:
497 return DELIM_NONE;
498 }
499
500 if ('\\' != p[0])
501 return DELIM_NONE;
502
503 if (0 == strcmp(p + 1, "."))
504 return DELIM_CLOSE;
505 if (0 == strcmp(p + 1, "fR|\\fP"))
506 return DELIM_MIDDLE;
507
508 return DELIM_NONE;
509 }