I submit a fix of the previous patch.

By the way, however, I can't understand why you adhere to keep using
egrep and/or fgrep instead of `grep -E' or `grep -F' on minor platforms
as MinGW.

Even if egrep and/or fgrep are shell scripts, they aren't troubled for
me.  Even if troubled,  I will replace them to `grep -E' and/or `grep -F'.
From aa749655f9e73592d97f74b7d2a702c3238982ad Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <nori...@kcn.ne.jp>
Date: Wed, 14 May 2014 16:37:52 +0900
Subject: [PATCH] grep: revert egrep and fgrep to executables from shell
 scripts and simplify them

Revert egrep and fgrep to executables due to not suppoted POSIX shells
and simplify them.

* src/Makefile.am (bin_PROGRAMS): Add egrep, fgrep.
(bin_SCRIPTS): Remove it.
(grep_SOURCES): Move searchutils.c, dfa.c, dfasearch.c, kwset.c,
kwsearch.c, pcresearch.c here to libgrep_a_SOURCES.
(egrep_SOURCES, fgrep_SOURCES): New macro.
(noinst_LIBRARIES): Add libgrep.a.
(libgrep_a_SOURCES): Move searchutils.c, dfa.c, dfasearch.c, kwset.c,
kwsearch.c, pcresearch.c here from grep_SOURCES.
(LDADD): Add libgrep.a.
(egrep, fgrep): Remove rules.
(CLEANFILES): Remove macro.
* src/grep.c (SELECTED_MATCHER, DEFAULT_MATCHER): New macro.
(do_execute, usage, matchers): Use them.
*  src/egrep.c: Define SELECTED_MATCHER macro for egrep.
*  src/fgrep.c: Define SELECTED_MATCHER macro for fgrep.
---
 src/Makefile.am | 35 ++++++++++-------------------------
 src/egrep.c     | 22 ++++++++++++++++++++++
 src/fgrep.c     | 22 ++++++++++++++++++++++
 src/grep.c      | 29 +++++++++++++++++++++++++----
 src/search.h    |  2 ++
 5 files changed, 81 insertions(+), 29 deletions(-)
 create mode 100644 src/egrep.c
 create mode 100644 src/fgrep.c

diff --git a/src/Makefile.am b/src/Makefile.am
index e2c82a4..80754be 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -21,20 +21,23 @@ AM_CFLAGS = $(WARN_CFLAGS) $(WERROR_CFLAGS)
 # Tell the linker to omit references to unused shared libraries.
 AM_LDFLAGS = $(IGNORE_UNUSED_LIBRARIES_CFLAGS)
 
-bin_PROGRAMS = grep
-bin_SCRIPTS = egrep fgrep
-grep_SOURCES = grep.c searchutils.c \
-          dfa.c dfasearch.c \
-          kwset.c kwsearch.c \
-          pcresearch.c
+bin_PROGRAMS = grep egrep fgrep
+grep_SOURCES = grep.c
+egrep_SOURCES = egrep.c
+fgrep_SOURCES = fgrep.c
 noinst_HEADERS = grep.h dfa.h kwset.h search.h system.h
 
+noinst_LIBRARIES = libgrep.a
+libgrep_a_SOURCES = kwset.c dfa.c searchutils.c dfasearch.c kwsearch.c \
+  pcresearch.c
+
 # Sometimes, the expansion of $(LIBINTL) includes -lc which may
 # include modules defining variables like 'optind', so libgreputils.a
 # must precede $(LIBINTL) in order to ensure we use GNU getopt.
 # But libgreputils.a must also follow $(LIBINTL), since libintl uses
 # replacement functions defined in libgreputils.a.
 LDADD = \
+  libgrep.a \
   ../lib/libgreputils.a $(LIBINTL) ../lib/libgreputils.a $(LIBICONV) \
   $(LIBTHREAD)
 
@@ -42,22 +45,4 @@ grep_LDADD = $(LDADD) $(LIB_PCRE)
 localedir = $(datadir)/locale
 AM_CPPFLAGS = -I$(top_builddir)/lib -I$(top_srcdir)/lib
 
