added modopen()/modclose()/modsym() to Awk/StdAwk.

migrated /lib/awk/mpi.c to mod/awk/mpi.c partially
This commit is contained in:
2012-10-31 13:51:32 +00:00
parent 65c324d373
commit acb40dea35
14 changed files with 507 additions and 245 deletions

View File

@ -1093,6 +1093,9 @@ int Awk::open ()
prm.math.log10 = log10;
prm.math.exp = exp;
prm.math.sqrt = sqrt;
prm.modopen = modopen;
prm.modclose = modclose;
prm.modsym = modsym;
awk = qse_awk_open (this->getMmgr(), QSE_SIZEOF(xtn_t), &prm);
if (awk == QSE_NULL)
@ -2009,6 +2012,23 @@ Awk::flt_t Awk::sqrt (awk_t* awk, flt_t x)
return xtn->awk->sqrt (x);
}
void* Awk::modopen (awk_t* awk, const char_t* dir, const char_t* name)
{
xtn_t* xtn = (xtn_t*) QSE_XTN (awk);
return xtn->awk->modopen (dir, name);
}
void Awk::modclose (awk_t* awk, void* handle)
{
xtn_t* xtn = (xtn_t*) QSE_XTN (awk);
xtn->awk->modclose (handle);
}
void* Awk::modsym (awk_t* awk, void* handle, const char_t* name)
{
xtn_t* xtn = (xtn_t*) QSE_XTN (awk);
return xtn->awk->modsym (handle, name);
}
/////////////////////////////////
QSE_END_NAMESPACE(QSE)
/////////////////////////////////

View File

