rpm  5.4.14
rpmhdr-rb.c
Go to the documentation of this file.
1 
5 #include "system.h"
6 
7 #include "rpm-rb.h"
8 #include "rpmds-rb.h"
9 #include "rpmfi-rb.h"
10 #include "rpmhdr-rb.h"
11 
12 #ifdef NOTYET
13 #include <argv.h>
14 #include <mire.h>
15 #endif
16 
17 #define _RPMDS_INTERNAL
18 #include <rpmtag.h>
19 #include <rpmtypes.h>
20 #include <rpmds.h>
21 #include <rpmfi.h>
22 #include <rpmio.h>
23 
24 #include <rpmcli.h> /* XXX rpmHeaderFormats */
25 
26 #include "../debug.h"
27 
29 
30 /*@unchecked@*/
31 static int _debug = 0;
32 
33 /* --- helpers */
34 static void *
35 rpmhdr_ptr(VALUE s)
36 {
37  void *ptr;
38  Data_Get_Struct(s, void, ptr);
39  return ptr;
40 }
41 
42 static VALUE
43 rpmhdrLoadTag(Header h, const char * name)
44 {
45  HE_t he = memset(alloca(sizeof(*he)), 0, sizeof(*he));
46  VALUE v = Qfalse;
47  int i;
48 
49 if (_debug)
50 fprintf(stderr, "==> %s(%p,%s)\n", __FUNCTION__, h, name);
51 
52  he->tag = tagValue(name);
53  if (headerGet(h, he, 0)) {
54 if (_debug < 0)
55 fprintf(stderr, "\t%s(%u) %u %p[%u]\n", name, (unsigned)he->tag, (unsigned)he->t, he->p.ptr, (unsigned)he->c);
56  switch (he->t) {
57  default:
58  goto exit;
59  /*@notreached@*/ break;
60  case RPM_BIN_TYPE: /* XXX return as array of octets for now. */
61  case RPM_UINT8_TYPE:
62  v = rb_ary_new();
63  for (i = 0; i < (int)he->c; i++)
64  rb_ary_push(v, INT2FIX(he->p.ui8p[i]));
65  break;
66  case RPM_UINT16_TYPE:
67  v = rb_ary_new();
68  for (i = 0; i < (int)he->c; i++)
69  rb_ary_push(v, INT2FIX(he->p.ui16p[i]));
70  break;
71  case RPM_UINT32_TYPE:
72  v = rb_ary_new();
73  for (i = 0; i < (int)he->c; i++)
74  rb_ary_push(v, INT2FIX(he->p.ui32p[i]));
75  break;
76  case RPM_UINT64_TYPE:
77  v = rb_ary_new();
78  for (i = 0; i < (int)he->c; i++)
79  rb_ary_push(v, INT2FIX(he->p.ui64p[i]));
80  break;
82  v = rb_ary_new();
83  for (i = 0; i < (int)he->c; i++)
84  rb_ary_push(v, rb_str_new2(he->p.argv[i]));
85  break;
86  case RPM_I18NSTRING_TYPE: /* XXX FIXME: is this ever seen? */
87  case RPM_STRING_TYPE:
88  v = rb_str_new2(he->p.str);
89  break;
90  }
91  }
92 
93 exit:
94  he->p.ptr = _free(he->p.ptr);
95  return v;
96 }
97 /* --- Object methods */
98 static VALUE
99 rpmhdr_sprintf(VALUE s, VALUE v)
100 {
101  Header h = rpmhdr_ptr(s);
102  const char *qfmt = StringValueCStr(v);
103  const char *q;
104  const char *errstr = NULL;
105 
106 if (_debug)
107 fprintf(stderr, "==> %s(0x%lx, 0x%lx) h %p\n", __FUNCTION__, s, v, h);
108 
109  if ((q = headerSprintf(h, qfmt, NULL, rpmHeaderFormats, &errstr)) == NULL)
110  q = errstr;
111 
112  return rb_str_new2(q);
113 }
114 
115 static VALUE
117 {
118  Header h = rpmhdr_ptr(s);
119 
120 if (_debug)
121 fprintf(stderr, "==> %s(0x%lx) h %p\n", __FUNCTION__, s, h);
122 
123  return rb_str_new2(headerGetOrigin(h));
124 }
125 
126 static VALUE
127 rpmhdr_setorigin(VALUE s, VALUE v)
128 {
129  Header h = rpmhdr_ptr(s);
130 
131 if (_debug)
132 fprintf(stderr, "==> %s(0x%lx, 0x%lx) h %p\n", __FUNCTION__, s, v, h);
133 
134  (void) headerSetOrigin(h, StringValueCStr(v));
135  return rb_str_new2(headerGetOrigin(h));
136 }
137 
138 static VALUE
139 rpmhdr_ds(int argc, VALUE *argv, VALUE s)
140 {
141  VALUE v_tag;
142  Header h = rpmhdr_ptr(s);
144  int flags = 0;
145 
146  rb_scan_args(argc, argv, "01", &v_tag);
147 
148  if (!NIL_P(v_tag))
149  tag = FIX2INT(v_tag);
150 
151 if (_debug)
152 fprintf(stderr, "==> %s(0x%lx) h %p\n", __FUNCTION__, s, h);
153 
154  return rpmrb_NewDs( rpmdsNew(h, tag, flags) );
155 }
156 
157 static VALUE
158 rpmhdr_fi(int argc, VALUE *argv, VALUE s)
159 {
160  VALUE v_tag;
161  Header h = rpmhdr_ptr(s);
163  int flags = 0;
164 
165  rb_scan_args(argc, argv, "01", &v_tag);
166 
167  if (!NIL_P(v_tag))
168  tag = FIX2INT(v_tag);
169 
170 if (_debug)
171 fprintf(stderr, "==> %s(0x%lx) h %p\n", __FUNCTION__, s, h);
172 
173  return rpmrb_NewFi( rpmfiNew(NULL, h, tag, flags) );
174 }
175 
176 static void
177 initMethods(VALUE klass)
178 {
179  rb_define_method(klass, "sprintf", rpmhdr_sprintf, 1);
180  rb_define_method(klass, "getorigin", rpmhdr_getorigin, 0);
181  rb_define_method(klass, "setorigin", rpmhdr_setorigin, 1);
182  rb_define_method(klass, "ds", rpmhdr_ds, -1);
183  rb_define_method(klass, "fi", rpmhdr_fi, -1);
184 }
185 
186 /* --- Object properties */
187 static VALUE
189 {
190 if (_debug)
191 fprintf(stderr, "==> %s(0x%lx)\n", __FUNCTION__, s);
192  return INT2FIX(_debug);
193 }
194 
195 static VALUE
196 rpmhdr_debug_set(VALUE s, VALUE v)
197 {
198 if (_debug)
199 fprintf(stderr, "==> %s(0x%lx, 0x%lx)\n", __FUNCTION__, s, v);
200  return INT2FIX(_debug = FIX2INT(v));
201 }
202 
203 static VALUE
204 rpmhdr__get(VALUE s, VALUE v)
205 {
206  Header h = rpmhdr_ptr(s);
207  char * vstr = StringValueCStr(v);
208 
209 if (_debug)
210 fprintf(stderr, "==> %s(0x%lx) %s\n", __FUNCTION__, s, vstr);
211 
212  return rpmhdrLoadTag(h, vstr);
213 }
214 
215 static void
216 initProperties(VALUE klass)
217 {
218  rb_define_method(klass, "debug", rpmhdr_debug_get, 0);
219  rb_define_method(klass, "debug=", rpmhdr_debug_set, 1);
220  rb_define_method(klass, "[]", rpmhdr__get, 1);
221 }
222 
223 /* --- Object ctors/dtors */
224 static void
226 {
227 if (_debug)
228 fprintf(stderr, "==> %s(%p)\n", __FUNCTION__, h);
229 
230  h = headerFree(h);
231 }
232 
233 static VALUE
234 rpmhdr_new(int argc, VALUE *argv, VALUE s)
235 {
236  Header h;
237 
238  rb_scan_args(argc, argv, "00");
239 
240  h = headerNew();
241 
242 if (_debug)
243 fprintf(stderr, "==> %s(%p[%d], 0x%lx) mi %p\n", __FUNCTION__, argv, argc, s, h);
244  return Data_Wrap_Struct(s, 0, rpmhdr_free, h);
245 }
246 
247 /* --- Class initialization */
248 void
250 {
251  rpmhdrClass = rb_define_class("Hdr", rb_cObject);
252 if (_debug)
253 fprintf(stderr, "==> %s() rpmhdrClass 0x%lx\n", __FUNCTION__, rpmhdrClass);
254 #ifdef NOTYET
255  rb_include_module(rpmhdrClass, rb_mEnumerable);
256 #endif
257  rb_define_singleton_method(rpmhdrClass, "new", rpmhdr_new, -1);
260 }
261 
262 VALUE
263 rpmrb_NewHdr(void *_h)
264 {
265  return Data_Wrap_Struct(rpmhdrClass, 0, rpmhdr_free, _h);
266 }
static VALUE rpmhdr_getorigin(VALUE s)
Definition: rpmhdr-rb.c:116
rpmTagType t
Definition: rpmtag.h:502
const char * str
Definition: rpmtag.h:73
rpmTag tag
Definition: rpmtag.h:501
void Init_rpmhdr(void)
Definition: rpmhdr-rb.c:249
const char ** argv
Definition: rpmtag.h:75
Header headerNew(void)
Create new (empty) header instance.
Definition: header.c:180
#define headerFree(_h)
Definition: rpmtag.h:870
q
Definition: macro.c:451
VALUE rpmrb_NewDs(void *_ds)
Definition: rpmds-rb.c:275
rpmuint32_t * ui32p
Definition: rpmtag.h:70
VALUE rpmrb_NewFi(void *_fi)
Definition: rpmfi-rb.c:384
Structure(s) used for file info tag sets.
VALUE rpmrb_NewHdr(void *_h)
Definition: rpmhdr-rb.c:263
The Header data structure.
headerSprintfExtension rpmHeaderFormats
Table of query format extensions.
Definition: formats.c:305
rpmuint16_t * ui16p
Definition: rpmtag.h:69
static VALUE rpmhdr_setorigin(VALUE s, VALUE v)
Definition: rpmhdr-rb.c:127
enum rpmTag_e rpmTag
Definition: rpmtag.h:468
static VALUE rpmhdr_sprintf(VALUE s, VALUE v)
Definition: rpmhdr-rb.c:99
rpmfi rpmfiNew(const void *_ts, Header h, rpmTag tagN, int flags)
Create and load a file info set.
Definition: rpmfi.c:1403
rpmds rpmdsNew(Header h, rpmTag tagN, int flags)
Create and load a dependency set.
Definition: rpmds.c:238
Header h
Definition: spec.c:739
argv
Definition: rpmmtree.c:3679
rpmTag tagValue(const char *tagstr)
Return tag value from name.
Definition: tagname.c:446
static VALUE rpmhdr_debug_get(VALUE s)
Definition: rpmhdr-rb.c:188
exit Fhe p ptr
Definition: db3.c:2119
char * alloca()
goto exit
Definition: db3.c:1903
memset(_r, 0, sizeof(*_r))
int headerSetOrigin(Header h, const char *origin)
Store header origin (e.g path or URL).
Definition: header.c:1189
void * ptr
Definition: rpmtag.h:67
char * headerSprintf(Header h, const char *fmt, headerTagTableEntry tags, headerSprintfExtension exts, errmsg_t *errmsg)
Return formatted output string from header tags.
Definition: hdrfmt.c:6748
static VALUE rpmhdrLoadTag(Header h, const char *name)
Definition: rpmhdr-rb.c:43
fprintf(stderr,"--> %s(%p,%p,%p) sig %p sigp %p\n", __FUNCTION__, dig, t, rsactx, sig, sigp)
RPM pattern matching.
he tag
Definition: db3.c:1927
rpmTagData p
Definition: rpmtag.h:504
Structure(s) used for dependency tag sets.
int headerGet(Header h, HE_t he, unsigned int flags)
Retrieve extension or tag value from a header.
Definition: header.c:2230
rpmTagCount c
Definition: rpmtag.h:505
static void rpmhdr_free(Header h)
Definition: rpmhdr-rb.c:225
rpmuint8_t * ui8p
Definition: rpmtag.h:68
VALUE rpmhdrClass
Definition: rpmhdr-rb.c:28
RPM Ruby bindings &quot;RPM&quot; module.
static void initMethods(VALUE klass)
Definition: rpmhdr-rb.c:177
static VALUE rpmhdr_new(int argc, VALUE *argv, VALUE s)
Definition: rpmhdr-rb.c:234
static VALUE rpmhdr__get(VALUE s, VALUE v)
Definition: rpmhdr-rb.c:204
Definition: rpmtag.h:500
const char * s
Definition: poptALL.c:734
static VALUE rpmhdr_ds(int argc, VALUE *argv, VALUE s)
Definition: rpmhdr-rb.c:139
static VALUE rpmhdr_fi(int argc, VALUE *argv, VALUE s)
Definition: rpmhdr-rb.c:158
int flags
Definition: fnmatch.c:282
static void * rpmhdr_ptr(VALUE s)
Definition: rpmhdr-rb.c:35
return NULL
Definition: poptALL.c:613
static void
Print copy of spec file, filling in Group/Description/Summary from specspo.
Definition: spec.c:737
static const char * name
static VALUE rpmhdr_debug_set(VALUE s, VALUE v)
Definition: rpmhdr-rb.c:196
int
Save source and expand field into target.
Definition: rpmds.c:2709
static void initProperties(VALUE klass)
Definition: rpmhdr-rb.c:216
const char * headerGetOrigin(Header h)
Return header origin (e.g path or URL).
Definition: header.c:1184
static int _debug
Definition: rpmhdr-rb.c:31
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
Definition: rpmiotypes.h:647
int i
Definition: spec.c:743
rpmuint64_t * ui64p
Definition: rpmtag.h:71