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