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