]> git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
Make struct_bl and struct_bd into pointers. This removes the need to do
[mandoc.git] / mdoc.c
1 /* $Id: mdoc.c,v 1.154 2010/07/01 22:56:17 kristaps 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
243 /*
244 * Let the roff nS register switch SYNOPSIS mode early,
245 * such that the parser knows at all times
246 * whether this mode is on or off.
247 * Note that this mode is also switched by the Sh macro.
248 */
249 if (m->regs->regs[(int)REG_nS].set) {
250 if (m->regs->regs[(int)REG_nS].v.u)
251 m->flags |= MDOC_SYNOPSIS;
252 else
253 m->flags &= ~MDOC_SYNOPSIS;
254 }
255
256 return(('.' == buf[offs] || '\'' == buf[offs]) ?
257 mdoc_pmacro(m, ln, buf, offs) :
258 mdoc_ptext(m, ln, buf, offs));
259 }
260
261
262 int
263 mdoc_vmsg(struct mdoc *mdoc, enum mandocerr t,
264 int ln, int pos, const char *fmt, ...)
265 {
266 char buf[256];
267 va_list ap;
268
269 va_start(ap, fmt);
270 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
271 va_end(ap);
272
273 return((*mdoc->msg)(t, mdoc->data, ln, pos, buf));
274 }
275
276
277 int
278 mdoc_macro(MACRO_PROT_ARGS)
279 {
280 assert(tok < MDOC_MAX);
281
282 /* If we're in the body, deny prologue calls. */
283
284 if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
285 MDOC_PBODY & m->flags)
286 return(mdoc_pmsg(m, line, ppos, MANDOCERR_BADBODY));
287
288 /* If we're in the prologue, deny "body" macros. */
289
290 if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
291 ! (MDOC_PBODY & m->flags)) {
292 if ( ! mdoc_pmsg(m, line, ppos, MANDOCERR_BADPROLOG))
293 return(0);
294 if (NULL == m->meta.title)
295 m->meta.title = mandoc_strdup("UNKNOWN");
296 if (NULL == m->meta.vol)
297 m->meta.vol = mandoc_strdup("LOCAL");
298 if (NULL == m->meta.os)
299 m->meta.os = mandoc_strdup("LOCAL");
300 if (0 == m->meta.date)
301 m->meta.date = time(NULL);
302 m->flags |= MDOC_PBODY;
303 }
304
305 return((*mdoc_macros[tok].fp)(m, tok, line, ppos, pos, buf));
306 }
307
308
309 static int
310 node_append(struct mdoc *mdoc, struct mdoc_node *p)
311 {
312
313 assert(mdoc->last);
314 assert(mdoc->first);
315 assert(MDOC_ROOT != p->type);
316
317 switch (mdoc->next) {
318 case (MDOC_NEXT_SIBLING):
319 mdoc->last->next = p;
320 p->prev = mdoc->last;
321 p->parent = mdoc->last->parent;
322 break;
323 case (MDOC_NEXT_CHILD):
324 mdoc->last->child = p;
325 p->parent = mdoc->last;
326 break;
327 default:
328 abort();
329 /* NOTREACHED */
330 }
331
332 p->parent->nchild++;
333
334 if ( ! mdoc_valid_pre(mdoc, p))
335 return(0);
336 if ( ! mdoc_action_pre(mdoc, p))
337 return(0);
338
339 switch (p->type) {
340 case (MDOC_HEAD):
341 assert(MDOC_BLOCK == p->parent->type);
342 p->parent->head = p;
343 break;
344 case (MDOC_TAIL):
345 assert(MDOC_BLOCK == p->parent->type);
346 p->parent->tail = p;
347 break;
348 case (MDOC_BODY):
349 if (p->end)
350 break;
351 assert(MDOC_BLOCK == p->parent->type);
352 p->parent->body = p;
353 break;
354 default:
355 break;
356 }
357
358 mdoc->last = p;
359
360 switch (p->type) {
361 case (MDOC_TEXT):
362 if ( ! mdoc_valid_post(mdoc))
363 return(0);
364 if ( ! mdoc_action_post(mdoc))
365 return(0);
366 break;
367 default:
368 break;
369 }
370
371 return(1);
372 }
373
374
375 static struct mdoc_node *
376 node_alloc(struct mdoc *m, int line, int pos,
377 enum mdoct tok, enum mdoc_type type)
378 {
379 struct mdoc_node *p;
380
381 p = mandoc_calloc(1, sizeof(struct mdoc_node));
382 p->sec = m->lastsec;
383 p->line = line;
384 p->pos = pos;
385 p->tok = tok;
386 p->type = type;
387
388 /* Flag analysis. */
389
390 if (MDOC_SYNOPSIS & m->flags)
391 p->flags |= MDOC_SYNPRETTY;
392 else
393 p->flags &= ~MDOC_SYNPRETTY;
394 if (MDOC_NEWLINE & m->flags)
395 p->flags |= MDOC_LINE;
396 m->flags &= ~MDOC_NEWLINE;
397
398 return(p);
399 }
400
401
402 int
403 mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
404 {
405 struct mdoc_node *p;
406
407 p = node_alloc(m, line, pos, tok, MDOC_TAIL);
408 if ( ! node_append(m, p))
409 return(0);
410 m->next = MDOC_NEXT_CHILD;
411 return(1);
412 }
413
414
415 int
416 mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
417 {
418 struct mdoc_node *p;
419
420 assert(m->first);
421 assert(m->last);
422
423 p = node_alloc(m, line, pos, tok, MDOC_HEAD);
424 if ( ! node_append(m, p))
425 return(0);
426 m->next = MDOC_NEXT_CHILD;
427 return(1);
428 }
429
430
431 int
432 mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
433 {
434 struct mdoc_node *p;
435
436 p = node_alloc(m, line, pos, tok, MDOC_BODY);
437 if ( ! node_append(m, p))
438 return(0);
439 m->next = MDOC_NEXT_CHILD;
440 return(1);
441 }
442
443
444 int
445 mdoc_endbody_alloc(struct mdoc *m, int line, int pos, enum mdoct tok,
446 struct mdoc_node *body, enum mdoc_endbody end)
447 {
448 struct mdoc_node *p;
449
450 p = node_alloc(m, line, pos, tok, MDOC_BODY);
451 p->pending = body;
452 p->end = end;
453 if ( ! node_append(m, p))
454 return(0);
455 m->next = MDOC_NEXT_SIBLING;
456 return(1);
457 }
458
459
460 int
461 mdoc_block_alloc(struct mdoc *m, int line, int pos,
462 enum mdoct tok, struct mdoc_arg *args)
463 {
464 struct mdoc_node *p;
465
466 p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
467 p->args = args;
468 if (p->args)
469 (args->refcnt)++;
470 if ( ! node_append(m, p))
471 return(0);
472 m->next = MDOC_NEXT_CHILD;
473 return(1);
474 }
475
476
477 int
478 mdoc_elem_alloc(struct mdoc *m, int line, int pos,
479 enum mdoct tok, struct mdoc_arg *args)
480 {
481 struct mdoc_node *p;
482
483 p = node_alloc(m, line, pos, tok, MDOC_ELEM);
484 p->args = args;
485 if (p->args)
486 (args->refcnt)++;
487 if ( ! node_append(m, p))
488 return(0);
489 m->next = MDOC_NEXT_CHILD;
490 return(1);
491 }
492
493
494 int
495 mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
496 {
497 struct mdoc_node *n;
498 size_t sv, len;
499
500 len = strlen(p);
501
502 n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
503 n->string = mandoc_malloc(len + 1);
504 sv = strlcpy(n->string, p, len + 1);
505
506 /* Prohibit truncation. */
507 assert(sv < len + 1);
508
509 if ( ! node_append(m, n))
510 return(0);
511
512 m->next = MDOC_NEXT_SIBLING;
513 return(1);
514 }
515
516
517 /* FIXME: put in mdoc_node_delete(). */
518 void
519 mdoc_node_free(struct mdoc_node *p)
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
529 if (p->string)
530 free(p->string);
531 if (p->args)
532 mdoc_argv_free(p->args);
533 free(p);
534 }
535
536
537 static void
538 mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
539 {
540
541 /* Adjust siblings. */
542
543 if (n->prev)
544 n->prev->next = n->next;
545 if (n->next)
546 n->next->prev = n->prev;
547
548 /* Adjust parent. */
549
550 if (n->parent) {
551 n->parent->nchild--;
552 if (n->parent->child == n)
553 n->parent->child = n->prev ? n->prev : n->next;
554 }
555
556 /* Adjust parse point, if applicable. */
557
558 if (m && m->last == n) {
559 if (n->prev) {
560 m->last = n->prev;
561 m->next = MDOC_NEXT_SIBLING;
562 } else {
563 m->last = n->parent;
564 m->next = MDOC_NEXT_CHILD;
565 }
566 }
567
568 if (m && m->first == n)
569 m->first = NULL;
570 }
571
572
573 void
574 mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
575 {
576
577 while (p->child) {
578 assert(p->nchild);
579 mdoc_node_delete(m, p->child);
580 }
581 assert(0 == p->nchild);
582
583 mdoc_node_unlink(m, p);
584 mdoc_node_free(p);
585 }
586
587
588 /*
589 * Parse free-form text, that is, a line that does not begin with the
590 * control character.
591 */
592 static int
593 mdoc_ptext(struct mdoc *m, int line, char *buf, int offs)
594 {
595 char *c, *ws, *end;
596 struct mdoc_node *n;
597
598 /* Ignore bogus comments. */
599
600 if ('\\' == buf[offs] &&
601 '.' == buf[offs + 1] &&
602 '"' == buf[offs + 2])
603 return(mdoc_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
604
605 /* No text before an initial macro. */
606
607 if (SEC_NONE == m->lastnamed)
608 return(mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT));
609
610 assert(m->last);
611 n = m->last;
612
613 /*
614 * Divert directly to list processing if we're encountering a
615 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
616 * (a MDOC_BODY means it's already open, in which case we should
617 * process within its context in the normal way).
618 */
619
620 if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
621 LIST_column == n->data.Bl->type) {
622 /* `Bl' is open without any children. */
623 m->flags |= MDOC_FREECOL;
624 return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
625 }
626
627 if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
628 NULL != n->parent &&
629 MDOC_Bl == n->parent->tok &&
630 LIST_column == n->parent->data.Bl->type) {
631 /* `Bl' has block-level `It' children. */
632 m->flags |= MDOC_FREECOL;
633 return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
634 }
635
636 /*
637 * Search for the beginning of unescaped trailing whitespace (ws)
638 * and for the first character not to be output (end).
639 */
640
641 /* FIXME: replace with strcspn(). */
642 ws = NULL;
643 for (c = end = buf + offs; *c; c++) {
644 switch (*c) {
645 case '-':
646 if (mandoc_hyph(buf + offs, c))
647 *c = ASCII_HYPH;
648 ws = NULL;
649 break;
650 case ' ':
651 if (NULL == ws)
652 ws = c;
653 continue;
654 case '\t':
655 /*
656 * Always warn about trailing tabs,
657 * even outside literal context,
658 * where they should be put on the next line.
659 */
660 if (NULL == ws)
661 ws = c;
662 /*
663 * Strip trailing tabs in literal context only;
664 * outside, they affect the next line.
665 */
666 if (MDOC_LITERAL & m->flags)
667 continue;
668 break;
669 case '\\':
670 /* Skip the escaped character, too, if any. */
671 if (c[1])
672 c++;
673 /* FALLTHROUGH */
674 default:
675 ws = NULL;
676 break;
677 }
678 end = c + 1;
679 }
680 *end = '\0';
681
682 if (ws)
683 if ( ! mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE))
684 return(0);
685
686 if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) {
687 if ( ! mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN))
688 return(0);
689
690 /*
691 * Insert a `Pp' in the case of a blank line. Technically,
692 * blank lines aren't allowed, but enough manuals assume this
693 * behaviour that we want to work around it.
694 */
695 if ( ! mdoc_elem_alloc(m, line, offs, MDOC_Pp, NULL))
696 return(0);
697
698 m->next = MDOC_NEXT_SIBLING;
699 return(1);
700 }
701
702 if ( ! mdoc_word_alloc(m, line, offs, buf+offs))
703 return(0);
704
705 if (MDOC_LITERAL & m->flags)
706 return(1);
707
708 /*
709 * End-of-sentence check. If the last character is an unescaped
710 * EOS character, then flag the node as being the end of a
711 * sentence. The front-end will know how to interpret this.
712 */
713
714 assert(buf < end);
715
716 if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
717 m->last->flags |= MDOC_EOS;
718
719 return(1);
720 }
721
722
723 static int
724 macrowarn(struct mdoc *m, int ln, const char *buf, int offs)
725 {
726 int rc;
727
728 rc = mdoc_vmsg(m, MANDOCERR_MACRO, ln, offs,
729 "unknown macro: %s%s",
730 buf, strlen(buf) > 3 ? "..." : "");
731
732 /* FIXME: logic should be in driver. */
733 /* FIXME: broken, will error out and not omit a message. */
734 return(MDOC_IGN_MACRO & m->pflags ? rc : 0);
735 }
736
737
738 /*
739 * Parse a macro line, that is, a line beginning with the control
740 * character.
741 */
742 int
743 mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs)
744 {
745 enum mdoct tok;
746 int i, j, sv;
747 char mac[5];
748 struct mdoc_node *n;
749
750 /* Empty lines are ignored. */
751
752 offs++;
753
754 if ('\0' == buf[offs])
755 return(1);
756
757 i = offs;
758
759 /* Accept whitespace after the initial control char. */
760
761 if (' ' == buf[i]) {
762 i++;
763 while (buf[i] && ' ' == buf[i])
764 i++;
765 if ('\0' == buf[i])
766 return(1);
767 }
768
769 sv = i;
770
771 /* Copy the first word into a nil-terminated buffer. */
772
773 for (j = 0; j < 4; j++, i++) {
774 if ('\0' == (mac[j] = buf[i]))
775 break;
776 else if (' ' == buf[i])
777 break;
778
779 /* Check for invalid characters. */
780
781 if (isgraph((u_char)buf[i]))
782 continue;
783 if ( ! mdoc_pmsg(m, ln, i, MANDOCERR_BADCHAR))
784 return(0);
785 i--;
786 }
787
788 mac[j] = '\0';
789
790 if (j == 4 || j < 2) {
791 if ( ! macrowarn(m, ln, mac, sv))
792 goto err;
793 return(1);
794 }
795
796 if (MDOC_MAX == (tok = mdoc_hash_find(mac))) {
797 if ( ! macrowarn(m, ln, mac, sv))
798 goto err;
799 return(1);
800 }
801
802 /* The macro is sane. Jump to the next word. */
803
804 while (buf[i] && ' ' == buf[i])
805 i++;
806
807 /*
808 * Trailing whitespace. Note that tabs are allowed to be passed
809 * into the parser as "text", so we only warn about spaces here.
810 */
811
812 if ('\0' == buf[i] && ' ' == buf[i - 1])
813 if ( ! mdoc_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
814 goto err;
815
816 /*
817 * If an initial macro or a list invocation, divert directly
818 * into macro processing.
819 */
820
821 if (NULL == m->last || MDOC_It == tok || MDOC_El == tok) {
822 if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
823 goto err;
824 return(1);
825 }
826
827 n = m->last;
828 assert(m->last);
829
830 /*
831 * If the first macro of a `Bl -column', open an `It' block
832 * context around the parsed macro.
833 */
834
835 if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
836 LIST_column == n->data.Bl->type) {
837 m->flags |= MDOC_FREECOL;
838 if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
839 goto err;
840 return(1);
841 }
842
843 /*
844 * If we're following a block-level `It' within a `Bl -column'
845 * context (perhaps opened in the above block or in ptext()),
846 * then open an `It' block context around the parsed macro.
847 */
848
849 if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
850 NULL != n->parent &&
851 MDOC_Bl == n->parent->tok &&
852 LIST_column == n->parent->data.Bl->type) {
853 m->flags |= MDOC_FREECOL;
854 if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
855 goto err;
856 return(1);
857 }
858
859 /* Normal processing of a macro. */
860
861 if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
862 goto err;
863
864 return(1);
865
866 err: /* Error out. */
867
868 m->flags |= MDOC_HALT;
869 return(0);
870 }
871
872