Print this page
9718 update mandoc to 1.14.4

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/mandoc/read.c
          +++ new/usr/src/cmd/mandoc/read.c
   1      -/*      $Id: read.c,v 1.192 2017/07/20 14:36:36 schwarze Exp $ */
        1 +/*      $Id: read.c,v 1.196 2018/07/28 18:34:15 schwarze Exp $ */
   2    2  /*
   3    3   * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
   4      - * Copyright (c) 2010-2017 Ingo Schwarze <schwarze@openbsd.org>
        4 + * Copyright (c) 2010-2018 Ingo Schwarze <schwarze@openbsd.org>
   5    5   * Copyright (c) 2010, 2012 Joerg Sonnenberger <joerg@netbsd.org>
   6    6   *
   7    7   * Permission to use, copy, modify, and distribute this software for any
   8    8   * purpose with or without fee is hereby granted, provided that the above
   9    9   * copyright notice and this permission notice appear in all copies.
  10   10   *
  11   11   * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
  12   12   * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13   13   * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
  14   14   * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
↓ open down ↓ 72 lines elided ↑ open up ↑
  87   87          "Mdocdate found",
  88   88          "Mdocdate missing",
  89   89          "unknown architecture",
  90   90          "operating system explicitly specified",
  91   91          "RCS id missing",
  92   92          "referenced manual not found",
  93   93  
  94   94          "generic style suggestion",
  95   95  
  96   96          "legacy man(7) date format",
       97 +        "normalizing date format to",
  97   98          "lower case character in document title",
  98   99          "duplicate RCS id",
  99      -        "typo in section name",
      100 +        "possible typo in section name",
 100  101          "unterminated quoted argument",
 101  102          "useless macro",
 102  103          "consider using OS macro",
 103  104          "errnos out of order",
 104  105          "duplicate errno",
 105  106          "trailing delimiter",
 106  107          "no blank before trailing delimiter",
 107  108          "fill mode already enabled, skipping",
 108  109          "fill mode already disabled, skipping",
      110 +        "verbatim \"--\", maybe consider using \\(em",
 109  111          "function name without markup",
 110  112          "whitespace at end of input line",
 111  113          "bad comment style",
 112  114  
 113  115          "generic warning",
 114  116  
 115  117          /* related to the prologue */
 116  118          "missing manual title, using UNTITLED",
 117  119          "missing manual title, using \"\"",
 118  120          "missing manual section, using \"\"",
↓ open down ↓ 430 lines elided ↑ open up ↑
 549  551  }
 550  552  
 551  553  static int
 552  554  read_whole_file(struct mparse *curp, const char *file, int fd,
 553  555                  struct buf *fb, int *with_mmap)
 554  556  {
 555  557          struct stat      st;
 556  558          gzFile           gz;
 557  559          size_t           off;
 558  560          ssize_t          ssz;
      561 +        int              gzerrnum, retval;
 559  562  
 560  563          if (fstat(fd, &st) == -1) {
 561  564                  mandoc_vmsg(MANDOCERR_FILE, curp, 0, 0,
 562  565                      "fstat: %s", strerror(errno));
 563  566                  return 0;
 564  567          }
 565  568  
 566  569          /*
 567  570           * If we're a regular file, try just reading in the whole entry
 568  571           * via mmap().  This is faster than reading it into blocks, and
↓ open down ↓ 7 lines elided ↑ open up ↑
 576  579                          return 0;
 577  580                  }
 578  581                  *with_mmap = 1;
 579  582                  fb->sz = (size_t)st.st_size;
 580  583                  fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
 581  584                  if (fb->buf != MAP_FAILED)
 582  585                          return 1;
 583  586          }
 584  587  
 585  588          if (curp->gzip) {
      589 +                /*
      590 +                 * Duplicating the file descriptor is required
      591 +                 * because we will have to call gzclose(3)
      592 +                 * to free memory used internally by zlib,
      593 +                 * but that will also close the file descriptor,
      594 +                 * which this function must not do.
      595 +                 */
      596 +                if ((fd = dup(fd)) == -1) {
      597 +                        mandoc_vmsg(MANDOCERR_FILE, curp, 0, 0,
      598 +                            "dup: %s", strerror(errno));
      599 +                        return 0;
      600 +                }
 586  601                  if ((gz = gzdopen(fd, "rb")) == NULL) {
 587  602                          mandoc_vmsg(MANDOCERR_FILE, curp, 0, 0,
 588  603                              "gzdopen: %s", strerror(errno));
      604 +                        close(fd);
 589  605                          return 0;
 590  606                  }
 591  607          } else
 592  608                  gz = NULL;
 593  609  
 594  610          /*
 595  611           * If this isn't a regular file (like, say, stdin), then we must
 596  612           * go the old way and just read things in bit by bit.
 597  613           */
 598  614  
 599  615          *with_mmap = 0;
 600  616          off = 0;
      617 +        retval = 0;
 601  618          fb->sz = 0;
 602  619          fb->buf = NULL;
 603  620          for (;;) {
 604  621                  if (off == fb->sz) {
 605  622                          if (fb->sz == (1U << 31)) {
 606  623                                  mandoc_msg(MANDOCERR_TOOLARGE, curp,
 607  624                                      0, 0, NULL);
 608  625                                  break;
 609  626                          }
 610  627                          resize_buf(fb, 65536);
 611  628                  }
 612  629                  ssz = curp->gzip ?
 613  630                      gzread(gz, fb->buf + (int)off, fb->sz - off) :
 614  631                      read(fd, fb->buf + (int)off, fb->sz - off);
 615  632                  if (ssz == 0) {
 616  633                          fb->sz = off;
 617      -                        return 1;
      634 +                        retval = 1;
      635 +                        break;
 618  636                  }
 619  637                  if (ssz == -1) {
 620      -                        mandoc_vmsg(MANDOCERR_FILE, curp, 0, 0,
 621      -                            "read: %s", strerror(errno));
      638 +                        if (curp->gzip)
      639 +                                (void)gzerror(gz, &gzerrnum);
      640 +                        mandoc_vmsg(MANDOCERR_FILE, curp, 0, 0, "read: %s",
      641 +                            curp->gzip && gzerrnum != Z_ERRNO ?
      642 +                            zError(gzerrnum) : strerror(errno));
 622  643                          break;
 623  644                  }
 624  645                  off += (size_t)ssz;
 625  646          }
 626  647  
 627      -        free(fb->buf);
 628      -        fb->buf = NULL;
 629      -        return 0;
      648 +        if (curp->gzip && (gzerrnum = gzclose(gz)) != Z_OK)
      649 +                mandoc_vmsg(MANDOCERR_FILE, curp, 0, 0, "gzclose: %s",
      650 +                    gzerrnum == Z_ERRNO ? strerror(errno) :
      651 +                    zError(gzerrnum));
      652 +        if (retval == 0) {
      653 +                free(fb->buf);
      654 +                fb->buf = NULL;
      655 +        }
      656 +        return retval;
 630  657  }
 631  658  
 632  659  static void
 633  660  mparse_end(struct mparse *curp)
 634  661  {
 635  662          if (curp->man->macroset == MACROSET_NONE)
 636  663                  curp->man->macroset = MACROSET_MAN;
 637  664          if (curp->man->macroset == MACROSET_MDOC)
 638  665                  mdoc_endparse(curp->man);
 639  666          else
↓ open down ↓ 262 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX