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