Annotation of pod2mdoc/pod2mdoc.c, Revision 1.2
1.2 ! schwarze 1: /* $Id: pod2mdoc.c,v 1.1 2014/03/20 15:07:56 schwarze Exp $ */
1.1 schwarze 2: /*
3: * Copyright (c) 2014 Kristaps Dzonsons <kristaps@bsd.lv>
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 above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #include <sys/stat.h>
18: #include <sys/time.h>
19:
20: #include <assert.h>
21: #include <ctype.h>
22: #include <fcntl.h>
23: #include <getopt.h>
24: #include <stdio.h>
25: #include <stdlib.h>
26: #include <string.h>
27: #include <unistd.h>
28:
29: struct args {
30: const char *title; /* override "Dt" title */
31: const char *date; /* override "Dd" date */
32: const char *section; /* override "Dt" section */
33: };
34:
35: struct state {
36: int parsing; /* after =cut of before command */
37: int paused; /* in =begin and before =end */
38: int haspar; /* in paragraph: do we need Pp? */
39: int isname; /* are we the NAME section? */
40: const char *fname; /* file being parsed */
41: };
42:
43: enum fmt {
44: FMT_ITALIC,
45: FMT_BOLD,
46: FMT_CODE,
47: FMT_LINK,
48: FMT_ESCAPE,
49: FMT_FILE,
50: FMT_NBSP,
51: FMT_INDEX,
52: FMT_NULL,
53: FMT__MAX
54: };
55:
56: enum cmd {
57: CMD_POD = 0,
58: CMD_HEAD1,
59: CMD_HEAD2,
60: CMD_HEAD3,
61: CMD_HEAD4,
62: CMD_OVER,
63: CMD_ITEM,
64: CMD_BACK,
65: CMD_BEGIN,
66: CMD_END,
67: CMD_FOR,
68: CMD_ENCODING,
69: CMD_CUT,
70: CMD__MAX
71: };
72:
73: static const char *const cmds[CMD__MAX] = {
74: "pod", /* CMD_POD */
75: "head1", /* CMD_HEAD1 */
76: "head2", /* CMD_HEAD2 */
77: "head3", /* CMD_HEAD3 */
78: "head4", /* CMD_HEAD4 */
79: "over", /* CMD_OVER */
80: "item", /* CMD_ITEM */
81: "back", /* CMD_BACK */
82: "begin", /* CMD_BEGIN */
83: "end", /* CMD_END */
84: "for", /* CMD_FOR */
85: "encoding", /* CMD_ENCODING */
86: "cut" /* CMD_CUT */
87: };
88:
89: static const char fmts[FMT__MAX] = {
90: 'I', /* FMT_ITALIC */
91: 'B', /* FMT_BOLD */
92: 'C', /* FMT_CODE */
93: 'L', /* FMT_LINK */
94: 'E', /* FMT_ESCAPE */
95: 'F', /* FMT_FILE */
96: 'S', /* FMT_NBSP */
97: 'X', /* FMT_INDEX */
98: 'Z' /* FMT_NULL */
99: };
100:
101: /*
102: * Given buf[*start] is at the start of an escape name, read til the end
103: * of the escape ('>') then try to do something with it.
104: * Sets start to be one after the '>'.
105: */
106: static void
107: formatescape(const char *buf, size_t *start, size_t end)
108: {
109: char esc[16]; /* no more needed */
110: size_t i, max;
111:
112: max = sizeof(esc) - 1;
113: i = 0;
114: /* Read til our buffer is full. */
115: while (*start < end && '>' != buf[*start] && i < max)
116: esc[i++] = buf[(*start)++];
117: esc[i] = '\0';
118:
119: if (i == max) {
120: /* Too long... skip til we end. */
121: while (*start < end && '>' != buf[*start])
122: (*start)++;
123: return;
124: } else if (*start >= end)
125: return;
126:
127: assert('>' == buf[*start]);
128: (*start)++;
129:
130: /*
131: * TODO: right now, we only recognise the named escapes.
132: * Just let the rest of them go.
133: */
134: if (0 == strcmp(esc, "lt"))
135: printf("\\(la");
136: else if (0 == strcmp(esc, "gt"))
137: printf("\\(ra");
138: else if (0 == strcmp(esc, "vb"))
139: printf("\\(ba");
140: else if (0 == strcmp(esc, "sol"))
141: printf("\\(sl");
142: }
143:
144: /*
145: * Skip space characters.
146: */
147: static void
148: skipspace(const char *buf, size_t *start, size_t end)
149: {
150:
151: while (*start < end && ' ' == buf[*start])
152: (*start)++;
153: }
154:
155: /*
156: * We're at the character in front of a format code, which is structured
157: * like X<...> and can contain nested format codes.
158: * This consumes the whole format code, and any nested format codes, til
159: * the end of matched production.
160: * If "reentrant", then we're being called after a macro has already
161: * been printed to the current line.
162: * "last" is set to the last read character: this is used to determine
163: * whether we should buffer with space or not.
164: * If "nomacro", then we don't print any macros, just contained data.
165: */
166: static int
167: formatcode(const char *buf, size_t *start,
168: size_t end, int reentrant, int last, int nomacro)
169: {
170: enum fmt fmt;
171:
172: assert(*start + 1 < end);
173: assert('<' == buf[*start + 1]);
174:
175: for (fmt = 0; fmt < FMT__MAX; fmt++)
176: if (buf[*start] == fmts[fmt])
177: break;
178:
179: /* Invalid macros are just regular text. */
180:
181: if (FMT__MAX == fmt) {
182: putchar(buf[*start]);
183: (*start)++;
184: return(0);
185: }
186:
187: *start += 2;
188:
189: /*
190: * Escapes don't print macro sequences, so just output them like
191: * normal text before processing for macros.
192: */
193: if (FMT_ESCAPE == fmt) {
194: formatescape(buf, start, end);
195: return(0);
196: } else if (FMT_NULL == fmt || FMT_INDEX == fmt) {
197: /* For indices and nulls, just consume. */
198: while (*start < end && '>' != buf[*start])
199: (*start)++;
200: if (*start < end)
201: (*start)++;
202: return(0);
203: }
204:
205: if ( ! nomacro) {
206: /*
207: * Print out the macro describing this format code.
208: * If we're not "reentrant" (not yet on a macro line)
209: * then print a newline, if necessary, and the macro
210: * indicator.
211: * Otherwise, offset us with a space.
212: */
213: if ( ! reentrant && last != '\n')
214: putchar('\n');
215: if ( ! reentrant)
216: putchar('.');
217: else
218: putchar(' ');
219:
220: /*
221: * If we don't have whitespace before us, then suppress
222: * macro whitespace with Ns.
223: */
224: if (' ' != last)
225: printf("Ns ");
226: switch (fmt) {
227: case (FMT_ITALIC):
228: printf("Em ");
229: break;
230: case (FMT_BOLD):
231: printf("Sy ");
232: break;
233: case (FMT_CODE):
1.2 ! schwarze 234: printf("Qo Li ");
1.1 schwarze 235: break;
236: case (FMT_LINK):
237: printf("Lk ");
238: break;
239: case (FMT_FILE):
240: printf("Pa ");
241: break;
242: case (FMT_NBSP):
243: /* TODO. */
244: printf("No ");
245: break;
246: default:
247: abort();
248: }
249: }
250:
251: /*
252: * Read until we reach the end market ('>') or until we find a
253: * nested format code.
254: * Don't emit any newlines: since we're on a macro line, we
255: * don't want to break the line.
256: */
257: while (*start < end) {
258: if ('>' == buf[*start]) {
259: (*start)++;
260: break;
261: }
262: if (*start + 1 < end && '<' == buf[*start + 1]) {
263: formatcode(buf, start, end, 1, last, nomacro);
264: continue;
265: }
266: if ('\n' != buf[*start]) {
267: /*
268: * Make sure that any macro-like words (or
269: * really any word starting with a capital
270: * letter) is assumed to be a macro that must be
271: * escaped.
272: * XXX: should this be isalpha()?
273: */
274: if ((' ' == last || '\n' == last) &&
275: isupper(buf[*start]))
276: printf("\\&");
277: putchar(last = buf[*start]);
278: }
279: (*start)++;
280: }
1.2 ! schwarze 281:
! 282: if ( ! nomacro && FMT_CODE == fmt)
! 283: printf(" Qc ");
1.1 schwarze 284:
285: if (reentrant)
286: return(1);
287:
288: /*
289: * If we're not reentrant, we want to put ending punctuation on
290: * the macro line so that it's properly handled by being
291: * smooshed against the terminal word.
292: */
293: skipspace(buf, start, end);
294: if (',' != buf[*start] && '.' != buf[*start] &&
295: '!' != buf[*start] && '?' != buf[*start] &&
296: ')' != buf[*start])
297: return(1);
298: while (*start < end) {
299: if (',' != buf[*start] &&
300: '.' != buf[*start] &&
301: '!' != buf[*start] &&
302: '?' != buf[*start] &&
303: ')' != buf[*start])
304: break;
305: putchar(' ');
306: putchar(buf[*start]);
307: (*start)++;
308: }
309: skipspace(buf, start, end);
310: return(1);
311: }
312:
313: /*
314: * Calls formatcode() til the end of a paragraph.
315: */
316: static void
317: formatcodeln(const char *buf, size_t *start, size_t end, int nomacro)
318: {
319: int last;
320:
321: last = '\n';
322: while (*start < end) {
323: if (*start + 1 < end && '<' == buf[*start + 1]) {
324: formatcode(buf, start, end, 1, last, nomacro);
325: continue;
326: }
327: if ('\n' != buf[*start])
328: putchar(last = buf[*start]);
329: (*start)++;
330: }
331: }
332:
333: /*
334: * A command paragraph, as noted in the perlpod manual, just indicates
335: * that we should do something, optionally with some text to print as
336: * well.
337: */
338: static void
339: command(struct state *st, const char *buf, size_t start, size_t end)
340: {
341: size_t len, csz;
342: enum cmd cmd;
343:
344: assert('=' == buf[start]);
345: start++;
346: len = end - start;
347:
348: for (cmd = 0; cmd < CMD__MAX; cmd++) {
349: csz = strlen(cmds[cmd]);
350: if (len < csz)
351: continue;
352: if (0 == memcmp(&buf[start], cmd[cmds], csz))
353: break;
354: }
355:
356: /* Ignore bogus commands. */
357:
358: if (CMD__MAX == cmd)
359: return;
360:
361: start += csz;
362: skipspace(buf, &start, end);
363: len = end - start;
364:
365: if (st->paused) {
366: st->paused = CMD_END != cmd;
367: return;
368: }
369:
370: switch (cmd) {
371: case (CMD_POD):
372: break;
373: case (CMD_HEAD1):
374: /*
375: * The behaviour of head= follows from a quick glance at
376: * how pod2man handles it.
377: */
378: printf(".Sh ");
379: st->isname = 0;
380: if (end - start == 4)
381: if (0 == memcmp(&buf[start], "NAME", 4))
382: st->isname = 1;
383: formatcodeln(buf, &start, end, 1);
384: putchar('\n');
385: st->haspar = 1;
386: break;
387: case (CMD_HEAD2):
388: printf(".Ss ");
389: formatcodeln(buf, &start, end, 1);
390: putchar('\n');
391: st->haspar = 1;
392: break;
393: case (CMD_HEAD3):
394: puts(".Pp");
395: printf(".Em ");
396: formatcodeln(buf, &start, end, 0);
397: putchar('\n');
398: puts(".Pp");
399: st->haspar = 1;
400: break;
401: case (CMD_HEAD4):
402: puts(".Pp");
403: printf(".No ");
404: formatcodeln(buf, &start, end, 0);
405: putchar('\n');
406: puts(".Pp");
407: st->haspar = 1;
408: break;
409: case (CMD_OVER):
410: /*
411: * TODO: we should be doing this after we process the
412: * first =item to see whether we'll do an -enum,
413: * -bullet, or something else.
414: */
415: puts(".Bl -tag -width Ds");
416: break;
417: case (CMD_ITEM):
418: printf(".It ");
419: formatcodeln(buf, &start, end, 0);
420: putchar('\n');
421: st->haspar = 1;
422: break;
423: case (CMD_BACK):
424: puts(".El");
425: break;
426: case (CMD_BEGIN):
427: /*
428: * We disregard all types for now.
429: * TODO: process at least "text" in a -literal block.
430: */
431: st->paused = 1;
432: break;
433: case (CMD_FOR):
434: /*
435: * We ignore all types of encodings and formats
436: * unilaterally.
437: */
438: break;
439: case (CMD_ENCODING):
440: break;
441: case (CMD_CUT):
442: st->parsing = 0;
443: return;
444: default:
445: abort();
446: }
447:
448: /* Any command (but =cut) makes us start parsing. */
449: st->parsing = 1;
450: }
451:
452: /*
453: * Just pump out the line in a verbatim block.
454: */
455: static void
456: verbatim(struct state *st, const char *buf, size_t start, size_t end)
457: {
458:
459: if ( ! st->parsing || st->paused)
460: return;
461:
462: puts(".Bd -literal");
463: printf("%.*s\n", (int)(end - start), &buf[start]);
464: puts(".Ed");
465: }
466:
467: /*
468: * Ordinary paragraph.
469: * Well, this is really the hardest--POD seems to assume that, for
470: * example, a leading space implies a newline, and so on.
471: * Lots of other snakes in the grass: escaping a newline followed by a
472: * period (accidental mdoc(7) control), double-newlines after macro
473: * passages, etc.
474: */
475: static void
476: ordinary(struct state *st, const char *buf, size_t start, size_t end)
477: {
478: int last;
479: size_t i, j;
480:
481: if ( ! st->parsing || st->paused)
482: return;
483:
484: /*
485: * Special-case: the NAME section.
486: * If we find a "-" when searching from the end, assume that
487: * we're in "name - description" format.
488: * To wit, print out a "Nm" and "Nd" in that format.
489: */
490: if (st->isname) {
491: for (i = end - 1; i > start; i--)
492: if ('-' == buf[i])
493: break;
494: if ('-' == buf[i]) {
495: j = i;
496: /* Roll over multiple "-". */
497: for ( ; i > start; i--)
498: if ('-' != buf[i])
499: break;
500: printf(".Nm %.*s\n",
501: (int)((i + 1) - start), &buf[start]);
502: printf(".Nd %.*s\n",
503: (int)(end - (j + 1)), &buf[j + 1]);
504: return;
505: }
506: }
507:
508: if ( ! st->haspar)
509: puts(".Pp");
510:
511: st->haspar = 0;
512: last = '\n';
513:
514: while (start < end) {
515: /*
516: * Loop til we get either to a newline or escape.
517: * Escape initial control characters.
518: */
519: while (start < end) {
520: if (start < end - 1 && '<' == buf[start + 1])
521: break;
522: else if ('\n' == buf[start])
523: break;
524: else if ('\n' == last && '.' == buf[start])
525: printf("\\&");
526: else if ('\n' == last && '\'' == buf[start])
527: printf("\\&");
528: putchar(last = buf[start++]);
529: }
530:
531: if (start < end - 1 && '<' == buf[start + 1]) {
532: /*
533: * We've encountered a format code.
534: * This is going to trigger a macro no matter
535: * what, so print a newline now.
536: * Then print the (possibly nested) macros and
537: * following that, a newline.
538: */
539: if (formatcode(buf, &start, end, 0, last, 0))
540: putchar(last = '\n');
541: } else if (start < end && '\n' == buf[start]) {
542: /*
543: * Print the newline only if we haven't already
544: * printed a newline.
545: */
546: if (last != '\n')
547: putchar(last = buf[start]);
548: if (++start >= end)
549: continue;
550: /*
551: * If we have whitespace next, eat it to prevent
552: * mdoc(7) from thinking that it's meant for
553: * verbatim text.
554: * It is--but if we start with that, we can't
555: * have a macro subsequent it, which may be
556: * possible if we have an escape next.
557: */
558: if (' ' == buf[start] || '\t' == buf[start]) {
559: puts(".br");
560: last = '\n';
561: }
562: for ( ; start < end; start++)
563: if (' ' != buf[start] && '\t' != buf[start])
564: break;
565: } else if (start < end) {
566: /*
567: * Default: print the character.
568: * Escape initial control characters.
569: */
570: if ('\n' == last && '.' == buf[start])
571: printf("\\&");
572: else if ('\n' == last && '\'' == buf[start])
573: printf("\\&");
574: putchar(last = buf[start++]);
575: }
576: }
577:
578: if (last != '\n')
579: putchar('\n');
580: }
581:
582: /*
583: * There are three kinds of paragraphs: verbatim (starts with whitespace
584: * of some sort), ordinary (starts without "=" marker), or a command
585: * (default: starts with "=").
586: */
587: static void
588: dopar(struct state *st, const char *buf, size_t start, size_t end)
589: {
590:
591: if (end == start)
592: return;
593: if (' ' == buf[start] || '\t' == buf[start])
594: verbatim(st, buf, start, end);
595: else if ('=' != buf[start])
596: ordinary(st, buf, start, end);
597: else
598: command(st, buf, start, end);
599: }
600:
601: /*
602: * Loop around paragraphs within a document, processing each one in the
603: * POD way.
604: */
605: static void
606: dofile(const struct args *args, const char *fname,
607: const struct tm *tm, const char *buf, size_t sz)
608: {
609: size_t sup, end, i, cur = 0;
610: struct state st;
611: const char *section, *date;
612: char datebuf[64];
613: char *title, *cp;
614:
615: if (0 == sz)
616: return;
617:
618: /* Title is last path component of the filename. */
619:
620: if (NULL != args->title)
621: title = strdup(args->title);
622: else if (NULL != (cp = strrchr(fname, '/')))
623: title = strdup(cp + 1);
624: else
625: title = strdup(fname);
626:
627: if (NULL == title) {
628: perror(NULL);
629: exit(EXIT_FAILURE);
630: }
631:
632: /* Section is 1 unless suffix is "pm". */
633:
634: if (NULL == (section = args->section)) {
635: section = "1";
636: if (NULL != (cp = strrchr(title, '.'))) {
637: *cp++ = '\0';
638: if (0 == strcmp(cp, "pm"))
639: section = "3p";
640: }
641: }
642:
643: /* Date. Or the given "tm" if not supplied. */
644:
645: if (NULL == (date = args->date)) {
646: strftime(datebuf, sizeof(datebuf), "%B %d, %Y", tm);
647: date = datebuf;
648: }
649:
650: for (cp = title; '\0' != *cp; cp++)
651: *cp = toupper((int)*cp);
652:
653: /* The usual mdoc(7) preamble. */
654:
655: printf(".Dd %s\n", date);
656: printf(".Dt %s %s\n", title, section);
657: puts(".Os");
658:
659: free(title);
660:
661: memset(&st, 0, sizeof(struct state));
662: assert(sz > 0);
663:
664: /* Main loop over file contents. */
665:
666: while (cur < sz) {
667: /* Read until next paragraph. */
668: for (i = cur + 1; i < sz; i++)
669: if ('\n' == buf[i] && '\n' == buf[i - 1]) {
670: /* Consume blank paragraphs. */
671: while (i + 1 < sz && '\n' == buf[i + 1])
672: i++;
673: break;
674: }
675:
676: /* Adjust end marker for EOF. */
677: end = i < sz ? i - 1 :
678: ('\n' == buf[sz - 1] ? sz - 1 : sz);
679: sup = i < sz ? end + 2 : sz;
680:
681: /* Process paragraph and adjust start. */
682: dopar(&st, buf, cur, end);
683: cur = sup;
684: }
685: }
686:
687: /*
688: * Read a single file fully into memory.
689: * If the file is "-", do it from stdin.
690: * If successfully read, send the input buffer to dofile() for further
691: * processing.
692: */
693: static int
694: readfile(const struct args *args, const char *fname)
695: {
696: int fd;
697: char *buf;
698: size_t bufsz, cur;
699: ssize_t ssz;
700: struct tm *tm;
701: time_t ttm;
702: struct stat st;
703:
704: assert(NULL != fname);
705:
706: fd = 0 != strcmp("-", fname) ?
707: open(fname, O_RDONLY, 0) : STDIN_FILENO;
708:
709: if (-1 == fd) {
710: perror(fname);
711: return(0);
712: }
713:
714: if (STDIN_FILENO == fd || -1 == fstat(fd, &st)) {
715: ttm = time(NULL);
716: tm = localtime(&ttm);
717: } else
718: tm = localtime(&st.st_mtime);
719:
720: /*
721: * Arbitrarily-sized initial buffer.
722: * Should be big enough for most files...
723: */
724: cur = 0;
725: bufsz = 1 << 14;
726: if (NULL == (buf = malloc(bufsz))) {
727: perror(NULL);
728: exit(EXIT_FAILURE);
729: }
730:
731: while ((ssz = read(fd, buf + cur, bufsz - cur)) > 0) {
732: /* Double buffer size on fill. */
733: if ((size_t)ssz == bufsz - cur) {
734: bufsz *= 2;
735: if (NULL == (buf = realloc(buf, bufsz))) {
736: perror(NULL);
737: exit(EXIT_FAILURE);
738: }
739: }
740: cur += (size_t)ssz;
741: }
742: if (ssz < 0) {
743: perror(fname);
744: free(buf);
745: return(0);
746: }
747:
748: dofile(args, STDIN_FILENO == fd ?
749: "STDIN" : fname, tm, buf, cur);
750: free(buf);
751: if (STDIN_FILENO != fd)
752: close(fd);
753: return(1);
754: }
755:
756: int
757: main(int argc, char *argv[])
758: {
759: const char *fname, *name;
760: struct args args;
761: int c;
762:
763: name = strrchr(argv[0], '/');
764: if (name == NULL)
765: name = argv[0];
766: else
767: ++name;
768:
769: memset(&args, 0, sizeof(struct args));
770: fname = "-";
771:
772: /* Accept no arguments for now. */
773:
774: while (-1 != (c = getopt(argc, argv, "c:d:hln:oq:rs:uv")))
775: switch (c) {
776: case ('h'):
777: /* FALLTHROUGH */
778: case ('l'):
779: /* FALLTHROUGH */
780: case ('c'):
781: /* FALLTHROUGH */
782: case ('o'):
783: /* FALLTHROUGH */
784: case ('q'):
785: /* FALLTHROUGH */
786: case ('r'):
787: /* FALLTHROUGH */
788: case ('u'):
789: /* FALLTHROUGH */
790: case ('v'):
791: /* Ignore these. */
792: break;
793: case ('d'):
794: args.date = optarg;
795: break;
796: case ('n'):
797: args.title = optarg;
798: break;
799: case ('s'):
800: args.section = optarg;
801: break;
802: default:
803: goto usage;
804: }
805:
806: argc -= optind;
807: argv += optind;
808:
809: /* Accept only a single input file. */
810:
811: if (argc > 2)
812: return(EXIT_FAILURE);
813: else if (1 == argc)
814: fname = *argv;
815:
816: return(readfile(&args, fname) ?
817: EXIT_SUCCESS : EXIT_FAILURE);
818:
819: usage:
820: fprintf(stderr, "usage: %s [-d date] "
821: "[-n title] [-s section]\n", name);
822:
823: return(EXIT_FAILURE);
824: }
CVSweb