-EXTRA_DIST = dosbuf.c egrep.sh
-
-egrep fgrep: egrep.sh Makefile
-       $(AM_V_GEN)grep=`echo grep | sed -e '$(transform)'`     && \
-       case $@ in egrep) option=-E;; fgrep) option=-F;; esac   && \
-       shell_does_substrings='set x/y && d=$${1%/*} && test "$$d" = x' && \
-       if $(SHELL) -c "$$shell_does_substrings" 2>/dev/null; then \
-         edit_substring='s,X,X,'; \
-       else \
-         edit_substring='s,\$${0%/\*},`expr "X$$0" : '\''X\\(.*\\)/'\''`,g'; \
-       fi && \
-       sed -e 's|[@]SHELL@|$(SHELL)|g' \
-           -e "$$edit_substring" \
-           -e "s|[@]grep@|$$grep|g" \
-           -e "s|[@]option@|$$option|g" <$(srcdir)/egrep.sh >$@-t
-       $(AM_V_at)chmod +x $@-t
-       $(AM_V_at)mv $@-t $@
-
-CLEANFILES = egrep fgrep *-t
+EXTRA_DIST = dosbuf.c
diff --git a/src/egrep.c b/src/egrep.c
new file mode 100644
index 0000000..c5e7438
--- /dev/null
+++ b/src/egrep.c
@@ -0,0 +1,22 @@
+/* egrep.c - wrapper file for egrep.
+   Copyright (C) 1992, 1997-2002, 2004-2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
+   02110-1301, USA.  */
+
+#undef SELECTED_MATCHER
+#define SELECTED_MATCHER "egrep"
+
+#include "grep.c"
diff --git a/src/fgrep.c b/src/fgrep.c
new file mode 100644
index 0000000..d8728ff
--- /dev/null
+++ b/src/fgrep.c
@@ -0,0 +1,22 @@
+/* fgrep.c - wrapper file for fgrep.
+   Copyright (C) 1992, 1997-2002, 2004-2014 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
+   02110-1301, USA.  */
+
+#undef SELECTED_MATCHER
+#define SELECTED_MATCHER "fgrep"
+
+#include "grep.c"
diff --git a/src/grep.c b/src/grep.c
index ec955d8..c204956 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -53,6 +53,15 @@
 #define SEP_CHAR_REJECTED '-'
 #define SEP_STR_GROUP    "--"
 
+#ifdef SELECTED_MATCHER
+# undef DEFAULT_MATCHER
+# define DEFAULT_MATCHER SELECTED_MATCHER
+#else
+# ifndef DEFAULT_MATCHER
+#  define DEFAULT_MATCHER "grep"
+# endif
+#endif
+
 #define AUTHORS \
   proper_name ("Mike Haertel"), \
   _("others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>")
@@ -1070,8 +1079,11 @@ do_execute (char const *buf, size_t size, size_t 
*match_size,
      to struct matcher to split the buffer passed to execute.  It would
      perform the memchr if line-by-line matching is necessary, or just
      return buf + size otherwise.  */
-  if (! (execute == Fexecute || execute == Pexecute)
-      || MB_CUR_MAX == 1 || !match_icase)
+  if (! (execute == Fexecute
+#if !defined(SELECTED_MATCHER)
+      || execute == Pexecute
+#endif
+      ) || MB_CUR_MAX == 1 || !match_icase)
     return execute (buf, size, match_size, start_ptr);
 
   for (line_next = buf; line_next < buf + size; )
@@ -1538,12 +1550,14 @@ usage (int status)
 Example: %s -i 'hello world' menu.h main.c\n\
 \n\
 Regexp selection and interpretation:\n"), program_name);
+#ifndef SELECTED_MATCHER
       printf (_("\
   -E, --extended-regexp     PATTERN is an extended regular expression (ERE)\n\
   -F, --fixed-strings       PATTERN is a set of newline-separated fixed 
strings\n\
   -G, --basic-regexp        PATTERN is a basic regular expression (BRE)\n\
   -P, --perl-regexp         PATTERN is a Perl regular expression\n"));
   /* -X is undocumented on purpose. */
+#endif
       printf (_("\
   -e, --regexp=PATTERN      use PATTERN for matching\n\
   -f, --file=FILE           obtain PATTERN from FILE\n\
@@ -1645,6 +1659,7 @@ Ecompile (char const *pattern, size_t size)
   GEAcompile (pattern, size, RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
 }
 
+#ifndef SELECTED_MATCHER
 static void
 Acompile (char const *pattern, size_t size)
 {
@@ -1662,6 +1677,7 @@ PAcompile (char const *pattern, size_t size)
 {
   GEAcompile (pattern, size, RE_SYNTAX_POSIX_AWK);
 }
+#endif
 
 struct matcher
 {
@@ -1673,10 +1689,12 @@ static struct matcher const matchers[] = {
   { "grep",      Gcompile, EGexecute },
   { "egrep",     Ecompile, EGexecute },
   { "fgrep",     Fcompile,  Fexecute },
+#ifndef SELECTED_MATCHER
   { "awk",       Acompile, EGexecute },
   { "gawk",     GAcompile, EGexecute },
   { "posixawk", PAcompile, EGexecute },
   { "perl",      Pcompile,  Pexecute },
+#endif
   { "", NULL, NULL },
 };
 
@@ -1980,8 +1998,6 @@ main (int argc, char **argv)
 
   last_recursive = 0;
   prepended = prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv);
-  compile = matchers[0].compile;
-  execute = matchers[0].execute;
 
   while (prev_optind = optind,
          (opt = get_nondigit_option (argc, argv, &default_context)) != -1)
@@ -2010,6 +2026,7 @@ main (int argc, char **argv)
           error (EXIT_TROUBLE, 0, _("unknown devices method"));
         break;
 
+#ifndef SELECTED_MATCHER
       case 'E':
         setmatcher ("egrep");
         break;
@@ -2029,6 +2046,7 @@ main (int argc, char **argv)
       case 'X': /* undocumented on purpose */
         setmatcher (optarg);
         break;
+#endif
 
       case 'H':
         with_filenames = 1;
@@ -2259,6 +2277,9 @@ main (int argc, char **argv)
 
       }
 
+  if (!matcher)
+    setmatcher (DEFAULT_MATCHER);
+
   if (color_option == 2)
     color_option = isatty (STDOUT_FILENO) && should_colorize ();
   init_colorize ();
diff --git a/src/search.h b/src/search.h
index 14877bc..effc977 100644
--- a/src/search.h
+++ b/src/search.h
@@ -57,8 +57,10 @@ extern size_t EGexecute (char const *, size_t, size_t *, 
char const *);
 extern void Fcompile (char const *, size_t);
 extern size_t Fexecute (char const *, size_t, size_t *, char const *);
 
+#ifndef SELECTED_MATCHER
 /* pcresearch.c */
 extern void Pcompile (char const *, size_t);
 extern size_t Pexecute (char const *, size_t, size_t *, char const *);
+#endif
 
 #endif /* GREP_SEARCH_H */
-- 
1.9.3

Reply via email to