|
| 1 | +/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright (c) 2024 Google, LLC. All rights reserved. |
| 4 | + * $COPYRIGHT$ |
| 5 | + * |
| 6 | + * Additional copyrights may follow |
| 7 | + * |
| 8 | + * $HEADER$ |
| 9 | + */ |
| 10 | + |
| 11 | +#include <regex.h> |
| 12 | + |
| 13 | +#include "opal_config.h" |
| 14 | + |
| 15 | +#include "opal/class/opal_include_list.h" |
| 16 | +#include "opal/class/opal_object.h" |
| 17 | +#include "opal/include/opal/constants.h" |
| 18 | +#include "opal/mca/base/base.h" |
| 19 | +#include "opal/util/argv.h" |
| 20 | +#include "opal/util/output.h" |
| 21 | +#include "opal/util/printf.h" |
| 22 | + |
| 23 | +static void include_list_destructor(opal_include_list_t *p); |
| 24 | + |
| 25 | +static int opal_include_list_deserialize(opal_include_list_t *object, const char *value) |
| 26 | +{ |
| 27 | + /* release any current value and set to defaults */ |
| 28 | + include_list_destructor(object); |
| 29 | + |
| 30 | + if (NULL == value || 0 == strlen(value)) { |
| 31 | + return OPAL_SUCCESS; |
| 32 | + } |
| 33 | + |
| 34 | + if ('^' == value[0]) { |
| 35 | + object->is_exclude = true; |
| 36 | + ++value; |
| 37 | + } |
| 38 | + |
| 39 | + object->items = opal_argv_split(value, ','); |
| 40 | + return OPAL_SUCCESS; |
| 41 | +} |
| 42 | + |
| 43 | +static char *opal_include_list_serialize(const opal_include_list_t *object) |
| 44 | +{ |
| 45 | + if (NULL == object->items) { |
| 46 | + return strdup(""); |
| 47 | + } |
| 48 | + |
| 49 | + char *tmp = opal_argv_join(object->items, ','); |
| 50 | + if (object->is_exclude) { |
| 51 | + char *value_str = NULL; |
| 52 | + (void) opal_asprintf(&value_str, "^%s", tmp); |
| 53 | + free(tmp); |
| 54 | + return value_str; |
| 55 | + } |
| 56 | + |
| 57 | + return tmp; |
| 58 | +} |
| 59 | + |
| 60 | +static bool opal_include_list_is_set(const opal_include_list_t *object) |
| 61 | +{ |
| 62 | + return (object->items != NULL); |
| 63 | +} |
| 64 | + |
| 65 | +static void include_list_contructor(opal_include_list_t *p) |
| 66 | +{ |
| 67 | + p->super.deserialize = (opal_serializable_deserialize_fn_t)opal_include_list_deserialize; |
| 68 | + p->super.serialize = (opal_serializable_serialize_fn_t)opal_include_list_serialize; |
| 69 | + p->super.is_set = (opal_serializable_is_set_fn_t)opal_include_list_is_set; |
| 70 | + p->items = NULL; |
| 71 | + p->is_exclude = false; |
| 72 | +} |
| 73 | + |
| 74 | +static void include_list_destructor(opal_include_list_t *p) |
| 75 | +{ |
| 76 | + opal_argv_free(p->items); |
| 77 | + include_list_contructor(p); |
| 78 | +} |
| 79 | + |
| 80 | +OBJ_CLASS_INSTANCE(opal_include_list_t, opal_object_t, include_list_contructor, |
| 81 | + include_list_destructor); |
| 82 | + |
| 83 | +static int include_list_match_regex(opal_include_list_t *include_list, const char *value, |
| 84 | + bool case_sensitive) |
| 85 | +{ |
| 86 | + int regex_flags = REG_EXTENDED | REG_NOSUB; |
| 87 | + regex_t regex; |
| 88 | + |
| 89 | + if (!case_sensitive) { |
| 90 | + regex_flags |= REG_ICASE; |
| 91 | + } |
| 92 | + |
| 93 | + for (int i = 0 ; include_list->items[i] ; ++i) { |
| 94 | + int rc = regcomp(®ex, include_list->items[i], regex_flags); |
| 95 | + if (rc != 0) { |
| 96 | + /* incorrectly formatted regular expression */ |
| 97 | + opal_output_verbose(MCA_BASE_VERBOSE_WARN, 0, "error compiling regular expression: %s, " |
| 98 | + "ignoring", include_list->items[i]); |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + rc = regexec(®ex, value, /*nmatch=*/0, /*pmatch=*/NULL, /*eflags=*/0); |
| 103 | + regfree(®ex); |
| 104 | + if (0 == rc) { |
| 105 | + return (include_list->is_exclude ? -1 : 1) * (i + 1); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return include_list->is_exclude ? 1 : -1; |
| 110 | +} |
| 111 | + |
| 112 | +static int include_list_match(opal_include_list_t *include_list, const char *value, |
| 113 | + bool case_sensitive) |
| 114 | +{ |
| 115 | + for (int i = 0 ; include_list->items[i] ; ++i) { |
| 116 | + bool match = false; |
| 117 | + if (case_sensitive) { |
| 118 | + if (0 == strcmp(include_list->items[i], value)) { |
| 119 | + match = true; |
| 120 | + } |
| 121 | + } else if (0 == strcasecmp(include_list->items[i], value)) { |
| 122 | + match = true; |
| 123 | + } |
| 124 | + |
| 125 | + if (match) { |
| 126 | + return (include_list->is_exclude ? -1 : 1) * (i + 1); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return include_list->is_exclude ? 1 : -1; |
| 131 | +} |
| 132 | + |
| 133 | +int opal_include_list_match(opal_include_list_t *include_list, const char *value, |
| 134 | + bool regex_match, bool case_sensitive) |
| 135 | +{ |
| 136 | + if (include_list == NULL || value == NULL || include_list->items == NULL) { |
| 137 | + opal_output_verbose(MCA_BASE_VERBOSE_ERROR, 0, "error matching in include list"); |
| 138 | + return -1; |
| 139 | + } |
| 140 | + |
| 141 | + if (regex_match) { |
| 142 | + return include_list_match_regex(include_list, value, case_sensitive); |
| 143 | + } |
| 144 | + |
| 145 | + return include_list_match(include_list, value, case_sensitive); |
| 146 | +} |
0 commit comments