#pragma prototyped
#include "colib.h"
int
coexport(Coshell_t* co, const char* name, const char* value)
{
Coexport_t* ex;
char* v;
if (!co->export)
{
if (!(co->exdisc = vmnewof(co->vm, 0, Dtdisc_t, 1, 0)))
return -1;
co->exdisc->link = offsetof(Coexport_t, link);
co->exdisc->key = offsetof(Coexport_t, name);
co->exdisc->size = 0;
if (!(co->export = dtnew(co->vm, co->exdisc, Dthash)))
{
vmfree(co->vm, co->exdisc);
return -1;
}
}
if (!(ex = (Coexport_t*)dtmatch(co->export, name)))
{
if (!value)
return 0;
if (!(ex = vmnewof(co->vm, 0, Coexport_t, 1, strlen(name))))
return -1;
strcpy(ex->name, name);
dtinsert(co->export, ex);
}
if (ex->value)
{
vmfree(co->vm, ex->value);
ex->value = 0;
}
if (value)
{
if (!(v = vmstrdup(co->vm, value)))
return -1;
ex->value = v;
}
else
{
dtdelete(co->export, ex);
vmfree(co->vm, ex);
}
co->init.sync = 1;
return 0;
}