]> git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
Merge from OpenBSD right after 1.10.6; now back to full sync.
[mandoc.git] / mdoc.c
1 /* $Id: mdoc.c,v 1.165 2010/09/27 23:03:44 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <sys/types.h>
23
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include "mandoc.h"
32 #include "libmdoc.h"
33 #include "libmandoc.h"
34
35 const char *const __mdoc_macronames[MDOC_MAX] = {
36 "Ap", "Dd", "Dt", "Os",
37 "Sh", "Ss", "Pp", "D1",
38 "Dl", "Bd", "Ed", "Bl",
39 "El", "It", "Ad", "An",
40 "Ar", "Cd", "Cm", "Dv",
41 "Er", "Ev", "Ex", "Fa",
42 "Fd", "Fl", "Fn", "Ft",
43 "Ic", "In", "Li", "Nd",
44 "Nm", "Op", "Ot", "Pa",
45 "Rv", "St", "Va", "Vt",
46 /* LINTED */
47 "Xr", "%A", "%B", "%D",
48 /* LINTED */
49 "%I", "%J", "%N", "%O",
50 /* LINTED */
51 "%P", "%R", "%T", "%V",
52 "Ac", "Ao", "Aq", "At",
53 "Bc", "Bf", "Bo", "Bq",
54 "Bsx", "Bx", "Db", "Dc",
55 "Do", "Dq", "Ec", "Ef",
56 "Em", "Eo", "Fx", "Ms",
57 "No", "Ns", "Nx", "Ox",
58 "Pc", "Pf", "Po", "Pq",
59 "Qc", "Ql", "Qo", "Qq",
60 "Re", "Rs", "Sc", "So",
61 "Sq", "Sm", "Sx", "Sy",
62 "Tn", "Ux", "Xc", "Xo",
63 "Fo", "Fc", "Oo", "Oc",
64 "Bk", "Ek", "Bt", "Hf",
65 "Fr", "Ud", "Lb", "Lp",
66 "Lk", "Mt", "Brq", "Bro",
67 /* LINTED */
68 "Brc", "%C", "Es", "En",
69 /* LINTED */
70 "Dx", "%Q", "br", "sp",
71 /* LINTED */
72 "%U", "Ta"
73 };
74
75 const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
76 "split", "nosplit", "ragged",
77 "unfilled", "literal", "file",
78 "offset", "bullet", "dash",
79 "hyphen", "item", "enum",
80 "tag", "diag", "hang",
81 "ohang", "inset", "column",
82 "width", "compact", "std",
83 "filled", "words", "emphasis",
84 "symbolic", "nested", "centered"
85 };
86
87 const char * const *mdoc_macronames = __mdoc_macronames;
88 const char * const *mdoc_argnames = __mdoc_argnames;
89
90 static void mdoc_node_free(struct mdoc_node *);
91 static void mdoc_node_unlink(struct mdoc *,
92 struct mdoc_node *);
93 static void mdoc_free1(struct mdoc *);
94 static void mdoc_alloc1(struct mdoc *);
95 static struct mdoc_node *node_alloc(struct mdoc *, int, int,
96 enum mdoct, enum mdoc_type);
97 static int node_append(struct mdoc *,
98 struct mdoc_node *);
99 static int mdoc_ptext(struct mdoc *, int, char *, int);
100 static int mdoc_pmacro(struct mdoc *, int, char *, int);
101
102
103 const struct mdoc_node *
104 mdoc_node(const struct mdoc *m)
105 {
106
107 return(MDOC_HALT & m->flags ? NULL : m->first);
108 }
109
110
111 const struct mdoc_meta *
112 mdoc_meta(const struct mdoc *m)
113 {
114
115 return(MDOC_HALT & m->flags ? NULL : &m->meta);
116 }
117
118
119 /*
120 * Frees volatile resources (parse tree, meta-data, fields).
121 */
122 static void
123 mdoc_free1(struct mdoc *mdoc)
124 {
125
126 if (mdoc->first)
127 mdoc_node_delete(mdoc, mdoc->first);
128 if (mdoc->meta.title)
129 free(mdoc->meta.title);
130 if (mdoc->meta.os)
131 free(mdoc->meta.os);
132 if (mdoc->meta.name)
133 free(mdoc->meta.name);
134 if (mdoc->meta.arch)
135 free(mdoc->meta.arch);
136 if (mdoc->meta.vol)
137 free(mdoc->meta.vol);
138 if (mdoc->meta.msec)
139 free(mdoc->meta.msec);
140 }
141
142
143 /*
144 * Allocate all volatile resources (parse tree, meta-data, fields).
145 */
146 static void
147 mdoc_alloc1(struct mdoc *mdoc)
148 {
149
150 memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
151 mdoc->flags = 0;
152 mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
153 mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
154 mdoc->first = mdoc->last;
155 mdoc->last->type = MDOC_ROOT;
156 mdoc->next = MDOC_NEXT_CHILD;
157 }
158
159
160 /*
161 * Free up volatile resources (see mdoc_free1()) then re-initialises the
162 * data with mdoc_alloc1(). After invocation, parse data has been reset
163 * and the parser is ready for re-invocation on a new tree; however,
164 * cross-parse non-volatile data is kept intact.
165 */
166 void
167 mdoc_reset(struct mdoc *mdoc)
168 {
169
170 mdoc_free1(mdoc);
171 mdoc_alloc1(mdoc);
172 }
173
174
175 /*
176 * Completely free up all volatile and non-volatile parse resources.
177 * After invocation, the pointer is no longer usable.
178 */
179 void
180 mdoc_free(struct mdoc *mdoc)
181 {
182
183 mdoc_free1(mdoc);
184 free(mdoc);
185 }
186
187
188 /*
189 * Allocate volatile and non-volatile parse resources.
190 */
191 struct mdoc *
192 mdoc_alloc(struct regset *regs, void *data, mandocmsg msg)
193 {
194 struct mdoc *p;
195
196 p = mandoc_calloc(1, sizeof(struct mdoc));
197
198 p->msg = msg;
199 p->data = data;
200 p->regs = regs;
201
202 mdoc_hash_init();
203 mdoc_alloc1(p);
204 return(p);
205 }
206
207
208 /*
209 * Climb back up the parse tree, validating open scopes. Mostly calls
210 * through to macro_end() in macro.c.
211 */
212 int
213 mdoc_endparse(struct mdoc *m)
214 {
215
216 if (MDOC_HALT & m->flags)
217 return(0);
218 else if (mdoc_macroend(m))
219 return(1);
220 m->flags |= MDOC_HALT;
221 return(0);
222 }
223
224
225 /*
226 * Main parse routine. Parses a single line -- really just hands off to
227 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
228 */
229 int
230 mdoc_parseln(struct mdoc *m, int ln, char *buf, int offs)
231 {
232
233 if (MDOC_HALT & m->flags)
234 return(0);
235
236 m->flags |= MDOC_NEWLINE;
237
238 /*
239 * Let the roff nS register switch SYNOPSIS mode early,
240 * such that the parser knows at all times
241 * whether this mode is on or off.
242 * Note that this mode is also switched by the Sh macro.
243 */
244 if (m->regs->regs[(int)REG_nS].set) {
245 if (m->regs->regs[(int)REG_nS].v.u)
246 m->flags |= MDOC_SYNOPSIS;
247 else
248 m->flags &= ~MDOC_SYNOPSIS;
249 }
250
251 return(('.' == buf[offs] || '\'' == buf[offs]) ?
252 mdoc_pmacro(m, ln, buf, offs) :
253 mdoc_ptext(m, ln, buf, offs));
254 }
255
256
257 int
258 mdoc_vmsg(struct mdoc *mdoc, enum mandocerr t,
259 int ln, int pos, const char *fmt, ...)
260 {
261 char buf[256];
262 va_list ap;
263
264 va_start(ap, fmt);
265 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
266 va_end(ap);
267
268 return((*mdoc->msg)(t, mdoc->data, ln, pos, buf));
269 }
270
271
272 int
273 mdoc_macro(MACRO_PROT_ARGS)
274 {
275 assert(tok < MDOC_MAX);
276
277 /* If we're in the body, deny prologue calls. */
278
279 if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
280 MDOC_PBODY & m->flags)
281 return(mdoc_pmsg(m, line, ppos, MANDOCERR_BADBODY));
282
283 /* If we're in the prologue, deny "body" macros. */
284
285 if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
286 ! (MDOC_PBODY & m->flags)) {
287 if ( ! mdoc_pmsg(m, line, ppos, MANDOCERR_BADPROLOG))
288 return(0);
289 if (NULL == m->meta.title)
290 m->meta.title = mandoc_strdup("UNKNOWN");
291 if (NULL == m->meta.vol)
292 m->meta.vol = mandoc_strdup("LOCAL");
293 if (NULL == m->meta.os)
294 m->meta.os = mandoc_strdup("LOCAL");
295 if (0 == m->meta.date)
296 m->meta.date = time(NULL);
297 m->flags |= MDOC_PBODY;
298 }
299
300 return((*mdoc_macros[tok].fp)(m, tok, line, ppos, pos, buf));
301 }
302
303
304 static int
305 node_append(struct mdoc *mdoc, struct mdoc_node *p)
306 {
307
308 assert(mdoc->last);
309 assert(mdoc->first);
310 assert(MDOC_ROOT != p->type);
311
312 switch (mdoc->next) {
313 case (MDOC_NEXT_SIBLING):
314 mdoc->last->next = p;
315 p->prev = mdoc->last;
316 p->parent = mdoc->last->parent;
317 break;
318 case (MDOC_NEXT_CHILD):
319 mdoc->last->child = p;
320 p->parent = mdoc->last;
321 break;
322 default:
323 abort();
324 /* NOTREACHED */
325 }
326
327 p->parent->nchild++;
328
329 if ( ! mdoc_valid_pre(mdoc, p))
330 return(0);
331 if ( ! mdoc_action_pre(mdoc, p))
332 return(0);
333
334 switch (p->type) {
335 case (MDOC_HEAD):
336 assert(MDOC_BLOCK == p->parent->type);
337 p->parent->head = p;
338 break;
339 case (MDOC_TAIL):
340 assert(MDOC_BLOCK == p->parent->type);
341 p->parent->tail = p;
342 break;
343 case (MDOC_BODY):
344 if (p->end)
345 break;
346 assert(MDOC_BLOCK == p->parent->type);
347 p->parent->body = p;
348 break;
349 default:
350 break;
351 }
352
353 mdoc->last = p;
354
355 switch (p->type) {
356 case (MDOC_TEXT):
357 if ( ! mdoc_valid_post(mdoc))
358 return(0);
359 if ( ! mdoc_action_post(mdoc))
360 return(0);
361 break;
362 default:
363 break;
364 }
365
366 return(1);
367 }
368
369
370 static struct mdoc_node *
371 node_alloc(struct mdoc *m, int line, int pos,
372 enum mdoct tok, enum mdoc_type type)
373 {
374 struct mdoc_node *p;
375
376 p = mandoc_calloc(1, sizeof(struct mdoc_node));
377 p->sec = m->lastsec;
378 p->line = line;
379 p->pos = pos;
380 p->tok = tok;
381 p->type = type;
382
383 /* Flag analysis. */
384
385 if (MDOC_SYNOPSIS & m->flags)
386 p->flags |= MDOC_SYNPRETTY;
387 else
388 p->flags &= ~MDOC_SYNPRETTY;
389 if (MDOC_NEWLINE & m->flags)
390 p->flags |= MDOC_LINE;
391 m->flags &= ~MDOC_NEWLINE;
392
393 return(p);
394 }
395
396
397 int
398 mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
399 {
400 struct mdoc_node *p;
401
402 p = node_alloc(m, line, pos, tok, MDOC_TAIL);
403 if ( ! node_append(m, p))
404 return(0);
405 m->next = MDOC_NEXT_CHILD;
406 return(1);
407 }
408
409
410 int
411 mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
412 {
413 struct mdoc_node *p;
414
415 assert(m->first);
416 assert(m->last);
417
418 p = node_alloc(m, line, pos, tok, MDOC_HEAD);
419 if ( ! node_append(m, p))
420 return(0);
421 m->next = MDOC_NEXT_CHILD;
422 return(1);
423 }
424
425
426 int
427 mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
428 {
429 struct mdoc_node *p;
430
431 p = node_alloc(m, line, pos, tok, MDOC_BODY);
432 if ( ! node_append(m, p))
433 return(0);
434 m->next = MDOC_NEXT_CHILD;
435 return(1);
436 }
437
438
439 int
440 mdoc_endbody_alloc(struct mdoc *m, int line, int pos, enum mdoct tok,
441 struct mdoc_node *body, enum mdoc_endbody end)
442 {
443 struct mdoc_node *p;
444
445 p = node_alloc(m, line, pos, tok, MDOC_BODY);
446 p->pending = body;
447 p->end = end;
448 if ( ! node_append(m, p))
449 return(0);
450 m->next = MDOC_NEXT_SIBLING;
451 return(1);
452 }
453
454
455 int
456 mdoc_block_alloc(struct mdoc *m, int line, int pos,
457 enum mdoct tok, struct mdoc_arg *args)
458 {
459 struct mdoc_node *p;
460
461 p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
462 p->args = args;
463 if (p->args)
464 (args->refcnt)++;
465 if ( ! node_append(m, p))
466 return(0);
467 m->next = MDOC_NEXT_CHILD;
468 return(1);
469 }
470
471
472 int
473 mdoc_elem_alloc(struct mdoc *m, int line, int pos,
474 enum mdoct tok, struct mdoc_arg *args)
475 {
476 struct mdoc_node *p;
477
478 p = node_alloc(m, line, pos, tok, MDOC_ELEM);
479 p->args = args;
480 if (p->args)
481 (args->refcnt)++;
482 if ( ! node_append(m, p))
483 return(0);
484 m->next = MDOC_NEXT_CHILD;
485 return(1);
486 }
487
488
489 int
490 mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
491 {
492 struct mdoc_node *n;
493 size_t sv, len;
494
495 len = strlen(p);
496
497 n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
498 n->string = mandoc_malloc(len + 1);
499 sv = strlcpy(n->string, p, len + 1);
500
501 /* Prohibit truncation. */
502 assert(sv < len + 1);
503
504 if ( ! node_append(m, n))
505 return(0);
506
507 m->next = MDOC_NEXT_SIBLING;
508 return(1);
509 }
510
511
512 static void
513 mdoc_node_free(struct mdoc_node *p)
514 {
515
516 /*
517 * XXX: if these end up being problematic in terms of memory
518 * management and dereferencing freed blocks, then make them
519 * into reference-counted double-pointers.
520 */
521
522 if (MDOC_Bd == p->tok && MDOC_BLOCK == p->type)
523 if (p->data.Bd)
524 free(p->data.Bd);
525 if (MDOC_Bl == p->tok && MDOC_BLOCK == p->type)
526 if (p->data.Bl)
527 free(p->data.Bl);
528 if (MDOC_Bf == p->tok && MDOC_HEAD == p->type)
529 if (p->data.Bf)
530 free(p->data.Bf);
531
532 if (p->string)
533 free(p->string);
534 if (p->args)
535 mdoc_argv_free(p->args);
536 free(p);
537 }
538
539
540 static void
541 mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
542 {
543
544 /* Adjust siblings. */
545
546 if (n->prev)
547 n->prev->next = n->next;
548 if (n->next)
549 n->next->prev = n->prev;
550
551 /* Adjust parent. */
552
553 if (n->parent) {
554 n->parent->nchild--;
555 if (n->parent->child == n)
556 n->parent->child = n->prev ? n->prev : n->next;
557 }
558
559 /* Adjust parse point, if applicable. */
560
561 if (m && m->last == n) {
562 if (n->prev) {
563 m->last = n->prev;
564 m->next = MDOC_NEXT_SIBLING;
565 } else {
566 m->last = n->parent;
567 m->next = MDOC_NEXT_CHILD;
568 }
569 }
570
571 if (m && m->first == n)
572 m->first = NULL;
573 }
574
575
576 void
577 mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
578 {
579
580 while (p->child) {
581 assert(p->nchild);
582 mdoc_node_delete(m, p->child);
583 }
584 assert(0 == p->nchild);
585
586 mdoc_node_unlink(m, p);
587 mdoc_node_free(p);
588 }
589
590
591 /*
592 * Parse free-form text, that is, a line that does not begin with the
593 * control character.
594 */
595 static int
596 mdoc_ptext(struct mdoc *m, int line, char *buf, int offs)
597 {
598 char *c, *ws, *end;
599 struct mdoc_node *n;
600
601 /* Ignore bogus comments. */
602
603 if ('\\' == buf[offs] &&
604 '.' == buf[offs + 1] &&
605 '"' == buf[offs + 2])
606 return(mdoc_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
607
608 /* No text before an initial macro. */
609
610 if (SEC_NONE == m->lastnamed)
611 return(mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT));
612
613 assert(m->last);
614 n = m->last;
615
616 /*
617 * Divert directly to list processing if we're encountering a
618 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
619 * (a MDOC_BODY means it's already open, in which case we should
620 * process within its context in the normal way).
621 */
622
623 if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
624 LIST_column == n->data.Bl->type) {
625 /* `Bl' is open without any children. */
626 m->flags |= MDOC_FREECOL;
627 return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
628 }
629
630 if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
631 NULL != n->parent &&
632 MDOC_Bl == n->parent->tok &&
633 LIST_column == n->parent->data.Bl->type) {
634 /* `Bl' has block-level `It' children. */
635 m->flags |= MDOC_FREECOL;
636 return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
637 }
638
639 /*
640 * Search for the beginning of unescaped trailing whitespace (ws)
641 * and for the first character not to be output (end).
642 */
643
644 /* FIXME: replace with strcspn(). */
645 ws = NULL;
646 for (c = end = buf + offs; *c; c++) {
647 switch (*c) {
648 case '-':
649 if (mandoc_hyph(buf + offs, c))
650 *c = ASCII_HYPH;
651 ws = NULL;
652 break;
653 case ' ':
654 if (NULL == ws)
655 ws = c;
656 continue;
657 case '\t':
658 /*
659 * Always warn about trailing tabs,
660 * even outside literal context,
661 * where they should be put on the next line.
662 */
663 if (NULL == ws)
664 ws = c;
665 /*
666 * Strip trailing tabs in literal context only;
667 * outside, they affect the next line.
668 */
669 if (MDOC_LITERAL & m->flags)
670 continue;
671 break;
672 case '\\':
673 /* Skip the escaped character, too, if any. */
674 if (c[1])
675 c++;
676 /* FALLTHROUGH */
677 default:
678 ws = NULL;
679 break;
680 }
681 end = c + 1;
682 }
683 *end = '\0';
684
685 if (ws)
686 if ( ! mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE))
687 return(0);
688
689 if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) {
690 if ( ! mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN))
691 return(0);
692
693 /*
694 * Insert a `sp' in the case of a blank line. Technically,
695 * blank lines aren't allowed, but enough manuals assume this
696 * behaviour that we want to work around it.
697 */
698 if ( ! mdoc_elem_alloc(m, line, offs, MDOC_sp, NULL))
699 return(0);
700
701 m->next = MDOC_NEXT_SIBLING;
702 return(1);
703 }
704
705 if ( ! mdoc_word_alloc(m, line, offs, buf+offs))
706 return(0);
707
708 if (MDOC_LITERAL & m->flags)
709 return(1);
710
711 /*
712 * End-of-sentence check. If the last character is an unescaped
713 * EOS character, then flag the node as being the end of a
714 * sentence. The front-end will know how to interpret this.
715 */
716
717 assert(buf < end);
718
719 if (mandoc_eos(buf+offs, (size_t)(end-buf-offs), 0))
720 m->last->flags |= MDOC_EOS;
721
722 return(1);
723 }
724
725
726 /*
727 * Parse a macro line, that is, a line beginning with the control
728 * character.
729 */
730 static int
731 mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs)
732 {
733 enum mdoct tok;
734 int i, j, sv;
735 char mac[5];
736 struct mdoc_node *n;
737
738 /* Empty lines are ignored. */
739
740 offs++;
741
742 if ('\0' == buf[offs])
743 return(1);
744
745 i = offs;
746
747 /* Accept tabs/whitespace after the initial control char. */
748
749 if (' ' == buf[i] || '\t' == buf[i]) {
750 i++;
751 while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
752 i++;
753 if ('\0' == buf[i])
754 return(1);
755 }
756
757 sv = i;
758
759 /*
760 * Copy the first word into a nil-terminated buffer.
761 * Stop copying when a tab, space, or eoln is encountered.
762 */
763
764 j = 0;
765 while (j < 4 && '\0' != buf[i] && ' ' != buf[i] && '\t' != buf[i])
766 mac[j++] = buf[i++];
767 mac[j] = '\0';
768
769 tok = (j > 1 || j < 4) ? mdoc_hash_find(mac) : MDOC_MAX;
770 if (MDOC_MAX == tok) {
771 mdoc_vmsg(m, MANDOCERR_MACRO, ln, sv,
772 "unknown macro: %s%s",
773 buf, strlen(buf) > 3 ? "..." : "");
774 return(1);
775 }
776
777 /* Disregard the first trailing tab, if applicable. */
778
779 if ('\t' == buf[i])
780 i++;
781
782 /* Jump to the next non-whitespace word. */
783
784 while (buf[i] && ' ' == buf[i])
785 i++;
786
787 /*
788 * Trailing whitespace. Note that tabs are allowed to be passed
789 * into the parser as "text", so we only warn about spaces here.
790 */
791
792 if ('\0' == buf[i] && ' ' == buf[i - 1])
793 if ( ! mdoc_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
794 goto err;
795
796 /*
797 * If an initial macro or a list invocation, divert directly
798 * into macro processing.
799 */
800
801 if (NULL == m->last || MDOC_It == tok || MDOC_El == tok) {
802 if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
803 goto err;
804 return(1);
805 }
806
807 n = m->last;
808 assert(m->last);
809
810 /*
811 * If the first macro of a `Bl -column', open an `It' block
812 * context around the parsed macro.
813 */
814
815 if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
816 LIST_column == n->data.Bl->type) {
817 m->flags |= MDOC_FREECOL;
818 if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
819 goto err;
820 return(1);
821 }
822
823 /*
824 * If we're following a block-level `It' within a `Bl -column'
825 * context (perhaps opened in the above block or in ptext()),
826 * then open an `It' block context around the parsed macro.
827 */
828
829 if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
830 NULL != n->parent &&
831 MDOC_Bl == n->parent->tok &&
832 LIST_column == n->parent->data.Bl->type) {
833 m->flags |= MDOC_FREECOL;
834 if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
835 goto err;
836 return(1);
837 }
838
839 /* Normal processing of a macro. */
840
841 if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
842 goto err;
843
844 return(1);
845
846 err: /* Error out. */
847
848 m->flags |= MDOC_HALT;
849 return(0);
850 }
851
852