[hackers] [sbase] Add grep -w support || sin
commit 64d8034aac94a593c3b94a3c63cdf36f3f816ca1
Author: sin <sin_AT_2f30.org>
Date: Thu Jan 22 17:07:57 2015 +0000
Add grep -w support
Require to use abuild on Alpine Linux with sbase.
diff --git a/grep.1 b/grep.1
index ddbb72e..15e5439 100644
--- a/grep.1
+++ b/grep.1
_AT_@ -58,6 +58,8 @@ Suppress the error messages ordinarily written for nonexistent or unreadable fil
Select lines which do
.B not
Match the pattern.
+.It Fl w
+The expression is search for as a word (as if surrounded by '[[:<:]]' and '[[:>:]]').
.It Fl x
Consider only input lines that use all characters in the line excluding the terminating <newline> to
match an entire fixed string or regular expression to be matching lines.
_AT_@ -82,5 +84,5 @@ utility is compliant with the
specification.
.Pp
The flags
-.Op Fl Hh
+.Op Fl Hhw
are an extension to that specification.
diff --git a/grep.c b/grep.c
index 1376940..7b932ca 100644
--- a/grep.c
+++ b/grep.c
_AT_@ -16,6 +16,7 @@ static void addpattern(const char *);
static void addpatternfile(FILE *);
static int grep(FILE *, const char *);
+static int Eflag;
static int Fflag;
static int Hflag;
static int eflag;
_AT_@ -24,6 +25,7 @@ static int hflag;
static int iflag;
static int sflag;
static int vflag;
+static int wflag;
static int xflag;
static int many;
static int mode;
_AT_@ -39,7 +41,7 @@ static SLIST_HEAD(phead, pattern) phead;
static void
usage(void)
{
- enprintf(Error, "usage: %s [-EFHchilnqsvx] [-e pattern] [-f file] [pattern] [file ...]\n", argv0);
+ enprintf(Error, "usage: %s [-EFHchilnqsvwx] [-e pattern] [-f file] [pattern] [file ...]\n", argv0);
}
int
_AT_@ -54,6 +56,7 @@ main(int argc, char *argv[])
ARGBEGIN {
case 'E':
+ Eflag = 1;
flags |= REG_EXTENDED;
break;
case 'F':
_AT_@ -99,6 +102,9 @@ main(int argc, char *argv[])
case 'v':
vflag = 1;
break;
+ case 'w':
+ wflag = 1;
+ break;
case 'x':
xflag = 1;
break;
_AT_@ -147,6 +153,8 @@ addpattern(const char *pattern)
{
struct pattern *pnode;
char *tmp;
+ int bol, eol;
+ size_t len;
/* a null BRE/ERE matches every line */
if (!Fflag)
_AT_@ -161,6 +169,24 @@ addpattern(const char *pattern)
pattern[0] == '^' ? "" : "^",
pattern,
pattern[strlen(pattern) - 1] == '$' ? "" : "$");
+ } else if (!Fflag && wflag) {
+ len = strlen(pattern) + 15 + (Eflag ? 2 : 4);
+ tmp = malloc(len);
+ if (!tmp)
+ enprintf(Error, "malloc:");
+
+ bol = eol = 0;
+ if (pattern[0] == '^')
+ bol = 1;
+ if (pattern[strlen(pattern) - 1] == '$')
+ eol = 1;
+
+ snprintf(tmp, len, "%s[[:<:]]%s%.*s%s[[:>:]]%s",
+ bol ? "^" : "",
+ Eflag ? "(" : "\\(",
+ (int)strlen(pattern) - bol - eol, pattern + bol,
+ Eflag ? ")" : "\\)",
+ eol ? "$" : "");
} else {
tmp = strdup(pattern);
if (!tmp)
Received on Thu Jan 22 2015 - 18:10:22 CET
This archive was generated by hypermail 2.3.0
: Thu Jan 22 2015 - 18:12:09 CET