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