10 #define _MIRE_INTERNAL
40 if (mire == NULL)
return 0;
41 mire->pattern =
_free(mire->pattern);
43 if (mire->preg != NULL) {
46 if (mire->preg) free(mire->preg);
52 mire->pcre =
_free(mire->pcre);
53 mire->hints =
_free(mire->hints);
66 fprintf(stderr,
"<-- mireClean(%p)\n", mire);
85 if (_mirePool == NULL) {
91 memset(((
char *)mire)+
sizeof(mire->_item), 0,
sizeof(*mire)-
sizeof(mire->_item));
102 if (mire->_item.use != NULL && mire->_item.pool != NULL)
105 if (mire) free(mire);
122 const unsigned char * table)
127 switch (mire->mode) {
163 mire->offsets = offsets;
165 mire->noffsets = noffsets;
171 mire->offsets = offsets;
173 mire->noffsets = noffsets;
185 #if defined(PCRE_CASELESS)
189 #if defined(FNM_CASEFOLD)
193 #if defined(PCRE_CASELESS)
197 #if defined(FNM_CASEFOLD)
203 #if defined(PCRE_MULTILINE)
207 #if defined(PCRE_MULTILINE)
213 #if defined(PCRE_UTF8)
217 #if defined(PCRE_UTF8)
227 if (newline == NULL) {
229 #if defined(PCRE_CONFIG_NEWLINE)
231 (void)pcre_config(PCRE_CONFIG_NEWLINE, &val);
235 default: newline =
"lf";
break;
236 case '\r': newline =
"cr";
break;
238 case (
'\r' << 8) |
'\n': newline =
"crlf";
break;
240 case -1: newline =
"any";
break;
241 case -2: newline =
"anycrlf";
break;
246 if (!strcasecmp(newline,
"cr")) {
247 #if defined(PCRE_NEWLINE_CR)
251 }
else if (!strcasecmp(newline,
"lf")) {
252 #if defined(PCRE_NEWLINE_LF)
256 }
else if (!strcasecmp(newline,
"crlf")) {
257 #if defined(PCRE_NEWLINE_CRLF)
261 }
else if (!strcasecmp(newline,
"any")) {
262 #if defined(PCRE_NEWLINE_ANY)
266 }
else if (!strcasecmp(newline,
"anycrlf")) {
267 #if defined(PCRE_NEWLINE_ANYCRLF)
280 const char * locale_from = NULL;
285 locale_from =
"--locale";
293 if ((locale =
getenv(
"LC_ALL")) != NULL)
294 locale_from =
"LC_ALL";
295 else if ((locale =
getenv(
"LC_CTYPE")) != NULL)
296 locale_from =
"LC_CTYPE";
298 locale_from =
"glibc";
308 if (locale != NULL) {
309 const char * olocale =
setlocale(LC_CTYPE, locale);
310 if (olocale == NULL) {
313 _(
"%s: Failed to set locale %s (obtained from %s)\n"),
318 #if defined(WITH_PCRE)
323 if (
setlocale(LC_CTYPE, olocale) == NULL)
336 mire->pattern =
xstrdup(pattern);
338 switch (mire->mode) {
346 mire->pcre = pcre_compile2(mire->pattern, mire->coptions,
347 &mire->errcode, &mire->errmsg, &mire->erroff, mire->table);
348 if (mire->pcre == NULL) {
351 _(
"pcre_compile2 failed: %s(%d) at offset %d of \"%s\"\n"),
352 mire->errmsg, mire->errcode, mire->erroff, mire->pattern);
362 mire->preg = (regex_t *)
xcalloc(1,
sizeof(*mire->preg));
363 if (mire->cflags == 0)
365 rc = regcomp(mire->preg, mire->pattern, mire->cflags);
368 (void) regerror(rc, mire->preg, msg,
sizeof(msg)-1);
369 msg[
sizeof(msg)-1] =
'\0';
375 if (mire->fnflags == 0)
391 fprintf(stderr,
"<-- mireRegcomp(%p, \"%s\") rc %d\n", mire, pattern, rc);
400 switch (mire->mode) {
402 if (mire->pattern == NULL)
405 vallen = strlen(val);
407 rc = strncmp(mire->pattern, val, vallen);
412 if (mire->preg == NULL)
417 char * t = strncpy(
alloca(vallen+1), val, vallen);
422 vallen = strlen(val);
425 rc = regexec(mire->preg, val,
426 mire->noffsets/3, (regmatch_t *)mire->offsets, mire->eflags);
429 case 0: rc = 0;
break;
430 case REG_NOMATCH: rc = -1;
break;
433 (void) regerror(rc, mire->preg, msg,
sizeof(msg)-1);
434 msg[
sizeof(msg)-1] =
'\0';
436 mire->pattern, msg, rc);
438 if (rc > 0) rc = -(rc+1);
444 if (mire->pcre == NULL)
447 vallen = strlen(val);
448 rc = pcre_exec((pcre *)mire->pcre, (pcre_extra *)mire->hints,
449 val, (
int)vallen, mire->startoff,
450 mire->eoptions, mire->offsets, mire->noffsets);
452 case 0: rc = 0;
break;
453 case PCRE_ERROR_NOMATCH: rc = -1;
break;
464 if (mire->pattern == NULL)
469 char * t = strncpy(
alloca(vallen+1), val, vallen);
473 rc =
fnmatch(mire->pattern, val, mire->fnflags);
475 case 0: rc = 0;
break;
481 if (rc > 0) rc = -(rc+1);
491 fprintf(stderr,
"<-- mireRegexec(%p, %p[%u]) rc %d mode %d \"%.*s\"\n", mire, val, (
unsigned)vallen, rc, mire->mode, (
int)(vallen < 20 ? vallen : 20), val);
498 const unsigned char * table,
miRE * mirep,
int * nmirep)
503 if (*mirep == NULL) {
508 void *
pool = (*mirep)->_item.pool;
511 (*mirep) = (
miRE)
xrealloc((*mirep), ((*nmirep) + 1) *
sizeof(*mire));
512 mire = (*mirep) + (*nmirep);
513 memset(mire, 0,
sizeof(*mire));
516 mire->_item.use = use;
517 mire->_item.pool = pool;
530 const unsigned char * table,
miRE * mirep,
int * nmirep)
535 if (patterns != NULL)
536 while ((pattern = *patterns++) != NULL) {
539 int xx =
mireAppend(mode, tag, pattern, table, mirep, nmirep);
559 for (i = 0; i < nmire; mire++, i++) {
563 if (rc < 0 && xx < 0)
565 if (rc > 0 && xx >= 0)
581 for (j = 0; j < nmires; mire++, j++) {
584 #if defined(WITH_PCRE)
585 {
const char * error;
586 mire->hints = pcre_study((pcre *)mire->pcre, 0, &error);
589 if (nmires == 1) s[0] =
'\0';
else sprintf(s,
_(
" number %d"), j);
599 #if defined(WITH_PCRE)
static void mireFini(void *_mire)
int mireSetEOptions(miRE mire, int *offsets, int noffsets)
Initialize pattern execute options (PCRE only).
miRE mireNew(rpmMireMode mode, int tag)
Create pattern container.
int mireApply(miRE mire, int nmire, const char *s, size_t slen, int rc)
Apply array of patterns to a string.
int _mireGLOBoptions
GLOB default: FNM_PATHNAME | FNM_PERIOD.
int mireLoadPatterns(rpmMireMode mode, int tag, const char **patterns, const unsigned char *table, miRE *mirep, int *nmirep)
Load patterns from string array.
char * getenv(const char *name)
void * mireFreeAll(miRE mire, int nmire)
Destroy compiled patterns.
char * xstrdup(const char *str)
const unsigned char * _mirePCREtables
int mireRegcomp(miRE mire, const char *pattern)
Compile pattern match.
int mireSetCOptions(miRE mire, rpmMireMode mode, int tag, int options, const unsigned char *table)
Initialize pattern compile options.
int mireClean(miRE mire)
Deallocate pattern match memory.
void * rpmioFreePoolItem(rpmioItem item, const char *msg, const char *fn, unsigned ln)
Free a pool item.
static void rpmlog(int code, const char *fmt,...)
int _mirePCREoptions
PCRE default: 0.
Yet Another syslog(3) API clone.
void * xcalloc(size_t nmemb, size_t size)
rpmioItem rpmioGetPool(rpmioPool pool, size_t size)
Get unused item from pool, or alloc a new item.
#define setlocale(Category, Locale)
int mireRegexec(miRE mire, const char *val, size_t vallen)
Execute pattern match.
enum rpmMireMode_e rpmMireMode
Tag value pattern match mode.
int mireStudy(miRE mire, int nmires)
Study PCRE patterns (if any).
int mireSetLocale(miRE mire, const char *locale)
Compile locale-specific PCRE tables.
rpmioPool rpmioNewPool(const char *name, size_t size, int limit, int flags, char *(*dbg)(void *item), void(*init)(void *item), void(*fini)(void *item))
Create a memory pool.
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
miRE mireGetPool(rpmioPool pool)
Allocate a miRE container from the pool.
int _mireSTRINGoptions
STRING default: 0.
int _mireREGEXoptions
REGEX default: REG_EXTENDED.
int mireAppend(rpmMireMode mode, int tag, const char *pattern, const unsigned char *table, miRE *mirep, int *nmirep)
Append pattern to array.
const char const char * pattern
miRE mireLink(miRE mire)
Reference a pattern container instance.
int mireSetGOptions(const char *newline, int caseless, int multiline, int utf8)
Initialize pattern global options (PCRE only).
static const char * locale
static const char * newline
const char const bson const bson int int int options
int fnmatch(char *pattern, const char *string, int flags) const
enum mireEL_e mireEL_t
Line ending types.