1*f491972cSAugustin Cavalier /*
2*f491972cSAugustin Cavalier * Copyright (c) 2007, Novell Inc.
3*f491972cSAugustin Cavalier *
4*f491972cSAugustin Cavalier * This program is licensed under the BSD license, read LICENSE.BSD
5*f491972cSAugustin Cavalier * for further information
6*f491972cSAugustin Cavalier */
7*f491972cSAugustin Cavalier
8*f491972cSAugustin Cavalier #include <stdio.h>
9*f491972cSAugustin Cavalier #include <stdlib.h>
10*f491972cSAugustin Cavalier #include <string.h>
11*f491972cSAugustin Cavalier
12*f491972cSAugustin Cavalier /* we need FNM_CASEFOLD */
13*f491972cSAugustin Cavalier #ifndef _GNU_SOURCE
14*f491972cSAugustin Cavalier #define _GNU_SOURCE
15*f491972cSAugustin Cavalier #endif
16*f491972cSAugustin Cavalier
17*f491972cSAugustin Cavalier #include <fnmatch.h>
18*f491972cSAugustin Cavalier
19*f491972cSAugustin Cavalier #include "pool.h"
20*f491972cSAugustin Cavalier #include "poolid.h"
21*f491972cSAugustin Cavalier #include "poolvendor.h"
22*f491972cSAugustin Cavalier #include "util.h"
23*f491972cSAugustin Cavalier
24*f491972cSAugustin Cavalier /*
25*f491972cSAugustin Cavalier * const char *vendorsclasses[] = {
26*f491972cSAugustin Cavalier * "!openSUSE Build Service*",
27*f491972cSAugustin Cavalier * "SUSE*",
28*f491972cSAugustin Cavalier * "openSUSE*",
29*f491972cSAugustin Cavalier * "SGI*",
30*f491972cSAugustin Cavalier * "Novell*",
31*f491972cSAugustin Cavalier * "Silicon Graphics*",
32*f491972cSAugustin Cavalier * "Jpackage Project*",
33*f491972cSAugustin Cavalier * "ATI Technologies Inc.*",
34*f491972cSAugustin Cavalier * "Nvidia*",
35*f491972cSAugustin Cavalier * 0,
36*f491972cSAugustin Cavalier * 0,
37*f491972cSAugustin Cavalier * };
38*f491972cSAugustin Cavalier */
39*f491972cSAugustin Cavalier
40*f491972cSAugustin Cavalier /* allows for 32 different vendor classes */
41*f491972cSAugustin Cavalier
pool_vendor2mask(Pool * pool,Id vendor)42*f491972cSAugustin Cavalier Id pool_vendor2mask(Pool *pool, Id vendor)
43*f491972cSAugustin Cavalier {
44*f491972cSAugustin Cavalier const char *vstr;
45*f491972cSAugustin Cavalier int i;
46*f491972cSAugustin Cavalier Id mask, m;
47*f491972cSAugustin Cavalier const char **v, *vs;
48*f491972cSAugustin Cavalier
49*f491972cSAugustin Cavalier if (vendor == 0 || !pool->vendorclasses)
50*f491972cSAugustin Cavalier return 0;
51*f491972cSAugustin Cavalier for (i = 0; i < pool->vendormap.count; i += 2)
52*f491972cSAugustin Cavalier if (pool->vendormap.elements[i] == vendor)
53*f491972cSAugustin Cavalier return pool->vendormap.elements[i + 1];
54*f491972cSAugustin Cavalier vstr = pool_id2str(pool, vendor);
55*f491972cSAugustin Cavalier m = 1;
56*f491972cSAugustin Cavalier mask = 0;
57*f491972cSAugustin Cavalier for (v = pool->vendorclasses; ; v++)
58*f491972cSAugustin Cavalier {
59*f491972cSAugustin Cavalier vs = *v;
60*f491972cSAugustin Cavalier if (vs == 0) /* end of block? */
61*f491972cSAugustin Cavalier {
62*f491972cSAugustin Cavalier v++;
63*f491972cSAugustin Cavalier if (*v == 0)
64*f491972cSAugustin Cavalier break;
65*f491972cSAugustin Cavalier if (m == (1 << 31))
66*f491972cSAugustin Cavalier break; /* sorry, out of bits */
67*f491972cSAugustin Cavalier m <<= 1; /* next vendor equivalence class */
68*f491972cSAugustin Cavalier }
69*f491972cSAugustin Cavalier if (fnmatch(*vs == '!' ? vs + 1 : vs, vstr, FNM_CASEFOLD) == 0)
70*f491972cSAugustin Cavalier {
71*f491972cSAugustin Cavalier if (*vs != '!')
72*f491972cSAugustin Cavalier mask |= m;
73*f491972cSAugustin Cavalier while (v[1]) /* forward to next block */
74*f491972cSAugustin Cavalier v++;
75*f491972cSAugustin Cavalier }
76*f491972cSAugustin Cavalier }
77*f491972cSAugustin Cavalier queue_push(&pool->vendormap, vendor);
78*f491972cSAugustin Cavalier queue_push(&pool->vendormap, mask);
79*f491972cSAugustin Cavalier return mask;
80*f491972cSAugustin Cavalier }
81*f491972cSAugustin Cavalier
82*f491972cSAugustin Cavalier void
pool_setvendorclasses(Pool * pool,const char ** vendorclasses)83*f491972cSAugustin Cavalier pool_setvendorclasses(Pool *pool, const char **vendorclasses)
84*f491972cSAugustin Cavalier {
85*f491972cSAugustin Cavalier int i;
86*f491972cSAugustin Cavalier const char **v;
87*f491972cSAugustin Cavalier
88*f491972cSAugustin Cavalier if (pool->vendorclasses)
89*f491972cSAugustin Cavalier {
90*f491972cSAugustin Cavalier for (v = pool->vendorclasses; v[0] || v[1]; v++)
91*f491972cSAugustin Cavalier solv_free((void *)*v);
92*f491972cSAugustin Cavalier pool->vendorclasses = solv_free(pool->vendorclasses);
93*f491972cSAugustin Cavalier }
94*f491972cSAugustin Cavalier if (!vendorclasses || !vendorclasses[0])
95*f491972cSAugustin Cavalier return;
96*f491972cSAugustin Cavalier for (v = vendorclasses; v[0] || v[1]; v++)
97*f491972cSAugustin Cavalier ;
98*f491972cSAugustin Cavalier pool->vendorclasses = solv_calloc(v - vendorclasses + 2, sizeof(const char *));
99*f491972cSAugustin Cavalier for (v = vendorclasses, i = 0; v[0] || v[1]; v++, i++)
100*f491972cSAugustin Cavalier pool->vendorclasses[i] = *v ? solv_strdup(*v) : 0;
101*f491972cSAugustin Cavalier pool->vendorclasses[i++] = 0;
102*f491972cSAugustin Cavalier pool->vendorclasses[i] = 0;
103*f491972cSAugustin Cavalier queue_empty(&pool->vendormap);
104*f491972cSAugustin Cavalier }
105*f491972cSAugustin Cavalier
106*f491972cSAugustin Cavalier void
pool_addvendorclass(Pool * pool,const char ** vendorclass)107*f491972cSAugustin Cavalier pool_addvendorclass(Pool *pool, const char **vendorclass)
108*f491972cSAugustin Cavalier {
109*f491972cSAugustin Cavalier int i, j;
110*f491972cSAugustin Cavalier
111*f491972cSAugustin Cavalier if (!vendorclass || !vendorclass[0])
112*f491972cSAugustin Cavalier return;
113*f491972cSAugustin Cavalier for (j = 1; vendorclass[j]; j++)
114*f491972cSAugustin Cavalier ;
115*f491972cSAugustin Cavalier i = 0;
116*f491972cSAugustin Cavalier if (pool->vendorclasses)
117*f491972cSAugustin Cavalier {
118*f491972cSAugustin Cavalier for (i = 0; pool->vendorclasses[i] || pool->vendorclasses[i + 1]; i++)
119*f491972cSAugustin Cavalier ;
120*f491972cSAugustin Cavalier if (i)
121*f491972cSAugustin Cavalier i++;
122*f491972cSAugustin Cavalier }
123*f491972cSAugustin Cavalier pool->vendorclasses = solv_realloc2(pool->vendorclasses, i + j + 2, sizeof(const char *));
124*f491972cSAugustin Cavalier for (j = 0; vendorclass[j]; j++)
125*f491972cSAugustin Cavalier pool->vendorclasses[i++] = solv_strdup(vendorclass[j]);
126*f491972cSAugustin Cavalier pool->vendorclasses[i++] = 0;
127*f491972cSAugustin Cavalier pool->vendorclasses[i] = 0;
128*f491972cSAugustin Cavalier queue_empty(&pool->vendormap);
129*f491972cSAugustin Cavalier }
130