]> git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
Implement the roff(7) .rr (remove register) request.
[mandoc.git] / mdoc.c
1 /* $Id: mdoc.c,v 1.212 2014/03/30 19:47:48 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2012, 2013, 2014 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 <ctype.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31
32 #include "mdoc.h"
33 #include "mandoc.h"
34 #include "mandoc_aux.h"
35 #include "libmdoc.h"
36 #include "libmandoc.h"
37
38 const char *const __mdoc_macronames[MDOC_MAX] = {
39 "Ap", "Dd", "Dt", "Os",
40 "Sh", "Ss", "Pp", "D1",
41 "Dl", "Bd", "Ed", "Bl",
42 "El", "It", "Ad", "An",
43 "Ar", "Cd", "Cm", "Dv",
44 "Er", "Ev", "Ex", "Fa",
45 "Fd", "Fl", "Fn", "Ft",
46 "Ic", "In", "Li", "Nd",
47 "Nm", "Op", "Ot", "Pa",
48 "Rv", "St", "Va", "Vt",
49 /* LINTED */
50 "Xr", "%A", "%B", "%D",
51 /* LINTED */
52 "%I", "%J", "%N", "%O",
53 /* LINTED */
54 "%P", "%R", "%T", "%V",
55 "Ac", "Ao", "Aq", "At",
56 "Bc", "Bf", "Bo", "Bq",
57 "Bsx", "Bx", "Db", "Dc",
58 "Do", "Dq", "Ec", "Ef",
59 "Em", "Eo", "Fx", "Ms",
60 "No", "Ns", "Nx", "Ox",
61 "Pc", "Pf", "Po", "Pq",
62 "Qc", "Ql", "Qo", "Qq",
63 "Re", "Rs", "Sc", "So",
64 "Sq", "Sm", "Sx", "Sy",
65 "Tn", "Ux", "Xc", "Xo",
66 "Fo", "Fc", "Oo", "Oc",
67 "Bk", "Ek", "Bt", "Hf",
68 "Fr", "Ud", "Lb", "Lp",
69 "Lk", "Mt", "Brq", "Bro",
70 /* LINTED */
71 "Brc", "%C", "Es", "En",
72 /* LINTED */
73 "Dx", "%Q", "br", "sp",
74 /* LINTED */
75 "%U", "Ta", "ll",
76 };
77
78 const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
79 "split", "nosplit", "ragged",
80 "unfilled", "literal", "file",
81 "offset", "bullet", "dash",
82 "hyphen", "item", "enum",
83 "tag", "diag", "hang",
84 "ohang", "inset", "column",
85 "width", "compact", "std",
86 "filled", "words", "emphasis",
87 "symbolic", "nested", "centered"
88 };
89
90 const char * const *mdoc_macronames = __mdoc_macronames;
91 const char * const *mdoc_argnames = __mdoc_argnames;
92
93 static void mdoc_node_free(struct mdoc_node *);
94 static void mdoc_node_unlink(struct mdoc *,
95 struct mdoc_node *);
96 static void mdoc_free1(struct mdoc *);
97 static void mdoc_alloc1(struct mdoc *);
98 static struct mdoc_node *node_alloc(struct mdoc *, int, int,
99 enum mdoct, enum mdoc_type);
100 static int node_append(struct mdoc *,
101 struct mdoc_node *);
102 #if 0
103 static int mdoc_preptext(struct mdoc *, int, char *, int);
104 #endif
105 static int mdoc_ptext(struct mdoc *, int, char *, int);
106 static int mdoc_pmacro(struct mdoc *, int, char *, int);
107
108 const struct mdoc_node *
109 mdoc_node(const struct mdoc *mdoc)
110 {
111
112 assert( ! (MDOC_HALT & mdoc->flags));
113 return(mdoc->first);
114 }
115
116
117 const struct mdoc_meta *
118 mdoc_meta(const struct mdoc *mdoc)
119 {
120
121 assert( ! (MDOC_HALT & mdoc->flags));
122 return(&mdoc->meta);
123 }
124
125
126 /*
127 * Frees volatile resources (parse tree, meta-data, fields).
128 */
129 static void
130 mdoc_free1(struct mdoc *mdoc)
131 {
132
133 if (mdoc->first)
134 mdoc_node_delete(mdoc, mdoc->first);
135 if (mdoc->meta.title)
136 free(mdoc->meta.title);
137 if (mdoc->meta.os)
138 free(mdoc->meta.os);
139 if (mdoc->meta.name)
140 free(mdoc->meta.name);
141 if (mdoc->meta.arch)
142 free(mdoc->meta.arch);
143 if (mdoc->meta.vol)
144 free(mdoc->meta.vol);
145 if (mdoc->meta.msec)
146 free(mdoc->meta.msec);
147 if (mdoc->meta.date)
148 free(mdoc->meta.date);
149 }
150
151
152 /*
153 * Allocate all volatile resources (parse tree, meta-data, fields).
154 */
155 static void
156 mdoc_alloc1(struct mdoc *mdoc)
157 {
158
159 memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
160 mdoc->flags = 0;
161 mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
162 mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
163 mdoc->first = mdoc->last;
164 mdoc->last->type = MDOC_ROOT;
165 mdoc->last->tok = MDOC_MAX;
166 mdoc->next = MDOC_NEXT_CHILD;
167 }
168
169
170 /*
171 * Free up volatile resources (see mdoc_free1()) then re-initialises the
172 * data with mdoc_alloc1(). After invocation, parse data has been reset
173 * and the parser is ready for re-invocation on a new tree; however,
174 * cross-parse non-volatile data is kept intact.
175 */
176 void
177 mdoc_reset(struct mdoc *mdoc)
178 {
179
180 mdoc_free1(mdoc);
181 mdoc_alloc1(mdoc);
182 }
183
184
185 /*
186 * Completely free up all volatile and non-volatile parse resources.
187 * After invocation, the pointer is no longer usable.
188 */
189 void
190 mdoc_free(struct mdoc *mdoc)
191 {
192
193 mdoc_free1(mdoc);
194 free(mdoc);
195 }
196
197
198 /*
199 * Allocate volatile and non-volatile parse resources.
200 */
201 struct mdoc *
202 mdoc_alloc(struct roff *roff, struct mparse *parse,
203 char *defos, int quick)
204 {
205 struct mdoc *p;
206
207 p = mandoc_calloc(1, sizeof(struct mdoc));
208
209 p->parse = parse;
210 p->defos = defos;
211 p->quick = quick;
212 p->roff = roff;
213
214 mdoc_hash_init();
215 mdoc_alloc1(p);
216 return(p);
217 }
218
219
220 /*
221 * Climb back up the parse tree, validating open scopes. Mostly calls
222 * through to macro_end() in macro.c.
223 */
224 int
225 mdoc_endparse(struct mdoc *mdoc)
226 {
227
228 assert( ! (MDOC_HALT & mdoc->flags));
229 if (mdoc_macroend(mdoc))
230 return(1);
231 mdoc->flags |= MDOC_HALT;
232 return(0);
233 }
234
235 int
236 mdoc_addeqn(struct mdoc *mdoc, const struct eqn *ep)
237 {
238 struct mdoc_node *n;
239
240 assert( ! (MDOC_HALT & mdoc->flags));
241
242 /* No text before an initial macro. */
243
244 if (SEC_NONE == mdoc->lastnamed) {
245 mdoc_pmsg(mdoc, ep->ln, ep->pos, MANDOCERR_NOTEXT);
246 return(1);
247 }
248
249 n = node_alloc(mdoc, ep->ln, ep->pos, MDOC_MAX, MDOC_EQN);
250 n->eqn = ep;
251
252 if ( ! node_append(mdoc, n))
253 return(0);
254
255 mdoc->next = MDOC_NEXT_SIBLING;
256 return(1);
257 }
258
259 int
260 mdoc_addspan(struct mdoc *mdoc, const struct tbl_span *sp)
261 {
262 struct mdoc_node *n;
263
264 assert( ! (MDOC_HALT & mdoc->flags));
265
266 /* No text before an initial macro. */
267
268 if (SEC_NONE == mdoc->lastnamed) {
269 mdoc_pmsg(mdoc, sp->line, 0, MANDOCERR_NOTEXT);
270 return(1);
271 }
272
273 n = node_alloc(mdoc, sp->line, 0, MDOC_MAX, MDOC_TBL);
274 n->span = sp;
275
276 if ( ! node_append(mdoc, n))
277 return(0);
278
279 mdoc->next = MDOC_NEXT_SIBLING;
280 return(1);
281 }
282
283
284 /*
285 * Main parse routine. Parses a single line -- really just hands off to
286 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
287 */
288 int
289 mdoc_parseln(struct mdoc *mdoc, int ln, char *buf, int offs)
290 {
291
292 assert( ! (MDOC_HALT & mdoc->flags));
293
294 mdoc->flags |= MDOC_NEWLINE;
295
296 /*
297 * Let the roff nS register switch SYNOPSIS mode early,
298 * such that the parser knows at all times
299 * whether this mode is on or off.
300 * Note that this mode is also switched by the Sh macro.
301 */
302 if (roff_getreg(mdoc->roff, "nS"))
303 mdoc->flags |= MDOC_SYNOPSIS;
304 else
305 mdoc->flags &= ~MDOC_SYNOPSIS;
306
307 return(roff_getcontrol(mdoc->roff, buf, &offs) ?
308 mdoc_pmacro(mdoc, ln, buf, offs) :
309 mdoc_ptext(mdoc, ln, buf, offs));
310 }
311
312 int
313 mdoc_macro(MACRO_PROT_ARGS)
314 {
315 assert(tok < MDOC_MAX);
316
317 /* If we're in the body, deny prologue calls. */
318
319 if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
320 MDOC_PBODY & mdoc->flags) {
321 mdoc_pmsg(mdoc, line, ppos, MANDOCERR_BADBODY);
322 return(1);
323 }
324
325 /* If we're in the prologue, deny "body" macros. */
326
327 if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
328 ! (MDOC_PBODY & mdoc->flags)) {
329 mdoc_pmsg(mdoc, line, ppos, MANDOCERR_BADPROLOG);
330 if (NULL == mdoc->meta.msec)
331 mdoc->meta.msec = mandoc_strdup("1");
332 if (NULL == mdoc->meta.title)
333 mdoc->meta.title = mandoc_strdup("UNKNOWN");
334 if (NULL == mdoc->meta.vol)
335 mdoc->meta.vol = mandoc_strdup("LOCAL");
336 if (NULL == mdoc->meta.os)
337 mdoc->meta.os = mandoc_strdup("LOCAL");
338 if (NULL == mdoc->meta.date)
339 mdoc->meta.date = mandoc_normdate
340 (mdoc->parse, NULL, line, ppos);
341 mdoc->flags |= MDOC_PBODY;
342 }
343
344 return((*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf));
345 }
346
347
348 static int
349 node_append(struct mdoc *mdoc, struct mdoc_node *p)
350 {
351
352 assert(mdoc->last);
353 assert(mdoc->first);
354 assert(MDOC_ROOT != p->type);
355
356 switch (mdoc->next) {
357 case (MDOC_NEXT_SIBLING):
358 mdoc->last->next = p;
359 p->prev = mdoc->last;
360 p->parent = mdoc->last->parent;
361 break;
362 case (MDOC_NEXT_CHILD):
363 mdoc->last->child = p;
364 p->parent = mdoc->last;
365 break;
366 default:
367 abort();
368 /* NOTREACHED */
369 }
370
371 p->parent->nchild++;
372
373 /*
374 * Copy over the normalised-data pointer of our parent. Not
375 * everybody has one, but copying a null pointer is fine.
376 */
377
378 switch (p->type) {
379 case (MDOC_BODY):
380 if (ENDBODY_NOT != p->end)
381 break;
382 /* FALLTHROUGH */
383 case (MDOC_TAIL):
384 /* FALLTHROUGH */
385 case (MDOC_HEAD):
386 p->norm = p->parent->norm;
387 break;
388 default:
389 break;
390 }
391
392 if ( ! mdoc_valid_pre(mdoc, p))
393 return(0);
394
395 switch (p->type) {
396 case (MDOC_HEAD):
397 assert(MDOC_BLOCK == p->parent->type);
398 p->parent->head = p;
399 break;
400 case (MDOC_TAIL):
401 assert(MDOC_BLOCK == p->parent->type);
402 p->parent->tail = p;
403 break;
404 case (MDOC_BODY):
405 if (p->end)
406 break;
407 assert(MDOC_BLOCK == p->parent->type);
408 p->parent->body = p;
409 break;
410 default:
411 break;
412 }
413
414 mdoc->last = p;
415
416 switch (p->type) {
417 case (MDOC_TBL):
418 /* FALLTHROUGH */
419 case (MDOC_TEXT):
420 if ( ! mdoc_valid_post(mdoc))
421 return(0);
422 break;
423 default:
424 break;
425 }
426
427 return(1);
428 }
429
430
431 static struct mdoc_node *
432 node_alloc(struct mdoc *mdoc, int line, int pos,
433 enum mdoct tok, enum mdoc_type type)
434 {
435 struct mdoc_node *p;
436
437 p = mandoc_calloc(1, sizeof(struct mdoc_node));
438 p->sec = mdoc->lastsec;
439 p->line = line;
440 p->pos = pos;
441 p->lastline = line;
442 p->tok = tok;
443 p->type = type;
444
445 /* Flag analysis. */
446
447 if (MDOC_SYNOPSIS & mdoc->flags)
448 p->flags |= MDOC_SYNPRETTY;
449 else
450 p->flags &= ~MDOC_SYNPRETTY;
451 if (MDOC_NEWLINE & mdoc->flags)
452 p->flags |= MDOC_LINE;
453 mdoc->flags &= ~MDOC_NEWLINE;
454
455 return(p);
456 }
457
458
459 int
460 mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok)
461 {
462 struct mdoc_node *p;
463
464 p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL);
465 if ( ! node_append(mdoc, p))
466 return(0);
467 mdoc->next = MDOC_NEXT_CHILD;
468 return(1);
469 }
470
471
472 int
473 mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok)
474 {
475 struct mdoc_node *p;
476
477 assert(mdoc->first);
478 assert(mdoc->last);
479
480 p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD);
481 if ( ! node_append(mdoc, p))
482 return(0);
483 mdoc->next = MDOC_NEXT_CHILD;
484 return(1);
485 }
486
487
488 int
489 mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok)
490 {
491 struct mdoc_node *p;
492
493 p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
494 if ( ! node_append(mdoc, p))
495 return(0);
496 mdoc->next = MDOC_NEXT_CHILD;
497 return(1);
498 }
499
500
501 int
502 mdoc_endbody_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok,
503 struct mdoc_node *body, enum mdoc_endbody end)
504 {
505 struct mdoc_node *p;
506
507 p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
508 p->pending = body;
509 p->norm = body->norm;
510 p->end = end;
511 if ( ! node_append(mdoc, p))
512 return(0);
513 mdoc->next = MDOC_NEXT_SIBLING;
514 return(1);
515 }
516
517
518 int
519 mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
520 enum mdoct tok, struct mdoc_arg *args)
521 {
522 struct mdoc_node *p;
523
524 p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK);
525 p->args = args;
526 if (p->args)
527 (args->refcnt)++;
528
529 switch (tok) {
530 case (MDOC_Bd):
531 /* FALLTHROUGH */
532 case (MDOC_Bf):
533 /* FALLTHROUGH */
534 case (MDOC_Bl):
535 /* FALLTHROUGH */
536 case (MDOC_Rs):
537 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
538 break;
539 default:
540 break;
541 }
542
543 if ( ! node_append(mdoc, p))
544 return(0);
545 mdoc->next = MDOC_NEXT_CHILD;
546 return(1);
547 }
548
549
550 int
551 mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
552 enum mdoct tok, struct mdoc_arg *args)
553 {
554 struct mdoc_node *p;
555
556 p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM);
557 p->args = args;
558 if (p->args)
559 (args->refcnt)++;
560
561 switch (tok) {
562 case (MDOC_An):
563 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
564 break;
565 default:
566 break;
567 }
568
569 if ( ! node_append(mdoc, p))
570 return(0);
571 mdoc->next = MDOC_NEXT_CHILD;
572 return(1);
573 }
574
575 int
576 mdoc_word_alloc(struct mdoc *mdoc, int line, int pos, const char *p)
577 {
578 struct mdoc_node *n;
579
580 n = node_alloc(mdoc, line, pos, MDOC_MAX, MDOC_TEXT);
581 n->string = roff_strdup(mdoc->roff, p);
582
583 if ( ! node_append(mdoc, n))
584 return(0);
585
586 mdoc->next = MDOC_NEXT_SIBLING;
587 return(1);
588 }
589
590 void
591 mdoc_word_append(struct mdoc *mdoc, const char *p)
592 {
593 struct mdoc_node *n;
594 char *addstr, *newstr;
595
596 n = mdoc->last;
597 addstr = roff_strdup(mdoc->roff, p);
598 mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
599 free(addstr);
600 free(n->string);
601 n->string = newstr;
602 mdoc->next = MDOC_NEXT_SIBLING;
603 }
604
605 static void
606 mdoc_node_free(struct mdoc_node *p)
607 {
608
609 if (MDOC_BLOCK == p->type || MDOC_ELEM == p->type)
610 free(p->norm);
611 if (p->string)
612 free(p->string);
613 if (p->args)
614 mdoc_argv_free(p->args);
615 free(p);
616 }
617
618
619 static void
620 mdoc_node_unlink(struct mdoc *mdoc, struct mdoc_node *n)
621 {
622
623 /* Adjust siblings. */
624
625 if (n->prev)
626 n->prev->next = n->next;
627 if (n->next)
628 n->next->prev = n->prev;
629
630 /* Adjust parent. */
631
632 if (n->parent) {
633 n->parent->nchild--;
634 if (n->parent->child == n)
635 n->parent->child = n->prev ? n->prev : n->next;
636 if (n->parent->last == n)
637 n->parent->last = n->prev ? n->prev : NULL;
638 }
639
640 /* Adjust parse point, if applicable. */
641
642 if (mdoc && mdoc->last == n) {
643 if (n->prev) {
644 mdoc->last = n->prev;
645 mdoc->next = MDOC_NEXT_SIBLING;
646 } else {
647 mdoc->last = n->parent;
648 mdoc->next = MDOC_NEXT_CHILD;
649 }
650 }
651
652 if (mdoc && mdoc->first == n)
653 mdoc->first = NULL;
654 }
655
656
657 void
658 mdoc_node_delete(struct mdoc *mdoc, struct mdoc_node *p)
659 {
660
661 while (p->child) {
662 assert(p->nchild);
663 mdoc_node_delete(mdoc, p->child);
664 }
665 assert(0 == p->nchild);
666
667 mdoc_node_unlink(mdoc, p);
668 mdoc_node_free(p);
669 }
670
671 int
672 mdoc_node_relink(struct mdoc *mdoc, struct mdoc_node *p)
673 {
674
675 mdoc_node_unlink(mdoc, p);
676 return(node_append(mdoc, p));
677 }
678
679 #if 0
680 /*
681 * Pre-treat a text line.
682 * Text lines can consist of equations, which must be handled apart from
683 * the regular text.
684 * Thus, use this function to step through a line checking if it has any
685 * equations embedded in it.
686 * This must handle multiple equations AND equations that do not end at
687 * the end-of-line, i.e., will re-enter in the next roff parse.
688 */
689 static int
690 mdoc_preptext(struct mdoc *mdoc, int line, char *buf, int offs)
691 {
692 char *start, *end;
693 char delim;
694
695 while ('\0' != buf[offs]) {
696 /* Mark starting position if eqn is set. */
697 start = NULL;
698 if ('\0' != (delim = roff_eqndelim(mdoc->roff)))
699 if (NULL != (start = strchr(buf + offs, delim)))
700 *start++ = '\0';
701
702 /* Parse text as normal. */
703 if ( ! mdoc_ptext(mdoc, line, buf, offs))
704 return(0);
705
706 /* Continue only if an equation exists. */
707 if (NULL == start)
708 break;
709
710 /* Read past the end of the equation. */
711 offs += start - (buf + offs);
712 assert(start == &buf[offs]);
713 if (NULL != (end = strchr(buf + offs, delim))) {
714 *end++ = '\0';
715 while (' ' == *end)
716 end++;
717 }
718
719 /* Parse the equation itself. */
720 roff_openeqn(mdoc->roff, NULL, line, offs, buf);
721
722 /* Process a finished equation? */
723 if (roff_closeeqn(mdoc->roff))
724 if ( ! mdoc_addeqn(mdoc, roff_eqn(mdoc->roff)))
725 return(0);
726 offs += (end - (buf + offs));
727 }
728
729 return(1);
730 }
731 #endif
732
733 /*
734 * Parse free-form text, that is, a line that does not begin with the
735 * control character.
736 */
737 static int
738 mdoc_ptext(struct mdoc *mdoc, int line, char *buf, int offs)
739 {
740 char *c, *ws, *end;
741 struct mdoc_node *n;
742
743 /* No text before an initial macro. */
744
745 if (SEC_NONE == mdoc->lastnamed) {
746 mdoc_pmsg(mdoc, line, offs, MANDOCERR_NOTEXT);
747 return(1);
748 }
749
750 assert(mdoc->last);
751 n = mdoc->last;
752
753 /*
754 * Divert directly to list processing if we're encountering a
755 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
756 * (a MDOC_BODY means it's already open, in which case we should
757 * process within its context in the normal way).
758 */
759
760 if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
761 LIST_column == n->norm->Bl.type) {
762 /* `Bl' is open without any children. */
763 mdoc->flags |= MDOC_FREECOL;
764 return(mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf));
765 }
766
767 if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
768 NULL != n->parent &&
769 MDOC_Bl == n->parent->tok &&
770 LIST_column == n->parent->norm->Bl.type) {
771 /* `Bl' has block-level `It' children. */
772 mdoc->flags |= MDOC_FREECOL;
773 return(mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf));
774 }
775
776 /*
777 * Search for the beginning of unescaped trailing whitespace (ws)
778 * and for the first character not to be output (end).
779 */
780
781 /* FIXME: replace with strcspn(). */
782 ws = NULL;
783 for (c = end = buf + offs; *c; c++) {
784 switch (*c) {
785 case ' ':
786 if (NULL == ws)
787 ws = c;
788 continue;
789 case '\t':
790 /*
791 * Always warn about trailing tabs,
792 * even outside literal context,
793 * where they should be put on the next line.
794 */
795 if (NULL == ws)
796 ws = c;
797 /*
798 * Strip trailing tabs in literal context only;
799 * outside, they affect the next line.
800 */
801 if (MDOC_LITERAL & mdoc->flags)
802 continue;
803 break;
804 case '\\':
805 /* Skip the escaped character, too, if any. */
806 if (c[1])
807 c++;
808 /* FALLTHROUGH */
809 default:
810 ws = NULL;
811 break;
812 }
813 end = c + 1;
814 }
815 *end = '\0';
816
817 if (ws)
818 mdoc_pmsg(mdoc, line, (int)(ws-buf), MANDOCERR_EOLNSPACE);
819
820 if ('\0' == buf[offs] && ! (MDOC_LITERAL & mdoc->flags)) {
821 mdoc_pmsg(mdoc, line, (int)(c-buf), MANDOCERR_NOBLANKLN);
822
823 /*
824 * Insert a `sp' in the case of a blank line. Technically,
825 * blank lines aren't allowed, but enough manuals assume this
826 * behaviour that we want to work around it.
827 */
828 if ( ! mdoc_elem_alloc(mdoc, line, offs, MDOC_sp, NULL))
829 return(0);
830
831 mdoc->next = MDOC_NEXT_SIBLING;
832
833 return(mdoc_valid_post(mdoc));
834 }
835
836 if ( ! mdoc_word_alloc(mdoc, line, offs, buf+offs))
837 return(0);
838
839 if (MDOC_LITERAL & mdoc->flags)
840 return(1);
841
842 /*
843 * End-of-sentence check. If the last character is an unescaped
844 * EOS character, then flag the node as being the end of a
845 * sentence. The front-end will know how to interpret this.
846 */
847
848 assert(buf < end);
849
850 if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
851 mdoc->last->flags |= MDOC_EOS;
852
853 return(1);
854 }
855
856
857 /*
858 * Parse a macro line, that is, a line beginning with the control
859 * character.
860 */
861 static int
862 mdoc_pmacro(struct mdoc *mdoc, int ln, char *buf, int offs)
863 {
864 enum mdoct tok;
865 int i, sv;
866 char mac[5];
867 struct mdoc_node *n;
868
869 /* Empty post-control lines are ignored. */
870
871 if ('"' == buf[offs]) {
872 mdoc_pmsg(mdoc, ln, offs, MANDOCERR_BADCOMMENT);
873 return(1);
874 } else if ('\0' == buf[offs])
875 return(1);
876
877 sv = offs;
878
879 /*
880 * Copy the first word into a nil-terminated buffer.
881 * Stop copying when a tab, space, or eoln is encountered.
882 */
883
884 i = 0;
885 while (i < 4 && '\0' != buf[offs] &&
886 ' ' != buf[offs] && '\t' != buf[offs])
887 mac[i++] = buf[offs++];
888
889 mac[i] = '\0';
890
891 tok = (i > 1 || i < 4) ? mdoc_hash_find(mac) : MDOC_MAX;
892
893 if (MDOC_MAX == tok) {
894 mandoc_vmsg(MANDOCERR_MACRO, mdoc->parse,
895 ln, sv, "%s", buf + sv - 1);
896 return(1);
897 }
898
899 /* Disregard the first trailing tab, if applicable. */
900
901 if ('\t' == buf[offs])
902 offs++;
903
904 /* Jump to the next non-whitespace word. */
905
906 while (buf[offs] && ' ' == buf[offs])
907 offs++;
908
909 /*
910 * Trailing whitespace. Note that tabs are allowed to be passed
911 * into the parser as "text", so we only warn about spaces here.
912 */
913
914 if ('\0' == buf[offs] && ' ' == buf[offs - 1])
915 mdoc_pmsg(mdoc, ln, offs - 1, MANDOCERR_EOLNSPACE);
916
917 /*
918 * If an initial macro or a list invocation, divert directly
919 * into macro processing.
920 */
921
922 if (NULL == mdoc->last || MDOC_It == tok || MDOC_El == tok) {
923 if ( ! mdoc_macro(mdoc, tok, ln, sv, &offs, buf))
924 goto err;
925 return(1);
926 }
927
928 n = mdoc->last;
929 assert(mdoc->last);
930
931 /*
932 * If the first macro of a `Bl -column', open an `It' block
933 * context around the parsed macro.
934 */
935
936 if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
937 LIST_column == n->norm->Bl.type) {
938 mdoc->flags |= MDOC_FREECOL;
939 if ( ! mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf))
940 goto err;
941 return(1);
942 }
943
944 /*
945 * If we're following a block-level `It' within a `Bl -column'
946 * context (perhaps opened in the above block or in ptext()),
947 * then open an `It' block context around the parsed macro.
948 */
949
950 if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
951 NULL != n->parent &&
952 MDOC_Bl == n->parent->tok &&
953 LIST_column == n->parent->norm->Bl.type) {
954 mdoc->flags |= MDOC_FREECOL;
955 if ( ! mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf))
956 goto err;
957 return(1);
958 }
959
960 /* Normal processing of a macro. */
961
962 if ( ! mdoc_macro(mdoc, tok, ln, sv, &offs, buf))
963 goto err;
964
965 /* In quick mode (for mandocdb), abort after the NAME section. */
966
967 if (mdoc->quick && MDOC_Sh == tok &&
968 SEC_NAME != mdoc->last->sec)
969 return(2);
970
971 return(1);
972
973 err: /* Error out. */
974
975 mdoc->flags |= MDOC_HALT;
976 return(0);
977 }
978
979 enum mdelim
980 mdoc_isdelim(const char *p)
981 {
982
983 if ('\0' == p[0])
984 return(DELIM_NONE);
985
986 if ('\0' == p[1])
987 switch (p[0]) {
988 case('('):
989 /* FALLTHROUGH */
990 case('['):
991 return(DELIM_OPEN);
992 case('|'):
993 return(DELIM_MIDDLE);
994 case('.'):
995 /* FALLTHROUGH */
996 case(','):
997 /* FALLTHROUGH */
998 case(';'):
999 /* FALLTHROUGH */
1000 case(':'):
1001 /* FALLTHROUGH */
1002 case('?'):
1003 /* FALLTHROUGH */
1004 case('!'):
1005 /* FALLTHROUGH */
1006 case(')'):
1007 /* FALLTHROUGH */
1008 case(']'):
1009 return(DELIM_CLOSE);
1010 default:
1011 return(DELIM_NONE);
1012 }
1013
1014 if ('\\' != p[0])
1015 return(DELIM_NONE);
1016
1017 if (0 == strcmp(p + 1, "."))
1018 return(DELIM_CLOSE);
1019 if (0 == strcmp(p + 1, "fR|\\fP"))
1020 return(DELIM_MIDDLE);
1021
1022 return(DELIM_NONE);
1023 }
1024
1025 void
1026 mdoc_deroff(char **dest, const struct mdoc_node *n)
1027 {
1028 char *cp;
1029 size_t sz;
1030
1031 if (MDOC_TEXT != n->type) {
1032 for (n = n->child; n; n = n->next)
1033 mdoc_deroff(dest, n);
1034 return;
1035 }
1036
1037 /* Skip leading whitespace. */
1038
1039 for (cp = n->string; '\0' != *cp; cp++)
1040 if (0 == isspace((unsigned char)*cp))
1041 break;
1042
1043 /* Skip trailing whitespace. */
1044
1045 for (sz = strlen(cp); sz; sz--)
1046 if (0 == isspace((unsigned char)cp[sz-1]))
1047 break;
1048
1049 /* Skip empty strings. */
1050
1051 if (0 == sz)
1052 return;
1053
1054 if (NULL == *dest) {
1055 *dest = mandoc_strndup(cp, sz);
1056 return;
1057 }
1058
1059 mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
1060 free(*dest);
1061 *dest = cp;
1062 }