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