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