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