[BACK]Return to pod2mdoc.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / pod2mdoc

Diff for /pod2mdoc/pod2mdoc.c between version 1.46 and 1.58

version 1.46, 2015/02/14 15:34:39 version 1.58, 2015/02/23 14:30:29
Line 75  struct state {
Line 75  struct state {
         char            *outbuf; /* text buffered for output */          char            *outbuf; /* text buffered for output */
         size_t           outbufsz; /* allocated size of outbuf */          size_t           outbufsz; /* allocated size of outbuf */
         size_t           outbuflen; /* current length of outbuf */          size_t           outbuflen; /* current length of outbuf */
           size_t           outlnlen; /* chars so far on this output line */
 };  };
   
 enum    fmt {  enum    fmt {
Line 107  enum cmd {
Line 108  enum cmd {
         CMD__MAX          CMD__MAX
 };  };
   
   static void      command(struct state *, const char *, size_t, size_t);
   static void      dofile(const struct args *, const char *,
                           const struct tm *, char *, size_t);
   static void      donamenm(struct state *, const char *, size_t *, size_t);
   static void      dopar(struct state *, char *, size_t, size_t);
   static void      dosynopsisfl(const char *, size_t *, size_t);
   static int       dosynopsisop(struct state *, const char *, size_t *,
                           size_t, size_t *);
   static int       formatcode(struct state *, const char *, size_t *,
                           size_t, int, int);
   static void      formatcodeln(struct state *, const char *, const char *,
                           size_t *, size_t, int);
   static void      formatescape(struct state *, const char *, size_t *, size_t);
   static int       hasmatch(const char *, size_t, size_t);
   static void      ordinary(struct state *, const char *, size_t, size_t);
   static void      outbuf_addchar(struct state *);
   static void      outbuf_addstr(struct state *, const char *);
   static void      outbuf_flush(struct state *);
   static void      outbuf_grow(struct state *, size_t);
   static enum list listguess(const char *, size_t, size_t);
   static void      mdoc_newln(struct state *);
   static int       readfile(const struct args *, const char *);
   static void      register_type(const char *);
   static int       trylink(const char *, size_t *, size_t, size_t);
   static void      verbatim(struct state *, char *, size_t, size_t);
   
 static  const char *const cmds[CMD__MAX] = {  static  const char *const cmds[CMD__MAX] = {
         "pod",          /* CMD_POD */          "pod",          /* CMD_POD */
         "head1",        /* CMD_HEAD1 */          "head1",        /* CMD_HEAD1 */
Line 182  outbuf_flush(struct state *st)
Line 209  outbuf_flush(struct state *st)
         if (0 == st->outbuflen)          if (0 == st->outbuflen)
                 return;                  return;
   
         if (OUST_TXT == st->oust && st->wantws)          st->outlnlen += st->outbuflen;
           if (OUST_TXT == st->oust && st->wantws) {
                   if (++st->outlnlen > 72) {
                           putchar('\n');
                           st->oust = OUST_NL;
                           st->outlnlen = st->outbuflen;
                   }
           }
           if (OUST_NL != st->oust && st->wantws)
                 putchar(' ');                  putchar(' ');
   
         fputs(st->outbuf, stdout);          if (OUST_MAC == st->oust && '"' == *st->outbuf)
                   printf("\\(dq%s", st->outbuf + 1);
           else
                   fputs(st->outbuf, stdout);
   
         *st->outbuf = '\0';          *st->outbuf = '\0';
         st->outbuflen = 0;          st->outbuflen = 0;
   
Line 203  mdoc_newln(struct state *st)
Line 242  mdoc_newln(struct state *st)
         putchar('\n');          putchar('\n');
         last = '\n';          last = '\n';
         st->oust = OUST_NL;          st->oust = OUST_NL;
           st->outlnlen = 0;
         st->wantws = 1;          st->wantws = 1;
 }  }
   
Line 411  again:
Line 451  again:
                 '?' != buf[*start + 1] &&                  '?' != buf[*start + 1] &&
                 '-' != buf[*start + 1]) {                  '-' != buf[*start + 1]) {
                 (*start)--;                  (*start)--;
                 fputs("Ar ", stdout);                  fputs("Ar", stdout);
                 return;                  return;
         }          }
   
Line 431  again:
Line 471  again:
         assert(i < end);          assert(i < end);
   
         if ( ! (' ' == buf[i] || '>' == buf[i])) {          if ( ! (' ' == buf[i] || '>' == buf[i])) {
                 printf("Ar ");                  fputs("Ar", stdout);
                 return;                  return;
         }          }
   
