]> git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
simple implementation of the roff(7) .als (macro alias) request,
[mandoc.git] / mdoc.c
1 /* $Id: mdoc.c,v 1.266 2017/06/07 20:58:49 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012-2017 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_argnames[MDOC_ARG_MAX] = {
39 "split", "nosplit", "ragged",
40 "unfilled", "literal", "file",
41 "offset", "bullet", "dash",
42 "hyphen", "item", "enum",
43 "tag", "diag", "hang",
44 "ohang", "inset", "column",
45 "width", "compact", "std",
46 "filled", "words", "emphasis",
47 "symbolic", "nested", "centered"
48 };
49 const char * const *mdoc_argnames = __mdoc_argnames;
50
51 static int mdoc_ptext(struct roff_man *, int, char *, int);
52 static int mdoc_pmacro(struct roff_man *, int, char *, int);
53
54
55 /*
56 * Main parse routine. Parses a single line -- really just hands off to
57 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
58 */
59 int
60 mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, int offs)
61 {
62
63 if (mdoc->last->type != ROFFT_EQN || ln > mdoc->last->line)
64 mdoc->flags |= MDOC_NEWLINE;
65
66 /*
67 * Let the roff nS register switch SYNOPSIS mode early,
68 * such that the parser knows at all times
69 * whether this mode is on or off.
70 * Note that this mode is also switched by the Sh macro.
71 */
72 if (roff_getreg(mdoc->roff, "nS"))
73 mdoc->flags |= MDOC_SYNOPSIS;
74 else
75 mdoc->flags &= ~MDOC_SYNOPSIS;
76
77 return roff_getcontrol(mdoc->roff, buf, &offs) ?
78 mdoc_pmacro(mdoc, ln, buf, offs) :
79 mdoc_ptext(mdoc, ln, buf, offs);
80 }
81
82 void
83 mdoc_macro(MACRO_PROT_ARGS)
84 {
85 assert(tok >= MDOC_Dd && tok < MDOC_MAX);
86 (*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf);
87 }
88
89 void
90 mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, enum roff_tok tok)
91 {
92 struct roff_node *p;
93
94 p = roff_node_alloc(mdoc, line, pos, ROFFT_TAIL, tok);
95 roff_node_append(mdoc, p);
96 mdoc->next = ROFF_NEXT_CHILD;
97 }
98
99 struct roff_node *
100 mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos,
101 enum roff_tok tok, struct roff_node *body)
102 {
103 struct roff_node *p;
104
105 body->flags |= NODE_ENDED;
106 body->parent->flags |= NODE_ENDED;
107 p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok);
108 p->body = body;
109 p->norm = body->norm;
110 p->end = ENDBODY_SPACE;
111 roff_node_append(mdoc, p);
112 mdoc->next = ROFF_NEXT_SIBLING;
113 return p;
114 }
115
116 struct roff_node *
117 mdoc_block_alloc(struct roff_man *mdoc, int line, int pos,
118 enum roff_tok tok, struct mdoc_arg *args)
119 {
120 struct roff_node *p;
121
122 p = roff_node_alloc(mdoc, line, pos, ROFFT_BLOCK, tok);
123 p->args = args;
124 if (p->args)
125 (args->refcnt)++;
126
127 switch (tok) {
128 case MDOC_Bd:
129 case MDOC_Bf:
130 case MDOC_Bl:
131 case MDOC_En:
132 case MDOC_Rs:
133 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
134 break;
135 default:
136 break;
137 }
138 roff_node_append(mdoc, p);
139 mdoc->next = ROFF_NEXT_CHILD;
140 return p;
141 }
142
143 void
144 mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos,
145 enum roff_tok tok, struct mdoc_arg *args)
146 {
147 struct roff_node *p;
148
149 p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok);
150 p->args = args;
151 if (p->args)
152 (args->refcnt)++;
153
154 switch (tok) {
155 case MDOC_An:
156 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
157 break;
158 default:
159 break;
160 }
161 roff_node_append(mdoc, p);
162 mdoc->next = ROFF_NEXT_CHILD;
163 }
164
165 void
166 mdoc_node_relink(struct roff_man *mdoc, struct roff_node *p)
167 {
168
169 roff_node_unlink(mdoc, p);
170 p->prev = p->next = NULL;
171 roff_node_append(mdoc, p);
172 }
173
174 /*
175 * Parse free-form text, that is, a line that does not begin with the
176 * control character.
177 */
178 static int
179 mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs)
180 {
181 struct roff_node *n;
182 char *c, *ws, *end;
183
184 n = mdoc->last;
185
186 /*
187 * If a column list contains plain text, assume an implicit item
188 * macro. This can happen one or more times at the beginning
189 * of such a list, intermixed with non-It mdoc macros and with
190 * nodes generated on the roff level, for example by tbl.
191 */
192
193 if ((n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
194 n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) ||
195 (n->parent != NULL && n->parent->tok == MDOC_Bl &&
196 n->parent->norm->Bl.type == LIST_column)) {
197 mdoc->flags |= MDOC_FREECOL;
198 mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
199 return 1;
200 }
201
202 /*
203 * Search for the beginning of unescaped trailing whitespace (ws)
204 * and for the first character not to be output (end).
205 */
206
207 /* FIXME: replace with strcspn(). */
208 ws = NULL;
209 for (c = end = buf + offs; *c; c++) {
210 switch (*c) {
211 case ' ':
212 if (NULL == ws)
213 ws = c;
214 continue;
215 case '\t':
216 /*
217 * Always warn about trailing tabs,
218 * even outside literal context,
219 * where they should be put on the next line.
220 */
221 if (NULL == ws)
222 ws = c;
223 /*
224 * Strip trailing tabs in literal context only;
225 * outside, they affect the next line.
226 */
227 if (MDOC_LITERAL & mdoc->flags)
228 continue;
229 break;
230 case '\\':
231 /* Skip the escaped character, too, if any. */
232 if (c[1])
233 c++;
234 /* FALLTHROUGH */
235 default:
236 ws = NULL;
237 break;
238 }
239 end = c + 1;
240 }
241 *end = '\0';
242
243 if (ws)
244 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
245 line, (int)(ws-buf), NULL);
246
247 if (buf[offs] == '\0' && ! (mdoc->flags & MDOC_LITERAL)) {
248 mandoc_msg(MANDOCERR_FI_BLANK, mdoc->parse,
249 line, (int)(c - buf), NULL);
250
251 /*
252 * Insert a `sp' in the case of a blank line. Technically,
253 * blank lines aren't allowed, but enough manuals assume this
254 * behaviour that we want to work around it.
255 */
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 & MDOC_LITERAL)
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 isalpha((unsigned char)c[-2]) == 0 ||
285 isalpha((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, mdoc->parse,
296 line, (int)(c - buf), NULL);
297 }
298
299 return 1;
300 }
301
302 /*
303 * Parse a macro line, that is, a line beginning with the control
304 * character.
305 */
306 static int
307 mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs)
308 {
309 struct roff_node *n;
310 const char *cp;
311 size_t sz;
312 enum roff_tok tok;
313 int sv;
314
315 /* Determine the line macro. */
316
317 sv = offs;
318 tok = TOKEN_NONE;
319 for (sz = 0; sz < 4 && strchr(" \t\\", buf[offs]) == NULL; sz++)
320 offs++;
321 if (sz == 2 || sz == 3)
322 tok = roffhash_find(mdoc->mdocmac, buf + sv, sz);
323 if (tok == TOKEN_NONE) {
324 mandoc_msg(MANDOCERR_MACRO, mdoc->parse,
325 ln, sv, buf + sv - 1);
326 return 1;
327 }
328
329 /* Skip a leading escape sequence or tab. */
330
331 switch (buf[offs]) {
332 case '\\':
333 cp = buf + offs + 1;
334 mandoc_escape(&cp, NULL, NULL);
335 offs = cp - buf;
336 break;
337 case '\t':
338 offs++;
339 break;
340 default:
341 break;
342 }
343
344 /* Jump to the next non-whitespace word. */
345
346 while (buf[offs] == ' ')
347 offs++;
348
349 /*
350 * Trailing whitespace. Note that tabs are allowed to be passed
351 * into the parser as "text", so we only warn about spaces here.
352 */
353
354 if ('\0' == buf[offs] && ' ' == buf[offs - 1])
355 mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
356 ln, offs - 1, NULL);
357
358 /*
359 * If an initial macro or a list invocation, divert directly
360 * into macro processing.
361 */
362
363 n = mdoc->last;
364 if (n == NULL || tok == MDOC_It || tok == MDOC_El) {
365 mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
366 return 1;
367 }
368
369 /*
370 * If a column list contains a non-It macro, assume an implicit
371 * item macro. This can happen one or more times at the
372 * beginning of such a list, intermixed with text lines and
373 * with nodes generated on the roff level, for example by tbl.
374 */
375
376 if ((n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
377 n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) ||
378 (n->parent != NULL && n->parent->tok == MDOC_Bl &&
379 n->parent->norm->Bl.type == LIST_column)) {
380 mdoc->flags |= MDOC_FREECOL;
381 mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
382 return 1;
383 }
384
385 /* Normal processing of a macro. */
386
387 mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
388
389 /* In quick mode (for mandocdb), abort after the NAME section. */
390
391 if (mdoc->quick && MDOC_Sh == tok &&
392 SEC_NAME != mdoc->last->sec)
393 return 2;
394
395 return 1;
396 }
397
398 enum mdelim
399 mdoc_isdelim(const char *p)
400 {
401
402 if ('\0' == p[0])
403 return DELIM_NONE;
404
405 if ('\0' == p[1])
406 switch (p[0]) {
407 case '(':
408 case '[':
409 return DELIM_OPEN;
410 case '|':
411 return DELIM_MIDDLE;
412 case '.':
413 case ',':
414 case ';':
415 case ':':
416 case '?':
417 case '!':
418 case ')':
419 case ']':
420 return DELIM_CLOSE;
421 default:
422 return DELIM_NONE;
423 }
424
425 if ('\\' != p[0])
426 return DELIM_NONE;
427
428 if (0 == strcmp(p + 1, "."))
429 return DELIM_CLOSE;
430 if (0 == strcmp(p + 1, "fR|\\fP"))
431 return DELIM_MIDDLE;
432
433 return DELIM_NONE;
434 }
435
436 void
437 mdoc_validate(struct roff_man *mdoc)
438 {
439
440 mdoc->last = mdoc->first;
441 mdoc_node_validate(mdoc);
442 mdoc_state_reset(mdoc);
443 }