rpm  4.5
hdrNVR.c
Go to the documentation of this file.
1 
5 #include "system.h"
6 #include <rpmlib.h>
7 #include <rpmmacro.h>
8 #include "debug.h"
9 
14 /*@observer@*/ /*@unchecked@*/
15 static struct tagMacro {
16 /*@observer@*/ /*@null@*/
17  const char *macroname;
19 } tagMacros[] = {
20  { "name", RPMTAG_NAME },
21  { "version", RPMTAG_VERSION },
22  { "release", RPMTAG_RELEASE },
23  { "epoch", RPMTAG_EPOCH },
24  { "arch", RPMTAG_ARCH },
25  { "os", RPMTAG_OS },
26  { NULL, 0 }
27 };
28 
30 {
31  struct tagMacro * tagm;
32  union {
33  const void * ptr;
34 /*@unused@*/
35  const char ** argv;
36  const char * str;
37  int_32 * i32p;
38  } body;
39  char numbuf[32];
40  int_32 type;
41  int xx;
42 
43  /* XXX pre-expand %{buildroot} (if any) */
44  { const char *s = rpmExpand("%{?buildroot}", NULL);
45  if (s && *s)
46  (void) addMacro(NULL, "..buildroot", NULL, s, -1);
47  s = _free(s);
48  }
49  { const char *s = rpmExpand("%{?_builddir}", NULL);
50  if (s && *s)
51  (void) addMacro(NULL, ".._builddir", NULL, s, -1);
52  s = _free(s);
53  }
54 
55  for (tagm = tagMacros; tagm->macroname != NULL; tagm++) {
56  xx = headerGetEntryMinMemory(h, tagm->tag, &type, (hPTR_t *) &body.ptr, NULL);
57  if (!xx)
58  continue;
59  switch (type) {
60  case RPM_INT32_TYPE:
61 /*@-boundsread@*/
62  sprintf(numbuf, "%d", *body.i32p);
63 /*@=boundsread@*/
64  addMacro(NULL, tagm->macroname, NULL, numbuf, -1);
65  /*@switchbreak@*/ break;
66  case RPM_STRING_TYPE:
67  addMacro(NULL, tagm->macroname, NULL, body.str, -1);
68  /*@switchbreak@*/ break;
71  case RPM_BIN_TYPE:
72  body.ptr = headerFreeData(body.ptr, type);
73  /*@fallthrough@*/
74  case RPM_NULL_TYPE:
75  case RPM_CHAR_TYPE:
76  case RPM_INT8_TYPE:
77  case RPM_INT16_TYPE:
78  default:
79  /*@switchbreak@*/ break;
80  }
81  }
82  return 0;
83 }
85 {
86  struct tagMacro * tagm;
87  union {
88  const void * ptr;
89 /*@unused@*/
90  const char ** argv;
91  const char * str;
92  int_32 * i32p;
93  } body;
94  int_32 type;
95  int xx;
96 
97  for (tagm = tagMacros; tagm->macroname != NULL; tagm++) {
98  xx = headerGetEntryMinMemory(h, tagm->tag, &type, (hPTR_t *) &body.ptr, NULL);
99  if (!xx)
100  continue;
101  switch (type) {
102  case RPM_INT32_TYPE:
103  delMacro(NULL, tagm->macroname);
104  /*@switchbreak@*/ break;
105  case RPM_STRING_TYPE:
106  delMacro(NULL, tagm->macroname);
107  /*@switchbreak@*/ break;
109  case RPM_I18NSTRING_TYPE:
110  case RPM_BIN_TYPE:
111  body.ptr = headerFreeData(body.ptr, type);
112  /*@fallthrough@*/
113  case RPM_NULL_TYPE:
114  case RPM_CHAR_TYPE:
115  case RPM_INT8_TYPE:
116  case RPM_INT16_TYPE:
117  default:
118  /*@switchbreak@*/ break;
119  }
120  }
121 
122  /* XXX restore previous %{buildroot} (if any) */
123  { const char *s = rpmExpand("%{?_builddir}", NULL);
124  if (s && *s)
125  (void) delMacro(NULL, "_builddir");
126  s = _free(s);
127  }
128  { const char *s = rpmExpand("%{?buildroot}", NULL);
129  if (s && *s)
130  (void) delMacro(NULL, "buildroot");
131  s = _free(s);
132  }
133 
134  return 0;
135 }
136 
137 int headerNVR(Header h, const char **np, const char **vp, const char **rp)
138 {
139  int type;
140  int count;
141 
142 /*@-boundswrite@*/
143  if (np) {
144  if (!(headerGetEntry(h, RPMTAG_NAME, &type, (void **) np, &count)
145  && type == RPM_STRING_TYPE && count == 1))
146  *np = NULL;
147  }
148  if (vp) {
149  if (!(headerGetEntry(h, RPMTAG_VERSION, &type, (void **) vp, &count)
150  && type == RPM_STRING_TYPE && count == 1))
151  *vp = NULL;
152  }
153  if (rp) {
154  if (!(headerGetEntry(h, RPMTAG_RELEASE, &type, (void **) rp, &count)
155  && type == RPM_STRING_TYPE && count == 1))
156  *rp = NULL;
157  }
158 /*@=boundswrite@*/
159  return 0;
160 }
161 
162 int headerNEVRA(Header h, const char **np,
163  /*@unused@*/ const char **ep, const char **vp, const char **rp,
164  const char **ap)
165 {
166  int type;
167  int count;
168 
169 /*@-boundswrite@*/
170  if (np) {
171  if (!(headerGetEntry(h, RPMTAG_NAME, &type, (void **) np, &count)
172  && type == RPM_STRING_TYPE && count == 1))
173  *np = NULL;
174  }
175  if (vp) {
176  if (!(headerGetEntry(h, RPMTAG_VERSION, &type, (void **) vp, &count)
177  && type == RPM_STRING_TYPE && count == 1))
178  *vp = NULL;
179  }
180  if (rp) {
181  if (!(headerGetEntry(h, RPMTAG_RELEASE, &type, (void **) rp, &count)
182  && type == RPM_STRING_TYPE && count == 1))
183  *rp = NULL;
184  }
185  if (ap) {
186  if (!(headerGetEntry(h, RPMTAG_ARCH, &type, (void **) ap, &count)
187  && type == RPM_STRING_TYPE && count == 1))
188  *ap = NULL;
189  }
190 /*@=boundswrite@*/
191  return 0;
192 }
193 
194 char * hGetNEVR(Header h, const char ** np)
195 {
196  const char * n, * v, * r;
197  char * NVR, * t;
198 
199  (void) headerNVR(h, &n, &v, &r);
200  NVR = t = xcalloc(1, strlen(n) + strlen(v) + strlen(r) + sizeof("--"));
201 /*@-boundswrite@*/
202  t = stpcpy(t, n);
203  t = stpcpy(t, "-");
204  t = stpcpy(t, v);
205  t = stpcpy(t, "-");
206  t = stpcpy(t, r);
207  if (np)
208  *np = n;
209 /*@=boundswrite@*/
210  return NVR;
211 }
212 
213 char * hGetNEVRA(Header h, const char ** np)
214 {
215  const char * n, * v, * r, * a;
216  char * NVRA, * t;
217 
218  (void) headerNVR(h, &n, &v, &r);
219  /* XXX pubkeys have no arch. */
220 /*@-branchstate@*/
221  a = NULL;
222  if (!headerGetEntry(h, RPMTAG_ARCH, NULL, &a, NULL) || a == NULL)
223  a = "pubkey";
224 /*@=branchstate@*/
225  NVRA = t = xcalloc(1, strlen(n) + strlen(v) + strlen(r) + strlen(a) + sizeof("--."));
226 /*@-boundswrite@*/
227  t = stpcpy(t, n);
228  t = stpcpy(t, "-");
229  t = stpcpy(t, v);
230  t = stpcpy(t, "-");
231  t = stpcpy(t, r);
232  t = stpcpy(t, ".");
233  t = stpcpy(t, a);
234  if (np)
235  *np = n;
236 /*@=boundswrite@*/
237  return NVRA;
238 }
239 
241 {
243  uint_32 hcolor = 0;
244  uint_32 * fcolors;
245  int_32 ncolors;
246  int i;
247 
248  fcolors = NULL;
249  ncolors = 0;
250  if (hge(h, RPMTAG_FILECOLORS, NULL, &fcolors, &ncolors)
251  && fcolors != NULL && ncolors > 0)
252  {
253 /*@-boundsread@*/
254  for (i = 0; i < ncolors; i++)
255  hcolor |= fcolors[i];
256 /*@=boundsread@*/
257  }
258  hcolor &= 0x0f;
259 
260  return hcolor;
261 }