]> git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
Correctly make quotes around `Lk' link-name argument. Noted by Aldis
[mandoc.git] / mdoc.c
1 /* $Id: mdoc.c,v 1.162 2010/08/08 14:51:32 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 static int macrowarn(struct mdoc *, int,
102 const char *, int);
103
104
105 const struct mdoc_node *
106 mdoc_node(const struct mdoc *m)
107 {
108
109 return(MDOC_HALT & m->flags ? NULL : m->first);
110 }
111
112
113 const struct mdoc_meta *
114 mdoc_meta(const struct mdoc *m)
115 {
116
117 return(MDOC_HALT & m->flags ? NULL : &m->meta);
118 }
119
120
121 /*
122 * Frees volatile resources (parse tree, meta-data, fields).
123 */
124 static void
125 mdoc_free1(struct mdoc *mdoc)
126 {
127
128 if (mdoc->first)
129 mdoc_node_delete(mdoc, mdoc->first);
130 if (mdoc->meta.title)
131 free(mdoc->meta.title);
132 if (mdoc->meta.os)
133 free(mdoc->meta.os);
134 if (mdoc->meta.name)
135 free(mdoc->meta.name);
136 if (mdoc->meta.arch)
137 free(mdoc->meta.arch);
138 if (mdoc->meta.vol)
139 free(mdoc->meta.vol);
140 if (mdoc->meta.msec)
141 free(mdoc->meta.msec);
142 }
143
144
145 /*
146 * Allocate all volatile resources (parse tree, meta-data, fields).
147 */
148 static void
149 mdoc_alloc1(struct mdoc *mdoc)
150 {
151
152 memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
153 mdoc->flags = 0;
154 mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
155 mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
156 mdoc->first = mdoc->last;
157 mdoc->last->type = MDOC_ROOT;
158 mdoc->next = MDOC_NEXT_CHILD;
159 }
160
161
162 /*
163 * Free up volatile resources (see mdoc_free1()) then re-initialises the
164 * data with mdoc_alloc1(). After invocation, parse data has been reset
165 * and the parser is ready for re-invocation on a new tree; however,
166 * cross-parse non-volatile data is kept intact.
167 */
168 void
169 mdoc_reset(struct mdoc *mdoc)
170 {
171
172 mdoc_free1(mdoc);
173 mdoc_alloc1(mdoc);
174 }
175
176
177 /*
178 * Completely free up all volatile and non-volatile parse resources.
179 * After invocation, the pointer is no longer usable.
180 */
181 void
182 mdoc_free(struct mdoc *mdoc)
183 {
184
185 mdoc_free1(mdoc);
186 free(mdoc);
187 }
188
189
190 /*
191 * Allocate volatile and non-volatile parse resources.
192 */
193 struct mdoc *
194 mdoc_alloc(struct regset *regs, void *data,
195 int pflags, mandocmsg msg)
196 {
197 struct mdoc *p;
198
199 p = mandoc_calloc(1, sizeof(struct mdoc));
200
201 p->msg = msg;
202 p->data = data;
203 p->pflags = pflags;
204 p->regs = regs;
205
206 mdoc_hash_init();
207 mdoc_alloc1(p);
208 return(p);
209 }
210
211
212 /*
213 * Climb back up the parse tree, validating open scopes. Mostly calls
214 * through to macro_end() in macro.c.
215 */
216 int
217 mdoc_endparse(struct mdoc *m)
218 {
219
220 if (MDOC_HALT & m->flags)
221 return(0);
222 else if (mdoc_macroend(m))
223 return(1);
224 m->flags |= MDOC_HALT;
225 return(0);
226 }
227
228
229 /*
230 * Main parse routine. Parses a single line -- really just hands off to
231 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
232 */
233 int
234 mdoc_parseln(struct mdoc *m, int ln, char *buf, int offs)
235 {
236
237 if (MDOC_HALT & m->flags)
238 return(0);
239
240 m->flags |= MDOC_NEWLINE;
241
242 /*
243 * Let the roff nS register switch SYNOPSIS mode early,
244 * such that the parser knows at all times
245 * whether this mode is on or off.
246 * Note that this mode is also switched by the Sh macro.
247 */
248 if (m->regs->regs[(int)REG_nS].set) {
249 if (m->regs->regs[(int)REG_nS].v.u)
250 m->flags |= MDOC_SYNOPSIS;
251 else
252 m->flags &= ~MDOC_SYNOPSIS;
253 }
254
255 return(('.' == buf[offs] || '\'' == buf[offs]) ?
256 mdoc_pmacro(m, ln, buf, offs) :
257 mdoc_ptext(m, ln, buf, offs));
258 }
259
260
261 int
262 mdoc_vmsg(struct mdoc *mdoc, enum mandocerr t,
263 int ln, int pos, const char *fmt, ...)
264 {
265 char buf[256];
266 va_list ap;
267
268 va_start(ap, fmt);
269 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
270 va_end(ap);
271
272 return((*mdoc->msg)(t, mdoc->data, ln, pos, buf));
273 }
274
275
276 int
277 mdoc_macro(MACRO_PROT_ARGS)
278 {
279 assert(tok < MDOC_MAX);
280
281 /* If we're in the body, deny prologue calls. */
282
283 if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
284 MDOC_PBODY & m->flags)
285 return(mdoc_pmsg(m, line, ppos, MANDOCERR_BADBODY));
286
287 /* If we're in the prologue, deny "body" macros. */
288
289 if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
290 ! (MDOC_PBODY & m->flags)) {
291 if ( ! mdoc_pmsg(m, line, ppos, MANDOCERR_BADPROLOG))
292 return(0);
293 if (NULL == m->meta.title)
294 m->meta.title = mandoc_strdup("UNKNOWN");
295 if (NULL == m->meta.vol)
296 m->meta.vol = mandoc_strdup("LOCAL");
297 if (NULL == m->meta.os)
298 m->meta.os = mandoc_strdup("LOCAL");
299 if (0 == m->meta.date)
300 m->meta.date = time(NULL);
301 m->flags |= MDOC_PBODY;
302 }
303
304 return((*mdoc_macros[tok].fp)(m, tok, line, ppos, pos, buf));
305 }
306
307
308 static int
309 node_append(struct mdoc *mdoc, struct mdoc_node *p)
310 {
311
312 assert(mdoc->last);
313 assert(mdoc->first);
314 assert(MDOC_ROOT != p->type);
315
316 switch (mdoc->next) {
317 case (MDOC_NEXT_SIBLING):
318 mdoc->last->next = p;
319 p->prev = mdoc->last;
320 p->parent = mdoc->last->parent;
321 break;
322 case (MDOC_NEXT_CHILD):
323 mdoc->last->child = p;
324 p->parent = mdoc->last;
325 break;
326 default:
327 abort();
328 /* NOTREACHED */
329 }
330
331 p->parent->nchild++;
332
333 if ( ! mdoc_valid_pre(mdoc, p))
334 return(0);
335 if ( ! mdoc_action_pre(mdoc, p))
336 return(0);
337
338 switch (p->type) {
339 case (MDOC_HEAD):
340 assert(MDOC_BLOCK == p->parent->type);
341 p->parent->head = p;
342 break;
343 case (MDOC_TAIL):
344 assert(MDOC_BLOCK == p->parent->type);
345 p->parent->tail = p;
346 break;
347 case (MDOC_BODY):
348 if (p->end)
349 break;
350 assert(MDOC_BLOCK == p->parent->type);
351 p->parent->body = p;
352 break;
353 default:
354 break;
355 }
356
357 mdoc->last = p;
358
359 switch (p->type) {
360 case (MDOC_TEXT):
361 if ( ! mdoc_valid_post(mdoc))
362 return(0);
363 if ( ! mdoc_action_post(mdoc))
364 return(0);
365 break;
366 default:
367 break;
368 }
369
370 return(1);
371 }
372
373
374 static struct mdoc_node *
375 node_alloc(struct mdoc *m, int line, int pos,
376 enum mdoct tok, enum mdoc_type type)
377 {
378 struct mdoc_node *p;
379
380 p = mandoc_calloc(1, sizeof(struct mdoc_node));
381 p->sec = m->lastsec;
382 p->line = line;
383 p->pos = pos;
384 p->tok = tok;
385 p->type = type;
386
387 /* Flag analysis. */
388
389 if (MDOC_SYNOPSIS & m->flags)
390 p->flags |= MDOC_SYNPRETTY;
391 else
392 p->flags &= ~MDOC_SYNPRETTY;
393 if (MDOC_NEWLINE & m->flags)
394 p->flags |= MDOC_LINE;
395 m->flags &= ~MDOC_NEWLINE;
396
397 return(p);
398 }
399
400
401 int
402 mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
403 {
404 struct mdoc_node *p;
405
406 p = node_alloc(m, line, pos, tok, MDOC_TAIL);
407 if ( ! node_append(m, p))
408 return(0);
409 m->next = MDOC_NEXT_CHILD;
410 return(1);
411 }
412
413
414 int
415 mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
416 {
417 struct mdoc_node *p;
418
419 assert(m->first);
420 assert(m->last);
421
422 p = node_alloc(m, line, pos, tok, MDOC_HEAD);
423 if ( ! node_append(m, p))
424 return(0);
425 m->next = MDOC_NEXT_CHILD;
426 return(1);
427 }
428
429
430 int
431 mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
432 {
433 struct mdoc_node *p;
434
435 p = node_alloc(m, line, pos, tok, MDOC_BODY);
436 if ( ! node_append(m, p))
437 return(0);
438 m->next = MDOC_NEXT_CHILD;
439 return(1);
440 }
441
442
443 int
444 mdoc_endbody_alloc(struct mdoc *m, int line, int pos, enum mdoct tok,
445 struct mdoc_node *body, enum mdoc_endbody end)
446 {
447 struct mdoc_node *p;
448
449 p = node_alloc(m, line, pos, tok, MDOC_BODY);
450 p->pending = body;
451 p->end = end;
452 if ( ! node_append(m, p))
453 return(0);
454 m->next = MDOC_NEXT_SIBLING;
455 return(1);
456 }
457
458
459 int
460 mdoc_block_alloc(struct mdoc *m, int line, int pos,
461 enum mdoct tok, struct mdoc_arg *args)
462 {
463 struct mdoc_node *p;
464
465 p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
466 p->args = args;
467 if (p->args)
468 (args->refcnt)++;
469 if ( ! node_append(m, p))
470 return(0);
471 m->next = MDOC_NEXT_CHILD;
472 return(1);
473 }
474
475
476 int
477 mdoc_elem_alloc(struct mdoc *m, int line, int pos,
478 enum mdoct tok, struct mdoc_arg *args)
479 {
480 struct mdoc_node *p;
481
482 p = node_alloc(m, line, pos, tok, MDOC_ELEM);
483 p->args = args;
484 if (p->args)
485 (args->refcnt)++;
486 if ( ! node_append(m, p))
487 return(0);
488 m->next = MDOC_NEXT_CHILD;
489 return(1);
490 }
491
492
493 int
494 mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
495 {
496 struct mdoc_node *n;
497 size_t sv, len;
498
499 len = strlen(p);
500
501 n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
502 n->string = mandoc_malloc(len + 1);
503 sv = strlcpy(n->string, p, len + 1);
504
505 /* Prohibit truncation. */
506 assert(sv < len + 1);
507
508 if ( ! node_append(m, n))
509 return(0);
510
511 m->next = MDOC_NEXT_SIBLING;
512 return(1);
513 }
514
515
516 static void
517 mdoc_node_free(struct mdoc_node *p)
518 {
519
520 /*
521 * XXX: if these end up being problematic in terms of memory
522 * management and dereferencing freed blocks, then make them
523 * into reference-counted double-pointers.
524 */
525
526 if (MDOC_Bd == p->tok && MDOC_BLOCK == p->type)
527 if (p->data.Bd)
528 free(p->data.Bd);
529 if (MDOC_Bl == p->tok && MDOC_BLOCK == p->type)
530 if (p->data.Bl)
531 free(p->data.Bl);
532 if (MDOC_Bf == p->tok && MDOC_HEAD == p->type)
533 if (p->data.Bf)
534 free(p->data.Bf);
535
536 if (p->string)
537 free(p->string);
538 if (p->args)
539 mdoc_argv_free(p->args);
540 free(p);
541 }
542
543
544 static void
545 mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
546 {
547
548 /* Adjust siblings. */
549
550 if (n->prev)
551 n->prev->next = n->next;
552 if (n->next)
553 n->next->prev = n->prev;
554
555 /* Adjust parent. */
556
557 if (n->parent) {
558 n->parent->nchild--;
559 if (n->parent->child == n)
560 n->parent->child = n->prev ? n->prev : n->next;
561 }
562
563 /* Adjust parse point, if applicable. */
564
565 if (m && m->last == n) {
566 if (n->prev) {
567 m->last = n->prev;
568 m->next = MDOC_NEXT_SIBLING;
569 } else {
570 m->last = n->parent;
571 m->next = MDOC_NEXT_CHILD;
572 }
573 }
574
575 if (m && m->first == n)
576 m->first = NULL;
577 }
578
579
580 void
581 mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
582 {
583
584 while (p->child) {
585 assert(p->nchild);
586 mdoc_node_delete(m, p->child);
587 }
588 assert(0 == p->nchild);
589
590 mdoc_node_unlink(m, p);
591 mdoc_node_free(p);
592 }
593
594
595 /*
596 * Parse free-form text, that is, a line that does not begin with the
597 * control character.
598 */
599 static int
600 mdoc_ptext(struct mdoc *m, int line, char *buf, int offs)
601 {
602 char *c, *ws, *end;
603 struct mdoc_node *n;
604
605 /* Ignore bogus comments. */
606
607 if ('\\' == buf[offs] &&
608 '.' == buf[offs + 1] &&
609 '"' == buf[offs + 2])
610 return(mdoc_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
611
612 /* No text before an initial macro. */
613
614 if (SEC_NONE == m->lastnamed)
615 return(mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT));
616
617 assert(m->last);
618 n = m->last;
619
620 /*
621 * Divert directly to list processing if we're encountering a
622 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
623 * (a MDOC_BODY means it's already open, in which case we should
624 * process within its context in the normal way).
625 */
626
627 if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
628 LIST_column == n->data.Bl->type) {
629 /* `Bl' is open without any children. */
630 m->flags |= MDOC_FREECOL;
631 return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
632 }
633
634 if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
635 NULL != n->parent &&
636 MDOC_Bl == n->parent->tok &&
637 LIST_column == n->parent->data.Bl->type) {
638 /* `Bl' has block-level `It' children. */
639 m->flags |= MDOC_FREECOL;
640 return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
641 }
642
643 /*
644 * Search for the beginning of unescaped trailing whitespace (ws)
645 * and for the first character not to be output (end).
646 */
647
648 /* FIXME: replace with strcspn(). */
649 ws = NULL;
650 for (c = end = buf + offs; *c; c++) {
651 switch (*c) {
652 case '-':
653 if (mandoc_hyph(buf + offs, c))
654 *c = ASCII_HYPH;
655 ws = NULL;
656 break;
657 case ' ':
658 if (NULL == ws)
659 ws = c;
660 continue;
661 case '\t':
662 /*
663 * Always warn about trailing tabs,
664 * even outside literal context,
665 * where they should be put on the next line.
666 */
667 if (NULL == ws)
668 ws = c;
669 /*
670 * Strip trailing tabs in literal context only;
671 * outside, they affect the next line.
672 */
673 if (MDOC_LITERAL & m->flags)
674 continue;
675 break;
676 case '\\':
677 /* Skip the escaped character, too, if any. */
678 if (c[1])
679 c++;
680 /* FALLTHROUGH */
681 default:
682 ws = NULL;
683 break;
684 }
685 end = c + 1;
686 }
687 *end = '\0';
688
689 if (ws)
690 if ( ! mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE))
691 return(0);
692
693 if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) {
694 if ( ! mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN))
695 return(0);
696
697 /*
698 * Insert a `Pp' in the case of a blank line. Technically,
699 * blank lines aren't allowed, but enough manuals assume this
700 * behaviour that we want to work around it.
701 */
702 if ( ! mdoc_elem_alloc(m, line, offs, MDOC_Pp, NULL))
703 return(0);
704
705 m->next = MDOC_NEXT_SIBLING;
706 return(1);
707 }
708
709 if ( ! mdoc_word_alloc(m, line, offs, buf+offs))
710 return(0);
711
712 if (MDOC_LITERAL & m->flags)
713 return(1);
714
715 /*
716 * End-of-sentence check. If the last character is an unescaped
717 * EOS character, then flag the node as being the end of a
718 * sentence. The front-end will know how to interpret this.
719 */
720
721 assert(buf < end);
722
723 if (mandoc_eos(buf+offs, (size_t)(end-buf-offs), 0))
724 m->last->flags |= MDOC_EOS;
725
726 return(1);
727 }
728
729
730 static int
731 macrowarn(struct mdoc *m, int ln, const char *buf, int offs)
732 {
733 int rc;
734
735 rc = mdoc_vmsg(m, MANDOCERR_MACRO, ln, offs,
736 "unknown macro: %s%s",
737 buf, strlen(buf) > 3 ? "..." : "");
738
739 /* FIXME: logic should be in driver. */
740 /* FIXME: broken, will error out and not omit a message. */
741 return(MDOC_IGN_MACRO & m->pflags ? rc : 0);
742 }
743
744
745 /*
746 * Parse a macro line, that is, a line beginning with the control
747 * character.
748 */
749 static int
750 mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs)
751 {
752 enum mdoct tok;
753 int i, j, sv;
754 char mac[5];
755 struct mdoc_node *n;
756
757 /* Empty lines are ignored. */
758
759 offs++;
760
761 if ('\0' == buf[offs])
762 return(1);
763
764 i = offs;
765
766 /* Accept tabs/whitespace after the initial control char. */
767
768 if (' ' == buf[i] || '\t' == buf[i]) {
769 i++;
770 while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
771 i++;
772 if ('\0' == buf[i])
773 return(1);
774 }
775
776 sv = i;
777
778 /*
779 * Copy the first word into a nil-terminated buffer.
780 * Stop copying when a tab, space, or eoln is encountered.
781 */
782
783 j = 0;
784 while (j < 4 && '\0' != buf[i] && ' ' != buf[i] && '\t' != buf[i])
785 mac[j++] = buf[i++];
786 mac[j] = '\0';
787
788 if (j == 4 || j < 2) {
789 if ( ! macrowarn(m, ln, mac, sv))
790 goto err;
791 return(1);
792 }
793
794 if (MDOC_MAX == (tok = mdoc_hash_find(mac))) {
795 if ( ! macrowarn(m, ln, mac, sv))
796 goto err;
797 return(1);
798 }
799
800 /* Disregard the first trailing tab, if applicable. */
801
802 if ('\t' == buf[i])
803 i++;
804
805 /* Jump to the next non-whitespace word. */
806
807 while (buf[i] && ' ' == buf[i])
808 i++;
809
810 /*
811 * Trailing whitespace. Note that tabs are allowed to be passed
812 * into the parser as "text", so we only warn about spaces here.
813 */
814
815 if ('\0' == buf[i] && ' ' == buf[i - 1])
816 if ( ! mdoc_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
817 goto err;
818
819 /*
820 * If an initial macro or a list invocation, divert directly
821 * into macro processing.
822 */
823
824 if (NULL == m->last || MDOC_It == tok || MDOC_El == tok) {
825 if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
826 goto err;
827 return(1);
828 }
829
830 n = m->last;
831 assert(m->last);
832
833 /*
834 * If the first macro of a `Bl -column', open an `It' block
835 * context around the parsed macro.
836 */
837
838 if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
839 LIST_column == n->data.Bl->type) {
840 m->flags |= MDOC_FREECOL;
841 if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
842 goto err;
843 return(1);
844 }
845
846 /*
847 * If we're following a block-level `It' within a `Bl -column'
848 * context (perhaps opened in the above block or in ptext()),
849 * then open an `It' block context around the parsed macro.
850 */
851
852 if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
853 NULL != n->parent &&
854 MDOC_Bl == n->parent->tok &&
855 LIST_column == n->parent->data.Bl->type) {
856 m->flags |= MDOC_FREECOL;
857 if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
858 goto err;
859 return(1);
860 }
861
862 /* Normal processing of a macro. */
863
864 if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
865 goto err;
866
867 return(1);
868
869 err: /* Error out. */
870
871 m->flags |= MDOC_HALT;
872 return(0);
873 }
874
875