]> git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
more info on man(7) .Xr hyperlinking
[mandoc.git] / mdoc.c
1 /* $Id: mdoc.c,v 1.258 2017/01/10 13:47:00 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012-2016 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 (*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf);
125 }
126
127 void
128 mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, int tok)
129 {
130 struct roff_node *p;
131
132 p = roff_node_alloc(mdoc, line, pos, ROFFT_TAIL, tok);
133 roff_node_append(mdoc, p);
134 mdoc->next = ROFF_NEXT_CHILD;
135 }
136
137 struct roff_node *
138 mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos, int tok,
139 struct roff_node *body, enum mdoc_endbody end)
140 {
141 struct roff_node *p;
142
143 body->flags |= NODE_ENDED;
144 body->parent->flags |= NODE_ENDED;
145 p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok);
146 p->body = body;
147 p->norm = body->norm;
148 p->end = end;
149 roff_node_append(mdoc, p);
150 mdoc->next = ROFF_NEXT_SIBLING;
151 return p;
152 }
153
154 struct roff_node *
155 mdoc_block_alloc(struct roff_man *mdoc, int line, int pos,
156 int tok, struct mdoc_arg *args)
157 {
158 struct roff_node *p;
159
160 p = roff_node_alloc(mdoc, line, pos, ROFFT_BLOCK, tok);
161 p->args = args;
162 if (p->args)
163 (args->refcnt)++;
164
165 switch (tok) {
166 case MDOC_Bd:
167 case MDOC_Bf:
168 case MDOC_Bl:
169 case MDOC_En:
170 case MDOC_Rs:
171 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
172 break;
173 default:
174 break;
175 }
176 roff_node_append(mdoc, p);
177 mdoc->next = ROFF_NEXT_CHILD;
178 return p;
179 }
180
181 void
182 mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos,
183 int tok, struct mdoc_arg *args)
184 {
185 struct roff_node *p;
186
187 p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok);
188 p->args = args;
189 if (p->args)
190 (args->refcnt)++;
191
192 switch (tok) {
193 case MDOC_An:
194 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
195 break;
196 default:
197 break;
198 }
199 roff_node_append(mdoc, p);
200 mdoc->next = ROFF_NEXT_CHILD;
201 }
202
203 void
204 mdoc_node_relink(struct roff_man *mdoc, struct roff_node *p)
205 {
206
207 roff_node_unlink(mdoc, p);
208 p->prev = p->next = NULL;
209 roff_node_append(mdoc, p);
210 }
211
212 /*
213 * Parse free-form text, that is, a line that does not begin with the
214 * control character.
215 */
216 static int
217 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs)
218 {
219 struct roff_node *n;
220 char *c, *ws, *end;
221
222 n = mdoc->last;
223
224 /*
225 * If a column list contains plain text, assume an implicit item
226 * macro. This can happen one or more times at the beginning
227 * of such a list, intermixed with non-It mdoc macros and with
228 * nodes generated on the roff level, for example by tbl.
229 */
230
231 if ((n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
232 n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) ||
233 (n->parent != NULL && n->parent->tok == MDOC_Bl &&
234 n->parent->norm->Bl.type == LIST_column)) {
235 mdoc->flags |= MDOC_FREECOL;
236 mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
237 return 1;
238 }
239
240 /*
241 * Search for the beginning of unescaped trailing whitespace (ws)
242 * and for the first character not to be output (end).
243 */
244
245 /* FIXME: replace with strcspn(). */
246 ws = NULL;
247 for (c = end = buf + offs; *c; c++) {
248 switch (*c) {
249 case ' ':
250 if (NULL == ws)
251 ws = c;
252 continue;
253 case '\t':
254 /*
255 * Always warn about trailing tabs,
256 * even outside literal context,
257 * where they should be put on the next line.
258 */
259 if (NULL == ws)
260 ws = c;
261 /*
262 * Strip trailing tabs in literal context only;
263 * outside, they affect the next line.
264 */
265 if (MDOC_LITERAL & mdoc->flags)
266 continue;
267 break;
268 case '\\':
269 /* Skip the escaped character, too, if any. */
270 if (c[1])
271 c++;
272 /* FALLTHROUGH */
273 default:
274 ws = NULL;
275 break;
276 }
277 end = c + 1;
278 }
279 *end = '\0';
280
281 if (ws)
282 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
283 line, (int)(ws-buf), NULL);
284
285 if (buf[offs] == '\0' && ! (mdoc->flags & MDOC_LITERAL)) {
286 mandoc_msg(MANDOCERR_FI_BLANK, mdoc->parse,
287 line, (int)(c - buf), NULL);
288
289 /*
290 * Insert a `sp' in the case of a blank line. Technically,
291 * blank lines aren't allowed, but enough manuals assume this
292 * behaviour that we want to work around it.
293 */
294 roff_elem_alloc(mdoc, line, offs, MDOC_sp);
295 mdoc->last->flags |= NODE_VALID | NODE_ENDED;
296 mdoc->next = ROFF_NEXT_SIBLING;
297 return 1;
298 }
299
300 roff_word_alloc(mdoc, line, offs, buf+offs);
301
302 if (mdoc->flags & MDOC_LITERAL)
303 return 1;
304
305 /*
306 * End-of-sentence check. If the last character is an unescaped
307 * EOS character, then flag the node as being the end of a
308 * sentence. The front-end will know how to interpret this.
309 */
310
311 assert(buf < end);
312
313 if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
314 mdoc->last->flags |= NODE_EOS;
315 return 1;
316 }
317
318 /*
319 * Parse a macro line, that is, a line beginning with the control
320 * character.
321 */
322 static int
323 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs)
324 {
325 struct roff_node *n;
326 const char *cp;
327 int tok;
328 int i, sv;
329 char mac[5];
330
331 sv = offs;
332
333 /*
334 * Copy the first word into a nil-terminated buffer.
335 * Stop when a space, tab, escape, or eoln is encountered.
336 */
337
338 i = 0;
339 while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
340 mac[i++] = buf[offs++];
341
342 mac[i] = '\0';
343
344 tok = (i > 1 && i < 4) ? mdoc_hash_find(mac) : TOKEN_NONE;
345
346 if (tok == TOKEN_NONE) {
347 mandoc_msg(MANDOCERR_MACRO, mdoc->parse,
348 ln, sv, buf + sv - 1);
349 return 1;
350 }
351
352 /* Skip a leading escape sequence or tab. */
353
354 switch (buf[offs]) {
355 case '\\':
356 cp = buf + offs + 1;
357 mandoc_escape(&cp, NULL, NULL);
358 offs = cp - buf;
359 break;
360 case '\t':
361 offs++;
362 break;
363 default:
364 break;
365 }
366
367 /* Jump to the next non-whitespace word. */
368
369 while (buf[offs] && ' ' == buf[offs])
370 offs++;
371
372 /*
373 * Trailing whitespace. Note that tabs are allowed to be passed
374 * into the parser as "text", so we only warn about spaces here.
375 */
376
377 if ('\0' == buf[offs] && ' ' == buf[offs - 1])
378 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
379 ln, offs - 1, NULL);
380
381 /*
382 * If an initial macro or a list invocation, divert directly
383 * into macro processing.
384 */
385
386 n = mdoc->last;
387 if (n == NULL || tok == MDOC_It || tok == MDOC_El) {
388 mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
389 return 1;
390 }
391
392 /*
393 * If a column list contains a non-It macro, assume an implicit
394 * item macro. This can happen one or more times at the
395 * beginning of such a list, intermixed with text lines and
396 * with nodes generated on the roff level, for example by tbl.
397 */
398
399 if ((n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
400 n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) ||
401 (n->parent != NULL && n->parent->tok == MDOC_Bl &&
402 n->parent->norm->Bl.type == LIST_column)) {
403 mdoc->flags |= MDOC_FREECOL;
404 mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
405 return 1;
406 }
407
408 /* Normal processing of a macro. */
409
410 mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
411
412 /* In quick mode (for mandocdb), abort after the NAME section. */
413
414 if (mdoc->quick && MDOC_Sh == tok &&
415 SEC_NAME != mdoc->last->sec)
416 return 2;
417
418 return 1;
419 }
420
421 enum mdelim
422 mdoc_isdelim(const char *p)
423 {
424
425 if ('\0' == p[0])
426 return DELIM_NONE;
427
428 if ('\0' == p[1])
429 switch (p[0]) {
430 case '(':
431 case '[':
432 return DELIM_OPEN;
433 case '|':
434 return DELIM_MIDDLE;
435 case '.':
436 case ',':
437 case ';':
438 case ':':
439 case '?':
440 case '!':
441 case ')':
442 case ']':
443 return DELIM_CLOSE;
444 default:
445 return DELIM_NONE;
446 }
447
448 if ('\\' != p[0])
449 return DELIM_NONE;
450
451 if (0 == strcmp(p + 1, "."))
452 return DELIM_CLOSE;
453 if (0 == strcmp(p + 1, "fR|\\fP"))
454 return DELIM_MIDDLE;
455
456 return DELIM_NONE;
457 }
458
459 void
460 mdoc_validate(struct roff_man *mdoc)
461 {
462
463 mdoc->last = mdoc->first;
464 mdoc_node_validate(mdoc);
465 mdoc_state_reset(mdoc);
466 }