rpm  5.4.14
rpmruby.c
Go to the documentation of this file.
1 #include "system.h"
2 #include <argv.h>
3 
4 #if defined(WITH_RUBYEMBED)
5 
6 /* Make sure Ruby's fun stuff has its own xmalloc & Co functions available */
7 #undef xmalloc
8 #undef xcalloc
9 #undef xrealloc
10 
11 #undef PACKAGE_NAME
12 #undef PACKAGE_TARNAME
13 #undef PACKAGE_VERSION
14 #undef PACKAGE_STRING
15 #undef PACKAGE_BUGREPORT
16 
17 #pragma GCC diagnostic ignored "-Wstrict-prototypes"
18 #include <ruby.h>
19 #pragma GCC diagnostic warning "-Wstrict-prototypes"
20 
21 #endif
22 
23 #define _RPMRUBY_INTERNAL 1
24 #include "rpmruby.h"
25 
26 #include "debug.h"
27 
28 /*@unchecked@*/
30 
31 /*@unchecked@*/ /*@relnull@*/
33 
37 static void rpmrubyFini(void *_ruby)
38  /*@globals fileSystem @*/
39  /*@modifies *_ruby, fileSystem @*/
40 {
41  rpmruby ruby = (rpmruby) _ruby;
42 
43 #if defined(WITH_RUBYEMBED)
44  ruby_cleanup(0);
45 #endif
46  ruby->I = NULL;
47 }
48 
53 /*@unchecked@*/ /*@only@*/ /*@null@*/
55 
66 static rpmruby rpmrubyGetPool(/*@null@*/ rpmioPool pool)
67  /*@globals _rpmrubyPool, fileSystem @*/
68  /*@modifies pool, _rpmrubyPool, fileSystem @*/
69 {
70  rpmruby ruby;
71 
72  if (_rpmrubyPool == NULL) {
73  _rpmrubyPool = rpmioNewPool("ruby", sizeof(*ruby), -1, _rpmruby_debug,
75  pool = _rpmrubyPool;
76  }
77 
78  return (rpmruby) rpmioGetPool(pool, sizeof(*ruby));
79 }
80 
81 #if defined(WITH_RUBYEMBED)
82 
83 /*@unchecked@*/
84 static const char * rpmrubyInitStringIO = "\
85 require 'stringio'\n\
86 $stdout = StringIO.new($result, \"w+\")\n\
87 ";
88 #endif
89 
90 static rpmruby rpmrubyI(void)
91  /*@globals _rpmrubyI @*/
92  /*@modifies _rpmrubyI @*/
93 {
94  if (_rpmrubyI == NULL)
96  return _rpmrubyI;
97 }
98 
99 rpmruby rpmrubyNew(char **av, uint32_t flags)
100 {
101  static const char *_av[] = { "rpmruby", NULL };
102 
103  /* XXX FIXME: recurse like every other embedded interpreter. */
104  if (_rpmrubyI)
105  return _rpmrubyI;
106 
107  rpmruby ruby = rpmrubyGetPool(_rpmrubyPool);
108 
109  if (av == NULL)
110  av = (char **) _av;
111 
112 #if defined(WITH_RUBYEMBED)
113  RUBY_INIT_STACK;
114  ruby_init();
115  ruby_init_loadpath();
116 
117  ruby_script((char *)av[0]);
118  rb_gv_set("$result", rb_str_new2(""));
119  (void) rpmrubyRun(ruby, rpmrubyInitStringIO, NULL);
120 #endif
121 
122  return rpmrubyLink(ruby);
123 }
124 
125 
126 rpmRC rpmrubyRun(rpmruby ruby, const char *str, const char **resultp)
127 {
128  rpmRC rc = RPMRC_FAIL;
129 
130 if (_rpmruby_debug)
131 fprintf(stderr, "==> %s(%p,%s,%p)\n", __FUNCTION__, ruby, str, resultp);
132 
133  if (ruby == NULL)
134  ruby = rpmrubyI();
135 
136 #if defined(WITH_RUBYEMBED)
137  if (str) {
138  int state = -1;
139  ruby->state = rb_eval_string_protect(str, &state);
140 
141  /* Check whether the evaluation was successful or not */
142 
143  if (state == 0) {
144  rc = RPMRC_OK;
145  if (resultp)
146  *resultp = RSTRING_PTR(rb_gv_get("$result"));
147  }
148  }
149 #endif
150 
151  return rc;
152 }
153 
rpmioPool _rpmrubyPool
The pool of Ruby interpreter instances.
Definition: poptALL.c:554
int rc
Definition: poptALL.c:670
static rpmruby rpmrubyI(void)
Definition: rpmruby.c:90
static rpmruby rpmrubyGetPool(rpmioPool pool)
Returns the current rpmio pool responsible for Ruby interpreter instances.
Definition: rpmruby.c:66
enum rpmRC_e rpmRC
RPM return codes.
Definition: signature.c:616
rpmruby rpmrubyNew(char **av, uint32_t flags)
Creates and initializes a Ruby interpreter.
Definition: rpmruby.c:99
rpmioItem rpmioGetPool(rpmioPool pool, size_t size)
Get unused item from pool, or alloc a new item.
Definition: rpmmalloc.c:220
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
rpmruby rpmrubyLink(rpmruby ruby)
References a Ruby interpreter instance.
Embedded Ruby interpreter.
struct rpmruby_s * rpmruby
Definition: rpmruby.h:32
rpmioPool rpmioNewPool(const char *name, size_t size, int limit, int flags, char *(*dbg)(void *item), void(*init)(void *item), void(*fini)(void *item))
Create a memory pool.
Definition: rpmmalloc.c:109
static void rpmrubyFini(void *_ruby)
Finalizes a Ruby interpreter instance/pool item.
Definition: rpmruby.c:37
int flags
Definition: fnmatch.c:282
return NULL
Definition: poptALL.c:613
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
const char ** av
Definition: rpmts-py.c:788
_rpmrubyI
Current (global) interpreter instance.
Definition: poptALL.c:553