]> git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
merge from VERSION_1_12:
[mandoc.git] / mdoc.c
1 /* $Id: mdoc.c,v 1.204 2013/10/05 22:08:12 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012 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 *mdoc)
108 {
109
110 assert( ! (MDOC_HALT & mdoc->flags));
111 return(mdoc->first);
112 }
113
114
115 const struct mdoc_meta *
116 mdoc_meta(const struct mdoc *mdoc)
117 {
118
119 assert( ! (MDOC_HALT & mdoc->flags));
120 return(&mdoc->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 *mdoc)
222 {
223
224 assert( ! (MDOC_HALT & mdoc->flags));
225 if (mdoc_macroend(mdoc))
226 return(1);
227 mdoc->flags |= MDOC_HALT;
228 return(0);
229 }
230
231 int
232 mdoc_addeqn(struct mdoc *mdoc, const struct eqn *ep)
233 {
234 struct mdoc_node *n;
235
236 assert( ! (MDOC_HALT & mdoc->flags));
237
238 /* No text before an initial macro. */
239
240 if (SEC_NONE == mdoc->lastnamed) {
241 mdoc_pmsg(mdoc, ep->ln, ep->pos, MANDOCERR_NOTEXT);
242 return(1);
243 }
244
245 n = node_alloc(mdoc, ep->ln, ep->pos, MDOC_MAX, MDOC_EQN);
246 n->eqn = ep;
247
248 if ( ! node_append(mdoc, n))
249 return(0);
250
251 mdoc->next = MDOC_NEXT_SIBLING;
252 return(1);
253 }
254
255 int
256 mdoc_addspan(struct mdoc *mdoc, const struct tbl_span *sp)
257 {
258 struct mdoc_node *n;
259
260 assert( ! (MDOC_HALT & mdoc->flags));
261
262 /* No text before an initial macro. */
263
264 if (SEC_NONE == mdoc->lastnamed) {
265 mdoc_pmsg(mdoc, sp->line, 0, MANDOCERR_NOTEXT);
266 return(1);
267 }
268
269 n = node_alloc(mdoc, sp->line, 0, MDOC_MAX, MDOC_TBL);
270 n->span = sp;
271
272 if ( ! node_append(mdoc, n))
273 return(0);
274
275 mdoc->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 *mdoc, int ln, char *buf, int offs)
286 {
287
288 assert( ! (MDOC_HALT & mdoc->flags));
289
290 mdoc->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_getreg(mdoc->roff, "nS"))
299 mdoc->flags |= MDOC_SYNOPSIS;
300 else
301 mdoc->flags &= ~MDOC_SYNOPSIS;
302
303 return(roff_getcontrol(mdoc->roff, buf, &offs) ?
304 mdoc_pmacro(mdoc, ln, buf, offs) :
305 mdoc_ptext(mdoc, 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 & mdoc->flags) {
317 mdoc_pmsg(mdoc, 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 & mdoc->flags)) {
325 mdoc_pmsg(mdoc, line, ppos, MANDOCERR_BADPROLOG);
326 if (NULL == mdoc->meta.msec)
327 mdoc->meta.msec = mandoc_strdup("1");
328 if (NULL == mdoc->meta.title)
329 mdoc->meta.title = mandoc_strdup("UNKNOWN");
330 if (NULL == mdoc->meta.vol)
331 mdoc->meta.vol = mandoc_strdup("LOCAL");
332 if (NULL == mdoc->meta.os)
333 mdoc->meta.os = mandoc_strdup("LOCAL");
334 if (NULL == mdoc->meta.date)
335 mdoc->meta.date = mandoc_normdate
336 (mdoc->parse, NULL, line, ppos);
337 mdoc->flags |= MDOC_PBODY;
338 }
339
340 return((*mdoc_macros[tok].fp)(mdoc, 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 if (ENDBODY_NOT != p->end)
377 break;
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 *mdoc, 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 = mdoc->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 & mdoc->flags)
443 p->flags |= MDOC_SYNPRETTY;
444 else
445 p->flags &= ~MDOC_SYNPRETTY;
446 if (MDOC_NEWLINE & mdoc->flags)
447 p->flags |= MDOC_LINE;
448 mdoc->flags &= ~MDOC_NEWLINE;
449
450 return(p);
451 }
452
453
454 int
455 mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok)
456 {
457 struct mdoc_node *p;
458
459 p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL);
460 if ( ! node_append(mdoc, p))
461 return(0);
462 mdoc->next = MDOC_NEXT_CHILD;
463 return(1);
464 }
465
466
467 int
468 mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok)
469 {
470 struct mdoc_node *p;
471
472 assert(mdoc->first);
473 assert(mdoc->last);
474
475 p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD);
476 if ( ! node_append(mdoc, p))
477 return(0);
478 mdoc->next = MDOC_NEXT_CHILD;
479 return(1);
480 }
481
482
483 int
484 mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok)
485 {
486 struct mdoc_node *p;
487
488 p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
489 if ( ! node_append(mdoc, p))
490 return(0);
491 mdoc->next = MDOC_NEXT_CHILD;
492 return(1);
493 }
494
495
496 int
497 mdoc_endbody_alloc(struct mdoc *mdoc, 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(mdoc, line, pos, tok, MDOC_BODY);
503 p->pending = body;
504 p->norm = body->norm;
505 p->end = end;
506 if ( ! node_append(mdoc, p))
507 return(0);
508 mdoc->next = MDOC_NEXT_SIBLING;
509 return(1);
510 }
511
512
513 int
514 mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
515 enum mdoct tok, struct mdoc_arg *args)
516 {
517 struct mdoc_node *p;
518
519 p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK);
520 p->args = args;
521 if (p->args)
522 (args->refcnt)++;
523
524 switch (tok) {
525 case (MDOC_Bd):
526 /* FALLTHROUGH */
527 case (MDOC_Bf):
528 /* FALLTHROUGH */
529 case (MDOC_Bl):
530 /* FALLTHROUGH */
531 case (MDOC_Rs):
532 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
533 break;
534 default:
535 break;
536 }
537
538 if ( ! node_append(mdoc, p))
539 return(0);
540 mdoc->next = MDOC_NEXT_CHILD;
541 return(1);
542 }
543
544
545 int
546 mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
547 enum mdoct tok, struct mdoc_arg *args)
548 {
549 struct mdoc_node *p;
550
551 p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM);
552 p->args = args;
553 if (p->args)
554 (args->refcnt)++;
555
556 switch (tok) {
557 case (MDOC_An):
558 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
559 break;
560 default:
561 break;
562 }
563
564 if ( ! node_append(mdoc, p))
565 return(0);
566 mdoc->next = MDOC_NEXT_CHILD;
567 return(1);
568 }
569
570 int
571 mdoc_word_alloc(struct mdoc *mdoc, int line, int pos, const char *p)
572 {
573 struct mdoc_node *n;
574
575 n = node_alloc(mdoc, line, pos, MDOC_MAX, MDOC_TEXT);
576 n->string = roff_strdup(mdoc->roff, p);
577
578 if ( ! node_append(mdoc, n))
579 return(0);
580
581 mdoc->next = MDOC_NEXT_SIBLING;
582 return(1);
583 }
584
585
586 static void
587 mdoc_node_free(struct mdoc_node *p)
588 {
589
590 if (MDOC_BLOCK == p->type || MDOC_ELEM == p->type)
591 free(p->norm);
592 if (p->string)
593 free(p->string);
594 if (p->args)
595 mdoc_argv_free(p->args);
596 free(p);
597 }
598
599
600 static void
601 mdoc_node_unlink(struct mdoc *mdoc, struct mdoc_node *n)
602 {
603
604 /* Adjust siblings. */
605
606 if (n->prev)
607 n->prev->next = n->next;
608 if (n->next)
609 n->next->prev = n->prev;
610
611 /* Adjust parent. */
612
613 if (n->parent) {
614 n->parent->nchild--;
615 if (n->parent->child == n)
616 n->parent->child = n->prev ? n->prev : n->next;
617 if (n->parent->last == n)
618 n->parent->last = n->prev ? n->prev : NULL;
619 }
620
621 /* Adjust parse point, if applicable. */
622
623 if (mdoc && mdoc->last == n) {
624 if (n->prev) {
625 mdoc->last = n->prev;
626 mdoc->next = MDOC_NEXT_SIBLING;
627 } else {
628 mdoc->last = n->parent;
629 mdoc->next = MDOC_NEXT_CHILD;
630 }
631 }
632
633 if (mdoc && mdoc->first == n)
634 mdoc->first = NULL;
635 }
636
637
638 void
639 mdoc_node_delete(struct mdoc *mdoc, struct mdoc_node *p)
640 {
641
642 while (p->child) {
643 assert(p->nchild);
644 mdoc_node_delete(mdoc, p->child);
645 }
646 assert(0 == p->nchild);
647
648 mdoc_node_unlink(mdoc, p);
649 mdoc_node_free(p);
650 }
651
652 int
653 mdoc_node_relink(struct mdoc *mdoc, struct mdoc_node *p)
654 {
655
656 mdoc_node_unlink(mdoc, p);
657 return(node_append(mdoc, p));
658 }
659
660 #if 0
661 /*
662 * Pre-treat a text line.
663 * Text lines can consist of equations, which must be handled apart from
664 * the regular text.
665 * Thus, use this function to step through a line checking if it has any
666 * equations embedded in it.
667 * This must handle multiple equations AND equations that do not end at
668 * the end-of-line, i.e., will re-enter in the next roff parse.
669 */
670 static int
671 mdoc_preptext(struct mdoc *mdoc, int line, char *buf, int offs)
672 {
673 char *start, *end;
674 char delim;
675
676 while ('\0' != buf[offs]) {
677 /* Mark starting position if eqn is set. */
678 start = NULL;
679 if ('\0' != (delim = roff_eqndelim(mdoc->roff)))
680 if (NULL != (start = strchr(buf + offs, delim)))
681 *start++ = '\0';
682
683 /* Parse text as normal. */
684 if ( ! mdoc_ptext(mdoc, line, buf, offs))
685 return(0);
686
687 /* Continue only if an equation exists. */
688 if (NULL == start)
689 break;
690
691 /* Read past the end of the equation. */
692 offs += start - (buf + offs);
693 assert(start == &buf[offs]);
694 if (NULL != (end = strchr(buf + offs, delim))) {
695 *end++ = '\0';
696 while (' ' == *end)
697 end++;
698 }
699
700 /* Parse the equation itself. */
701 roff_openeqn(mdoc->roff, NULL, line, offs, buf);
702
703 /* Process a finished equation? */
704 if (roff_closeeqn(mdoc->roff))
705 if ( ! mdoc_addeqn(mdoc, roff_eqn(mdoc->roff)))
706 return(0);
707 offs += (end - (buf + offs));
708 }
709
710 return(1);
711 }
712 #endif
713
714 /*
715 * Parse free-form text, that is, a line that does not begin with the
716 * control character.
717 */
718 static int
719 mdoc_ptext(struct mdoc *mdoc, int line, char *buf, int offs)
720 {
721 char *c, *ws, *end;
722 struct mdoc_node *n;
723
724 /* No text before an initial macro. */
725
726 if (SEC_NONE == mdoc->lastnamed) {
727 mdoc_pmsg(mdoc, line, offs, MANDOCERR_NOTEXT);
728 return(1);
729 }
730
731 assert(mdoc->last);
732 n = mdoc->last;
733
734 /*
735 * Divert directly to list processing if we're encountering a
736 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
737 * (a MDOC_BODY means it's already open, in which case we should
738 * process within its context in the normal way).
739 */
740
741 if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
742 LIST_column == n->norm->Bl.type) {
743 /* `Bl' is open without any children. */
744 mdoc->flags |= MDOC_FREECOL;
745 return(mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf));
746 }
747
748 if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
749 NULL != n->parent &&
750 MDOC_Bl == n->parent->tok &&
751 LIST_column == n->parent->norm->Bl.type) {
752 /* `Bl' has block-level `It' children. */
753 mdoc->flags |= MDOC_FREECOL;
754 return(mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf));
755 }
756
757 /*
758 * Search for the beginning of unescaped trailing whitespace (ws)
759 * and for the first character not to be output (end).
760 */
761
762 /* FIXME: replace with strcspn(). */
763 ws = NULL;
764 for (c = end = buf + offs; *c; c++) {
765 switch (*c) {
766 case ' ':
767 if (NULL == ws)
768 ws = c;
769 continue;
770 case '\t':
771 /*
772 * Always warn about trailing tabs,
773 * even outside literal context,
774 * where they should be put on the next line.
775 */
776 if (NULL == ws)
777 ws = c;
778 /*
779 * Strip trailing tabs in literal context only;
780 * outside, they affect the next line.
781 */
782 if (MDOC_LITERAL & mdoc->flags)
783 continue;
784 break;
785 case '\\':
786 /* Skip the escaped character, too, if any. */
787 if (c[1])
788 c++;
789 /* FALLTHROUGH */
790 default:
791 ws = NULL;
792 break;
793 }
794 end = c + 1;
795 }
796 *end = '\0';
797
798 if (ws)
799 mdoc_pmsg(mdoc, line, (int)(ws-buf), MANDOCERR_EOLNSPACE);
800
801 if ('\0' == buf[offs] && ! (MDOC_LITERAL & mdoc->flags)) {
802 mdoc_pmsg(mdoc, line, (int)(c-buf), MANDOCERR_NOBLANKLN);
803
804 /*
805 * Insert a `sp' in the case of a blank line. Technically,
806 * blank lines aren't allowed, but enough manuals assume this
807 * behaviour that we want to work around it.
808 */
809 if ( ! mdoc_elem_alloc(mdoc, line, offs, MDOC_sp, NULL))
810 return(0);
811
812 mdoc->next = MDOC_NEXT_SIBLING;
813
814 return(mdoc_valid_post(mdoc));
815 }
816
817 if ( ! mdoc_word_alloc(mdoc, line, offs, buf+offs))
818 return(0);
819
820 if (MDOC_LITERAL & mdoc->flags)
821 return(1);
822
823 /*
824 * End-of-sentence check. If the last character is an unescaped
825 * EOS character, then flag the node as being the end of a
826 * sentence. The front-end will know how to interpret this.
827 */
828
829 assert(buf < end);
830
831 if (mandoc_eos(buf+offs, (size_t)(end-buf-offs), 0))
832 mdoc->last->flags |= MDOC_EOS;
833
834 return(1);
835 }
836
837
838 /*
839 * Parse a macro line, that is, a line beginning with the control
840 * character.
841 */
842 static int
843 mdoc_pmacro(struct mdoc *mdoc, int ln, char *buf, int offs)
844 {
845 enum mdoct tok;
846 int i, sv;
847 char mac[5];
848 struct mdoc_node *n;
849
850 /* Empty post-control lines are ignored. */
851
852 if ('"' == buf[offs]) {
853 mdoc_pmsg(mdoc, ln, offs, MANDOCERR_BADCOMMENT);
854 return(1);
855 } else if ('\0' == buf[offs])
856 return(1);
857
858 sv = offs;
859
860 /*
861 * Copy the first word into a nil-terminated buffer.
862 * Stop copying when a tab, space, or eoln is encountered.
863 */
864
865 i = 0;
866 while (i < 4 && '\0' != buf[offs] &&
867 ' ' != buf[offs] && '\t' != buf[offs])
868 mac[i++] = buf[offs++];
869
870 mac[i] = '\0';
871
872 tok = (i > 1 || i < 4) ? mdoc_hash_find(mac) : MDOC_MAX;
873
874 if (MDOC_MAX == tok) {
875 mandoc_vmsg(MANDOCERR_MACRO, mdoc->parse,
876 ln, sv, "%s", buf + sv - 1);
877 return(1);
878 }
879
880 /* Disregard the first trailing tab, if applicable. */
881
882 if ('\t' == buf[offs])
883 offs++;
884
885 /* Jump to the next non-whitespace word. */
886
887 while (buf[offs] && ' ' == buf[offs])
888 offs++;
889
890 /*
891 * Trailing whitespace. Note that tabs are allowed to be passed
892 * into the parser as "text", so we only warn about spaces here.
893 */
894
895 if ('\0' == buf[offs] && ' ' == buf[offs - 1])
896 mdoc_pmsg(mdoc, ln, offs - 1, MANDOCERR_EOLNSPACE);
897
898 /*
899 * If an initial macro or a list invocation, divert directly
900 * into macro processing.
901 */
902
903 if (NULL == mdoc->last || MDOC_It == tok || MDOC_El == tok) {
904 if ( ! mdoc_macro(mdoc, tok, ln, sv, &offs, buf))
905 goto err;
906 return(1);
907 }
908
909 n = mdoc->last;
910 assert(mdoc->last);
911
912 /*
913 * If the first macro of a `Bl -column', open an `It' block
914 * context around the parsed macro.
915 */
916
917 if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
918 LIST_column == n->norm->Bl.type) {
919 mdoc->flags |= MDOC_FREECOL;
920 if ( ! mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf))
921 goto err;
922 return(1);
923 }
924
925 /*
926 * If we're following a block-level `It' within a `Bl -column'
927 * context (perhaps opened in the above block or in ptext()),
928 * then open an `It' block context around the parsed macro.
929 */
930
931 if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
932 NULL != n->parent &&
933 MDOC_Bl == n->parent->tok &&
934 LIST_column == n->parent->norm->Bl.type) {
935 mdoc->flags |= MDOC_FREECOL;
936 if ( ! mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf))
937 goto err;
938 return(1);
939 }
940
941 /* Normal processing of a macro. */
942
943 if ( ! mdoc_macro(mdoc, tok, ln, sv, &offs, buf))
944 goto err;
945
946 return(1);
947
948 err: /* Error out. */
949
950 mdoc->flags |= MDOC_HALT;
951 return(0);
952 }
953
954 enum mdelim
955 mdoc_isdelim(const char *p)
956 {
957
958 if ('\0' == p[0])
959 return(DELIM_NONE);
960
961 if ('\0' == p[1])
962 switch (p[0]) {
963 case('('):
964 /* FALLTHROUGH */
965 case('['):
966 return(DELIM_OPEN);
967 case('|'):
968 return(DELIM_MIDDLE);
969 case('.'):
970 /* FALLTHROUGH */
971 case(','):
972 /* FALLTHROUGH */
973 case(';'):
974 /* FALLTHROUGH */
975 case(':'):
976 /* FALLTHROUGH */
977 case('?'):
978 /* FALLTHROUGH */
979 case('!'):
980 /* FALLTHROUGH */
981 case(')'):
982 /* FALLTHROUGH */
983 case(']'):
984 return(DELIM_CLOSE);
985 default:
986 return(DELIM_NONE);
987 }
988
989 if ('\\' != p[0])
990 return(DELIM_NONE);
991
992 if (0 == strcmp(p + 1, "."))
993 return(DELIM_CLOSE);
994 if (0 == strcmp(p + 1, "fR|\\fP"))
995 return(DELIM_MIDDLE);
996
997 return(DELIM_NONE);
998 }