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