Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/find add -regex and -iregex primaries which, like GN...



details:   https://anonhg.NetBSD.org/src/rev/88bc48c95e07
branches:  trunk
changeset: 474775:88bc48c95e07
user:      cgd <cgd%NetBSD.org@localhost>
date:      Tue Jul 20 01:28:41 1999 +0000

description:
add -regex and -iregex primaries which, like GNU find's primaries of the
same name, match files' entire paths against regular expressions.
-regex is case sensitive, -iregex is case-insensitive.  Note that these
primaries are _not_ entirely compatible with the GNU find primaries,
because their BREs appear to support alternation with \| whereas our BREs
do not.  Also note there are no primaries which provide extended regular
expressions matching, though if they are desired they would be trivial
to implement.

diffstat:

 usr.bin/find/extern.h   |   4 ++-
 usr.bin/find/find.1     |  16 ++++++++++-
 usr.bin/find/find.h     |  11 +++++--
 usr.bin/find/function.c |  70 +++++++++++++++++++++++++++++++++++++++++++++++-
 usr.bin/find/option.c   |   6 ++-
 5 files changed, 98 insertions(+), 9 deletions(-)

diffs (245 lines):

diff -r aa34301f584a -r 88bc48c95e07 usr.bin/find/extern.h
--- a/usr.bin/find/extern.h     Mon Jul 19 23:35:29 1999 +0000
+++ b/usr.bin/find/extern.h     Tue Jul 20 01:28:41 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: extern.h,v 1.11 1999/01/16 13:27:30 simonb Exp $       */
+/*     $NetBSD: extern.h,v 1.12 1999/07/20 01:28:41 cgd Exp $  */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -61,6 +61,7 @@
 PLAN   *c_fstype __P((char ***, int));
 PLAN   *c_group __P((char ***, int));
 PLAN   *c_inum __P((char ***, int));
+PLAN   *c_iregex __P((char ***, int));
 PLAN   *c_links __P((char ***, int));
 PLAN   *c_ls __P((char ***, int));
 PLAN   *c_mmin __P((char ***, int));
@@ -75,6 +76,7 @@
 PLAN   *c_print0 __P((char ***, int));
 PLAN   *c_printx __P((char ***, int));
 PLAN   *c_prune __P((char ***, int));
+PLAN   *c_regex __P((char ***, int));
 PLAN   *c_size __P((char ***, int));
 PLAN   *c_type __P((char ***, int));
 PLAN   *c_user __P((char ***, int));
diff -r aa34301f584a -r 88bc48c95e07 usr.bin/find/find.1
--- a/usr.bin/find/find.1       Mon Jul 19 23:35:29 1999 +0000
+++ b/usr.bin/find/find.1       Tue Jul 20 01:28:41 1999 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: find.1,v 1.21 1999/04/30 00:52:58 simonb Exp $
+.\"    $NetBSD: find.1,v 1.22 1999/07/20 01:28:41 cgd Exp $
 .\"
 .\" Copyright (c) 1990, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -239,6 +239,13 @@
 .It Ic -inum Ar n 
 True if the file has inode number
 .Ar n  .
+.It Ic -iregex Ar regexp
+True if the path name of the current file matches the case-insensitive
+basic regular expression
+.Pq see Xr re_format 7
+.Ar regexp .
+This is a match on the whole path, not a search for the regular expression
+within the path.
 .It Ic -links Ar n 
 True if the file has
 .Ar n
@@ -377,6 +384,13 @@
 primary has no effect if the
 .Fl d
 option was specified.
+.It Ic -regex Ar regexp
+True if the path name of the current file matches the case-sensitive
+basic regular expression
+.Pq see Xr re_format 7
+.Ar regexp .
+This is a match on the whole path, not a search for the regular expression
+within the path.
 .It Ic -size Ar n Ns Op Cm c 
 True if the file's size, rounded up, in 512\-byte blocks is
 .Ar n  .
