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