]> git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
Delete an assignment that is unconditionally overwritten two lines later;
[mandoc.git] / mdoc.c
1 /* $Id: mdoc.c,v 1.254 2015/10/12 00:08:15 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 case MDOC_Bf:
186 case MDOC_Bl:
187 case MDOC_En:
188 case MDOC_Rs:
189 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
190 break;
191 default:
192 break;
193 }
194 roff_node_append(mdoc, p);
195 mdoc->next = ROFF_NEXT_CHILD;
196 return p;
197 }
198
199 void
200 mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos,
201 int tok, struct mdoc_arg *args)
202 {
203 struct roff_node *p;
204
205 p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok);
206 p->args = args;
207 if (p->args)
208 (args->refcnt)++;
209
210 switch (tok) {
211 case MDOC_An:
212 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
213 break;
214 default:
215 break;
216 }
217 roff_node_append(mdoc, p);
218 mdoc->next = ROFF_NEXT_CHILD;
219 }
220
221 void
222 mdoc_node_relink(struct roff_man *mdoc, struct roff_node *p)
223 {
224
225 roff_node_unlink(mdoc, p);
226 roff_node_append(mdoc, p);
227 }
228
229 /*
230 * Parse free-form text, that is, a line that does not begin with the
231 * control character.
232 */
233 static int
234 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs)
235 {
236 struct roff_node *n;
237 char *c, *ws, *end;
238
239 assert(mdoc->last);
240 n = mdoc->last;
241
242 /*
243 * Divert directly to list processing if we're encountering a
244 * columnar ROFFT_BLOCK with or without a prior ROFFT_BLOCK entry
245 * (a ROFFT_BODY means it's already open, in which case we should
246 * process within its context in the normal way).
247 */
248
249 if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
250 n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
251 /* `Bl' is open without any children. */
252 mdoc->flags |= MDOC_FREECOL;
253 mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
254 return 1;
255 }
256
257 if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
258 NULL != n->parent &&
259 MDOC_Bl == n->parent->tok &&
260 LIST_column == n->parent->norm->Bl.type) {
261 /* `Bl' has block-level `It' children. */
262 mdoc->flags |= MDOC_FREECOL;
263 mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
264 return 1;
265 }
266
267 /*
268 * Search for the beginning of unescaped trailing whitespace (ws)
269 * and for the first character not to be output (end).
270 */
271
272 /* FIXME: replace with strcspn(). */
273 ws = NULL;
274 for (c = end = buf + offs; *c; c++) {
275 switch (*c) {
276 case ' ':
277 if (NULL == ws)
278 ws = c;
279 continue;
280 case '\t':
281 /*
282 * Always warn about trailing tabs,
283 * even outside literal context,
284 * where they should be put on the next line.
285 */
286 if (NULL == ws)
287 ws = c;
288 /*
289 * Strip trailing tabs in literal context only;
290 * outside, they affect the next line.
291 */
292 if (MDOC_LITERAL & mdoc->flags)
293 continue;
294 break;
295 case '\\':
296 /* Skip the escaped character, too, if any. */
297 if (c[1])
298 c++;
299 /* FALLTHROUGH */
300 default:
301 ws = NULL;
302 break;
303 }
304 end = c + 1;
305 }
306 *end = '\0';
307
308 if (ws)
309 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
310 line, (int)(ws-buf), NULL);
311
312 if (buf[offs] == '\0' && ! (mdoc->flags & MDOC_LITERAL)) {
313 mandoc_msg(MANDOCERR_FI_BLANK, mdoc->parse,
314 line, (int)(c - buf), NULL);
315
316 /*
317 * Insert a `sp' in the case of a blank line. Technically,
318 * blank lines aren't allowed, but enough manuals assume this
319 * behaviour that we want to work around it.
320 */
321 roff_elem_alloc(mdoc, line, offs, MDOC_sp);
322 mdoc->next = ROFF_NEXT_SIBLING;
323 mdoc_valid_post(mdoc);
324 return 1;
325 }
326
327 roff_word_alloc(mdoc, line, offs, buf+offs);
328
329 if (mdoc->flags & MDOC_LITERAL)
330 return 1;
331
332 /*
333 * End-of-sentence check. If the last character is an unescaped
334 * EOS character, then flag the node as being the end of a
335 * sentence. The front-end will know how to interpret this.
336 */
337
338 assert(buf < end);
339
340 if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
341 mdoc->last->flags |= MDOC_EOS;
342 return 1;
343 }
344
345 /*
346 * Parse a macro line, that is, a line beginning with the control
347 * character.
348 */
349 static int
350 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs)
351 {
352 struct roff_node *n;
353 const char *cp;
354 int tok;
355 int i, sv;
356 char mac[5];
357
358 sv = offs;
359
360 /*
361 * Copy the first word into a nil-terminated buffer.
362 * Stop when a space, tab, escape, or eoln is encountered.
363 */
364
365 i = 0;
366 while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
367 mac[i++] = buf[offs++];
368
369 mac[i] = '\0';
370
371 tok = (i > 1 && i < 4) ? mdoc_hash_find(mac) : TOKEN_NONE;
372
373 if (tok == TOKEN_NONE) {
374 mandoc_msg(MANDOCERR_MACRO, mdoc->parse,
375 ln, sv, buf + sv - 1);
376 return 1;
377 }
378
379 /* Skip a leading escape sequence or tab. */
380
381 switch (buf[offs]) {
382 case '\\':
383 cp = buf + offs + 1;
384 mandoc_escape(&cp, NULL, NULL);
385 offs = cp - buf;
386 break;
387 case '\t':
388 offs++;
389 break;
390 default:
391 break;
392 }
393
394 /* Jump to the next non-whitespace word. */
395
396 while (buf[offs] && ' ' == buf[offs])
397 offs++;
398
399 /*
400 * Trailing whitespace. Note that tabs are allowed to be passed
401 * into the parser as "text", so we only warn about spaces here.
402 */
403
404 if ('\0' == buf[offs] && ' ' == buf[offs - 1])
405 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
406 ln, offs - 1, NULL);
407
408 /*
409 * If an initial macro or a list invocation, divert directly
410 * into macro processing.
411 */
412
413 if (NULL == mdoc->last || MDOC_It == tok || MDOC_El == tok) {
414 mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
415 return 1;
416 }
417
418 n = mdoc->last;
419 assert(mdoc->last);
420
421 /*
422 * If the first macro of a `Bl -column', open an `It' block
423 * context around the parsed macro.
424 */
425
426 if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
427 n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
428 mdoc->flags |= MDOC_FREECOL;
429 mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
430 return 1;
431 }
432
433 /*
434 * If we're following a block-level `It' within a `Bl -column'
435 * context (perhaps opened in the above block or in ptext()),
436 * then open an `It' block context around the parsed macro.
437 */
438
439 if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
440 NULL != n->parent &&
441 MDOC_Bl == n->parent->tok &&
442 LIST_column == n->parent->norm->Bl.type) {
443 mdoc->flags |= MDOC_FREECOL;
444 mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
445 return 1;
446 }
447
448 /* Normal processing of a macro. */
449
450 mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
451
452 /* In quick mode (for mandocdb), abort after the NAME section. */
453
454 if (mdoc->quick && MDOC_Sh == tok &&
455 SEC_NAME != mdoc->last->sec)
456 return 2;
457
458 return 1;
459 }
460
461 enum mdelim
462 mdoc_isdelim(const char *p)
463 {
464
465 if ('\0' == p[0])
466 return DELIM_NONE;
467
468 if ('\0' == p[1])
469 switch (p[0]) {
470 case '(':
471 case '[':
472 return DELIM_OPEN;
473 case '|':
474 return DELIM_MIDDLE;
475 case '.':
476 case ',':
477 case ';':
478 case ':':
479 case '?':
480 case '!':
481 case ')':
482 case ']':
483 return DELIM_CLOSE;
484 default:
485 return DELIM_NONE;
486 }
487
488 if ('\\' != p[0])
489 return DELIM_NONE;
490
491 if (0 == strcmp(p + 1, "."))
492 return DELIM_CLOSE;
493 if (0 == strcmp(p + 1, "fR|\\fP"))
494 return DELIM_MIDDLE;
495
496 return DELIM_NONE;
497 }