dfa: port to freestanding DJGPP (Bug#17056)

* src/dfa.c (setlocale) [!LC_ALL]: Define a dummy.
egrep, fgrep: go back to shell scripts

Although egrep's and fgrep's switch from shell scripts to
executables may have made sense in 2005, it complicated
maintenance and recently has caused subtle performance bugs.
Go back to the old way of doing things, as it's simpler and more
easily separated from the mainstream implementation.  This should
be good enough nowadays, as POSIX has withdrawn egrep/fgrep and
portable applications should be using -E/-F anyway.
* po/POTFILES.in: Remove src/egrep.c, src/fgrep.c, src/main.c.
* src/Makefile.am (bin_PROGRAMS): Remove egrep, fgrep.
(bin_SCRIPTS): New macro.
(grep_SOURCES): Move searchutils.c, dfa.c, dfasearch.c, kwset.c,
kwsearch.c, pcresearch.c here from libgrep_a_SOURCES.
(egrep_SOURCES, fgrep_SOURCES, noinst_LIBRARIES, libgrep_a_SOURCES):
Remove.
(LDADD): Remove libgrep.a.
(egrep, fgrep): New rules.
(CLEANFILES): New macro.
* src/grep.c: Rename from src/main.c.
(usage, setmatcher, main):
Simplify, since there's now just one executable.
(Gcompile, Ecompile, Acompile, GAcompile, PAcompile, matchers):
Move here from the (removed) src/grep.c.
(compile_fp_t, execute_fp_t, struct matcher, matchers):
Move here from src/grep.h, as they no longer need to be public.
(struct matcher.name): Avoid one level of indirection/relocation.
(do_execute, main): Fix a performance bug when it was compiled
as 'fgrep', due to confusion about which matcher was which.
(main): Fix a performance bug with -P, likewise.
* src/grep.h (before_options, after_options): Remove.
* src/egrep.c, src/fgrep.c, src/grep.c: Remove.
grep: optimization for fgrep with changing the macher to grep macher.

fgrep macher is only use kwset engine.  However, it's very slow for
case-insensitive matching in multibyte locales.

And so, if the matcher is fgrep and case-insensitive and keys including
any alphabets, change it into grep matcher by escape of keys.  OTOH, if
keys include no alphabet, turn match_icase flag off.

I prepare following string to measure the performance.

    yes $(printf '%078dm' 0)| head -1000000 | tr 0 a > in
    A=`printf '\xef\xbc\xa1'`   # FULLWIDTH LATIN CAPITAL LETTER A

I run three tests with this patch (best-of-5 trials):

    env LC_ALL=en_US.UTF-8 time -p src/fgrep -i "$A" in
        real 8.54       user 7.13       sys 1.16

Back out that commit (temporarily), recompile, and rerun the experiment:

    env LC_ALL=en_US.UTF-8 time -p src/fgrep -i "$A" in
        real 0.07       user 0.02       sys 0.05

* src/fgrep.c (Gcompile) New function.
* src/main.c (check_any_alphabets) New function.
(fgrep_to_grep_pattern) New function.
(main) Use them.
grep: fix and simplify grep -iF optimization

* src/grep.c (check_any_alphabets): Remove.
(fgrep_to_grep_pattern): Fix problems when mbrtowc returns -1 or -2.
Simplify a bit.
(main): Don't bother optimizing 'grep -iF PAT' when PAT contains no
alphabetics; it's so rare it's not worth the complexity.
dfa: avoid undefined behavior

* src/dfa.c (FETCH_WC, addtok_wc): Don't rely on undefined behavior
when converting an out-of-range value to 'int'.
(FETCH_WC, prepare_wc_buf): Don't rely on conversion state after
mbrtowc returns a special value, as it's undefined for (size_t) -1.
(prepare_wc_buf): Simplify test for valid character.
egrep, fgrep: improve diagnostics from shell scripts

This should fix Bug#17098.
* src/Makefile.am (EXTRA_DIST): Add egrep.sh.
(egrep fgrep): Depend on egrep.sh and Makefile.
Build from new file egrep.sh, as this makes the build process
easier to follow.  Arrange for $0 to look nicer in subgrep.
* src/egrep.sh: New file.
dfa: improve port to freestanding DJGPP

* src/dfa.c (setlocale) [!LC_ALL]: Return "C", not NULL (Bug#17056).
(using_simple_locale): Store setlocale result in a ptr-to-const.
maint: fix "make dist"

* src/Makefile.am (egrep fgrep): Specify egrep.sh via
$(srcdir)/egrep.sh, so non-srcdir builds work once again.
grep: perform the kwset-helping DFA match in narrower range

When kwsexec gives us the offset of a potential match, we compute
line begin/end and then run the DFA matcher to see if there really
is a match on that line.  When the beginning of the line, BEG, is
not on a multibyte character boundary, advance BEG until it on such
a boundary, before running the DFA search.
* src/dfasearch.c (EGexecute): As above.  Add a comment.
* tests/euc-mb: Add a test case that exercises this code.
This addresses http://debbugs.gnu.org/17095.
dfa: cache results of mbrtowc for speed

Idea suggested by Norihiro Tanaka in Bug#16842.
* src/dfa.c (mbrtowc_cache): New static var.
(build_mbrtowc_cache, mbs_to_wchar): New functions.
(FETCH_WC) [MBS_SUPPORT]: Speed up by using mbs_to_wchar
instead of mbrtowc and wctob.
(FETCH_WC) [!MBS_SUPPORT]: Rewrite in terms of old FETCH macro.
(FETCH): Remove; no longer used.
(lex): Simplify by avoiding the need for FETCH.
(prepare_wc_buf) [MBS_SUPPORT]: Speed up by using mbs_to_wchar.
Simplify the loop.
(dfacomp): Initialize the cache.
grep: take mbrtowc_cache into new member of struct dfa

When struct dfa more than one are used at the same time, mbrtowc cache
may be conflict.  So, take mbrtowc_cache into new member of struct dfa,
and define each mbrtowc cache for them.

* src/dfa.c (struct dfa): New member `mbrtowc_cache'.
(dfambcache): Rename from build_mbrtowc_cache.  Add dependency on struct dfa.
(mbs_to_wchar): Add dependency on struct dfa.
(FETCH_WC): Use it.
(prepare_wc_buf): Use it.  Add dependency on struct dfa.
(dfacomp): Call it.
(dfafree): Release it.
dfa: avoid an indirection and port wint_t usage

* src/dfa.c (struct dfa): Put mbrtowc_cache directly into struct dfa
rather than having a pointer; this saves a malloc and an indirection.
All uses changed.
(dfambcache): Port to hosts where wint_t * can't be cast to wchar_t *.
tests: placate "make syntax-check" re compare arg ordering

* tests/euc-mb: Reverse order of arguments to compare.
Be consistent in ordering compare arguments: expected followed
by actual.
dfa: improve port to freestanding DJGPP

Suggested by Aharon Robbins (Bug#17056).
* src/dfa.c (setlocale) [!LC_ALL]: Return NULL, not "C",
reverting part of a recent change.
(using_simple_locale): Return true if setlocale returns null.