diff -r aa34301f584a -r 88bc48c95e07 usr.bin/find/find.h
--- a/usr.bin/find/find.h       Mon Jul 19 23:35:29 1999 +0000
+++ b/usr.bin/find/find.h       Tue Jul 20 01:28:41 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: find.h,v 1.11 1999/01/16 13:27:30 simonb Exp $ */
+/*     $NetBSD: find.h,v 1.12 1999/07/20 01:28:41 cgd Exp $    */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -38,14 +38,17 @@
  *     from: @(#)find.h        8.1 (Berkeley) 6/6/93
  */
 
+#include <regex.h>
+
 /* node type */
 enum ntype {
        N_AND = 1,                              /* must start > 0 */
        N_AMIN, N_ATIME, N_CLOSEPAREN, N_CMIN, N_CTIME, N_DEPTH, N_EXEC,
-       N_EXPR, N_FLAGS, N_FOLLOW, N_FSTYPE, N_GROUP, N_INUM, N_LINKS, N_LS,
+       N_EXPR, N_FLAGS, N_FOLLOW, N_FSTYPE, N_GROUP, N_INUM, N_IREGEX,
+       N_LINKS, N_LS,
        N_MMIN, N_MTIME, N_NAME, N_NEWER, N_NOGROUP, N_NOT, N_NOUSER, N_OK,
        N_OPENPAREN, N_OR, N_PATH, N_PERM, N_PRINT, N_PRINT0, N_PRINTX,
-       N_PRUNE, N_SIZE, N_TYPE, N_USER, N_XDEV,
+       N_PRUNE, N_REGEX, N_SIZE, N_TYPE, N_USER, N_XDEV,
 };
 
 /* node definition */
@@ -80,6 +83,7 @@
                } ex;
                char *_a_data[2];               /* array of char pointers */
                char *_c_data;                  /* char pointer */
+               regex_t _regexp_data;           /* compiled regexp */
        } p_un;
 } PLAN;
 #define        a_data  p_un._a_data
@@ -97,6 +101,7 @@
 #define        e_argv  p_un.ex._e_argv
 #define        e_orig  p_un.ex._e_orig
 #define        e_len   p_un.ex._e_len
+#define        regexp_data p_un._regexp_data
 
 typedef struct _option {
        char *name;                     /* option name */
diff -r aa34301f584a -r 88bc48c95e07 usr.bin/find/function.c
--- a/usr.bin/find/function.c   Mon Jul 19 23:35:29 1999 +0000
+++ b/usr.bin/find/function.c   Tue Jul 20 01:28:41 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: function.c,v 1.30 1999/02/04 16:41:17 kleink Exp $     */
+/*     $NetBSD: function.c,v 1.31 1999/07/20 01:28:41 cgd Exp $        */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -41,7 +41,7 @@
 #if 0
 static char sccsid[] = "from: @(#)function.c   8.10 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: function.c,v 1.30 1999/02/04 16:41:17 kleink Exp $");
+__RCSID("$NetBSD: function.c,v 1.31 1999/07/20 01:28:41 cgd Exp $");
 #endif
 #endif /* not lint */
 
@@ -104,11 +104,13 @@
        int     f_print0 __P((PLAN *, FTSENT *));
        int     f_printx __P((PLAN *, FTSENT *));
        int     f_prune __P((PLAN *, FTSENT *));
+       int     f_regex __P((PLAN *, FTSENT *));
        int     f_size __P((PLAN *, FTSENT *));
        int     f_type __P((PLAN *, FTSENT *));
        int     f_user __P((PLAN *, FTSENT *));
        int     f_not __P((PLAN *, FTSENT *));
        int     f_or __P((PLAN *, FTSENT *));
