Advertisement
danielhilst

0001-diff.c-Add-new-option-only-that-matches.patch

Oct 4th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 2.67 KB | None | 0 0
  1. From 6f5b9efbd09b44b25cd6bdb23a8593337075a919 Mon Sep 17 00:00:00 2001
  2. From: Daniel Hilst Selli <danielhilst@gmail.com>
  3. Date: Sat, 4 Oct 2014 03:26:01 +0000
  4. Subject: [PATCH] diff.c Add new option --only-that-matches
  5.  
  6. This option is the oposite of --exclude, only files
  7. that match the regex passed to it will be parsed
  8. ---
  9. src/diff.c | 15 +++++++++++++++
  10.  src/diff.h |  2 ++
  11.  src/dir.c  |  7 +++++++
  12.  3 files changed, 24 insertions(+)
  13.  
  14. diff --git a/src/diff.c b/src/diff.c
  15. index 397815e..b5992e2 100644
  16. --- a/src/diff.c
  17. +++ b/src/diff.c
  18. @@ -126,6 +126,7 @@ enum
  19.    SUPPRESS_COMMON_LINES_OPTION,
  20.    TABSIZE_OPTION,
  21.    TO_FILE_OPTION,
  22. +  ONLY_THAT_MATCHES_OPTION,
  23.  
  24.    /* These options must be in sequence.  */
  25.    UNCHANGED_LINE_FORMAT_OPTION,
  26. @@ -191,6 +192,7 @@ static struct option const longopts[] =
  27.    {"normal", 0, 0, NORMAL_OPTION},
  28.    {"old-group-format", 1, 0, OLD_GROUP_FORMAT_OPTION},
  29.    {"old-line-format", 1, 0, OLD_LINE_FORMAT_OPTION},
  30. +  {"only-that-matches", 1, 0, ONLY_THAT_MATCHES_OPTION},
  31.    {"paginate", 0, 0, 'l'},
  32.    {"rcs", 0, 0, 'n'},
  33.    {"recursive", 0, 0, 'r'},
  34. @@ -609,6 +611,19 @@ main (int argc, char **argv)
  35.       specify_value (&to_file, optarg, "--to-file");
  36.       break;
  37.  
  38. +        case ONLY_THAT_MATCHES_OPTION:
  39. +          {
  40. +            char regex_error[256];
  41. +            int rc = regcomp(&otm_regex, optarg, REG_NOSUB | REG_EXTENDED);
  42. +            if (rc)
  43. +              {
  44. +                regerror(rc, &otm_regex, regex_error, 256);
  45. +                error(EXIT_TROUBLE, 0, "Can't compile regex %s: %s", optarg, regex_error);
  46. +                abort();
  47. +              }
  48. +          }
  49. +          break;
  50. +
  51.     case UNCHANGED_LINE_FORMAT_OPTION:
  52.     case OLD_LINE_FORMAT_OPTION:
  53.     case NEW_LINE_FORMAT_OPTION:
  54. diff --git a/src/diff.h b/src/diff.h
  55. index e9f0471..5c91bfa 100644
  56. --- a/src/diff.h
  57. +++ b/src/diff.h
  58. @@ -328,6 +328,8 @@ XTERN FILE *outfile;
  59.  
  60.  /* Declare various functions.  */
  61.  
  62. +XTERN regex_t otm_regex;
  63. +
  64.  /* analyze.c */
  65.  extern int diff_2_files (struct comparison *);
  66.  
  67. diff --git a/src/dir.c b/src/dir.c
  68. index d3b0a2d..d6207a8 100644
  69. --- a/src/dir.c
  70. +++ b/src/dir.c
  71. @@ -90,12 +90,19 @@ dir_read (struct file_data const *dir, struct dirdata *dirdata)
  72.     {
  73.       char *d_name = next->d_name;
  74.       size_t d_size = _D_EXACT_NAMLEN (next) + 1;
  75. +          int rmatch;
  76.  
  77.       /* Ignore "." and "..".  */
  78.       if (d_name[0] == '.'
  79.           && (d_name[1] == 0 || (d_name[1] == '.' && d_name[2] == 0)))
  80.         continue;
  81.  
  82. +          rmatch = regexec(&otm_regex, d_name, 0, 0, 0);
  83. +          if (rmatch == REG_NOMATCH)
  84. +            continue;
  85. +          else if (rmatch)
  86. +              fatal("Out of memory while executing regexp");
  87. +
  88.       if (excluded_file_name (excluded, d_name))
  89.         continue;
  90.  
  91. --
  92. 2.1.0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement