rpm  5.4.14
trb.c
Go to the documentation of this file.
1 #include "system.h"
2 
3 #include <rpmio.h>
4 #include <argv.h>
5 
6 #define _RPMRUBY_INTERNAL
7 #include <rpmruby.h>
8 
9 #include "rpm-rb.h"
10 
11 #include "rpmds-rb.h"
12 #include "rpmfi-rb.h"
13 #include "rpmhdr-rb.h"
14 #include "rpmmc-rb.h"
15 #include "rpmmi-rb.h"
16 #include "rpmps-rb.h"
17 #include "rpmte-rb.h"
18 #include "rpmts-rb.h"
19 
20 #ifdef NOTYET
21 #include "syck-rb.h"
22 #include "uuid-rb.h"
23 #endif
24 
25 #include <rpmcli.h>
26 
27 #include "debug.h"
28 
29 /*@unchecked@*/
30 static int _debug = 0;
31 
32 /*@unchecked@*/
33 static int _loglvl = 0;
34 
35 /*@unchecked@*/
36 static int _test = 1;
37 
38 typedef struct rpmrbClassTable_s {
39 /*@observer@*/
40  const char *name;
41  void (*init) (void);
42  int ix;
43 } * rpmrbClassTable;
44 
45 /*@unchecked@*/ /*@observer@*/
46 static struct rpmrbClassTable_s classTable[] = {
47  { "Ps", Init_rpmps, 9 },
48  { "Mc", Init_rpmmc, 8 },
49  { "Te", Init_rpmte, 7 },
50  { "Fi", Init_rpmfi, 6 },
51  { "Ds", Init_rpmds, 5 },
52  { "Hdr", Init_rpmhdr, 4 },
53  { "Mi", Init_rpmmi, 3 },
54  { "Ts", Init_rpmts, 2 },
55  { "Rpm", Init_rpm, 1 },
56 };
57 
58 /*@unchecked@*/
59 static size_t nclassTable = sizeof(classTable) / sizeof(classTable[0]);
60 
61 /*@unchecked@*/
62 static const char tscripts[] = "./tscripts/";
63 
64 /*@unchecked@*/
65 static const char * _acknack = "\
66 def ack(cmd, expected)\n\
67  begin\n\
68  actual = eval(cmd)\n\
69  rescue\n\
70  print(\"NACK: ack(\"+cmd.to_s+\")\tcaught '\"+\"#{$!}\"+\"'\n\")\n\
71  return\n\
72  end\n\
73  if (actual != expected && expected != nil)\n\
74  print(\"NACK: ack(\"+cmd.to_s+\")\tgot '\"+actual.to_s+\"' not '\"+expected.to_s+\"'\n\")\n\
75  elsif ($loglvl)\n\
76  print(\" ack(\"+cmd.to_s+\")\tgot '\"+actual.to_s+\"'\n\")\n\
77  end\n\
78 end\n\
79 ";
80 
81 static rpmRC
82 rpmrbLoadFile(rpmruby rb, const char * pre, const char * fn)
83 {
84  const char * result = NULL;
85  char * str;
86  rpmRC ret;
87 
88  if (pre == NULL)
89  pre = "";
90  str = rpmExpand(pre, "load(\"", fn, "\");", NULL);
91 if (_debug)
92 fprintf(stderr, "\trunning:%s%s\n", (*pre ? "\n" : " "), str);
93  result = NULL;
94  ret = rpmrubyRun(NULL, str, &result);
95  if (result != NULL && *result != '\0')
96  fprintf(stdout, "%s\n", result);
97  str = _free(str);
98  return ret;
99 }
100 
101 static void
103 {
104  const char * pre = NULL;
105  int * order = NULL;
106  size_t norder = 64;
107  rpmrbClassTable tbl;
108  rpmruby rb;
109  const char * result;
110  int ix;
111  size_t i;
112 
113  i = norder * sizeof(*order);
114  order = memset(alloca(i), 0, i);
115 
116  /* Inject _debug and _loglvl into the interpreter context. */
117  { char dstr[32];
118  char lstr[32];
119  sprintf(dstr, "%d", _debug);
120  sprintf(lstr, "%d", _loglvl);
121  pre = rpmExpand("$debug = ", dstr, ";\n"
122  "$loglvl = ", lstr, ";\n",
123  _acknack, NULL);
124  }
125 
126  /* Load requested classes and initialize the test order. */
127  /* XXX FIXME: resultp != NULL to actually execute?!? */
128  (void) rpmrubyRun(NULL, "puts \"loading RPM classes.\";", &result);
129  rb = _rpmrubyI;
130  for (i = 0, tbl = classTable; i < nclassTable; i++, tbl++) {
131  if (tbl->ix <= 0)
132  continue;
133  order[tbl->ix & (norder - 1)] = i + 1;
134  if (tbl->init != NULL)
135  (void) (*tbl->init) ();
136  }
137 
138  /* Test requested classes in order. */
139  for (i = 0; i < norder; i++) {
140  const char * fn;
141  struct stat sb;
142 
143  if (order[i] <= 0)
144  continue;
145  ix = order[i] - 1;
146  tbl = &classTable[ix];
147  fn = rpmGetPath(tscripts, "/", tbl->name, ".rb", NULL);
148  if (Stat(fn, &sb) == 0) {
149  (void) rpmrbLoadFile(NULL, pre, fn);
150  pre = _free(pre);
151  }
152  fn = _free(fn);
153  }
154 
155  pre = _free(pre);
156  return;
157 }
158 
159 static struct poptOption optionsTable[] = {
160  { "debug", 'd', POPT_ARG_VAL, &_debug, -1, NULL, NULL },
161  { "test", 't', POPT_ARG_VAL, &_test, -1, NULL, NULL },
162 
163  { NULL, '\0', POPT_ARG_INCLUDE_TABLE, rpmcliAllPoptTable, 0,
164  N_("Common options for all rpm executables:"), NULL },
165 
166  POPT_AUTOHELP
167  POPT_TABLEEND
168 };
169 
170 int
171 main(int argc, char *argv[])
172 {
173  poptContext optCon = rpmcliInit(argc, argv, optionsTable);
174  ARGV_t av = poptGetArgs(optCon);
175  int ac = argvCount(av);
176  const char * fn;
177  int rc = 1; /* assume failure */
178 
179  if (!_test && ac < 1) {
180  poptPrintUsage(optCon, stderr, 0);
181  goto exit;
182  }
183 
184 _rpmts_debug = 0;
185 
186 _rpmruby_debug = 0;
187  if (_debug && !_loglvl) _loglvl = 1;
189 _rpmruby_debug = 1;
190 
191  if (av != NULL)
192  while ((fn = *av++) != NULL) {
193  rpmRC ret = rpmrbLoadFile(NULL, NULL, fn);
194  if (ret != RPMRC_OK)
195  goto exit;
196  }
197 
198  rc = 0;
199 
200 exit:
201 _rpmruby_debug = 0;
202  optCon = rpmcliFini(optCon);
203 
204  return rc;
205 }
void Init_rpmhdr(void)
Definition: rpmhdr-rb.c:249
struct rpmrbClassTable_s * rpmrbClassTable
static int _loglvl
Definition: trb.c:33
char * rpmGetPath(const char *path,...)
Return (malloc&#39;ed) expanded, canonicalized, file path.
Definition: macro.c:3371
int rc
Definition: poptALL.c:670
int main(int argc, const char **argv, char **envp)
Definition: rpmqv.c:433
int Stat(const char *path, struct stat *st)
stat(2) clone.
Definition: rpmrpc.c:1361
static size_t nclassTable
Definition: trb.c:59
void Init_rpmmi(void)
Definition: rpmmi-rb.c:155
int ac
Definition: rpmgrep.c:1431
void Init_rpmte(void)
Definition: rpmte-rb.c:149
argv
Definition: rpmmtree.c:3679
pid_t result
Definition: rpmsq.c:737
sprintf(t," (%u)",(unsigned) dig->nbytes)
ret
Definition: macro.c:387
static int _debug
Definition: trb.c:30
char * alloca()
enum rpmRC_e rpmRC
RPM return codes.
Definition: signature.c:616
int _rpmts_debug
Definition: rpmts.c:93
goto exit
Definition: db3.c:1903
static const char tscripts[]
Definition: trb.c:62
memset(_r, 0, sizeof(*_r))
int ix
Definition: rpmps-py.c:174
fprintf(stderr,"--> %s(%p,%p,%p) sig %p sigp %p\n", __FUNCTION__, dig, t, rsactx, sig, sigp)
int _rpmruby_debug
Triggers printing of debugging information.
Definition: rpmruby.c:29
#define N_(Text)
Definition: system.h:531
int argvCount(const ARGV_t argv)
Return no.
Definition: argv.c:71
void Init_rpmts(void)
Definition: rpmts-rb.c:349
void Init_rpm(void)
Defines the &quot;RPM&quot; Ruby module and makes it known to the Interpreter.
Definition: rpm-rb.c:38
void Init_rpmds(void)
Definition: rpmds-rb.c:261
Ruby bindings to the RPM Transaction Set API.
static void rpmrbLoadClasses(void)
Definition: trb.c:102
static struct poptOption optionsTable[]
Definition: trb.c:159
static rpmRC rpmrbLoadFile(rpmruby rb, const char *pre, const char *fn)
Definition: trb.c:82
char * rpmExpand(const char *arg,...)
Return (malloc&#39;ed) concatenated macro expansion(s).
Definition: macro.c:3178
static int _test
Definition: trb.c:36
Embedded Ruby interpreter.
void Init_rpmmc(void)
Definition: rpmmc-rb.c:348
void Init_rpmfi(void)
Definition: rpmfi-rb.c:370
struct rpmruby_s * rpmruby
Definition: rpmruby.h:32
RPM Ruby bindings &quot;RPM&quot; module.
optCon
Definition: poptALL.c:604
struct poptOption rpmcliAllPoptTable[]
Popt option table for options shared by all modes and executables.
Definition: poptALL.c:417
static struct rpmrbClassTable_s classTable[]
Definition: trb.c:46
const char * name
Definition: trb.c:40
return NULL
Definition: poptALL.c:613
void Init_rpmps(void)
Definition: rpmps-rb.c:178
static void
Print copy of spec file, filling in Group/Description/Summary from specspo.
Definition: spec.c:737
rpmRC rpmrubyRun(rpmruby ruby, const char *str, const char **resultp)
Evaluates Ruby code stored in a string.
Definition: rpmruby.c:126
void(* init)(void)
Definition: trb.c:41
Ruby bindings to RPM&#39;s macro context facility.
ARGstr_t * ARGV_t
Definition: argv.h:12
poptContext
Definition: poptALL.c:525
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
const char ** av
Definition: rpmts-py.c:788
_rpmrubyI
Current (global) interpreter instance.
Definition: poptALL.c:553
size_t fn
Definition: macro.c:1698
static const char * _acknack
Definition: trb.c:65