@ -44,6 +44,7 @@
/* anything ? */
#else
# include <unistd.h>
# include <ltdl.h>
#endif
#ifndef QSE_HAVE_CONFIG_H
@ -1381,6 +1382,106 @@ StdAwk::flt_t StdAwk::sqrt (flt_t x)
#endif
}
void* StdAwk::modopen (const qse_char_t* dir, const qse_char_t* name)
{
#if defined(_WIN32)
/*TODO: implemente this - use LoadLibrary... */
this->setError (QSE_AWK_ENOIMPL);
return -1;
#elif defined(__OS2__)
/*TODO: implemente this */
this->setError (QSE_AWK_ENOIMPL);
return -1;
#elif defined(__DOS__)
/*TODO: implemente this */
this->setError (QSE_AWK_ENOIMPL);
return -1;
#else
void* h;
qse_mchar_t* modpath;
const qse_char_t* tmp[5];
int count = 0;
if (dir && dir[0] != QSE_T('\0'))
{
tmp[count++] = dir;
tmp[count++] = QSE_T("/");
}
tmp[count++] = QSE_T("libawk");
tmp[count++] = name;
tmp[count] = QSE_NULL;
#if defined(QSE_CHAR_IS_MCHAR)
modpath = qse_mbsadup (tmp, QSE_NULL, this->getMmgr());
#else
modpath = qse_wcsatombsdup (tmp, QSE_NULL, this->getMmgr());
#endif
if (!modpath)
{
this->setError (QSE_AWK_ENOMEM);
return QSE_NULL;
}
h = lt_dlopenext (modpath);
QSE_MMGR_FREE (awk->mmgr, modpath);
return h;
#endif
}
void StdAwk::modclose (void* handle)
{
#if defined(_WIN32)
/*TODO: implemente this */
#elif defined(__OS2__)
/*TODO: implemente this */
#elif defined(__DOS__)
/*TODO: implemente this */
#else
lt_dlclose ((lt_dlhandle)handle);
#endif
}
void* StdAwk::modsym (void* handle, const qse_char_t* name)
{
#if defined(_WIN32)
/*TODO: implemente this */
#elif defined(__OS2__)
/*TODO: implemente this */
#elif defined(__DOS__)
/*TODO: implemente this */
#else
void* s;
qse_mchar_t* mname;
#if defined(QSE_CHAR_IS_MCHAR)
mname = name;
#else
mname = qse_wcstombsdup (name, QSE_NULL, this->getMmgr());
if (!mname)
{
this->setError (QSE_AWK_ENOMEM);
return QSE_NULL;
}
#endif
s = lt_dlsym ((lt_dlhandle)handle, mname);
#if defined(QSE_CHAR_IS_MCHAR)
/* nothing to do */
#else
QSE_MMGR_FREE (awk->mmgr, mname);
#endif
return s;
#endif
}
int StdAwk::SourceFile::open (Data& io)
{
qse_sio_t* sio;

View File

@ -345,6 +345,11 @@ static void* custom_awk_modopen (
#else
modpath = qse_wcsatombsdup (tmp, QSE_NULL, awk->mmgr);
#endif
if (!modpath)
{
qse_awk_seterrnum (awk, QSE_AWK_ENOMEM, QSE_NULL);
return QSE_NULL;
}
h = lt_dlopenext (modpath);
@ -2539,136 +2544,3 @@ static int add_functions (qse_awk_t* awk)
return 0;
}
#if 0
static int query_module (
qse_awk_t* awk, const qse_char_t* name, qse_awk_mod_sym_t* sym)
{
const qse_char_t* dc;
xtn_t* xtn;
qse_rbt_pair_t* pair;
qse_size_t namelen;
mod_data_t md;
qse_cstr_t ea;
xtn = (xtn_t*)QSE_XTN(awk);
/* TODO: support module calls with deeper levels ... */
dc = qse_strstr (name, QSE_T("::"));
QSE_ASSERT (dc != QSE_NULL);
namelen = dc - name;
#if defined(_WIN32)
/*TODO: implemente this */
qse_awk_seterrnum (awk, QSE_AWK_ENOIMPL, QSE_NULL);
return -1;
#elif defined(__OS2__)
/*TODO: implemente this */
qse_awk_seterrnum (awk, QSE_AWK_ENOIMPL, QSE_NULL);
return -1;
#elif defined(__DOS__)
qse_awk_seterrnum (awk, QSE_AWK_ENOIMPL, QSE_NULL);
return -1;
#else
pair = qse_rbt_search (&xtn->modtab, name, namelen);
if (pair)
{
md = *(mod_data_t*)QSE_RBT_VPTR(pair);
}
else
{
qse_mchar_t* modpath;
qse_awk_modstd_load_t load;
#if defined(QSE_CHAR_IS_MCHAR)
qse_mcstr_t tmp[5] =
{
{ QSE_WT(""), 0 },
{ QSE_WT("/"), 0 },
{ QSE_MT("lib"), 3 },
{ QSE_NULL, 0 },
{ QSE_NULL, 0 }
};
#else
qse_wcstr_t tmp[5] =
{
{ QSE_WT(""), 0 },
{ QSE_WT("/"), 0 },
{ QSE_WT("lib"), 3 },
{ QSE_NULL, 0 },
{ QSE_NULL, 0 }
};
#endif
if (xtn->opt.moddir.len > 0)
{
tmp[0].ptr = xtn->opt.moddir.ptr;
tmp[0].len = xtn->opt.moddir.len;
tmp[1].len = 1;
}
#if defined(DEFAULT_MODDIR)
else
{
tmp[0].ptr = QSE_T(DEFAULT_MODDIR);
tmp[0].len = qse_strlen(tmp[0].ptr);
tmp[1].len = 1;
}
#endif
tmp[3].ptr = name;
tmp[3].len = namelen;
#if defined(QSE_CHAR_IS_MCHAR)
modpath = qse_mcstradup (tmp, QSE_NULL, awk->mmgr);
#else
modpath = qse_wcsnatombsdup (tmp, QSE_NULL, awk->mmgr);
#endif
if (!modpath)
{
qse_awk_seterrnum (awk, QSE_AWK_ENOMEM, QSE_NULL);
return -1;
}
md.dh = lt_dlopenext (modpath);
QSE_MMGR_FREE (awk->mmgr, modpath);
if (!md.dh)
{
ea.ptr = name;
ea.len = namelen;
qse_awk_seterror (awk, QSE_AWK_ENOENT, &ea, QSE_NULL);
return -1;
}
load = lt_dlsym (md.dh, QSE_MT("load"));
if (!load)
{
lt_dlclose (md.dh);
ea.ptr = QSE_T("load");
ea.len = 4;
qse_awk_seterror (awk, QSE_AWK_ENOENT, &ea, QSE_NULL);
return -1;
}
if (load (&md.modstd, awk) <= -1)
{
lt_dlclose (md.dh);
return -1;
}
if (qse_rbt_insert (&xtn->modtab, (void*)name, namelen, &md, QSE_SIZEOF(md)) == QSE_NULL)
{
qse_awk_seterrnum (awk, QSE_AWK_ENOMEM, QSE_NULL);
lt_dlclose (md.dh);
return -1;
}
}
return md.modstd.query (&md.modstd, awk, dc + 2, sym);
#endif
}
#endif