+static PLAN   *c_regex_common __P((char ***, int, enum ntype, int));
 static PLAN   *palloc __P((enum ntype, int (*) __P((PLAN *, FTSENT *))));
 
 /*
@@ -1069,7 +1071,71 @@
 {
        return (palloc(N_PRUNE, f_prune));
 }
+
+/*
+ * -regex regexp (and related) functions --
+ *
+ *     True if the complete file path matches the regular expression regexp.
+ *     For -regex, regexp is a case-sensitive (basic) regular expression.
+ *     For -iregex, regexp is a case-insensitive (basic) regular expression.
+ */
+int
+f_regex(plan, entry)
+       PLAN *plan;
+       FTSENT *entry;
+{
+
+       return (regexec(&plan->regexp_data, entry->fts_path, 0, NULL, 0) == 0);
+}
  
+static PLAN *
+c_regex_common(argvp, isok, type, regcomp_flags)
+       char ***argvp;
+       int isok, regcomp_flags;
+       enum ntype type;
+{
+       char errbuf[LINE_MAX];
+       regex_t reg;
+       char *regexp = **argvp;
+       char *lineregexp;
+       PLAN *new;
+       int rv;
+
+       (*argvp)++;
+
+       lineregexp = alloca(strlen(regexp) + 1 + 6);    /* max needed */
+       sprintf(lineregexp, "^%s(%s%s)$",
+           (regcomp_flags & REG_EXTENDED) ? "" : "\\", regexp,
+           (regcomp_flags & REG_EXTENDED) ? "" : "\\");
+       rv = regcomp(&reg, lineregexp, REG_NOSUB|regcomp_flags);
+       if (rv != 0) {
+               regerror(rv, &reg, errbuf, sizeof errbuf);
+               errx(1, "regexp %s: %s", regexp, errbuf);
+       }
+       
+       new = palloc(type, f_regex);
+       new->regexp_data = reg;
+       return (new);
+}
+
+PLAN *
+c_regex(argvp, isok)
+       char ***argvp;
+       int isok;
+{
+
+       return (c_regex_common(argvp, isok, N_REGEX, REG_BASIC));
+}
+
+PLAN *
+c_iregex(argvp, isok)
+       char ***argvp;
+       int isok;
+{
+
+       return (c_regex_common(argvp, isok, N_IREGEX, REG_BASIC|REG_ICASE));
+}
+
 /*
  * -size n[c] functions --
  *
diff -r aa34301f584a -r 88bc48c95e07 usr.bin/find/option.c
--- a/usr.bin/find/option.c     Mon Jul 19 23:35:29 1999 +0000
+++ b/usr.bin/find/option.c     Tue Jul 20 01:28:41 1999 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: option.c,v 1.12 1999/01/16 13:27:31 simonb Exp $       */
+/*     $NetBSD: option.c,v 1.13 1999/07/20 01:28:42 cgd Exp $  */
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -41,7 +41,7 @@
 #if 0
 static char sccsid[] = "from: @(#)option.c     8.2 (Berkeley) 4/16/94";
 #else
-__RCSID("$NetBSD: option.c,v 1.12 1999/01/16 13:27:31 simonb Exp $");
+__RCSID("$NetBSD: option.c,v 1.13 1999/07/20 01:28:42 cgd Exp $");
 #endif
 #endif /* not lint */
 
@@ -77,6 +77,7 @@
        { "-fstype",    N_FSTYPE,       c_fstype,       1 },
        { "-group",     N_GROUP,        c_group,        1 },
        { "-inum",      N_INUM,         c_inum,         1 },
+       { "-iregex",    N_IREGEX,       c_iregex,       1 },
        { "-links",     N_LINKS,        c_links,        1 },
        { "-ls",        N_LS,           c_ls,           0 },
        { "-mmin",      N_MMIN,         c_mmin,         1 },
@@ -94,6 +95,7 @@
        { "-print0",    N_PRINT0,       c_print0,       0 },
        { "-printx",    N_PRINTX,       c_printx,       0 },
        { "-prune",     N_PRUNE,        c_prune,        0 },
+       { "-regex",     N_REGEX,        c_regex,        1 },
        { "-size",      N_SIZE,         c_size,         1 },
        { "-type",      N_TYPE,         c_type,         1 },
        { "-user",      N_USER,         c_user,         1 },



Home | Main Index | Thread Index | Old Index