]> git.cameronkatri.com Git - mandoc.git/blob - roff.c
*** empty log message ***
[mandoc.git] / roff.c
1 /* $Id: roff.c,v 1.49 2008/12/07 16:41:04 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19 #include <sys/param.h>
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <err.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include "libmdocml.h"
32 #include "private.h"
33 #include "roff.h"
34
35 /* FIXME: First letters of quoted-text interpreted in rofffindtok. */
36 /* FIXME: `No' not implemented. */
37 /* TODO: warn if Pp occurs before/after Sh etc. (see mdoc.samples). */
38 /* TODO: warn about "X section only" macros. */
39 /* TODO: warn about empty lists. */
40 /* TODO: (warn) some sections need specific elements. */
41 /* TODO: (warn) NAME section has particular order. */
42 /* TODO: unify empty-content tags a la <br />. */
43 /* TODO: macros with a set number of arguments? */
44 /* TODO: validate Dt macro arguments. */
45 /* FIXME: Bl -diag supposed to ignore callable children. */
46 /* FIXME: Nm has newline when used in NAME section. */
47
48 struct roffnode {
49 int tok; /* Token id. */
50 struct roffnode *parent; /* Parent (or NULL). */
51 };
52
53 struct rofftree {
54 struct roffnode *last; /* Last parsed node. */
55 char *cur; /* Line start. */
56 struct tm tm; /* `Dd' results. */
57 char name[64]; /* `Nm' results. */
58 char os[64]; /* `Os' results. */
59 char title[64]; /* `Dt' results. */
60 char section[64]; /* `Dt' results. */
61 char volume[64]; /* `Dt' results. */
62 int state;
63 #define ROFF_PRELUDE (1 << 1) /* In roff prelude. */
64 #define ROFF_PRELUDE_Os (1 << 2) /* `Os' is parsed. */
65 #define ROFF_PRELUDE_Dt (1 << 3) /* `Dt' is parsed. */
66 #define ROFF_PRELUDE_Dd (1 << 4) /* `Dd' is parsed. */
67 #define ROFF_BODY (1 << 5) /* In roff body. */
68 struct roffcb cb; /* Callbacks. */
69 void *arg; /* Callbacks' arg. */
70 };
71
72 static struct roffnode *roffnode_new(int, struct rofftree *);
73 static void roffnode_free(struct rofftree *);
74 static void roff_warn(const struct rofftree *,
75 const char *, char *, ...);
76 static void roff_err(const struct rofftree *,
77 const char *, char *, ...);
78 static int roffpurgepunct(struct rofftree *, char **);
79 static int roffscan(int, const int *);
80 static int rofffindtok(const char *);
81 static int rofffindarg(const char *);
82 static int rofffindcallable(const char *);
83 static int roffargs(const struct rofftree *,
84 int, char *, char **);
85 static int roffargok(int, int);
86 static int roffnextopt(const struct rofftree *,
87 int, char ***, char **);
88 static int roffparseopts(struct rofftree *, int,
89 char ***, int *, char **);
90 static int roffcall(struct rofftree *, int, char **);
91 static int roffparse(struct rofftree *, char *);
92 static int textparse(struct rofftree *, char *);
93 static int roffdata(struct rofftree *, int, char *);
94 static int roffspecial(struct rofftree *, int,
95 const char *, const int *,
96 const char **, size_t, char **);
97 static int roffsetname(struct rofftree *, char **);
98
99 #ifdef __linux__
100 extern size_t strlcat(char *, const char *, size_t);
101 extern size_t strlcpy(char *, const char *, size_t);
102 extern int vsnprintf(char *, size_t,
103 const char *, va_list);
104 extern char *strptime(const char *, const char *,
105 struct tm *);
106 #endif
107
108 int
109 roff_free(struct rofftree *tree, int flush)
110 {
111 int error, t;
112 struct roffnode *n;
113
114 error = 0;
115
116 if ( ! flush)
117 goto end;
118
119 error = 1;
120
121 if (ROFF_PRELUDE & tree->state) {
122 roff_err(tree, NULL, "prelude never finished");
123 goto end;
124 }
125
126 for (n = tree->last; n; n = n->parent) {
127 if (0 != tokens[n->tok].ctx)
128 continue;
129 roff_err(tree, NULL, "closing explicit scope `%s'",
130 toknames[n->tok]);
131 goto end;
132 }
133
134 while (tree->last) {
135 t = tree->last->tok;
136 if ( ! (*tokens[t].cb)(t, tree, NULL, ROFF_EXIT))
137 goto end;
138 }
139
140 if ( ! (*tree->cb.rofftail)(tree->arg))
141 goto end;
142
143 error = 0;
144
145 end:
146
147 while (tree->last)
148 roffnode_free(tree);
149
150 free(tree);
151
152 return(error ? 0 : 1);
153 }
154
155
156 struct rofftree *
157 roff_alloc(const struct roffcb *cb, void *args)
158 {
159 struct rofftree *tree;
160
161 assert(args);
162 assert(cb);
163
164 if (NULL == (tree = calloc(1, sizeof(struct rofftree))))
165 err(1, "calloc");
166
167 tree->state = ROFF_PRELUDE;
168 tree->arg = args;
169
170 (void)memcpy(&tree->cb, cb, sizeof(struct roffcb));
171
172 return(tree);
173 }
174
175
176 int
177 roff_engine(struct rofftree *tree, char *buf)
178 {
179
180 tree->cur = buf;
181 assert(buf);
182
183 if (0 == *buf) {
184 roff_err(tree, buf, "blank line");
185 return(0);
186 } else if ('.' != *buf)
187 return(textparse(tree, buf));
188
189 return(roffparse(tree, buf));
190 }
191
192
193 static int
194 textparse(struct rofftree *tree, char *buf)
195 {
196 char *bufp;
197
198 /* TODO: literal parsing. */
199
200 if ( ! (ROFF_BODY & tree->state)) {
201 roff_err(tree, buf, "data not in body");
202 return(0);
203 }
204
205 /* LINTED */
206 while (*buf) {
207 while (*buf && isspace(*buf))
208 buf++;
209
210 if (0 == *buf)
211 break;
212
213 bufp = buf++;
214
215 while (*buf && ! isspace(*buf))
216 buf++;
217
218 if (0 != *buf) {
219 *buf++ = 0;
220 if ( ! roffdata(tree, 1, bufp))
221 return(0);
222 continue;
223 }
224
225 if ( ! roffdata(tree, 1, bufp))
226 return(0);
227 break;
228 }
229
230 return(1);
231 }
232
233
234 static int
235 roffargs(const struct rofftree *tree,
236 int tok, char *buf, char **argv)
237 {
238 int i;
239 char *p;
240
241 assert(tok >= 0 && tok < ROFF_MAX);
242 assert('.' == *buf);
243
244 p = buf;
245
246 /*
247 * This is an ugly little loop. It parses a line into
248 * space-delimited tokens. If a quote mark is encountered, a
249 * token is alloted the entire quoted text. If whitespace is
250 * escaped, it's included in the prior alloted token.
251 */
252
253 /* LINTED */
254 for (i = 0; *buf && i < ROFF_MAXLINEARG; i++) {
255 if ('\"' == *buf) {
256 argv[i] = ++buf;
257 while (*buf && '\"' != *buf)
258 buf++;
259 if (0 == *buf) {
260 roff_err(tree, argv[i], "unclosed "
261 "quote in argument "
262 "list for `%s'",
263 toknames[tok]);
264 return(0);
265 }
266 } else {
267 argv[i] = buf++;
268 while (*buf) {
269 if ( ! isspace(*buf)) {
270 buf++;
271 continue;
272 }
273 if (*(buf - 1) == '\\') {
274 buf++;
275 continue;
276 }
277 break;
278 }
279 if (0 == *buf)
280 continue;
281 }
282 *buf++ = 0;
283 while (*buf && isspace(*buf))
284 buf++;
285 }
286
287 assert(i > 0);
288 if (ROFF_MAXLINEARG == i && *buf) {
289 roff_err(tree, p, "too many arguments for `%s'", toknames
290 [tok]);
291 return(0);
292 }
293
294 argv[i] = NULL;
295 return(1);
296 }
297
298
299 static int
300 roffscan(int tok, const int *tokv)
301 {
302
303 if (NULL == tokv)
304 return(1);
305
306 for ( ; ROFF_MAX != *tokv; tokv++)
307 if (tok == *tokv)
308 return(1);
309
310 return(0);
311 }
312
313
314 static int
315 roffparse(struct rofftree *tree, char *buf)
316 {
317 int tok, t;
318 struct roffnode *n;
319 char *argv[ROFF_MAXLINEARG];
320 char **argvp;
321
322 if (0 != *buf && 0 != *(buf + 1) && 0 != *(buf + 2))
323 if (0 == strncmp(buf, ".\\\"", 3))
324 return(1);
325
326 if (ROFF_MAX == (tok = rofffindtok(buf + 1))) {
327 roff_err(tree, buf + 1, "bogus line macro");
328 return(0);
329 } else if (NULL == tokens[tok].cb) {
330 roff_err(tree, buf + 1, "unsupported macro `%s'",
331 toknames[tok]);
332 return(0);
333 }
334
335 assert(ROFF___ != tok);
336 if ( ! roffargs(tree, tok, buf, argv))
337 return(0);
338
339 argvp = (char **)argv;
340
341 /*
342 * Prelude macros break some assumptions, so branch now.
343 */
344
345 if (ROFF_PRELUDE & tree->state) {
346 assert(NULL == tree->last);
347 return((*tokens[tok].cb)(tok, tree, argvp, ROFF_ENTER));
348 }
349
350 assert(ROFF_BODY & tree->state);
351
352 /*
353 * First check that our possible parents and parent's possible
354 * children are satisfied.
355 */
356
357 if (tree->last && ! roffscan
358 (tree->last->tok, tokens[tok].parents)) {
359 roff_err(tree, *argvp, "`%s' has invalid parent `%s'",
360 toknames[tok],
361 toknames[tree->last->tok]);
362 return(0);
363 }
364
365 if (tree->last && ! roffscan
366 (tok, tokens[tree->last->tok].children)) {
367 roff_err(tree, *argvp, "`%s' is invalid child of `%s'",
368 toknames[tok],
369 toknames[tree->last->tok]);
370 return(0);
371 }
372
373 /*
374 * Branch if we're not a layout token.
375 */
376
377 if (ROFF_LAYOUT != tokens[tok].type)
378 return((*tokens[tok].cb)(tok, tree, argvp, ROFF_ENTER));
379 if (0 == tokens[tok].ctx)
380 return((*tokens[tok].cb)(tok, tree, argvp, ROFF_ENTER));
381
382 /*
383 * First consider implicit-end tags, like as follows:
384 * .Sh SECTION 1
385 * .Sh SECTION 2
386 * In this, we want to close the scope of the NAME section. If
387 * there's an intermediary implicit-end tag, such as
388 * .Sh SECTION 1
389 * .Ss Subsection 1
390 * .Sh SECTION 2
391 * then it must be closed as well.
392 */
393
394 if (tok == tokens[tok].ctx) {
395 /*
396 * First search up to the point where we must close.
397 * If one doesn't exist, then we can open a new scope.
398 */
399
400 for (n = tree->last; n; n = n->parent) {
401 assert(0 == tokens[n->tok].ctx ||
402 n->tok == tokens[n->tok].ctx);
403 if (n->tok == tok)
404 break;
405 if (ROFF_SHALLOW & tokens[tok].flags) {
406 n = NULL;
407 break;
408 }
409 if (tokens[n->tok].ctx == n->tok)
410 continue;
411 roff_err(tree, *argv, "`%s' breaks `%s' scope",
412 toknames[tok], toknames[n->tok]);
413 return(0);
414 }
415
416 /*
417 * Create a new scope, as no previous one exists to
418 * close out.
419 */
420
421 if (NULL == n)
422 return((*tokens[tok].cb)(tok, tree, argvp, ROFF_ENTER));
423
424 /*
425 * Close out all intermediary scoped blocks, then hang
426 * the current scope from our predecessor's parent.
427 */
428
429 do {
430 t = tree->last->tok;
431 if ( ! (*tokens[t].cb)(t, tree, NULL, ROFF_EXIT))
432 return(0);
433 } while (t != tok);
434
435 return((*tokens[tok].cb)(tok, tree, argvp, ROFF_ENTER));
436 }
437
438 /*
439 * Now consider explicit-end tags, where we want to close back
440 * to a specific tag. Example:
441 * .Bl
442 * .It Item.
443 * .El
444 * In this, the `El' tag closes out the scope of `Bl'.
445 */
446
447 assert(tok != tokens[tok].ctx && 0 != tokens[tok].ctx);
448
449 /* LINTED */
450 for (n = tree->last; n; n = n->parent)
451 if (n->tok != tokens[tok].ctx) {
452 if (n->tok == tokens[n->tok].ctx)
453 continue;
454 roff_err(tree, *argv, "`%s' breaks `%s' scope",
455 toknames[tok], toknames[n->tok]);
456 return(0);
457 } else
458 break;
459
460
461 if (NULL == n) {
462 roff_err(tree, *argv, "`%s' has no starting tag `%s'",
463 toknames[tok],
464 toknames[tokens[tok].ctx]);
465 return(0);
466 }
467
468 /* LINTED */
469 do {
470 t = tree->last->tok;
471 if ( ! (*tokens[t].cb)(t, tree, NULL, ROFF_EXIT))
472 return(0);
473 } while (t != tokens[tok].ctx);
474
475 return(1);
476 }
477
478
479 static int
480 rofffindarg(const char *name)
481 {
482 size_t i;
483
484 /* FIXME: use a table, this is slow but ok for now. */
485
486 /* LINTED */
487 for (i = 0; i < ROFF_ARGMAX; i++)
488 /* LINTED */
489 if (0 == strcmp(name, tokargnames[i]))
490 return((int)i);
491
492 return(ROFF_ARGMAX);
493 }
494
495
496 static int
497 rofffindtok(const char *buf)
498 {
499 char token[4];
500 int i;
501
502 for (i = 0; *buf && ! isspace(*buf) && i < 3; i++, buf++)
503 token[i] = *buf;
504
505 if (i == 3)
506 return(ROFF_MAX);
507
508 token[i] = 0;
509
510 /* FIXME: use a table, this is slow but ok for now. */
511
512 /* LINTED */
513 for (i = 0; i < ROFF_MAX; i++)
514 /* LINTED */
515 if (0 == strcmp(toknames[i], token))
516 return((int)i);
517
518 return(ROFF_MAX);
519 }
520
521
522 static int
523 roffispunct(const char *p)
524 {
525
526 if (0 == *p)
527 return(0);
528 if (0 != *(p + 1))
529 return(0);
530
531 switch (*p) {
532 case('{'):
533 /* FALLTHROUGH */
534 case('.'):
535 /* FALLTHROUGH */
536 case(','):
537 /* FALLTHROUGH */
538 case(';'):
539 /* FALLTHROUGH */
540 case(':'):
541 /* FALLTHROUGH */
542 case('?'):
543 /* FALLTHROUGH */
544 case('!'):
545 /* FALLTHROUGH */
546 case('('):
547 /* FALLTHROUGH */
548 case(')'):
549 /* FALLTHROUGH */
550 case('['):
551 /* FALLTHROUGH */
552 case(']'):
553 /* FALLTHROUGH */
554 case('}'):
555 return(1);
556 default:
557 break;
558 }
559
560 return(0);
561 }
562
563
564 static int
565 rofffindcallable(const char *name)
566 {
567 int c;
568
569 if (ROFF_MAX == (c = rofffindtok(name)))
570 return(ROFF_MAX);
571 assert(c >= 0 && c < ROFF_MAX);
572 return(ROFF_CALLABLE & tokens[c].flags ? c : ROFF_MAX);
573 }
574
575
576 static struct roffnode *
577 roffnode_new(int tokid, struct rofftree *tree)
578 {
579 struct roffnode *p;
580
581 if (NULL == (p = malloc(sizeof(struct roffnode))))
582 err(1, "malloc");
583
584 p->tok = tokid;
585 p->parent = tree->last;
586 tree->last = p;
587
588 return(p);
589 }
590
591
592 static int
593 roffargok(int tokid, int argid)
594 {
595 const int *c;
596
597 if (NULL == (c = tokens[tokid].args))
598 return(0);
599
600 for ( ; ROFF_ARGMAX != *c; c++)
601 if (argid == *c)
602 return(1);
603
604 return(0);
605 }
606
607
608 static void
609 roffnode_free(struct rofftree *tree)
610 {
611 struct roffnode *p;
612
613 assert(tree->last);
614
615 p = tree->last;
616 tree->last = tree->last->parent;
617 free(p);
618 }
619
620
621 static int
622 roffspecial(struct rofftree *tree, int tok, const char *start,
623 const int *argc, const char **argv,
624 size_t sz, char **ordp)
625 {
626
627 switch (tok) {
628 case (ROFF_At):
629 if (0 == sz)
630 break;
631 if (0 == strcmp(*ordp, "v6"))
632 break;
633 else if (0 == strcmp(*ordp, "v7"))
634 break;
635 else if (0 == strcmp(*ordp, "32v"))
636 break;
637 else if (0 == strcmp(*ordp, "V.1"))
638 break;
639 else if (0 == strcmp(*ordp, "V.4"))
640 break;
641 roff_err(tree, start, "invalid `At' arg");
642 return(0);
643
644 case (ROFF_Fn):
645 if (0 != sz)
646 break;
647 roff_err(tree, start, "`%s' expects at least "
648 "one arg", toknames[tok]);
649 return(0);
650
651 case (ROFF_Nm):
652 if (0 == sz) {
653 if (0 == tree->name[0]) {
654 roff_err(tree, start, "`Nm' not set");
655 return(0);
656 }
657 ordp[0] = tree->name;
658 ordp[1] = NULL;
659 } else if ( ! roffsetname(tree, ordp))
660 return(0);
661 break;
662
663 case (ROFF_Rv):
664 /* FALLTHROUGH*/
665 case (ROFF_Sx):
666 /* FALLTHROUGH*/
667 case (ROFF_Ex):
668 if (1 == sz)
669 break;
670 roff_err(tree, start, "`%s' expects one arg",
671 toknames[tok]);
672 return(0);
673
674 case (ROFF_Sm):
675 if (1 != sz) {
676 roff_err(tree, start, "`Sm' expects one arg");
677 return(0);
678 }
679
680 if (0 != strcmp(ordp[0], "on") &&
681 0 != strcmp(ordp[0], "off")) {
682 roff_err(tree, start, "`Sm' has invalid argument");
683 return(0);
684 }
685 break;
686
687 case (ROFF_Ud):
688 /* FALLTHROUGH */
689 case (ROFF_Ux):
690 /* FALLTHROUGH */
691 case (ROFF_Bt):
692 if (0 != sz) {
693 roff_err(tree, start, "`%s' expects no args",
694 toknames[tok]);
695 return(0);
696 }
697 break;
698 default:
699 break;
700 }
701
702 return((*tree->cb.roffspecial)(tree->arg, tok,
703 tree->cur, argc, argv, ordp));
704 }
705
706
707 static int
708 roffcall(struct rofftree *tree, int tok, char **argv)
709 {
710
711 if (NULL == tokens[tok].cb) {
712 roff_err(tree, *argv, "unsupported macro `%s'",
713 toknames[tok]);
714 return(0);
715 }
716 if ( ! (*tokens[tok].cb)(tok, tree, argv, ROFF_ENTER))
717 return(0);
718 return(1);
719 }
720
721
722 static int
723 roffnextopt(const struct rofftree *tree, int tok,
724 char ***in, char **val)
725 {
726 char *arg, **argv;
727 int v;
728
729 *val = NULL;
730 argv = *in;
731 assert(argv);
732
733 if (NULL == (arg = *argv))
734 return(-1);
735 if ('-' != *arg)
736 return(-1);
737
738 if (ROFF_ARGMAX == (v = rofffindarg(arg + 1))) {
739 roff_warn(tree, arg, "argument-like parameter `%s' to "
740 "`%s'", arg, toknames[tok]);
741 return(-1);
742 }
743
744 if ( ! roffargok(tok, v)) {
745 roff_warn(tree, arg, "invalid argument parameter `%s' to "
746 "`%s'", tokargnames[v], toknames[tok]);
747 return(-1);
748 }
749
750 if ( ! (ROFF_VALUE & tokenargs[v]))
751 return(v);
752
753 *in = ++argv;
754
755 if (NULL == *argv) {
756 roff_err(tree, arg, "empty value of `%s' for `%s'",
757 tokargnames[v], toknames[tok]);
758 return(ROFF_ARGMAX);
759 }
760
761 return(v);
762 }
763
764
765 static int
766 roffpurgepunct(struct rofftree *tree, char **argv)
767 {
768 int i;
769
770 i = 0;
771 while (argv[i])
772 i++;
773 assert(i > 0);
774 if ( ! roffispunct(argv[--i]))
775 return(1);
776 while (i >= 0 && roffispunct(argv[i]))
777 i--;
778 i++;
779
780 /* LINTED */
781 while (argv[i])
782 if ( ! roffdata(tree, 0, argv[i++]))
783 return(0);
784 return(1);
785 }
786
787
788 static int
789 roffparseopts(struct rofftree *tree, int tok,
790 char ***args, int *argc, char **argv)
791 {
792 int i, c;
793 char *v;
794
795 i = 0;
796
797 while (-1 != (c = roffnextopt(tree, tok, args, &v))) {
798 if (ROFF_ARGMAX == c)
799 return(0);
800
801 argc[i] = c;
802 argv[i] = v;
803 i++;
804 *args = *args + 1;
805 }
806
807 argc[i] = ROFF_ARGMAX;
808 argv[i] = NULL;
809 return(1);
810 }
811
812
813 static int
814 roffdata(struct rofftree *tree, int space, char *buf)
815 {
816
817 if (0 == *buf)
818 return(1);
819 return((*tree->cb.roffdata)(tree->arg,
820 space != 0, tree->cur, buf));
821 }
822
823
824 /* ARGSUSED */
825 static int
826 roff_Dd(ROFFCALL_ARGS)
827 {
828 time_t t;
829 char *p, buf[32];
830
831 if (ROFF_BODY & tree->state) {
832 assert( ! (ROFF_PRELUDE & tree->state));
833 assert(ROFF_PRELUDE_Dd & tree->state);
834 return(roff_text(tok, tree, argv, type));
835 }
836
837 assert(ROFF_PRELUDE & tree->state);
838 assert( ! (ROFF_BODY & tree->state));
839
840 if (ROFF_PRELUDE_Dd & tree->state) {
841 roff_err(tree, *argv, "repeated `Dd' in prelude");
842 return(0);
843 } else if (ROFF_PRELUDE_Dt & tree->state) {
844 roff_err(tree, *argv, "out-of-order `Dd' in prelude");
845 return(0);
846 }
847
848 assert(NULL == tree->last);
849
850 argv++;
851
852 if (0 == strcmp(*argv, "$Mdocdate: December 7 2008 $")) {
853 t = time(NULL);
854 if (NULL == localtime_r(&t, &tree->tm))
855 err(1, "localtime_r");
856 tree->state |= ROFF_PRELUDE_Dd;
857 return(1);
858 }
859
860 /* Build this from Mdocdate or raw date. */
861
862 buf[0] = 0;
863 p = *argv;
864
865 if (0 != strcmp(*argv, "$Mdocdate:")) {
866 while (*argv) {
867 if (strlcat(buf, *argv++, sizeof(buf))
868 < sizeof(buf))
869 continue;
870 roff_err(tree, p, "bad `Dd' date");
871 return(0);
872 }
873 if (strptime(buf, "%b%d,%Y", &tree->tm)) {
874 tree->state |= ROFF_PRELUDE_Dd;
875 return(1);
876 }
877 roff_err(tree, *argv, "bad `Dd' date");
878 return(0);
879 }
880
881 argv++;
882 while (*argv && **argv != '$') {
883 if (strlcat(buf, *argv++, sizeof(buf))
884 >= sizeof(buf)) {
885 roff_err(tree, p, "bad `Dd' Mdocdate");
886 return(0);
887 }
888 if (strlcat(buf, " ", sizeof(buf))
889 >= sizeof(buf)) {
890 roff_err(tree, p, "bad `Dd' Mdocdate");
891 return(0);
892 }
893 }
894 if (NULL == *argv) {
895 roff_err(tree, p, "bad `Dd' Mdocdate");
896 return(0);
897 }
898
899 if (NULL == strptime(buf, "%b %d %Y", &tree->tm)) {
900 roff_err(tree, *argv, "bad `Dd' Mdocdate");
901 return(0);
902 }
903
904 tree->state |= ROFF_PRELUDE_Dd;
905 return(1);
906 }
907
908
909 /* ARGSUSED */
910 static int
911 roff_Dt(ROFFCALL_ARGS)
912 {
913
914 if (ROFF_BODY & tree->state) {
915 assert( ! (ROFF_PRELUDE & tree->state));
916 assert(ROFF_PRELUDE_Dt & tree->state);
917 return(roff_text(tok, tree, argv, type));
918 }
919
920 assert(ROFF_PRELUDE & tree->state);
921 assert( ! (ROFF_BODY & tree->state));
922
923 if ( ! (ROFF_PRELUDE_Dd & tree->state)) {
924 roff_err(tree, *argv, "out-of-order `Dt' in prelude");
925 return(0);
926 } else if (ROFF_PRELUDE_Dt & tree->state) {
927 roff_err(tree, *argv, "repeated `Dt' in prelude");
928 return(0);
929 }
930
931 argv++;
932 if (NULL == *argv) {
933 roff_err(tree, *argv, "`Dt' needs document title");
934 return(0);
935 } else if (strlcpy(tree->title, *argv, sizeof(tree->title))
936 >= sizeof(tree->title)) {
937 roff_err(tree, *argv, "`Dt' document title too long");
938 return(0);
939 }
940
941 argv++;
942 if (NULL == *argv) {
943 roff_err(tree, *argv, "`Dt' needs section");
944 return(0);
945 } else if (strlcpy(tree->section, *argv, sizeof(tree->section))
946 >= sizeof(tree->section)) {
947 roff_err(tree, *argv, "`Dt' section too long");
948 return(0);
949 }
950
951 argv++;
952 if (NULL == *argv) {
953 tree->volume[0] = 0;
954 } else if (strlcpy(tree->volume, *argv, sizeof(tree->volume))
955 >= sizeof(tree->volume)) {
956 roff_err(tree, *argv, "`Dt' volume too long");
957 return(0);
958 }
959
960 assert(NULL == tree->last);
961 tree->state |= ROFF_PRELUDE_Dt;
962
963 return(1);
964 }
965
966
967 static int
968 roffsetname(struct rofftree *tree, char **ordp)
969 {
970
971 assert(*ordp);
972
973 /* FIXME: not all sections can set this. */
974
975 if (NULL != *(ordp + 1)) {
976 roff_err(tree, *ordp, "too many `Nm' args");
977 return(0);
978 }
979
980 if (strlcpy(tree->name, *ordp, sizeof(tree->name))
981 >= sizeof(tree->name)) {
982 roff_err(tree, *ordp, "`Nm' arg too long");
983 return(0);
984 }
985
986 return(1);
987 }
988
989
990 /* ARGSUSED */
991 static int
992 roff_Ns(ROFFCALL_ARGS)
993 {
994 int j, c, first;
995 char *morep[1];
996
997 first = (*argv++ == tree->cur);
998 morep[0] = NULL;
999
1000 if ( ! roffspecial(tree, tok, *argv, NULL, NULL, 0, morep))
1001 return(0);
1002
1003 while (*argv) {
1004 if (ROFF_MAX != (c = rofffindcallable(*argv))) {
1005 if ( ! roffcall(tree, c, argv))
1006 return(0);
1007 break;
1008 }
1009
1010 if ( ! roffispunct(*argv)) {
1011 if ( ! roffdata(tree, 1, *argv++))
1012 return(0);
1013 continue;
1014 }
1015
1016 for (j = 0; argv[j]; j++)
1017 if ( ! roffispunct(argv[j]))
1018 break;
1019
1020 if (argv[j]) {
1021 if ( ! roffdata(tree, 0, *argv++))
1022 return(0);
1023 continue;
1024 }
1025
1026 break;
1027 }
1028
1029 if ( ! first)
1030 return(1);
1031
1032 return(roffpurgepunct(tree, argv));
1033 }
1034
1035
1036 /* ARGSUSED */
1037 static int
1038 roff_Os(ROFFCALL_ARGS)
1039 {
1040 char *p;
1041
1042 if (ROFF_BODY & tree->state) {
1043 assert( ! (ROFF_PRELUDE & tree->state));
1044 assert(ROFF_PRELUDE_Os & tree->state);
1045 return(roff_text(tok, tree, argv, type));
1046 }
1047
1048 assert(ROFF_PRELUDE & tree->state);
1049 if ( ! (ROFF_PRELUDE_Dt & tree->state) ||
1050 ! (ROFF_PRELUDE_Dd & tree->state)) {
1051 roff_err(tree, *argv, "out-of-order `Os' in prelude");
1052 return(0);
1053 }
1054
1055 tree->os[0] = 0;
1056
1057 p = *++argv;
1058
1059 while (*argv) {
1060 if (strlcat(tree->os, *argv++, sizeof(tree->os))
1061 < sizeof(tree->os))
1062 continue;
1063 roff_err(tree, p, "`Os' value too long");
1064 return(0);
1065 }
1066
1067 if (0 == tree->os[0])
1068 if (strlcpy(tree->os, "LOCAL", sizeof(tree->os))
1069 >= sizeof(tree->os)) {
1070 roff_err(tree, p, "`Os' value too long");
1071 return(0);
1072 }
1073
1074 tree->state |= ROFF_PRELUDE_Os;
1075 tree->state &= ~ROFF_PRELUDE;
1076 tree->state |= ROFF_BODY;
1077
1078 assert(NULL == tree->last);
1079
1080 return((*tree->cb.roffhead)(tree->arg, &tree->tm,
1081 tree->os, tree->title, tree->section,
1082 tree->volume));
1083 }
1084
1085
1086 /* ARGSUSED */
1087 static int
1088 roff_layout(ROFFCALL_ARGS)
1089 {
1090 int i, c, argcp[ROFF_MAXLINEARG];
1091 char *argvp[ROFF_MAXLINEARG];
1092
1093 if (ROFF_PRELUDE & tree->state) {
1094 roff_err(tree, *argv, "bad `%s' in prelude",
1095 toknames[tok]);
1096 return(0);
1097 } else if (ROFF_EXIT == type) {
1098 roffnode_free(tree);
1099 if ( ! (*tree->cb.roffblkbodyout)(tree->arg, tok))
1100 return(0);
1101 return((*tree->cb.roffblkout)(tree->arg, tok));
1102 }
1103
1104 assert( ! (ROFF_CALLABLE & tokens[tok].flags));
1105
1106 ++argv;
1107
1108 if ( ! roffparseopts(tree, tok, &argv, argcp, argvp))
1109 return(0);
1110 if (NULL == roffnode_new(tok, tree))
1111 return(0);
1112
1113 /*
1114 * Layouts have two parts: the layout body and header. The
1115 * layout header is the trailing text of the line macro, while
1116 * the layout body is everything following until termination.
1117 */
1118
1119 if ( ! (*tree->cb.roffblkin)(tree->arg, tok, argcp, argvp))
1120 return(0);
1121 if (NULL == *argv)
1122 return((*tree->cb.roffblkbodyin)
1123 (tree->arg, tok, argcp, argvp));
1124
1125 if ( ! (*tree->cb.roffblkheadin)(tree->arg, tok, argcp, argvp))
1126 return(0);
1127
1128 /*
1129 * If there are no parsable parts, then write remaining tokens
1130 * into the layout header and exit.
1131 */
1132
1133 if ( ! (ROFF_PARSED & tokens[tok].flags)) {
1134 i = 0;
1135 while (*argv)
1136 if ( ! roffdata(tree, i++, *argv++))
1137 return(0);
1138
1139 if ( ! (*tree->cb.roffblkheadout)(tree->arg, tok))
1140 return(0);
1141 return((*tree->cb.roffblkbodyin)
1142 (tree->arg, tok, argcp, argvp));
1143 }
1144
1145 /*
1146 * Parsable elements may be in the header (or be the header, for
1147 * that matter). Follow the regular parsing rules for these.
1148 */
1149
1150 i = 0;
1151 while (*argv) {
1152 if (ROFF_MAX == (c = rofffindcallable(*argv))) {
1153 assert(tree->arg);
1154 if ( ! roffdata(tree, i++, *argv++))
1155 return(0);
1156 continue;
1157 }
1158 if ( ! roffcall(tree, c, argv))
1159 return(0);
1160 break;
1161 }
1162
1163 /*
1164 * If there's trailing punctuation in the header, then write it
1165 * out now. Here we mimic the behaviour of a line-dominant text
1166 * macro.
1167 */
1168
1169 if (NULL == *argv) {
1170 if ( ! (*tree->cb.roffblkheadout)(tree->arg, tok))
1171 return(0);
1172 return((*tree->cb.roffblkbodyin)
1173 (tree->arg, tok, argcp, argvp));
1174 }
1175
1176 /*
1177 * Expensive. Scan to the end of line then work backwards until
1178 * a token isn't punctuation.
1179 */
1180
1181 if ( ! roffpurgepunct(tree, argv))
1182 return(0);
1183
1184 if ( ! (*tree->cb.roffblkheadout)(tree->arg, tok))
1185 return(0);
1186 return((*tree->cb.roffblkbodyin)
1187 (tree->arg, tok, argcp, argvp));
1188 }
1189
1190
1191 /* ARGSUSED */
1192 static int
1193 roff_ordered(ROFFCALL_ARGS)
1194 {
1195 int i, first, c, argcp[ROFF_MAXLINEARG];
1196 char *ordp[ROFF_MAXLINEARG], *p,
1197 *argvp[ROFF_MAXLINEARG];
1198
1199 if (ROFF_PRELUDE & tree->state) {
1200 roff_err(tree, *argv, "`%s' disallowed in prelude",
1201 toknames[tok]);
1202 return(0);
1203 }
1204
1205 first = (*argv == tree->cur);
1206 p = *argv++;
1207
1208 if ( ! roffparseopts(tree, tok, &argv, argcp, argvp))
1209 return(0);
1210
1211 if (NULL == *argv) {
1212 ordp[0] = NULL;
1213 return(roffspecial(tree, tok, p, argcp,
1214 (const char **)argvp, 0, ordp));
1215 }
1216
1217 i = 0;
1218 while (*argv && i < ROFF_MAXLINEARG) {
1219 c = ROFF_PARSED & tokens[tok].flags ?
1220 rofffindcallable(*argv) : ROFF_MAX;
1221
1222 if (ROFF_MAX == c && ! roffispunct(*argv)) {
1223 ordp[i++] = *argv++;
1224 continue;
1225 }
1226 ordp[i] = NULL;
1227
1228 if (ROFF_MAX == c)
1229 break;
1230
1231 if ( ! roffspecial(tree, tok, p, argcp,
1232 (const char **)argvp,
1233 (size_t)i, ordp))
1234 return(0);
1235
1236 return(roffcall(tree, c, argv));
1237 }
1238
1239 assert(i != ROFF_MAXLINEARG);
1240 ordp[i] = NULL;
1241
1242 if ( ! roffspecial(tree, tok, p, argcp,
1243 (const char**)argvp,
1244 (size_t)i, ordp))
1245 return(0);
1246
1247 /* FIXME: error if there's stuff after the punctuation. */
1248
1249 if ( ! first || NULL == *argv)
1250 return(1);
1251
1252 return(roffpurgepunct(tree, argv));
1253 }
1254
1255
1256 /* ARGSUSED */
1257 static int
1258 roff_text(ROFFCALL_ARGS)
1259 {
1260 int i, j, first, c, argcp[ROFF_MAXLINEARG];
1261 char *argvp[ROFF_MAXLINEARG];
1262
1263 if (ROFF_PRELUDE & tree->state) {
1264 roff_err(tree, *argv, "`%s' disallowed in prelude",
1265 toknames[tok]);
1266 return(0);
1267 }
1268
1269 first = (*argv == tree->cur);
1270 argv++;
1271
1272 if ( ! roffparseopts(tree, tok, &argv, argcp, argvp))
1273 return(0);
1274 if ( ! (*tree->cb.roffin)(tree->arg, tok, argcp, argvp))
1275 return(0);
1276 if (NULL == *argv)
1277 return((*tree->cb.roffout)(tree->arg, tok));
1278
1279 if ( ! (ROFF_PARSED & tokens[tok].flags)) {
1280 i = 0;
1281 while (*argv)
1282 if ( ! roffdata(tree, i++, *argv++))
1283 return(0);
1284
1285 return((*tree->cb.roffout)(tree->arg, tok));
1286 }
1287
1288 /*
1289 * Deal with punctuation. Ugly. Work ahead until we encounter
1290 * terminating punctuation. If we encounter it and all
1291 * subsequent tokens are punctuation, then stop processing (the
1292 * line-dominant macro will print these tokens after closure).
1293 * If the punctuation is followed by non-punctuation, then close
1294 * and re-open our scope, then continue.
1295 */
1296
1297 i = 0;
1298 while (*argv) {
1299 if (ROFF_MAX != (c = rofffindcallable(*argv))) {
1300 if ( ! (ROFF_LSCOPE & tokens[tok].flags))
1301 if ( ! (*tree->cb.roffout)(tree->arg, tok))
1302 return(0);
1303
1304 if ( ! roffcall(tree, c, argv))
1305 return(0);
1306
1307 if (ROFF_LSCOPE & tokens[tok].flags)
1308 if ( ! (*tree->cb.roffout)(tree->arg, tok))
1309 return(0);
1310
1311 break;
1312 }
1313
1314 if ( ! roffispunct(*argv)) {
1315 if ( ! roffdata(tree, i++, *argv++))
1316 return(0);
1317 continue;
1318 }
1319
1320 i = 1;
1321 for (j = 0; argv[j]; j++)
1322 if ( ! roffispunct(argv[j]))
1323 break;
1324
1325 if (argv[j]) {
1326 if (ROFF_LSCOPE & tokens[tok].flags) {
1327 if ( ! roffdata(tree, 0, *argv++))
1328 return(0);
1329 continue;
1330 }
1331 if ( ! (*tree->cb.roffout)(tree->arg, tok))
1332 return(0);
1333 if ( ! roffdata(tree, 0, *argv++))
1334 return(0);
1335 if ( ! (*tree->cb.roffin)(tree->arg, tok,
1336 argcp, argvp))
1337 return(0);
1338
1339 i = 0;
1340 continue;
1341 }
1342
1343 if ( ! (*tree->cb.roffout)(tree->arg, tok))
1344 return(0);
1345 break;
1346 }
1347
1348 if (NULL == *argv)
1349 return((*tree->cb.roffout)(tree->arg, tok));
1350 if ( ! first)
1351 return(1);
1352
1353 return(roffpurgepunct(tree, argv));
1354 }
1355
1356
1357 /* ARGSUSED */
1358 static int
1359 roff_noop(ROFFCALL_ARGS)
1360 {
1361
1362 return(1);
1363 }
1364
1365
1366 /* ARGSUSED */
1367 static int
1368 roff_depr(ROFFCALL_ARGS)
1369 {
1370
1371 roff_err(tree, *argv, "`%s' is deprecated", toknames[tok]);
1372 return(0);
1373 }
1374
1375
1376 static void
1377 roff_warn(const struct rofftree *tree, const char *pos, char *fmt, ...)
1378 {
1379 va_list ap;
1380 char buf[128];
1381
1382 va_start(ap, fmt);
1383 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
1384 va_end(ap);
1385
1386 (*tree->cb.roffmsg)(tree->arg,
1387 ROFF_WARN, tree->cur, pos, buf);
1388 }
1389
1390
1391 static void
1392 roff_err(const struct rofftree *tree, const char *pos, char *fmt, ...)
1393 {
1394 va_list ap;
1395 char buf[128];
1396
1397 va_start(ap, fmt);
1398 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
1399 va_end(ap);
1400
1401 (*tree->cb.roffmsg)(tree->arg,
1402 ROFF_ERROR, tree->cur, pos, buf);
1403 }
1404