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