=================================================================== RCS file: /cvs/pod2mdoc/pod2mdoc.c,v retrieving revision 1.28 retrieving revision 1.54 diff -u -p -r1.28 -r1.54 --- pod2mdoc/pod2mdoc.c 2014/07/11 10:31:28 1.28 +++ pod2mdoc/pod2mdoc.c 2015/02/19 15:26:45 1.54 @@ -1,6 +1,7 @@ -/* $Id: pod2mdoc.c,v 1.28 2014/07/11 10:31:28 schwarze Exp $ */ +/* $Id: pod2mdoc.c,v 1.54 2015/02/19 15:26:45 schwarze Exp $ */ /* * Copyright (c) 2014 Kristaps Dzonsons + * Copyright (c) 2014, 2015 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -26,6 +27,8 @@ #include #include +#include "dict.h" + /* * In what section can we find Perl module manuals? * Sometimes (Mac OS X) it's 3pm, sometimes (OpenBSD, etc.) 3p. @@ -52,15 +55,26 @@ enum sect { SECT_SYNOPSIS, /* SYNOPSIS section */ }; +enum outstate { + OUST_NL = 0, /* just started a new output line */ + OUST_TXT, /* text line output in progress */ + OUST_MAC /* macro line output in progress */ +}; + struct state { + const char *fname; /* file being parsed */ int parsing; /* after =cut of before command */ int paused; /* in =begin and before =end */ - int haspar; /* in paragraph: do we need Pp? */ enum sect sect; /* which section are we in? */ - const char *fname; /* file being parsed */ #define LIST_STACKSZ 128 enum list lstack[LIST_STACKSZ]; /* open lists */ size_t lpos; /* where in list stack */ + int haspar; /* in paragraph: do we need Pp? */ + enum outstate oust; /* state of the mdoc output stream */ + int wantws; /* let mdoc(7) output whitespace here */ + char *outbuf; /* text buffered for output */ + size_t outbufsz; /* allocated size of outbuf */ + size_t outbuflen; /* current length of outbuf */ }; enum fmt { @@ -121,15 +135,92 @@ static const char fmts[FMT__MAX] = { 'Z' /* FMT_NULL */ }; -static int last; +static unsigned char last; + +static void +outbuf_grow(struct state *st, size_t by) +{ + + st->outbufsz += (by / 128 + 1) * 128; + st->outbuf = realloc(st->outbuf, st->outbufsz); + if (NULL == st->outbuf) { + perror(NULL); + exit(EXIT_FAILURE); + } +} + +static void +outbuf_addchar(struct state *st) +{ + + if (st->outbuflen + 2 >= st->outbufsz) + outbuf_grow(st, 1); + st->outbuf[st->outbuflen++] = last; + if ('\\' == last) + st->outbuf[st->outbuflen++] = 'e'; + st->outbuf[st->outbuflen] = '\0'; +} + +static void +outbuf_addstr(struct state *st, const char *str) +{ + size_t slen; + + slen = strlen(str); + if (st->outbuflen + slen >= st->outbufsz) + outbuf_grow(st, slen); + memcpy(st->outbuf + st->outbuflen, str, slen+1); + st->outbuflen += slen; + last = str[slen - 1]; +} + +static void +outbuf_flush(struct state *st) +{ + + if (0 == st->outbuflen) + return; + + if (OUST_TXT == st->oust && st->wantws) + putchar(' '); + + if (OUST_MAC == st->oust && '"' == *st->outbuf) + printf("\\(dq%s", st->outbuf + 1); + else + fputs(st->outbuf, stdout); + + *st->outbuf = '\0'; + st->outbuflen = 0; + + if (OUST_NL == st->oust) + st->oust = OUST_TXT; +} + +static void +mdoc_newln(struct state *st) +{ + + if (OUST_NL == st->oust) + return; + + putchar('\n'); + last = '\n'; + st->oust = OUST_NL; + st->wantws = 1; +} + /* * Given buf[*start] is at the start of an escape name, read til the end * of the escape ('>') then try to do something with it. * Sets start to be one after the '>'. + * + * This function does not care about output modes, + * it merely appends text to the output buffer, + * which can then be used in any mode. */ static void -formatescape(const char *buf, size_t *start, size_t end) +formatescape(struct state *st, const char *buf, size_t *start, size_t end) { char esc[16]; /* no more needed */ size_t i, max; @@ -157,17 +248,13 @@ formatescape(const char *buf, size_t *start, size_t en * Just let the rest of them go. */ if (0 == strcmp(esc, "lt")) - printf("\\(la"); + outbuf_addstr(st, "\\(la"); else if (0 == strcmp(esc, "gt")) - printf("\\(ra"); - else if (0 == strcmp(esc, "vb")) - printf("\\(ba"); + outbuf_addstr(st, "\\(ra"); + else if (0 == strcmp(esc, "verbar")) + outbuf_addstr(st, "\\(ba"); else if (0 == strcmp(esc, "sol")) - printf("\\(sl"); - else - return; - - last = 'a'; + outbuf_addstr(st, "\\(sl"); } /* @@ -175,6 +262,9 @@ formatescape(const char *buf, size_t *start, size_t en * I set "start" to be the end of the sequence (last right-carrot) so * that the caller can safely just continue processing. * If this is just an empty tag, I'll return 0. + * + * Always operates in OUST_MAC mode. + * Mode handling is done by the caller. */ static int trylink(const char *buf, size_t *start, size_t end, size_t dsz) @@ -309,6 +399,9 @@ trylink(const char *buf, size_t *start, size_t end, si * Our flag might be followed by an argument, so make sure that we're * accounting for that, too. * If we don't have a flag at all, however, then assume we're an "Ar". + * + * Always operates in OUST_MAC mode. + * Mode handlinf is done by the caller. */ static void dosynopsisfl(const char *buf, size_t *start, size_t end) @@ -374,42 +467,36 @@ again: * like X<...> and can contain nested format codes. * This consumes the whole format code, and any nested format codes, til * the end of matched production. - * If "reentrant", then we're being called after a macro has already - * been printed to the current line. * If "nomacro", then we don't print any macros, just contained data * (e.g., following "Sh" or "Nm"). * "pos" is only significant in SYNOPSIS, and should be 0 when invoked * as the first format code on a line (for decoration as an "Nm"), * non-zero otherwise. - * Return whether we've printed a macro or not--in other words, whether - * this should trigger a subsequent newline (this should be ignored when - * reentrant). + * + * Output mode handling is most complicated here. + * We may enter in any mode. + * We usually exit in OUST_MAC mode, except when + * entering without OUST_MAC and the code is invalid. */ static int formatcode(struct state *st, const char *buf, size_t *start, - size_t end, int reentrant, int nomacro, int pos) + size_t end, int nomacro, int pos) { - enum fmt fmt; size_t i, j, dsz; + enum fmt fmt; + unsigned char uc; assert(*start + 1 < end); assert('<' == buf[*start + 1]); /* * First, look up the format code. - * If it's not valid, then exit immediately. + * If it's not valid, treat it as a NOOP. */ for (fmt = 0; fmt < FMT__MAX; fmt++) if (buf[*start] == fmts[fmt]) break; - if (FMT__MAX == fmt) { - putchar(last = buf[(*start)++]); - if ('\\' == last) - putchar('e'); - return(0); - } - /* * Determine whether we're overriding our delimiter. * According to POD, if we have more than one '<' followed by a @@ -434,7 +521,7 @@ formatcode(struct state *st, const char *buf, size_t * * processing for real macros. */ if (FMT_ESCAPE == fmt) { - formatescape(buf, start, end); + formatescape(st, buf, start, end); return(0); } else if (FMT_NULL == fmt || FMT_INDEX == fmt) { /* @@ -472,28 +559,51 @@ formatcode(struct state *st, const char *buf, size_t * * Check whether we're supposed to print macro stuff (this is * suppressed in, e.g., "Nm" and "Sh" macros). */ - if ( ! nomacro) { + if (FMT__MAX != fmt && !nomacro) { + /* - * Print out the macro describing this format code. - * If we're not "reentrant" (not yet on a macro line) - * then print a newline, if necessary, and the macro - * indicator. - * Otherwise, offset us with a space. + * If we are on a text line and there is no + * whitespace before our content, we have to make + * the previous word a prefix to the macro line. */ - if ( ! reentrant) { - if (last != '\n') - putchar('\n'); + + if (OUST_MAC != st->oust && ' ' != buf[*start] && + st->outbuflen) { + if (OUST_NL != st->oust) + mdoc_newln(st); + printf(".Pf "); + st->oust = OUST_MAC; + st->wantws = 1; + } + + outbuf_flush(st); + + /* + * Whitespace is easier to suppress on macro lines. + * We may already have wantws if there was whitespace + * before the code ("text Boust && ' ' != buf[*start] && + ! st->wantws) + printf(" Ns"); + + /* Unless we are on a macro line, start one. */ + + if (OUST_MAC != st->oust) { + if (OUST_NL != st->oust) + mdoc_newln(st); putchar('.'); - } else + st->oust = OUST_MAC; + } else putchar(' '); - + st->wantws = 1; + /* - * If we don't have whitespace before us (and none after - * the opening delimiter), then suppress macro - * whitespace with Pf. + * Print the macro corresponding to this format code, + * and update the output state afterwards. */ - if (' ' != last && '\n' != last && ' ' != buf[*start]) - printf("Pf "); switch (fmt) { case (FMT_ITALIC): @@ -508,13 +618,28 @@ formatcode(struct state *st, const char *buf, size_t * else printf("Ar "); break; - } - if (0 == strncmp(buf + *start, "NULL", 4) && - ('=' == buf[*start + 4] || - '>' == buf[*start + 4])) + } + i = 0; + uc = buf[*start]; + while (isalnum(uc) || '_' == uc || ' ' == uc) + uc = buf[*start + ++i]; + if ('=' != uc && '>' != uc) + i = 0; + if (4 == i && ! strncmp(buf + *start, "NULL", 4)) { printf("Dv "); - else + break; + } + switch (i ? dict_get(buf + *start, i) : MDOC_MAX) { + case MDOC_Fa: + printf("Fa "); + break; + case MDOC_Vt: + printf("Vt "); + break; + default: printf("Sy "); + break; + } break; case (FMT_CODE): printf("Qo Li "); @@ -533,7 +658,8 @@ formatcode(struct state *st, const char *buf, size_t * default: abort(); } - } + } else + outbuf_flush(st); /* * Process until we reach the end marker (e.g., '>') or until we @@ -562,72 +688,114 @@ formatcode(struct state *st, const char *buf, size_t * break; } } - if (*start + 1 < end && '<' == buf[*start + 1]) { - formatcode(st, buf, start, end, 1, nomacro, 1); + if (*start + 1 < end && '<' == buf[*start + 1] && + 'A' <= buf[*start] && 'Z' >= buf[*start]) { + if ( ! formatcode(st, buf, start, end, nomacro, 1)) + st->wantws = 1; continue; } - /* - * Make sure that any macro-like words (or - * really any word starting with a capital - * letter) is assumed to be a macro that must be - * escaped. - * This matches "Xx " and "XxEOLN". - */ - if ((' ' == last || '\n' == last) && - end - *start > 1 && - isupper((int)buf[*start]) && - islower((int)buf[*start + 1]) && - (end - *start == 2 || - ' ' == buf[*start + 2])) - printf("\\&"); + /* Suppress newlines and multiple spaces. */ - /* Suppress newline. */ - if ('\n' == buf[*start]) - putchar(last = ' '); - else - putchar(last = buf[*start]); + last = buf[(*start)++]; + if (' ' == last || '\n' == last) { + putchar(' '); + while (*start < end && ' ' == buf[*start]) + (*start)++; + continue; + } + if (OUST_MAC == st->oust && FMT__MAX != fmt) { + if ( ! st->wantws) { + printf(" Ns "); + st->wantws = 1; + } + + /* + * Escape macro-like words. + * This matches "Xx " and "XxEOLN". + */ + + if (end - *start > 0 && + isupper((unsigned char)last) && + islower((unsigned char)buf[*start]) && + (end - *start == 1 || + ' ' == buf[*start + 1] || + '>' == buf[*start + 1])) + printf("\\&"); + } + + putchar(last); + /* Protect against character escapes. */ + if ('\\' == last) putchar('e'); - - (*start)++; - - if (' ' == last) - while (*start < end && ' ' == buf[*start]) - (*start)++; } if ( ! nomacro && FMT_CODE == fmt) printf(" Qc "); - /* - * We're now subsequent the format code. - * If there isn't a space (or newline) here, and we haven't just - * printed a space, then suppress space. - */ - if ( ! nomacro && ' ' != last) - if (' ' != buf[*start] && '\n' != buf[*start]) - printf(" Ns "); - - return(1); + st->wantws = ' ' == last; + return(FMT__MAX != fmt); } /* * Calls formatcode() til the end of a paragraph. + * Goes to OUST_MAC mode and stays there when returning, + * such that the caller can add arguments to the macro line + * before closing it out. */ static void -formatcodeln(struct state *st, const char *buf, - size_t *start, size_t end, int nomacro) +formatcodeln(struct state *st, const char *linemac, + const char *buf, size_t *start, size_t end, int nomacro) { + int gotmacro, wantws; - last = ' '; + assert(OUST_NL == st->oust); + assert(st->wantws); + printf(".%s ", linemac); + st->oust = OUST_MAC; + + gotmacro = 0; while (*start < end) { - if (*start + 1 < end && '<' == buf[*start + 1]) { - formatcode(st, buf, start, end, 1, nomacro, 1); + wantws = ' ' == buf[*start] || '\n' == buf[*start]; + if (wantws) { + last = ' '; + do { + (*start)++; + } while (*start < end && ' ' == buf[*start]); + } + + if (*start + 1 < end && '<' == buf[*start + 1] && + 'A' <= buf[*start] && 'Z' >= buf[*start]) { + st->wantws |= wantws; + gotmacro = formatcode(st, buf, + start, end, nomacro, 1); continue; } + + if (gotmacro) { + if (*start < end || st->outbuflen) { + if (st->wantws || + (wantws && !st->outbuflen)) + printf(" No "); + else + printf(" Ns "); + } + gotmacro = 0; + } + outbuf_flush(st); + st->wantws = wantws; + + if (*start >= end) + break; + + if (st->wantws) { + putchar(' '); + st->wantws = 0; + } + /* * Since we're already on a macro line, we want to make * sure that we don't inadvertently invoke a macro. @@ -636,18 +804,15 @@ formatcodeln(struct state *st, const char *buf, * something that needn't be escaped. */ if (' ' == last && end - *start > 1 && - isupper((int)buf[*start]) && - islower((int)buf[*start + 1]) && - (end - *start == 2 || - ' ' == buf[*start + 2])) + isupper((unsigned char)buf[*start]) && + islower((unsigned char)buf[*start + 1]) && + (end - *start == 2 || ' ' == buf[*start + 2])) printf("\\&"); - if ('\n' == buf[*start]) - putchar(last = ' '); - else - putchar(last = buf[*start]); + putchar(last = buf[*start]); /* Protect against character escapes. */ + if ('\\' == last) putchar('e'); @@ -681,6 +846,9 @@ listguess(const char *buf, size_t start, size_t end) * A command paragraph, as noted in the perlpod manual, just indicates * that we should do something, optionally with some text to print as * well. + * From the perspective of external callers, + * always stays in OUST_NL/wantws mode, + * but its children do use OUST_MAC. */ static void command(struct state *st, const char *buf, size_t start, size_t end) @@ -724,7 +892,6 @@ command(struct state *st, const char *buf, size_t star * The behaviour of head= follows from a quick glance at * how pod2man handles it. */ - printf(".Sh "); st->sect = SECT_NONE; if (end - start == 4) { if (0 == memcmp(&buf[start], "NAME", 4)) @@ -733,29 +900,26 @@ command(struct state *st, const char *buf, size_t star if (0 == memcmp(&buf[start], "SYNOPSIS", 8)) st->sect = SECT_SYNOPSIS; } - formatcodeln(st, buf, &start, end, 1); - putchar('\n'); + formatcodeln(st, "Sh", buf, &start, end, 1); + mdoc_newln(st); st->haspar = 1; break; case (CMD_HEAD2): - printf(".Ss "); - formatcodeln(st, buf, &start, end, 1); - putchar('\n'); + formatcodeln(st, "Ss", buf, &start, end, 1); + mdoc_newln(st); st->haspar = 1; break; case (CMD_HEAD3): puts(".Pp"); - printf(".Em "); - formatcodeln(st, buf, &start, end, 0); - putchar('\n'); + formatcodeln(st, "Em", buf, &start, end, 0); + mdoc_newln(st); puts(".Pp"); st->haspar = 1; break; case (CMD_HEAD4): puts(".Pp"); - printf(".No "); - formatcodeln(st, buf, &start, end, 0); - putchar('\n'); + formatcodeln(st, "No", buf, &start, end, 0); + mdoc_newln(st); puts(".Pp"); st->haspar = 1; break; @@ -807,9 +971,8 @@ command(struct state *st, const char *buf, size_t star } switch (st->lstack[st->lpos - 1]) { case (LIST_TAG): - printf(".It "); - formatcodeln(st, buf, &start, end, 0); - putchar('\n'); + formatcodeln(st, "It", buf, &start, end, 0); + mdoc_newln(st); break; case (LIST_ENUM): /* FALLTHROUGH */ @@ -860,17 +1023,61 @@ command(struct state *st, const char *buf, size_t star } /* + * Put the type provided as an argument into the dictionary. + */ +static void +register_type(const char *ptype) +{ + const char *pname, *pend; + + pname = ptype; + while (isalnum((unsigned char)*pname) || '_' == *pname) + pname++; + if ((pname - ptype == 6 && ! strncmp(ptype, "struct", 6)) || + (pname - ptype == 4 && ! strncmp(ptype, "enum", 4))) { + while (' ' == *pname) + pname++; + pend = pname; + while (isalnum((unsigned char)*pend) || '_' == *pend) + pend++; + if (pend > pname) + dict_put(pname, pend - pname, MDOC_Vt); + } else + pend = pname; + if (pend > ptype) + dict_put(ptype, pend - ptype, MDOC_Vt); +} + +/* * Just pump out the line in a verbatim block. + * From the perspective of external callers, + * always stays in OUST_NL/wantws mode. */ static void -verbatim(struct state *st, const char *buf, size_t start, size_t end) +verbatim(struct state *st, char *buf, size_t start, size_t end) { - int last; - size_t i; + size_t i, ift, ifo, ifa, ifc, inl; + char *cp, *cp2; + int indisplay, nopen, wantsp; - if ( ! st->parsing || st->paused) + if (st->paused || ! st->parsing) return; + + indisplay = wantsp = 0; + again: + if (start == end) { + if (indisplay) + puts(".Ed"); + return; + } + + if ('\n' == buf[start]) { + wantsp = 1; + start++; + goto again; + } + /* * If we're in the SYNOPSIS, see if we're an #include block. * If we are, then print the "In" macro and re-loop. @@ -879,16 +1086,20 @@ again: */ if (SECT_SYNOPSIS == st->sect) { i = start; - for (i = start; i < end && ' ' == buf[i]; i++) - /* Spin. */ ; + while (i < end && buf[i] == ' ') + i++; if (i == end) - return; + goto again; + /* We're an include block! */ if (end - i > 10 && 0 == memcmp(&buf[i], "#include <", 10)) { start = i + 10; while (start < end && ' ' == buf[start]) start++; + if (indisplay) + puts(".Ed"); + indisplay = wantsp = 0; fputs(".In ", stdout); /* Stop til the '>' marker or we hit eoln. */ while (start < end && @@ -899,29 +1110,167 @@ again: start++; if (start < end && '\n' == buf[start]) start++; - if (start < end) + goto again; + } + + /* Other preprocessor directives. */ + if ('#' == buf[i]) { + if (indisplay) + puts(".Ed"); + indisplay = wantsp = 0; + fputs(".Fd ", stdout); + start = i; + while(start < end && '\n' != buf[start]) + putchar(buf[start++]); + putchar('\n'); + if (start < end && '\n' == buf[start]) + start++; + + /* Remember #define for Dv or Fn. */ + + if (strncmp(buf + i + 1, "define", 6) || + ! isspace((unsigned char)buf[i + 7])) goto again; - return; + + ifo = i + 7; + while (ifo < start && + isspace((unsigned char)buf[ifo])) + ifo++; + ifa = ifo; + while ('_' == buf[ifa] || + isalnum((unsigned char)buf[ifa])) + ifa++; + dict_put(buf + ifo, ifa - ifo, + '(' == buf[ifa] ? MDOC_Fo : MDOC_Dv); + + goto again; } + + /* Parse function declaration. */ + ifo = ifa = ifc = 0; + inl = end; + nopen = 0; + for (ift = i; i < end; i++) { + if (ifc) { + if (buf[i] != '\n') + continue; + inl = i; + break; + } + switch (buf[i]) { + case '\t': + /* FALLTHROUGH */ + case ' ': + if ( ! ifa) + ifo = i; + break; + case '(': + if (ifo) { + nopen++; + if ( ! ifa) + ifa = i; + } else + i = end; + break; + case ')': + switch (nopen) { + case 0: + i = end; + break; + case 1: + ifc = i; + break; + default: + nopen--; + break; + } + break; + default: + break; + } + } + + /* Encode function declaration. */ + if (ifc) { + for (i = ifa; i < ifc; i++) + if (buf[i] == '\n') + buf[i] = ' '; + buf[ifo++] = '\0'; + register_type(buf + ift); + if (indisplay) + puts(".Ed"); + indisplay = wantsp = 0; + printf(".Ft %s", buf + ift); + if (buf[ifo] == '*') { + fputs(" *", stdout); + ifo++; + } + putchar('\n'); + buf[ifa++] = '\0'; + printf(".Fo %s\n", buf + ifo); + dict_put(buf + ifo, 0, MDOC_Fo); + buf[ifc++] = '\0'; + for (;;) { + cp = strchr(buf + ifa, ','); + if (cp != NULL) { + cp2 = cp; + *cp++ = '\0'; + } else + cp2 = strchr(buf + ifa, '\0'); + while (isalnum((unsigned char)cp2[-1]) || + '_' == cp2[-1]) + cp2--; + if ('\0' != *cp2) + dict_put(cp2, 0, MDOC_Fa); + register_type(buf + ifa); + if (strchr(buf + ifa, ' ') == NULL) + printf(".Fa %s\n", buf + ifa); + else + printf(".Fa \"%s\"\n", buf + ifa); + if (cp == NULL) + break; + while (*cp == ' ' || *cp == '\t') + cp++; + ifa = cp - buf; + } + puts(".Fc"); + if (buf[ifc] == ';') + ifc++; + if (ifc < inl) { + buf[inl] = '\0'; + puts(buf + ifc); + } + start = inl < end ? inl + 1 : end; + goto again; + } } - - if (start == end) - return; - puts(".Bd -literal"); - for (last = ' '; start < end; start++) { + + if ( ! indisplay) + puts(".Bd -literal"); + else if (wantsp) + putchar('\n'); + indisplay = 1; + wantsp = 0; + + for (last = '\n'; start < end; start++) { /* * Handle accidental macros (newline starting with * control character) and escapes. */ - if ('\n' == last) + if ('\n' == last) { + if ('\n' == buf[start]) + goto again; if ('.' == buf[start] || '\'' == buf[start]) printf("\\&"); + } putchar(last = buf[start]); if ('\\' == buf[start]) printf("e"); } - putchar('\n'); - puts(".Ed"); + if ('\n' != last) + putchar('\n'); + if (indisplay) + puts(".Ed"); } /* @@ -950,30 +1299,28 @@ hasmatch(const char *buf, size_t start, size_t end) * If we're an ending bracket, see if we have a stack already. */ static int -dosynopsisop(const char *buf, int *last, +dosynopsisop(struct state *st, const char *buf, size_t *start, size_t end, size_t *opstack) { assert('[' == buf[*start] || ']' == buf[*start]); if ('[' == buf[*start] && hasmatch(buf, *start + 1, end)) { - if ('\n' != *last) - putchar('\n'); + mdoc_newln(st); puts(".Oo"); (*opstack)++; } else if ('[' == buf[*start]) return(0); if (']' == buf[*start] && *opstack > 0) { - if ('\n' != *last) - putchar('\n'); + mdoc_newln(st); puts(".Oc"); (*opstack)--; } else if (']' == buf[*start]) return(0); (*start)++; - *last = '\n'; + last = '\n'; while (' ' == buf[*start]) (*start)++; return(1); @@ -981,13 +1328,19 @@ dosynopsisop(const char *buf, int *last, /* * Format multiple "Nm" manpage names in the NAME section. + * From the perspective of external callers, + * always stays in OUST_NL/wantws mode, + * but its children do use OUST_MAC. */ static void donamenm(struct state *st, const char *buf, size_t *start, size_t end) { size_t word; - while (*start < end && ' ' == buf[*start]) + assert(OUST_NL == st->oust); + assert(st->wantws); + + while (*start < end && isspace((unsigned char)buf[*start])) (*start)++; if (end == *start) { @@ -996,19 +1349,19 @@ donamenm(struct state *st, const char *buf, size_t *st } while (*start < end) { - fputs(".Nm ", stdout); for (word = *start; word < end; word++) if (',' == buf[word]) break; - formatcodeln(st, buf, start, word, 1); + formatcodeln(st, "Nm", buf, start, word, 1); if (*start == end) { - putchar('\n'); - continue; + mdoc_newln(st); + break; } assert(',' == buf[*start]); - puts(" ,"); + printf(" ,"); + mdoc_newln(st); (*start)++; - while (*start < end && ' ' == buf[*start]) + while (*start < end && isspace((unsigned char)buf[*start])) (*start)++; } } @@ -1020,12 +1373,20 @@ donamenm(struct state *st, const char *buf, size_t *st * Lots of other snakes in the grass: escaping a newline followed by a * period (accidental mdoc(7) control), double-newlines after macro * passages, etc. + * + * Uses formatcode() to go to OUST_MAC mode + * and outbuf_flush() to go to OUST_TXT mode. + * In text mode, wantws requests white space before the text + * currently contained in the outbuf, not before upcoming text. + * Must make sure to go back to OUST_NL/wantws mode before returning. */ static void ordinary(struct state *st, const char *buf, size_t start, size_t end) { - size_t i, j, opstack; - int seq; + size_t i, j, opstack, wend; + enum mdoc_type mtype; + int eos, noeos, seq; + char savechar; if ( ! st->parsing || st->paused) return; @@ -1038,7 +1399,8 @@ ordinary(struct state *st, const char *buf, size_t sta */ if (SECT_NAME == st->sect) { for (i = end - 2; i > start; i--) - if ('-' == buf[i] && ' ' == buf[i + 1]) + if ('-' == buf[i] && + isspace((unsigned char)buf[i + 1])) break; if ('-' == buf[i]) { j = i; @@ -1048,11 +1410,11 @@ ordinary(struct state *st, const char *buf, size_t sta break; donamenm(st, buf, &start, i + 1); start = j + 1; - while (start < end && ' ' == buf[start]) + while (start < end && + isspace((unsigned char)buf[start])) start++; - fputs(".Nd ", stdout); - formatcodeln(st, buf, &start, end, 1); - putchar('\n'); + formatcodeln(st, "Nd", buf, &start, end, 1); + mdoc_newln(st); return; } } @@ -1070,63 +1432,178 @@ ordinary(struct state *st, const char *buf, size_t sta * Escape initial control characters. */ while (start < end) { - if (start < end - 1 && '<' == buf[start + 1]) + if (start < end - 1 && '<' == buf[start + 1] && + 'A' <= buf[start] && 'Z' >= buf[start]) break; else if ('\n' == buf[start]) break; else if ('\n' == last && '.' == buf[start]) - printf("\\&"); + outbuf_addstr(st, "\\&"); else if ('\n' == last && '\'' == buf[start]) - printf("\\&"); + outbuf_addstr(st, "\\&"); /* * If we're in the SYNOPSIS, have square * brackets indicate that we're opening and * closing an optional context. */ + if (SECT_SYNOPSIS == st->sect && ('[' == buf[start] || ']' == buf[start]) && - dosynopsisop(buf, &last, - &start, end, &opstack)) + dosynopsisop(st, buf, + &start, end, &opstack)) continue; - putchar(last = buf[start++]); - if ('\\' == last) - putchar('e'); - } - if (start < end - 1 && '<' == buf[start + 1]) { + /* Merely buffer non-whitespace. */ + + last = buf[start++]; + if ( ! isspace(last)) + outbuf_addchar(st); + if (start < end && + ! isspace((unsigned char)buf[start - 1]) && + ! isspace((unsigned char)buf[start])) + continue; + /* - * We've encountered a format code. - * This is going to trigger a macro no matter - * what, so print a newline now. - * Then print the (possibly nested) macros and - * following that, a newline. - * Consume all whitespace so we don't - * accidentally start an implicit literal line. - * If the macro ends with a flush comma or - * period, let mdoc(7) handle it for us. + * Found the end of a word. + * Rewind trailing delimiters. */ - if (formatcode(st, buf, &start, end, 0, 0, seq)) { - if ((start == end - 1 || - (start < end - 1 && - (' ' == buf[start + 1] || - '\n' == buf[start + 1]))) && - ('.' == buf[start] || - ',' == buf[start])) { + + eos = noeos = 0; + for (wend = st->outbuflen; wend; wend--) + if ('.' == st->outbuf[wend - 1] || + '!' == st->outbuf[wend - 1] || + '?' == st->outbuf[wend - 1]) + eos = 1; + else if ('|' == st->outbuf[wend - 1] || + ',' == st->outbuf[wend - 1] || + ';' == st->outbuf[wend - 1] || + ':' == st->outbuf[wend - 1]) + noeos = 1; + else if ('\'' != st->outbuf[wend - 1] && + '"' != st->outbuf[wend - 1] && + ')' != st->outbuf[wend - 1] && + ']' != st->outbuf[wend - 1]) + break; + eos &= ! noeos; + + /* + * Detect function names. + */ + + mtype = MDOC_Fa; + savechar = '\0'; + if (wend && ')' == st->outbuf[wend] && + '(' == st->outbuf[wend - 1]) { + mtype = dict_get(st->outbuf, --wend); + if (MDOC_Dv == mtype) + mtype = MDOC_Fo; + if (MDOC_Fo == mtype || MDOC_MAX == mtype) { + st->outbuflen = wend; + st->outbuf[wend] = '\0'; + mdoc_newln(st); + if (MDOC_Fo == mtype) + fputs(".Fn ", stdout); + else + fputs(".Xr ", stdout); + st->oust = OUST_MAC; + } + } else { + mtype = dict_get(st->outbuf, wend); + if (MDOC_Dv == mtype) { + savechar = st->outbuf[wend]; + st->outbuf[wend] = '\0'; + mdoc_newln(st); + fputs(".Dv ", stdout); + st->oust = OUST_MAC; + } else + mtype = MDOC_Fa; + } + + /* + * On whitespace, flush the output buffer + * and allow breaking to a macro line. + */ + + outbuf_flush(st); + + /* + * End macro lines, and + * end text lines at the end of sentences. + */ + + if (OUST_MAC == st->oust || (eos && wend > 1 && + islower((unsigned char)st->outbuf[wend - 1]))) { + if (MDOC_MAX == mtype) + fputs(" 3", stdout); + if (MDOC_Fa != mtype) { + if (MDOC_Dv == mtype) + st->outbuf[wend] = savechar; + else + wend += 2; + while ('\0' != st->outbuf[wend]) + printf(" %c", + st->outbuf[wend++]); + } + mdoc_newln(st); + } + + /* Advance to the next word. */ + + while ('\n' != buf[start] && + isspace((unsigned char)buf[start])) + start++; + st->wantws = 1; + } + + if (start < end - 1 && '<' == buf[start + 1] && + 'A' <= buf[start] && 'Z' >= buf[start]) { + formatcode(st, buf, &start, end, 0, seq); + if (OUST_MAC == st->oust) { + /* + * Let mdoc(7) handle trailing punctuation. + * XXX Some punctuation characters + * are not handled yet. + */ + if ((start == end - 1 || + (start < end - 1 && + (' ' == buf[start + 1] || + '\n' == buf[start + 1]))) && + NULL != strchr("|.,;:?!)]", buf[start])) { putchar(' '); putchar(buf[start++]); } - putchar(last = '\n'); + + if (st->wantws || + ' ' == buf[start] || + '\n' == buf[start]) + mdoc_newln(st); + + /* + * Consume all whitespace + * so we don't accidentally start + * an implicit literal line. + */ + while (start < end && ' ' == buf[start]) start++; + + /* + * Some text is following. + * Implement requested spacing. + */ + + if ( ! st->wantws && start < end && + ('<' != buf[start + 1] || + 'A' > buf[start] || + 'Z' < buf[start])) { + printf(" Ns "); + st->wantws = 1; + } } } else if (start < end && '\n' == buf[start]) { - /* - * Print the newline only if we haven't already - * printed a newline. - */ - if (last != '\n') - putchar(last = buf[start]); + outbuf_flush(st); + mdoc_newln(st); if (++start >= end) continue; /* @@ -1137,18 +1614,15 @@ ordinary(struct state *st, const char *buf, size_t sta * have a macro subsequent it, which may be * possible if we have an escape next. */ - if (' ' == buf[start] || '\t' == buf[start]) { + if (' ' == buf[start] || '\t' == buf[start]) puts(".br"); - last = '\n'; - } for ( ; start < end; start++) if (' ' != buf[start] && '\t' != buf[start]) break; } } - - if (last != '\n') - putchar('\n'); + outbuf_flush(st); + mdoc_newln(st); } /* @@ -1157,9 +1631,12 @@ ordinary(struct state *st, const char *buf, size_t sta * (default: starts with "="). */ static void -dopar(struct state *st, const char *buf, size_t start, size_t end) +dopar(struct state *st, char *buf, size_t start, size_t end) { + assert(OUST_NL == st->oust); + assert(st->wantws); + if (end == start) return; if (' ' == buf[start] || '\t' == buf[start]) @@ -1176,26 +1653,43 @@ dopar(struct state *st, const char *buf, size_t start, */ static void dofile(const struct args *args, const char *fname, - const struct tm *tm, const char *buf, size_t sz) + const struct tm *tm, char *buf, size_t sz) { - size_t sup, end, i, cur = 0; - struct state st; - const char *section, *date; char datebuf[64]; + struct state st; + const char *fbase, *fext, *section, *date, *format; char *title, *cp; + size_t cur, end; + int verb; if (0 == sz) return; - /* Title is last path component of the filename. */ + /* + * Parsing the filename is almost always required, + * except when both the title and the section + * are provided on the command line. + */ - if (NULL != args->title) - title = strdup(args->title); - else if (NULL != (cp = strrchr(fname, '/'))) - title = strdup(cp + 1); - else - title = strdup(fname); - + if (NULL == args->title || NULL == args->section) { + fbase = strrchr(fname, '/'); + if (NULL == fbase) + fbase = fname; + else + fbase++; + fext = strrchr(fbase, '.'); + } else + fext = NULL; + + /* + * The title will be converted to uppercase, + * so it needs to be copied. + */ + + title = (NULL != args->title) ? strdup(args->title) : + (NULL != fext) ? strndup(fbase, fext - fbase) : + strdup(fbase); + if (NULL == title) { perror(NULL); exit(EXIT_FAILURE); @@ -1203,19 +1697,18 @@ dofile(const struct args *args, const char *fname, /* Section is 1 unless suffix is "pm". */ - if (NULL == (section = args->section)) { - section = "1"; - if (NULL != (cp = strrchr(title, '.'))) { - *cp++ = '\0'; - if (0 == strcmp(cp, "pm")) - section = PERL_SECTION; - } - } + section = (NULL != args->section) ? args->section : + (NULL == fext || strcmp(fext + 1, "pm")) ? "1" : + PERL_SECTION; /* Date. Or the given "tm" if not supplied. */ - if (NULL == (date = args->date)) { - strftime(datebuf, sizeof(datebuf), "%B %d, %Y", tm); + date = args->date; + format = (NULL == date) ? "%B %d, %Y" : + strcmp(date, "Mdocdate") ? NULL : "$" "Mdocdate: %B %d %Y $"; + + if (NULL != format) { + strftime(datebuf, sizeof(datebuf), format, tm); date = datebuf; } @@ -1230,30 +1723,43 @@ dofile(const struct args *args, const char *fname, free(title); + dict_init(); memset(&st, 0, sizeof(struct state)); + st.oust = OUST_NL; + st.wantws = 1; + assert(sz > 0); /* Main loop over file contents. */ - while (cur < sz) { + cur = 0; + for (;;) { + while (cur < sz && '\n' == buf[cur]) + cur++; + if (cur >= sz) + break; + + verb = isspace((unsigned char)buf[cur]); + /* Read until next paragraph. */ - for (i = cur + 1; i < sz; i++) - if ('\n' == buf[i] && '\n' == buf[i - 1]) { - /* Consume blank paragraphs. */ - while (i + 1 < sz && '\n' == buf[i + 1]) - i++; + + for (end = cur + 1; end + 1 < sz; end++) + if ('\n' == buf[end] && '\n' == buf[end + 1] && + !(verb && end + 2 < sz && + isspace((unsigned char)buf[end + 2]))) break; - } /* Adjust end marker for EOF. */ - end = i < sz ? i - 1 : - ('\n' == buf[sz - 1] ? sz - 1 : sz); - sup = i < sz ? end + 2 : sz; + if (end < sz && '\n' != buf[end]) + end++; + /* Process paragraph and adjust start. */ + dopar(&st, buf, cur, end); - cur = sup; + cur = end + 2; } + dict_destroy(); } /*