| 1 |
#include <EXTERN.h> |
| 2 |
#include <perl.h> |
| 3 |
#undef DIE /* use our own DIE */ |
| 4 |
|
| 5 |
#include "common.h" |
| 6 |
#include "configuration.h" |
| 7 |
#include "database.h" |
| 8 |
#include "perldb.h" |
| 9 |
#include "template.h" |
| 10 |
#include "question.h" |
| 11 |
#include "strutl.h" |
| 12 |
|
| 13 |
#include <dirent.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <string.h> |
| 16 |
#include <sys/types.h> |
| 17 |
|
| 18 |
#define DBDATA(db) ((struct perldb_data *)db->data) |
| 19 |
|
| 20 |
static PerlInterpreter *perlinterp = 0; |
| 21 |
|
| 22 |
static int perldb_initialize(struct database *db, struct configuration *cfg) |
| 23 |
{ |
| 24 |
char *embedded[] = { "", "-e", "0" }; |
| 25 |
struct perldb_data *data = NEW(struct perldb_data); |
| 26 |
|
| 27 |
memset(data, 0, sizeof(struct perldb_data)); |
| 28 |
db->data = data; |
| 29 |
|
| 30 |
perlinterp = perl_alloc(); |
| 31 |
perl_construct(perlinterp); |
| 32 |
perl_parse(perlinterp, NULL, 3, embedded, NULL); |
| 33 |
perl_run(perlinterp); |
| 34 |
|
| 35 |
perl_eval_pv("use Data::Dumper", TRUE); |
| 36 |
|
| 37 |
return DC_OK; |
| 38 |
} |
| 39 |
|
| 40 |
static int perldb_shutdown(struct database *db) |
| 41 |
{ |
| 42 |
perl_destruct(perlinterp); |
| 43 |
perl_free(perlinterp); |
| 44 |
return DC_OK; |
| 45 |
} |
| 46 |
|
| 47 |
static int perldb_load(struct database *db) |
| 48 |
{ |
| 49 |
int ret = DC_OK; |
| 50 |
const char *questiondb, *templatedb; |
| 51 |
char perlstr[1024]; |
| 52 |
SV **tmp, *junk; |
| 53 |
dSP; |
| 54 |
|
| 55 |
questiondb = db->config->get(db->config, |
| 56 |
"database::driver::perldb::questiondb", 0); |
| 57 |
templatedb = db->config->get(db->config, |
| 58 |
"database::driver::perldb::templatedb", 0); |
| 59 |
|
| 60 |
ENTER; |
| 61 |
SAVETMPS; |
| 62 |
PUSHMARK(SP); |
| 63 |
snprintf(perlstr, sizeof(perlstr), "require \"%s\"", questiondb); |
| 64 |
perl_call_pv(perlstr, G_EVAL|G_SCALAR); |
| 65 |
snprintf(perlstr, sizeof(perlstr), "require \"%s\"", templatedb); |
| 66 |
perl_call_pv(perlstr, G_EVAL|G_SCALAR); |
| 67 |
SPAGAIN; |
| 68 |
if (SvTRUE(ERRSV)) |
| 69 |
{ |
| 70 |
STRLEN n_a; |
| 71 |
fprintf(stderr, "%s\n", SvPV(ERRSV, n_a)); |
| 72 |
junk = POPs; |
| 73 |
ret = DC_NOTOK; |
| 74 |
} |
| 75 |
PUTBACK; |
| 76 |
FREETMPS; |
| 77 |
LEAVE; |
| 78 |
|
| 79 |
tmp = hv_fetch(PL_defstash, "questions", 9, 0); |
| 80 |
if (tmp != 0) |
| 81 |
DBDATA(db)->questions = *tmp; |
| 82 |
tmp = hv_fetch(PL_defstash, "templates", 9, 0); |
| 83 |
if (tmp != 0) |
| 84 |
DBDATA(db)->templates = *tmp; |
| 85 |
|
| 86 |
return ret; |
| 87 |
} |
| 88 |
|
| 89 |
static int perldb_save(struct database *db) |
| 90 |
{ |
| 91 |
const char *questiondb, *templatedb; |
| 92 |
int count; |
| 93 |
SV *dumper; |
| 94 |
dSP; |
| 95 |
|
| 96 |
questiondb = db->config->get(db->config, |
| 97 |
"database::driver::perldb::questiondb", 0); |
| 98 |
templatedb = db->config->get(db->config, |
| 99 |
"database::driver::perldb::templatedb", 0); |
| 100 |
|
| 101 |
ENTER; |
| 102 |
SAVETMPS; |
| 103 |
PUSHMARK(SP); |
| 104 |
|
| 105 |
count = perl_call_pv("Data::Dumper->new([\%questions], ['*questions']", G_SCALAR); |
| 106 |
SPAGAIN; |
| 107 |
if (count == 1) |
| 108 |
{ |
| 109 |
dumper = POPs; |
| 110 |
|
| 111 |
XPUSHs(dumper); |
| 112 |
PUTBACK; |
| 113 |
count = perl_call_method("Dump", G_SCALAR); |
| 114 |
} |
| 115 |
PUTBACK; |
| 116 |
|
| 117 |
FREETMPS; |
| 118 |
LEAVE; |
| 119 |
|
| 120 |
|
| 121 |
return DC_OK; |
| 122 |
} |
| 123 |
|
| 124 |
static int perldb_template_set(struct database *db, struct template *t) |
| 125 |
{ |
| 126 |
return DC_OK; |
| 127 |
} |
| 128 |
|
| 129 |
static struct template *perldb_template_get(struct database *db, |
| 130 |
const char *ltag) |
| 131 |
{ |
| 132 |
return NULL; |
| 133 |
} |
| 134 |
|
| 135 |
static int perldb_template_remove(struct database *db, const char *tag) |
| 136 |
{ |
| 137 |
return DC_OK; |
| 138 |
} |
| 139 |
|
| 140 |
static struct template *perldb_template_iterate(struct database *db, |
| 141 |
void **iter) |
| 142 |
{ |
| 143 |
return NULL; |
| 144 |
} |
| 145 |
|
| 146 |
static int perldb_question_set(struct database *db, struct question *q) |
| 147 |
{ |
| 148 |
return DC_OK; |
| 149 |
} |
| 150 |
|
| 151 |
static struct question *perldb_question_get(struct database *db, |
| 152 |
const char *ltag) |
| 153 |
{ |
| 154 |
return NULL; |
| 155 |
} |
| 156 |
|
| 157 |
static int perldb_question_disown(struct database *db, const char *tag, |
| 158 |
const char *owner) |
| 159 |
{ |
| 160 |
return DC_OK; |
| 161 |
} |
| 162 |
|
| 163 |
static struct question *perldb_question_iterate(struct database *db, |
| 164 |
void **iter) |
| 165 |
{ |
| 166 |
return NULL; |
| 167 |
} |
| 168 |
|
| 169 |
struct database_module debconf_database_module = |
| 170 |
{ |
| 171 |
initialize: perldb_initialize, |
| 172 |
shutdown: perldb_shutdown, |
| 173 |
load: perldb_load, |
| 174 |
save: perldb_save, |
| 175 |
|
| 176 |
template_set: perldb_template_set, |
| 177 |
template_get: perldb_template_get, |
| 178 |
template_remove: perldb_template_remove, |
| 179 |
template_iterate: perldb_template_iterate, |
| 180 |
|
| 181 |
question_get: perldb_question_get, |
| 182 |
question_set: perldb_question_set, |
| 183 |
question_disown: perldb_question_disown, |
| 184 |
question_iterate: perldb_question_iterate |
| 185 |
}; |