61#define NLC_FILE_HISTORY ".nlc_history"
183 NLC *nlc =
static_cast<NLC *
> (param);
196 fprintf(stdout,
"%s", str);
337 const auto encoded_value = curl_easy_escape(
nullptr, decoded.c_str(),
static_cast<int> (decoded.length()));
338 std::string
result(encoded_value);
339 curl_free(encoded_value);
345 const auto decoded_value = curl_easy_unescape(
nullptr, encoded.c_str(),
static_cast<int> (encoded.length()), &output_length);
346 std::string
result(decoded_value, output_length);
347 curl_free(decoded_value);
351 static std::string
AnswerCGI(std::string query, std::string respond) {
357 result +=
"\",\"out\":\"";
359 result +=
"\",\"version\":\"";
368 return "nlc [--nlc- ... ] [-h?po] <-c command | -m module-name | - | script> [main args ...]";
371 int Run(
int argc,
char** argv) {
381 bool is_playground =
false;
382 std::string file_name;
383 std::string out_file;
386 while (++pos < argc) {
389 if (strcmp(argv[pos],
"-v") == 0 || strcmp(argv[pos],
"--version") == 0) {
393 if (strcmp(argv[pos],
"-h") == 0 || strcmp(argv[pos],
"-?") == 0 || strcmp(argv[pos],
"--help") == 0) {
399 if (strstr(argv[pos],
"--nlc-") == argv[pos]) {
400 rt_args.push_back(std::string(argv[pos]));
402 }
else if (strcmp(argv[pos],
"-p") == 0 || strcmp(argv[pos],
"--playground-cgi") == 0) {
403 is_playground =
true;
404 rt_args.push_back(
"--nlc-no-embed-source");
405 rt_args.push_back(
"--nlc-no-import-module");
406 rt_args.push_back(
"--nlc-no-import-native");
407 rt_args.push_back(
"--nlc-no-eval-enable");
409 }
else if (strcmp(argv[pos],
"-o") == 0 || strcmp(argv[pos],
"--output") == 0) {
411 LOG_RUNTIME(
"Argument expected after '%s'", argv[pos - 1]);
413 out_file = argv[pos];
415 }
else if (strcmp(argv[pos],
"-l") == 0 || strcmp(argv[pos],
"--log-dir") == 0) {
417 LOG_RUNTIME(
"Argument expected after '%s'", argv[pos - 1]);
424 if (strcmp(argv[pos],
"-c") == 0 || strcmp(argv[pos],
"-m") == 0 || strcmp(argv[pos],
"-") == 0) {
425 if (strcmp(argv[pos++],
"-") == 0) {
426 std::getline(std::cin, command);
429 LOG_RUNTIME(
"Argument expected after '%s'", argv[pos - 1]);
431 if (strcmp(argv[pos - 1],
"-m") == 0) {
434 LOG_RUNTIME(
"You can't run the module in playground mode!");
437 file_name = argv[pos];
441 ASSERT(strcmp(argv[pos - 1],
"-c") == 0);
448 file_name = argv[pos];
452 while (++pos < argc) {
461 if (!std::filesystem::exists(std::filesystem::path(
m_log_file_dir))) {
462 std::filesystem::create_directories(std::filesystem::path(
m_log_file_dir));
465 if (!std::filesystem::exists(std::filesystem::path(
m_log_file_dir))) {
466 LOG_RUNTIME(
"The directory '%s' for log files does not exist or an error occurred while creating it!",
m_log_file_dir.c_str());
468 if (!std::filesystem::is_directory(std::filesystem::path(
m_log_file_dir))) {
472 const std::time_t now = std::time(
nullptr);
474 std::strftime(filename,
sizeof (filename),
"%y%m%d_%H%M%S.log", std::localtime(&now));
495 ASSERT(file_name.empty());
497 if (command.empty()) {
498 command = getenv(
"QUERY_STRING") ? getenv(
"QUERY_STRING") :
"";
506 std::string header = \
507 "Content-Type: text/plain;\n"
508 "Access-Control-Allow-Origin: *\n\n";
512 if (out_file.empty()) {
517 LOG_RUNTIME(
"Fail write to file '%s'", out_file.c_str());
528 }
catch (std::exception &err) {
539 if (out_file.empty()) {
544 LOG_RUNTIME(
"Fail write to file '%s'", out_file.c_str());
549 }
else if (!file_name.empty()) {
552 }
else if (!command.empty()) {
553 ASSERT(file_name.empty());
554 result = jit->
Run(command, main_args.get());
556 ret_code =
RunREPL(rt, main_args);
ObjPtr Run(const std::string_view str, Obj *args=nullptr)
ObjPtr RunFile(std::string file, Obj *args=nullptr)
void FuncCallback(void *param, LogLevelType level, const char *str, bool flush)
void SetCallback(FuncCallback *func, void *param)
LogLevelType GetLogLevel()
void SaveCallback(FuncCallback *&func, void *¶m)
static Logger * Instance()
Logger::FuncCallback * m_log_callback_save
static std::string AnswerCGI(std::string query, std::string respond)
int RunREPL(RuntimePtr rt, ObjPtr main_args)
static std::string url_decode(const std::string &encoded)
static std::string url_encode(const std::string &decoded)
std::string m_log_file_dir
static void LoggerCallback(void *param, Logger::LogLevelType level, const char *str, bool flush)
int Run(int argc, char **argv)
Logger::LogLevelType m_loglevel_save
void * m_log_callback_arg_save
static ObjPtr CreateString(const std::string_view str, Sync *sync=nullptr)
static ObjPtr CreateDict(Sync *sync=nullptr)
static RuntimePtr Init(StringArray args)
#define LOG_RUNTIME(format,...)
#define ASSERT(condition)
std::shared_ptr< Term > TermPtr
std::shared_ptr< Obj > ObjPtr
std::shared_ptr< RunTime > RuntimePtr
std::vector< std::string > StringArray
#define VERSION_SOURCE_FULL_ID