Annotation of pod2mdoc/pod2mdoc.c, Revision 1.38
1.38 ! schwarze 1: /* $Id: pod2mdoc.c,v 1.37 2015/02/13 00:44:16 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] ||
1.38 ! schwarze 618: '>' == buf[*start + 4])) {
1.27 schwarze 619: printf("Dv ");
1.38 ! schwarze 620: break;
! 621: }
! 622: i = 0;
! 623: while (isalnum((unsigned char)buf[*start + i]) ||
! 624: '_' == buf[*start + i])
! 625: i++;
! 626: if (i && MDOC_Fa == dict_get(buf + *start, i))
! 627: printf("Fa ");
1.27 schwarze 628: else
629: printf("Sy ");
1.1 schwarze 630: break;
631: case (FMT_CODE):
1.2 schwarze 632: printf("Qo Li ");
1.1 schwarze 633: break;
634: case (FMT_LINK):
1.19 kristaps 635: /* Try to link; use "No" if it's empty. */
1.9 kristaps 636: if ( ! trylink(buf, start, end, dsz))
637: printf("No ");
1.1 schwarze 638: break;
639: case (FMT_FILE):
640: printf("Pa ");
641: break;
642: case (FMT_NBSP):
643: printf("No ");
644: break;
645: default:
646: abort();
647: }
1.32 schwarze 648: st->oust = OUST_MAC;
649: st->wantws = 1;
1.31 schwarze 650: } else
651: outbuf_flush(st);
1.1 schwarze 652:
653: /*
1.6 kristaps 654: * Process until we reach the end marker (e.g., '>') or until we
1.5 kristaps 655: * find a nested format code.
1.1 schwarze 656: * Don't emit any newlines: since we're on a macro line, we
657: * don't want to break the line.
658: */
659: while (*start < end) {
1.5 kristaps 660: if ('>' == buf[*start] && 1 == dsz) {
1.1 schwarze 661: (*start)++;
662: break;
1.5 kristaps 663: } else if ('>' == buf[*start] &&
664: ' ' == buf[*start - 1]) {
665: /*
666: * Handle custom delimiters.
667: * These require a certain number of
668: * space-preceded carrots before we're really at
669: * the end.
670: */
671: i = *start;
672: for (j = 0; i < end && j < dsz; j++)
673: if ('>' != buf[i++])
674: break;
675: if (dsz == j) {
676: *start += dsz;
677: break;
678: }
1.1 schwarze 679: }
1.34 schwarze 680: if (*start + 1 < end && '<' == buf[*start + 1] &&
681: 'A' <= buf[*start] && 'Z' >= buf[*start]) {
1.32 schwarze 682: formatcode(st, buf, start, end, nomacro, 1);
1.1 schwarze 683: continue;
684: }
1.3 schwarze 685:
1.32 schwarze 686: /* Suppress newlines and multiple spaces. */
687:
688: last = buf[(*start)++];
689: if (' ' == last || '\n' == last) {
690: putchar(' ');
691: while (*start < end && ' ' == buf[*start])
692: (*start)++;
693: continue;
694: }
695:
1.33 schwarze 696: if (OUST_MAC == st->oust && FMT__MAX != fmt) {
1.32 schwarze 697: if ( ! st->wantws) {
698: printf(" Ns ");
699: st->wantws = 1;
700: }
701:
702: /*
703: * Escape macro-like words.
704: * This matches "Xx " and "XxEOLN".
705: */
706:
707: if (end - *start > 0 &&
708: isupper((unsigned char)last) &&
709: islower((unsigned char)buf[*start]) &&
710: (end - *start == 1 ||
711: ' ' == buf[*start + 1] ||
712: '>' == buf[*start + 1]))
713: printf("\\&");
714: }
1.3 schwarze 715:
1.32 schwarze 716: putchar(last);
1.4 schwarze 717:
1.8 kristaps 718: /* Protect against character escapes. */
1.32 schwarze 719:
1.8 kristaps 720: if ('\\' == last)
721: putchar('e');
1.1 schwarze 722: }
1.2 schwarze 723:
1.33 schwarze 724: if (FMT__MAX == fmt)
725: return(0);
726:
1.2 schwarze 727: if ( ! nomacro && FMT_CODE == fmt)
728: printf(" Qc ");
1.1 schwarze 729:
1.33 schwarze 730: st->wantws = ' ' == last;
731: return(1);
1.1 schwarze 732: }
733:
734: /*
735: * Calls formatcode() til the end of a paragraph.
1.32 schwarze 736: * Goes to OUST_MAC mode and stays there when returning,
737: * such that the caller can add arguments to the macro line
738: * before closing it out.
1.1 schwarze 739: */
740: static void
1.32 schwarze 741: formatcodeln(struct state *st, const char *linemac,
742: const char *buf, size_t *start, size_t end, int nomacro)
1.1 schwarze 743: {
1.33 schwarze 744: int gotmacro, wantws;
1.1 schwarze 745:
1.32 schwarze 746: assert(OUST_NL == st->oust);
747: assert(st->wantws);
748: printf(".%s ", linemac);
749: st->oust = OUST_MAC;
750:
1.33 schwarze 751: gotmacro = 0;
1.1 schwarze 752: while (*start < end) {
1.33 schwarze 753: wantws = ' ' == buf[*start] || '\n' == buf[*start];
754: if (wantws) {
755: last = ' ';
756: do {
757: (*start)++;
758: } while (*start < end && ' ' == buf[*start]);
759: }
760:
1.34 schwarze 761: if (*start + 1 < end && '<' == buf[*start + 1] &&
762: 'A' <= buf[*start] && 'Z' >= buf[*start]) {
1.33 schwarze 763: st->wantws |= wantws;
764: gotmacro = formatcode(st, buf,
765: start, end, nomacro, 1);
1.1 schwarze 766: continue;
767: }
1.32 schwarze 768:
1.33 schwarze 769: if (gotmacro) {
770: if (*start < end || st->outbuflen) {
771: if (st->wantws ||
772: (wantws && !st->outbuflen))
773: printf(" No ");
774: else
775: printf(" Ns ");
776: }
777: gotmacro = 0;
778: }
779: outbuf_flush(st);
780: st->wantws = wantws;
781:
782: if (*start >= end)
783: break;
784:
785: if (st->wantws) {
786: putchar(' ');
787: st->wantws = 0;
1.32 schwarze 788: }
789:
1.4 schwarze 790: /*
791: * Since we're already on a macro line, we want to make
792: * sure that we don't inadvertently invoke a macro.
793: * We need to do this carefully because section names
794: * are used in troff and we don't want to escape
795: * something that needn't be escaped.
796: */
797: if (' ' == last && end - *start > 1 &&
1.33 schwarze 798: isupper((unsigned char)buf[*start]) &&
799: islower((unsigned char)buf[*start + 1]) &&
800: (end - *start == 2 || ' ' == buf[*start + 2]))
1.4 schwarze 801: printf("\\&");
802:
1.33 schwarze 803: putchar(last = buf[*start]);
1.8 kristaps 804:
805: /* Protect against character escapes. */
1.33 schwarze 806:
1.8 kristaps 807: if ('\\' == last)
808: putchar('e');
809:
1.1 schwarze 810: (*start)++;
811: }
812: }
813:
814: /*
1.4 schwarze 815: * Guess at what kind of list we are.
816: * These are taken straight from the POD manual.
817: * I don't know what people do in real life.
818: */
819: static enum list
820: listguess(const char *buf, size_t start, size_t end)
821: {
822: size_t len = end - start;
823:
824: assert(end >= start);
825:
826: if (len == 1 && '*' == buf[start])
827: return(LIST_BULLET);
828: if (len == 2 && '1' == buf[start] && '.' == buf[start + 1])
829: return(LIST_ENUM);
830: else if (len == 1 && '1' == buf[start])
831: return(LIST_ENUM);
832: else
833: return(LIST_TAG);
834: }
835:
836: /*
1.1 schwarze 837: * A command paragraph, as noted in the perlpod manual, just indicates
838: * that we should do something, optionally with some text to print as
839: * well.
1.32 schwarze 840: * From the perspective of external callers,
841: * always stays in OUST_NL/wantws mode,
842: * but its children do use OUST_MAC.
1.1 schwarze 843: */
844: static void
845: command(struct state *st, const char *buf, size_t start, size_t end)
846: {
847: size_t len, csz;
848: enum cmd cmd;
849:
850: assert('=' == buf[start]);
851: start++;
852: len = end - start;
853:
854: for (cmd = 0; cmd < CMD__MAX; cmd++) {
855: csz = strlen(cmds[cmd]);
856: if (len < csz)
857: continue;
858: if (0 == memcmp(&buf[start], cmd[cmds], csz))
859: break;
860: }
861:
862: /* Ignore bogus commands. */
863:
864: if (CMD__MAX == cmd)
865: return;
866:
867: start += csz;
1.8 kristaps 868: while (start < end && ' ' == buf[start])
869: start++;
870:
1.1 schwarze 871: len = end - start;
872:
873: if (st->paused) {
874: st->paused = CMD_END != cmd;
875: return;
876: }
877:
878: switch (cmd) {
879: case (CMD_POD):
880: break;
881: case (CMD_HEAD1):
882: /*
883: * The behaviour of head= follows from a quick glance at
884: * how pod2man handles it.
885: */
1.11 kristaps 886: st->sect = SECT_NONE;
887: if (end - start == 4) {
1.1 schwarze 888: if (0 == memcmp(&buf[start], "NAME", 4))
1.11 kristaps 889: st->sect = SECT_NAME;
890: } else if (end - start == 8) {
891: if (0 == memcmp(&buf[start], "SYNOPSIS", 8))
892: st->sect = SECT_SYNOPSIS;
893: }
1.32 schwarze 894: formatcodeln(st, "Sh", buf, &start, end, 1);
895: mdoc_newln(st);
1.1 schwarze 896: st->haspar = 1;
897: break;
898: case (CMD_HEAD2):
1.32 schwarze 899: formatcodeln(st, "Ss", buf, &start, end, 1);
900: mdoc_newln(st);
1.1 schwarze 901: st->haspar = 1;
902: break;
903: case (CMD_HEAD3):
904: puts(".Pp");
1.32 schwarze 905: formatcodeln(st, "Em", buf, &start, end, 0);
906: mdoc_newln(st);
1.1 schwarze 907: puts(".Pp");
908: st->haspar = 1;
909: break;
910: case (CMD_HEAD4):
911: puts(".Pp");
1.32 schwarze 912: formatcodeln(st, "No", buf, &start, end, 0);
913: mdoc_newln(st);
1.1 schwarze 914: puts(".Pp");
915: st->haspar = 1;
916: break;
917: case (CMD_OVER):
1.4 schwarze 918: /*
919: * If we have an existing list that hasn't had an =item
920: * yet, then make sure that we open it now.
921: * We use the default list type, but that can't be
922: * helped (we haven't seen any items yet).
1.1 schwarze 923: */
1.4 schwarze 924: if (st->lpos > 0)
925: if (LIST__MAX == st->lstack[st->lpos - 1]) {
926: st->lstack[st->lpos - 1] = LIST_TAG;
927: puts(".Bl -tag -width Ds");
928: }
929: st->lpos++;
930: assert(st->lpos < LIST_STACKSZ);
931: st->lstack[st->lpos - 1] = LIST__MAX;
1.1 schwarze 932: break;
933: case (CMD_ITEM):
1.6 kristaps 934: if (0 == st->lpos) {
935: /*
936: * Bad markup.
937: * Try to compensate.
938: */
939: st->lstack[st->lpos] = LIST__MAX;
940: st->lpos++;
941: }
1.4 schwarze 942: assert(st->lpos > 0);
943: /*
944: * If we're the first =item, guess at what our content
945: * will be: "*" is a bullet list, "1." is a numbered
946: * list, and everything is tagged.
947: */
948: if (LIST__MAX == st->lstack[st->lpos - 1]) {
949: st->lstack[st->lpos - 1] =
950: listguess(buf, start, end);
951: switch (st->lstack[st->lpos - 1]) {
952: case (LIST_BULLET):
953: puts(".Bl -bullet");
954: break;
955: case (LIST_ENUM):
956: puts(".Bl -enum");
957: break;
958: default:
959: puts(".Bl -tag -width Ds");
960: break;
961: }
962: }
963: switch (st->lstack[st->lpos - 1]) {
964: case (LIST_TAG):
1.32 schwarze 965: formatcodeln(st, "It", buf, &start, end, 0);
966: mdoc_newln(st);
1.4 schwarze 967: break;
968: case (LIST_ENUM):
969: /* FALLTHROUGH */
970: case (LIST_BULLET):
971: /*
972: * Abandon the remainder of the paragraph
973: * because we're going to be a bulletted or
974: * numbered list.
975: */
976: puts(".It");
977: break;
978: default:
979: abort();
980: }
1.1 schwarze 981: st->haspar = 1;
982: break;
983: case (CMD_BACK):
1.4 schwarze 984: /* Make sure we don't back over the stack. */
985: if (st->lpos > 0) {
986: st->lpos--;
987: puts(".El");
988: }
1.1 schwarze 989: break;
990: case (CMD_BEGIN):
991: /*
992: * We disregard all types for now.
993: * TODO: process at least "text" in a -literal block.
994: */
995: st->paused = 1;
996: break;
997: case (CMD_FOR):
998: /*
999: * We ignore all types of encodings and formats
1000: * unilaterally.
1001: */
1002: break;
1003: case (CMD_ENCODING):
1004: break;
1005: case (CMD_CUT):
1006: st->parsing = 0;
1007: return;
1008: default:
1009: abort();
1010: }
1011:
1012: /* Any command (but =cut) makes us start parsing. */
1013: st->parsing = 1;
1014: }
1015:
1016: /*
1017: * Just pump out the line in a verbatim block.
1.32 schwarze 1018: * From the perspective of external callers,
1019: * always stays in OUST_NL/wantws mode.
1.1 schwarze 1020: */
1021: static void
1.35 schwarze 1022: verbatim(struct state *st, char *buf, size_t start, size_t end)
1.1 schwarze 1023: {
1.36 schwarze 1024: size_t i, ift, ifo, ifa, ifc, inl;
1.38 ! schwarze 1025: char *cp, *cp2;
1.36 schwarze 1026: int nopen;
1.1 schwarze 1027:
1.35 schwarze 1028: if ( ! st->parsing || st->paused || start == end)
1.1 schwarze 1029: return;
1.22 kristaps 1030: again:
1031: /*
1032: * If we're in the SYNOPSIS, see if we're an #include block.
1033: * If we are, then print the "In" macro and re-loop.
1034: * This handles any number of inclusions, but only when they
1035: * come before the remaining parts...
1036: */
1037: if (SECT_SYNOPSIS == st->sect) {
1038: i = start;
1.35 schwarze 1039: while (i < end && buf[i] == ' ')
1040: i++;
1.22 kristaps 1041: if (i == end)
1042: return;
1.35 schwarze 1043:
1.22 kristaps 1044: /* We're an include block! */
1045: if (end - i > 10 &&
1046: 0 == memcmp(&buf[i], "#include <", 10)) {
1047: start = i + 10;
1048: while (start < end && ' ' == buf[start])
1049: start++;
1050: fputs(".In ", stdout);
1051: /* Stop til the '>' marker or we hit eoln. */
1052: while (start < end &&
1053: '>' != buf[start] && '\n' != buf[start])
1054: putchar(buf[start++]);
1055: putchar('\n');
1056: if (start < end && '>' == buf[start])
1057: start++;
1058: if (start < end && '\n' == buf[start])
1059: start++;
1060: if (start < end)
1061: goto again;
1062: return;
1063: }
1.35 schwarze 1064:
1065: /* Parse function declaration. */
1066: ifo = ifa = ifc = 0;
1.36 schwarze 1067: inl = end;
1068: nopen = 0;
1069: for (ift = i; i < end; i++) {
1070: if (ifc) {
1071: if (buf[i] != '\n')
1072: continue;
1073: inl = i;
1074: break;
1075: }
1076: switch (buf[i]) {
1077: case ' ':
1078: if ( ! ifa)
1079: ifo = i;
1080: break;
1081: case '(':
1082: if (ifo) {
1083: nopen++;
1084: if ( ! ifa)
1085: ifa = i;
1086: } else
1087: i = end;
1088: break;
1089: case ')':
1090: switch (nopen) {
1091: case 0:
1092: i = end;
1093: break;
1094: case 1:
1.35 schwarze 1095: ifc = i;
1.36 schwarze 1096: break;
1097: default:
1098: nopen--;
1099: break;
1100: }
1101: break;
1102: default:
1103: break;
1104: }
1.35 schwarze 1105: }
1106:
1107: /* Encode function declaration. */
1108: if (ifc) {
1.36 schwarze 1109: for (i = ifa; i < ifc; i++)
1110: if (buf[i] == '\n')
1111: buf[i] = ' ';
1.35 schwarze 1112: buf[ifo++] = '\0';
1113: printf(".Ft %s", buf + ift);
1114: if (buf[ifo] == '*') {
1115: fputs(" *", stdout);
1116: ifo++;
1117: }
1118: putchar('\n');
1119: buf[ifa++] = '\0';
1120: printf(".Fo %s\n", buf + ifo);
1.37 schwarze 1121: dict_put(buf + ifo, MDOC_Fo);
1.35 schwarze 1122: buf[ifc++] = '\0';
1123: for (;;) {
1124: cp = strchr(buf + ifa, ',');
1.38 ! schwarze 1125: if (cp != NULL) {
! 1126: cp2 = cp;
1.36 schwarze 1127: *cp++ = '\0';
1.38 ! schwarze 1128: } else
! 1129: cp2 = strchr(buf + ifa, '\0');
! 1130: while (isalnum((unsigned char)cp2[-1]) ||
! 1131: '_' == cp2[-1])
! 1132: cp2--;
! 1133: if ('\0' != *cp2)
! 1134: dict_put(cp2, MDOC_Fa);
1.35 schwarze 1135: printf(".Fa \"%s\"\n", buf + ifa);
1136: if (cp == NULL)
1137: break;
1.36 schwarze 1138: while (*cp == ' ')
1139: cp++;
1140: ifa = cp - buf;
1.35 schwarze 1141: }
1142: puts(".Fc");
1143: if (buf[ifc] == ';')
1144: ifc++;
1.36 schwarze 1145: if (ifc < inl) {
1146: buf[inl] = '\0';
1.35 schwarze 1147: puts(buf + ifc);
1148: }
1.36 schwarze 1149: start = inl + 1;
1.35 schwarze 1150: if (start < end)
1151: goto again;
1152: return;
1153: }
1.22 kristaps 1154: }
1155:
1.1 schwarze 1156: puts(".Bd -literal");
1.8 kristaps 1157: for (last = ' '; start < end; start++) {
1158: /*
1159: * Handle accidental macros (newline starting with
1160: * control character) and escapes.
1161: */
1162: if ('\n' == last)
1.7 kristaps 1163: if ('.' == buf[start] || '\'' == buf[start])
1164: printf("\\&");
1.8 kristaps 1165: putchar(last = buf[start]);
1166: if ('\\' == buf[start])
1167: printf("e");
1.7 kristaps 1168: }
1.31 schwarze 1169: putchar(last = '\n');
1.1 schwarze 1170: puts(".Ed");
1171: }
1172:
1173: /*
1.13 kristaps 1174: * See dosynopsisop().
1175: */
1176: static int
1177: hasmatch(const char *buf, size_t start, size_t end)
1178: {
1179: size_t stack;
1180:
1181: for (stack = 0; start < end; start++)
1182: if (buf[start] == '[')
1183: stack++;
1184: else if (buf[start] == ']' && 0 == stack)
1185: return(1);
1186: else if (buf[start] == ']')
1187: stack--;
1188: return(0);
1189: }
1190:
1191: /*
1192: * If we're in the SYNOPSIS section and we've encounter braces in an
1193: * ordinary paragraph, then try to see whether we're an [-option].
1194: * Do this, if we're an opening bracket, by first seeing if we have a
1195: * matching end via hasmatch().
1196: * If we're an ending bracket, see if we have a stack already.
1197: */
1198: static int
1.32 schwarze 1199: dosynopsisop(struct state *st, const char *buf,
1200: size_t *start, size_t end, size_t *opstack)
1.13 kristaps 1201: {
1202:
1203: assert('[' == buf[*start] || ']' == buf[*start]);
1204:
1205: if ('[' == buf[*start] && hasmatch(buf, *start + 1, end)) {
1.32 schwarze 1206: mdoc_newln(st);
1.13 kristaps 1207: puts(".Oo");
1208: (*opstack)++;
1209: } else if ('[' == buf[*start])
1210: return(0);
1211:
1212: if (']' == buf[*start] && *opstack > 0) {
1.32 schwarze 1213: mdoc_newln(st);
1.13 kristaps 1214: puts(".Oc");
1215: (*opstack)--;
1216: } else if (']' == buf[*start])
1217: return(0);
1218:
1219: (*start)++;
1.31 schwarze 1220: last = '\n';
1.13 kristaps 1221: while (' ' == buf[*start])
1222: (*start)++;
1223: return(1);
1224: }
1225:
1226: /*
1.17 kristaps 1227: * Format multiple "Nm" manpage names in the NAME section.
1.32 schwarze 1228: * From the perspective of external callers,
1229: * always stays in OUST_NL/wantws mode,
1230: * but its children do use OUST_MAC.
1.17 kristaps 1231: */
1232: static void
1233: donamenm(struct state *st, const char *buf, size_t *start, size_t end)
1234: {
1235: size_t word;
1236:
1.32 schwarze 1237: assert(OUST_NL == st->oust);
1238: assert(st->wantws);
1239:
1.17 kristaps 1240: while (*start < end && ' ' == buf[*start])
1241: (*start)++;
1242:
1243: if (end == *start) {
1244: puts(".Nm unknown");
1245: return;
1246: }
1247:
1248: while (*start < end) {
1249: for (word = *start; word < end; word++)
1250: if (',' == buf[word])
1251: break;
1.32 schwarze 1252: formatcodeln(st, "Nm", buf, start, word, 1);
1.17 kristaps 1253: if (*start == end) {
1.32 schwarze 1254: mdoc_newln(st);
1255: break;
1.17 kristaps 1256: }
1257: assert(',' == buf[*start]);
1.32 schwarze 1258: printf(" ,");
1259: mdoc_newln(st);
1.17 kristaps 1260: (*start)++;
1261: while (*start < end && ' ' == buf[*start])
1262: (*start)++;
1263: }
1264: }
1265:
1266: /*
1.1 schwarze 1267: * Ordinary paragraph.
1268: * Well, this is really the hardest--POD seems to assume that, for
1269: * example, a leading space implies a newline, and so on.
1270: * Lots of other snakes in the grass: escaping a newline followed by a
1271: * period (accidental mdoc(7) control), double-newlines after macro
1272: * passages, etc.
1.32 schwarze 1273: *
1274: * Uses formatcode() to go to OUST_MAC mode
1275: * and outbuf_flush() to go to OUST_TXT mode.
1276: * Main text mode wantws handling is in this function.
1277: * Must make sure to go back to OUST_NL/wantws mode before returning.
1.1 schwarze 1278: */
1279: static void
1280: ordinary(struct state *st, const char *buf, size_t start, size_t end)
1281: {
1.13 kristaps 1282: size_t i, j, opstack;
1.15 kristaps 1283: int seq;
1.1 schwarze 1284:
1285: if ( ! st->parsing || st->paused)
1286: return;
1287:
1288: /*
1289: * Special-case: the NAME section.
1290: * If we find a "-" when searching from the end, assume that
1291: * we're in "name - description" format.
1292: * To wit, print out a "Nm" and "Nd" in that format.
1293: */
1.11 kristaps 1294: if (SECT_NAME == st->sect) {
1.15 kristaps 1295: for (i = end - 2; i > start; i--)
1296: if ('-' == buf[i] && ' ' == buf[i + 1])
1.1 schwarze 1297: break;
1298: if ('-' == buf[i]) {
1299: j = i;
1300: /* Roll over multiple "-". */
1301: for ( ; i > start; i--)
1302: if ('-' != buf[i])
1303: break;
1.17 kristaps 1304: donamenm(st, buf, &start, i + 1);
1.5 kristaps 1305: start = j + 1;
1.17 kristaps 1306: while (start < end && ' ' == buf[start])
1307: start++;
1.32 schwarze 1308: formatcodeln(st, "Nd", buf, &start, end, 1);
1309: mdoc_newln(st);
1.1 schwarze 1310: return;
1311: }
1312: }
1313:
1314: if ( ! st->haspar)
1315: puts(".Pp");
1316:
1317: st->haspar = 0;
1318: last = '\n';
1.13 kristaps 1319: opstack = 0;
1.1 schwarze 1320:
1.15 kristaps 1321: for (seq = 0; start < end; seq++) {
1.1 schwarze 1322: /*
1323: * Loop til we get either to a newline or escape.
1324: * Escape initial control characters.
1325: */
1326: while (start < end) {
1.34 schwarze 1327: if (start < end - 1 && '<' == buf[start + 1] &&
1328: 'A' <= buf[start] && 'Z' >= buf[start])
1.1 schwarze 1329: break;
1330: else if ('\n' == buf[start])
1331: break;
1332: else if ('\n' == last && '.' == buf[start])
1.31 schwarze 1333: outbuf_addstr(st, "\\&");
1.1 schwarze 1334: else if ('\n' == last && '\'' == buf[start])
1.31 schwarze 1335: outbuf_addstr(st, "\\&");
1.12 kristaps 1336: /*
1337: * If we're in the SYNOPSIS, have square
1338: * brackets indicate that we're opening and
1339: * closing an optional context.
1340: */
1.32 schwarze 1341:
1.13 kristaps 1342: if (SECT_SYNOPSIS == st->sect &&
1343: ('[' == buf[start] ||
1344: ']' == buf[start]) &&
1.32 schwarze 1345: dosynopsisop(st, buf,
1346: &start, end, &opstack))
1.13 kristaps 1347: continue;
1.32 schwarze 1348:
1349: /*
1350: * On whitespace, flush the output buffer
1351: * and allow breaking to a macro line.
1352: * Otherwise, buffer text and clear wantws.
1353: */
1354:
1.31 schwarze 1355: last = buf[start++];
1.37 schwarze 1356: if (' ' != last) {
1357: outbuf_addchar(st);
1358: continue;
1359: }
1360:
1361: if ( ! strcmp(st->outbuf + st->outbuflen - 2, "()") &&
1362: dict_get(st->outbuf, st->outbuflen - 2) ==
1363: MDOC_Fo) {
1364: st->outbuflen -= 2;
1365: st->outbuf[st->outbuflen] = '\0';
1366: mdoc_newln(st);
1367: fputs(".Fn ", stdout);
1.31 schwarze 1368: outbuf_flush(st);
1.37 schwarze 1369: mdoc_newln(st);
1370: continue;
1371: }
1372:
1373: outbuf_flush(st);
1374: putchar(' ');
1375: st->wantws = 1;
1.1 schwarze 1376: }
1377:
1.34 schwarze 1378: if (start < end - 1 && '<' == buf[start + 1] &&
1379: 'A' <= buf[start] && 'Z' >= buf[start]) {
1.32 schwarze 1380: formatcode(st, buf, &start, end, 0, seq);
1381: if (OUST_MAC == st->oust) {
1.30 schwarze 1382: /*
1383: * Let mdoc(7) handle trailing punctuation.
1384: * XXX Some punctuation characters
1385: * are not handled yet.
1386: */
1.16 kristaps 1387: if ((start == end - 1 ||
1388: (start < end - 1 &&
1389: (' ' == buf[start + 1] ||
1390: '\n' == buf[start + 1]))) &&
1391: ('.' == buf[start] ||
1392: ',' == buf[start])) {
1393: putchar(' ');
1394: putchar(buf[start++]);
1395: }
1.32 schwarze 1396:
1397: if (st->wantws ||
1398: ' ' == buf[start] ||
1399: '\n' == buf[start])
1400: mdoc_newln(st);
1401:
1.30 schwarze 1402: /*
1403: * Consume all whitespace
1404: * so we don't accidentally start
1405: * an implicit literal line.
1406: */
1.32 schwarze 1407:
1.6 kristaps 1408: while (start < end && ' ' == buf[start])
1409: start++;
1.32 schwarze 1410:
1411: /*
1412: * Some text is following.
1413: * Implement requested spacing.
1414: */
1415:
1416: if ( ! st->wantws && start < end &&
1.34 schwarze 1417: ('<' != buf[start + 1] ||
1418: 'A' > buf[start] ||
1419: 'Z' < buf[start])) {
1.32 schwarze 1420: printf(" Ns ");
1421: st->wantws = 1;
1422: }
1.6 kristaps 1423: }
1.1 schwarze 1424: } else if (start < end && '\n' == buf[start]) {
1.32 schwarze 1425: outbuf_flush(st);
1426: mdoc_newln(st);
1.1 schwarze 1427: if (++start >= end)
1428: continue;
1429: /*
1430: * If we have whitespace next, eat it to prevent
1431: * mdoc(7) from thinking that it's meant for
1432: * verbatim text.
1433: * It is--but if we start with that, we can't
1434: * have a macro subsequent it, which may be
1435: * possible if we have an escape next.
1436: */
1.31 schwarze 1437: if (' ' == buf[start] || '\t' == buf[start])
1.1 schwarze 1438: puts(".br");
1439: for ( ; start < end; start++)
1440: if (' ' != buf[start] && '\t' != buf[start])
1441: break;
1.12 kristaps 1442: }
1.1 schwarze 1443: }
1.32 schwarze 1444: outbuf_flush(st);
1445: mdoc_newln(st);
1.1 schwarze 1446: }
1447:
1448: /*
1449: * There are three kinds of paragraphs: verbatim (starts with whitespace
1450: * of some sort), ordinary (starts without "=" marker), or a command
1451: * (default: starts with "=").
1452: */
1453: static void
1.35 schwarze 1454: dopar(struct state *st, char *buf, size_t start, size_t end)
1.1 schwarze 1455: {
1456:
1.32 schwarze 1457: assert(OUST_NL == st->oust);
1458: assert(st->wantws);
1459:
1.1 schwarze 1460: if (end == start)
1461: return;
1462: if (' ' == buf[start] || '\t' == buf[start])
1463: verbatim(st, buf, start, end);
1464: else if ('=' != buf[start])
1465: ordinary(st, buf, start, end);
1466: else
1467: command(st, buf, start, end);
1468: }
1469:
1470: /*
1471: * Loop around paragraphs within a document, processing each one in the
1472: * POD way.
1473: */
1474: static void
1475: dofile(const struct args *args, const char *fname,
1.35 schwarze 1476: const struct tm *tm, char *buf, size_t sz)
1.1 schwarze 1477: {
1.29 schwarze 1478: char datebuf[64];
1.1 schwarze 1479: struct state st;
1.29 schwarze 1480: const char *fbase, *fext, *section, *date;
1.1 schwarze 1481: char *title, *cp;
1.29 schwarze 1482: size_t sup, end, i, cur = 0;
1.1 schwarze 1483:
1484: if (0 == sz)
1485: return;
1486:
1.29 schwarze 1487: /*
1488: * Parsing the filename is almost always required,
1489: * except when both the title and the section
1490: * are provided on the command line.
1491: */
1492:
1493: if (NULL == args->title || NULL == args->section) {
1494: fbase = strrchr(fname, '/');
1495: if (NULL == fbase)
1496: fbase = fname;
1497: else
1498: fbase++;
1499: fext = strrchr(fbase, '.');
1500: } else
1501: fext = NULL;
1502:
1503: /*
1504: * The title will be converted to uppercase,
1505: * so it needs to be copied.
1506: */
1507:
1508: title = (NULL != args->title) ? strdup(args->title) :
1509: (NULL != fext) ? strndup(fbase, fext - fbase) :
1510: strdup(fbase);
1.1 schwarze 1511:
1512: if (NULL == title) {
1513: perror(NULL);
1514: exit(EXIT_FAILURE);
1515: }
1516:
1517: /* Section is 1 unless suffix is "pm". */
1518:
1.29 schwarze 1519: section = (NULL != args->section) ? args->section :
1520: (NULL == fext || strcmp(fext + 1, "pm")) ? "1" :
1521: PERL_SECTION;
1.1 schwarze 1522:
1523: /* Date. Or the given "tm" if not supplied. */
1524:
1525: if (NULL == (date = args->date)) {
1526: strftime(datebuf, sizeof(datebuf), "%B %d, %Y", tm);
1527: date = datebuf;
1528: }
1529:
1530: for (cp = title; '\0' != *cp; cp++)
1531: *cp = toupper((int)*cp);
1532:
1533: /* The usual mdoc(7) preamble. */
1534:
1535: printf(".Dd %s\n", date);
1536: printf(".Dt %s %s\n", title, section);
1537: puts(".Os");
1538:
1539: free(title);
1540:
1.37 schwarze 1541: dict_init();
1.1 schwarze 1542: memset(&st, 0, sizeof(struct state));
1.32 schwarze 1543: st.oust = OUST_NL;
1544: st.wantws = 1;
1545:
1.1 schwarze 1546: assert(sz > 0);
1547:
1548: /* Main loop over file contents. */
1549:
1550: while (cur < sz) {
1551: /* Read until next paragraph. */
1552: for (i = cur + 1; i < sz; i++)
1553: if ('\n' == buf[i] && '\n' == buf[i - 1]) {
1554: /* Consume blank paragraphs. */
1555: while (i + 1 < sz && '\n' == buf[i + 1])
1556: i++;
1557: break;
1558: }
1559:
1560: /* Adjust end marker for EOF. */
1561: end = i < sz ? i - 1 :
1562: ('\n' == buf[sz - 1] ? sz - 1 : sz);
1563: sup = i < sz ? end + 2 : sz;
1564:
1565: /* Process paragraph and adjust start. */
1566: dopar(&st, buf, cur, end);
1567: cur = sup;
1568: }
1.37 schwarze 1569: dict_destroy();
1.1 schwarze 1570: }
1571:
1572: /*
1573: * Read a single file fully into memory.
1574: * If the file is "-", do it from stdin.
1575: * If successfully read, send the input buffer to dofile() for further
1576: * processing.
1577: */
1578: static int
1579: readfile(const struct args *args, const char *fname)
1580: {
1581: int fd;
1582: char *buf;
1583: size_t bufsz, cur;
1584: ssize_t ssz;
1585: struct tm *tm;
1586: time_t ttm;
1587: struct stat st;
1588:
1589: fd = 0 != strcmp("-", fname) ?
1590: open(fname, O_RDONLY, 0) : STDIN_FILENO;
1591:
1592: if (-1 == fd) {
1593: perror(fname);
1594: return(0);
1595: }
1596:
1597: if (STDIN_FILENO == fd || -1 == fstat(fd, &st)) {
1598: ttm = time(NULL);
1599: tm = localtime(&ttm);
1600: } else
1601: tm = localtime(&st.st_mtime);
1602:
1603: /*
1604: * Arbitrarily-sized initial buffer.
1605: * Should be big enough for most files...
1606: */
1607: cur = 0;
1608: bufsz = 1 << 14;
1609: if (NULL == (buf = malloc(bufsz))) {
1610: perror(NULL);
1611: exit(EXIT_FAILURE);
1612: }
1613:
1614: while ((ssz = read(fd, buf + cur, bufsz - cur)) > 0) {
1615: /* Double buffer size on fill. */
1616: if ((size_t)ssz == bufsz - cur) {
1617: bufsz *= 2;
1618: if (NULL == (buf = realloc(buf, bufsz))) {
1619: perror(NULL);
1620: exit(EXIT_FAILURE);
1621: }
1622: }
1623: cur += (size_t)ssz;
1624: }
1625: if (ssz < 0) {
1626: perror(fname);
1627: free(buf);
1628: return(0);
1629: }
1630:
1631: dofile(args, STDIN_FILENO == fd ?
1632: "STDIN" : fname, tm, buf, cur);
1633: free(buf);
1634: if (STDIN_FILENO != fd)
1635: close(fd);
1636: return(1);
1637: }
1638:
1639: int
1640: main(int argc, char *argv[])
1641: {
1642: const char *fname, *name;
1643: struct args args;
1644: int c;
1645:
1646: name = strrchr(argv[0], '/');
1647: if (name == NULL)
1648: name = argv[0];
1649: else
1650: ++name;
1651:
1652: memset(&args, 0, sizeof(struct args));
1653: fname = "-";
1654:
1655: /* Accept no arguments for now. */
1656:
1657: while (-1 != (c = getopt(argc, argv, "c:d:hln:oq:rs:uv")))
1658: switch (c) {
1659: case ('h'):
1660: /* FALLTHROUGH */
1661: case ('l'):
1662: /* FALLTHROUGH */
1663: case ('c'):
1664: /* FALLTHROUGH */
1665: case ('o'):
1666: /* FALLTHROUGH */
1667: case ('q'):
1668: /* FALLTHROUGH */
1669: case ('r'):
1670: /* FALLTHROUGH */
1671: case ('u'):
1672: /* FALLTHROUGH */
1673: case ('v'):
1674: /* Ignore these. */
1675: break;
1676: case ('d'):
1677: args.date = optarg;
1678: break;
1679: case ('n'):
1680: args.title = optarg;
1681: break;
1682: case ('s'):
1683: args.section = optarg;
1684: break;
1685: default:
1686: goto usage;
1687: }
1688:
1689: argc -= optind;
1690: argv += optind;
1691:
1692: /* Accept only a single input file. */
1693:
1.25 schwarze 1694: if (argc > 1)
1695: goto usage;
1.1 schwarze 1696: else if (1 == argc)
1697: fname = *argv;
1698:
1699: return(readfile(&args, fname) ?
1700: EXIT_SUCCESS : EXIT_FAILURE);
1701:
1702: usage:
1703: fprintf(stderr, "usage: %s [-d date] "
1.25 schwarze 1704: "[-n title] [-s section] [file]\n", name);
1.1 schwarze 1705:
1706: return(EXIT_FAILURE);
1707: }
CVSweb