13 #define _RPMSX_INTERNAL
34 sxp =
xmalloc(
sizeof(*sxp) * sx->Count);
38 for (i = 0; i < sx->Count; i++) {
39 if (!sx->sxp[i].hasMetaChars)
41 memcpy(sxp + j, sx->sxp + i,
sizeof(*sxp));
46 for (i = 0; i < sx->Count; i++) {
47 if (sx->sxp[i].hasMetaChars)
49 memcpy(sxp + j, sx->sxp + i,
sizeof(*sxp));
53 sx->sxp =
_free(sx->sxp);
64 const char * s = sxp->pattern;
65 size_t ns = strlen(s);
66 const char * se = s + ns;
68 sxp->hasMetaChars = 0;
84 sxp->hasMetaChars = 1;
107 static const char *
const regex_chars =
".^$?*+|[({";
108 const char * tmp = strchr(buf,
'/');
114 for (ind = buf; ind < tmp; ind++) {
115 if (strchr(regex_chars, (
int)*ind))
128 const char * tmp = strchr(buf + 1,
'/');
151 for (i = 0; i < sx->nsxs; i++) {
153 if (stem_len != sxs->len)
155 if (strncmp(*bpp, sxs->stem, stem_len))
161 if (sx->nsxs == sx->maxsxs) {
162 sx->maxsxs = sx->maxsxs * 2 + 16;
163 sx->sxs =
xrealloc(sx->sxs,
sizeof(*sx->sxs) * sx->maxsxs);
165 sxs = sx->sxs + sx->nsxs;
168 sxs->stem = strndup(*bpp, stem_len);
170 sxs->stem =
xmalloc(stem_len+1);
171 strncpy((
char *)sxs->stem, *bpp, stem_len);
193 if (sx != NULL && stem_len > 0)
194 for (i = 0; i < sx->nsxs; i++) {
196 if (stem_len != sxs->len)
198 if (strncmp(*bpp, sxs->stem, stem_len))
214 for (i = 0; i < sx->Count; i++) {
216 sxp->pattern =
_free(sxp->pattern);
217 sxp->type =
_free(sxp->type);
218 sxp->context =
_free(sxp->context);
220 sxp->preg =
_free(sxp->preg);
222 sx->sxp =
_free(sx->sxp);
225 for (i = 0; i < sx->nsxs; i++) {
227 sxs->stem =
_free(sxs->stem);
229 sx->sxs =
_free(sx->sxs);
242 if (_rpmsxPool == NULL) {
265 for (i = 0; i < sx->Count; i++) {
266 rpmsxp sxpi = sx->sxp + i;
267 for (j = i + 1; j < sx->Count; j++) {
268 rpmsxp sxpj = sx->sxp + j;
271 if (strcmp(sxpj->pattern, sxpi->pattern))
273 if (sxpj->fmode && sxpi->fmode && sxpj->fmode != sxpi->fmode)
277 if (strcmp(sxpj->context, sxpi->context)) {
281 "ERROR: Multiple different specifications for %s (%s and %s).\n",
282 sxpi->pattern, sxpj->context, sxpi->context);
289 "WARNING: Multiple same specifications for %s.\n",
301 char buf[BUFSIZ + 1];
306 char * anchored_regex;
314 #define inc_err() nerr++
317 fn =
"%{?__file_context_path}";
321 if (myfn == NULL || *myfn ==
'\0'
322 || (fp = fopen(myfn,
"r")) == NULL)
338 for (pass = 0; pass < 2; pass++) {
344 while (fgets(buf,
sizeof(buf)-1, fp)) {
345 buf[
sizeof(buf)-1] =
'\0';
348 if (buf[len - 1] !=
'\n') {
350 _(
"%s: no newline on line number %d (only read %s)\n"),
360 if (*bp ==
'#' || *bp == 0)
363 #if defined(__GLIBC__)
364 items = sscanf(buf,
"%as %as %as", ®ex, &type, &context);
366 regex = malloc(len+1);
367 type = malloc(len+1);
368 context = malloc(len+1);
369 items = sscanf(buf,
"%s %s %s", regex, type, context);
374 _(
"%s: line number %d is missing fields (only read %s)\n"),
380 }
else if (items == 2) {
389 const char * reg_buf = regex;
390 sxp->fstem =
rpmsxAdd(sx, ®_buf);
391 sxp->pattern = regex;
394 len = strlen(reg_buf);
395 anchored_regex =
xmalloc(len + 3);
396 sprintf(anchored_regex,
"^%s$", reg_buf);
399 sxp->preg =
xcalloc(1,
sizeof(*sxp->preg));
400 regerr = regcomp(sxp->preg, anchored_regex,
401 REG_EXTENDED | REG_NOSUB);
403 char errbuf[BUFSIZ + 1];
404 (void) regerror(regerr, sxp->preg, errbuf,
sizeof(errbuf)-1);
405 errbuf[
sizeof(errbuf)-1] =
'\0';
407 _(
"%s: unable to compile regular expression %s on line number %d: %s\n"),
412 free(anchored_regex);
420 if (type[0] !=
'-' || len != 2) {
422 _(
"%s: invalid type specifier %s on line number %d\n"),
428 case 'b': sxp->fmode = S_IFBLK;
break;
429 case 'c': sxp->fmode = S_IFCHR;
break;
430 case 'd': sxp->fmode = S_IFDIR;
break;
431 case 'p': sxp->fmode = S_IFIFO;
break;
432 case 'l': sxp->fmode = S_IFLNK;
break;
433 case 's': sxp->fmode =
S_IFSOCK;
break;
434 case '-': sxp->fmode = S_IFREG;
break;
437 _(
"%s: invalid type specifier %s on line number %d\n"),
445 sxp->context = context;
447 if (strcmp(context,
"<<none>>")) {
450 _(
"%s: invalid context %s on line number %d\n"),
451 fn, context, lineno);
479 if (sx->Count == 0) {
483 sx->sxp =
xcalloc(sx->Count,
sizeof(*sx->sxp));
522 return (sx != NULL ? sx->Count : 0);
527 return (sx != NULL ? sx->i : -1);
543 const char * pattern = NULL;
545 if (sx != NULL && sx->i >= 0 && sx->i < sx->Count)
546 pattern = (sx->sxp + sx->i)->pattern;
552 const char * type = NULL;
554 if (sx != NULL && sx->i >= 0 && sx->i < sx->Count)
555 type = (sx->sxp + sx->i)->type;
561 const char * context = NULL;
563 if (sx != NULL && sx->i >= 0 && sx->i < sx->Count)
564 context = (sx->sxp + sx->i)->context;
570 regex_t * preg = NULL;
572 if (sx != NULL && sx->i >= 0 && sx->i < sx->Count)
573 preg = (sx->sxp + sx->i)->preg;
581 if (sx != NULL && sx->i >= 0 && sx->i < sx->Count)
582 fmode = (sx->sxp + sx->i)->fmode;
590 if (sx != NULL && sx->i >= 0 && sx->i < sx->Count)
591 fstem = (sx->sxp + sx->i)->fstem;
601 if (sx->reverse != 0) {
609 if (sx->i >= sx->Count) {
618 fprintf(stderr,
"*** sx %p\t%s[%d]\t%s\t%s\n", sx,
"rpmsxNext", i, sxp->pattern, sxp->context);
632 sx->i = (sx->reverse ? sx->Count : -1);
641 const char * fcontext = NULL;
642 const char * myfn = fn;
657 if (sxfstem != -1 && sxfstem != fstem)
661 if (sxfmode && (fmode & S_IFMT) != sxfmode)
668 ret = regexec(preg, (sxfstem == -1 ? fn : myfn), 0, NULL, 0);
677 {
static char errbuf[BUFSIZ + 1];
678 (void) regerror(ret, preg, errbuf,
sizeof(errbuf)-1);
680 errbuf[
sizeof(errbuf)-1] =
'\0';
681 fprintf(stderr,
"unable to match %s against %s: %s\n",