pocketlang/cli/main.c

243 lines
6.1 KiB
C
Raw Normal View History

2021-02-07 15:40:00 +08:00
/*
* Copyright (c) 2020-2022 Thakee Nathees
* Copyright (c) 2021-2022 Pocketlang Contributors
2021-06-09 18:42:26 +08:00
* Distributed Under The MIT License
2021-02-07 15:40:00 +08:00
*/
#include "internal.h"
#include "modules/modules.h"
#include "thirdparty/argparse/argparse.h"
2021-06-09 18:42:26 +08:00
// FIXME: Everything below here is temporary and for testing.
2021-02-08 02:30:29 +08:00
int repl(PKVM* vm, const PkCompileOptions* options);
2021-06-09 18:42:26 +08:00
const char* read_line(uint32_t* length);
2021-05-23 04:59:32 +08:00
// ---------------------------------------
2021-06-07 13:54:06 +08:00
void onResultDone(PKVM* vm, PkStringPtr result) {
if ((bool)result.user_data) {
free((void*)result.string);
}
}
void stderrWrite(PKVM* vm, const char* text) {
fprintf(stderr, "%s", text);
2021-02-08 02:30:29 +08:00
}
void stdoutWrite(PKVM* vm, const char* text) {
2021-02-12 01:35:43 +08:00
fprintf(stdout, "%s", text);
2021-02-08 02:30:29 +08:00
}
PkStringPtr stdinRead(PKVM* vm) {
2021-06-07 13:54:06 +08:00
PkStringPtr result;
2021-06-09 18:42:26 +08:00
result.string = read_line(NULL);
2021-06-07 13:54:06 +08:00
result.on_done = onResultDone;
result.user_data = (void*)true;
return result;
2021-02-08 02:30:29 +08:00
}
2021-06-04 22:55:06 +08:00
PkStringPtr resolvePath(PKVM* vm, const char* from, const char* path) {
PkStringPtr result;
2021-05-06 22:19:30 +08:00
result.on_done = onResultDone;
2021-05-29 02:53:46 +08:00
size_t from_dir_len;
pathGetDirName(from, &from_dir_len);
// FIXME: Should handle paths with size of more than FILENAME_MAX.
if (from_dir_len == 0 || pathIsAbsolute(path)) {
size_t length = strlen(path);
char* resolved = (char*)malloc(length + 1);
pathNormalize(path, resolved, length + 1);
result.string = resolved;
result.user_data = (void*)true;
} else {
char from_dir[FILENAME_MAX];
strncpy(from_dir, from, from_dir_len);
from_dir[from_dir_len] = '\0';
char fullpath[FILENAME_MAX];
size_t length = pathJoin(from_dir, path, fullpath, sizeof(fullpath));
char* resolved = (char*)malloc(length + 1);
pathNormalize(fullpath, resolved, length + 1);
result.string = resolved;
result.user_data = (void*)true;
}
2021-05-06 22:19:30 +08:00
return result;
}
2021-06-04 22:55:06 +08:00
PkStringPtr loadScript(PKVM* vm, const char* path) {
2021-06-04 22:55:06 +08:00
PkStringPtr result;
2021-05-06 22:19:30 +08:00
result.on_done = onResultDone;
2021-02-12 01:35:43 +08:00
// Open the file.
FILE* file = fopen(path, "r");
if (file == NULL) {
// Setting .string to NULL to indicate the failure of loading the script.
result.string = NULL;
2021-02-12 01:35:43 +08:00
return result;
}
// Get the source length.
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
// Read source to buffer.
2021-05-19 02:59:09 +08:00
char* buff = (char*)malloc((size_t)(file_size) + 1);
2021-02-12 01:35:43 +08:00
// Using read instead of file_size is because "\r\n" is read as '\n' in
2021-05-22 21:27:40 +08:00
// windows.
if (buff != NULL) {
size_t read = fread(buff, sizeof(char), file_size, file);
buff[read] = '\0';
}
2021-02-12 01:35:43 +08:00
fclose(file);
2021-05-06 22:19:30 +08:00
result.string = buff;
result.user_data = (void*)true;
2021-02-12 01:35:43 +08:00
return result;
2021-02-08 02:30:29 +08:00
}
// FIXME:
// Included for isatty(). This should be moved to somewhere. and I'm not sure
// about the portability of these headers. and isatty() function.
#ifdef _WIN32
#include <Windows.h>
#include <io.h>
#else
#include <unistd.h>
#endif
// Create new pocket VM and set it's configuration.
static PKVM* intializePocketVM() {
2021-06-04 22:55:06 +08:00
PkConfiguration config = pkNewConfiguration();
config.stderr_write = stderrWrite;
config.stdout_write = stdoutWrite;
config.stdin_read = stdinRead;
2021-02-12 01:35:43 +08:00
config.load_script_fn = loadScript;
2021-05-07 17:41:19 +08:00
config.resolve_path_fn = resolvePath;
2021-02-12 01:35:43 +08:00
// FIXME:
// Refactor and make it portable. Maybe custom is_tty() function?.
// Windows isatty depricated -- use _isatty.
if (!!isatty(fileno(stderr))) {
#ifdef _WIN32
DWORD outmode = 0;
HANDLE handle = GetStdHandle(STD_ERROR_HANDLE);
GetConsoleMode(handle, &outmode);
SetConsoleMode(handle, outmode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
#endif
config.use_ansi_color = true;
}
return pkNewVM(&config);
}
int main(int argc, const char** argv) {
// Parse command line arguments.
const char* usage[] = {
"pocket ... [-c cmd | file] ...",
NULL,
};
const char* cmd = NULL;
int debug = false, help = false, quiet = false, version = false;
struct argparse_option cli_opts[] = {
OPT_STRING('c', "cmd", (void*)&cmd,
"Evaluate and run the passed string.", NULL, 0, 0),
OPT_BOOLEAN('d', "debug", (void*)&debug,
"Compile and run the debug version.", NULL, 0, 0),
OPT_BOOLEAN('h', "help", (void*)&help,
"Prints this help message and exit.", NULL, 0, 0),
OPT_BOOLEAN('q', "quiet", (void*)&quiet,
"Don't print version and copyright statement on REPL startup.",
NULL, 0, 0),
2021-06-07 13:54:06 +08:00
OPT_BOOLEAN('v', "version", &version,
"Prints the pocketlang version and exit.", NULL, 0, 0),
OPT_END(),
};
2021-06-09 18:42:26 +08:00
// Parse the options.
struct argparse argparse;
argparse_init(&argparse, cli_opts, usage, 0);
argc = argparse_parse(&argparse, argc, argv);
if (help) { // pocket --help.
argparse_usage(&argparse);
return 0;
}
if (version) { // pocket --version
fprintf(stdout, "pocketlang %s\n", PK_VERSION_STRING);
return 0;
}
int exitcode = 0;
// Create and initialize pocket VM.
PKVM* vm = intializePocketVM();
2021-06-09 18:42:26 +08:00
VmUserData user_data;
user_data.repl_mode = false;
pkSetUserData(vm, &user_data);
REGISTER_ALL_MODULES(vm);
2021-06-04 22:55:06 +08:00
PkCompileOptions options = pkNewCompilerOptions();
options.debug = debug;
2021-06-07 13:54:06 +08:00
if (cmd != NULL) { // pocket -c "print('foo')"
2021-06-04 22:55:06 +08:00
PkStringPtr source = { cmd, NULL, NULL, 0, 0 };
2022-04-13 08:34:14 +08:00
PkStringPtr path = { "@(Source)", NULL, NULL, 0, 0 };
PkResult result = pkInterpretSource(vm, source, path, NULL);
exitcode = (int)result;
2021-06-04 22:55:06 +08:00
} else if (argc == 0) { // Run on REPL mode.
// Print the copyright and license notice, if --quiet not set.
if (!quiet) {
printf("%s", CLI_NOTICE);
}
2021-05-22 21:27:40 +08:00
options.repl_mode = true;
exitcode = repl(vm, &options);
2021-05-23 04:59:32 +08:00
} else { // pocket file.pk ...
2021-05-24 06:17:52 +08:00
PkStringPtr resolved = resolvePath(vm, ".", argv[0]);
PkStringPtr source = loadScript(vm, resolved.string);
if (source.string != NULL) {
PkResult result = pkInterpretSource(vm, source, resolved, &options);
exitcode = (int)result;
} else {
fprintf(stderr, "Error: cannot open file at \"%s\"\n", resolved.string);
if (resolved.on_done != NULL) resolved.on_done(vm, resolved);
if (source.on_done != NULL) source.on_done(vm, source);
}
2021-06-04 22:55:06 +08:00
}
// Cleanup the VM and exit.
2021-06-04 22:55:06 +08:00
pkFreeVM(vm);
return exitcode;
2021-02-07 15:40:00 +08:00
}