rpm  5.4.4
rpmio/rpmruby.c
Go to the documentation of this file.
00001 #include "system.h"
00002 #include <argv.h>
00003 
00004 #ifdef  HAVE_CONFIG_H
00005 #include "config.h"
00006 #endif
00007 
00008 
00009 #if defined(WITH_RUBYEMBED)
00010 
00011 /* Make sure Ruby's fun stuff has its own xmalloc & Co functions available */
00012 
00013 #undef xmalloc
00014 #undef xcalloc
00015 #undef xrealloc
00016 
00017 #include <ruby.h>
00018 
00019 #endif
00020 
00021 
00025 #define _RPMRUBY_INTERNAL 1
00026 #include "rpmruby.h"
00027 
00028 #include "debug.h"
00029 
00030 /*@unchecked@*/
00031 int _rpmruby_debug = 0;
00032 
00033 /*@unchecked@*/ /*@relnull@*/
00034 rpmruby _rpmrubyI = NULL;
00035 
00036 
00037 
00041 static void rpmrubyFini(void *_ruby)
00042         /*@globals fileSystem @*/
00043         /*@modifies *_ruby, fileSystem @*/
00044 {
00045     rpmruby ruby = _ruby;
00046 
00047 #   if defined(WITH_RUBYEMBED)
00048         ruby_cleanup(0); 
00049 #   endif
00050     ruby->I = NULL;
00051 }
00052 
00053 
00058 /*@unchecked@*/ /*@only@*/ /*@null@*/
00059 rpmioPool _rpmrubyPool;
00060 
00061 
00072 static rpmruby rpmrubyGetPool(/*@null@*/ rpmioPool pool)
00073         /*@globals _rpmrubyPool, fileSystem @*/
00074         /*@modifies pool, _rpmrubyPool, fileSystem @*/
00075 {
00076     rpmruby ruby;
00077 
00078     if (_rpmrubyPool == NULL) {
00079         _rpmrubyPool = rpmioNewPool("ruby", sizeof(*ruby), -1, _rpmruby_debug,
00080             NULL, NULL, rpmrubyFini);
00081         pool = _rpmrubyPool;
00082     } 
00083 
00084     return (rpmruby) rpmioGetPool(pool, sizeof(*ruby));
00085 }
00086 
00087 
00088 /*@unchecked@*/
00089 #if defined(WITH_RUBYEMBED)
00090 
00092 static const char * rpmrubyInitStringIO = "\
00093 require 'stringio'\n\
00094 $stdout = StringIO.new($result, \"w+\")\n\
00095 ";
00096 
00097 #endif
00098 
00099 
00100 static rpmruby rpmrubyI()
00101         /*@globals _rpmrubyI @*/
00102         /*@modifies _rpmrubyI @*/
00103 {
00104     if (NULL == _rpmrubyI) {
00105         _rpmrubyI = rpmrubyNew(NULL, 0);
00106     }
00107     return _rpmrubyI;
00108 }
00109 
00110 
00111 rpmruby rpmrubyNew(char **av, uint32_t flags)
00112 {
00113     /* TODO: Once Ruby allows several interpreter instances, distinguish
00114      * between the global interpreter (1<<31 flag) and a new one.
00115      */
00116     
00117     if (NULL != _rpmrubyI) {
00118         return _rpmrubyI;
00119     }
00120 
00121     rpmruby ruby = rpmrubyGetPool(_rpmrubyPool);
00122 
00123     static char *_av[] = { "rpmruby", NULL };
00124     if (NULL == av) {
00125         av = _av;
00126     }
00127 
00128 
00129     /* Set up embedded Ruby interpreter if embedded Ruby is enabled. */
00130 
00131 #   if defined(WITH_RUBYEMBED)
00132         RUBY_INIT_STACK;
00133         ruby_init();
00134         ruby_init_loadpath();
00135 
00136         ruby_script((char *)av[0]);
00137         rb_gv_set("$result", rb_str_new2(""));
00138         (void) rpmrubyRun(ruby, rpmrubyInitStringIO, NULL);
00139 #   endif
00140 
00141     return rpmrubyLink(ruby);
00142 }
00143 
00144 
00145 rpmRC rpmrubyRun(rpmruby ruby, const char *str, const char **resultp)
00146 {
00147     rpmRC rc = RPMRC_FAIL;
00148 
00149     if (_rpmruby_debug) {
00150         fprintf(stderr, "==> %s(%p,%s,%p)\n",
00151             __FUNCTION__, ruby, str, resultp);
00152     }
00153 
00154     if (NULL == ruby) {
00155         ruby = rpmrubyI();
00156     }
00157 
00158     if (NULL != str) {
00159 #       if defined(WITH_RUBYEMBED)
00160             int state = -1;
00161             ruby->state = rb_eval_string_protect(str, &state);
00162 
00163             /* Check whether the evaluation was successful or not */
00164 
00165             if(0 == state) {
00166                 rc = RPMRC_OK;
00167                 if (resultp != NULL) {
00168                     *resultp = RSTRING_PTR(rb_gv_get("$result"));
00169                 }
00170             }
00171 #       endif
00172     }
00173 
00174     return rc;
00175 }
00176