Line 442  again:
Line 482  again:
                 (end - *start == 2 ||                  (end - *start == 2 ||
                  ' ' == buf[*start + 2]))                   ' ' == buf[*start + 2]))
                 printf("\\&");                  printf("\\&");
         printf("%.*s ", (int)(i - *start), &buf[*start]);          printf("%.*s", (int)(i - *start), &buf[*start]);
         *start = i;          *start = i;
   
         if (' ' == buf[i]) {          if (' ' == buf[i]) {
Line 453  again:
Line 493  again:
                         *start = i;                          *start = i;
                         goto again;                          goto again;
                 }                  }
                 printf("Ar ");                  fputs("Ar", stdout);
                 *start = i;                  *start = i;
         }          }
 }  }
Line 480  formatcode(struct state *st, const char *buf, size_t *
Line 520  formatcode(struct state *st, const char *buf, size_t *
 {  {
         size_t           i, j, dsz;          size_t           i, j, dsz;
         enum fmt         fmt;          enum fmt         fmt;
         int              wantws;  
         unsigned char    uc;          unsigned char    uc;
           int              gotmacro, wantws;
   
         assert(*start + 1 < end);          assert(*start + 1 < end);
         assert('<' == buf[*start + 1]);          assert('<' == buf[*start + 1]);
Line 559  formatcode(struct state *st, const char *buf, size_t *
Line 599  formatcode(struct state *st, const char *buf, size_t *
         if (FMT__MAX != fmt && !nomacro) {          if (FMT__MAX != fmt && !nomacro) {
   
                 /*                  /*
                    * Do we need spacing before the upcoming macro,
                    * after any pending text already in the outbuf?
                  * We may already have wantws if there was whitespace                   * We may already have wantws if there was whitespace
                  * before the code ("text B<text"), but initial                   * before the code ("text B<text"), or there may be
                  * whitespace inside our scope ("textB< text")                   * whitespace inside our scope ("textB< text").
                  * allows to break at this point as well.  
                  */                   */
   
                 wantws = ' ' == buf[*start] ||                  wantws = ' ' == buf[*start] ||
                     (OUST_MAC == st->oust ? st->wantws : ! st->outbuflen);                      (st->wantws && ! st->outbuflen);
   
                 /*                  /*
                  * If we are on a text line and there is no                   * If we are on a text line and there is no
                  * whitespace before our content, we have to make                   * whitespace before our content, we have to make
                  * the previous word a prefix to the macro line.                   * the previous word a prefix to the macro line.
                  * In the following, mdoc_newln() must not be used  
                  * lest we clobber out output state.  
                  */                   */
   
                 if (OUST_MAC != st->oust && ! wantws) {                  if (OUST_MAC != st->oust && ! wantws) {
                         if (OUST_NL != st->oust)                          if (OUST_NL != st->oust)
                                 putchar('\n');                                  mdoc_newln(st);
                         printf(".Pf ");                          fputs(".Pf", stdout);
                         st->wantws = 0;                          st->oust = OUST_MAC;
                           st->wantws = wantws = 1;
                 }                  }
   
                 outbuf_flush(st);                  outbuf_flush(st);
Line 588  formatcode(struct state *st, const char *buf, size_t *
Line 628  formatcode(struct state *st, const char *buf, size_t *
                 /* Whitespace is easier to suppress on macro lines. */                  /* Whitespace is easier to suppress on macro lines. */
   
                 if (OUST_MAC == st->oust && ! wantws)                  if (OUST_MAC == st->oust && ! wantws)
                         printf(" Ns ");                          printf(" Ns");
   
                 /* Unless we are on a macro line, start one. */                  /* Unless we are on a macro line, start one. */
   
                 if (OUST_MAC != st->oust && wantws) {                  if (OUST_MAC != st->oust) {
                         if (OUST_NL != st->oust)                          if (OUST_NL != st->oust)
                                 putchar('\n');                                  mdoc_newln(st);
                         putchar('.');                          putchar('.');
                           st->oust = OUST_MAC;
                 } else                  } else
                         putchar(' ');                          putchar(' ');
                   st->wantws = 1;
   
                 /*                  /*
                  * Print the macro corresponding to this format code,                   * Print the macro corresponding to this format code,
Line 606  formatcode(struct state *st, const char *buf, size_t *
Line 648  formatcode(struct state *st, const char *buf, size_t *
   
                 switch (fmt) {                  switch (fmt) {
                 case (FMT_ITALIC):                  case (FMT_ITALIC):
                         printf("Em ");                          fputs("Em", stdout);
                         break;                          break;
                 case (FMT_BOLD):                  case (FMT_BOLD):
                         if (SECT_SYNOPSIS == st->sect) {                          if (SECT_SYNOPSIS == st->sect) {
                                 if (1 == dsz && '-' == buf[*start])                                  if (1 == dsz && '-' == buf[*start])
                                         dosynopsisfl(buf, start, end);                                          dosynopsisfl(buf, start, end);
                                 else if (0 == pos)                                  else if (0 == pos)
                                         printf("Nm ");                                          fputs("Nm", stdout);
                                 else                                  else
                                         printf("Ar ");                                          fputs("Ar", stdout);
                                 break;                                  break;
                         }                          }
                         i = 0;                          i = 0;
Line 625  formatcode(struct state *st, const char *buf, size_t *
Line 667  formatcode(struct state *st, const char *buf, size_t *
                         if ('=' != uc && '>' != uc)                          if ('=' != uc && '>' != uc)
                                 i = 0;                                  i = 0;
                         if (4 == i && ! strncmp(buf + *start, "NULL", 4)) {                          if (4 == i && ! strncmp(buf + *start, "NULL", 4)) {
                                 printf("Dv ");                                  fputs("Dv", stdout);
                                 break;                                  break;
                         }                          }
                         switch (i ? dict_get(buf + *start, i) : MDOC_MAX) {                          switch (i ? dict_get(buf + *start, i) : MDOC_MAX) {
                         case MDOC_Fa:                          case MDOC_Fa:
                                 printf("Fa ");                                  fputs("Fa", stdout);
                                 break;                                  break;
                         case MDOC_Vt:                          case MDOC_Vt:
                                 printf("Vt ");                                  fputs("Vt", stdout);
                                 break;                                  break;
                         default:                          default:
                                 printf("Sy ");                                  fputs("Sy", stdout);
                                 break;                                  break;
                         }                          }
                         break;                          break;
                 case (FMT_CODE):                  case (FMT_CODE):
                         printf("Qo Li ");                          fputs("Qo Li", stdout);
                         break;                          break;
                 case (FMT_LINK):                  case (FMT_LINK):
                         /* Try to link; use "No" if it's empty. */                          /* Try to link; use "No" if it's empty. */
                         if ( ! trylink(buf, start, end, dsz))                          if ( ! trylink(buf, start, end, dsz))
                                 printf("No ");                                  fputs("No", stdout);
                         break;                          break;
                 case (FMT_FILE):                  case (FMT_FILE):
                         printf("Pa ");                          fputs("Pa", stdout);
                         break;                          break;
                 case (FMT_NBSP):                  case (FMT_NBSP):
                         printf("No ");                          fputs("No", stdout);
                         break;                          break;
                 default:                  default:
                         abort();                          abort();
                 }                  }
                 st->oust = OUST_MAC;          } else {
                 st->wantws = 1;  
         } else  
                 outbuf_flush(st);                  outbuf_flush(st);
                   st->wantws = 0;
           }
   
         /*          /*
          * Process until we reach the end marker (e.g., '>') or until we           * Process until we reach the end marker (e.g., '>') or until we
Line 668  formatcode(struct state *st, const char *buf, size_t *
Line 710  formatcode(struct state *st, const char *buf, size_t *
          * Don't emit any newlines: since we're on a macro line, we           * Don't emit any newlines: since we're on a macro line, we
          * don't want to break the line.           * don't want to break the line.
          */           */
   
           gotmacro = 0;
         while (*start < end) {          while (*start < end) {
                 if ('>' == buf[*start] && 1 == dsz) {                  if ('>' == buf[*start] && 1 == dsz) {
                         (*start)++;                          (*start)++;
Line 691  formatcode(struct state *st, const char *buf, size_t *
Line 735  formatcode(struct state *st, const char *buf, size_t *
                 }                  }
                 if (*start + 1 < end && '<' == buf[*start + 1] &&                  if (*start + 1 < end && '<' == buf[*start + 1] &&
                     'A' <= buf[*start] && 'Z' >= buf[*start]) {                      'A' <= buf[*start] && 'Z' >= buf[*start]) {
                         if ( ! formatcode(st, buf, start, end, nomacro, 1))                          gotmacro = formatcode(st, buf,
                                 st->wantws = 1;                              start, end, nomacro, 1);
                         continue;                          continue;
                 }                  }
   
                 /* Suppress newlines and multiple spaces. */                  /* Suppress newlines and multiple spaces. */
   
                 last = buf[(*start)++];                  last = buf[(*start)++];
                 if (' ' == last || '\n' == last) {                  if (isspace(last)) {
                         putchar(' ');                          outbuf_flush(st);
                         while (*start < end && ' ' == buf[*start])                          st->wantws = 1;
                           gotmacro = 0;
                           while (*start < end &&
                               isspace((unsigned char)buf[*start]))
                                 (*start)++;                                  (*start)++;
                         continue;                          continue;
                 }                  }
   
                 if (OUST_MAC == st->oust && FMT__MAX != fmt) {                  if (OUST_MAC == st->oust && FMT__MAX != fmt) {
                         if ( ! st->wantws) {                          if (gotmacro && ! st->wantws) {
                                 printf(" Ns ");                                  printf(" Ns");
                                 st->wantws = 1;                                  st->wantws = 1;
                         }                          }
                           gotmacro = 0;
   
                         /*                          /*
                          * Escape macro-like words.                           * Escape macro-like words.
                          * This matches "Xx " and "XxEOLN".                           * This matches "Xx " and "XxEOLN".
                          */                           */
   
                         if (end - *start > 0 &&                          if (*start < end && ! st->outbuflen &&
                             isupper((unsigned char)last) &&                              isupper(last) &&
                             islower((unsigned char)buf[*start]) &&                              islower((unsigned char)buf[*start]) &&
                             (end - *start == 1 ||                              (end - *start == 1 ||
                              ' ' == buf[*start + 1] ||                               ' ' == buf[*start + 1] ||
                              '>' == buf[*start + 1]))                               '>' == buf[*start + 1]))
                                 printf("\\&");                                  outbuf_addstr(st, "\\&");
                           last = buf[*start - 1];
                 }                  }
                   outbuf_addchar(st);
           }
   
                 putchar(last);          if (FMT__MAX == fmt)
                   return(0);
   
                 /* Protect against character escapes. */          outbuf_flush(st);
   
                 if ('\\' == last)  
                         putchar('e');  
         }  
   
         if ( ! nomacro && FMT_CODE == fmt)          if ( ! nomacro && FMT_CODE == fmt)
                 printf(" Qc ");                  fputs(" Qc", stdout);
   
         st->wantws = ' ' == last;          st->wantws = ' ' == last;
         return(FMT__MAX != fmt);          return(1);
 }  }
   
 /*  /*
Line 751  static void
Line 799  static void
 formatcodeln(struct state *st, const char *linemac,  formatcodeln(struct state *st, const char *linemac,
         const char *buf, size_t *start, size_t end, int nomacro)          const char *buf, size_t *start, size_t end, int nomacro)
 {  {
         int      gotmacro, wantws;          int      gotmacro;
   
         assert(OUST_NL == st->oust);          assert(OUST_NL == st->oust);
         assert(st->wantws);          assert(st->wantws);
         printf(".%s ", linemac);          printf(".%s", linemac);
         st->oust = OUST_MAC;          st->oust = OUST_MAC;
   
         gotmacro = 0;          gotmacro = 0;
         while (*start < end)  {          while (*start < end)  {
                 wantws = ' ' == buf[*start] || '\n' == buf[*start];  
                 if (wantws) {  
                         last = ' ';  
                         do {  
                                 (*start)++;  
                         } while (*start < end && ' ' == buf[*start]);  
                 }  
   
                 if (*start + 1 < end && '<' == buf[*start + 1] &&                  if (*start + 1 < end && '<' == buf[*start + 1] &&
                     'A' <= buf[*start] && 'Z' >= buf[*start]) {                      'A' <= buf[*start] && 'Z' >= buf[*start]) {
                         st->wantws |= wantws;  
                         gotmacro = formatcode(st, buf,                          gotmacro = formatcode(st, buf,
                             start, end, nomacro, 1);                              start, end, nomacro, 1);
                         continue;                          continue;
                 }                  }
   
                   /* Suppress newlines and multiple spaces. */
   
                   last = buf[(*start)++];
                   if (isspace(last)) {
                           outbuf_flush(st);
                           st->wantws = 1;
                           while (*start < end &&
                               isspace((unsigned char)buf[*start]))
                                   (*start)++;
                           continue;
                   }
   
                 if (gotmacro) {                  if (gotmacro) {
                         if (*start < end || st->outbuflen) {                          if (*start < end) {
                                 if (st->wantws ||                                  if (st->wantws)
                                     (wantws && !st->outbuflen))                                          printf(" No");
                                         printf(" No ");  
                                 else                                  else
                                         printf(" Ns ");                                          printf(" Ns");
                         }                          }
                           st->wantws = 1;
                         gotmacro = 0;                          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                   * Since we're already on a macro line, we want to make
                  * sure that we don't inadvertently invoke a macro.                   * sure that we don't inadvertently invoke a macro.
Line 804  formatcodeln(struct state *st, const char *linemac,
Line 845  formatcodeln(struct state *st, const char *linemac,
                  * are used in troff and we don't want to escape                   * are used in troff and we don't want to escape
                  * something that needn't be escaped.                   * something that needn't be escaped.
                  */                   */
                 if (' ' == last && end - *start > 1 &&                  if (*start < end && ! st->outbuflen && isupper(last) &&
                     isupper((unsigned char)buf[*start]) &&                      islower((unsigned char)buf[*start]) &&
                     islower((unsigned char)buf[*start + 1]) &&                      (end - *start == 1 || ' ' == buf[*start + 1])) {
                     (end - *start == 2 || ' ' == buf[*start + 2]))                          outbuf_addstr(st, "\\&");
                         printf("\\&");                          last = buf[*start - 1];
                   }
                 putchar(last = buf[*start]);                  outbuf_addchar(st);
   
                 /* Protect against character escapes. */  
   
                 if ('\\' == last)  
                         putchar('e');  
   
                 (*start)++;  
         }          }
           outbuf_flush(st);
           st->wantws = 1;
 }  }
   
 /*  /*
Line 1059  verbatim(struct state *st, char *buf, size_t start, si
Line 1095  verbatim(struct state *st, char *buf, size_t start, si
 {  {
         size_t           i, ift, ifo, ifa, ifc, inl;          size_t           i, ift, ifo, ifa, ifc, inl;
         char            *cp, *cp2;          char            *cp, *cp2;
         int              nopen;          int              indisplay, nopen, wantsp;
   
         if ( ! st->parsing || st->paused || start == end)          if (st->paused || ! st->parsing)
                 return;                  return;
   
           indisplay = wantsp = 0;
   
 again:  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're in the SYNOPSIS, see if we're an #include block.
          * If we are, then print the "In" macro and re-loop.           * If we are, then print the "In" macro and re-loop.
Line 1075  again:
Line 1126  again:
                 while (i < end && buf[i] == ' ')                  while (i < end && buf[i] == ' ')
                         i++;                          i++;
                 if (i == end)                  if (i == end)
                         return;                          goto again;
   
                 /* We're an include block! */                  /* We're an include block! */
                 if (end - i > 10 &&                  if (end - i > 10 &&
Line 1083  again:
Line 1134  again:
                         start = i + 10;                          start = i + 10;
                         while (start < end && ' ' == buf[start])                          while (start < end && ' ' == buf[start])
                                 start++;                                  start++;
                           if (indisplay)
                                   puts(".Ed");
                           indisplay = wantsp = 0;
                         fputs(".In ", stdout);                          fputs(".In ", stdout);
                         /* Stop til the '>' marker or we hit eoln. */                          /* Stop til the '>' marker or we hit eoln. */
                         while (start < end &&                          while (start < end &&
Line 1098  again:
Line 1152  again:
   
                 /* Other preprocessor directives. */                  /* Other preprocessor directives. */
                 if ('#' == buf[i]) {                  if ('#' == buf[i]) {
                           if (indisplay)
                                   puts(".Ed");
                           indisplay = wantsp = 0;
                         fputs(".Fd ", stdout);                          fputs(".Fd ", stdout);
                         start = i;                          start = i;
                         while(start < end && '\n' != buf[start])                          while(start < end && '\n' != buf[start])
Line 1105  again:
Line 1162  again:
                         putchar('\n');                          putchar('\n');
                         if (start < end && '\n' == buf[start])                          if (start < end && '\n' == buf[start])
                                 start++;                                  start++;
   
                           /* Remember #define for Dv or Fn. */
   
                           if (strncmp(buf + i + 1, "define", 6) ||
                               ! isspace((unsigned char)buf[i + 7]))
                                   goto again;
   
                           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;                          goto again;
                 }                  }
   
Line 1159  again:
Line 1234  again:
                                         buf[i] = ' ';                                          buf[i] = ' ';
                         buf[ifo++] = '\0';                          buf[ifo++] = '\0';
                         register_type(buf + ift);                          register_type(buf + ift);
                           if (indisplay)
                                   puts(".Ed");
                           indisplay = wantsp = 0;
                         printf(".Ft %s", buf + ift);                          printf(".Ft %s", buf + ift);
                         if (buf[ifo] == '*') {                          if (buf[ifo] == '*') {
                                 fputs(" *", stdout);                                  fputs(" *", stdout);
Line 1182  again:
Line 1260  again:
                                 if ('\0' != *cp2)                                  if ('\0' != *cp2)
                                         dict_put(cp2, 0, MDOC_Fa);                                          dict_put(cp2, 0, MDOC_Fa);
                                 register_type(buf + ifa);                                  register_type(buf + ifa);
                                 printf(".Fa \"%s\"\n", buf + ifa);                                  if (strchr(buf + ifa, ' ') == NULL)
                                           printf(".Fa %s\n", buf + ifa);
                                   else
                                           printf(".Fa \"%s\"\n", buf + ifa);
                                 if (cp == NULL)                                  if (cp == NULL)
                                         break;                                          break;
                                 while (*cp == ' ' || *cp == '\t')                                  while (*cp == ' ' || *cp == '\t')
Line 1196  again:
Line 1277  again:
                                 buf[inl] = '\0';                                  buf[inl] = '\0';
                                 puts(buf + ifc);                                  puts(buf + ifc);
                         }                          }
                         start = inl + 1;                          start = inl < end ? inl + 1 : end;
                         if (start < end)                          goto again;
                                 goto again;  
                         return;  
                 }                  }
         }          }
   
         puts(".Bd -literal");          if ( ! indisplay)
         for (last = ' '; start < end; start++) {                  puts(".Bd -literal");
           else if (wantsp)
                   putchar('\n');
           indisplay = 1;
           wantsp = 0;
   
           for (last = '\n'; start < end; start++) {
                 /*                  /*
                  * Handle accidental macros (newline starting with                   * Handle accidental macros (newline starting with
                  * control character) and escapes.                   * control character) and escapes.
                  */                   */
                 if ('\n' == last)                  if ('\n' == last) {
                           if ('\n' == buf[start])
                                   goto again;
                         if ('.' == buf[start] || '\'' == buf[start])                          if ('.' == buf[start] || '\'' == buf[start])
                                 printf("\\&");                                  printf("\\&");
                   }
                 putchar(last = buf[start]);                  putchar(last = buf[start]);
                 if ('\\' == buf[start])                  if ('\\' == buf[start])
                         printf("e");                          printf("e");
         }          }
         putchar(last = '\n');          if ('\n' != last)
         puts(".Ed");                  putchar('\n');
           if (indisplay)
                   puts(".Ed");
 }  }
   
 /*  /*
Line 1287  donamenm(struct state *st, const char *buf, size_t *st
Line 1377  donamenm(struct state *st, const char *buf, size_t *st
         assert(OUST_NL == st->oust);          assert(OUST_NL == st->oust);
         assert(st->wantws);          assert(st->wantws);
   
         while (*start < end && ' ' == buf[*start])          while (*start < end && isspace((unsigned char)buf[*start]))
                 (*start)++;                  (*start)++;
   
         if (end == *start) {          if (end == *start) {
Line 1308  donamenm(struct state *st, const char *buf, size_t *st
Line 1398  donamenm(struct state *st, const char *buf, size_t *st
                 printf(" ,");                  printf(" ,");
                 mdoc_newln(st);                  mdoc_newln(st);
                 (*start)++;                  (*start)++;
                 while (*start < end && ' ' == buf[*start])                  while (*start < end && isspace((unsigned char)buf[*start]))
                         (*start)++;                          (*start)++;
         }          }
 }  }
Line 1333  ordinary(struct state *st, const char *buf, size_t sta
Line 1423  ordinary(struct state *st, const char *buf, size_t sta
         size_t          i, j, opstack, wend;          size_t          i, j, opstack, wend;
         enum mdoc_type  mtype;          enum mdoc_type  mtype;
         int             eos, noeos, seq;          int             eos, noeos, seq;
           char            savechar;
   
         if ( ! st->parsing || st->paused)          if ( ! st->parsing || st->paused)
                 return;                  return;
Line 1345  ordinary(struct state *st, const char *buf, size_t sta
Line 1436  ordinary(struct state *st, const char *buf, size_t sta
          */           */
         if (SECT_NAME == st->sect) {          if (SECT_NAME == st->sect) {
                 for (i = end - 2; i > start; i--)                  for (i = end - 2; i > start; i--)
                         if ('-' == buf[i] && ' ' == buf[i + 1])                          if ('-' == buf[i] &&
                               isspace((unsigned char)buf[i + 1]))
                                 break;                                  break;
                 if ('-' == buf[i]) {                  if ('-' == buf[i]) {
                         j = i;                          j = i;
Line 1355  ordinary(struct state *st, const char *buf, size_t sta
Line 1447  ordinary(struct state *st, const char *buf, size_t sta
                                         break;                                          break;
                         donamenm(st, buf, &start, i + 1);                          donamenm(st, buf, &start, i + 1);
                         start = j + 1;                          start = j + 1;
                         while (start < end && ' ' == buf[start])                          while (start < end &&
                                isspace((unsigned char)buf[start]))
                                 start++;                                  start++;
                           while (start < end && '.' == buf[end - 1])
                                   end--;
                         formatcodeln(st, "Nd", buf, &start, end, 1);                          formatcodeln(st, "Nd", buf, &start, end, 1);
                         mdoc_newln(st);                          mdoc_newln(st);
                         return;                          return;
Line 1404  ordinary(struct state *st, const char *buf, size_t sta
Line 1499  ordinary(struct state *st, const char *buf, size_t sta
                         if ( ! isspace(last))                          if ( ! isspace(last))
                                 outbuf_addchar(st);                                  outbuf_addchar(st);
                         if (start < end &&                          if (start < end &&
                               ! isspace((unsigned char)buf[start - 1]) &&
                             ! isspace((unsigned char)buf[start]))                              ! isspace((unsigned char)buf[start]))
                                 continue;                                  continue;
   
Line 1435  ordinary(struct state *st, const char *buf, size_t sta
Line 1531  ordinary(struct state *st, const char *buf, size_t sta
                          */                           */
   
                         mtype = MDOC_Fa;                          mtype = MDOC_Fa;
                           savechar = '\0';
                         if (wend && ')' == st->outbuf[wend] &&                          if (wend && ')' == st->outbuf[wend] &&
                             '(' == st->outbuf[wend - 1]) {                              '(' == st->outbuf[wend - 1]) {
                                 mtype = dict_get(st->outbuf, --wend);                                  mtype = dict_get(st->outbuf, --wend);
                                   if (MDOC_Dv == mtype)
                                           mtype = MDOC_Fo;
                                 if (MDOC_Fo == mtype || MDOC_MAX == mtype) {                                  if (MDOC_Fo == mtype || MDOC_MAX == mtype) {
                                         st->outbuflen = wend;                                          st->outbuflen = wend;
                                         st->outbuf[wend] = '\0';                                          st->outbuf[wend] = '\0';
                                         mdoc_newln(st);                                          mdoc_newln(st);
                                         if (MDOC_Fo == mtype)                                          if (MDOC_Fo == mtype)
                                                 fputs(".Fn ", stdout);                                                  fputs(".Fn", stdout);
                                         else                                          else
                                                 fputs(".Xr ", stdout);                                                  fputs(".Xr", stdout);
                                         st->oust = OUST_MAC;                                          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;
                         }                          }
   
                         /*                          /*
Line 1466  ordinary(struct state *st, const char *buf, size_t sta
Line 1575  ordinary(struct state *st, const char *buf, size_t sta
                             islower((unsigned char)st->outbuf[wend - 1]))) {                              islower((unsigned char)st->outbuf[wend - 1]))) {
                                 if (MDOC_MAX == mtype)                                  if (MDOC_MAX == mtype)
                                         fputs(" 3", stdout);                                          fputs(" 3", stdout);
                                 if (MDOC_Fa != mtype)                                  if (MDOC_Fa != mtype) {
                                         for (wend += 2;                                          if (MDOC_Dv == mtype)
                                              '\0' != st->outbuf[wend];                                                  st->outbuf[wend] = savechar;
                                              wend++)                                          else
                                                   wend += 2;
                                           while ('\0' != st->outbuf[wend])
                                                 printf(" %c",                                                  printf(" %c",
                                                     st->outbuf[wend]);                                                      st->outbuf[wend++]);
                                   }
                                 mdoc_newln(st);                                  mdoc_newln(st);
                         }                          }
   
Line 1492  ordinary(struct state *st, const char *buf, size_t sta
Line 1604  ordinary(struct state *st, const char *buf, size_t sta
                                  * XXX Some punctuation characters                                   * XXX Some punctuation characters
                                  *     are not handled yet.                                   *     are not handled yet.
                                  */                                   */
                                 if ((start == end - 1 ||                                  if ((start == end - 1 ||
                                         (start < end - 1 &&                                       (start < end - 1 &&
                                          (' ' == buf[start + 1] ||                                        (' ' == buf[start + 1] ||
                                           '\n' == buf[start + 1]))) &&                                         '\n' == buf[start + 1]))) &&
                                         ('.' == buf[start] ||                                      NULL != strchr("|.,;:?!)]", buf[start])) {
                                          ',' == buf[start])) {  
                                         putchar(' ');                                          putchar(' ');
                                         putchar(buf[start++]);                                          putchar(buf[start++]);
                                 }                                  }
Line 1525  ordinary(struct state *st, const char *buf, size_t sta
Line 1636  ordinary(struct state *st, const char *buf, size_t sta
                                     ('<' != buf[start + 1] ||                                      ('<' != buf[start + 1] ||
                                      'A' > buf[start] ||                                       'A' > buf[start] ||
                                      'Z' < buf[start])) {                                       'Z' < buf[start])) {
                                         printf(" Ns ");                                          fputs(" Ns", stdout);
                                         st->wantws = 1;                                          st->wantws = 1;
                                 }                                  }
                         }                          }
                 } else if (start < end && '\n' == buf[start]) {                  } else if (start < end && '\n' == buf[start]) {
                         outbuf_flush(st);                          outbuf_flush(st);
                         mdoc_newln(st);                          st->wantws = 1;
                         if (++start >= end)                          if (++start >= end)
                                 continue;                                  continue;
                         /*                          /*
Line 1542  ordinary(struct state *st, const char *buf, size_t sta
Line 1653  ordinary(struct state *st, const char *buf, size_t sta
                          * have a macro subsequent it, which may be                           * have a macro subsequent it, which may be
                          * possible if we have an escape next.                           * possible if we have an escape next.
                          */                           */
                         if (' ' == buf[start] || '\t' == buf[start])                          if (' ' == buf[start] || '\t' == buf[start]) {
                                   mdoc_newln(st);
                                 puts(".br");                                  puts(".br");
                           }
                         for ( ; start < end; start++)                          for ( ; start < end; start++)
                                 if (' ' != buf[start] && '\t' != buf[start])                                  if (' ' != buf[start] && '\t' != buf[start])
                                         break;                                          break;
Line 1587  dofile(const struct args *args, const char *fname, 
Line 1700  dofile(const struct args *args, const char *fname, 
         struct state     st;          struct state     st;
         const char      *fbase, *fext, *section, *date, *format;          const char      *fbase, *fext, *section, *date, *format;
         char            *title, *cp;          char            *title, *cp;
         size_t           sup, end, i, cur = 0;          size_t           cur, end;
           int              verb;
   
         if (0 == sz)          if (0 == sz)
                 return;                  return;
Line 1632  dofile(const struct args *args, const char *fname, 
Line 1746  dofile(const struct args *args, const char *fname, 
   
         date = args->date;          date = args->date;
         format = (NULL == date) ? "%B %d, %Y" :          format = (NULL == date) ? "%B %d, %Y" :
             strcmp(date, "Mdocdate") ? NULL : "$Mdocdate$";              strcmp(date, "Mdocdate") ? NULL : "$" "Mdocdate: %B %d %Y $";
   
         if (NULL != format) {          if (NULL != format) {
                 strftime(datebuf, sizeof(datebuf), format, tm);                  strftime(datebuf, sizeof(datebuf), format, tm);
Line 1659  dofile(const struct args *args, const char *fname, 
Line 1773  dofile(const struct args *args, const char *fname, 
   
         /* Main loop over file contents. */          /* 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. */                  /* Read until next paragraph. */
                 for (i = cur + 1; i < sz; i++)  
                         if ('\n' == buf[i] && '\n' == buf[i - 1]) {                  for (end = cur + 1; end + 1 < sz; end++)
                                 /* Consume blank paragraphs. */                          if ('\n' == buf[end] && '\n' == buf[end + 1] &&
                                 while (i + 1 < sz && '\n' == buf[i + 1])                              !(verb && end + 2 < sz &&
                                         i++;                                isspace((unsigned char)buf[end + 2])))
                                 break;                                  break;
                         }  
   
                 /* Adjust end marker for EOF. */                  /* 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. */                  /* Process paragraph and adjust start. */
   
                 dopar(&st, buf, cur, end);                  dopar(&st, buf, cur, end);
                 cur = sup;                  cur = end + 2;
         }          }
         dict_destroy();          dict_destroy();
 }  }

Legend:
Removed from v.1.46  
changed lines
  Added in v.1.58

CVSweb