[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
mostly-cleanup/readability/maintainability patches
From: |
Jim Meyering |
Subject: |
mostly-cleanup/readability/maintainability patches |
Date: |
Sun, 04 Apr 2010 09:43:46 +0200 |
The following series is mostly no-semantic-change.
The few semantic changes are in separate commits:
- handle white space consistently in parsing command line arguments
- give a better diagnostic for --directories=INVALID
The rest is clean-up.
cleanup: rely on gnulib's ctype.h functions; remove IS* macros and is_*
cleanup and improvement: parse command line arguments consistently
maint: enable the useless_cpp_parens syntax check
maint: use STREQ in place of strcmp
maint: MBS_SUPPORT: define to 0/1, not undef/1
maint: include <wchar.h> and <wctype.h> unconditionally
maint: const-correctness
dfa.c: use a better (unsigned) type for an index: int->size_t
maint: style: use sizeof VAR, rather than sizeof TYPE, where possible
dfa.c: use a better (unsigned) type for an index: int->unsigned int
dfa.c: const correctness; and remove useless casts of realloc and malloc
maint: use argmatch, for better --directories=INVAL diagnostics
Some could doubtless be merged, but it's not worth it to me, now.
I expect to push this today or tomorrow.
>From 62dd3a746d4559e3c841c53793d9460374a1bd59 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Thu, 1 Apr 2010 11:05:00 +0200
Subject: [PATCH 01/12] cleanup: rely on gnulib's ctype.h functions; remove IS*
macros and is_*
* src/dfa.c (setbit_case_fold, prednames): Use official names.
(IS_WORD_CONSTITUENT, lex): Likewise.
(ISALNUM, ISALPHA, ISCNTRL, ISDIGIT, ISGRAPH): Remove definitions.
(ISLOWER, ISPRINT, ISPUNCT, ISSPACE, ISUPPER, ISXDIGIT): Likewise.
(is_alnum, is_alpha, is_blank, is_cntrl, is_digit, is_graph): Likewise.
(is_lower, is_print, is_punct, is_space, is_upper, is_xdigit): Likewise.
(isgraph): Likewise.
---
src/dfa.c | 88 ++++++++++++++-----------------------------------------------
1 files changed, 20 insertions(+), 68 deletions(-)
diff --git a/src/dfa.c b/src/dfa.c
index ba78b08..7822e95 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -31,42 +31,12 @@
#include <string.h>
#include <locale.h>
-#ifndef isgraph
-#define isgraph(C) (isprint(C) && !isspace(C))
-#endif
-
-#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
-#define ISALPHA(C) isalpha(C)
-#define ISUPPER(C) isupper(C)
-#define ISLOWER(C) islower(C)
-#define ISDIGIT(C) isdigit(C)
-#define ISXDIGIT(C) isxdigit(C)
-#define ISSPACE(C) isspace(C)
-#define ISPUNCT(C) ispunct(C)
-#define ISALNUM(C) isalnum(C)
-#define ISPRINT(C) isprint(C)
-#define ISGRAPH(C) isgraph(C)
-#define ISCNTRL(C) iscntrl(C)
-#else
-#define ISALPHA(C) (isascii(C) && isalpha(C))
-#define ISUPPER(C) (isascii(C) && isupper(C))
-#define ISLOWER(C) (isascii(C) && islower(C))
-#define ISDIGIT(C) (isascii(C) && isdigit(C))
-#define ISXDIGIT(C) (isascii(C) && isxdigit(C))
-#define ISSPACE(C) (isascii(C) && isspace(C))
-#define ISPUNCT(C) (isascii(C) && ispunct(C))
-#define ISALNUM(C) (isascii(C) && isalnum(C))
-#define ISPRINT(C) (isascii(C) && isprint(C))
-#define ISGRAPH(C) (isascii(C) && isgraph(C))
-#define ISCNTRL(C) (isascii(C) && iscntrl(C))
-#endif
-
-/* ISASCIIDIGIT differs from ISDIGIT, as follows:
+/* ISASCIIDIGIT differs from isdigit, as follows:
- Its arg may be any int or unsigned int; it need not be an unsigned char.
- It's guaranteed to evaluate its argument exactly once.
- It's typically faster.
Posix 1003.2-1992 section 2.5.2.1 page 50 lines 1556-1558 says that
- only '0' through '9' are digits. Prefer ISASCIIDIGIT to ISDIGIT unless
+ only '0' through '9' are digits. Prefer ISASCIIDIGIT to isdigit unless
it's important to use the locale's definition of `digit' even when the
host does not conform to Posix. */
#define ISASCIIDIGIT(c) ((unsigned) (c) - '0' <= 9)
@@ -266,8 +236,8 @@ setbit_case_fold (
else
#endif
{
- unsigned char b1 = ISUPPER(b) ? tolower(b) : b;
- unsigned char b2 = ISLOWER(b) ? toupper(b) : b;
+ unsigned char b1 = isupper(b) ? tolower(b) : b;
+ unsigned char b2 = islower(b) ? toupper(b) : b;
setbit (b1, c);
if (b2 != b1)
setbit (b2, c);
@@ -406,24 +376,6 @@ in_coll_range (char ch, char from, char to)
return strcoll (&c[0], &c[2]) <= 0 && strcoll (&c[2], &c[4]) <= 0;
}
-static int is_alpha(int c) { return ISALPHA(c); }
-static int is_upper(int c) { return ISUPPER(c); }
-static int is_lower(int c) { return ISLOWER(c); }
-static int is_digit(int c) { return ISDIGIT(c); }
-static int is_xdigit(int c) { return ISXDIGIT(c); }
-static int is_space(int c) { return ISSPACE(c); }
-static int is_punct(int c) { return ISPUNCT(c); }
-static int is_alnum(int c) { return ISALNUM(c); }
-static int is_print(int c) { return ISPRINT(c); }
-static int is_graph(int c) { return ISGRAPH(c); }
-static int is_cntrl(int c) { return ISCNTRL(c); }
-
-static int
-is_blank (int c)
-{
- return (c == ' ' || c == '\t');
-}
-
typedef int predicate (int);
/* The following list maps the names of the Posix named character classes
@@ -433,18 +385,18 @@ static struct {
const char *name;
predicate *pred;
} const prednames[] = {
- { "alpha", is_alpha },
- { "upper", is_upper },
- { "lower", is_lower },
- { "digit", is_digit },
- { "xdigit", is_xdigit },
- { "space", is_space },
- { "punct", is_punct },
- { "alnum", is_alnum },
- { "print", is_print },
- { "graph", is_graph },
- { "cntrl", is_cntrl },
- { "blank", is_blank },
+ { "alpha", isalpha },
+ { "upper", isupper },
+ { "lower", islower },
+ { "digit", isdigit },
+ { "xdigit", isxdigit },
+ { "space", isspace },
+ { "punct", ispunct },
+ { "alnum", isalnum },
+ { "print", isprint },
+ { "graph", isgraph },
+ { "cntrl", iscntrl },
+ { "blank", isblank },
{ NULL, NULL }
};
@@ -688,7 +640,7 @@ parse_bracket_exp (void)
setbit_case_fold (c, ccl);
else
for (c = 0; c < NOTCHAR; ++c)
- if (!(case_fold && ISUPPER (c))
+ if (!(case_fold && isupper (c))
&& in_coll_range (c, c1, c2))
setbit_case_fold (c, ccl);
}
@@ -767,7 +719,7 @@ parse_bracket_exp (void)
}
/* Return non-zero if C is a `word-constituent' byte; zero otherwise. */
-#define IS_WORD_CONSTITUENT(C) (ISALNUM(C) || (C) == '_')
+#define IS_WORD_CONSTITUENT(C) (isalnum(C) || (C) == '_')
static token
lex (void)
@@ -1044,7 +996,7 @@ lex (void)
goto normal_char;
zeroset(ccl);
for (c2 = 0; c2 < NOTCHAR; ++c2)
- if (ISSPACE(c2))
+ if (isspace(c2))
setbit(c2, ccl);
if (c == 'S')
notset(ccl);
@@ -1081,7 +1033,7 @@ lex (void)
return lasttok = WCHAR;
#endif
- if (case_fold && ISALPHA(c))
+ if (case_fold && isalpha(c))
{
zeroset(ccl);
setbit_case_fold (c, ccl);
--
1.7.0.4.529.g78fb
>From 5efb9a58e9b860918f1a2866ac976b8a89fa97a1 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Thu, 1 Apr 2010 11:11:11 +0200
Subject: [PATCH 02/12] cleanup and improvement: parse command line arguments
consistently
* src/main.c: Include c-ctype.h, for this:
(prepend_args): Use c_isspace, not ISSPACE.
This is important so that we parse arguments consistently,
and independently of the current locale.
* bootstrap.conf (gnulib_modules): Add c-ctype.
* src/system.h: Remove IS* definitions here, too.
* src/dfasearch.c (WCHAR): Use isalnum, not ISALNUM.
* src/kwsearch.c (WCHAR): Likewise.
* src/searchutils.c (kwsinit): Use tolower, not TOLOWER.
---
bootstrap.conf | 1 +
src/dfasearch.c | 2 +-
src/kwsearch.c | 2 +-
src/main.c | 5 +++--
src/searchutils.c | 2 +-
src/system.h | 24 ------------------------
6 files changed, 7 insertions(+), 29 deletions(-)
diff --git a/bootstrap.conf b/bootstrap.conf
index df7b95f..682e578 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -23,6 +23,7 @@ announce-gen
atexit
binary-io
btowc
+c-ctype
closeout
dirent
dirname
diff --git a/src/dfasearch.c b/src/dfasearch.c
index 5de40b6..cb9578a 100644
--- a/src/dfasearch.c
+++ b/src/dfasearch.c
@@ -23,7 +23,7 @@
#include "dfa.h"
/* For -w, we also consider _ to be word constituent. */
-#define WCHAR(C) (ISALNUM(C) || (C) == '_')
+#define WCHAR(C) (isalnum (C) || (C) == '_')
/* KWset compiled pattern. For Ecompile and Gcompile, we compile
a list of strings, at least one of which is known to occur in
diff --git a/src/kwsearch.c b/src/kwsearch.c
index 973eb60..0334665 100644
--- a/src/kwsearch.c
+++ b/src/kwsearch.c
@@ -22,7 +22,7 @@
#include "search.h"
/* For -w, we also consider _ to be word constituent. */
-#define WCHAR(C) (ISALNUM(C) || (C) == '_')
+#define WCHAR(C) (isalnum (C) || (C) == '_')
/* KWset compiled pattern. For Ecompile and Gcompile, we compile
a list of strings, at least one of which is known to occur in
diff --git a/src/main.c b/src/main.c
index b4a2e94..c9b209f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -34,6 +34,7 @@
#include <stdio.h>
#include "system.h"
+#include "c-ctype.h"
#include "closeout.h"
#include "error.h"
#include "exclude.h"
@@ -1552,7 +1553,7 @@ prepend_args (char const *options, char *buf, char **argv)
for (;;)
{
- while (ISSPACE ((unsigned char) *o))
+ while (c_isspace ((unsigned char) *o))
o++;
if (!*o)
return n;
@@ -1563,7 +1564,7 @@ prepend_args (char const *options, char *buf, char **argv)
do
if ((*b++ = *o++) == '\\' && *o)
b[-1] = *o++;
- while (*o && ! ISSPACE ((unsigned char) *o));
+ while (*o && ! c_isspace ((unsigned char) *o));
*b++ = '\0';
}
diff --git a/src/searchutils.c b/src/searchutils.c
index f28cf29..bb3a0db 100644
--- a/src/searchutils.c
+++ b/src/searchutils.c
@@ -34,7 +34,7 @@ kwsinit (kwset_t *kwset)
)
{
for (i = 0; i < NCHAR; ++i)
- trans[i] = TOLOWER (i);
+ trans[i] = tolower (i);
*kwset = kwsalloc (trans);
}
diff --git a/src/system.h b/src/system.h
index 3b617fb..ee85b22 100644
--- a/src/system.h
+++ b/src/system.h
@@ -42,30 +42,6 @@
enum { EXIT_TROUBLE = 2 };
-#ifndef isgraph
-# define isgraph(C) (isprint(C) && !isspace(C))
-#endif
-
-#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
-# define IN_CTYPE_DOMAIN(c) 1
-#else
-# define IN_CTYPE_DOMAIN(c) isascii(c)
-#endif
-
-#define ISALPHA(C) (IN_CTYPE_DOMAIN (C) && isalpha (C))
-#define ISUPPER(C) (IN_CTYPE_DOMAIN (C) && isupper (C))
-#define ISLOWER(C) (IN_CTYPE_DOMAIN (C) && islower (C))
-#define ISDIGIT(C) (IN_CTYPE_DOMAIN (C) && isdigit (C))
-#define ISXDIGIT(C) (IN_CTYPE_DOMAIN (C) && isxdigit (C))
-#define ISSPACE(C) (IN_CTYPE_DOMAIN (C) && isspace (C))
-#define ISPUNCT(C) (IN_CTYPE_DOMAIN (C) && ispunct (C))
-#define ISALNUM(C) (IN_CTYPE_DOMAIN (C) && isalnum (C))
-#define ISPRINT(C) (IN_CTYPE_DOMAIN (C) && isprint (C))
-#define ISGRAPH(C) (IN_CTYPE_DOMAIN (C) && isgraph (C))
-#define ISCNTRL(C) (IN_CTYPE_DOMAIN (C) && iscntrl (C))
-
-#define TOLOWER(C) (ISUPPER(C) ? tolower(C) : (C))
-
#include <gettext.h>
#define N_(String) gettext_noop(String)
#define _(String) gettext(String)
--
1.7.0.4.529.g78fb
>From 1a70575e0802e6e689269c1f416e9f5e70c1d068 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Thu, 1 Apr 2010 11:23:20 +0200
Subject: [PATCH 03/12] maint: enable the useless_cpp_parens syntax check
* cfg.mk (local-checks-to-skip): Remove sc_useless_cpp_parens.
* src/main.c (devices, fillbuf, exit_on_match): Remove useless parens.
(print_line_head, grepfile, set_limits, main): Likewise.
* src/vms_fab.h: Likewise.
* vms/config_vms.h: Likewise.
* src/mbsupport.h: Likewise.
---
cfg.mk | 3 +--
src/main.c | 24 ++++++++++++------------
src/mbsupport.h | 2 +-
3 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/cfg.mk b/cfg.mk
index 59605ec..e5017b2 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -23,8 +23,7 @@ gpg_key_ID = B9AB9A16
# Tests not to run as part of "make distcheck".
local-checks-to-skip = \
sc_prohibit_strcmp \
- sc_texinfo_acronym \
- sc_useless_cpp_parens
+ sc_texinfo_acronym
# Tools used to bootstrap this package, used for "announcement".
bootstrap-tools = autoconf,automake,gnulib
diff --git a/src/main.c b/src/main.c
index c9b209f..b0733c5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -21,7 +21,7 @@
#include <config.h>
#include <sys/types.h>
#include <sys/stat.h>
-#if defined(HAVE_SETRLIMIT)
+#if defined HAVE_SETRLIMIT
# include <sys/time.h>
# include <sys/resource.h>
#endif
@@ -367,7 +367,7 @@ static enum
} devices = READ_DEVICES;
static int grepdir (char const *, struct stats const *);
-#if defined(HAVE_DOS_FILE_CONTENTS)
+#if defined HAVE_DOS_FILE_CONTENTS
static inline int undossify_input (char *, size_t);
#endif
@@ -540,7 +540,7 @@ fillbuf (size_t save, struct stats const *stats)
}
bufoffset += fillsize;
-#if defined(HAVE_DOS_FILE_CONTENTS)
+#if defined HAVE_DOS_FILE_CONTENTS
if (fillsize)
fillsize = undossify_input (readbuf, fillsize);
#endif
@@ -587,7 +587,7 @@ static int pending; /* Pending lines of output.
static int done_on_match; /* Stop scanning file on first match. */
static int exit_on_match; /* Exit on first match. */
-#if defined(HAVE_DOS_FILE_CONTENTS)
+#if defined HAVE_DOS_FILE_CONTENTS
# include "dosbuf.c"
#endif
@@ -695,7 +695,7 @@ print_line_head (char const *beg, char const *lim, int sep)
if (out_byte)
{
uintmax_t pos = add_count (totalcc, beg - bufbeg);
-#if defined(HAVE_DOS_FILE_CONTENTS)
+#if defined HAVE_DOS_FILE_CONTENTS
pos = dossified_pos (pos);
#endif
if (pending_sep)
@@ -1216,7 +1216,7 @@ grepfile (char const *file, struct stats *stats)
if (directories == SKIP_DIRECTORIES)
switch (e)
{
-#if defined(EISDIR)
+#if defined EISDIR
case EISDIR:
return 1;
#endif
@@ -1236,7 +1236,7 @@ grepfile (char const *file, struct stats *stats)
filename = file;
}
-#if defined(SET_BINARY)
+#if defined SET_BINARY
/* Set input to binary mode. Pipes are simulated with files
on DOS, so this includes the case of "foo | grep bar". */
if (!isatty (desc))
@@ -1509,7 +1509,7 @@ setmatcher (char const *m)
static void
set_limits(void)
{
-#if defined(HAVE_SETRLIMIT) && defined(RLIMIT_STACK)
+#if defined HAVE_SETRLIMIT && defined RLIMIT_STACK
struct rlimit rlim;
/* I think every platform needs to do this, so that regex.c
@@ -1761,10 +1761,10 @@ main (int argc, char **argv)
only_matching = 0;
/* Internationalization. */
-#if defined(HAVE_SETLOCALE)
+#if defined HAVE_SETLOCALE
setlocale (LC_ALL, "");
#endif
-#if defined(ENABLE_NLS)
+#if defined ENABLE_NLS
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
#endif
@@ -1835,13 +1835,13 @@ main (int argc, char **argv)
break;
case 'U':
-#if defined(HAVE_DOS_FILE_CONTENTS)
+#if defined HAVE_DOS_FILE_CONTENTS
dos_use_file_type = DOS_BINARY;
#endif
break;
case 'u':
-#if defined(HAVE_DOS_FILE_CONTENTS)
+#if defined HAVE_DOS_FILE_CONTENTS
dos_report_unix_offset = 1;
#endif
break;
diff --git a/src/mbsupport.h b/src/mbsupport.h
index 40a85ab..57df54c 100644
--- a/src/mbsupport.h
+++ b/src/mbsupport.h
@@ -30,7 +30,7 @@
#include <stdlib.h>
#endif
-#if defined(HAVE_WCSCOLL) && defined(HAVE_ISWCTYPE)
+#if defined HAVE_WCSCOLL && defined HAVE_ISWCTYPE
# define MBS_SUPPORT 1
#else
# undef MBS_SUPPORT
--
1.7.0.4.529.g78fb
>From 4289ad8ce5c2ad714c7b51f85d6d47146b51cb13 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Thu, 1 Apr 2010 16:03:22 +0200
Subject: [PATCH 04/12] maint: use STREQ in place of strcmp
perl -pi -e 's/\bstrcmp *\((.*?)\) == 0/STREQ ($1)/' src/main.c
perl -pi -e 's/\bstrcmp *\((.*?)\) != 0/!STREQ ($1)/' src/main.c
* src/dfa.c (STREQ): Define.
Use it instead of strcmp.
* src/main.c (STREQ): Likewise.
* cfg.mk (local-checks-to-skip): Remove sc_prohibit_strcmp,
to enable the strcmp-prohibition.
---
cfg.mk | 1 -
src/dfa.c | 15 +++++++++------
src/main.c | 30 ++++++++++++++++--------------
3 files changed, 25 insertions(+), 21 deletions(-)
diff --git a/cfg.mk b/cfg.mk
index e5017b2..0adeb74 100644
--- a/cfg.mk
+++ b/cfg.mk
@@ -22,7 +22,6 @@ gpg_key_ID = B9AB9A16
# Tests not to run as part of "make distcheck".
local-checks-to-skip = \
- sc_prohibit_strcmp \
sc_texinfo_acronym
# Tools used to bootstrap this package, used for "announcement".
diff --git a/src/dfa.c b/src/dfa.c
index 7822e95..f2c7a31 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -31,6 +31,8 @@
#include <string.h>
#include <locale.h>
+#define STREQ(a, b) (strcmp (a, b) == 0)
+
/* ISASCIIDIGIT differs from isdigit, as follows:
- Its arg may be any int or unsigned int; it need not be an unsigned char.
- It's guaranteed to evaluate its argument exactly once.
@@ -83,6 +85,7 @@ static void regexp (int toplevel);
REALLOC(p, t, nalloc); \
}
+
#ifdef DEBUG
static void
@@ -262,7 +265,7 @@ using_utf8 (void)
if (utf8 == -1)
{
#if defined HAVE_LANGINFO_CODESET && defined MBS_SUPPORT
- utf8 = (strcmp (nl_langinfo (CODESET), "UTF-8") == 0);
+ utf8 = (STREQ (nl_langinfo (CODESET), "UTF-8"));
#else
utf8 = 0;
#endif
@@ -405,7 +408,7 @@ find_pred (const char *str)
{
unsigned int i;
for (i = 0; prednames[i].name; ++i)
- if (!strcmp(str, prednames[i].name))
+ if (STREQ (str, prednames[i].name))
break;
return prednames[i].pred;
@@ -501,8 +504,8 @@ parse_bracket_exp (void)
/* build character class. */
{
char const *class
- = (case_fold && (!strcmp (str, "upper")
- || !strcmp (str, "lower"))
+ = (case_fold && (STREQ (str, "upper")
+ || STREQ (str, "lower"))
? "alpha"
: str);
#ifdef MBS_SUPPORT
@@ -3412,7 +3415,7 @@ dfamust (struct dfa *d)
rmp = --mp;
lmp = --mp;
/* Guaranteed to be. Unlikely, but. . . */
- if (strcmp(lmp->is, rmp->is) != 0)
+ if (!STREQ (lmp->is, rmp->is))
lmp->is[0] = '\0';
/* Left side--easy */
i = 0;
@@ -3451,7 +3454,7 @@ dfamust (struct dfa *d)
for (i = 0; musts[0].in[i] != NULL; ++i)
if (strlen(musts[0].in[i]) > strlen(result))
result = musts[0].in[i];
- if (strcmp(result, musts[0].is) == 0)
+ if (STREQ (result, musts[0].is))
exact = 1;
goto done;
case CAT:
diff --git a/src/main.c b/src/main.c
index b0733c5..c5d6dea 100644
--- a/src/main.c
+++ b/src/main.c
@@ -52,6 +52,8 @@
#define SEP_CHAR_REJECTED '-'
#define SEP_STR_GROUP "--"
+#define STREQ(a, b) (strcmp (a, b) == 0)
+
struct stats
{
struct stats const *parent;
@@ -1481,7 +1483,7 @@ setmatcher (char const *m)
else if (matcher)
{
- if (matcher && strcmp (matcher, m) == 0)
+ if (matcher && STREQ (matcher, m))
;
else if (!matchers[1].name)
@@ -1494,7 +1496,7 @@ setmatcher (char const *m)
else
{
for (i = 0; matchers[i].name; i++)
- if (strcmp (m, matchers[i].name) == 0)
+ if (STREQ (m, matchers[i].name))
{
compile = matchers[i].compile;
execute = matchers[i].execute;
@@ -1681,7 +1683,7 @@ parse_grep_colors (void)
/* Empty name without val (empty cap)
* won't match and will be ignored. */
for (cap = color_dict; cap->name; cap++)
- if (strcmp(cap->name, name) == 0)
+ if (STREQ (cap->name, name))
break;
/* If name unknown, go on for forward compatibility. */
if (cap->name)
@@ -1793,9 +1795,9 @@ main (int argc, char **argv)
break;
case 'D':
- if (strcmp (optarg, "read") == 0)
+ if (STREQ (optarg, "read"))
devices = READ_DEVICES;
- else if (strcmp (optarg, "skip") == 0)
+ else if (STREQ (optarg, "skip"))
devices = SKIP_DEVICES;
else
error (EXIT_TROUBLE, 0, _("unknown devices method"));
@@ -1863,11 +1865,11 @@ main (int argc, char **argv)
break;
case 'd':
- if (strcmp (optarg, "read") == 0)
+ if (STREQ (optarg, "read"))
directories = READ_DIRECTORIES;
- else if (strcmp (optarg, "skip") == 0)
+ else if (STREQ (optarg, "skip"))
directories = SKIP_DIRECTORIES;
- else if (strcmp (optarg, "recurse") == 0)
+ else if (STREQ (optarg, "recurse"))
directories = RECURSE_DIRECTORIES;
else
error (EXIT_TROUBLE, 0, _("unknown directories method"));
@@ -1882,7 +1884,7 @@ main (int argc, char **argv)
break;
case 'f':
- fp = strcmp (optarg, "-") != 0 ? fopen (optarg, "r") : stdin;
+ fp = STREQ (optarg, "-") ? stdin : fopen (optarg, "r");
if (!fp)
error (EXIT_TROUBLE, errno, "%s", optarg);
for (keyalloc = 1; keyalloc <= keycc + 1; keyalloc *= 2)
@@ -1986,11 +1988,11 @@ main (int argc, char **argv)
break;
case BINARY_FILES_OPTION:
- if (strcmp (optarg, "binary") == 0)
+ if (STREQ (optarg, "binary"))
binary_files = BINARY_BINARY_FILES;
- else if (strcmp (optarg, "text") == 0)
+ else if (STREQ (optarg, "text"))
binary_files = TEXT_BINARY_FILES;
- else if (strcmp (optarg, "without-match") == 0)
+ else if (STREQ (optarg, "without-match"))
binary_files = WITHOUT_MATCH_BINARY_FILES;
else
error (EXIT_TROUBLE, 0, _("unknown binary-files type"));
@@ -2015,7 +2017,7 @@ main (int argc, char **argv)
{
char const *t;
if (isatty (STDOUT_FILENO) && (t = getenv ("TERM"))
- && strcmp (t, "dumb"))
+ && !STREQ (t, "dumb"))
color_option = 1;
else
color_option = 0;
@@ -2171,7 +2173,7 @@ There is NO WARRANTY, to the extent permitted by law.\n"),
excluded_file_name (excluded_patterns, file))
continue;
}
- status &= grepfile (strcmp (file, "-") == 0 ? (char *) NULL : file,
+ status &= grepfile (STREQ (file, "-") ? (char *) NULL : file,
&stats_base);
}
while ( ++optind < argc);
--
1.7.0.4.529.g78fb
>From a163349de41bd7dc7f965d3b71332039bdd2a650 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Thu, 1 Apr 2010 16:41:51 +0200
Subject: [PATCH 05/12] maint: MBS_SUPPORT: define to 0/1, not undef/1
Prepare to remove many of these #ifdefs.
* src/mbsupport.h (MBS_SUPPORT): Define to 0/1, not undef/1.
Change each "#ifdef MBS_SUPPORT" to "#if MBS_SUPPORT". Use this:
perl -pi -e 's/ifdef (MBS_SUPPORT)/if $1/' $(g grep -l ifdef.MBS_SUPPO)
* src/dfa.c: s/#ifdef MBS_SUPPORT/#if MBS_SUPPORT/
* src/dfa.h: Likewise.
* src/dfasearch.c: Likewise.
* src/kwsearch.c: Likewise.
* src/main.c: Likewise.
* src/search.h: Likewise.
* src/searchutils.c: Likewise.
---
src/dfa.c | 92 ++++++++++++++++++++++++++--------------------------
src/dfa.h | 8 ++--
src/dfasearch.c | 6 ++--
src/kwsearch.c | 6 ++--
src/main.c | 2 +-
src/mbsupport.h | 2 +-
src/search.h | 4 +-
src/searchutils.c | 4 +-
8 files changed, 62 insertions(+), 62 deletions(-)
diff --git a/src/dfa.c b/src/dfa.c
index f2c7a31..37f1f5e 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -48,7 +48,7 @@
#define _(str) gettext (str)
#include "mbsupport.h" /* defines MBS_SUPPORT if appropriate */
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
/* We can handle multibyte strings. */
# include <wchar.h>
# include <wctype.h>
@@ -117,7 +117,7 @@ prtok (token t)
case ORTOP: s = "ORTOP"; break;
case LPAREN: s = "LPAREN"; break;
case RPAREN: s = "RPAREN"; break;
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
case ANYCHAR: s = "ANYCHAR"; break;
case MBCSET: s = "MBCSET"; break;
#endif /* MBS_SUPPORT */
@@ -217,7 +217,7 @@ dfasyntax (reg_syntax_t bits, int fold, unsigned char eol)
so the resulting charset may only be used as an optimization. */
static void
setbit_case_fold (
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
wint_t b,
#else
unsigned int b,
@@ -226,7 +226,7 @@ setbit_case_fold (
{
if (case_fold)
{
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (MB_CUR_MAX > 1)
{
wint_t b1 = iswupper(b) ? towlower(b) : b;
@@ -248,7 +248,7 @@ setbit_case_fold (
}
else
{
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (wctob ((unsigned char)b) == b)
#endif
setbit (b, c);
@@ -290,7 +290,7 @@ static int hard_LC_COLLATE; /* Nonzero if LC_COLLATE is
hard. */
static int cur_mb_len = 1; /* Length of the multibyte representation of
wctok. */
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
/* These variables are used only if (MB_CUR_MAX > 1). */
static mbstate_t mbs; /* Mbstate for mbrlen(). */
static wchar_t wctok; /* Wide character representation of the current
@@ -316,7 +316,7 @@ static unsigned char const *buf_end; /* reference to
end in dfaexec(). */
#endif /* MBS_SUPPORT */
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
/* Note that characters become unsigned here. */
# define FETCH_WC(c, wc, eoferr) \
do { \
@@ -424,7 +424,7 @@ parse_bracket_exp (void)
int c, c1, c2;
charclass ccl;
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
wint_t wc, wc1, wc2;
/* Work area to build a mb_char_classes. */
@@ -478,7 +478,7 @@ parse_bracket_exp (void)
/* If pattern contains `[[:', `[[.', or `[[='. */
if (c1 == ':'
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
/* TODO: handle `[[.' and `[[=' also for MB_CUR_MAX == 1. */
|| (MB_CUR_MAX > 1 && (c1 == '.' || c1 == '='))
#endif
@@ -508,7 +508,7 @@ parse_bracket_exp (void)
|| STREQ (str, "lower"))
? "alpha"
: str);
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (MB_CUR_MAX > 1)
{
/* Store the character class as wctype_t. */
@@ -533,7 +533,7 @@ parse_bracket_exp (void)
}
}
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
else if (c1 == '=' || c1 == '.')
{
char *elem;
@@ -598,7 +598,7 @@ parse_bracket_exp (void)
&& (syntax_bits & RE_BACKSLASH_ESCAPE_IN_LISTS))
FETCH_WC(c2, wc2, _("unbalanced ["));
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (MB_CUR_MAX > 1)
{
/* When case folding map a range, say [m-z] (or even [M-z])
@@ -652,7 +652,7 @@ parse_bracket_exp (void)
continue;
}
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
/* Build normal characters. */
setbit_case_fold (wc, ccl);
if (MB_CUR_MAX > 1)
@@ -686,12 +686,12 @@ parse_bracket_exp (void)
#endif
}
while ((
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
wc = wc1,
#endif
(c = c1) != ']'));
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (MB_CUR_MAX > 1
&& (!using_utf8()
|| invert
@@ -710,7 +710,7 @@ parse_bracket_exp (void)
if (invert)
{
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
assert(MB_CUR_MAX == 1);
#endif
notset(ccl);
@@ -740,7 +740,7 @@ lex (void)
"if (backslash) ...". */
for (i = 0; i < 2; ++i)
{
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (MB_CUR_MAX > 1)
{
FETCH_WC (c, wctok, NULL);
@@ -974,7 +974,7 @@ lex (void)
case '.':
if (backslash)
goto normal_char;
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (MB_CUR_MAX > 1)
{
/* In multibyte environment period must match with a single
@@ -1029,7 +1029,7 @@ lex (void)
default:
normal_char:
laststart = 0;
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
/* For multibyte character sets, folding is done in atom. Always
return WCHAR. */
if (MB_CUR_MAX > 1)
@@ -1065,7 +1065,7 @@ static int depth; /* Current depth of a
hypothetical stack
static void
addtok_mb (token t, int mbprop)
{
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (MB_CUR_MAX > 1)
{
REALLOC_IF_NECESSARY(dfa->multibyte_prop, int, dfa->nmultibyte_prop,
@@ -1107,7 +1107,7 @@ addtok_mb (token t, int mbprop)
static void
addtok (token t)
{
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (MB_CUR_MAX > 1 && t == MBCSET)
addtok_mb (MBCSET, ((dfa->nmbcsets - 1) << 2) + 3);
else
@@ -1115,7 +1115,7 @@ addtok (token t)
addtok_mb (t, 3);
}
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
/* We treat a multibyte character as a single atom, so that DFA
can treat a multibyte character as a single expression.
@@ -1184,7 +1184,7 @@ addtok_wc (wint_t wc)
static void
atom (void)
{
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (tok == WCHAR)
{
addtok_wc (case_fold ? towlower(wctok) : wctok);
@@ -1203,7 +1203,7 @@ atom (void)
if ((tok >= 0 && tok < NOTCHAR) || tok >= CSET || tok == BACKREF
|| tok == BEGLINE || tok == ENDLINE || tok == BEGWORD
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
|| tok == ANYCHAR || tok == MBCSET /* MB_CUR_MAX > 1 */
#endif /* MBS_SUPPORT */
|| tok == ENDWORD || tok == LIMWORD || tok == NOTLIMWORD)
@@ -1254,7 +1254,7 @@ copytoks (int tindex, int ntokens)
for (i = 0; i < ntokens; ++i)
{
addtok(dfa->tokens[tindex + i]);
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
/* Update index into multibyte csets. */
if (MB_CUR_MAX > 1 && dfa->tokens[tindex + i] == MBCSET)
dfa->multibyte_prop[dfa->tindex - 1] = dfa->multibyte_prop[tindex + i];
@@ -1338,7 +1338,7 @@ dfaparse (char const *s, size_t len, struct dfa *d)
#ifdef LC_COLLATE
hard_LC_COLLATE = hard_locale (LC_COLLATE);
#endif
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (MB_CUR_MAX > 1)
{
cur_mb_len = 0;
@@ -1489,7 +1489,7 @@ state_index (struct dfa *d, position_set const *s, int
newline, int letter)
d->states[i].backref = 0;
d->states[i].constraint = 0;
d->states[i].first_end = 0;
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
d->states[i].mbps.nelem = 0;
d->states[i].mbps.elems = NULL;
#endif
@@ -1533,7 +1533,7 @@ epsclosure (position_set *s, struct dfa const *d)
for (i = 0; i < s->nelem; ++i)
if (d->tokens[s->elems[i].index] >= NOTCHAR
&& d->tokens[s->elems[i].index] != BACKREF
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
&& d->tokens[s->elems[i].index] != ANYCHAR
&& d->tokens[s->elems[i].index] != MBCSET
#endif
@@ -1816,7 +1816,7 @@ dfaanalyze (struct dfa *d, int searchflag)
it with its epsilon closure. */
for (i = 0; i < d->tindex; ++i)
if (d->tokens[i] < NOTCHAR || d->tokens[i] == BACKREF
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
|| d->tokens[i] == ANYCHAR
|| d->tokens[i] == MBCSET
#endif
@@ -1921,7 +1921,7 @@ dfastate (int s, struct dfa *d, int trans[])
int wants_letter; /* New state wants to know letter context. */
int state_letter; /* New state on a letter transition. */
static int initialized; /* Flag for static initialization. */
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
int next_isnt_1st_byte = 0; /* Flag if we can't add state0. */
#endif
int i, j, k;
@@ -1945,7 +1945,7 @@ dfastate (int s, struct dfa *d, int trans[])
setbit(d->tokens[pos.index], matches);
else if (d->tokens[pos.index] >= CSET)
copyset(d->charclasses[d->tokens[pos.index] - CSET], matches);
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
else if (d->tokens[pos.index] == ANYCHAR
|| d->tokens[pos.index] == MBCSET)
/* MB_CUR_MAX > 1 */
@@ -2098,7 +2098,7 @@ dfastate (int s, struct dfa *d, int trans[])
for (k = 0; k < d->follows[grps[i].elems[j].index].nelem; ++k)
insert(d->follows[grps[i].elems[j].index].elems[k], &follows);
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (d->mb_cur_max > 1)
{
/* If a token in follows.elems is not 1st byte of a multibyte
@@ -2133,7 +2133,7 @@ dfastate (int s, struct dfa *d, int trans[])
/* If we are building a searching matcher, throw in the positions
of state 0 as well. */
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (d->searchflag && (d->mb_cur_max == 1 || !next_isnt_1st_byte))
#else
if (d->searchflag)
@@ -2281,7 +2281,7 @@ build_state_zero (struct dfa *d)
build_state(0, d);
}
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
/* Multibyte character handling sub-routines for dfaexec. */
/* Initial state may encounter the byte which is not a single byte character
@@ -2757,7 +2757,7 @@ dfaexec (struct dfa *d, char const *begin, char *end,
unsigned char saved_end = *(unsigned char *) end;
*end = eol;
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (d->mb_cur_max > 1)
{
int remain_bytes, i;
@@ -2802,7 +2802,7 @@ dfaexec (struct dfa *d, char const *begin, char *end,
for (;;)
{
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (d->mb_cur_max > 1)
while ((t = trans[s]))
{
@@ -2839,7 +2839,7 @@ dfaexec (struct dfa *d, char const *begin, char *end,
{
if (backref)
*backref = (d->states[s].backref != 0);
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (d->mb_cur_max > 1)
{
free(mblen_buf);
@@ -2851,7 +2851,7 @@ dfaexec (struct dfa *d, char const *begin, char *end,
}
s1 = s;
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (d->mb_cur_max > 1)
{
/* Can match with a multibyte character (and multicharacter
@@ -2872,7 +2872,7 @@ dfaexec (struct dfa *d, char const *begin, char *end,
/* Check if we've run off the end of the buffer. */
if ((char *) p > end)
{
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (d->mb_cur_max > 1)
{
free(mblen_buf);
@@ -2900,7 +2900,7 @@ dfaexec (struct dfa *d, char const *begin, char *end,
}
}
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
static void
free_mbdata (struct dfa *d)
{
@@ -2946,7 +2946,7 @@ dfainit (struct dfa *d)
MALLOC(d->tokens, token, d->talloc);
d->tindex = d->depth = d->nleaves = d->nregexps = 0;
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
d->mb_cur_max = MB_CUR_MAX;
if (d->mb_cur_max > 1)
{
@@ -2971,7 +2971,7 @@ dfainit (struct dfa *d)
#endif
}
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
static void
dfaoptimize (struct dfa *d)
{
@@ -3004,7 +3004,7 @@ dfacomp (char const *s, size_t len, struct dfa *d, int
searchflag)
dfainit(d);
dfaparse(s, len, d);
dfamust(d);
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
dfaoptimize(d);
#endif
dfaanalyze(d, searchflag);
@@ -3020,14 +3020,14 @@ dfafree (struct dfa *d)
free(d->charclasses);
free(d->tokens);
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (d->mb_cur_max > 1)
free_mbdata(d);
#endif /* MBS_SUPPORT */
for (i = 0; i < d->sindex; ++i) {
free(d->states[i].elems.elems);
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
free(d->states[i].mbps.elems);
#endif /* MBS_SUPPORT */
}
@@ -3526,7 +3526,7 @@ dfamust (struct dfa *d)
goto done;
}
else if (t >= CSET
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
|| t == ANYCHAR
|| t == MBCSET
#endif /* MBS_SUPPORT */
diff --git a/src/dfa.h b/src/dfa.h
index 1c85207..e0a575f 100644
--- a/src/dfa.h
+++ b/src/dfa.h
@@ -126,7 +126,7 @@ typedef enum
RPAREN, /* RPAREN never appears in the parse tree. */
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
ANYCHAR, /* ANYCHAR is a terminal symbol that matches
any multibyte (or single byte) characters.
It is used only if MB_CUR_MAX > 1. */
@@ -225,7 +225,7 @@ typedef struct
char backref; /* True if this state matches a
\<digit>. */
unsigned char constraint; /* Constraint for this state to accept. */
int first_end; /* Token value of the first END in elems. */
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
position_set mbps; /* Positions which can match multibyte
characters. e.g. period.
These staff are used only if
@@ -242,7 +242,7 @@ struct dfamust
struct dfamust *next;
};
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
/* A bracket operator.
e.g. [a-c], [[:alpha:]], etc. */
struct mb_char_classes
@@ -281,7 +281,7 @@ struct dfa
int nleaves; /* Number of leaves on the parse tree. */
int nregexps; /* Count of parallel regexps being built
with dfaparse(). */
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
unsigned int mb_cur_max; /* Cached value of MB_CUR_MAX. */
/* The following are used only if MB_CUR_MAX > 1. */
diff --git a/src/dfasearch.c b/src/dfasearch.c
index cb9578a..08f3767 100644
--- a/src/dfasearch.c
+++ b/src/dfasearch.c
@@ -67,7 +67,7 @@ kwsincr_case (const char *must)
size_t n;
n = strlen (must);
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (match_icase && MB_CUR_MAX > 1)
buf = mbtolower (must, &n);
else
@@ -207,7 +207,7 @@ EGexecute (char const *buf, size_t size, size_t *match_size,
int backref, start, len, best_len;
struct kwsmatch kwsm;
size_t i, ret_val;
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (MB_CUR_MAX > 1)
{
if (match_icase)
@@ -248,7 +248,7 @@ EGexecute (char const *buf, size_t size, size_t *match_size,
--beg;
if (kwsm.index < kwset_exact_matches)
{
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (mb_start < beg)
mb_start = beg;
if (MB_CUR_MAX == 1
diff --git a/src/kwsearch.c b/src/kwsearch.c
index 0334665..1da6623 100644
--- a/src/kwsearch.c
+++ b/src/kwsearch.c
@@ -37,7 +37,7 @@ Fcompile (char const *pattern, size_t size)
kwsinit (&kwset);
psize = size;
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (match_icase && MB_CUR_MAX > 1)
pat = mbtolower (pattern, &psize);
else
@@ -85,7 +85,7 @@ Fexecute (char const *buf, size_t size, size_t *match_size,
char eol = eolbyte;
struct kwsmatch kwsmatch;
size_t ret_val;
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (MB_CUR_MAX > 1)
{
if (match_icase)
@@ -104,7 +104,7 @@ Fexecute (char const *buf, size_t size, size_t *match_size,
if (offset == (size_t) -1)
goto failure;
len = kwsmatch.size[0];
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
if (MB_CUR_MAX > 1 && is_mb_middle (&mb_start, beg + offset, buf + size,
len))
{
diff --git a/src/main.c b/src/main.c
index c5d6dea..11406ee 100644
--- a/src/main.c
+++ b/src/main.c
@@ -26,7 +26,7 @@
# include <sys/resource.h>
#endif
#include "mbsupport.h"
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
# include <wchar.h>
# include <wctype.h>
#endif
diff --git a/src/mbsupport.h b/src/mbsupport.h
index 57df54c..ce2007b 100644
--- a/src/mbsupport.h
+++ b/src/mbsupport.h
@@ -33,5 +33,5 @@
#if defined HAVE_WCSCOLL && defined HAVE_ISWCTYPE
# define MBS_SUPPORT 1
#else
-# undef MBS_SUPPORT
+# define MBS_SUPPORT 0
#endif
diff --git a/src/search.h b/src/search.h
index e9049a9..58461c9 100644
--- a/src/search.h
+++ b/src/search.h
@@ -24,7 +24,7 @@
#include <sys/types.h>
#include "mbsupport.h"
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
/* We can handle multibyte strings. */
# include <wchar.h>
# include <wctype.h>
@@ -40,7 +40,7 @@
/* searchutils.c */
void kwsinit (kwset_t *);
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
char * mbtolower (const char *, size_t *);
bool is_mb_middle(const char **, const char *, const char *, size_t);
#endif
diff --git a/src/searchutils.c b/src/searchutils.c
index bb3a0db..d8321e7 100644
--- a/src/searchutils.c
+++ b/src/searchutils.c
@@ -28,7 +28,7 @@ kwsinit (kwset_t *kwset)
int i;
if (match_icase
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
&& MB_CUR_MAX == 1
#endif
)
@@ -45,7 +45,7 @@ kwsinit (kwset_t *kwset)
xalloc_die ();
}
-#ifdef MBS_SUPPORT
+#if MBS_SUPPORT
/* Convert the *N-byte string, BEG, to lowercase, and write the
NUL-terminated result into malloc'd storage. Upon success, set *N
to the length (in bytes) of the resulting string (not including the
--
1.7.0.4.529.g78fb
>From 97f127bfd2cd2ea6af3a2cbecc53b7b2d1edcd3c Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Thu, 1 Apr 2010 16:49:37 +0200
Subject: [PATCH 06/12] maint: include <wchar.h> and <wctype.h> unconditionally
* src/main.c: Include <wchar.h> and <wctype.h> unconditionally.
Their presence/usefulness are assured by gnulib.
* src/dfa.c: Likewise.
* src/search.h: Likewise.
---
src/dfa.c | 8 ++++----
src/main.c | 6 ++----
src/search.h | 10 ++++------
3 files changed, 10 insertions(+), 14 deletions(-)
diff --git a/src/dfa.c b/src/dfa.c
index 37f1f5e..19b9b1b 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -48,10 +48,10 @@
#define _(str) gettext (str)
#include "mbsupport.h" /* defines MBS_SUPPORT if appropriate */
-#if MBS_SUPPORT
-/* We can handle multibyte strings. */
-# include <wchar.h>
-# include <wctype.h>
+#include <wchar.h>
+#include <wctype.h>
+
+#if HAVE_LANGINFO_CODESET
# include <langinfo.h>
#endif
diff --git a/src/main.c b/src/main.c
index 11406ee..690c905 100644
--- a/src/main.c
+++ b/src/main.c
@@ -26,10 +26,8 @@
# include <sys/resource.h>
#endif
#include "mbsupport.h"
-#if MBS_SUPPORT
-# include <wchar.h>
-# include <wctype.h>
-#endif
+#include <wchar.h>
+#include <wctype.h>
#include <fcntl.h>
#include <stdio.h>
#include "system.h"
diff --git a/src/search.h b/src/search.h
index 58461c9..6df7019 100644
--- a/src/search.h
+++ b/src/search.h
@@ -24,16 +24,14 @@
#include <sys/types.h>
#include "mbsupport.h"
-#if MBS_SUPPORT
-/* We can handle multibyte strings. */
-# include <wchar.h>
-# include <wctype.h>
-#endif
+#include <wchar.h>
+#include <wctype.h>
#include <regex.h>
+
#include "system.h"
-#include "grep.h"
#include "error.h"
+#include "grep.h"
#include "kwset.h"
#include "xalloc.h"
--
1.7.0.4.529.g78fb
>From 9eee3e657ab4632f8a45d6d20b025288db46cbfb Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Fri, 2 Apr 2010 15:18:50 +0200
Subject: [PATCH 07/12] maint: const-correctness
* src/dfa.c (tstbit, copyset, equal, charclass_index): Declare read-only
"charclass" parameters to be "const". No semantic change.
---
src/dfa.c | 8 ++++----
1 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/dfa.c b/src/dfa.c
index 19b9b1b..147cc90 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -131,7 +131,7 @@ prtok (token t)
/* Stuff pertaining to charclasses. */
static int
-tstbit (unsigned int b, charclass c)
+tstbit (unsigned int b, charclass const c)
{
return c[b / INTBITS] & 1 << b % INTBITS;
}
@@ -149,7 +149,7 @@ clrbit (unsigned int b, charclass c)
}
static void
-copyset (charclass src, charclass dst)
+copyset (charclass const src, charclass dst)
{
memcpy (dst, src, sizeof (charclass));
}
@@ -170,7 +170,7 @@ notset (charclass s)
}
static int
-equal (charclass s1, charclass s2)
+equal (charclass const s1, charclass const s2)
{
return memcmp (s1, s2, sizeof (charclass)) == 0;
}
@@ -180,7 +180,7 @@ static struct dfa *dfa;
/* Find the index of charclass s in dfa->charclasses, or allocate a new
charclass. */
static int
-charclass_index (charclass s)
+charclass_index (charclass const s)
{
int i;
--
1.7.0.4.529.g78fb
>From ac08601c3dca2dc83c1d8eadaa3bbf6a04719d4f Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Thu, 1 Apr 2010 18:09:57 +0200
Subject: [PATCH 08/12] dfa.c: use a better (unsigned) type for an index:
int->size_t
* src/dfa.c (parse_bracket_exp): Use size_t as type of index, not int.
---
src/dfa.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/src/dfa.c b/src/dfa.c
index 147cc90..26afa56 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -484,7 +484,7 @@ parse_bracket_exp (void)
#endif
)
{
- int len = 0;
+ size_t len = 0;
for (;;)
{
FETCH_WC (c, wc, _("unbalanced ["));
--
1.7.0.4.529.g78fb
>From 20af8515611d62229cd8aa35ab7dff415984aefb Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Fri, 2 Apr 2010 15:44:17 +0200
Subject: [PATCH 09/12] maint: style: use sizeof VAR, rather than sizeof TYPE,
where possible
* src/dfa.c (copyset, zeroset): Prefer sizeof EXPR, over sizeof TYPE,
for improved readability/maintainability.
(equal, parse_bracket_exp, addtok_wc, dfaparse, dfaexec): Likewise.
---
src/dfa.c | 8 ++++----
1 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/dfa.c b/src/dfa.c
index 26afa56..bee0b00 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -452,7 +452,7 @@ parse_bracket_exp (void)
work_mbc = NULL;
#endif
- memset (ccl, 0, sizeof(ccl));
+ memset (ccl, 0, sizeof ccl);
FETCH_WC (c, wc, _("unbalanced ["));
if (c == '^')
{
@@ -1128,7 +1128,7 @@ addtok_wc (wint_t wc)
unsigned char buf[MB_LEN_MAX];
mbstate_t s;
int i;
- memset (&s, 0, sizeof(s));
+ memset (&s, 0, sizeof s);
cur_mb_len = wcrtomb ((char *) buf, wc, &s);
/* This is merely stop-gap. When cur_mb_len is 0 or negative,
@@ -1342,7 +1342,7 @@ dfaparse (char const *s, size_t len, struct dfa *d)
if (MB_CUR_MAX > 1)
{
cur_mb_len = 0;
- memset(&mbs, 0, sizeof(mbstate_t));
+ memset(&mbs, 0, sizeof mbs);
}
#endif /* MBS_SUPPORT */
@@ -2767,7 +2767,7 @@ dfaexec (struct dfa *d, char const *begin, char *end,
/* initialize mblen_buf, and inputwcs. */
MALLOC(mblen_buf, unsigned char, end - begin + 2);
MALLOC(inputwcs, wchar_t, end - begin + 2);
- memset(&mbs, 0, sizeof(mbstate_t));
+ memset(&mbs, 0, sizeof mbs);
remain_bytes = 0;
for (i = 0; i < end - begin + 1; i++)
{
--
1.7.0.4.529.g78fb
>From af5005b163ec2b43d895168a0f2242f5de8d7c2b Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Fri, 2 Apr 2010 15:55:24 +0200
Subject: [PATCH 10/12] dfa.c: use a better (unsigned) type for an index:
int->unsigned int
* src/dfa.c (dfaexec): Use "unsigned int" for a logically unsigned index.
---
src/dfa.c | 5 +++--
1 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/src/dfa.c b/src/dfa.c
index bee0b00..a931979 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -2740,7 +2740,7 @@ dfaexec (struct dfa *d, char const *begin, char *end,
if (! sbit_init)
{
- int i;
+ unsigned int i;
sbit_init = 1;
for (i = 0; i < NOTCHAR; ++i)
@@ -2760,7 +2760,8 @@ dfaexec (struct dfa *d, char const *begin, char *end,
#if MBS_SUPPORT
if (d->mb_cur_max > 1)
{
- int remain_bytes, i;
+ unsigned int i;
+ int remain_bytes;
buf_begin = (unsigned char *) begin;
buf_end = (unsigned char *) end;
--
1.7.0.4.529.g78fb
>From 1e2eac49d65c715c6c5534c3ae185a0b03ce8453 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Fri, 2 Apr 2010 16:20:47 +0200
Subject: [PATCH 11/12] dfa.c: const correctness; and remove useless casts of
realloc and malloc
* src/dfa.c (icatalloc, icpyalloc, istrstr, enlist): As above.
(inboth, dfamust, comsubs): Likewise.
---
src/dfa.c | 30 +++++++++++++++---------------
1 files changed, 15 insertions(+), 15 deletions(-)
diff --git a/src/dfa.c b/src/dfa.c
index a931979..ca32b66 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -3138,7 +3138,7 @@ dfafree (struct dfa *d)
'psi|epsilon' is likelier)? */
static char *
-icatalloc (char *old, char *new)
+icatalloc (char const *old, char const *new)
{
char *result;
size_t oldsize, newsize;
@@ -3147,33 +3147,33 @@ icatalloc (char *old, char *new)
if (old == NULL)
oldsize = 0;
else if (newsize == 0)
- return old;
+ return (char *) old;
else oldsize = strlen(old);
if (old == NULL)
- result = (char *) malloc(newsize + 1);
+ result = malloc(newsize + 1);
else
- result = (char *) realloc((void *) old, oldsize + newsize + 1);
+ result = realloc((void *) old, oldsize + newsize + 1);
if (result != NULL && new != NULL)
(void) strcpy(result + oldsize, new);
return result;
}
static char *
-icpyalloc (char *string)
+icpyalloc (char const *string)
{
- return icatalloc((char *) NULL, string);
+ return icatalloc (NULL, string);
}
static char *
-istrstr (char *lookin, char *lookfor)
+istrstr (char const *lookin, char const *lookfor)
{
- char *cp;
+ char const *cp;
size_t len;
len = strlen(lookfor);
for (cp = lookin; *cp != '\0'; ++cp)
if (strncmp(cp, lookfor, len) == 0)
- return cp;
+ return (char *) cp;
return NULL;
}
@@ -3225,7 +3225,7 @@ enlist (char **cpp, char *new, size_t len)
cpp[i] = NULL;
}
/* Add the new string. */
- cpp = (char **) realloc((char *) cpp, (i + 2) * sizeof *cpp);
+ cpp = realloc((char *) cpp, (i + 2) * sizeof *cpp);
if (cpp == NULL)
return NULL;
cpp[i] = new;
@@ -3237,7 +3237,7 @@ enlist (char **cpp, char *new, size_t len)
list of their distinct common substrings. Return NULL if something
seems wild. */
static char **
-comsubs (char *left, char *right)
+comsubs (char *left, char const *right)
{
char **cpp;
char *lcp;
@@ -3246,7 +3246,7 @@ comsubs (char *left, char *right)
if (left == NULL || right == NULL)
return NULL;
- cpp = (char **) malloc(sizeof *cpp);
+ cpp = malloc(sizeof *cpp);
if (cpp == NULL)
return NULL;
cpp[0] = NULL;
@@ -3297,7 +3297,7 @@ inboth (char **left, char **right)
if (left == NULL || right == NULL)
return NULL;
- both = (char **) malloc(sizeof *both);
+ both = malloc(sizeof *both);
if (both == NULL)
return NULL;
both[0] = NULL;
@@ -3352,7 +3352,7 @@ dfamust (struct dfa *d)
result = empty_string;
exact = 0;
- musts = (must *) malloc((d->tindex + 1) * sizeof *musts);
+ musts = malloc((d->tindex + 1) * sizeof *musts);
if (musts == NULL)
return;
mp = musts;
@@ -3360,7 +3360,7 @@ dfamust (struct dfa *d)
mp[i] = must0;
for (i = 0; i <= d->tindex; ++i)
{
- mp[i].in = (char **) malloc(sizeof *mp[i].in);
+ mp[i].in = malloc(sizeof *mp[i].in);
mp[i].left = malloc(2);
mp[i].right = malloc(2);
mp[i].is = malloc(2);
--
1.7.0.4.529.g78fb
>From 54dd6301e2f3e452abc4ccdbe6893f342fdb7abb Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sat, 3 Apr 2010 10:35:34 +0200
Subject: [PATCH 12/12] maint: use argmatch, for better --directories=INVAL
diagnostics
Before, you'd see this:
grep: unknown directories method
Now, you'll see this:
grep: invalid argument `INVAL' for `--directories'
Valid arguments are:
- `read'
- `recurse'
- `skip'
Usage: src/grep [OPTION]... PATTERN [FILE]...
Try `src/grep --help' for more information.
* bootstrap.conf: Add argmatch.
* configure.ac: Define ARGMATCH_DIE and ARGMATCH_DIE_DECL.
* src/main.c (directories_type): Define.
(directories_args, directories_types) Define.
All of the above so we can...
(main): Use XARGMATCH.
(usage): Declare extern, now that argmatch calls it via ARGMATCH_DIE.
---
bootstrap.conf | 1 +
configure.ac | 6 ++++++
src/main.c | 35 +++++++++++++++++++++--------------
3 files changed, 28 insertions(+), 14 deletions(-)
diff --git a/bootstrap.conf b/bootstrap.conf
index 682e578..54d4775 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -20,6 +20,7 @@
gnulib_modules='
alloca
announce-gen
+argmatch
atexit
binary-io
btowc
diff --git a/configure.ac b/configure.ac
index 56452e7..820b459 100644
--- a/configure.ac
+++ b/configure.ac
@@ -128,6 +128,12 @@ if test "$gl_gcc_warnings" = yes; then
AC_SUBST([GNULIB_WARN_CFLAGS])
fi
+# By default, argmatch should fail calling usage (1).
+AC_DEFINE([ARGMATCH_DIE], [usage (1)],
+ [Define to the function xargmatch calls on failures.])
+AC_DEFINE([ARGMATCH_DIE_DECL], [void usage (int _e)],
+ [Define to the declaration of the xargmatch failure function.])
+
dnl Checks for header files.
AC_HEADER_STDC
AC_HEADER_DIRENT
diff --git a/src/main.c b/src/main.c
index 690c905..ddb1413 100644
--- a/src/main.c
+++ b/src/main.c
@@ -32,6 +32,7 @@
#include <stdio.h>
#include "system.h"
+#include "argmatch.h"
#include "c-ctype.h"
#include "closeout.h"
#include "error.h"
@@ -351,13 +352,25 @@ unsigned char eolbyte;
static char const *filename;
static int errseen;
-/* How to handle directories. */
-static enum
+enum directories_type
{
- READ_DIRECTORIES,
+ READ_DIRECTORIES = 2,
RECURSE_DIRECTORIES,
SKIP_DIRECTORIES
- } directories = READ_DIRECTORIES;
+ };
+
+/* How to handle directories. */
+static char const *const directories_args[] =
+{
+ "read", "recurse", "skip", NULL
+};
+static enum directories_type const directories_types[] =
+{
+ READ_DIRECTORIES, RECURSE_DIRECTORIES, SKIP_DIRECTORIES
+};
+ARGMATCH_VERIFY (directories_args, directories_types);
+
+static enum directories_type directories = READ_DIRECTORIES;
/* How to handle devices. */
static enum
@@ -1351,8 +1364,8 @@ grepdir (char const *dir, struct stats const *stats)
return status;
}
-static void usage (int status) __attribute__ ((noreturn));
-static void
+void usage (int status) __attribute__ ((noreturn));
+void
usage (int status)
{
if (status != 0)
@@ -1863,14 +1876,8 @@ main (int argc, char **argv)
break;
case 'd':
- if (STREQ (optarg, "read"))
- directories = READ_DIRECTORIES;
- else if (STREQ (optarg, "skip"))
- directories = SKIP_DIRECTORIES;
- else if (STREQ (optarg, "recurse"))
- directories = RECURSE_DIRECTORIES;
- else
- error (EXIT_TROUBLE, 0, _("unknown directories method"));
+ directories = XARGMATCH ("--directories", optarg,
+ directories_args, directories_types);
break;
case 'e':
--
1.7.0.4.529.g78fb
- mostly-cleanup/readability/maintainability patches,
Jim Meyering <=