diff options
author | Nguyễn Thái Ngọc Duy <pclouds@gmail.com> | 2016-06-25 07:22:29 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2016-06-27 07:31:35 -0700 |
commit | d8acfe1eaf8621025c4095d3fbb88b2703b3fa54 (patch) | |
tree | 1ad5ff7738d18adcabdefa466b6d70f73b91f3b8 | |
parent | 949782d860889cbd50edfdf8c00e91d7a4f6cb05 (diff) | |
download | git-d8acfe1eaf8621025c4095d3fbb88b2703b3fa54.tar.gz git-d8acfe1eaf8621025c4095d3fbb88b2703b3fa54.tar.xz |
test-regex: expose full regcomp() to the command line
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r-- | test-regex.c | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/test-regex.c b/test-regex.c index 67a1a6506..eff26f534 100644 --- a/test-regex.c +++ b/test-regex.c @@ -1,4 +1,21 @@ #include "git-compat-util.h" +#include "gettext.h" + +struct reg_flag { + const char *name; + int flag; +}; + +static struct reg_flag reg_flags[] = { + { "EXTENDED", REG_EXTENDED }, + { "NEWLINE", REG_NEWLINE }, + { "ICASE", REG_ICASE }, + { "NOTBOL", REG_NOTBOL }, +#ifdef REG_STARTEND + { "STARTEND", REG_STARTEND }, +#endif + { NULL, 0 } +}; static int test_regex_bug(void) { @@ -21,8 +38,38 @@ static int test_regex_bug(void) int main(int argc, char **argv) { + const char *pat; + const char *str; + int flags = 0; + regex_t r; + regmatch_t m[1]; + if (argc == 2 && !strcmp(argv[1], "--bug")) return test_regex_bug(); - else - usage("test-regex --bug"); + else if (argc < 3) + usage("test-regex --bug\n" + "test-regex <pattern> <string> [<options>]"); + + argv++; + pat = *argv++; + str = *argv++; + while (*argv) { + struct reg_flag *rf; + for (rf = reg_flags; rf->name; rf++) + if (!strcmp(*argv, rf->name)) { + flags |= rf->flag; + break; + } + if (!rf->name) + die("do not recognize %s", *argv); + argv++; + } + git_setup_gettext(); + + if (regcomp(&r, pat, flags)) + die("failed regcomp() for pattern '%s'", pat); + if (regexec(&r, str, 1, m, 0)) + return 1; + + return 0; } |