Permalink
Browse files

Import of our changes to grep developed in othersrc. A very

brief summary:

* Add NetBSD RCS Ids. Change to use a date based version number.
* Remove unused variables and functions.
* Move towards NetBSD code style.
* Add missing GNU options (except for --include, --exclude and
	--line-buffered)
* Bug fixes
* Bug fixes and changes from OpenBSD's src/usr.bin/grep

A full list of changes can be viewed in the NetBSD CVS repository at
othersrc/usr.bin/grep. A ChangeLog is also available at:
	ftp://ftp.NetBSD.org/pub/NetBSD/misc/cjep/grep-ChangeLog.txt

If you want to help out, please let me (cjep@) know so that we can
organise our efforts efficiently.
  • Loading branch information...
cjep
cjep committed Jan 2, 2004
1 parent a8e134c commit 271df26e3fb6b23a7123a20498e43acde994c9fc
Showing with 726 additions and 333 deletions.
  1. +15 −9 usr.bin/grep/Makefile
  2. +24 −16 usr.bin/grep/binary.c
  3. +65 −43 usr.bin/grep/file.c
  4. +42 −22 usr.bin/grep/grep.1
  5. +313 −114 usr.bin/grep/grep.c
  6. +76 −43 usr.bin/grep/grep.h
  7. +13 −13 usr.bin/grep/mmfile.c
  8. +15 −15 usr.bin/grep/queue.c
  9. +163 −58 usr.bin/grep/util.c
