/*- Author: Brian Tiffin Dedicated to the public domain Date started: December 2016 Modified: 2016-12-09/03:37-0500 +*/ /* unicox.c, trial for libcox embedding in Unicon tectonics: gcc -o unicox.so -shared -fpic unicox.c libcox.c */ #include #include "libcox.h" #include "icall.h" static libcox *libcoxHandle; static libcox_value *libcoxResult; int unicox(int argc, descriptor argv[]) { const char *libcoxValue; int rc; /* handle is remembered across calls */ if (!libcoxHandle) { rc = libcox_init(&libcoxHandle); if (rc != LIBCOX_OK) { Error(500); } } /* Unicon passes a command string (and possibly one argument) */ ArgString(1); if (argc > 1) { ArgString(2); } /* last result left alone, freed before converting a new value */ if (libcoxResult) { libcox_exec_result_destroy(libcoxHandle, libcoxResult); } /* Evaluate the command, with no, or one argument */ if (argc > 1) { rc = libcox_exec_fmt(libcoxHandle, &libcoxResult, StringVal(argv[1]), StringVal(argv[2])); } else { rc = libcox_exec(libcoxHandle, &libcoxResult, StringVal(argv[1]), -1); } if (rc != LIBCOX_OK) { Error(107); } libcoxValue = libcox_value_to_string(libcoxResult, 0); RetString((char *)libcoxValue); } int unicoxClose(int argc, descriptor argv[]) { if (libcoxHandle) { libcox_release(libcoxHandle); RetNull(); } else { Fail; } }