rpm  5.4.10
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 }