View
@@ -1,16 +1,22 @@
-# $Id: Makefile,v 1.1.1.1 2004/01/02 14:58:45 cjep Exp $
+# $NetBSD: Makefile,v 1.1.1.2 2004/01/02 15:00:25 cjep Exp $
PROG= grep
SRCS= binary.c file.c grep.c mmfile.c queue.c util.c
-LINKS= ${BINDIR}/grep ${BINDIR}/egrep \
- ${BINDIR}/grep ${BINDIR}/fgrep \
- ${BINDIR}/grep ${BINDIR}/zgrep
-MLINKS= grep.1 egrep.1 \
- grep.1 fgrep.1 \
- grep.1 zgrep.1
-CFLAGS+= -I/usr/local/include -Wall -pedantic
+LINKS= ${BINDIR}/grep ${BINDIR}/egrep \
+ ${BINDIR}/grep ${BINDIR}/fgrep \
+ ${BINDIR}/grep ${BINDIR}/zgrep \
+ ${BINDIR}/grep ${BINDIR}/zegrep \
+ ${BINDIR}/grep ${BINDIR}/zfgrep
-LDADD= -lz -L/usr/local/lib/ -liberty
+MLINKS= grep.1 egrep.1 \
+ grep.1 fgrep.1 \
+ grep.1 zgrep.1 \
+ grep.1 zegrep.1 \
+ grep.1 zfgrep.1
+
+LDADD= -lz
+
+WARNS=2
.include <bsd.prog.mk>
View
@@ -1,3 +1,5 @@
+/* $NetBSD: binary.c,v 1.1.1.2 2004/01/02 15:00:27 cjep Exp $ */
+
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
* All rights reserved.
@@ -23,51 +25,57 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $Id: binary.c,v 1.1.1.1 2004/01/02 14:58:45 cjep Exp $
*/
+#include <sys/cdefs.h>
+#ifndef lint
+__RCSID("$NetBSD: binary.c,v 1.1.1.2 2004/01/02 15:00:27 cjep Exp $");
+#endif /* not lint */
+
#include <ctype.h>
#include <stdio.h>
#include <zlib.h>
#include "grep.h"
-#define BUFFER_SIZE 32
+#define BUFFER_SIZE 128
int
bin_file(FILE *f)
{
- char buf[BUFFER_SIZE];
- int i, m;
+ char buf[BUFFER_SIZE];
+ int i, m;
- if (fseek(f, SEEK_SET, 0) == -1)
+ if (fseek(f, 0L, SEEK_SET) == -1)
return 0;
if ((m = (int)fread(buf, 1, BUFFER_SIZE, f)) == 0)
return 0;
for (i = 0; i < m; i++)
- if (!isprint(buf[i]) && !isspace(buf[i]))
- return 1;
-
+ if (!isprint(buf[i]) && !isspace(buf[i]) &&
+ buf[i] != line_endchar)
+ return 1;
+
rewind(f);
return 0;
}
int
gzbin_file(gzFile *f)
{
- char buf[BUFFER_SIZE];
- int i, m;
+ char buf[BUFFER_SIZE];
+ int i, m;
- if (gzseek(f, SEEK_SET, 0) == -1)
+ if (gzseek(f, 0L, SEEK_SET) == -1)
return 0;
- if ((m = (int)gzread(f, buf, BUFFER_SIZE)) == 0)
+ if ((m = gzread(f, buf, BUFFER_SIZE)) == 0)
return 0;
for (i = 0; i < m; i++)
- if (!isprint(buf[i]))
+ if (!isprint(buf[i]) && !isspace(buf[i]) &&
+ buf[i] != line_endchar)
return 1;
gzrewind(f);
@@ -78,10 +86,10 @@ int
mmbin_file(mmf_t *f)
{
int i;
-
/* XXX knows too much about mmf internals */
- for (i = 0; i < BUFFER_SIZE && i < f->len; i++)
- if (!isprint(f->base[i]))
+ for (i = 0; i < BUFFER_SIZE && i < f->len - 1; i++)
+ if (!isprint(f->base[i]) && !isspace(f->base[i]) &&
+ f->base[i] != line_endchar)
return 1;
mmrewind(f);
return 0;
View
@@ -1,3 +1,5 @@
+/* $NetBSD: file.c,v 1.1.1.2 2004/01/02 15:00:27 cjep Exp $ */
+
/*-
* Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
* All rights reserved.
@@ -23,9 +25,13 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $Id: file.c,v 1.1.1.1 2004/01/02 14:58:45 cjep Exp $
*/
+#include <sys/cdefs.h>
+#ifndef lint
+__RCSID("$NetBSD: file.c,v 1.1.1.2 2004/01/02 15:00:27 cjep Exp $");
+#endif /* not lint */
+
#include <sys/param.h>
#include <err.h>
@@ -35,26 +41,55 @@
#include "grep.h"
-static char fname[MAXPATHLEN];
-static char *lnbuf;
-static int lnbuflen;
+static char fname[MAXPATHLEN];
+static char *lnbuf;
+static int lnbuflen;
#define FILE_STDIO 0
#define FILE_MMAP 1
#define FILE_GZIP 2
struct file {
- int type;
- FILE *f;
- mmf_t *mmf;
- gzFile *gzf;
+ int type;
+ FILE *f;
+ mmf_t *mmf;
+ gzFile *gzf;
};
+static char *
+grepfgetln(FILE *f, size_t *len)
+{
+ size_t n;
+ int c;
+
+ for (n = 0; ; ++n) {
+ c = getc(f);
+ if (c == EOF) {
+ if (feof(f))
+ break;
+ err(2, "%s", fname);
+ /* ERROR */
+
+ }
+ if (c == line_endchar)
+ break;
+ if (n >= lnbuflen) {
+ lnbuflen *= 2;
+ lnbuf = grep_realloc(lnbuf, ++lnbuflen);
+ }
+ lnbuf[n] = c;
+ }
+ if (feof(f) && n == 0)
+ return NULL;
+ *len = n;
+ return lnbuf;
+}
+
static char *
gzfgetln(gzFile *f, size_t *len)
{
- size_t n;
- int c;
+ size_t n;
+ int c;
for (n = 0; ; ++n) {
c = gzgetc(f);
@@ -64,14 +99,14 @@ gzfgetln(gzFile *f, size_t *len)
if (gzeof(f))
break;
-
+
gzerrstr = gzerror(f, &gzerr);
if (gzerr == Z_ERRNO)
- err(1, "%s", fname);
+ err(2, "%s", fname);
else
- errx(1, "%s: %s", fname, gzerrstr);
+ errx(2, "%s: %s", fname, gzerrstr);
}
- if (c == '\n')
+ if (c == line_endchar)
break;
if (n >= lnbuflen) {
lnbuflen *= 2;
@@ -95,10 +130,10 @@ grep_fdopen(int fd, char *mode)
sprintf(fname, "(standard input)");
else
sprintf(fname, "(fd %d)", fd);
-
+
f = grep_malloc(sizeof *f);
-
- if (Zflag) {
+
+ if (zgrep) {
f->type = FILE_GZIP;
if ((f->gzf = gzdopen(fd, mode)) != NULL)
return f;
@@ -107,7 +142,7 @@ grep_fdopen(int fd, char *mode)
if ((f->f = fdopen(fd, mode)) != NULL)
return f;
}
-
+
free(f);
return NULL;
}
@@ -118,10 +153,10 @@ grep_open(char *path, char *mode)
file_t *f;
snprintf(fname, MAXPATHLEN, "%s", path);
-
+
f = grep_malloc(sizeof *f);
-
- if (Zflag) {
+
+ if (zgrep) {
f->type = FILE_GZIP;
if ((f->gzf = gzopen(fname, mode)) != NULL)
return f;
@@ -135,7 +170,7 @@ grep_open(char *path, char *mode)
if ((f->f = fopen(path, mode)) != NULL)
return f;
}
-
+
free(f);
return NULL;
}
@@ -147,28 +182,12 @@ grep_bin_file(file_t *f)
case FILE_STDIO:
return bin_file(f->f);
case FILE_MMAP:
- return mmbin_file(f->mmf);
+ return mmbin_file(f->mmf);
case FILE_GZIP:
return gzbin_file(f->gzf);
default:
/* can't happen */
- errx(1, "invalid file type");
- }
-}
-
-long
-grep_tell(file_t *f)
-{
- switch (f->type) {
- case FILE_STDIO:
- return ftell(f->f);
- case FILE_MMAP:
- return mmtell(f->mmf);
- case FILE_GZIP:
- return gztell(f->gzf);
- default:
- /* can't happen */
- errx(1, "invalid file type");
+ errx(2, "invalid file type");
}
}
@@ -177,14 +196,17 @@ grep_fgetln(file_t *f, size_t *l)
{
switch (f->type) {
case FILE_STDIO:
- return fgetln(f->f, l);
+ if (line_endchar == '\n')
+ return fgetln(f->f, l);
+ else
+ return grepfgetln(f->f, l);
case FILE_MMAP:
return mmfgetln(f->mmf, l);
case FILE_GZIP:
return gzfgetln(f->gzf, l);
default:
/* can't happen */
- errx(1, "invalid file type");
+ errx(2, "invalid file type");
}
}
@@ -203,6 +225,6 @@ grep_close(file_t *f)
break;
default:
/* can't happen */
- errx(1, "invalid file type");
+ errx(2, "invalid file type");
}
}
Oops, something went wrong.

0 comments on commit 271df26

Please sign in to comment.