Author: kevans
Date: Sat Apr 21 01:42:02 2018
New Revision: 332852
URL: https://svnweb.freebsd.org/changeset/base/332852

Log:
  bsdgrep: if chain => switch
  
  This makes some of this a little easier to follow (in my opinion).

Modified:
  head/usr.bin/grep/file.c

Modified: head/usr.bin/grep/file.c
==============================================================================
--- head/usr.bin/grep/file.c    Sat Apr 21 01:33:13 2018        (r332851)
+++ head/usr.bin/grep/file.c    Sat Apr 21 01:42:02 2018        (r332852)
@@ -86,6 +86,9 @@ static inline int
 grep_refill(struct file *f)
 {
        ssize_t nr;
+#ifndef WITHOUT_LZMA
+       lzma_ret lzmaret;
+#endif
 
        if (filebehave == FILE_MMAP)
                return (0);
@@ -93,41 +96,52 @@ grep_refill(struct file *f)
        bufpos = buffer;
        bufrem = 0;
 
-       if (filebehave == FILE_GZIP) {
+       switch (filebehave) {
+       case FILE_GZIP:
                nr = gzread(gzbufdesc, buffer, MAXBUFSIZ);
+               break;
 #ifndef WITHOUT_BZIP2
-       } else if (filebehave == FILE_BZIP && bzbufdesc != NULL) {
-               int bzerr;
+       case FILE_BZIP:
+               if (bzbufdesc != NULL) {
+                       int bzerr;
 
-               nr = BZ2_bzRead(&bzerr, bzbufdesc, buffer, MAXBUFSIZ);
-               switch (bzerr) {
-               case BZ_OK:
-               case BZ_STREAM_END:
-                       /* No problem, nr will be okay */
-                       break;
-               case BZ_DATA_ERROR_MAGIC:
+                       nr = BZ2_bzRead(&bzerr, bzbufdesc, buffer, MAXBUFSIZ);
+                       switch (bzerr) {
+                       case BZ_OK:
+                       case BZ_STREAM_END:
+                               /* No problem, nr will be okay */
+                               break;
+                       case BZ_DATA_ERROR_MAGIC:
+                               /*
+                                * As opposed to gzread(), which simply returns 
the
+                                * plain file data, if it is not in the correct
+                                * compressed format, BZ2_bzRead() instead 
aborts.
+                                *
+                                * So, just restart at the beginning of the 
file again,
+                                * and use plain reads from now on.
+                                */
+                               BZ2_bzReadClose(&bzerr, bzbufdesc);
+                               bzbufdesc = NULL;
+                               if (lseek(f->fd, 0, SEEK_SET) == -1)
+                                       return (-1);
+                               nr = read(f->fd, buffer, MAXBUFSIZ);
+                               break;
+                       default:
+                               /* Make sure we exit with an error */
+                               nr = -1;
+                       }
+               } else
                        /*
-                        * As opposed to gzread(), which simply returns the
-                        * plain file data, if it is not in the correct
-                        * compressed format, BZ2_bzRead() instead aborts.
-                        *
-                        * So, just restart at the beginning of the file again,
-                        * and use plain reads from now on.
+                        * Also an error case; we should never have a scenario
+                        * where we have an open file but no bzip descriptor
+                        * at this point. See: grep_open
                         */
-                       BZ2_bzReadClose(&bzerr, bzbufdesc);
-                       bzbufdesc = NULL;
-                       if (lseek(f->fd, 0, SEEK_SET) == -1)
-                               return (-1);
-                       nr = read(f->fd, buffer, MAXBUFSIZ);
-                       break;
-               default:
-                       /* Make sure we exit with an error */
                        nr = -1;
-               }
+               break;
 #endif
 #ifndef WITHOUT_LZMA
-       } else if ((filebehave == FILE_XZ) || (filebehave == FILE_LZMA)) {
-               lzma_ret ret;
+       case FILE_XZ:
+       case FILE_LZMA:
                lstrm.next_out = buffer;
 
                do {
@@ -143,23 +157,23 @@ grep_refill(struct file *f)
                                lstrm.avail_in = nr;
                        }
 
-                       ret = lzma_code(&lstrm, laction);
+                       lzmaret = lzma_code(&lstrm, laction);
 
-                       if (ret != LZMA_OK && ret != LZMA_STREAM_END)
+                       if (lzmaret != LZMA_OK && lzmaret != LZMA_STREAM_END)
                                return (-1);
 
-                       if (lstrm.avail_out == 0 || ret == LZMA_STREAM_END) {
+                       if (lstrm.avail_out == 0 || lzmaret == LZMA_STREAM_END) 
{
                                bufrem = MAXBUFSIZ - lstrm.avail_out;
                                lstrm.next_out = buffer;
                                lstrm.avail_out = MAXBUFSIZ;
                        }
-               } while (bufrem == 0 && ret != LZMA_STREAM_END);
+               } while (bufrem == 0 && lzmaret != LZMA_STREAM_END);
 
                return (0);
-#endif /* WIHTOUT_LZMA */
-       } else
+#endif /* WITHOUT_LZMA */
+       default:
                nr = read(f->fd, buffer, MAXBUFSIZ);
-
+       }
        if (nr < 0)
                return (-1);
 
