Annotation of pod2mdoc/pod2mdoc.c, Revision 1.52
1.52 ! schwarze 1: /* $Id: pod2mdoc.c,v 1.51 2015/02/19 11:09:44 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++;
1.49 schwarze 1108:
1109: /* Remember #define for Dv or Fn. */
1110:
1111: if (strncmp(buf + i + 1, "define", 6) ||
1112: ! isspace((unsigned char)buf[i + 7]))
1113: goto again;
1114:
1115: ifo = i + 7;
1116: while (ifo < start &&
1117: isspace((unsigned char)buf[ifo]))
1118: ifo++;
1119: ifa = ifo;
1120: while ('_' == buf[ifa] ||
1121: isalnum((unsigned char)buf[ifa]))
1122: ifa++;
1123: dict_put(buf + ifo, ifa - ifo,
1124: '(' == buf[ifa] ? MDOC_Fo : MDOC_Dv);
1125:
1.41 schwarze 1126: goto again;
1.22 kristaps 1127: }
1.35 schwarze 1128:
1129: /* Parse function declaration. */
1130: ifo = ifa = ifc = 0;
1.36 schwarze 1131: inl = end;
1132: nopen = 0;
1133: for (ift = i; i < end; i++) {
1134: if (ifc) {
1135: if (buf[i] != '\n')
1136: continue;
1137: inl = i;
1138: break;
1139: }
1140: switch (buf[i]) {
1.45 schwarze 1141: case '\t':
1142: /* FALLTHROUGH */
1.36 schwarze 1143: case ' ':
1144: if ( ! ifa)
1145: ifo = i;
1146: break;
1147: case '(':
1148: if (ifo) {
1149: nopen++;
1150: if ( ! ifa)
1151: ifa = i;
1152: } else
1153: i = end;
1154: break;
1155: case ')':
1156: switch (nopen) {
1157: case 0:
1158: i = end;
1159: break;
1160: case 1:
1.35 schwarze 1161: ifc = i;
1.36 schwarze 1162: break;
1163: default:
1164: nopen--;
1165: break;
1166: }
1167: break;
1168: default:
1169: break;
1170: }
1.35 schwarze 1171: }
1172:
1173: /* Encode function declaration. */
1174: if (ifc) {
1.36 schwarze 1175: for (i = ifa; i < ifc; i++)
1176: if (buf[i] == '\n')
1177: buf[i] = ' ';
1.35 schwarze 1178: buf[ifo++] = '\0';
1.39 schwarze 1179: register_type(buf + ift);
1.35 schwarze 1180: printf(".Ft %s", buf + ift);
1181: if (buf[ifo] == '*') {
1182: fputs(" *", stdout);
1183: ifo++;
1184: }
1185: putchar('\n');
1186: buf[ifa++] = '\0';
1187: printf(".Fo %s\n", buf + ifo);
1.39 schwarze 1188: dict_put(buf + ifo, 0, MDOC_Fo);
1.35 schwarze 1189: buf[ifc++] = '\0';
1190: for (;;) {
1191: cp = strchr(buf + ifa, ',');
1.38 schwarze 1192: if (cp != NULL) {
1193: cp2 = cp;
1.36 schwarze 1194: *cp++ = '\0';
1.38 schwarze 1195: } else
1196: cp2 = strchr(buf + ifa, '\0');
1197: while (isalnum((unsigned char)cp2[-1]) ||
1198: '_' == cp2[-1])
1199: cp2--;
1200: if ('\0' != *cp2)
1.39 schwarze 1201: dict_put(cp2, 0, MDOC_Fa);
1202: register_type(buf + ifa);
1.50 schwarze 1203: if (strchr(buf + ifa, ' ') == NULL)
1204: printf(".Fa %s\n", buf + ifa);
1205: else
1206: printf(".Fa \"%s\"\n", buf + ifa);
1.35 schwarze 1207: if (cp == NULL)
1208: break;
1.45 schwarze 1209: while (*cp == ' ' || *cp == '\t')
1.36 schwarze 1210: cp++;
1211: ifa = cp - buf;
1.35 schwarze 1212: }
1213: puts(".Fc");
1214: if (buf[ifc] == ';')
1215: ifc++;
1.36 schwarze 1216: if (ifc < inl) {
1217: buf[inl] = '\0';
1.35 schwarze 1218: puts(buf + ifc);
1219: }
1.36 schwarze 1220: start = inl + 1;
1.35 schwarze 1221: if (start < end)
1222: goto again;
1223: return;
1224: }
1.22 kristaps 1225: }
1226:
1.1 schwarze 1227: puts(".Bd -literal");
1.8 kristaps 1228: for (last = ' '; start < end; start++) {
1229: /*
1230: * Handle accidental macros (newline starting with
1231: * control character) and escapes.
1232: */
1233: if ('\n' == last)
1.7 kristaps 1234: if ('.' == buf[start] || '\'' == buf[start])
1235: printf("\\&");
1.8 kristaps 1236: putchar(last = buf[start]);
1237: if ('\\' == buf[start])
1238: printf("e");
1.7 kristaps 1239: }
1.31 schwarze 1240: putchar(last = '\n');
1.1 schwarze 1241: puts(".Ed");
1242: }
1243:
1244: /*
1.13 kristaps 1245: * See dosynopsisop().
1246: */
1247: static int
1248: hasmatch(const char *buf, size_t start, size_t end)
1249: {
1250: size_t stack;
1251:
1252: for (stack = 0; start < end; start++)
1253: if (buf[start] == '[')
1254: stack++;
1255: else if (buf[start] == ']' && 0 == stack)
1256: return(1);
1257: else if (buf[start] == ']')
1258: stack--;
1259: return(0);
1260: }
1261:
1262: /*
1263: * If we're in the SYNOPSIS section and we've encounter braces in an
1264: * ordinary paragraph, then try to see whether we're an [-option].
1265: * Do this, if we're an opening bracket, by first seeing if we have a
1266: * matching end via hasmatch().
1267: * If we're an ending bracket, see if we have a stack already.
1268: */
1269: static int
1.32 schwarze 1270: dosynopsisop(struct state *st, const char *buf,
1271: size_t *start, size_t end, size_t *opstack)
1.13 kristaps 1272: {
1273:
1274: assert('[' == buf[*start] || ']' == buf[*start]);
1275:
1276: if ('[' == buf[*start] && hasmatch(buf, *start + 1, end)) {
1.32 schwarze 1277: mdoc_newln(st);
1.13 kristaps 1278: puts(".Oo");
1279: (*opstack)++;
1280: } else if ('[' == buf[*start])
1281: return(0);
1282:
1283: if (']' == buf[*start] && *opstack > 0) {
1.32 schwarze 1284: mdoc_newln(st);
1.13 kristaps 1285: puts(".Oc");
1286: (*opstack)--;
1287: } else if (']' == buf[*start])
1288: return(0);
1289:
1290: (*start)++;
1.31 schwarze 1291: last = '\n';
1.13 kristaps 1292: while (' ' == buf[*start])
1293: (*start)++;
1294: return(1);
1295: }
1296:
1297: /*
1.17 kristaps 1298: * Format multiple "Nm" manpage names in the NAME section.
1.32 schwarze 1299: * From the perspective of external callers,
1300: * always stays in OUST_NL/wantws mode,
1301: * but its children do use OUST_MAC.
1.17 kristaps 1302: */
1303: static void
1304: donamenm(struct state *st, const char *buf, size_t *start, size_t end)
1305: {
1306: size_t word;
1307:
1.32 schwarze 1308: assert(OUST_NL == st->oust);
1309: assert(st->wantws);
1310:
1.47 schwarze 1311: while (*start < end && isspace((unsigned char)buf[*start]))
1.17 kristaps 1312: (*start)++;
1313:
1314: if (end == *start) {
1315: puts(".Nm unknown");
1316: return;
1317: }
1318:
1319: while (*start < end) {
1320: for (word = *start; word < end; word++)
1321: if (',' == buf[word])
1322: break;
1.32 schwarze 1323: formatcodeln(st, "Nm", buf, start, word, 1);
1.17 kristaps 1324: if (*start == end) {
1.32 schwarze 1325: mdoc_newln(st);
1326: break;
1.17 kristaps 1327: }
1328: assert(',' == buf[*start]);
1.32 schwarze 1329: printf(" ,");
1330: mdoc_newln(st);
1.17 kristaps 1331: (*start)++;
1.47 schwarze 1332: while (*start < end && isspace((unsigned char)buf[*start]))
1.17 kristaps 1333: (*start)++;
1334: }
1335: }
1336:
1337: /*
1.1 schwarze 1338: * Ordinary paragraph.
1339: * Well, this is really the hardest--POD seems to assume that, for
1340: * example, a leading space implies a newline, and so on.
1341: * Lots of other snakes in the grass: escaping a newline followed by a
1342: * period (accidental mdoc(7) control), double-newlines after macro
1343: * passages, etc.
1.32 schwarze 1344: *
1345: * Uses formatcode() to go to OUST_MAC mode
1346: * and outbuf_flush() to go to OUST_TXT mode.
1.40 schwarze 1347: * In text mode, wantws requests white space before the text
1348: * currently contained in the outbuf, not before upcoming text.
1.32 schwarze 1349: * Must make sure to go back to OUST_NL/wantws mode before returning.
1.1 schwarze 1350: */
1351: static void
1352: ordinary(struct state *st, const char *buf, size_t start, size_t end)
1353: {
1.44 schwarze 1354: size_t i, j, opstack, wend;
1.43 schwarze 1355: enum mdoc_type mtype;
1.44 schwarze 1356: int eos, noeos, seq;
1.49 schwarze 1357: char savechar;
1.1 schwarze 1358:
1359: if ( ! st->parsing || st->paused)
1360: return;
1361:
1362: /*
1363: * Special-case: the NAME section.
1364: * If we find a "-" when searching from the end, assume that
1365: * we're in "name - description" format.
1366: * To wit, print out a "Nm" and "Nd" in that format.
1367: */
1.11 kristaps 1368: if (SECT_NAME == st->sect) {
1.15 kristaps 1369: for (i = end - 2; i > start; i--)
1.47 schwarze 1370: if ('-' == buf[i] &&
1371: isspace((unsigned char)buf[i + 1]))
1.1 schwarze 1372: break;
1373: if ('-' == buf[i]) {
1374: j = i;
1375: /* Roll over multiple "-". */
1376: for ( ; i > start; i--)
1377: if ('-' != buf[i])
1378: break;
1.17 kristaps 1379: donamenm(st, buf, &start, i + 1);
1.5 kristaps 1380: start = j + 1;
1.47 schwarze 1381: while (start < end &&
1382: isspace((unsigned char)buf[start]))
1.17 kristaps 1383: start++;
1.32 schwarze 1384: formatcodeln(st, "Nd", buf, &start, end, 1);
1385: mdoc_newln(st);
1.1 schwarze 1386: return;
1387: }
1388: }
1389:
1390: if ( ! st->haspar)
1391: puts(".Pp");
1392:
1393: st->haspar = 0;
1394: last = '\n';
1.13 kristaps 1395: opstack = 0;
1.1 schwarze 1396:
1.15 kristaps 1397: for (seq = 0; start < end; seq++) {
1.1 schwarze 1398: /*
1399: * Loop til we get either to a newline or escape.
1400: * Escape initial control characters.
1401: */
1402: while (start < end) {
1.34 schwarze 1403: if (start < end - 1 && '<' == buf[start + 1] &&
1404: 'A' <= buf[start] && 'Z' >= buf[start])
1.1 schwarze 1405: break;
1406: else if ('\n' == buf[start])
1407: break;
1408: else if ('\n' == last && '.' == buf[start])
1.31 schwarze 1409: outbuf_addstr(st, "\\&");
1.1 schwarze 1410: else if ('\n' == last && '\'' == buf[start])
1.31 schwarze 1411: outbuf_addstr(st, "\\&");
1.12 kristaps 1412: /*
1413: * If we're in the SYNOPSIS, have square
1414: * brackets indicate that we're opening and
1415: * closing an optional context.
1416: */
1.32 schwarze 1417:
1.13 kristaps 1418: if (SECT_SYNOPSIS == st->sect &&
1419: ('[' == buf[start] ||
1420: ']' == buf[start]) &&
1.32 schwarze 1421: dosynopsisop(st, buf,
1422: &start, end, &opstack))
1.13 kristaps 1423: continue;
1.32 schwarze 1424:
1.42 schwarze 1425: /* Merely buffer non-whitespace. */
1.32 schwarze 1426:
1.31 schwarze 1427: last = buf[start++];
1.44 schwarze 1428: if ( ! isspace(last))
1.37 schwarze 1429: outbuf_addchar(st);
1.44 schwarze 1430: if (start < end &&
1.52 ! schwarze 1431: ! isspace((unsigned char)buf[start - 1]) &&
1.44 schwarze 1432: ! isspace((unsigned char)buf[start]))
1.37 schwarze 1433: continue;
1434:
1.44 schwarze 1435: /*
1436: * Found the end of a word.
1437: * Rewind trailing delimiters.
1438: */
1439:
1440: eos = noeos = 0;
1441: for (wend = st->outbuflen; wend; wend--)
1442: if ('.' == st->outbuf[wend - 1] ||
1443: '!' == st->outbuf[wend - 1] ||
1444: '?' == st->outbuf[wend - 1])
1445: eos = 1;
1446: else if ('|' == st->outbuf[wend - 1] ||
1447: ',' == st->outbuf[wend - 1] ||
1448: ';' == st->outbuf[wend - 1] ||
1449: ':' == st->outbuf[wend - 1])
1450: noeos = 1;
1451: else if ('\'' != st->outbuf[wend - 1] &&
1452: '"' != st->outbuf[wend - 1] &&
1453: ')' != st->outbuf[wend - 1] &&
1454: ']' != st->outbuf[wend - 1])
1455: break;
1456: eos &= ! noeos;
1457:
1458: /*
1459: * Detect function names.
1460: */
1.42 schwarze 1461:
1.43 schwarze 1462: mtype = MDOC_Fa;
1.49 schwarze 1463: savechar = '\0';
1.44 schwarze 1464: if (wend && ')' == st->outbuf[wend] &&
1465: '(' == st->outbuf[wend - 1]) {
1466: mtype = dict_get(st->outbuf, --wend);
1.49 schwarze 1467: if (MDOC_Dv == mtype)
1468: mtype = MDOC_Fo;
1.43 schwarze 1469: if (MDOC_Fo == mtype || MDOC_MAX == mtype) {
1.44 schwarze 1470: st->outbuflen = wend;
1471: st->outbuf[wend] = '\0';
1.43 schwarze 1472: mdoc_newln(st);
1473: if (MDOC_Fo == mtype)
1474: fputs(".Fn ", stdout);
1475: else
1476: fputs(".Xr ", stdout);
1477: st->oust = OUST_MAC;
1478: }
1.49 schwarze 1479: } else {
1480: mtype = dict_get(st->outbuf, wend);
1481: if (MDOC_Dv == mtype) {
1482: savechar = st->outbuf[wend];
1483: st->outbuf[wend] = '\0';
1484: mdoc_newln(st);
1485: fputs(".Dv ", stdout);
1486: st->oust = OUST_MAC;
1487: } else
1488: mtype = MDOC_Fa;
1.37 schwarze 1489: }
1490:
1.42 schwarze 1491: /*
1492: * On whitespace, flush the output buffer
1493: * and allow breaking to a macro line.
1494: */
1495:
1.37 schwarze 1496: outbuf_flush(st);
1.42 schwarze 1497:
1498: /*
1499: * End macro lines, and
1500: * end text lines at the end of sentences.
1501: */
1502:
1.44 schwarze 1503: if (OUST_MAC == st->oust || (eos && wend > 1 &&
1504: islower((unsigned char)st->outbuf[wend - 1]))) {
1.43 schwarze 1505: if (MDOC_MAX == mtype)
1506: fputs(" 3", stdout);
1.49 schwarze 1507: if (MDOC_Fa != mtype) {
1508: if (MDOC_Dv == mtype)
1509: st->outbuf[wend] = savechar;
1510: else
1511: wend += 2;
1512: while ('\0' != st->outbuf[wend])
1.44 schwarze 1513: printf(" %c",
1.49 schwarze 1514: st->outbuf[wend++]);
1515: }
1.40 schwarze 1516: mdoc_newln(st);
1.43 schwarze 1517: }
1.42 schwarze 1518:
1519: /* Advance to the next word. */
1520:
1.44 schwarze 1521: while ('\n' != buf[start] &&
1522: isspace((unsigned char)buf[start]))
1.42 schwarze 1523: start++;
1524: st->wantws = 1;
1.1 schwarze 1525: }
1526:
1.34 schwarze 1527: if (start < end - 1 && '<' == buf[start + 1] &&
1528: 'A' <= buf[start] && 'Z' >= buf[start]) {
1.32 schwarze 1529: formatcode(st, buf, &start, end, 0, seq);
1530: if (OUST_MAC == st->oust) {
1.30 schwarze 1531: /*
1532: * Let mdoc(7) handle trailing punctuation.
1533: * XXX Some punctuation characters
1534: * are not handled yet.
1535: */
1.51 schwarze 1536: if ((start == end - 1 ||
1537: (start < end - 1 &&
1538: (' ' == buf[start + 1] ||
1539: '\n' == buf[start + 1]))) &&
1540: NULL != strchr("|.,;:?!)]", buf[start])) {
1.16 kristaps 1541: putchar(' ');
1542: putchar(buf[start++]);
1543: }
1.32 schwarze 1544:
1545: if (st->wantws ||
1546: ' ' == buf[start] ||
1547: '\n' == buf[start])
1548: mdoc_newln(st);
1549:
1.30 schwarze 1550: /*
1551: * Consume all whitespace
1552: * so we don't accidentally start
1553: * an implicit literal line.
1554: */
1.32 schwarze 1555:
1.6 kristaps 1556: while (start < end && ' ' == buf[start])
1557: start++;
1.32 schwarze 1558:
1559: /*
1560: * Some text is following.
1561: * Implement requested spacing.
1562: */
1563:
1564: if ( ! st->wantws && start < end &&
1.34 schwarze 1565: ('<' != buf[start + 1] ||
1566: 'A' > buf[start] ||
1567: 'Z' < buf[start])) {
1.32 schwarze 1568: printf(" Ns ");
1569: st->wantws = 1;
1570: }
1.6 kristaps 1571: }
1.1 schwarze 1572: } else if (start < end && '\n' == buf[start]) {
1.32 schwarze 1573: outbuf_flush(st);
1574: mdoc_newln(st);
1.1 schwarze 1575: if (++start >= end)
1576: continue;
1577: /*
1578: * If we have whitespace next, eat it to prevent
1579: * mdoc(7) from thinking that it's meant for
1580: * verbatim text.
1581: * It is--but if we start with that, we can't
1582: * have a macro subsequent it, which may be
1583: * possible if we have an escape next.
1584: */
1.31 schwarze 1585: if (' ' == buf[start] || '\t' == buf[start])
1.1 schwarze 1586: puts(".br");
1587: for ( ; start < end; start++)
1588: if (' ' != buf[start] && '\t' != buf[start])
1589: break;
1.12 kristaps 1590: }
1.1 schwarze 1591: }
1.32 schwarze 1592: outbuf_flush(st);
1593: mdoc_newln(st);
1.1 schwarze 1594: }
1595:
1596: /*
1597: * There are three kinds of paragraphs: verbatim (starts with whitespace
1598: * of some sort), ordinary (starts without "=" marker), or a command
1599: * (default: starts with "=").
1600: */
1601: static void
1.35 schwarze 1602: dopar(struct state *st, char *buf, size_t start, size_t end)
1.1 schwarze 1603: {
1604:
1.32 schwarze 1605: assert(OUST_NL == st->oust);
1606: assert(st->wantws);
1607:
1.1 schwarze 1608: if (end == start)
1609: return;
1610: if (' ' == buf[start] || '\t' == buf[start])
1611: verbatim(st, buf, start, end);
1612: else if ('=' != buf[start])
1613: ordinary(st, buf, start, end);
1614: else
1615: command(st, buf, start, end);
1616: }
1617:
1618: /*
1619: * Loop around paragraphs within a document, processing each one in the
1620: * POD way.
1621: */
1622: static void
1623: dofile(const struct args *args, const char *fname,
1.35 schwarze 1624: const struct tm *tm, char *buf, size_t sz)
1.1 schwarze 1625: {
1.29 schwarze 1626: char datebuf[64];
1.1 schwarze 1627: struct state st;
1.46 schwarze 1628: const char *fbase, *fext, *section, *date, *format;
1.1 schwarze 1629: char *title, *cp;
1.29 schwarze 1630: size_t sup, end, i, cur = 0;
1.1 schwarze 1631:
1632: if (0 == sz)
1633: return;
1634:
1.29 schwarze 1635: /*
1636: * Parsing the filename is almost always required,
1637: * except when both the title and the section
1638: * are provided on the command line.
1639: */
1640:
1641: if (NULL == args->title || NULL == args->section) {
1642: fbase = strrchr(fname, '/');
1643: if (NULL == fbase)
1644: fbase = fname;
1645: else
1646: fbase++;
1647: fext = strrchr(fbase, '.');
1648: } else
1649: fext = NULL;
1650:
1651: /*
1652: * The title will be converted to uppercase,
1653: * so it needs to be copied.
1654: */
1655:
1656: title = (NULL != args->title) ? strdup(args->title) :
1657: (NULL != fext) ? strndup(fbase, fext - fbase) :
1658: strdup(fbase);
1.1 schwarze 1659:
1660: if (NULL == title) {
1661: perror(NULL);
1662: exit(EXIT_FAILURE);
1663: }
1664:
1665: /* Section is 1 unless suffix is "pm". */
1666:
1.29 schwarze 1667: section = (NULL != args->section) ? args->section :
1668: (NULL == fext || strcmp(fext + 1, "pm")) ? "1" :
1669: PERL_SECTION;
1.1 schwarze 1670:
1671: /* Date. Or the given "tm" if not supplied. */
1672:
1.46 schwarze 1673: date = args->date;
1674: format = (NULL == date) ? "%B %d, %Y" :
1.48 schwarze 1675: strcmp(date, "Mdocdate") ? NULL : "$" "Mdocdate: %B %d %Y $";
1.46 schwarze 1676:
1677: if (NULL != format) {
1678: strftime(datebuf, sizeof(datebuf), format, tm);
1.1 schwarze 1679: date = datebuf;
1680: }
1681:
1682: for (cp = title; '\0' != *cp; cp++)
1683: *cp = toupper((int)*cp);
1684:
1685: /* The usual mdoc(7) preamble. */
1686:
1687: printf(".Dd %s\n", date);
1688: printf(".Dt %s %s\n", title, section);
1689: puts(".Os");
1690:
1691: free(title);
1692:
1.37 schwarze 1693: dict_init();
1.1 schwarze 1694: memset(&st, 0, sizeof(struct state));
1.32 schwarze 1695: st.oust = OUST_NL;
1696: st.wantws = 1;
1697:
1.1 schwarze 1698: assert(sz > 0);
1699:
1700: /* Main loop over file contents. */
1701:
1702: while (cur < sz) {
1703: /* Read until next paragraph. */
1704: for (i = cur + 1; i < sz; i++)
1705: if ('\n' == buf[i] && '\n' == buf[i - 1]) {
1706: /* Consume blank paragraphs. */
1707: while (i + 1 < sz && '\n' == buf[i + 1])
1708: i++;
1709: break;
1710: }
1711:
1712: /* Adjust end marker for EOF. */
1713: end = i < sz ? i - 1 :
1714: ('\n' == buf[sz - 1] ? sz - 1 : sz);
1715: sup = i < sz ? end + 2 : sz;
1716:
1717: /* Process paragraph and adjust start. */
1718: dopar(&st, buf, cur, end);
1719: cur = sup;
1720: }
1.37 schwarze 1721: dict_destroy();
1.1 schwarze 1722: }
1723:
1724: /*
1725: * Read a single file fully into memory.
1726: * If the file is "-", do it from stdin.
1727: * If successfully read, send the input buffer to dofile() for further
1728: * processing.
1729: */
1730: static int
1731: readfile(const struct args *args, const char *fname)
1732: {
1733: int fd;
1734: char *buf;
1735: size_t bufsz, cur;
1736: ssize_t ssz;
1737: struct tm *tm;
1738: time_t ttm;
1739: struct stat st;
1740:
1741: fd = 0 != strcmp("-", fname) ?
1742: open(fname, O_RDONLY, 0) : STDIN_FILENO;
1743:
1744: if (-1 == fd) {
1745: perror(fname);
1746: return(0);
1747: }
1748:
1749: if (STDIN_FILENO == fd || -1 == fstat(fd, &st)) {
1750: ttm = time(NULL);
1751: tm = localtime(&ttm);
1752: } else
1753: tm = localtime(&st.st_mtime);
1754:
1755: /*
1756: * Arbitrarily-sized initial buffer.
1757: * Should be big enough for most files...
1758: */
1759: cur = 0;
1760: bufsz = 1 << 14;
1761: if (NULL == (buf = malloc(bufsz))) {
1762: perror(NULL);
1763: exit(EXIT_FAILURE);
1764: }
1765:
1766: while ((ssz = read(fd, buf + cur, bufsz - cur)) > 0) {
1767: /* Double buffer size on fill. */
1768: if ((size_t)ssz == bufsz - cur) {
1769: bufsz *= 2;
1770: if (NULL == (buf = realloc(buf, bufsz))) {
1771: perror(NULL);
1772: exit(EXIT_FAILURE);
1773: }
1774: }
1775: cur += (size_t)ssz;
1776: }
1777: if (ssz < 0) {
1778: perror(fname);
1779: free(buf);
1780: return(0);
1781: }
1782:
1783: dofile(args, STDIN_FILENO == fd ?
1784: "STDIN" : fname, tm, buf, cur);
1785: free(buf);
1786: if (STDIN_FILENO != fd)
1787: close(fd);
1788: return(1);
1789: }
1790:
1791: int
1792: main(int argc, char *argv[])
1793: {
1794: const char *fname, *name;
1795: struct args args;
1796: int c;
1797:
1798: name = strrchr(argv[0], '/');
1799: if (name == NULL)
1800: name = argv[0];
1801: else
1802: ++name;
1803:
1804: memset(&args, 0, sizeof(struct args));
1805: fname = "-";
1806:
1807: /* Accept no arguments for now. */
1808:
1809: while (-1 != (c = getopt(argc, argv, "c:d:hln:oq:rs:uv")))
1810: switch (c) {
1811: case ('h'):
1812: /* FALLTHROUGH */
1813: case ('l'):
1814: /* FALLTHROUGH */
1815: case ('c'):
1816: /* FALLTHROUGH */
1817: case ('o'):
1818: /* FALLTHROUGH */
1819: case ('q'):
1820: /* FALLTHROUGH */
1821: case ('r'):
1822: /* FALLTHROUGH */
1823: case ('u'):
1824: /* FALLTHROUGH */
1825: case ('v'):
1826: /* Ignore these. */
1827: break;
1828: case ('d'):
1829: args.date = optarg;
1830: break;
1831: case ('n'):
1832: args.title = optarg;
1833: break;
1834: case ('s'):
1835: args.section = optarg;
1836: break;
1837: default:
1838: goto usage;
1839: }
1840:
1841: argc -= optind;
1842: argv += optind;
1843:
1844: /* Accept only a single input file. */
1845:
1.25 schwarze 1846: if (argc > 1)
1847: goto usage;
1.1 schwarze 1848: else if (1 == argc)
1849: fname = *argv;
1850:
1851: return(readfile(&args, fname) ?
1852: EXIT_SUCCESS : EXIT_FAILURE);
1853:
1854: usage:
1855: fprintf(stderr, "usage: %s [-d date] "
1.25 schwarze 1856: "[-n title] [-s section] [file]\n", name);
1.1 schwarze 1857:
1858: return(EXIT_FAILURE);
1859: }
CVSweb