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