diff options
| author | Nalin Dahyabhai <nalin.dahyabhai@pobox.com> | 2008-05-16 19:04:09 -0400 |
|---|---|---|
| committer | Nalin Dahyabhai <nalin.dahyabhai@pobox.com> | 2008-05-16 19:04:09 -0400 |
| commit | 77993730c95d779e4dc0a8649a00a4bf41000adc (patch) | |
| tree | 888cf8de8e1975c826744214a0a9f73aea166c98 /src | |
| parent | 266877f7f2577bbc442aca5be53902ebcaa1779a (diff) | |
- add a simple regmatch()
Diffstat (limited to 'src')
| -rw-r--r-- | src/format.c | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/src/format.c b/src/format.c index 3a96187..7f9c89b 100644 --- a/src/format.c +++ b/src/format.c @@ -1,6 +1,7 @@ #include <sys/types.h> #include <rpcsvc/yp.h> #include <fnmatch.h> +#include <regex.h> #include <stdlib.h> #include "defaults.h" #include "format.h" @@ -415,8 +416,8 @@ format_merge(struct plugin_state *state, Slapi_PBlock *pb, Slapi_Entry *e, return ret; } -/* Look up the entry's values for the attribute named by the second argument, - * and use the callback to check if they match the first argument. If we find +/* Look up the entry's values for the attribute named by the first argument, + * and use the callback to check if they match the second argument. If we find * exactly one match, store it in the output buffer, otherwise store the text * of the third argument if given, or return an error if no third argument was * given. */ @@ -450,14 +451,14 @@ format_match_generic(struct plugin_state *state, matched = NULL; count = 0; values = NULL; - if (slapi_vattr_values_get(e, argv[1], &values, + if (slapi_vattr_values_get(e, argv[0], &values, &disposition, &actual_attr, 0, &buffer_flags) == 0) { for (i = slapi_valueset_first_value(values, &value); i != -1; i = slapi_valueset_next_value(values, i, &value)) { cvalue = slapi_value_get_string(value); - if (match_fn(argv[0], cvalue)) { + if (match_fn(argv[1], cvalue)) { count++; if (count > 1) { break; @@ -571,6 +572,32 @@ format_match(struct plugin_state *state, "format_match", format_match_cb); } +/* Check for a regex match. */ +static PRBool +format_regmatch_cb(const char *pattern, const char *value) +{ + regex_t reg; + regmatch_t matches; + PRBool matched; + memset(®, 0, sizeof(reg)); + if (regcomp(®, pattern, REG_NOSUB) != 0) { + return PR_FALSE; + } + matched = (regexec(®, value, 1, &matches, 0) == 0); + regfree(®); + return matched; +} +static int +format_regmatch(struct plugin_state *state, + Slapi_PBlock *pb, Slapi_Entry *e, + const char *args, char *outbuf, int outbuf_len, + char ***visited_ndns) +{ + return format_match_generic(state, pb, e, args, + outbuf, outbuf_len, visited_ndns, + "format_regmatch", format_regmatch_cb); +} + /* Choose a formatting function by name. */ static void * format_lookup_fn(const char *fnname) @@ -589,6 +616,7 @@ format_lookup_fn(const char *fnname) {"deref", format_deref}, {"merge", format_merge}, {"match", format_match}, + {"regmatch", format_regmatch}, }; for (i = 0; i < sizeof(fns) / sizeof(fns[0]); i++) { if ((fns[i].name != NULL) && |
