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