Annotation of pod2mdoc/pod2mdoc.c, Revision 1.37
1.37 ! schwarze 1: /* $Id: pod2mdoc.c,v 1.36 2014/10/24 00:28:34 schwarze Exp $ */
1.1 schwarze 2: /*
3: * Copyright (c) 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.37 ! schwarze 4: * Copyright (c) 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.1 schwarze 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 <sys/stat.h>
19: #include <sys/time.h>
20:
21: #include <assert.h>
22: #include <ctype.h>
23: #include <fcntl.h>
24: #include <getopt.h>
25: #include <stdio.h>
26: #include <stdlib.h>
27: #include <string.h>
28: #include <unistd.h>
29:
1.37 ! schwarze 30: #include "dict.h"
! 31:
1.10 kristaps 32: /*
1.19 kristaps 33: * In what section can we find Perl module manuals?
34: * Sometimes (Mac OS X) it's 3pm, sometimes (OpenBSD, etc.) 3p.
35: * XXX IF YOU CHANGE THIS, CHANGE POD2MDOC.1 AS WELL.
1.10 kristaps 36: */
37: #define PERL_SECTION "3p"
38:
1.1 schwarze 39: struct args {
40: const char *title; /* override "Dt" title */
41: const char *date; /* override "Dd" date */
42: const char *section; /* override "Dt" section */
43: };
44:
1.4 schwarze 45: enum list {
46: LIST_BULLET = 0,
47: LIST_ENUM,
48: LIST_TAG,
49: LIST__MAX
50: };
51:
1.11 kristaps 52: enum sect {
53: SECT_NONE = 0,
54: SECT_NAME, /* NAME section */
55: SECT_SYNOPSIS, /* SYNOPSIS section */
56: };
57:
1.32 schwarze 58: enum outstate {
59: OUST_NL = 0, /* just started a new output line */
60: OUST_TXT, /* text line output in progress */
61: OUST_MAC /* macro line output in progress */
62: };
63:
1.1 schwarze 64: struct state {
1.31 schwarze 65: const char *fname; /* file being parsed */
1.1 schwarze 66: int parsing; /* after =cut of before command */
67: int paused; /* in =begin and before =end */
1.11 kristaps 68: enum sect sect; /* which section are we in? */
1.4 schwarze 69: #define LIST_STACKSZ 128
70: enum list lstack[LIST_STACKSZ]; /* open lists */
71: size_t lpos; /* where in list stack */
1.31 schwarze 72: int haspar; /* in paragraph: do we need Pp? */
1.32 schwarze 73: enum outstate oust; /* state of the mdoc output stream */
74: int wantws; /* let mdoc(7) output whitespace here */
1.31 schwarze 75: char *outbuf; /* text buffered for output */
76: size_t outbufsz; /* allocated size of outbuf */
77: size_t outbuflen; /* current length of outbuf */
1.1 schwarze 78: };
79:
80: enum fmt {
81: FMT_ITALIC,
82: FMT_BOLD,
83: FMT_CODE,
84: FMT_LINK,
85: FMT_ESCAPE,
86: FMT_FILE,
87: FMT_NBSP,
88: FMT_INDEX,
89: FMT_NULL,
90: FMT__MAX
91: };
92:
93: enum cmd {
94: CMD_POD = 0,
95: CMD_HEAD1,
96: CMD_HEAD2,
97: CMD_HEAD3,
98: CMD_HEAD4,
99: CMD_OVER,
100: CMD_ITEM,
101: CMD_BACK,
102: CMD_BEGIN,
103: CMD_END,
104: CMD_FOR,
105: CMD_ENCODING,
106: CMD_CUT,
107: CMD__MAX
108: };
109:
110: static const char *const cmds[CMD__MAX] = {
111: "pod", /* CMD_POD */
112: "head1", /* CMD_HEAD1 */
113: "head2", /* CMD_HEAD2 */
114: "head3", /* CMD_HEAD3 */
115: "head4", /* CMD_HEAD4 */
116: "over", /* CMD_OVER */
117: "item", /* CMD_ITEM */
118: "back", /* CMD_BACK */
119: "begin", /* CMD_BEGIN */
120: "end", /* CMD_END */
121: "for", /* CMD_FOR */
122: "encoding", /* CMD_ENCODING */
123: "cut" /* CMD_CUT */
124: };
125:
126: static const char fmts[FMT__MAX] = {
127: 'I', /* FMT_ITALIC */
128: 'B', /* FMT_BOLD */
129: 'C', /* FMT_CODE */
130: 'L', /* FMT_LINK */
131: 'E', /* FMT_ESCAPE */
132: 'F', /* FMT_FILE */
133: 'S', /* FMT_NBSP */
134: 'X', /* FMT_INDEX */
135: 'Z' /* FMT_NULL */
136: };
137:
1.6 kristaps 138: static int last;
139:
1.31 schwarze 140:
141: static void
142: outbuf_grow(struct state *st, size_t by)
143: {
144:
145: st->outbufsz += (by / 128 + 1) * 128;
146: st->outbuf = realloc(st->outbuf, st->outbufsz);
147: if (NULL == st->outbuf) {
148: perror(NULL);
149: exit(EXIT_FAILURE);
150: }
151: }
152:
153: static void
154: outbuf_addchar(struct state *st)
155: {
156:
157: if (st->outbuflen + 2 >= st->outbufsz)
158: outbuf_grow(st, 1);
159: st->outbuf[st->outbuflen++] = last;
160: if ('\\' == last)
161: st->outbuf[st->outbuflen++] = 'e';
162: st->outbuf[st->outbuflen] = '\0';
1.32 schwarze 163: st->wantws = 0;
1.31 schwarze 164: }
165:
166: static void
167: outbuf_addstr(struct state *st, const char *str)
168: {
169: size_t slen;
170:
171: slen = strlen(str);
172: if (st->outbuflen + slen >= st->outbufsz)
173: outbuf_grow(st, slen);
174: memcpy(st->outbuf + st->outbuflen, str, slen+1);
1.33 schwarze 175: st->outbuflen += slen;
1.31 schwarze 176: last = str[slen - 1];
1.32 schwarze 177: st->wantws = 0;
1.31 schwarze 178: }
179:
180: static void
181: outbuf_flush(struct state *st)
182: {
183:
184: if (0 == st->outbuflen)
185: return;
186:
187: fputs(st->outbuf, stdout);
188: *st->outbuf = '\0';
189: st->outbuflen = 0;
1.32 schwarze 190:
191: if (OUST_NL == st->oust)
192: st->oust = OUST_TXT;
1.31 schwarze 193: }
194:
195: static void
1.32 schwarze 196: mdoc_newln(struct state *st)
1.31 schwarze 197: {
198:
1.32 schwarze 199: if (OUST_NL == st->oust)
1.31 schwarze 200: return;
1.32 schwarze 201:
1.31 schwarze 202: putchar('\n');
203: last = '\n';
1.32 schwarze 204: st->oust = OUST_NL;
205: st->wantws = 1;
1.31 schwarze 206: }
207:
1.1 schwarze 208: /*
209: * Given buf[*start] is at the start of an escape name, read til the end
210: * of the escape ('>') then try to do something with it.
211: * Sets start to be one after the '>'.
1.32 schwarze 212: *
213: * This function does not care about output modes,
214: * it merely appends text to the output buffer,
215: * which can then be used in any mode.
1.1 schwarze 216: */
217: static void
1.31 schwarze 218: formatescape(struct state *st, const char *buf, size_t *start, size_t end)
1.1 schwarze 219: {
220: char esc[16]; /* no more needed */
221: size_t i, max;
222:
223: max = sizeof(esc) - 1;
224: i = 0;
225: /* Read til our buffer is full. */
226: while (*start < end && '>' != buf[*start] && i < max)
227: esc[i++] = buf[(*start)++];
228: esc[i] = '\0';
229:
230: if (i == max) {
231: /* Too long... skip til we end. */
232: while (*start < end && '>' != buf[*start])
233: (*start)++;
234: return;
235: } else if (*start >= end)
236: return;
237:
238: assert('>' == buf[*start]);
239: (*start)++;
240:
241: /*
242: * TODO: right now, we only recognise the named escapes.
243: * Just let the rest of them go.
244: */
1.6 kristaps 245: if (0 == strcmp(esc, "lt"))
1.31 schwarze 246: outbuf_addstr(st, "\\(la");
1.1 schwarze 247: else if (0 == strcmp(esc, "gt"))
1.31 schwarze 248: outbuf_addstr(st, "\\(ra");
1.33 schwarze 249: else if (0 == strcmp(esc, "verbar"))
1.31 schwarze 250: outbuf_addstr(st, "\\(ba");
1.1 schwarze 251: else if (0 == strcmp(esc, "sol"))
1.31 schwarze 252: outbuf_addstr(st, "\\(sl");
1.1 schwarze 253: }
254:
255: /*
1.9 kristaps 256: * Run some heuristics to intuit a link format.
1.19 kristaps 257: * I set "start" to be the end of the sequence (last right-carrot) so
1.9 kristaps 258: * that the caller can safely just continue processing.
1.19 kristaps 259: * If this is just an empty tag, I'll return 0.
1.32 schwarze 260: *
261: * Always operates in OUST_MAC mode.
262: * Mode handling is done by the caller.
1.9 kristaps 263: */
264: static int
265: trylink(const char *buf, size_t *start, size_t end, size_t dsz)
266: {
1.21 kristaps 267: size_t linkstart, realend, linkend,
268: i, j, textsz, stack;
1.9 kristaps 269:
270: /*
271: * Scan to the start of the terminus.
272: * This function is more or less replicated in the formatcode()
273: * for null or index formatting codes.
1.23 kristaps 274: * However, we're slightly different because we might have
275: * nested escapes we need to ignore.
1.9 kristaps 276: */
1.21 kristaps 277: stack = 0;
1.19 kristaps 278: for (linkstart = realend = *start; realend < end; realend++) {
1.23 kristaps 279: if ('<' == buf[realend])
280: stack++;
1.19 kristaps 281: if ('>' != buf[realend])
1.9 kristaps 282: continue;
1.23 kristaps 283: else if (stack-- > 0)
284: continue;
285: if (dsz == 1)
1.9 kristaps 286: break;
1.19 kristaps 287: assert(realend > 0);
288: if (' ' != buf[realend - 1])
1.9 kristaps 289: continue;
1.19 kristaps 290: for (i = realend, j = 0; i < end && j < dsz; j++)
1.9 kristaps 291: if ('>' != buf[i++])
292: break;
293: if (dsz == j)
294: break;
295: }
1.19 kristaps 296:
297: /* Ignore stubs. */
298: if (realend == end || realend == *start)
1.9 kristaps 299: return(0);
300:
1.19 kristaps 301: /* Set linkend to the end of content. */
302: linkend = dsz > 1 ? realend - 1 : realend;
1.18 kristaps 303:
1.19 kristaps 304: /* Re-scan to see if we have a title or section. */
305: for (textsz = *start; textsz < linkend; textsz++)
306: if ('|' == buf[textsz] || '/' == buf[textsz])
1.18 kristaps 307: break;
308:
1.19 kristaps 309: if (textsz < linkend && '|' == buf[textsz]) {
1.20 kristaps 310: /* With title: set start, then end at section. */
1.19 kristaps 311: linkstart = textsz + 1;
1.18 kristaps 312: textsz = textsz - *start;
1.19 kristaps 313: for (i = linkstart; i < linkend; i++)
314: if ('/' == buf[i])
315: break;
316: if (i < linkend)
317: linkend = i;
1.20 kristaps 318: } else if (textsz < linkend && '/' == buf[textsz]) {
319: /* With section: set end at section. */
320: linkend = textsz;
321: textsz = 0;
322: } else
323: /* No title, no section. */
1.18 kristaps 324: textsz = 0;
1.19 kristaps 325:
326: *start = realend;
327: j = linkend - linkstart;
328:
1.20 kristaps 329: /* Do we have only subsection material? */
330: if (0 == j && '/' == buf[linkend]) {
331: linkstart = linkend + 1;
332: linkend = dsz > 1 ? realend - 1 : realend;
333: if (0 == (j = linkend - linkstart))
334: return(0);
335: printf("Sx %.*s", (int)j, &buf[linkstart]);
336: return(1);
337: } else if (0 == j)
1.19 kristaps 338: return(0);
339:
340: /* See if we qualify as being a link or not. */
1.20 kristaps 341: if ((j > 4 && 0 == memcmp("http:", &buf[linkstart], j)) ||
342: (j > 5 && 0 == memcmp("https:", &buf[linkstart], j)) ||
343: (j > 3 && 0 == memcmp("ftp:", &buf[linkstart], j)) ||
344: (j > 4 && 0 == memcmp("sftp:", &buf[linkstart], j)) ||
345: (j > 3 && 0 == memcmp("smb:", &buf[linkstart], j)) ||
346: (j > 3 && 0 == memcmp("afs:", &buf[linkstart], j))) {
347: /* Gross. */
348: printf("Lk %.*s", (int)((dsz > 1 ? realend - 1 :
349: realend) - linkstart), &buf[linkstart]);
1.19 kristaps 350: return(1);
351: }
352:
353: /* See if we qualify as a mailto. */
1.20 kristaps 354: if (j > 6 && 0 == memcmp("mailto:", &buf[linkstart], j)) {
1.19 kristaps 355: printf("Mt %.*s", (int)j, &buf[linkstart]);
356: return(1);
357: }
358:
359: /* See if we're a foo(5), foo(5x), or foo(5xx) manpage. */
360: if ((j > 3 && ')' == buf[linkend - 1]) &&
361: ('(' == buf[linkend - 3])) {
362: printf("Xr %.*s %c", (int)(j - 3),
363: &buf[linkstart], buf[linkend - 2]);
364: return(1);
365: } else if ((j > 4 && ')' == buf[linkend - 1]) &&
366: ('(' == buf[linkend - 4])) {
367: printf("Xr %.*s %.*s", (int)(j - 4),
368: &buf[linkstart], 2, &buf[linkend - 3]);
369: return(1);
370: } else if ((j > 5 && ')' == buf[linkend - 1]) &&
371: ('(' == buf[linkend - 5])) {
372: printf("Xr %.*s %.*s", (int)(j - 5),
373: &buf[linkstart], 3, &buf[linkend - 4]);
374: return(1);
375: }
376:
377: /* Last try: do we have a double-colon? */
378: for (i = linkstart + 1; i < linkend; i++)
379: if (':' == buf[i] && ':' == buf[i - 1])
1.18 kristaps 380: break;
1.9 kristaps 381:
1.19 kristaps 382: if (i < linkend)
1.10 kristaps 383: printf("Xr %.*s " PERL_SECTION,
1.19 kristaps 384: (int)j, &buf[linkstart]);
1.9 kristaps 385: else
1.19 kristaps 386: printf("Xr %.*s 1", (int)j, &buf[linkstart]);
1.9 kristaps 387:
388: return(1);
389: }
390:
1.13 kristaps 391: /*
392: * Doclifting: if we're a bold "-xx" and we're in the SYNOPSIS section,
393: * then it's likely that we're a flag.
394: * Our flag might be followed by an argument, so make sure that we're
395: * accounting for that, too.
396: * If we don't have a flag at all, however, then assume we're an "Ar".
1.32 schwarze 397: *
398: * Always operates in OUST_MAC mode.
399: * Mode handlinf is done by the caller.
1.13 kristaps 400: */
401: static void
402: dosynopsisfl(const char *buf, size_t *start, size_t end)
403: {
404: size_t i;
405: again:
1.14 kristaps 406: assert(*start + 1 < end);
407: assert('-' == buf[*start]);
408:
409: if ( ! isalnum((int)buf[*start + 1]) &&
410: '?' != buf[*start + 1] &&
411: '-' != buf[*start + 1]) {
412: (*start)--;
413: fputs("Ar ", stdout);
414: return;
415: }
416:
1.13 kristaps 417: (*start)++;
418: for (i = *start; i < end; i++)
419: if (isalnum((int)buf[i]))
420: continue;
1.14 kristaps 421: else if ('?' == buf[i])
422: continue;
1.13 kristaps 423: else if ('-' == buf[i])
424: continue;
425: else if ('_' == buf[i])
426: continue;
427: else
428: break;
429:
430: assert(i < end);
431:
432: if ( ! (' ' == buf[i] || '>' == buf[i])) {
433: printf("Ar ");
434: return;
435: }
436:
437: printf("Fl ");
438: if (end - *start > 1 &&
439: isupper((int)buf[*start]) &&
440: islower((int)buf[*start + 1]) &&
441: (end - *start == 2 ||
442: ' ' == buf[*start + 2]))
443: printf("\\&");
444: printf("%.*s ", (int)(i - *start), &buf[*start]);
445: *start = i;
446:
447: if (' ' == buf[i]) {
448: while (i < end && ' ' == buf[i])
449: i++;
450: assert(i < end);
451: if ('-' == buf[i]) {
452: *start = i;
453: goto again;
454: }
455: printf("Ar ");
456: *start = i;
457: }
458: }
459:
1.9 kristaps 460: /*
1.1 schwarze 461: * We're at the character in front of a format code, which is structured
462: * like X<...> and can contain nested format codes.
463: * This consumes the whole format code, and any nested format codes, til
464: * the end of matched production.
1.6 kristaps 465: * If "nomacro", then we don't print any macros, just contained data
466: * (e.g., following "Sh" or "Nm").
1.15 kristaps 467: * "pos" is only significant in SYNOPSIS, and should be 0 when invoked
468: * as the first format code on a line (for decoration as an "Nm"),
469: * non-zero otherwise.
1.32 schwarze 470: *
471: * Output mode handling is most complicated here.
472: * We may enter in any mode.
473: * We usually exit in OUST_MAC mode, except when
474: * entering without OUST_MAC and the code is invalid.
1.1 schwarze 475: */
1.33 schwarze 476: static int
1.15 kristaps 477: formatcode(struct state *st, const char *buf, size_t *start,
1.32 schwarze 478: size_t end, int nomacro, int pos)
1.1 schwarze 479: {
480: enum fmt fmt;
1.5 kristaps 481: size_t i, j, dsz;
1.1 schwarze 482:
483: assert(*start + 1 < end);
484: assert('<' == buf[*start + 1]);
485:
1.6 kristaps 486: /*
487: * First, look up the format code.
1.30 schwarze 488: * If it's not valid, treat it as a NOOP.
1.6 kristaps 489: */
490: for (fmt = 0; fmt < FMT__MAX; fmt++)
491: if (buf[*start] == fmts[fmt])
492: break;
493:
1.5 kristaps 494: /*
495: * Determine whether we're overriding our delimiter.
496: * According to POD, if we have more than one '<' followed by a
497: * space, then we need a space followed by matching '>' to close
498: * the expression.
499: * Otherwise we use the usual '<' and '>' matched pair.
500: */
501: i = *start + 1;
502: while (i < end && '<' == buf[i])
503: i++;
504: assert(i > *start + 1);
505: dsz = i - (*start + 1);
506: if (dsz > 1 && (i >= end || ' ' != buf[i]))
507: dsz = 1;
508:
509: /* Remember, if dsz>1, to jump the trailing space. */
510: *start += dsz + 1 + (dsz > 1 ? 1 : 0);
1.1 schwarze 511:
512: /*
1.6 kristaps 513: * Escapes and ignored codes (NULL and INDEX) don't print macro
514: * sequences, so just output them like normal text before
515: * processing for real macros.
1.1 schwarze 516: */
517: if (FMT_ESCAPE == fmt) {
1.31 schwarze 518: formatescape(st, buf, start, end);
1.33 schwarze 519: return(0);
1.1 schwarze 520: } else if (FMT_NULL == fmt || FMT_INDEX == fmt) {
1.5 kristaps 521: /*
1.6 kristaps 522: * Just consume til the end delimiter, accounting for
523: * whether it's a custom one.
1.5 kristaps 524: */
525: for ( ; *start < end; (*start)++) {
526: if ('>' != buf[*start])
527: continue;
528: else if (dsz == 1)
529: break;
530: assert(*start > 0);
531: if (' ' != buf[*start - 1])
532: continue;
533: i = *start;
534: for (j = 0; i < end && j < dsz; j++)
535: if ('>' != buf[i++])
536: break;
537: if (dsz != j)
538: continue;
539: (*start) += dsz;
540: break;
541: }
1.24 kristaps 542: if (*start < end) {
543: assert('>' == buf[*start]);
544: (*start)++;
545: }
546: if (isspace(last))
547: while (*start < end && isspace((int)buf[*start]))
548: (*start)++;
1.33 schwarze 549: return(0);
1.1 schwarze 550: }
551:
1.6 kristaps 552: /*
553: * Check whether we're supposed to print macro stuff (this is
554: * suppressed in, e.g., "Nm" and "Sh" macros).
555: */
1.30 schwarze 556: if (FMT__MAX != fmt && !nomacro) {
1.32 schwarze 557:
558: /*
559: * We may already have wantws if there was whitespace
560: * before the code ("text B<text"), but initial
561: * whitespace inside our scope ("textB< text")
562: * allows to break at this point as well.
563: */
564:
565: st->wantws |= ' ' == buf[*start];
1.31 schwarze 566:
1.1 schwarze 567: /*
1.31 schwarze 568: * If we are on a text line and there is no
569: * whitespace before our content, we have to make
570: * the previous word a prefix to the macro line.
1.32 schwarze 571: * In the following, mdoc_newln() must not be used
572: * lest we clobber out output state.
1.1 schwarze 573: */
1.31 schwarze 574:
1.32 schwarze 575: if (OUST_MAC != st->oust && !st->wantws) {
576: if (OUST_NL != st->oust)
1.31 schwarze 577: putchar('\n');
578: printf(".Pf ");
579: }
580:
581: outbuf_flush(st);
582:
583: /* Whitespace is easier to suppress on macro lines. */
584:
1.32 schwarze 585: if (OUST_MAC == st->oust && !st->wantws)
586: printf(" Ns ");
1.31 schwarze 587:
588: /* Unless we are on a macro line, start one. */
589:
1.32 schwarze 590: if (OUST_MAC != st->oust && st->wantws) {
591: if (OUST_NL != st->oust)
1.6 kristaps 592: putchar('\n');
1.1 schwarze 593: putchar('.');
1.31 schwarze 594: } else
1.1 schwarze 595: putchar(' ');
1.31 schwarze 596:
1.32 schwarze 597: /*
598: * Print the macro corresponding to this format code,
599: * and update the output state afterwards.
600: */
1.6 kristaps 601:
1.1 schwarze 602: switch (fmt) {
603: case (FMT_ITALIC):
604: printf("Em ");
605: break;
606: case (FMT_BOLD):
1.14 kristaps 607: if (SECT_SYNOPSIS == st->sect) {
608: if (1 == dsz && '-' == buf[*start])
609: dosynopsisfl(buf, start, end);
1.15 kristaps 610: else if (0 == pos)
611: printf("Nm ");
1.14 kristaps 612: else
613: printf("Ar ");
614: break;
615: }
1.27 schwarze 616: if (0 == strncmp(buf + *start, "NULL", 4) &&
617: ('=' == buf[*start + 4] ||
618: '>' == buf[*start + 4]))
619: printf("Dv ");
620: else
621: printf("Sy ");
1.1 schwarze 622: break;
623: case (FMT_CODE):
1.2 schwarze 624: printf("Qo Li ");
1.1 schwarze 625: break;
626: case (FMT_LINK):
1.19 kristaps 627: /* Try to link; use "No" if it's empty. */
1.9 kristaps 628: if ( ! trylink(buf, start, end, dsz))
629: printf("No ");
1.1 schwarze 630: break;
631: case (FMT_FILE):
632: printf("Pa ");
633: break;
634: case (FMT_NBSP):
635: printf("No ");
636: break;
637: default:
638: abort();
639: }
1.32 schwarze 640: st->oust = OUST_MAC;
641: st->wantws = 1;
1.31 schwarze 642: } else
643: outbuf_flush(st);
1.1 schwarze 644:
645: /*
1.6 kristaps 646: * Process until we reach the end marker (e.g., '>') or until we
1.5 kristaps 647: * find a nested format code.
1.1 schwarze 648: * Don't emit any newlines: since we're on a macro line, we
649: * don't want to break the line.
650: */
651: while (*start < end) {
1.5 kristaps 652: if ('>' == buf[*start] && 1 == dsz) {
1.1 schwarze 653: (*start)++;
654: break;
1.5 kristaps 655: } else if ('>' == buf[*start] &&
656: ' ' == buf[*start - 1]) {
657: /*
658: * Handle custom delimiters.
659: * These require a certain number of
660: * space-preceded carrots before we're really at
661: * the end.
662: */
663: i = *start;
664: for (j = 0; i < end && j < dsz; j++)
665: if ('>' != buf[i++])
666: break;
667: if (dsz == j) {
668: *start += dsz;
669: break;
670: }
1.1 schwarze 671: }
1.34 schwarze 672: if (*start + 1 < end && '<' == buf[*start + 1] &&
673: 'A' <= buf[*start] && 'Z' >= buf[*start]) {
1.32 schwarze 674: formatcode(st, buf, start, end, nomacro, 1);
1.1 schwarze 675: continue;
676: }
1.3 schwarze 677:
1.32 schwarze 678: /* Suppress newlines and multiple spaces. */
679:
680: last = buf[(*start)++];
681: if (' ' == last || '\n' == last) {
682: putchar(' ');
683: while (*start < end && ' ' == buf[*start])
684: (*start)++;
685: continue;
686: }
687:
1.33 schwarze 688: if (OUST_MAC == st->oust && FMT__MAX != fmt) {
1.32 schwarze 689: if ( ! st->wantws) {
690: printf(" Ns ");
691: st->wantws = 1;
692: }
693:
694: /*
695: * Escape macro-like words.
696: * This matches "Xx " and "XxEOLN".
697: */
698:
699: if (end - *start > 0 &&
700: isupper((unsigned char)last) &&
701: islower((unsigned char)buf[*start]) &&
702: (end - *start == 1 ||
703: ' ' == buf[*start + 1] ||
704: '>' == buf[*start + 1]))
705: printf("\\&");
706: }
1.3 schwarze 707:
1.32 schwarze 708: putchar(last);
1.4 schwarze 709:
1.8 kristaps 710: /* Protect against character escapes. */
1.32 schwarze 711:
1.8 kristaps 712: if ('\\' == last)
713: putchar('e');
1.1 schwarze 714: }
1.2 schwarze 715:
1.33 schwarze 716: if (FMT__MAX == fmt)
717: return(0);
718:
1.2 schwarze 719: if ( ! nomacro && FMT_CODE == fmt)
720: printf(" Qc ");
1.1 schwarze 721:
1.33 schwarze 722: st->wantws = ' ' == last;
723: return(1);
1.1 schwarze 724: }
725:
726: /*
727: * Calls formatcode() til the end of a paragraph.
1.32 schwarze 728: * Goes to OUST_MAC mode and stays there when returning,
729: * such that the caller can add arguments to the macro line
730: * before closing it out.
1.1 schwarze 731: */
732: static void
1.32 schwarze 733: formatcodeln(struct state *st, const char *linemac,
734: const char *buf, size_t *start, size_t end, int nomacro)
1.1 schwarze 735: {
1.33 schwarze 736: int gotmacro, wantws;
1.1 schwarze 737:
1.32 schwarze 738: assert(OUST_NL == st->oust);
739: assert(st->wantws);
740: printf(".%s ", linemac);
741: st->oust = OUST_MAC;
742:
1.33 schwarze 743: gotmacro = 0;
1.1 schwarze 744: while (*start < end) {
1.33 schwarze 745: wantws = ' ' == buf[*start] || '\n' == buf[*start];
746: if (wantws) {
747: last = ' ';
748: do {
749: (*start)++;
750: } while (*start < end && ' ' == buf[*start]);
751: }
752:
1.34 schwarze 753: if (*start + 1 < end && '<' == buf[*start + 1] &&
754: 'A' <= buf[*start] && 'Z' >= buf[*start]) {
1.33 schwarze 755: st->wantws |= wantws;
756: gotmacro = formatcode(st, buf,
757: start, end, nomacro, 1);
1.1 schwarze 758: continue;
759: }
1.32 schwarze 760:
1.33 schwarze 761: if (gotmacro) {
762: if (*start < end || st->outbuflen) {
763: if (st->wantws ||
764: (wantws && !st->outbuflen))
765: printf(" No ");
766: else
767: printf(" Ns ");
768: }
769: gotmacro = 0;
770: }
771: outbuf_flush(st);
772: st->wantws = wantws;
773:
774: if (*start >= end)
775: break;
776:
777: if (st->wantws) {
778: putchar(' ');
779: st->wantws = 0;
1.32 schwarze 780: }
781:
1.4 schwarze 782: /*
783: * Since we're already on a macro line, we want to make
784: * sure that we don't inadvertently invoke a macro.
785: * We need to do this carefully because section names
786: * are used in troff and we don't want to escape
787: * something that needn't be escaped.
788: */
789: if (' ' == last && end - *start > 1 &&
1.33 schwarze 790: isupper((unsigned char)buf[*start]) &&
791: islower((unsigned char)buf[*start + 1]) &&
792: (end - *start == 2 || ' ' == buf[*start + 2]))
1.4 schwarze 793: printf("\\&");
794:
1.33 schwarze 795: putchar(last = buf[*start]);
1.8 kristaps 796:
797: /* Protect against character escapes. */
1.33 schwarze 798:
1.8 kristaps 799: if ('\\' == last)
800: putchar('e');
801:
1.1 schwarze 802: (*start)++;
803: }
804: }
805:
806: /*
1.4 schwarze 807: * Guess at what kind of list we are.
808: * These are taken straight from the POD manual.
809: * I don't know what people do in real life.
810: */
811: static enum list
812: listguess(const char *buf, size_t start, size_t end)
813: {
814: size_t len = end - start;
815:
816: assert(end >= start);
817:
818: if (len == 1 && '*' == buf[start])
819: return(LIST_BULLET);
820: if (len == 2 && '1' == buf[start] && '.' == buf[start + 1])
821: return(LIST_ENUM);
822: else if (len == 1 && '1' == buf[start])
823: return(LIST_ENUM);
824: else
825: return(LIST_TAG);
826: }
827:
828: /*
1.1 schwarze 829: * A command paragraph, as noted in the perlpod manual, just indicates
830: * that we should do something, optionally with some text to print as
831: * well.
1.32 schwarze 832: * From the perspective of external callers,
833: * always stays in OUST_NL/wantws mode,
834: * but its children do use OUST_MAC.
1.1 schwarze 835: */
836: static void
837: command(struct state *st, const char *buf, size_t start, size_t end)
838: {
839: size_t len, csz;
840: enum cmd cmd;
841:
842: assert('=' == buf[start]);
843: start++;
844: len = end - start;
845:
846: for (cmd = 0; cmd < CMD__MAX; cmd++) {
847: csz = strlen(cmds[cmd]);
848: if (len < csz)
849: continue;
850: if (0 == memcmp(&buf[start], cmd[cmds], csz))
851: break;
852: }
853:
854: /* Ignore bogus commands. */
855:
856: if (CMD__MAX == cmd)
857: return;
858:
859: start += csz;
1.8 kristaps 860: while (start < end && ' ' == buf[start])
861: start++;
862:
1.1 schwarze 863: len = end - start;
864:
865: if (st->paused) {
866: st->paused = CMD_END != cmd;
867: return;
868: }
869:
870: switch (cmd) {
871: case (CMD_POD):
872: break;
873: case (CMD_HEAD1):
874: /*
875: * The behaviour of head= follows from a quick glance at
876: * how pod2man handles it.
877: */
1.11 kristaps 878: st->sect = SECT_NONE;
879: if (end - start == 4) {
1.1 schwarze 880: if (0 == memcmp(&buf[start], "NAME", 4))
1.11 kristaps 881: st->sect = SECT_NAME;
882: } else if (end - start == 8) {
883: if (0 == memcmp(&buf[start], "SYNOPSIS", 8))
884: st->sect = SECT_SYNOPSIS;
885: }
1.32 schwarze 886: formatcodeln(st, "Sh", buf, &start, end, 1);
887: mdoc_newln(st);
1.1 schwarze 888: st->haspar = 1;
889: break;
890: case (CMD_HEAD2):
1.32 schwarze 891: formatcodeln(st, "Ss", buf, &start, end, 1);
892: mdoc_newln(st);
1.1 schwarze 893: st->haspar = 1;
894: break;
895: case (CMD_HEAD3):
896: puts(".Pp");
1.32 schwarze 897: formatcodeln(st, "Em", buf, &start, end, 0);
898: mdoc_newln(st);
1.1 schwarze 899: puts(".Pp");
900: st->haspar = 1;
901: break;
902: case (CMD_HEAD4):
903: puts(".Pp");
1.32 schwarze 904: formatcodeln(st, "No", buf, &start, end, 0);
905: mdoc_newln(st);
1.1 schwarze 906: puts(".Pp");
907: st->haspar = 1;
908: break;
909: case (CMD_OVER):
1.4 schwarze 910: /*
911: * If we have an existing list that hasn't had an =item
912: * yet, then make sure that we open it now.
913: * We use the default list type, but that can't be
914: * helped (we haven't seen any items yet).
1.1 schwarze 915: */
1.4 schwarze 916: if (st->lpos > 0)
917: if (LIST__MAX == st->lstack[st->lpos - 1]) {
918: st->lstack[st->lpos - 1] = LIST_TAG;
919: puts(".Bl -tag -width Ds");
920: }
921: st->lpos++;
922: assert(st->lpos < LIST_STACKSZ);
923: st->lstack[st->lpos - 1] = LIST__MAX;
1.1 schwarze 924: break;
925: case (CMD_ITEM):
1.6 kristaps 926: if (0 == st->lpos) {
927: /*
928: * Bad markup.
929: * Try to compensate.
930: */
931: st->lstack[st->lpos] = LIST__MAX;
932: st->lpos++;
933: }
1.4 schwarze 934: assert(st->lpos > 0);
935: /*
936: * If we're the first =item, guess at what our content
937: * will be: "*" is a bullet list, "1." is a numbered
938: * list, and everything is tagged.
939: */
940: if (LIST__MAX == st->lstack[st->lpos - 1]) {
941: st->lstack[st->lpos - 1] =
942: listguess(buf, start, end);
943: switch (st->lstack[st->lpos - 1]) {
944: case (LIST_BULLET):
945: puts(".Bl -bullet");
946: break;
947: case (LIST_ENUM):
948: puts(".Bl -enum");
949: break;
950: default:
951: puts(".Bl -tag -width Ds");
952: break;
953: }
954: }
955: switch (st->lstack[st->lpos - 1]) {
956: case (LIST_TAG):
1.32 schwarze 957: formatcodeln(st, "It", buf, &start, end, 0);
958: mdoc_newln(st);
1.4 schwarze 959: break;
960: case (LIST_ENUM):
961: /* FALLTHROUGH */
962: case (LIST_BULLET):
963: /*
964: * Abandon the remainder of the paragraph
965: * because we're going to be a bulletted or
966: * numbered list.
967: */
968: puts(".It");
969: break;
970: default:
971: abort();
972: }
1.1 schwarze 973: st->haspar = 1;
974: break;
975: case (CMD_BACK):
1.4 schwarze 976: /* Make sure we don't back over the stack. */
977: if (st->lpos > 0) {
978: st->lpos--;
979: puts(".El");
980: }
1.1 schwarze 981: break;
982: case (CMD_BEGIN):
983: /*
984: * We disregard all types for now.
985: * TODO: process at least "text" in a -literal block.
986: */
987: st->paused = 1;
988: break;
989: case (CMD_FOR):
990: /*
991: * We ignore all types of encodings and formats
992: * unilaterally.
993: */
994: break;
995: case (CMD_ENCODING):
996: break;
997: case (CMD_CUT):
998: st->parsing = 0;
999: return;
1000: default:
1001: abort();
1002: }
1003:
1004: /* Any command (but =cut) makes us start parsing. */
1005: st->parsing = 1;
1006: }
1007:
1008: /*
1009: * Just pump out the line in a verbatim block.
1.32 schwarze 1010: * From the perspective of external callers,
1011: * always stays in OUST_NL/wantws mode.
1.1 schwarze 1012: */
1013: static void
1.35 schwarze 1014: verbatim(struct state *st, char *buf, size_t start, size_t end)
1.1 schwarze 1015: {
1.36 schwarze 1016: size_t i, ift, ifo, ifa, ifc, inl;
1.35 schwarze 1017: char *cp;
1.36 schwarze 1018: int nopen;
1.1 schwarze 1019:
1.35 schwarze 1020: if ( ! st->parsing || st->paused || start == end)
1.1 schwarze 1021: return;
1.22 kristaps 1022: again:
1023: /*
1024: * If we're in the SYNOPSIS, see if we're an #include block.
1025: * If we are, then print the "In" macro and re-loop.
1026: * This handles any number of inclusions, but only when they
1027: * come before the remaining parts...
1028: */
1029: if (SECT_SYNOPSIS == st->sect) {
1030: i = start;
1.35 schwarze 1031: while (i < end && buf[i] == ' ')
1032: i++;
1.22 kristaps 1033: if (i == end)
1034: return;
1.35 schwarze 1035:
1.22 kristaps 1036: /* We're an include block! */
1037: if (end - i > 10 &&
1038: 0 == memcmp(&buf[i], "#include <", 10)) {
1039: start = i + 10;
1040: while (start < end && ' ' == buf[start])
1041: start++;
1042: fputs(".In ", stdout);
1043: /* Stop til the '>' marker or we hit eoln. */
1044: while (start < end &&
1045: '>' != buf[start] && '\n' != buf[start])
1046: putchar(buf[start++]);
1047: putchar('\n');
1048: if (start < end && '>' == buf[start])
1049: start++;
1050: if (start < end && '\n' == buf[start])
1051: start++;
1052: if (start < end)
1053: goto again;
1054: return;
1055: }
1.35 schwarze 1056:
1057: /* Parse function declaration. */
1058: ifo = ifa = ifc = 0;
1.36 schwarze 1059: inl = end;
1060: nopen = 0;
1061: for (ift = i; i < end; i++) {
1062: if (ifc) {
1063: if (buf[i] != '\n')
1064: continue;
1065: inl = i;
1066: break;
1067: }
1068: switch (buf[i]) {
1069: case ' ':
1070: if ( ! ifa)
1071: ifo = i;
1072: break;
1073: case '(':
1074: if (ifo) {
1075: nopen++;
1076: if ( ! ifa)
1077: ifa = i;
1078: } else
1079: i = end;
1080: break;
1081: case ')':
1082: switch (nopen) {
1083: case 0:
1084: i = end;
1085: break;
1086: case 1:
1.35 schwarze 1087: ifc = i;
1.36 schwarze 1088: break;
1089: default:
1090: nopen--;
1091: break;
1092: }
1093: break;
1094: default:
1095: break;
1096: }
1.35 schwarze 1097: }
1098:
1099: /* Encode function declaration. */
1100: if (ifc) {
1.36 schwarze 1101: for (i = ifa; i < ifc; i++)
1102: if (buf[i] == '\n')
1103: buf[i] = ' ';
1.35 schwarze 1104: buf[ifo++] = '\0';
1105: printf(".Ft %s", buf + ift);
1106: if (buf[ifo] == '*') {
1107: fputs(" *", stdout);
1108: ifo++;
1109: }
1110: putchar('\n');
1111: buf[ifa++] = '\0';
1112: printf(".Fo %s\n", buf + ifo);
1.37 ! schwarze 1113: dict_put(buf + ifo, MDOC_Fo);
1.35 schwarze 1114: buf[ifc++] = '\0';
1115: for (;;) {
1116: cp = strchr(buf + ifa, ',');
1117: if (cp != NULL)
1.36 schwarze 1118: *cp++ = '\0';
1.35 schwarze 1119: printf(".Fa \"%s\"\n", buf + ifa);
1120: if (cp == NULL)
1121: break;
1.36 schwarze 1122: while (*cp == ' ')
1123: cp++;
1124: ifa = cp - buf;
1.35 schwarze 1125: }
1126: puts(".Fc");
1127: if (buf[ifc] == ';')
1128: ifc++;
1.36 schwarze 1129: if (ifc < inl) {
1130: buf[inl] = '\0';
1.35 schwarze 1131: puts(buf + ifc);
1132: }
1.36 schwarze 1133: start = inl + 1;
1.35 schwarze 1134: if (start < end)
1135: goto again;
1136: return;
1137: }
1.22 kristaps 1138: }
1139:
1.1 schwarze 1140: puts(".Bd -literal");
1.8 kristaps 1141: for (last = ' '; start < end; start++) {
1142: /*
1143: * Handle accidental macros (newline starting with
1144: * control character) and escapes.
1145: */
1146: if ('\n' == last)
1.7 kristaps 1147: if ('.' == buf[start] || '\'' == buf[start])
1148: printf("\\&");
1.8 kristaps 1149: putchar(last = buf[start]);
1150: if ('\\' == buf[start])
1151: printf("e");
1.7 kristaps 1152: }
1.31 schwarze 1153: putchar(last = '\n');
1.1 schwarze 1154: puts(".Ed");
1155: }
1156:
1157: /*
1.13 kristaps 1158: * See dosynopsisop().
1159: */
1160: static int
1161: hasmatch(const char *buf, size_t start, size_t end)
1162: {
1163: size_t stack;
1164:
1165: for (stack = 0; start < end; start++)
1166: if (buf[start] == '[')
1167: stack++;
1168: else if (buf[start] == ']' && 0 == stack)
1169: return(1);
1170: else if (buf[start] == ']')
1171: stack--;
1172: return(0);
1173: }
1174:
1175: /*
1176: * If we're in the SYNOPSIS section and we've encounter braces in an
1177: * ordinary paragraph, then try to see whether we're an [-option].
1178: * Do this, if we're an opening bracket, by first seeing if we have a
1179: * matching end via hasmatch().
1180: * If we're an ending bracket, see if we have a stack already.
1181: */
1182: static int
1.32 schwarze 1183: dosynopsisop(struct state *st, const char *buf,
1184: size_t *start, size_t end, size_t *opstack)
1.13 kristaps 1185: {
1186:
1187: assert('[' == buf[*start] || ']' == buf[*start]);
1188:
1189: if ('[' == buf[*start] && hasmatch(buf, *start + 1, end)) {
1.32 schwarze 1190: mdoc_newln(st);
1.13 kristaps 1191: puts(".Oo");
1192: (*opstack)++;
1193: } else if ('[' == buf[*start])
1194: return(0);
1195:
1196: if (']' == buf[*start] && *opstack > 0) {
1.32 schwarze 1197: mdoc_newln(st);
1.13 kristaps 1198: puts(".Oc");
1199: (*opstack)--;
1200: } else if (']' == buf[*start])
1201: return(0);
1202:
1203: (*start)++;
1.31 schwarze 1204: last = '\n';
1.13 kristaps 1205: while (' ' == buf[*start])
1206: (*start)++;
1207: return(1);
1208: }
1209:
1210: /*
1.17 kristaps 1211: * Format multiple "Nm" manpage names in the NAME section.
1.32 schwarze 1212: * From the perspective of external callers,
1213: * always stays in OUST_NL/wantws mode,
1214: * but its children do use OUST_MAC.
1.17 kristaps 1215: */
1216: static void
1217: donamenm(struct state *st, const char *buf, size_t *start, size_t end)
1218: {
1219: size_t word;
1220:
1.32 schwarze 1221: assert(OUST_NL == st->oust);
1222: assert(st->wantws);
1223:
1.17 kristaps 1224: while (*start < end && ' ' == buf[*start])
1225: (*start)++;
1226:
1227: if (end == *start) {
1228: puts(".Nm unknown");
1229: return;
1230: }
1231:
1232: while (*start < end) {
1233: for (word = *start; word < end; word++)
1234: if (',' == buf[word])
1235: break;
1.32 schwarze 1236: formatcodeln(st, "Nm", buf, start, word, 1);
1.17 kristaps 1237: if (*start == end) {
1.32 schwarze 1238: mdoc_newln(st);
1239: break;
1.17 kristaps 1240: }
1241: assert(',' == buf[*start]);
1.32 schwarze 1242: printf(" ,");
1243: mdoc_newln(st);
1.17 kristaps 1244: (*start)++;
1245: while (*start < end && ' ' == buf[*start])
1246: (*start)++;
1247: }
1248: }
1249:
1250: /*
1.1 schwarze 1251: * Ordinary paragraph.
1252: * Well, this is really the hardest--POD seems to assume that, for
1253: * example, a leading space implies a newline, and so on.
1254: * Lots of other snakes in the grass: escaping a newline followed by a
1255: * period (accidental mdoc(7) control), double-newlines after macro
1256: * passages, etc.
1.32 schwarze 1257: *
1258: * Uses formatcode() to go to OUST_MAC mode
1259: * and outbuf_flush() to go to OUST_TXT mode.
1260: * Main text mode wantws handling is in this function.
1261: * Must make sure to go back to OUST_NL/wantws mode before returning.
1.1 schwarze 1262: */
1263: static void
1264: ordinary(struct state *st, const char *buf, size_t start, size_t end)
1265: {
1.13 kristaps 1266: size_t i, j, opstack;
1.15 kristaps 1267: int seq;
1.1 schwarze 1268:
1269: if ( ! st->parsing || st->paused)
1270: return;
1271:
1272: /*
1273: * Special-case: the NAME section.
1274: * If we find a "-" when searching from the end, assume that
1275: * we're in "name - description" format.
1276: * To wit, print out a "Nm" and "Nd" in that format.
1277: */
1.11 kristaps 1278: if (SECT_NAME == st->sect) {
1.15 kristaps 1279: for (i = end - 2; i > start; i--)
1280: if ('-' == buf[i] && ' ' == buf[i + 1])
1.1 schwarze 1281: break;
1282: if ('-' == buf[i]) {
1283: j = i;
1284: /* Roll over multiple "-". */
1285: for ( ; i > start; i--)
1286: if ('-' != buf[i])
1287: break;
1.17 kristaps 1288: donamenm(st, buf, &start, i + 1);
1.5 kristaps 1289: start = j + 1;
1.17 kristaps 1290: while (start < end && ' ' == buf[start])
1291: start++;
1.32 schwarze 1292: formatcodeln(st, "Nd", buf, &start, end, 1);
1293: mdoc_newln(st);
1.1 schwarze 1294: return;
1295: }
1296: }
1297:
1298: if ( ! st->haspar)
1299: puts(".Pp");
1300:
1301: st->haspar = 0;
1302: last = '\n';
1.13 kristaps 1303: opstack = 0;
1.1 schwarze 1304:
1.15 kristaps 1305: for (seq = 0; start < end; seq++) {
1.1 schwarze 1306: /*
1307: * Loop til we get either to a newline or escape.
1308: * Escape initial control characters.
1309: */
1310: while (start < end) {
1.34 schwarze 1311: if (start < end - 1 && '<' == buf[start + 1] &&
1312: 'A' <= buf[start] && 'Z' >= buf[start])
1.1 schwarze 1313: break;
1314: else if ('\n' == buf[start])
1315: break;
1316: else if ('\n' == last && '.' == buf[start])
1.31 schwarze 1317: outbuf_addstr(st, "\\&");
1.1 schwarze 1318: else if ('\n' == last && '\'' == buf[start])
1.31 schwarze 1319: outbuf_addstr(st, "\\&");
1.12 kristaps 1320: /*
1321: * If we're in the SYNOPSIS, have square
1322: * brackets indicate that we're opening and
1323: * closing an optional context.
1324: */
1.32 schwarze 1325:
1.13 kristaps 1326: if (SECT_SYNOPSIS == st->sect &&
1327: ('[' == buf[start] ||
1328: ']' == buf[start]) &&
1.32 schwarze 1329: dosynopsisop(st, buf,
1330: &start, end, &opstack))
1.13 kristaps 1331: continue;
1.32 schwarze 1332:
1333: /*
1334: * On whitespace, flush the output buffer
1335: * and allow breaking to a macro line.
1336: * Otherwise, buffer text and clear wantws.
1337: */
1338:
1.31 schwarze 1339: last = buf[start++];
1.37 ! schwarze 1340: if (' ' != last) {
! 1341: outbuf_addchar(st);
! 1342: continue;
! 1343: }
! 1344:
! 1345: if ( ! strcmp(st->outbuf + st->outbuflen - 2, "()") &&
! 1346: dict_get(st->outbuf, st->outbuflen - 2) ==
! 1347: MDOC_Fo) {
! 1348: st->outbuflen -= 2;
! 1349: st->outbuf[st->outbuflen] = '\0';
! 1350: mdoc_newln(st);
! 1351: fputs(".Fn ", stdout);
1.31 schwarze 1352: outbuf_flush(st);
1.37 ! schwarze 1353: mdoc_newln(st);
! 1354: continue;
! 1355: }
! 1356:
! 1357: outbuf_flush(st);
! 1358: putchar(' ');
! 1359: st->wantws = 1;
1.1 schwarze 1360: }
1361:
1.34 schwarze 1362: if (start < end - 1 && '<' == buf[start + 1] &&
1363: 'A' <= buf[start] && 'Z' >= buf[start]) {
1.32 schwarze 1364: formatcode(st, buf, &start, end, 0, seq);
1365: if (OUST_MAC == st->oust) {
1.30 schwarze 1366: /*
1367: * Let mdoc(7) handle trailing punctuation.
1368: * XXX Some punctuation characters
1369: * are not handled yet.
1370: */
1.16 kristaps 1371: if ((start == end - 1 ||
1372: (start < end - 1 &&
1373: (' ' == buf[start + 1] ||
1374: '\n' == buf[start + 1]))) &&
1375: ('.' == buf[start] ||
1376: ',' == buf[start])) {
1377: putchar(' ');
1378: putchar(buf[start++]);
1379: }
1.32 schwarze 1380:
1381: if (st->wantws ||
1382: ' ' == buf[start] ||
1383: '\n' == buf[start])
1384: mdoc_newln(st);
1385:
1.30 schwarze 1386: /*
1387: * Consume all whitespace
1388: * so we don't accidentally start
1389: * an implicit literal line.
1390: */
1.32 schwarze 1391:
1.6 kristaps 1392: while (start < end && ' ' == buf[start])
1393: start++;
1.32 schwarze 1394:
1395: /*
1396: * Some text is following.
1397: * Implement requested spacing.
1398: */
1399:
1400: if ( ! st->wantws && start < end &&
1.34 schwarze 1401: ('<' != buf[start + 1] ||
1402: 'A' > buf[start] ||
1403: 'Z' < buf[start])) {
1.32 schwarze 1404: printf(" Ns ");
1405: st->wantws = 1;
1406: }
1.6 kristaps 1407: }
1.1 schwarze 1408: } else if (start < end && '\n' == buf[start]) {
1.32 schwarze 1409: outbuf_flush(st);
1410: mdoc_newln(st);
1.1 schwarze 1411: if (++start >= end)
1412: continue;
1413: /*
1414: * If we have whitespace next, eat it to prevent
1415: * mdoc(7) from thinking that it's meant for
1416: * verbatim text.
1417: * It is--but if we start with that, we can't
1418: * have a macro subsequent it, which may be
1419: * possible if we have an escape next.
1420: */
1.31 schwarze 1421: if (' ' == buf[start] || '\t' == buf[start])
1.1 schwarze 1422: puts(".br");
1423: for ( ; start < end; start++)
1424: if (' ' != buf[start] && '\t' != buf[start])
1425: break;
1.12 kristaps 1426: }
1.1 schwarze 1427: }
1.32 schwarze 1428: outbuf_flush(st);
1429: mdoc_newln(st);
1.1 schwarze 1430: }
1431:
1432: /*
1433: * There are three kinds of paragraphs: verbatim (starts with whitespace
1434: * of some sort), ordinary (starts without "=" marker), or a command
1435: * (default: starts with "=").
1436: */
1437: static void
1.35 schwarze 1438: dopar(struct state *st, char *buf, size_t start, size_t end)
1.1 schwarze 1439: {
1440:
1.32 schwarze 1441: assert(OUST_NL == st->oust);
1442: assert(st->wantws);
1443:
1.1 schwarze 1444: if (end == start)
1445: return;
1446: if (' ' == buf[start] || '\t' == buf[start])
1447: verbatim(st, buf, start, end);
1448: else if ('=' != buf[start])
1449: ordinary(st, buf, start, end);
1450: else
1451: command(st, buf, start, end);
1452: }
1453:
1454: /*
1455: * Loop around paragraphs within a document, processing each one in the
1456: * POD way.
1457: */
1458: static void
1459: dofile(const struct args *args, const char *fname,
1.35 schwarze 1460: const struct tm *tm, char *buf, size_t sz)
1.1 schwarze 1461: {
1.29 schwarze 1462: char datebuf[64];
1.1 schwarze 1463: struct state st;
1.29 schwarze 1464: const char *fbase, *fext, *section, *date;
1.1 schwarze 1465: char *title, *cp;
1.29 schwarze 1466: size_t sup, end, i, cur = 0;
1.1 schwarze 1467:
1468: if (0 == sz)
1469: return;
1470:
1.29 schwarze 1471: /*
1472: * Parsing the filename is almost always required,
1473: * except when both the title and the section
1474: * are provided on the command line.
1475: */
1476:
1477: if (NULL == args->title || NULL == args->section) {
1478: fbase = strrchr(fname, '/');
1479: if (NULL == fbase)
1480: fbase = fname;
1481: else
1482: fbase++;
1483: fext = strrchr(fbase, '.');
1484: } else
1485: fext = NULL;
1486:
1487: /*
1488: * The title will be converted to uppercase,
1489: * so it needs to be copied.
1490: */
1491:
1492: title = (NULL != args->title) ? strdup(args->title) :
1493: (NULL != fext) ? strndup(fbase, fext - fbase) :
1494: strdup(fbase);
1.1 schwarze 1495:
1496: if (NULL == title) {
1497: perror(NULL);
1498: exit(EXIT_FAILURE);
1499: }
1500:
1501: /* Section is 1 unless suffix is "pm". */
1502:
1.29 schwarze 1503: section = (NULL != args->section) ? args->section :
1504: (NULL == fext || strcmp(fext + 1, "pm")) ? "1" :
1505: PERL_SECTION;
1.1 schwarze 1506:
1507: /* Date. Or the given "tm" if not supplied. */
1508:
1509: if (NULL == (date = args->date)) {
1510: strftime(datebuf, sizeof(datebuf), "%B %d, %Y", tm);
1511: date = datebuf;
1512: }
1513:
1514: for (cp = title; '\0' != *cp; cp++)
1515: *cp = toupper((int)*cp);
1516:
1517: /* The usual mdoc(7) preamble. */
1518:
1519: printf(".Dd %s\n", date);
1520: printf(".Dt %s %s\n", title, section);
1521: puts(".Os");
1522:
1523: free(title);
1524:
1.37 ! schwarze 1525: dict_init();
1.1 schwarze 1526: memset(&st, 0, sizeof(struct state));
1.32 schwarze 1527: st.oust = OUST_NL;
1528: st.wantws = 1;
1529:
1.1 schwarze 1530: assert(sz > 0);
1531:
1532: /* Main loop over file contents. */
1533:
1534: while (cur < sz) {
1535: /* Read until next paragraph. */
1536: for (i = cur + 1; i < sz; i++)
1537: if ('\n' == buf[i] && '\n' == buf[i - 1]) {
1538: /* Consume blank paragraphs. */
1539: while (i + 1 < sz && '\n' == buf[i + 1])
1540: i++;
1541: break;
1542: }
1543:
1544: /* Adjust end marker for EOF. */
1545: end = i < sz ? i - 1 :
1546: ('\n' == buf[sz - 1] ? sz - 1 : sz);
1547: sup = i < sz ? end + 2 : sz;
1548:
1549: /* Process paragraph and adjust start. */
1550: dopar(&st, buf, cur, end);
1551: cur = sup;
1552: }
1.37 ! schwarze 1553: dict_destroy();
1.1 schwarze 1554: }
1555:
1556: /*
1557: * Read a single file fully into memory.
1558: * If the file is "-", do it from stdin.
1559: * If successfully read, send the input buffer to dofile() for further
1560: * processing.
1561: */
1562: static int
1563: readfile(const struct args *args, const char *fname)
1564: {
1565: int fd;
1566: char *buf;
1567: size_t bufsz, cur;
1568: ssize_t ssz;
1569: struct tm *tm;
1570: time_t ttm;
1571: struct stat st;
1572:
1573: fd = 0 != strcmp("-", fname) ?
1574: open(fname, O_RDONLY, 0) : STDIN_FILENO;
1575:
1576: if (-1 == fd) {
1577: perror(fname);
1578: return(0);
1579: }
1580:
1581: if (STDIN_FILENO == fd || -1 == fstat(fd, &st)) {
1582: ttm = time(NULL);
1583: tm = localtime(&ttm);
1584: } else
1585: tm = localtime(&st.st_mtime);
1586:
1587: /*
1588: * Arbitrarily-sized initial buffer.
1589: * Should be big enough for most files...
1590: */
1591: cur = 0;
1592: bufsz = 1 << 14;
1593: if (NULL == (buf = malloc(bufsz))) {
1594: perror(NULL);
1595: exit(EXIT_FAILURE);
1596: }
1597:
1598: while ((ssz = read(fd, buf + cur, bufsz - cur)) > 0) {
1599: /* Double buffer size on fill. */
1600: if ((size_t)ssz == bufsz - cur) {
1601: bufsz *= 2;
1602: if (NULL == (buf = realloc(buf, bufsz))) {
1603: perror(NULL);
1604: exit(EXIT_FAILURE);
1605: }
1606: }
1607: cur += (size_t)ssz;
1608: }
1609: if (ssz < 0) {
1610: perror(fname);
1611: free(buf);
1612: return(0);
1613: }
1614:
1615: dofile(args, STDIN_FILENO == fd ?
1616: "STDIN" : fname, tm, buf, cur);
1617: free(buf);
1618: if (STDIN_FILENO != fd)
1619: close(fd);
1620: return(1);
1621: }
1622:
1623: int
1624: main(int argc, char *argv[])
1625: {
1626: const char *fname, *name;
1627: struct args args;
1628: int c;
1629:
1630: name = strrchr(argv[0], '/');
1631: if (name == NULL)
1632: name = argv[0];
1633: else
1634: ++name;
1635:
1636: memset(&args, 0, sizeof(struct args));
1637: fname = "-";
1638:
1639: /* Accept no arguments for now. */
1640:
1641: while (-1 != (c = getopt(argc, argv, "c:d:hln:oq:rs:uv")))
1642: switch (c) {
1643: case ('h'):
1644: /* FALLTHROUGH */
1645: case ('l'):
1646: /* FALLTHROUGH */
1647: case ('c'):
1648: /* FALLTHROUGH */
1649: case ('o'):
1650: /* FALLTHROUGH */
1651: case ('q'):
1652: /* FALLTHROUGH */
1653: case ('r'):
1654: /* FALLTHROUGH */
1655: case ('u'):
1656: /* FALLTHROUGH */
1657: case ('v'):
1658: /* Ignore these. */
1659: break;
1660: case ('d'):
1661: args.date = optarg;
1662: break;
1663: case ('n'):
1664: args.title = optarg;
1665: break;
1666: case ('s'):
1667: args.section = optarg;
1668: break;
1669: default:
1670: goto usage;
1671: }
1672:
1673: argc -= optind;
1674: argv += optind;
1675:
1676: /* Accept only a single input file. */
1677:
1.25 schwarze 1678: if (argc > 1)
1679: goto usage;
1.1 schwarze 1680: else if (1 == argc)
1681: fname = *argv;
1682:
1683: return(readfile(&args, fname) ?
1684: EXIT_SUCCESS : EXIT_FAILURE);
1685:
1686: usage:
1687: fprintf(stderr, "usage: %s [-d date] "
1.25 schwarze 1688: "[-n title] [-s section] [file]\n", name);
1.1 schwarze 1689:
1690: return(EXIT_FAILURE);
1691: }
CVSweb