@@ -255,6 +269,9 @@ struct file *
 grep_open(const char *path)
 {
        struct file *f;
+#ifndef WITHOUT_LZMA
+       lzma_ret lzmaret;
+#endif
 
        f = grep_malloc(sizeof *f);
        memset(f, 0, sizeof *f);
@@ -292,30 +309,34 @@ grep_open(const char *path)
        if ((buffer == NULL) || (buffer == MAP_FAILED))
                buffer = grep_malloc(MAXBUFSIZ);
 
-       if (filebehave == FILE_GZIP &&
-           (gzbufdesc = gzdopen(f->fd, "r")) == NULL)
-               goto error2;
-
+       switch (filebehave) {
+       case FILE_GZIP:
+               if ((gzbufdesc = gzdopen(f->fd, "r")) == NULL)
+                       goto error2;
+               break;
 #ifndef WITHOUT_BZIP2
-       if (filebehave == FILE_BZIP &&
-           (bzbufdesc = BZ2_bzdopen(f->fd, "r")) == NULL)
-               goto error2;
+       case FILE_BZIP:
+               if ((bzbufdesc = BZ2_bzdopen(f->fd, "r")) == NULL)
+                       goto error2;
+               break;
 #endif
 #ifndef WITHOUT_LZMA
-       else if ((filebehave == FILE_XZ) || (filebehave == FILE_LZMA)) {
-               lzma_ret ret;
+       case FILE_XZ:
+       case FILE_LZMA:
 
-               ret = (filebehave == FILE_XZ) ?
-                       lzma_stream_decoder(&lstrm, UINT64_MAX,
-                                       LZMA_CONCATENATED) :
-                       lzma_alone_decoder(&lstrm, UINT64_MAX);
+               if (filebehave == FILE_XZ)
+                       lzmaret = lzma_stream_decoder(&lstrm, UINT64_MAX,
+                           LZMA_CONCATENATED);
+               else
+                       lzmaret = lzma_alone_decoder(&lstrm, UINT64_MAX);
 
-               if (ret != LZMA_OK)
+               if (lzmaret != LZMA_OK)
                        goto error2;
 
                lstrm.avail_in = 0;
                lstrm.avail_out = MAXBUFSIZ;
                laction = LZMA_RUN;
+               break;
        }
 #endif
 
@@ -326,7 +347,7 @@ grep_open(const char *path)
        /* Check for binary stuff, if necessary */
        if (binbehave != BINFILE_TEXT && fileeol != '\0' &&
            memchr(bufpos, '\0', bufrem) != NULL)
-       f->binary = true;
+               f->binary = true;
 
        return (f);
 
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to