From 15814787c0be99af5627315e7699058b7533c75d Mon Sep 17 00:00:00 2001 From: Norihiro Tanaka Date: Wed, 14 May 2014 08:03:21 +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): New macro. (do_execute, usage, matchers): Use it. * 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 | 20 ++++++++++++++++---- src/search.h | 2 ++ 5 files changed, 72 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|address@hidden@|$(SHELL)|g' \ - -e "$$edit_substring" \ - -e "s|address@hidden@|$$grep|g" \ - -e "s|address@hidden@|$$option|g" <$(srcdir)/egrep.sh >address@hidden - $(AM_V_at)chmod +x address@hidden - $(AM_V_at)mv address@hidden $@ - -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..d39a32c 100644 --- a/src/grep.c +++ b/src/grep.c @@ -1070,8 +1070,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 +#ifndef 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 +1541,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\ @@ -1673,10 +1678,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 +1987,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) @@ -2259,6 +2264,13 @@ main (int argc, char **argv) } + if (!matcher) +#ifdef SELECTED_MATCHER + setmatcher (SELECTED_MATCHER); +#else + setmatcher ("grep"); +#endif + 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