9 #define _RPMSX_INTERNAL
30 sxp =
xmalloc(
sizeof(*sxp) * sx->Count);
34 for (i = 0; i < sx->Count; i++) {
35 if (!sx->sxp[i].hasMetaChars)
37 memcpy(sxp + j, sx->sxp + i,
sizeof(*sxp));
42 for (i = 0; i < sx->Count; i++) {
43 if (sx->sxp[i].hasMetaChars)
45 memcpy(sxp + j, sx->sxp + i,
sizeof(*sxp));
49 sx->sxp =
_free(sx->sxp);
60 const char * s = sxp->pattern;
61 size_t ns = strlen(s);
62 const char * se = s + ns;
64 sxp->hasMetaChars = 0;
80 sxp->hasMetaChars = 1;
103 static const char *
const regex_chars =
".^$?*+|[({";
104 const char * tmp = strchr(buf,
'/');
110 for (ind = buf; ind < tmp; ind++) {
111 if (strchr(regex_chars, (
int)*ind))
124 const char * tmp = strchr(buf + 1,
'/');
147 for (i = 0; i < sx->nsxs; i++) {
149 if (stem_len != sxs->len)
151 if (strncmp(*bpp, sxs->stem, stem_len))
157 if (sx->nsxs == sx->maxsxs) {
158 sx->maxsxs = sx->maxsxs * 2 + 16;
159 sx->sxs =
xrealloc(sx->sxs,
sizeof(*sx->sxs) * sx->maxsxs);
161 sxs = sx->sxs + sx->nsxs;
164 sxs->stem = strndup(*bpp, stem_len);
166 sxs->stem =
xmalloc(stem_len+1);
167 strncpy(sxs->stem, *bpp, stem_len);
189 if (sx != NULL && stem_len > 0)
190 for (i = 0; i < sx->nsxs; i++) {
192 if (stem_len != sxs->len)
194 if (strncmp(*bpp, sxs->stem, stem_len))
204 if (sx == NULL)
return NULL;
207 fprintf(stderr,
"--> sx %p -- %d %s at %s:%u\n", sx, sx->nrefs, msg, fn, ln);
215 if (sx == NULL)
return NULL;
220 fprintf(stderr,
"--> sx %p ++ %d %s at %s:%u\n", sx, sx->nrefs, msg, fn, ln);
238 fprintf(stderr,
"*** sx %p\t%s[%d]\n", sx, __func__, sx->Count);
243 for (i = 0; i < sx->Count; i++) {
245 sxp->pattern =
_free(sxp->pattern);
246 sxp->type =
_free(sxp->type);
247 sxp->context =
_free(sxp->context);
249 sxp->preg =
_free(sxp->preg);
251 sx->sxp =
_free(sx->sxp);
254 for (i = 0; i < sx->nsxs; i++) {
256 sxs->stem =
_free(sxs->stem);
258 sx->sxs =
_free(sx->sxs);
264 memset(sx, 0,
sizeof(*sx));
286 for (i = 0; i < sx->Count; i++) {
287 rpmsxp sxpi = sx->sxp + i;
288 for (j = i + 1; j < sx->Count; j++) {
289 rpmsxp sxpj = sx->sxp + j;
292 if (strcmp(sxpj->pattern, sxpi->pattern))
294 if (sxpj->fmode && sxpi->fmode && sxpj->fmode != sxpi->fmode)
298 if (strcmp(sxpj->context, sxpi->context)) {
302 "ERROR: Multiple different specifications for %s (%s and %s).\n",
303 sxpi->pattern, sxpj->context, sxpi->context);
310 "WARNING: Multiple same specifications for %s.\n",
322 char buf[BUFSIZ + 1];
327 char * anchored_regex;
335 #define inc_err() nerr++
339 fn =
"%{?__file_context_path}";
344 if (myfn == NULL || *myfn ==
'\0'
345 || (fp = fopen(myfn,
"r")) == NULL)
362 for (pass = 0; pass < 2; pass++) {
368 while (fgets(buf,
sizeof(buf)-1, fp)) {
369 buf[
sizeof(buf)-1] =
'\0';
372 if (buf[len - 1] !=
'\n') {
374 _(
"%s: no newline on line number %d (only read %s)\n"),
384 if (*bp ==
'#' || *bp == 0)
387 items = sscanf(buf,
"%as %as %as", ®ex, &type, &context);
391 _(
"%s: line number %d is missing fields (only read %s)\n"),
397 }
else if (items == 2) {
406 const char * reg_buf = regex;
407 sxp->fstem =
rpmsxAdd(sx, ®_buf);
408 sxp->pattern = regex;
411 len = strlen(reg_buf);
412 anchored_regex =
xmalloc(len + 3);
413 sprintf(anchored_regex,
"^%s$", reg_buf);
416 sxp->preg =
xcalloc(1,
sizeof(*sxp->preg));
417 regerr = regcomp(sxp->preg, anchored_regex,
418 REG_EXTENDED | REG_NOSUB);
420 char errbuf[BUFSIZ + 1];
421 (void) regerror(regerr, sxp->preg, errbuf,
sizeof(errbuf)-1);
422 errbuf[
sizeof(errbuf)-1] =
'\0';
424 _(
"%s: unable to compile regular expression %s on line number %d: %s\n"),
429 free(anchored_regex);
437 if (type[0] !=
'-' || len != 2) {
439 _(
"%s: invalid type specifier %s on line number %d\n"),
445 case 'b': sxp->fmode = S_IFBLK;
break;
446 case 'c': sxp->fmode = S_IFCHR;
break;
447 case 'd': sxp->fmode = S_IFDIR;
break;
448 case 'p': sxp->fmode = S_IFIFO;
break;
449 case 'l': sxp->fmode = S_IFLNK;
break;
450 case 's': sxp->fmode =
S_IFSOCK;
break;
451 case '-': sxp->fmode = S_IFREG;
break;
454 _(
"%s: invalid type specifier %s on line number %d\n"),
462 sxp->context = context;
464 if (strcmp(context,
"<<none>>")) {
467 _(
"%s: invalid context %s on line number %d\n"),
468 fn, context, lineno);
496 if (sx->Count == 0) {
500 sx->sxp =
xcalloc(sx->Count,
sizeof(*sx->sxp));
541 return (sx != NULL ? sx->Count : 0);
546 return (sx != NULL ? sx->i : -1);
562 const char * pattern = NULL;
564 if (sx != NULL && sx->i >= 0 && sx->i < sx->Count)
565 pattern = (sx->sxp + sx->i)->pattern;
571 const char * type = NULL;
573 if (sx != NULL && sx->i >= 0 && sx->i < sx->Count)
574 type = (sx->sxp + sx->i)->type;
580 const char * context = NULL;
582 if (sx != NULL && sx->i >= 0 && sx->i < sx->Count)
583 context = (sx->sxp + sx->i)->context;
589 regex_t * preg = NULL;
591 if (sx != NULL && sx->i >= 0 && sx->i < sx->Count)
592 preg = (sx->sxp + sx->i)->preg;
600 if (sx != NULL && sx->i >= 0 && sx->i < sx->Count)
601 fmode = (sx->sxp + sx->i)->fmode;
609 if (sx != NULL && sx->i >= 0 && sx->i < sx->Count)
610 fstem = (sx->sxp + sx->i)->fstem;
620 if (sx->reverse != 0) {
628 if (sx->i >= sx->Count) {
637 fprintf(stderr,
"*** sx %p\t%s[%d]\t%s\t%s\n", sx, __func__, i, sxp->pattern, sxp->context);
651 sx->i = (sx->reverse ? sx->Count : -1);
660 const char * fcontext = NULL;
661 const char * myfn = fn;
676 if (sxfstem != -1 && sxfstem != fstem)
680 if (sxfmode && (fmode & S_IFMT) != sxfmode)
687 ret = regexec(preg, (sxfstem == -1 ? fn : myfn), 0, NULL, 0);
696 {
static char errbuf[BUFSIZ + 1];
697 (void) regerror(ret, preg, errbuf,
sizeof(errbuf)-1);
699 errbuf[
sizeof(errbuf)-1] =
'\0';
700 fprintf(stderr,
"unable to match %s against %s: %s\n",