1 Commits

Author SHA1 Message Date
wash fa9572d33a fx: iterator: return NULL value if iterator status is not SUCCESS 2026-05-30 20:19:47 +01:00
46 changed files with 309 additions and 2031 deletions
-4
View File
@@ -1,4 +0,0 @@
add_fx_assembly(
NAME fx.diagnostics
NAMESPACES fx.diagnostics
DEPENDENCIES fx.runtime fx.collections fx.io)
-7
View File
@@ -1,7 +0,0 @@
#include <fx/macros.h>
#include <fx/reflection/assembly.h>
FX_ASSEMBLY_BEGIN(fx_diagnostics)
FX_ASSEMBLY_NAME("fx.diagnostics");
FX_ASSEMBLY_VERSION(1, 0, 0, 0);
FX_ASSEMBLY_END(fx_diagnostics)
+1 -1
View File
@@ -48,7 +48,7 @@ enum fx_status fx_arglist_report_missing_option(
} }
fx_err("required option `" F_YELLOW "%s" F_RESET "` was not specified.", fx_err("required option `" F_YELLOW "%s" F_RESET "` was not specified.",
fx_stringstream_get_cstr(opt_name)); fx_stringstream_ptr(opt_name));
fx_i("usage: %s", fx_string_get_cstr(opt_string)); fx_i("usage: %s", fx_string_get_cstr(opt_string));
fx_i("for more information, use `" F_YELLOW "--help" F_RESET "`"); fx_i("for more information, use `" F_YELLOW "--help" F_RESET "`");
+11 -30
View File
@@ -68,14 +68,9 @@ static fx_status convert_key_to_index(
return status; return status;
} }
size_t base_index = hash % table_size; size_t index = hash % table_size, i = 1;
size_t index = base_index, i = 0;
bool index_found = false; bool index_found = false;
while (i <= table_size) { while (table[index].i_item && i <= table_size) {
if (!table[index].i_item) {
goto skip;
}
const struct fx_hashtable_item_p *item = fx_object_get_private( const struct fx_hashtable_item_p *item = fx_object_get_private(
table[index].i_item, table[index].i_item,
FX_TYPE_HASHTABLE_ITEM); FX_TYPE_HASHTABLE_ITEM);
@@ -85,8 +80,7 @@ static fx_status convert_key_to_index(
break; break;
} }
skip: index = (index + (i * i)) % table_size;
index = (base_index + (i * i)) % table_size;
i++; i++;
} }
@@ -114,7 +108,7 @@ static fx_status allocate_index_for_key(
size_t index = hash % table_size; size_t index = hash % table_size;
bool ok = false; bool ok = false;
for (size_t i = 0; i < table_size; i++) { for (size_t i = 1; i < table_size; i++) {
if (!table[index].i_item) { if (!table[index].i_item) {
ok = true; ok = true;
break; break;
@@ -282,27 +276,18 @@ static fx_status hashtable_put(
break; break;
} while (1); } while (1);
fx_hashtable_item *item = hashtable->t_items[index].i_item; fx_hashtable_item *item = fx_object_create(FX_TYPE_HASHTABLE_ITEM);
struct fx_hashtable_item_p *item_p = NULL;
if (!item) { if (!item) {
item = fx_object_create(FX_TYPE_HASHTABLE_ITEM); return FX_ERR_NO_MEMORY;
if (!item) {
return FX_ERR_NO_MEMORY;
}
item_p = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(&item_p->i_key, key);
hashtable->t_items[index].i_item = item;
hashtable->t_count++;
} else {
item_p = fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_unset(&item_p->i_value);
} }
struct fx_hashtable_item_p *item_p
= fx_object_get_private(item, FX_TYPE_HASHTABLE_ITEM);
fx_value_copy(&item_p->i_key, key);
fx_value_copy(&item_p->i_value, value); fx_value_copy(&item_p->i_value, value);
hashtable->t_items[index].i_item = item;
hashtable->t_count++;
return FX_SUCCESS; return FX_SUCCESS;
} }
@@ -638,10 +623,6 @@ static const fx_value *iterator_get_value(const fx_iterator *obj)
struct table_item *table = it->_h_p->t_items; struct table_item *table = it->_h_p->t_items;
size_t capacity = primes[it->_h_p->t_max_index]; size_t capacity = primes[it->_h_p->t_max_index];
if (it->i >= capacity) {
return NULL;
}
if (table[it->i].i_item) { if (table[it->i].i_item) {
return &it->item_value; return &it->item_value;
} }
+1 -1
View File
@@ -11,7 +11,7 @@ int main(int argc, const char **argv)
fx_stream_read_all_bytes_s(fx_stdin, dest_stream, buf, &nr_read); fx_stream_read_all_bytes_s(fx_stdin, dest_stream, buf, &nr_read);
printf("done. read %zu bytes total.\n", nr_read); printf("done. read %zu bytes total.\n", nr_read);
printf("%s\n", fx_stringstream_get_cstr(dest_stream)); printf("%s\n", fx_stringstream_ptr(dest_stream));
fx_stringstream_unref(dest_stream); fx_stringstream_unref(dest_stream);
fx_stream_buffer_unref(buf); fx_stream_buffer_unref(buf);
-1
View File
@@ -1 +0,0 @@
export_fx_namespace_details(fx.diagnostics)
@@ -1,46 +0,0 @@
#ifndef FX_DIAGNOSTICS_PROCESS_H_
#define FX_DIAGNOSTICS_PROCESS_H_
#include <fx/collections/hashtable.h>
#include <fx/macros.h>
#include <fx/stream.h>
FX_DECLS_BEGIN;
#define FX_DIAGNOSTICS_TYPE_PROCESS (fx_process_get_type())
FX_DECLARE_TYPE(fx_process);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_process)
FX_TYPE_CLASS_DECLARATION_END(fx_process)
typedef struct fx_process_start_info {
bool proc_redirect_stdin, proc_redirect_stdout, proc_redirect_stderr;
const char *proc_file_name;
const char **proc_args;
size_t proc_args_count;
const fx_hashtable *proc_environment;
} fx_process_start_info;
FX_API fx_type_id fx_process_get_type(void);
FX_API fx_process *fx_process_create(const fx_process_start_info *info);
FX_API fx_process *fx_process_get_self(void);
FX_API fx_status fx_process_start(fx_process *proc);
FX_API fx_status fx_process_wait(fx_process *proc, int *out_result);
FX_API fx_status fx_process_kill(fx_process *proc);
FX_API const fx_hashtable *fx_process_get_environment(const fx_process *proc);
FX_API fx_stream *fx_process_get_stdin(fx_process *proc);
FX_API fx_stream *fx_process_get_stdout(fx_process *proc);
FX_API fx_stream *fx_process_get_stderr(fx_process *proc);
FX_API void fx_process_close_stdin(fx_process *proc);
FX_API void fx_process_close_stdout(fx_process *proc);
FX_API void fx_process_close_stderr(fx_process *proc);
FX_DECLS_END;
#endif
-368
View File
@@ -1,368 +0,0 @@
#include <fx/collections/array.h>
#include <fx/diagnostics/process.h>
#include <fx/io/pipe.h>
#include <fx/io/stream.h>
#include <fx/iterator.h>
#include <fx/macros.h>
#include <fx/string.h>
#include <signal.h>
#include <unistd.h>
FX_API fx_type_id fx_process_iterator_get_type();
FX_DECLARE_TYPE(fx_process_iterator);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_process_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_process_iterator)
struct fx_process_p {
fx_string *proc_exec_path;
fx_array *proc_args;
bool proc_redirect_stdin, proc_redirect_stdout, proc_redirect_stderr;
fx_stream *proc_stdin, *proc_stdout, *proc_stderr;
pid_t proc_id;
};
struct fx_process_iterator_p {
};
/*** PRIVATE FUNCTIONS ********************************************************/
static fx_status process_start(struct fx_process_p *proc)
{
fx_iostream *in_r = NULL, *in_w = NULL;
fx_iostream *out_r = NULL, *out_w = NULL;
fx_iostream *err_r = NULL, *err_w = NULL;
fx_status status = FX_SUCCESS;
if (proc->proc_redirect_stdin) {
status = fx_pipe_create(&in_r, &in_w);
if (!FX_OK(status)) {
goto cleanup;
}
}
if (proc->proc_redirect_stdout) {
status = fx_pipe_create(&out_r, &out_w);
if (!FX_OK(status)) {
goto cleanup;
}
}
if (proc->proc_redirect_stderr) {
status = fx_pipe_create(&err_r, &err_w);
if (!FX_OK(status)) {
goto cleanup;
}
}
pid_t pid = fork();
if (pid < 0) {
status = FX_ERR_INVALID_ARGUMENT;
goto cleanup;
}
if (pid > 0) {
proc->proc_id = pid;
proc->proc_stdin = fx_iostream_ref(in_w);
proc->proc_stdout = fx_iostream_ref(out_r);
proc->proc_stderr = fx_iostream_ref(err_r);
status = FX_SUCCESS;
goto cleanup;
}
int fd = -1;
if (proc->proc_redirect_stdin) {
fx_iostream_unref(in_w);
fd = fx_iostream_steal_os_handle(in_r);
fprintf(stderr, "dup2(%d, %d)\n", fd, 0);
int err = dup2(fd, 0);
close(fd);
}
if (proc->proc_redirect_stdout) {
fx_iostream_unref(out_r);
fd = fx_iostream_steal_os_handle(out_w);
dup2(fd, 1);
close(fd);
}
if (proc->proc_redirect_stderr) {
fx_iostream_unref(err_r);
fd = fx_iostream_steal_os_handle(err_w);
dup2(fd, 2);
close(fd);
}
const char **argv = NULL;
size_t argc = 0;
if (proc->proc_args) {
argc = fx_array_get_size(proc->proc_args);
}
if (argc) {
argv = calloc(argc + 1, sizeof(char *));
for (size_t i = 0; i < argc; i++) {
const fx_value *arg_value
= fx_array_get_ref(proc->proc_args, i);
fx_value_get_cstr(arg_value, &argv[i]);
}
}
execv(fx_string_get_cstr(proc->proc_exec_path), (char *const *)argv);
exit(127);
cleanup:
fx_iostream_unref(in_r);
fx_iostream_unref(in_w);
fx_iostream_unref(out_r);
fx_iostream_unref(out_w);
fx_iostream_unref(err_r);
fx_iostream_unref(err_w);
return status;
}
static fx_status process_wait(struct fx_process_p *proc, int *out_result)
{
if (proc->proc_id == 0) {
return FX_ERR_BAD_STATE;
}
int status = 0;
do {
int err = waitpid(proc->proc_id, &status, WUNTRACED);
if (err != 0) {
return FX_ERR_BAD_STATE;
}
} while (!WIFEXITED(status));
proc->proc_id = 0;
*out_result = WEXITSTATUS(status);
return FX_ERR_NOT_SUPPORTED;
}
static fx_status process_kill(struct fx_process_p *proc)
{
if (proc->proc_id == 0) {
return FX_ERR_BAD_STATE;
}
int err = kill(proc->proc_id, SIGTERM);
if (err != 0) {
return FX_ERR_BAD_STATE;
}
return FX_SUCCESS;
}
static fx_stream *process_get_stdin(struct fx_process_p *proc)
{
return proc->proc_stdin;
}
static fx_stream *process_get_stdout(struct fx_process_p *proc)
{
return proc->proc_stdout;
}
static fx_stream *process_get_stderr(struct fx_process_p *proc)
{
return proc->proc_stderr;
}
static void process_close_stdin(struct fx_process_p *proc)
{
fx_iostream_unref(proc->proc_stdin);
proc->proc_stdin = NULL;
}
static void process_close_stdout(struct fx_process_p *proc)
{
fx_iostream_unref(proc->proc_stdout);
proc->proc_stdout = NULL;
}
static void process_close_stderr(struct fx_process_p *proc)
{
fx_iostream_unref(proc->proc_stderr);
proc->proc_stderr = NULL;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_process *fx_process_create(const fx_process_start_info *info)
{
fx_process *out = fx_object_create(FX_DIAGNOSTICS_TYPE_PROCESS);
if (!out) {
return NULL;
}
struct fx_process_p *proc
= fx_object_get_private(out, FX_DIAGNOSTICS_TYPE_PROCESS);
proc->proc_exec_path = fx_string_create_from_cstr(info->proc_file_name);
if (!proc->proc_exec_path) {
fx_process_unref(out);
return NULL;
}
if (info->proc_args_count > 0) {
proc->proc_args = fx_array_create();
if (!proc->proc_args) {
fx_process_unref(out);
return NULL;
}
}
for (size_t i = 0; i < info->proc_args_count; i++) {
fx_string *arg = fx_string_create_from_cstr(info->proc_args[i]);
if (!arg) {
fx_process_unref(out);
return NULL;
}
fx_array_push_back(proc->proc_args, FX_VALUE_OBJECT(arg));
fx_string_unref(arg);
}
proc->proc_redirect_stdin = info->proc_redirect_stdin;
proc->proc_redirect_stdout = info->proc_redirect_stdout;
proc->proc_redirect_stderr = info->proc_redirect_stderr;
return out;
}
fx_status fx_process_start(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_start,
proc);
}
fx_status fx_process_wait(fx_process *proc, int *out_result)
{
FX_CLASS_DISPATCH_STATIC(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_wait,
proc,
out_result);
}
fx_status fx_process_kill(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_kill,
proc);
}
fx_stream *fx_process_get_stdin(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_stdin,
proc);
}
fx_stream *fx_process_get_stdout(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_stdout,
proc);
}
fx_stream *fx_process_get_stderr(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_stderr,
proc);
}
void fx_process_close_stdin(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_V0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_close_stdin,
proc);
}
void fx_process_close_stdout(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_V0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_close_stdout,
proc);
}
void fx_process_close_stderr(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_V0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_close_stderr,
proc);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void process_init(fx_object *obj, void *priv)
{
}
static void process_fini(fx_object *obj, void *priv)
{
}
/*** ITERATOR DEFINITION ******************************************************/
static enum fx_status process_iterator_move_next(const fx_iterator *obj)
{
return FX_ERR_NO_DATA;
}
static const fx_value *process_iterator_get_value(const fx_iterator *obj)
{
return NULL;
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_process)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_process)
FX_TYPE_DEFINITION_BEGIN(fx_process)
FX_TYPE_ID(0x7334594f, 0xa1c3, 0x4715, 0xab69, 0x68a709e1b64b);
FX_TYPE_NAME("fx.diagnostics.process");
FX_TYPE_CLASS(fx_process_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_process_p);
FX_TYPE_INSTANCE_INIT(process_init);
FX_TYPE_INSTANCE_FINI(process_fini);
FX_TYPE_DEFINITION_END(fx_process)
FX_TYPE_CLASS_BEGIN(fx_process_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = process_iterator_move_next;
FX_INTERFACE_ENTRY(it_get_value) = process_iterator_get_value;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_process_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_process_iterator)
FX_TYPE_ID(0x67eb13b6, 0x25d1, 0x424a, 0xb136, 0x871a07829089);
FX_TYPE_NAME("fx.diagnostics.process.iterator");
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_process_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_process_iterator_p);
FX_TYPE_DEFINITION_END(fx_process_iterator)
-491
View File
@@ -1,491 +0,0 @@
#define _POSIX_C_SOURCE 200809L
#include <fx/collections/array.h>
#include <fx/diagnostics/process.h>
#include <fx/global.h>
#include <fx/io/pipe.h>
#include <fx/io/stream.h>
#include <fx/iterator.h>
#include <fx/macros.h>
#include <fx/string.h>
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
static fx_process *self = NULL;
extern const char **environ;
FX_API fx_type_id fx_process_iterator_get_type();
FX_DECLARE_TYPE(fx_process_iterator);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_process_iterator)
FX_TYPE_CLASS_DECLARATION_END(fx_process_iterator)
struct fx_process_p {
fx_string *proc_exec_path;
fx_array *proc_args;
bool proc_redirect_stdin, proc_redirect_stdout, proc_redirect_stderr;
fx_stream *proc_stdin, *proc_stdout, *proc_stderr;
pid_t proc_id;
fx_hashtable *proc_environment;
};
struct fx_process_iterator_p {
};
/*** PRIVATE FUNCTIONS ********************************************************/
static fx_status process_start(struct fx_process_p *proc)
{
fx_iostream *in_r = NULL, *in_w = NULL;
fx_iostream *out_r = NULL, *out_w = NULL;
fx_iostream *err_r = NULL, *err_w = NULL;
fx_status status = FX_SUCCESS;
if (proc->proc_redirect_stdin) {
status = fx_pipe_create(&in_r, &in_w);
if (!FX_OK(status)) {
goto cleanup;
}
}
if (proc->proc_redirect_stdout) {
status = fx_pipe_create(&out_r, &out_w);
if (!FX_OK(status)) {
goto cleanup;
}
}
if (proc->proc_redirect_stderr) {
status = fx_pipe_create(&err_r, &err_w);
if (!FX_OK(status)) {
goto cleanup;
}
}
pid_t pid = fork();
if (pid < 0) {
status = FX_ERR_INVALID_ARGUMENT;
goto cleanup;
}
if (pid > 0) {
proc->proc_id = pid;
proc->proc_stdin = fx_iostream_ref(in_w);
proc->proc_stdout = fx_iostream_ref(out_r);
proc->proc_stderr = fx_iostream_ref(err_r);
status = FX_SUCCESS;
goto cleanup;
}
int fd = -1;
if (proc->proc_redirect_stdin) {
fx_iostream_unref(in_w);
fd = fx_iostream_steal_os_handle(in_r);
int err = dup2(fd, 0);
close(fd);
}
if (proc->proc_redirect_stdout) {
fx_iostream_unref(out_r);
fd = fx_iostream_steal_os_handle(out_w);
dup2(fd, 1);
close(fd);
}
if (proc->proc_redirect_stderr) {
fx_iostream_unref(err_r);
fd = fx_iostream_steal_os_handle(err_w);
dup2(fd, 2);
close(fd);
}
const char **argv = NULL;
const char **envp = NULL;
const char *default_argv[] = {
fx_string_get_cstr(proc->proc_exec_path),
NULL,
};
size_t argc = 0;
size_t envc = 0;
if (proc->proc_args) {
argc = fx_array_get_size(proc->proc_args);
}
if (proc->proc_environment) {
envc = fx_hashtable_get_count(proc->proc_environment);
}
if (argc) {
argv = calloc(argc + 1, sizeof(char *));
for (size_t i = 0; i < argc; i++) {
const fx_value *arg_value
= fx_array_get_ref(proc->proc_args, i);
fx_value_get_cstr(arg_value, &argv[i]);
}
} else {
argv = default_argv;
}
if (envc) {
envp = calloc(envc + 1, sizeof(char *));
size_t i = 0;
fx_stringstream *tmp = fx_stringstream_create();
const fx_iterator *it
= fx_iterator_begin(proc->proc_environment);
fx_foreach(v, it)
{
fx_hashtable_item *item = NULL;
fx_value_get_object(v, &item);
const fx_value *name_v
= fx_hashtable_item_get_key(item);
const fx_value *value_v
= fx_hashtable_item_get_value(item);
fx_value_to_string(name_v, tmp, NULL);
fx_stream_write_char(tmp, '=');
fx_value_to_string(value_v, tmp, NULL);
envp[i++] = fx_stringstream_steal(tmp);
}
fx_iterator_unref(it);
fx_string_unref(tmp);
}
execve(fx_string_get_cstr(proc->proc_exec_path),
(char *const *)argv,
(char *const *)envp);
exit(127);
cleanup:
fx_iostream_unref(in_r);
fx_iostream_unref(in_w);
fx_iostream_unref(out_r);
fx_iostream_unref(out_w);
fx_iostream_unref(err_r);
fx_iostream_unref(err_w);
return status;
}
static fx_status process_wait(struct fx_process_p *proc, int *out_result)
{
if (proc->proc_id == 0) {
return FX_ERR_BAD_STATE;
}
int status = 0;
do {
int err = waitpid(proc->proc_id, &status, WUNTRACED);
if (err != 0) {
return FX_ERR_BAD_STATE;
}
} while (!WIFEXITED(status));
proc->proc_id = 0;
*out_result = WEXITSTATUS(status);
return FX_ERR_NOT_SUPPORTED;
}
static fx_status process_kill(struct fx_process_p *proc)
{
if (proc->proc_id == 0) {
return FX_ERR_BAD_STATE;
}
int err = kill(proc->proc_id, SIGTERM);
if (err != 0) {
return FX_ERR_BAD_STATE;
}
return FX_SUCCESS;
}
static const fx_hashtable *process_get_environment(
const struct fx_process_p *proc)
{
return proc->proc_environment;
}
static fx_stream *process_get_stdin(struct fx_process_p *proc)
{
return proc->proc_stdin;
}
static fx_stream *process_get_stdout(struct fx_process_p *proc)
{
return proc->proc_stdout;
}
static fx_stream *process_get_stderr(struct fx_process_p *proc)
{
return proc->proc_stderr;
}
static void process_close_stdin(struct fx_process_p *proc)
{
fx_iostream_unref(proc->proc_stdin);
proc->proc_stdin = NULL;
}
static void process_close_stdout(struct fx_process_p *proc)
{
fx_iostream_unref(proc->proc_stdout);
proc->proc_stdout = NULL;
}
static void process_close_stderr(struct fx_process_p *proc)
{
fx_iostream_unref(proc->proc_stderr);
proc->proc_stderr = NULL;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_process *fx_process_create(const fx_process_start_info *info)
{
fx_process *out = fx_object_create(FX_DIAGNOSTICS_TYPE_PROCESS);
if (!out) {
return NULL;
}
struct fx_process_p *proc
= fx_object_get_private(out, FX_DIAGNOSTICS_TYPE_PROCESS);
proc->proc_exec_path = fx_string_create_from_cstr(info->proc_file_name);
if (!proc->proc_exec_path) {
fx_process_unref(out);
return NULL;
}
if (info->proc_args_count > 0) {
proc->proc_args = fx_array_create();
if (!proc->proc_args) {
fx_process_unref(out);
return NULL;
}
}
for (size_t i = 0; i < info->proc_args_count; i++) {
fx_string *arg = fx_string_create_from_cstr(info->proc_args[i]);
if (!arg) {
fx_process_unref(out);
return NULL;
}
fx_array_push_back(proc->proc_args, FX_VALUE_OBJECT(arg));
fx_string_unref(arg);
}
proc->proc_environment
= fx_hashtable_ref((fx_hashtable *)info->proc_environment);
proc->proc_redirect_stdin = info->proc_redirect_stdin;
proc->proc_redirect_stdout = info->proc_redirect_stdout;
proc->proc_redirect_stderr = info->proc_redirect_stderr;
return out;
}
fx_process *fx_process_get_self(void)
{
if (self) {
return self;
}
self = fx_object_create(FX_DIAGNOSTICS_TYPE_PROCESS);
if (!self) {
return NULL;
}
struct fx_process_p *proc
= fx_object_get_private(self, FX_DIAGNOSTICS_TYPE_PROCESS);
proc->proc_id = getpid();
fx_hashtable *env = fx_hashtable_create();
for (size_t i = 0; environ[i]; i++) {
char *envstr = fx_strdup(environ[i]);
const char *name = envstr;
char *sep = strchr(name, '=');
const char *value = NULL;
if (sep) {
*sep = '\0';
value = sep + 1;
}
fx_string *name_str = fx_string_create_from_cstr(name);
fx_string *value_str = fx_string_create();
if (value) {
fx_string_append_cstr(value_str, value);
}
fx_hashtable_put(
env,
&FX_VALUE_OBJECT(name_str),
&FX_VALUE_OBJECT(value_str));
fx_string_unref(name_str);
fx_string_unref(value_str);
free(envstr);
}
proc->proc_environment = env;
fx_register_global(self);
return self;
}
fx_status fx_process_start(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_start,
proc);
}
fx_status fx_process_wait(fx_process *proc, int *out_result)
{
FX_CLASS_DISPATCH_STATIC(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_wait,
proc,
out_result);
}
fx_status fx_process_kill(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_kill,
proc);
}
const fx_hashtable *fx_process_get_environment(const fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_environment,
proc);
}
fx_stream *fx_process_get_stdin(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_stdin,
proc);
}
fx_stream *fx_process_get_stdout(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_stdout,
proc);
}
fx_stream *fx_process_get_stderr(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_get_stderr,
proc);
}
void fx_process_close_stdin(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_V0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_close_stdin,
proc);
}
void fx_process_close_stdout(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_V0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_close_stdout,
proc);
}
void fx_process_close_stderr(fx_process *proc)
{
FX_CLASS_DISPATCH_STATIC_V0(
FX_DIAGNOSTICS_TYPE_PROCESS,
process_close_stderr,
proc);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void process_init(fx_object *obj, void *priv)
{
}
static void process_fini(fx_object *obj, void *priv)
{
struct fx_process_p *proc = priv;
fx_array_unref(proc->proc_args);
fx_iostream_unref(proc->proc_stdin);
fx_iostream_unref(proc->proc_stdout);
fx_iostream_unref(proc->proc_stderr);
fx_string_unref(proc->proc_exec_path);
fx_hashtable_unref(proc->proc_environment);
}
/*** ITERATOR DEFINITION
* ******************************************************/
static enum fx_status process_iterator_move_next(const fx_iterator *obj)
{
return FX_ERR_NO_DATA;
}
static const fx_value *process_iterator_get_value(const fx_iterator *obj)
{
return NULL;
}
/*** CLASS DEFINITION
* *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_process)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_END(fx_process)
FX_TYPE_DEFINITION_BEGIN(fx_process)
FX_TYPE_ID(0x7334594f, 0xa1c3, 0x4715, 0xab69, 0x68a709e1b64b);
FX_TYPE_NAME("fx.diagnostics.process");
FX_TYPE_CLASS(fx_process_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_process_p);
FX_TYPE_INSTANCE_INIT(process_init);
FX_TYPE_INSTANCE_FINI(process_fini);
FX_TYPE_DEFINITION_END(fx_process)
FX_TYPE_CLASS_BEGIN(fx_process_iterator)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_iterator, FX_TYPE_ITERATOR)
FX_INTERFACE_ENTRY(it_move_next) = process_iterator_move_next;
FX_INTERFACE_ENTRY(it_get_value) = process_iterator_get_value;
FX_TYPE_VTABLE_INTERFACE_END(fx_iterator, FX_TYPE_ITERATOR)
FX_TYPE_CLASS_END(fx_process_iterator)
FX_TYPE_DEFINITION_BEGIN(fx_process_iterator)
FX_TYPE_ID(0x67eb13b6, 0x25d1, 0x424a, 0xb136, 0x871a07829089);
FX_TYPE_NAME("fx.diagnostics.process.iterator");
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_process_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_process_iterator_p);
FX_TYPE_DEFINITION_END(fx_process_iterator)
-63
View File
@@ -1,63 +0,0 @@
#include <fx/diagnostics/process.h>
int main(int argc, const char **argv)
{
if (argc < 3) {
return -1;
}
const char *exec = argv[1];
const char *msg = argv[2];
const char *args[] = {
"hello",
"world",
};
fx_process *self = fx_process_get_self();
const fx_hashtable *env = fx_process_get_environment(self);
fx_process_start_info info = {
.proc_args = args,
.proc_args_count = sizeof args / sizeof args[0],
.proc_redirect_stdout = true,
.proc_redirect_stdin = true,
.proc_file_name = exec,
.proc_environment = env,
};
fx_process *process = fx_process_create(&info);
if (!process) {
fprintf(stderr, "Process creation failed\n");
return -1;
}
fx_status status = fx_process_start(process);
if (!FX_OK(status)) {
fprintf(stderr,
"Process creation failed: %s\n",
fx_status_description(status));
return -1;
}
printf("process stdin: %s\n", msg);
fx_stream *in = fx_process_get_stdin(process);
fx_stream_write_cstr(in, msg, NULL);
fx_stream_write_char(in, '\n');
fx_process_close_stdin(process);
fx_stream *out = fx_process_get_stdout(process);
printf("process stdout:\n");
printf("---------------------\n");
while (FX_OK(fx_stream_read_line_s(out, fx_stdout))) { }
printf("---------------------\n");
int result;
fx_process_wait(process, &result);
fx_process_unref(process);
return 0;
}
+4 -11
View File
@@ -18,25 +18,18 @@ FX_TYPE_CLASS_DECLARATION_END(fx_path)
#define FX_RV_PATH(cstr) FX_RV(fx_path_create_from_cstr(cstr)) #define FX_RV_PATH(cstr) FX_RV(fx_path_create_from_cstr(cstr))
typedef enum fx_system_path {
FX_PATH_ROOT = 0,
FX_PATH_CWD,
FX_PATH_HOME,
FX_PATH_CONFIG,
} fx_system_path;
struct fx_file_info; struct fx_file_info;
FX_API fx_type_id fx_path_get_type(void); FX_API fx_type_id fx_path_get_type(void);
FX_TYPE_DEFAULT_CONSTRUCTOR(fx_path, FX_TYPE_PATH); FX_TYPE_DEFAULT_CONSTRUCTOR(fx_path, FX_TYPE_PATH);
FX_API fx_path *fx_path_create_root();
FX_API fx_path *fx_path_create_cwd();
FX_API fx_path *fx_path_create_from_cstr(const char *path); FX_API fx_path *fx_path_create_from_cstr(const char *path);
FX_API fx_path *fx_path_get_system(fx_system_path path_type);
FX_API fx_path *fx_path_duplicate(const fx_path *path); FX_API fx_path *fx_path_duplicate(const fx_path *path);
FX_API fx_path *fx_path_join_array(const fx_value *values[], size_t nr_values); FX_API fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths);
FX_API fx_path *fx_path_join_list(size_t count, ...);
FX_API fx_path *fx_path_make_absolute(const fx_path *in); FX_API fx_path *fx_path_make_absolute(const fx_path *in);
FX_API fx_path *fx_path_make_relative(const fx_path *in); FX_API fx_path *fx_path_make_relative(const fx_path *in);
@@ -56,7 +49,7 @@ FX_API enum fx_status fx_path_get_filename(
const fx_path *path, const fx_path *path,
fx_string *out_name); fx_string *out_name);
FX_API const char *fx_path_get_cstr(const fx_path *path); FX_API const char *fx_path_ptr(const fx_path *path);
FX_API size_t fx_path_length(const fx_path *path); FX_API size_t fx_path_length(const fx_path *path);
FX_DECLS_END; FX_DECLS_END;
-8
View File
@@ -1,8 +0,0 @@
#ifndef FX_IO_PIPE_H_
#define FX_IO_PIPE_H_
#include <fx/io/stream.h>
FX_API fx_status fx_pipe_create(fx_iostream **reader, fx_iostream **writer);
#endif
-29
View File
@@ -1,29 +0,0 @@
#ifndef FX_IO_STREAM_H_
#define FX_IO_STREAM_H_
#include <fx/int.h>
#include <fx/macros.h>
#include <fx/stream.h>
FX_DECLS_BEGIN;
#define FX_IO_TYPE_STREAM (fx_iostream_get_type())
FX_DECLARE_TYPE(fx_iostream);
FX_TYPE_CLASS_DECLARATION_BEGIN(fx_iostream)
FX_TYPE_CLASS_DECLARATION_END(fx_iostream)
FX_API fx_type_id fx_iostream_get_type(void);
FX_API fx_iostream *fx_iostream_create(
fx_stream_mode mode,
uptr os_handle,
bool close_handle_on_release);
FX_API uptr fx_iostream_get_os_handle(const fx_iostream *stream);
FX_API uptr fx_iostream_steal_os_handle(fx_iostream *stream);
FX_DECLS_END;
#endif
+4 -4
View File
@@ -51,12 +51,12 @@ static const fx_path *directory_get_rel_path(const struct fx_directory_p *dir)
static const char *directory_get_path_cstr(const struct fx_directory_p *dir) static const char *directory_get_path_cstr(const struct fx_directory_p *dir)
{ {
return fx_path_get_cstr(dir->d_path_abs); return fx_path_ptr(dir->d_path_abs);
} }
static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir) static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir)
{ {
return fx_path_get_cstr(dir->d_path_rel); return fx_path_ptr(dir->d_path_rel);
} }
static fx_result directory_delete( static fx_result directory_delete(
@@ -263,7 +263,7 @@ static fx_result directory_open(
int root_fd = root ? root->d_fd : AT_FDCWD; int root_fd = root ? root->d_fd : AT_FDCWD;
const char *path_cstr = fx_path_get_cstr(path); const char *path_cstr = fx_path_ptr(path);
if (root) { if (root) {
while (*path_cstr == '/') { while (*path_cstr == '/') {
path_cstr++; path_cstr++;
@@ -571,7 +571,7 @@ fx_iterator *fx_directory_begin(
int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR; int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR;
const char *path_list[] = { const char *path_list[] = {
fx_path_get_cstr(it->_p->d_path_abs), fx_path_ptr(it->_p->d_path_abs),
NULL, NULL,
}; };
+5 -5
View File
@@ -248,11 +248,11 @@ static enum fx_status file_swap_shadow(
int err; int err;
err = rename(fx_path_get_cstr(main_file->path), fx_path_get_cstr(tmp_path)); err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path));
err = rename( err = rename(
fx_path_get_cstr(shadow_file->path), fx_path_ptr(shadow_file->path),
fx_path_get_cstr(main_file->path)); fx_path_ptr(main_file->path));
err = rename(fx_path_get_cstr(tmp_path), fx_path_get_cstr(shadow_file->path)); err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path));
fx_path_unref(tmp_path); fx_path_unref(tmp_path);
@@ -441,7 +441,7 @@ fx_result fx_file_open(
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
int fd = open(fx_path_get_cstr(abs_path), flags, 0644); int fd = open(fx_path_ptr(abs_path), flags, 0644);
if (fd == -1) { if (fd == -1) {
fx_path_unref(abs_path); fx_path_unref(abs_path);
return FX_RESULT_STATUS( return FX_RESULT_STATUS(
+42 -55
View File
@@ -41,7 +41,7 @@ static bool path_is_absolute(const struct fx_path_p *path)
return s[0] == '/'; return s[0] == '/';
} }
static const char *path_get_cstr(const struct fx_path_p *path) static const char *path_ptr(const struct fx_path_p *path)
{ {
return fx_string_get_cstr(path->p_pathstr); return fx_string_get_cstr(path->p_pathstr);
} }
@@ -51,7 +51,7 @@ static enum fx_status path_stat(
struct fx_file_info *out) struct fx_file_info *out)
{ {
struct stat st; struct stat st;
int err = stat(path_get_cstr(path), &st); int err = stat(path_ptr(path), &st);
if (err != 0) { if (err != 0) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE); return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
@@ -135,8 +135,8 @@ static enum fx_status path_get_directory(
size_t dir_path_len = (size_t)(sep - path_cstr); size_t dir_path_len = (size_t)(sep - path_cstr);
fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len); fx_string *dir_path_s = fx_string_get_substr(path_str, 0, dir_path_len);
fx_path *dir_path fx_path *dir_path = fx_path_create_from_cstr(
= fx_path_create_from_cstr(fx_string_get_cstr(dir_path_s)); fx_string_get_cstr(dir_path_s));
fx_string_unref(dir_path_s); fx_string_unref(dir_path_s);
*out_dir_path = dir_path; *out_dir_path = dir_path;
@@ -200,6 +200,33 @@ fx_path *fx_path_create_root()
return path; return path;
} }
fx_path *fx_path_create_cwd()
{
const long buf_len = 2048;
char *buf = malloc(buf_len);
if (!buf) {
return NULL;
}
if (!getcwd(buf, buf_len)) {
free(buf);
return NULL;
}
fx_path *path = fx_path_create();
if (!path) {
free(buf);
return NULL;
}
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
fx_string_append_cstr(p->p_pathstr, buf);
free(buf);
return path;
}
fx_path *fx_path_create_from_cstr(const char *cstr) fx_path *fx_path_create_from_cstr(const char *cstr)
{ {
fx_path *path = fx_path_create(); fx_path *path = fx_path_create();
@@ -232,49 +259,6 @@ fx_path *fx_path_create_from_cstr(const char *cstr)
return path; return path;
} }
fx_path *fx_path_get_system(fx_system_path path_type)
{
char path_buf[4096];
const char *path_value = NULL, *default_value = NULL;
switch (path_type) {
case FX_PATH_ROOT:
path_value = "/";
break;
case FX_PATH_CWD:
if (getcwd(path_buf, sizeof path_buf) == NULL) {
path_value = path_buf;
}
break;
case FX_PATH_HOME:
path_value = getenv("HOME");
default_value = "~";
break;
case FX_PATH_CONFIG: {
const char *home = getenv("HOME");
path_value = getenv("XDG_CONFIG_HOME");
snprintf(
path_buf,
sizeof path_buf,
"%s/.config",
home ? home : "~");
default_value = path_buf;
break;
}
default:
return NULL;
}
if (path_value) {
return fx_path_create_from_cstr(path_value);
}
if (default_value) {
return fx_path_create_from_cstr(default_value);
}
return NULL;
}
fx_path *fx_path_duplicate(const fx_path *path) fx_path *fx_path_duplicate(const fx_path *path)
{ {
fx_path *new_path = fx_path_create(); fx_path *new_path = fx_path_create();
@@ -301,13 +285,15 @@ fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
return NULL; return NULL;
} }
struct fx_path_p *result_p struct fx_path_p *result_p = fx_object_get_private(
= fx_object_get_private(result, FX_TYPE_PATH); result,
FX_TYPE_PATH);
for (size_t i = 0; i < nr_paths; i++) { for (size_t i = 0; i < nr_paths; i++) {
if (paths[i]) { if (paths[i]) {
struct fx_path_p *path_p struct fx_path_p *path_p = fx_object_get_private(
= fx_object_get_private(paths[i], FX_TYPE_PATH); paths[i],
FX_TYPE_PATH);
append_path(result_p, path_p); append_path(result_p, path_p);
} }
} }
@@ -380,9 +366,9 @@ enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
out_name); out_name);
} }
const char *fx_path_get_cstr(const fx_path *path) const char *fx_path_ptr(const fx_path *path)
{ {
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_get_cstr, path); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path);
} }
size_t fx_path_length(const fx_path *path) size_t fx_path_length(const fx_path *path)
@@ -414,8 +400,9 @@ static fx_status path_to_string(
fx_stream *out, fx_stream *out,
const char *format) const char *format)
{ {
struct fx_path_p *path struct fx_path_p *path = fx_object_get_private(
= fx_object_get_private(obj->v_object, FX_TYPE_PATH); obj->v_object,
FX_TYPE_PATH);
fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL); fx_stream_write_cstr(out, fx_string_get_cstr(path->p_pathstr), NULL);
return FX_SUCCESS; return FX_SUCCESS;
-29
View File
@@ -1,29 +0,0 @@
#include "posix.h"
#include <errno.h>
#include <fx/io/stream.h>
#include <unistd.h>
fx_status fx_pipe_create(fx_iostream **reader, fx_iostream **writer)
{
int fds[2];
int err = pipe(fds);
if (err != 0) {
return fx_status_from_errno(errno, FX_ERR_NOT_SUPPORTED);
}
fx_iostream *r = fx_iostream_create(FX_STREAM_READ, fds[0], true);
fx_iostream *w = fx_iostream_create(FX_STREAM_WRITE, fds[1], true);
if (!r || !w) {
fx_iostream_unref(r);
fx_iostream_unref(w);
close(fds[0]);
close(fds[1]);
return FX_ERR_NO_MEMORY;
}
*reader = r;
*writer = w;
return FX_SUCCESS;
}
-149
View File
@@ -1,149 +0,0 @@
#include "posix.h"
#include <errno.h>
#include <fx/io/stream.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*** PRIVATE DATA *************************************************************/
struct fx_iostream_p {
int s_fd;
bool s_fd_close_on_release;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static uptr iostream_get_os_handle(const struct fx_iostream_p *stream)
{
return stream->s_fd;
}
static uptr iostream_steal_os_handle(struct fx_iostream_p *stream)
{
uptr out = stream->s_fd;
stream->s_fd = -1;
stream->s_fd_close_on_release = false;
return out;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_iostream *fx_iostream_create(
fx_stream_mode mode,
uptr os_handle,
bool close_handle_on_release)
{
fx_iostream *s = fx_object_create(FX_IO_TYPE_STREAM);
if (!s) {
return NULL;
}
fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM);
struct fx_iostream_p *p = fx_object_get_private(s, FX_IO_TYPE_STREAM);
cfg->s_mode = mode | Z__FX_STREAM_STATIC;
p->s_fd = (int)os_handle;
p->s_fd_close_on_release = close_handle_on_release;
return s;
}
uptr fx_iostream_get_os_handle(const fx_iostream *stream)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_IO_TYPE_STREAM,
iostream_get_os_handle,
stream);
}
uptr fx_iostream_steal_os_handle(fx_iostream *stream)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_IO_TYPE_STREAM,
iostream_steal_os_handle,
stream);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void iostream_init(fx_object *obj, void *priv)
{
struct fx_iostream_p *stream = priv;
}
static void iostream_fini(fx_object *obj, void *priv)
{
struct fx_iostream_p *stream = priv;
if (stream->s_fd_close_on_release) {
close(stream->s_fd);
}
}
enum fx_status stream_read(
fx_stream *stream,
void *buf,
size_t count,
size_t *nr_read)
{
struct fx_iostream_p *s
= fx_object_get_private(stream, FX_IO_TYPE_STREAM);
long r = read(s->s_fd, buf, count);
if (r < 0) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*nr_read = r;
return FX_SUCCESS;
}
enum fx_status stream_write(
fx_stream *stream,
const void *buf,
size_t count,
size_t *nr_written)
{
struct fx_iostream_p *s
= fx_object_get_private(stream, FX_IO_TYPE_STREAM);
long w = write(s->s_fd, buf, count);
if (w < 0) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*nr_written = w;
return FX_SUCCESS;
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_iostream)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_INTERFACE_ENTRY(s_close) = NULL;
FX_INTERFACE_ENTRY(s_seek) = NULL;
FX_INTERFACE_ENTRY(s_tell) = NULL;
FX_INTERFACE_ENTRY(s_getc) = NULL;
FX_INTERFACE_ENTRY(s_read) = stream_read;
FX_INTERFACE_ENTRY(s_write) = stream_write;
FX_INTERFACE_ENTRY(s_reserve) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_END(fx_iostream)
FX_TYPE_DEFINITION_BEGIN(fx_iostream)
FX_TYPE_ID(0xc0b1c3c9, 0xa9c6, 0x4910, 0x8786, 0x574b0696e7aa);
FX_TYPE_NAME("fx.io.stream");
FX_TYPE_EXTENDS(FX_TYPE_STREAM);
FX_TYPE_CLASS(fx_iostream_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_iostream_p);
FX_TYPE_INSTANCE_INIT(iostream_init);
FX_TYPE_INSTANCE_FINI(iostream_fini);
FX_TYPE_DEFINITION_END(fx_iostream)
+29 -35
View File
@@ -53,12 +53,12 @@ static const fx_path *directory_get_rel_path(const struct fx_directory_p *dir)
static const char *directory_get_path_cstr(const struct fx_directory_p *dir) static const char *directory_get_path_cstr(const struct fx_directory_p *dir)
{ {
return fx_path_get_cstr(dir->d_path_abs); return fx_path_ptr(dir->d_path_abs);
} }
static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir) static const char *directory_get_rel_path_cstr(const struct fx_directory_p *dir)
{ {
return fx_path_get_cstr(dir->d_path_rel); return fx_path_ptr(dir->d_path_rel);
} }
static fx_result directory_delete( static fx_result directory_delete(
@@ -90,13 +90,12 @@ static bool directory_path_exists(
const struct fx_directory_p *root, const struct fx_directory_p *root,
const fx_path *path) const fx_path *path)
{ {
const fx_value *parts[] = { const fx_path *parts[] = {
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL), root ? root->d_path_abs : NULL,
&FX_VALUE_OBJECT(path), path,
}; };
fx_path *abs_path fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) { if (!abs_path) {
return false; return false;
} }
@@ -111,13 +110,12 @@ static bool directory_path_is_file(
const struct fx_directory_p *root, const struct fx_directory_p *root,
const fx_path *path) const fx_path *path)
{ {
const fx_value *parts[] = { const fx_path *parts[] = {
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL), root ? root->d_path_abs : NULL,
&FX_VALUE_OBJECT(path), path,
}; };
fx_path *abs_path fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) { if (!abs_path) {
return false; return false;
} }
@@ -132,13 +130,12 @@ static bool directory_path_is_directory(
const struct fx_directory_p *root, const struct fx_directory_p *root,
const fx_path *path) const fx_path *path)
{ {
const fx_value *parts[] = { const fx_path *parts[] = {
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL), root ? root->d_path_abs : NULL,
&FX_VALUE_OBJECT(path), path,
}; };
fx_path *abs_path fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) { if (!abs_path) {
return false; return false;
} }
@@ -154,13 +151,12 @@ static fx_result directory_path_stat(
const fx_path *path, const fx_path *path,
struct fx_file_info *out) struct fx_file_info *out)
{ {
const fx_value *parts[] = { const fx_path *parts[] = {
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL), root ? root->d_path_abs : NULL,
&FX_VALUE_OBJECT(path), path,
}; };
fx_path *abs_path fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) { if (!abs_path) {
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
@@ -175,13 +171,12 @@ static fx_result directory_path_unlink(
const struct fx_directory_p *root, const struct fx_directory_p *root,
const fx_path *path) const fx_path *path)
{ {
const fx_value *parts[] = { const fx_path *parts[] = {
&FX_VALUE_OBJECT(root ? root->d_path_abs : NULL), root ? root->d_path_abs : NULL,
&FX_VALUE_OBJECT(path), path,
}; };
fx_path *abs_path fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!abs_path) { if (!abs_path) {
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
@@ -269,7 +264,7 @@ static fx_result directory_open(
int root_fd = root ? root->d_fd : AT_FDCWD; int root_fd = root ? root->d_fd : AT_FDCWD;
const char *path_cstr = fx_path_get_cstr(path); const char *path_cstr = fx_path_ptr(path);
if (root) { if (root) {
while (*path_cstr == '/') { while (*path_cstr == '/') {
path_cstr++; path_cstr++;
@@ -303,16 +298,15 @@ static fx_result directory_open(
= fx_object_get_private(dir, FX_TYPE_DIRECTORY); = fx_object_get_private(dir, FX_TYPE_DIRECTORY);
if (!root) { if (!root) {
cwd = fx_path_get_system(FX_PATH_CWD); cwd = fx_path_create_cwd();
} }
const fx_value *parts[] = { const fx_path *parts[] = {
&FX_VALUE_OBJECT(root ? root->d_path_abs : cwd), root ? root->d_path_abs : cwd,
&FX_VALUE_OBJECT(path), path,
}; };
fx_path *new_path fx_path *new_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (!new_path) { if (!new_path) {
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
@@ -576,7 +570,7 @@ fx_iterator *fx_directory_begin(
int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR; int fts_flags = FTS_COMFOLLOW | FTS_NOCHDIR;
const char *path_list[] = { const char *path_list[] = {
fx_path_get_cstr(it->_p->d_path_abs), fx_path_ptr(it->_p->d_path_abs),
NULL, NULL,
}; };
+18 -25
View File
@@ -86,13 +86,13 @@ static fx_result file_open_shadow(
= fx_path_create_from_cstr(fx_string_get_cstr(filename)); = fx_path_create_from_cstr(fx_string_get_cstr(filename));
fx_string_unref(filename); fx_string_unref(filename);
const fx_value *parts[] = { const fx_path *parts[] = {
&FX_VALUE_OBJECT(dir), dir,
&FX_VALUE_OBJECT(shadow_filename), shadow_filename,
}; };
fx_path *shadow_filepath fx_path *shadow_filepath
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]); = fx_path_join(parts, sizeof parts / sizeof parts[0]);
fx_path_unref(dir); fx_path_unref(dir);
fx_path_unref(shadow_filename); fx_path_unref(shadow_filename);
@@ -229,14 +229,12 @@ static enum fx_status file_swap_shadow(
z__fx_io_generate_tmp_filename(tmp_name, sizeof tmp_name); z__fx_io_generate_tmp_filename(tmp_name, sizeof tmp_name);
fx_path *tmp_name_p = fx_path_create_from_cstr(tmp_name); fx_path *tmp_name_p = fx_path_create_from_cstr(tmp_name);
const fx_value *parts[] = { const fx_path *parts[] = {
&FX_VALUE_OBJECT(dir_path), dir_path,
&FX_VALUE_OBJECT(tmp_name_p), tmp_name_p,
}; };
tmp_path = fx_path_join_array( tmp_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
parts,
sizeof parts / sizeof parts[0]);
fx_path_unref(tmp_name_p); fx_path_unref(tmp_name_p);
@@ -252,15 +250,11 @@ static enum fx_status file_swap_shadow(
int err; int err;
err = rename(fx_path_ptr(main_file->path), fx_path_ptr(tmp_path));
err = rename( err = rename(
fx_path_get_cstr(main_file->path), fx_path_ptr(shadow_file->path),
fx_path_get_cstr(tmp_path)); fx_path_ptr(main_file->path));
err = rename( err = rename(fx_path_ptr(tmp_path), fx_path_ptr(shadow_file->path));
fx_path_get_cstr(shadow_file->path),
fx_path_get_cstr(main_file->path));
err = rename(
fx_path_get_cstr(tmp_path),
fx_path_get_cstr(shadow_file->path));
fx_path_unref(tmp_path); fx_path_unref(tmp_path);
@@ -430,17 +424,16 @@ fx_result fx_file_open(
if (root) { if (root) {
root_path = fx_directory_get_path(root); root_path = fx_directory_get_path(root);
} else { } else {
root_path = fx_path_get_system(FX_PATH_CWD); root_path = fx_path_create_cwd();
free_root_path = true; free_root_path = true;
} }
const fx_value *parts[] = { const fx_path *parts[] = {
&FX_VALUE_OBJECT(root_path), root_path,
&FX_VALUE_OBJECT(file_path), file_path,
}; };
fx_path *abs_path fx_path *abs_path = fx_path_join(parts, sizeof parts / sizeof parts[0]);
= fx_path_join_array(parts, sizeof parts / sizeof parts[0]);
if (free_root_path) { if (free_root_path) {
fx_path_unref((fx_path *)root_path); fx_path_unref((fx_path *)root_path);
@@ -450,7 +443,7 @@ fx_result fx_file_open(
return FX_RESULT_ERR(NO_MEMORY); return FX_RESULT_ERR(NO_MEMORY);
} }
int fd = open(fx_path_get_cstr(abs_path), flags, 0644); int fd = open(fx_path_ptr(abs_path), flags, 0644);
if (fd == -1) { if (fd == -1) {
fx_path_unref(abs_path); fx_path_unref(abs_path);
return FX_RESULT_STATUS( return FX_RESULT_STATUS(
+41 -96
View File
@@ -91,22 +91,22 @@ static bool path_is_directory(const struct fx_path_p *path)
return (info.attrib & FX_FILE_ATTRIB_DIRECTORY) != 0; return (info.attrib & FX_FILE_ATTRIB_DIRECTORY) != 0;
} }
static void append_path(struct fx_path_p *dest, const fx_string *src) static void append_path(struct fx_path_p *dest, const struct fx_path_p *src)
{ {
const char *src_cstr = fx_string_get_cstr(src); if (path_is_absolute(src)) {
if (src_cstr[0] == '/') {
fx_string_clear(dest->p_pathstr); fx_string_clear(dest->p_pathstr);
fx_string_append_s(dest->p_pathstr, src); fx_string_append_s(dest->p_pathstr, src->p_pathstr);
return; return;
} }
if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0 if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0
&& fx_string_get_last_char(dest->p_pathstr) != '/' && fx_string_get_last_char(dest->p_pathstr) != '/'
&& fx_string_get_first_char(src) != '/') { && fx_string_get_first_char(src->p_pathstr) != '/') {
fx_string_append_c(dest->p_pathstr, '/'); char s[] = {'/', 0};
fx_string_append_cstr(dest->p_pathstr, s);
} }
fx_string_append_s(dest->p_pathstr, src); fx_string_append_s(dest->p_pathstr, src->p_pathstr);
} }
static enum fx_status path_unlink(const struct fx_path_p *path) static enum fx_status path_unlink(const struct fx_path_p *path)
@@ -199,6 +199,33 @@ fx_path *fx_path_create_root()
return path; return path;
} }
fx_path *fx_path_create_cwd()
{
const long buf_len = 2048;
char *buf = malloc(buf_len);
if (!buf) {
return NULL;
}
if (!getcwd(buf, buf_len)) {
free(buf);
return NULL;
}
fx_path *path = fx_path_create();
if (!path) {
free(buf);
return NULL;
}
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
fx_string_append_cstr(p->p_pathstr, buf);
free(buf);
return path;
}
fx_path *fx_path_create_from_cstr(const char *cstr) fx_path *fx_path_create_from_cstr(const char *cstr)
{ {
fx_path *path = fx_path_create(); fx_path *path = fx_path_create();
@@ -231,49 +258,6 @@ fx_path *fx_path_create_from_cstr(const char *cstr)
return path; return path;
} }
fx_path *fx_path_get_system(fx_system_path path_type)
{
char path_buf[4096];
const char *path_value = NULL, *default_value = NULL;
switch (path_type) {
case FX_PATH_ROOT:
path_value = "/";
break;
case FX_PATH_CWD:
if (getcwd(path_buf, sizeof path_buf) == NULL) {
path_value = path_buf;
}
break;
case FX_PATH_HOME:
path_value = getenv("HOME");
default_value = "~";
break;
case FX_PATH_CONFIG: {
const char *home = getenv("HOME");
path_value = getenv("XDG_CONFIG_HOME");
snprintf(
path_buf,
sizeof path_buf,
"%s/.config",
home ? home : "~");
default_value = path_buf;
break;
}
default:
return NULL;
}
if (path_value) {
return fx_path_create_from_cstr(path_value);
}
if (default_value) {
return fx_path_create_from_cstr(default_value);
}
return NULL;
}
fx_path *fx_path_duplicate(const fx_path *path) fx_path *fx_path_duplicate(const fx_path *path)
{ {
fx_path *new_path = fx_path_create(); fx_path *new_path = fx_path_create();
@@ -293,7 +277,7 @@ fx_path *fx_path_duplicate(const fx_path *path)
return new_path; return new_path;
} }
fx_path *fx_path_join_array(const fx_value *values[], size_t nr_values) fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
{ {
fx_path *result = fx_path_create(); fx_path *result = fx_path_create();
if (!result) { if (!result) {
@@ -303,53 +287,14 @@ fx_path *fx_path_join_array(const fx_value *values[], size_t nr_values)
struct fx_path_p *result_p struct fx_path_p *result_p
= fx_object_get_private(result, FX_TYPE_PATH); = fx_object_get_private(result, FX_TYPE_PATH);
fx_stringstream *tmp = fx_stringstream_create(); for (size_t i = 0; i < nr_paths; i++) {
fx_string *value_s = fx_string_create(); if (paths[i]) {
for (size_t i = 0; i < nr_values; i++) { struct fx_path_p *path_p
if (!values[i]) { = fx_object_get_private(paths[i], FX_TYPE_PATH);
continue; append_path(result_p, path_p);
} }
fx_stringstream_reset(tmp);
fx_value_to_string(values[i], tmp, NULL);
fx_string_clear(value_s);
fx_string_append_cstr(value_s, fx_stringstream_get_cstr(tmp));
append_path(result_p, value_s);
} }
fx_string_unref(value_s);
fx_stringstream_unref(tmp);
return result;
}
fx_path *fx_path_join_list(size_t count, ...)
{
fx_path *result = fx_path_create();
if (!result) {
return NULL;
}
struct fx_path_p *result_p
= fx_object_get_private(result, FX_TYPE_PATH);
va_list arg;
va_start(arg, count);
fx_stringstream *tmp = fx_stringstream_create();
fx_string *value_s = fx_string_create();
for (size_t i = 0; i < count; i++) {
const fx_value *value = va_arg(arg, const fx_value *);
fx_stringstream_reset(tmp);
fx_value_to_string(value, tmp, NULL);
fx_string_clear(value_s);
fx_string_append_cstr(value_s, fx_stringstream_get_cstr(tmp));
append_path(result_p, value_s);
}
fx_string_unref(value_s);
fx_stringstream_unref(tmp);
return result; return result;
} }
@@ -418,7 +363,7 @@ enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
out_name); out_name);
} }
const char *fx_path_get_cstr(const fx_path *path) const char *fx_path_ptr(const fx_path *path)
{ {
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path);
} }
-29
View File
@@ -1,29 +0,0 @@
#include "posix.h"
#include <errno.h>
#include <fx/io/stream.h>
#include <unistd.h>
fx_status fx_pipe_create(fx_iostream **reader, fx_iostream **writer)
{
int fds[2];
int err = pipe(fds);
if (err != 0) {
return fx_status_from_errno(errno, FX_ERR_NOT_SUPPORTED);
}
fx_iostream *r = fx_iostream_create(FX_STREAM_READ, fds[0], true);
fx_iostream *w = fx_iostream_create(FX_STREAM_WRITE, fds[1], true);
if (!r || !w) {
fx_iostream_unref(r);
fx_iostream_unref(w);
close(fds[0]);
close(fds[1]);
return FX_ERR_NO_MEMORY;
}
*reader = r;
*writer = w;
return FX_SUCCESS;
}
-149
View File
@@ -1,149 +0,0 @@
#include "posix.h"
#include <errno.h>
#include <fx/io/stream.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*** PRIVATE DATA *************************************************************/
struct fx_iostream_p {
int s_fd;
bool s_fd_close_on_release;
};
/*** PRIVATE FUNCTIONS ********************************************************/
static uptr iostream_get_os_handle(const struct fx_iostream_p *stream)
{
return stream->s_fd;
}
static uptr iostream_steal_os_handle(struct fx_iostream_p *stream)
{
uptr out = stream->s_fd;
stream->s_fd = -1;
stream->s_fd_close_on_release = false;
return out;
}
/*** PUBLIC FUNCTIONS *********************************************************/
fx_iostream *fx_iostream_create(
fx_stream_mode mode,
uptr os_handle,
bool close_handle_on_release)
{
fx_iostream *s = fx_object_create(FX_IO_TYPE_STREAM);
if (!s) {
return NULL;
}
fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM);
struct fx_iostream_p *p = fx_object_get_private(s, FX_IO_TYPE_STREAM);
cfg->s_mode = mode | Z__FX_STREAM_STATIC;
p->s_fd = (int)os_handle;
p->s_fd_close_on_release = close_handle_on_release;
return s;
}
uptr fx_iostream_get_os_handle(const fx_iostream *stream)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_IO_TYPE_STREAM,
iostream_get_os_handle,
stream);
}
uptr fx_iostream_steal_os_handle(fx_iostream *stream)
{
FX_CLASS_DISPATCH_STATIC_0(
FX_IO_TYPE_STREAM,
iostream_steal_os_handle,
stream);
}
/*** VIRTUAL FUNCTIONS ********************************************************/
static void iostream_init(fx_object *obj, void *priv)
{
struct fx_iostream_p *stream = priv;
}
static void iostream_fini(fx_object *obj, void *priv)
{
struct fx_iostream_p *stream = priv;
if (stream->s_fd_close_on_release) {
close(stream->s_fd);
}
}
static enum fx_status stream_read(
fx_stream *stream,
void *buf,
size_t count,
size_t *nr_read)
{
struct fx_iostream_p *s
= fx_object_get_private(stream, FX_IO_TYPE_STREAM);
long r = read(s->s_fd, buf, count);
if (r < 0) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*nr_read = r;
return FX_SUCCESS;
}
static enum fx_status stream_write(
fx_stream *stream,
const void *buf,
size_t count,
size_t *nr_written)
{
struct fx_iostream_p *s
= fx_object_get_private(stream, FX_IO_TYPE_STREAM);
long w = write(s->s_fd, buf, count);
if (w < 0) {
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
*nr_written = w;
return FX_SUCCESS;
}
/*** CLASS DEFINITION *********************************************************/
FX_TYPE_CLASS_BEGIN(fx_iostream)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_stream, FX_TYPE_STREAM)
FX_INTERFACE_ENTRY(s_close) = NULL;
FX_INTERFACE_ENTRY(s_seek) = NULL;
FX_INTERFACE_ENTRY(s_tell) = NULL;
FX_INTERFACE_ENTRY(s_getc) = NULL;
FX_INTERFACE_ENTRY(s_read) = stream_read;
FX_INTERFACE_ENTRY(s_write) = stream_write;
FX_INTERFACE_ENTRY(s_reserve) = NULL;
FX_TYPE_VTABLE_INTERFACE_END(fx_stream, FX_TYPE_STREAM)
FX_TYPE_CLASS_END(fx_iostream)
FX_TYPE_DEFINITION_BEGIN(fx_iostream)
FX_TYPE_ID(0xc0b1c3c9, 0xa9c6, 0x4910, 0x8786, 0x574b0696e7aa);
FX_TYPE_NAME("fx.io.stream");
FX_TYPE_EXTENDS(FX_TYPE_STREAM);
FX_TYPE_CLASS(fx_iostream_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_iostream_p);
FX_TYPE_INSTANCE_INIT(iostream_init);
FX_TYPE_INSTANCE_FINI(iostream_fini);
FX_TYPE_DEFINITION_END(fx_iostream)
+3 -3
View File
@@ -60,7 +60,7 @@ enum fx_status fx_directory_open(
} }
HANDLE dir_handle = CreateFileA( HANDLE dir_handle = CreateFileA(
fx_path_get_cstr(new_path), GENERIC_READ, fx_path_ptr(new_path), GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, INVALID_HANDLE_VALUE); OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, INVALID_HANDLE_VALUE);
@@ -197,7 +197,7 @@ static bool move_into_directory(struct fx_directory_iterator *it, const char *di
state = push_iteration_state(it->_z); state = push_iteration_state(it->_z);
state->search_path = dir_path; state->search_path = dir_path;
state->search = FindFirstFileA(fx_path_get_cstr(search_path), &state->data); state->search = FindFirstFileA(fx_path_ptr(search_path), &state->data);
fx_path_release(search_path); fx_path_release(search_path);
fx_path_release(wildcard); fx_path_release(wildcard);
@@ -224,7 +224,7 @@ static bool move_into_directory(struct fx_directory_iterator *it, const char *di
static bool move_to_first_item( static bool move_to_first_item(
struct fx_directory_iterator *it, const struct fx_path *root_dir) struct fx_directory_iterator *it, const struct fx_path *root_dir)
{ {
bool has_results = move_into_directory(it, fx_path_get_cstr(root_dir)); bool has_results = move_into_directory(it, fx_path_ptr(root_dir));
if (!has_results) { if (!has_results) {
return false; return false;
+4 -4
View File
@@ -205,13 +205,13 @@ bool fx_path_is_absolute(const struct fx_path *path)
bool fx_path_exists(const struct fx_path *path) bool fx_path_exists(const struct fx_path *path)
{ {
DWORD attrib = GetFileAttributesA(fx_path_get_cstr(path)); DWORD attrib = GetFileAttributesA(fx_path_ptr(path));
return attrib != INVALID_FILE_ATTRIBUTES; return attrib != INVALID_FILE_ATTRIBUTES;
} }
bool fx_path_is_file(const struct fx_path *path) bool fx_path_is_file(const struct fx_path *path)
{ {
DWORD attrib = GetFileAttributesA(fx_path_get_cstr(path)); DWORD attrib = GetFileAttributesA(fx_path_ptr(path));
if (attrib == INVALID_FILE_ATTRIBUTES) { if (attrib == INVALID_FILE_ATTRIBUTES) {
return false; return false;
} }
@@ -221,7 +221,7 @@ bool fx_path_is_file(const struct fx_path *path)
bool fx_path_is_directory(const struct fx_path *path) bool fx_path_is_directory(const struct fx_path *path)
{ {
DWORD attrib = GetFileAttributesA(fx_path_get_cstr(path)); DWORD attrib = GetFileAttributesA(fx_path_ptr(path));
if (attrib == INVALID_FILE_ATTRIBUTES) { if (attrib == INVALID_FILE_ATTRIBUTES) {
return false; return false;
} }
@@ -229,7 +229,7 @@ bool fx_path_is_directory(const struct fx_path *path)
return (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0; return (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0;
} }
const char *fx_path_get_cstr(const struct fx_path *path) const char *fx_path_ptr(const struct fx_path *path)
{ {
return fx_string_get_cstr(path->pathstr); return fx_string_get_cstr(path->pathstr);
} }
-23
View File
@@ -1,23 +0,0 @@
#include <fx/io/pipe.h>
#include <stdio.h>
int main(void)
{
fx_iostream *read = NULL, *write = NULL;
fx_status status = fx_pipe_create(&read, &write);
if (!FX_OK(status)) {
fprintf(stderr,
"failed to create pipe: %s\n",
fx_status_description(status));
return -1;
}
fx_stream_write_cstr(write, "Hello, world!\n", NULL);
char buf[128];
fx_stream_read_line(read, buf, sizeof buf);
printf("read from pipe: %s\n", buf);
return 0;
}
+1 -1
View File
@@ -27,7 +27,7 @@ int main(int argc, const char **argv)
{ {
fx_directory_entry *entry = NULL; fx_directory_entry *entry = NULL;
fx_value_get_pointer(val, (void **)&entry); fx_value_get_pointer(val, (void **)&entry);
printf("%s\n", fx_path_get_cstr(entry->filepath)); printf("%s\n", fx_path_ptr(entry->filepath));
} }
fx_iterator_unref(it); fx_iterator_unref(it);
+1 -1
View File
@@ -130,7 +130,7 @@ void write_tagged_datetime(fx_datetime *data)
fx_stringstream *new_data = fx_stringstream_create(); fx_stringstream *new_data = fx_stringstream_create();
fx_datetime_to_string(data, FX_DATETIME_FORMAT_RFC3339, new_data); fx_datetime_to_string(data, FX_DATETIME_FORMAT_RFC3339, new_data);
fx_stream_write_cstr(fx_stdout, fx_stringstream_get_cstr(new_data), NULL); fx_stream_write_cstr(fx_stdout, fx_stringstream_ptr(new_data), NULL);
fx_stream_write_cstr(fx_stdout, "\" }", NULL); fx_stream_write_cstr(fx_stdout, "\" }", NULL);
+1
View File
@@ -1852,6 +1852,7 @@ static fx_result parse_array_inline(struct ctx *ctx, fx_value *result)
static fx_result parse_value(struct ctx *ctx, fx_value *result) static fx_result parse_value(struct ctx *ctx, fx_value *result)
{ {
struct token *tok = peek_token(ctx); struct token *tok = peek_token(ctx);
if (!tok) { if (!tok) {
return FX_RESULT_ERR(BAD_FORMAT); return FX_RESULT_ERR(BAD_FORMAT);
-6
View File
@@ -50,11 +50,6 @@ enum {
FX_KEY_BACKSPACE, FX_KEY_BACKSPACE,
FX_KEY_RETURN, FX_KEY_RETURN,
/* within this range, we further reserve a sub-range. applications can
* define their own special keycode values within this range */
FX_KEY_RESERVED_START = 0xFFF00,
FX_KEY_RESERVED_END = 0xFFFFD,
FX_MOD_CTRL = 0x10000000, FX_MOD_CTRL = 0x10000000,
FX_KEY_EOF = 0xFFFFFFFF, FX_KEY_EOF = 0xFFFFFFFF,
@@ -158,7 +153,6 @@ FX_API void fx_tty_set_vmode(
struct fx_tty *tty, struct fx_tty *tty,
const struct fx_tty_vmode *vmode); const struct fx_tty_vmode *vmode);
FX_API void fx_tty_reset_vmode(struct fx_tty *tty); FX_API void fx_tty_reset_vmode(struct fx_tty *tty);
FX_API void fx_tty_flush(struct fx_tty *tty);
FX_API enum fx_status fx_tty_get_dimensions( FX_API enum fx_status fx_tty_get_dimensions(
struct fx_tty *tty, struct fx_tty *tty,
+14 -95
View File
@@ -2,7 +2,6 @@
#include "../../tty.h" #include "../../tty.h"
#include <ctype.h>
#include <fx/term/tty.h> #include <fx/term/tty.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -104,7 +103,6 @@ static void init_tty(struct fx_tty *tty, FILE *in, FILE *out)
tcgetattr(fd, &tty->t_ios_default); tcgetattr(fd, &tty->t_ios_default);
memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw); memcpy(&tty->t_ios_raw, &tty->t_ios_default, sizeof tty->t_ios_raw);
setvbuf(out, NULL, _IOLBF, 4096);
tty->t_ios_raw.c_iflag &= ~(ICRNL | IXON); tty->t_ios_raw.c_iflag &= ~(ICRNL | IXON);
tty->t_ios_raw.c_oflag &= ~(OPOST); tty->t_ios_raw.c_oflag &= ~(OPOST);
@@ -209,14 +207,8 @@ void fx_tty_reset_vmode(struct fx_tty *tty)
tty->t_vmode.v_attrib = FX_TTY_ATTRIB_NORMAL; tty->t_vmode.v_attrib = FX_TTY_ATTRIB_NORMAL;
} }
void fx_tty_flush(struct fx_tty *tty)
{
fflush(tty->t_out);
}
static int compare_colour( static int compare_colour(
const struct fx_tty_colour *a, const struct fx_tty_colour *a, const struct fx_tty_colour *b)
const struct fx_tty_colour *b)
{ {
if (a->c_mode != b->c_mode) { if (a->c_mode != b->c_mode) {
return -1; return -1;
@@ -253,9 +245,7 @@ static int compare_colour(
return 0; return 0;
} }
static int compare_vmode( static int compare_vmode(const struct fx_tty_vmode *a, const struct fx_tty_vmode *b)
const struct fx_tty_vmode *a,
const struct fx_tty_vmode *b)
{ {
if (a->v_attrib != b->v_attrib) { if (a->v_attrib != b->v_attrib) {
return -1; return -1;
@@ -283,48 +273,45 @@ static void put_ansi_attrib(struct fx_tty *tty, const char *s)
} }
static void set_attrib( static void set_attrib(
struct fx_tty *tty, struct fx_tty *tty, enum fx_tty_attrib old, enum fx_tty_attrib new)
enum fx_tty_attrib old,
enum fx_tty_attrib new)
{ {
if (old == new) { if (old == new) {
return; return;
} }
/* Bold ON */ /* Bold ON */
if (!(old & FX_TTY_ATTRIB_BOLD) && new & FX_TTY_ATTRIB_BOLD) { if (!(old & FX_TTY_ATTRIB_BOLD) && new &FX_TTY_ATTRIB_BOLD) {
put_ansi_attrib(tty, ANSI_BOLD_ON); put_ansi_attrib(tty, ANSI_BOLD_ON);
} }
/* Bold OFF */ /* Bold OFF */
if (old & FX_TTY_ATTRIB_BOLD && !(new & FX_TTY_ATTRIB_BOLD)) { if (old & FX_TTY_ATTRIB_BOLD && !(new &FX_TTY_ATTRIB_BOLD)) {
put_ansi_attrib(tty, ANSI_BOLD_OFF); put_ansi_attrib(tty, ANSI_BOLD_OFF);
} }
/* Underline ON */ /* Underline ON */
if (!(old & FX_TTY_ATTRIB_UNDERLINE) && new & FX_TTY_ATTRIB_UNDERLINE) { if (!(old & FX_TTY_ATTRIB_UNDERLINE) && new &FX_TTY_ATTRIB_UNDERLINE) {
put_ansi_attrib(tty, ANSI_UNDERLINE_ON); put_ansi_attrib(tty, ANSI_UNDERLINE_ON);
} }
/* Underline OFF */ /* Underline OFF */
if (old & FX_TTY_ATTRIB_UNDERLINE && !(new & FX_TTY_ATTRIB_UNDERLINE)) { if (old & FX_TTY_ATTRIB_UNDERLINE && !(new &FX_TTY_ATTRIB_UNDERLINE)) {
put_ansi_attrib(tty, ANSI_UNDERLINE_OFF); put_ansi_attrib(tty, ANSI_UNDERLINE_OFF);
} }
/* Italic ON */ /* Italic ON */
if (!(old & FX_TTY_ATTRIB_ITALIC) && new & FX_TTY_ATTRIB_ITALIC) { if (!(old & FX_TTY_ATTRIB_ITALIC) && new &FX_TTY_ATTRIB_ITALIC) {
put_ansi_attrib(tty, ANSI_ITALIC_ON); put_ansi_attrib(tty, ANSI_ITALIC_ON);
} }
/* Italic OFF */ /* Italic OFF */
if (old & FX_TTY_ATTRIB_ITALIC && !(new & FX_TTY_ATTRIB_ITALIC)) { if (old & FX_TTY_ATTRIB_ITALIC && !(new &FX_TTY_ATTRIB_ITALIC)) {
put_ansi_attrib(tty, ANSI_ITALIC_OFF); put_ansi_attrib(tty, ANSI_ITALIC_OFF);
} }
} }
static void set_fg( static void set_fg(
struct fx_tty *tty, struct fx_tty *tty, const struct fx_tty_colour *old,
const struct fx_tty_colour *old,
const struct fx_tty_colour *new) const struct fx_tty_colour *new)
{ {
if (compare_colour(old, new) == 0) { if (compare_colour(old, new) == 0) {
@@ -360,8 +347,7 @@ static void set_fg(
} }
static void set_bg( static void set_bg(
struct fx_tty *tty, struct fx_tty *tty, const struct fx_tty_colour *old,
const struct fx_tty_colour *old,
const struct fx_tty_colour *new) const struct fx_tty_colour *new)
{ {
if (compare_colour(old, new) == 0) { if (compare_colour(old, new) == 0) {
@@ -474,10 +460,7 @@ fx_keycode fx_tty_read_key(struct fx_tty *tty)
return c; return c;
} }
void fx_tty_move_cursor_x( void fx_tty_move_cursor_x(struct fx_tty *tty, enum fx_tty_position_base base, int pos)
struct fx_tty *tty,
enum fx_tty_position_base base,
int pos)
{ {
if (base == FX_TTY_POS_CURSOR && pos < 0 && pos >= -4) { if (base == FX_TTY_POS_CURSOR && pos < 0 && pos >= -4) {
for (int i = 0; i > pos; i--) { for (int i = 0; i > pos; i--) {
@@ -506,10 +489,7 @@ void fx_tty_move_cursor_x(
} }
} }
void fx_tty_move_cursor_y( void fx_tty_move_cursor_y(struct fx_tty *tty, enum fx_tty_position_base base, int pos)
struct fx_tty *tty,
enum fx_tty_position_base base,
int pos)
{ {
if (base == FX_TTY_POS_START) { if (base == FX_TTY_POS_START) {
/* we don't need this functionality right now */ /* we don't need this functionality right now */
@@ -550,9 +530,7 @@ void fx_tty_clear(struct fx_tty *tty, enum fx_tty_clear_mode mode)
} }
enum fx_status fx_tty_get_dimensions( enum fx_status fx_tty_get_dimensions(
struct fx_tty *tty, struct fx_tty *tty, unsigned int *w, unsigned int *h)
unsigned int *w,
unsigned int *h)
{ {
if (!(tty->t_flags & FX_TTY_INTERACTIVE)) { if (!(tty->t_flags & FX_TTY_INTERACTIVE)) {
return -1; return -1;
@@ -574,62 +552,3 @@ enum fx_status fx_tty_get_dimensions(
return 0; return 0;
} }
enum fx_status fx_tty_get_cursor_position(
struct fx_tty *tty,
unsigned int *w,
unsigned int *h)
{
if (!(tty->t_flags & FX_TTY_INTERACTIVE)) {
return -1;
}
int in_fd = fileno(tty->t_in);
int out_fd = fileno(tty->t_out);
write(out_fd, "\x1b[6n", 4);
char c;
read(in_fd, &c, 1);
read(in_fd, &c, 1);
char s[16] = {0};
for (int i = 0; i < sizeof s - 1; i++) {
read(in_fd, &c, 1);
if (!isdigit(c)) {
break;
}
s[i] = c;
s[i + 1] = 0;
}
if (w) {
if (s[0]) {
*w = atoi(s);
} else {
*w = 0;
}
}
s[0] = 0;
for (int i = 0; i < sizeof s - 1; i++) {
read(in_fd, &c, 1);
if (!isdigit(c)) {
break;
}
s[i] = c;
s[i + 1] = 0;
}
if (h) {
if (s[0]) {
*h = atoi(s);
} else {
*h = 0;
}
}
return 0;
}
+5 -2
View File
@@ -1217,8 +1217,11 @@ static bool wchar_is_control(fx_wchar c)
return true; return true;
} }
/* treat the reserved range as control characters */ if (c == 0xFEFF) {
if (c >= 0xF0000 && c <= 0xFFFFD) { return true;
}
if (c >= 0xFFF9 && c <= 0xFFFB) {
return true; return true;
} }
-39
View File
@@ -1,39 +0,0 @@
#include <fx/global.h>
#include <fx/thread.h>
#include <stdlib.h>
#include <string.h>
static fx_queue globals = FX_QUEUE_INIT;
static fx_mutex globals_lock = FX_MUTEX_INIT;
struct global {
fx_object *g_object;
fx_queue_entry g_entry;
};
void fx_cleanup_global(void)
{
fx_queue_entry *cur = fx_queue_first(&globals);
while (cur) {
struct global *global = fx_unbox(struct global, cur, g_entry);
cur = fx_queue_next(cur);
fx_object_unref(global->g_object);
free(global);
}
}
void fx_register_global(fx_object *obj)
{
struct global *global = malloc(sizeof *global);
if (!global) {
return;
}
memset(global, 0x0, sizeof *global);
global->g_object = fx_object_ref(obj);
fx_mutex_lock(&globals_lock);
fx_queue_push_back(&globals, &global->g_entry);
fx_mutex_unlock(&globals_lock);
}
-10
View File
@@ -1,10 +0,0 @@
#ifndef FX_GLOBAL_H_
#define FX_GLOBAL_H_
#include <fx/misc.h>
#include <fx/object.h>
FX_API void fx_register_global(fx_object *obj);
FX_API void fx_cleanup_global(void);
#endif
+1 -1
View File
@@ -95,7 +95,7 @@ fx_string_insert_cstrf(fx_string *dest, size_t at, const char *format, ...);
FX_API void fx_string_clear(fx_string *str); FX_API void fx_string_clear(fx_string *str);
FX_API fx_iterator *fx_string_tokenise( FX_API fx_iterator *fx_string_tokenise(
const fx_string *str, fx_string *str,
const char *delims[], const char *delims[],
size_t nr_delims, size_t nr_delims,
fx_string_tokenise_flags flags); fx_string_tokenise_flags flags);
+1 -1
View File
@@ -27,7 +27,7 @@ FX_API fx_status fx_stringstream_reset(fx_stringstream *strv);
FX_API fx_status FX_API fx_status
fx_stringstream_reset_with_buffer(fx_stringstream *strv, char *buf, size_t max); fx_stringstream_reset_with_buffer(fx_stringstream *strv, char *buf, size_t max);
FX_API const char *fx_stringstream_get_cstr(const fx_stringstream *strv); FX_API const char *fx_stringstream_ptr(const fx_stringstream *strv);
FX_API char *fx_stringstream_steal(fx_stringstream *strv); FX_API char *fx_stringstream_steal(fx_stringstream *strv);
FX_API size_t fx_stringstream_get_length(const fx_stringstream *strv); FX_API size_t fx_stringstream_get_length(const fx_stringstream *strv);
+1 -21
View File
@@ -25,10 +25,6 @@ static enum fx_status iterator_set_status(
const fx_iterator *fx_iterator_begin(const fx_iterable *it) const fx_iterator *fx_iterator_begin(const fx_iterable *it)
{ {
if (!it) {
return NULL;
}
FX_CLASS_DISPATCH_VIRTUAL_0( FX_CLASS_DISPATCH_VIRTUAL_0(
fx_iterable, fx_iterable,
FX_TYPE_ITERABLE, FX_TYPE_ITERABLE,
@@ -39,19 +35,11 @@ const fx_iterator *fx_iterator_begin(const fx_iterable *it)
enum fx_status fx_iterator_get_status(const fx_iterator *it) enum fx_status fx_iterator_get_status(const fx_iterator *it)
{ {
if (!it) {
return FX_ERR_NO_DATA;
}
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ITERATOR, iterator_get_status, it); FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_ITERATOR, iterator_get_status, it);
} }
enum fx_status fx_iterator_set_status(const fx_iterator *it, fx_status status) enum fx_status fx_iterator_set_status(const fx_iterator *it, fx_status status)
{ {
if (!it) {
return FX_ERR_NO_DATA;
}
FX_CLASS_DISPATCH_STATIC( FX_CLASS_DISPATCH_STATIC(
FX_TYPE_ITERATOR, FX_TYPE_ITERATOR,
iterator_set_status, iterator_set_status,
@@ -61,10 +49,6 @@ enum fx_status fx_iterator_set_status(const fx_iterator *it, fx_status status)
enum fx_status fx_iterator_move_next(const fx_iterator *it) enum fx_status fx_iterator_move_next(const fx_iterator *it)
{ {
if (!it) {
return FX_ERR_NO_DATA;
}
enum fx_status status = FX_ERR_NOT_SUPPORTED; enum fx_status status = FX_ERR_NOT_SUPPORTED;
fx_iterator_class *iface fx_iterator_class *iface
@@ -81,7 +65,7 @@ enum fx_status fx_iterator_move_next(const fx_iterator *it)
const fx_value *fx_iterator_get_value(const fx_iterator *it) const fx_value *fx_iterator_get_value(const fx_iterator *it)
{ {
if (!it) { if (fx_iterator_get_status(it) != FX_SUCCESS) {
return NULL; return NULL;
} }
@@ -95,10 +79,6 @@ const fx_value *fx_iterator_get_value(const fx_iterator *it)
fx_status fx_iterator_erase(fx_iterator *it) fx_status fx_iterator_erase(fx_iterator *it)
{ {
if (!it) {
return FX_ERR_NO_DATA;
}
enum fx_status status = FX_ERR_NOT_SUPPORTED; enum fx_status status = FX_ERR_NOT_SUPPORTED;
fx_iterator_class *iface fx_iterator_class *iface
+1 -8
View File
@@ -200,19 +200,12 @@ enum fx_status fx_object_get_data(
struct _fx_object *fx_object_ref(struct _fx_object *p) struct _fx_object *fx_object_ref(struct _fx_object *p)
{ {
if (p) { p->obj_ref++;
p->obj_ref++;
}
return p; return p;
} }
void fx_object_unref(struct _fx_object *p) void fx_object_unref(struct _fx_object *p)
{ {
if (!p) {
return;
}
if (p->obj_ref > 1) { if (p->obj_ref > 1) {
p->obj_ref--; p->obj_ref--;
return; return;
+25 -18
View File
@@ -642,8 +642,9 @@ static fx_stream *init_stdio_stream(FILE *fp, fx_stream_mode mode)
return NULL; return NULL;
} }
struct fx_stdio_stream_p *p struct fx_stdio_stream_p *p = fx_object_get_private(
= fx_object_get_private(stream, FX_TYPE_STDIO_STREAM); stream,
FX_TYPE_STDIO_STREAM);
fx_stream_cfg *cfg = fx_object_get_protected(stream, FX_TYPE_STREAM); fx_stream_cfg *cfg = fx_object_get_protected(stream, FX_TYPE_STREAM);
p->s_fp = fp; p->s_fp = fp;
cfg->s_mode = mode; cfg->s_mode = mode;
@@ -680,8 +681,8 @@ static enum fx_status stream_push_indent(struct stream_data *stream, int indent)
stream->s_private->s_istack_size += 4; stream->s_private->s_istack_size += 4;
} }
int cur_indent int cur_indent = stream->s_private
= stream->s_private->s_istack[stream->s_private->s_istack_ptr]; ->s_istack[stream->s_private->s_istack_ptr];
stream->s_private->s_istack[++stream->s_private->s_istack_ptr] stream->s_private->s_istack[++stream->s_private->s_istack_ptr]
= cur_indent + indent; = cur_indent + indent;
@@ -749,8 +750,9 @@ fx_stream_buffer *fx_stream_buffer_create_dynamic(size_t buffer_size)
return NULL; return NULL;
} }
struct fx_stream_buffer_p *p struct fx_stream_buffer_p *p = fx_object_get_private(
= fx_object_get_private(buffer, FX_TYPE_STREAM_BUFFER); buffer,
FX_TYPE_STREAM_BUFFER);
p->p_buf = malloc(buffer_size); p->p_buf = malloc(buffer_size);
if (!p->p_buf) { if (!p->p_buf) {
@@ -771,8 +773,9 @@ fx_stream_buffer *fx_stream_buffer_create(void *buf, size_t len)
return NULL; return NULL;
} }
struct fx_stream_buffer_p *p struct fx_stream_buffer_p *p = fx_object_get_private(
= fx_object_get_private(buffer, FX_TYPE_STREAM_BUFFER); buffer,
FX_TYPE_STREAM_BUFFER);
p->p_buf = buf; p->p_buf = buf;
p->p_buf_len = len; p->p_buf_len = len;
@@ -1127,14 +1130,15 @@ static enum fx_status stdio_read(
size_t max, size_t max,
size_t *nr_read) size_t *nr_read)
{ {
struct fx_stdio_stream_p *p struct fx_stdio_stream_p *p = fx_object_get_private(
= fx_object_get_private(stream, FX_TYPE_STDIO_STREAM); stream,
FX_TYPE_STDIO_STREAM);
enum fx_status status = FX_SUCCESS; enum fx_status status = FX_SUCCESS;
size_t count = fread(out, 1, max, p->s_fp); size_t count = fread(out, 1, max, p->s_fp);
if (count == 0 && ferror(p->s_fp)) { if (ferror(p->s_fp)) {
status = FX_ERR_IO_FAILURE; status = FX_ERR_IO_FAILURE;
} }
@@ -1148,13 +1152,14 @@ static enum fx_status stdio_write(
size_t count, size_t count,
size_t *nr_written) size_t *nr_written)
{ {
struct fx_stdio_stream_p *p struct fx_stdio_stream_p *p = fx_object_get_private(
= fx_object_get_private(stream, FX_TYPE_STDIO_STREAM); stream,
FX_TYPE_STDIO_STREAM);
enum fx_status status = FX_SUCCESS; enum fx_status status = FX_SUCCESS;
size_t w = fwrite(data, 1, count, p->s_fp); size_t w = fwrite(data, 1, count, p->s_fp);
if (count == 0 && ferror(p->s_fp)) { if (ferror(p->s_fp)) {
status = FX_ERR_IO_FAILURE; status = FX_ERR_IO_FAILURE;
} }
@@ -1167,8 +1172,9 @@ static enum fx_status stdio_seek(
long long offset, long long offset,
fx_stream_seek_origin origin) fx_stream_seek_origin origin)
{ {
struct fx_stdio_stream_p *p struct fx_stdio_stream_p *p = fx_object_get_private(
= fx_object_get_private(stream, FX_TYPE_STDIO_STREAM); stream,
FX_TYPE_STDIO_STREAM);
int whence = 0; int whence = 0;
switch (origin) { switch (origin) {
@@ -1195,8 +1201,9 @@ static enum fx_status stdio_seek(
static enum fx_status stdio_tell(const fx_stream *stream, size_t *cursor) static enum fx_status stdio_tell(const fx_stream *stream, size_t *cursor)
{ {
struct fx_stdio_stream_p *p struct fx_stdio_stream_p *p = fx_object_get_private(
= fx_object_get_private(stream, FX_TYPE_STDIO_STREAM); stream,
FX_TYPE_STDIO_STREAM);
long pos = ftell(p->s_fp); long pos = ftell(p->s_fp);
if (pos == -1L) { if (pos == -1L) {
+9 -10
View File
@@ -56,7 +56,7 @@ struct fx_string_p {
struct fx_string_iterator_p { struct fx_string_iterator_p {
int _m, _f; int _m, _f;
fx_string *_tmp; fx_string *_tmp;
const struct fx_string_p *_s_p, *_tmp_p; struct fx_string_p *_s_p, *_tmp_p;
const char **_d; const char **_d;
size_t _nd, _ds; size_t _nd, _ds;
@@ -301,7 +301,7 @@ static fx_string *string_duplicate(const struct fx_string_p *str)
const char *src = string_ptr(str); const char *src = string_ptr(str);
char *dst = string_ptr(new_str_p); char *dst = string_ptr(new_str_p);
memcpy(dst, src, str->s_len + 1); memcpy(dst, src, str->s_len);
new_str_p->s_len = str->s_len; new_str_p->s_len = str->s_len;
new_str_p->s_codepoints = str->s_codepoints; new_str_p->s_codepoints = str->s_codepoints;
@@ -478,7 +478,7 @@ static fx_status string_replace_all_with_stringstream(
size_t new_len = fx_stringstream_get_length(new_data); size_t new_len = fx_stringstream_get_length(new_data);
string_reserve(str, new_len); string_reserve(str, new_len);
char *dest = string_ptr(str); char *dest = string_ptr(str);
memcpy(dest, fx_stringstream_get_cstr(new_data), new_len); memcpy(dest, fx_stringstream_ptr(new_data), new_len);
dest[new_len] = '\0'; dest[new_len] = '\0';
str->s_len = new_len; str->s_len = new_len;
@@ -513,8 +513,7 @@ static enum fx_status remove_ansi(
memmove(removal_start, excess_src, excess_length); memmove(removal_start, excess_src, excess_length);
s[new_str_len] = '\0'; s[new_str_len] = '\0';
str->s_len -= length; str->s_len = new_str_len;
str->s_codepoints -= length;
return FX_SUCCESS; return FX_SUCCESS;
} }
@@ -1024,7 +1023,7 @@ static enum fx_status find_next_token(struct fx_string_iterator_p *it)
} }
static fx_iterator *string_tokenise( static fx_iterator *string_tokenise(
const struct fx_string_p *str, struct fx_string_p *str,
const char *delims[], const char *delims[],
size_t nr_delims, size_t nr_delims,
fx_string_tokenise_flags flags) fx_string_tokenise_flags flags)
@@ -1391,7 +1390,7 @@ void fx_string_clear(fx_string *str)
} }
fx_iterator *fx_string_tokenise( fx_iterator *fx_string_tokenise(
const fx_string *str, fx_string *str,
const char *delims[], const char *delims[],
size_t nr_delims, size_t nr_delims,
fx_string_tokenise_flags flags) fx_string_tokenise_flags flags)
@@ -1575,9 +1574,10 @@ static fx_status get_length(
/*** ITERATOR FUNCTIONS *******************************************************/ /*** ITERATOR FUNCTIONS *******************************************************/
static void iterator_fini(fx_object *obj, void *priv) static void iterator_fini(fx_iterator *obj)
{ {
struct fx_string_iterator_p *it = priv; struct fx_string_iterator_p *it
= fx_object_get_private(obj, FX_TYPE_STRING_ITERATOR);
if (it->_tmp) { if (it->_tmp) {
fx_string_unref(it->_tmp); fx_string_unref(it->_tmp);
} }
@@ -1840,5 +1840,4 @@ FX_TYPE_DEFINITION_BEGIN(fx_string_iterator)
FX_TYPE_EXTENDS(FX_TYPE_ITERATOR); FX_TYPE_EXTENDS(FX_TYPE_ITERATOR);
FX_TYPE_CLASS(fx_string_iterator_class); FX_TYPE_CLASS(fx_string_iterator_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_string_iterator_p); FX_TYPE_INSTANCE_PRIVATE(struct fx_string_iterator_p);
FX_TYPE_INSTANCE_FINI(iterator_fini);
FX_TYPE_DEFINITION_END(fx_string_iterator) FX_TYPE_DEFINITION_END(fx_string_iterator)
+21 -19
View File
@@ -141,7 +141,7 @@ static size_t stringstream_get_length(const struct fx_stringstream_p *strv)
return strv->ss_len; return strv->ss_len;
} }
static const char *stringstream_get_cstr(const struct fx_stringstream_p *ss) static const char *stringstream_ptr(const struct fx_stringstream_p *ss)
{ {
return ss->ss_buf; return ss->ss_buf;
} }
@@ -167,8 +167,9 @@ fx_stringstream *fx_stringstream_create_with_buffer(char *buf, size_t max)
} }
fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM); fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM);
struct fx_stringstream_p *p struct fx_stringstream_p *p = fx_object_get_private(
= fx_object_get_private(s, FX_TYPE_STRINGSTREAM); s,
FX_TYPE_STRINGSTREAM);
cfg->s_mode = FX_STREAM_READ | FX_STREAM_WRITE | Z__FX_STREAM_STATIC; cfg->s_mode = FX_STREAM_READ | FX_STREAM_WRITE | Z__FX_STREAM_STATIC;
@@ -188,8 +189,9 @@ fx_stringstream *fx_stringstream_create(void)
} }
fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM); fx_stream_cfg *cfg = fx_object_get_protected(s, FX_TYPE_STREAM);
struct fx_stringstream_p *p struct fx_stringstream_p *p = fx_object_get_private(
= fx_object_get_private(s, FX_TYPE_STRINGSTREAM); s,
FX_TYPE_STRINGSTREAM);
cfg->s_mode = FX_STREAM_READ | FX_STREAM_WRITE | Z__FX_STREAM_STATIC; cfg->s_mode = FX_STREAM_READ | FX_STREAM_WRITE | Z__FX_STREAM_STATIC;
@@ -236,12 +238,9 @@ enum fx_status fx_stringstream_reset_with_buffer(
max); max);
} }
const char *fx_stringstream_get_cstr(const fx_stringstream *ss) const char *fx_stringstream_ptr(const fx_stringstream *ss)
{ {
FX_CLASS_DISPATCH_STATIC_0( FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_STRINGSTREAM, stringstream_ptr, ss);
FX_TYPE_STRINGSTREAM,
stringstream_get_cstr,
ss);
} }
char *fx_stringstream_steal(fx_stringstream *ss) char *fx_stringstream_steal(fx_stringstream *ss)
@@ -269,38 +268,41 @@ static void stringstream_fini(fx_object *obj, void *priv)
} }
} }
static enum fx_status stream_getc(fx_stream *stream, fx_wchar *c) enum fx_status stream_getc(fx_stream *stream, fx_wchar *c)
{ {
struct fx_stringstream_p *s struct fx_stringstream_p *s = fx_object_get_private(
= fx_object_get_private(stream, FX_TYPE_STRINGSTREAM); stream,
FX_TYPE_STRINGSTREAM);
enum fx_status status = __getc(s, c); enum fx_status status = __getc(s, c);
return status; return status;
} }
static enum fx_status stream_read( enum fx_status stream_read(
fx_stream *stream, fx_stream *stream,
void *buf, void *buf,
size_t count, size_t count,
size_t *nr_read) size_t *nr_read)
{ {
struct fx_stringstream_p *s struct fx_stringstream_p *s = fx_object_get_private(
= fx_object_get_private(stream, FX_TYPE_STRINGSTREAM); stream,
FX_TYPE_STRINGSTREAM);
enum fx_status status = __gets(s, buf, count, nr_read); enum fx_status status = __gets(s, buf, count, nr_read);
return status; return status;
} }
static enum fx_status stream_write( enum fx_status stream_write(
fx_stream *stream, fx_stream *stream,
const void *buf, const void *buf,
size_t count, size_t count,
size_t *nr_written) size_t *nr_written)
{ {
struct fx_stringstream_p *s struct fx_stringstream_p *s = fx_object_get_private(
= fx_object_get_private(stream, FX_TYPE_STRINGSTREAM); stream,
FX_TYPE_STRINGSTREAM);
enum fx_status status = __puts(s, (const char *)buf, count, nr_written); enum fx_status status = __puts(s, (const char *)buf, count, nr_written);
-17
View File
@@ -1,17 +0,0 @@
#include <fx/thread.h>
#include <pthread.h>
bool fx_mutex_lock(fx_mutex *mut)
{
return pthread_mutex_lock(mut) == 0;
}
bool fx_mutex_trylock(fx_mutex *mut)
{
return pthread_mutex_trylock(mut) == 0;
}
bool fx_mutex_unlock(fx_mutex *mut)
{
return pthread_mutex_trylock(mut) == 0;
}
-17
View File
@@ -1,17 +0,0 @@
#include <fx/thread.h>
#include <pthread.h>
bool fx_mutex_lock(fx_mutex *mut)
{
return pthread_mutex_lock(mut) == 0;
}
bool fx_mutex_trylock(fx_mutex *mut)
{
return pthread_mutex_trylock(mut) == 0;
}
bool fx_mutex_unlock(fx_mutex *mut)
{
return pthread_mutex_trylock(mut) == 0;
}
+31 -78
View File
@@ -5,7 +5,6 @@
#include <fx/bst.h> #include <fx/bst.h>
#include <fx/endian.h> #include <fx/endian.h>
#include <fx/global.h>
#include <fx/int.h> #include <fx/int.h>
#include <fx/object.h> #include <fx/object.h>
#include <fx/reflection/function.h> #include <fx/reflection/function.h>
@@ -17,7 +16,6 @@
static struct fx_bst type_idmap = FX_BST_INIT; static struct fx_bst type_idmap = FX_BST_INIT;
static struct fx_namemap type_namemap = FX_NAMEMAP_INIT; static struct fx_namemap type_namemap = FX_NAMEMAP_INIT;
static fx_once runtime_cleanup_init = FX_ONCE_INIT;
static union fx_type_id zero_id = {0}; static union fx_type_id zero_id = {0};
struct type_init_ctx { struct type_init_ctx {
@@ -25,37 +23,6 @@ struct type_init_ctx {
size_t ctx_instance_offset; size_t ctx_instance_offset;
}; };
static fx_result fx_type_destroy(struct fx_type_info *info);
static void type_namemap_cleanup(struct fx_namemap_entry *entry)
{
struct fx_type_info *ty
= fx_unbox(struct fx_type_info, entry, ty_namemap_entry);
fx_type_destroy(ty);
}
static void function_namemap_cleanup(struct fx_namemap_entry *entry)
{
struct fx_type_function *func
= fx_unbox(struct fx_type_function, entry, f_entry);
fx_function_unref(func->f_func);
free(func);
}
static void property_namemap_cleanup(struct fx_namemap_entry *entry)
{
struct fx_type_property *prop
= fx_unbox(struct fx_type_property, entry, p_entry);
fx_property_unref(prop->p_prop);
free(prop);
}
static void runtime_cleanup(void)
{
fx_cleanup_global();
// fx_namemap_cleanup(&type_namemap, type_namemap_cleanup);
}
static inline int type_info_compare( static inline int type_info_compare(
const struct fx_type_info *a, const struct fx_type_info *a,
const struct fx_type_info *b) const struct fx_type_info *b)
@@ -89,8 +56,10 @@ static struct fx_type_info *get_type(
{ {
fx_bst_node *cur = tree->bst_root; fx_bst_node *cur = tree->bst_root;
while (cur) { while (cur) {
struct fx_type_info *cur_node struct fx_type_info *cur_node = fx_unbox(
= fx_unbox(struct fx_type_info, cur, ty_idmap_node); struct fx_type_info,
cur,
ty_idmap_node);
int cmp = fx_type_id_compare(key, &cur_node->ty_id); int cmp = fx_type_id_compare(key, &cur_node->ty_id);
if (cmp > 0) { if (cmp > 0) {
@@ -111,8 +80,10 @@ struct fx_type_component *fx_type_get_component(
{ {
fx_bst_node *cur = tree->bst_root; fx_bst_node *cur = tree->bst_root;
while (cur) { while (cur) {
struct fx_type_component *cur_node struct fx_type_component *cur_node = fx_unbox(
= fx_unbox(struct fx_type_component, cur, c_node); struct fx_type_component,
cur,
c_node);
int cmp = fx_type_id_compare(key, &cur_node->c_type->ty_id); int cmp = fx_type_id_compare(key, &cur_node->c_type->ty_id);
if (cmp > 0) { if (cmp > 0) {
@@ -180,14 +151,16 @@ static fx_result locate_interface(
struct fx_type_info *dest, struct fx_type_info *dest,
struct type_init_ctx *init_ctx) struct type_init_ctx *init_ctx)
{ {
struct fx_type_component *interface_comp struct fx_type_component *interface_comp = fx_type_get_component(
= fx_type_get_component(&dest->ty_components, interface_id); &dest->ty_components,
interface_id);
if (interface_comp) { if (interface_comp) {
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
struct fx_type_info *interface_reg struct fx_type_info *interface_reg = get_type(
= get_type(&type_idmap, interface_id); &type_idmap,
interface_id);
if (!interface_reg) { if (!interface_reg) {
return FX_RESULT_ERR(NO_ENTRY); return FX_RESULT_ERR(NO_ENTRY);
} }
@@ -254,8 +227,9 @@ static fx_result find_type_components(struct fx_type_info *type)
} }
while (1) { while (1) {
struct fx_type_info *dep_class struct fx_type_info *dep_class = get_type(
= get_type(&type_idmap, current_id); &type_idmap,
current_id);
if (!dep_class) { if (!dep_class) {
return FX_RESULT_ERR(NO_ENTRY); return FX_RESULT_ERR(NO_ENTRY);
} }
@@ -328,10 +302,6 @@ static bool type_has_base_class(struct fx_type_info *info)
fx_result fx_type_register(struct fx_type_info *info) fx_result fx_type_register(struct fx_type_info *info)
{ {
if (fx_init_once(&runtime_cleanup_init)) {
atexit(runtime_cleanup);
}
if (!type_has_base_class(info)) { if (!type_has_base_class(info)) {
fx_type_id_copy(FX_TYPE_OBJECT, &info->ty_parent_id); fx_type_id_copy(FX_TYPE_OBJECT, &info->ty_parent_id);
} }
@@ -370,34 +340,12 @@ fx_result fx_type_register(struct fx_type_info *info)
return FX_RESULT_SUCCESS; return FX_RESULT_SUCCESS;
} }
static fx_result fx_type_destroy(struct fx_type_info *info)
{
info->ty_category = FX_TYPE_CLASS;
union fx_type_id metatype = {0};
fx_bst_node *cur = fx_bst_first(&info->ty_components);
while (cur) {
struct fx_type_component *component
= fx_unbox(struct fx_type_component, cur, c_node);
cur = fx_bst_next(cur);
free(component);
}
fx_namemap_cleanup(&info->ty_properties, property_namemap_cleanup);
fx_namemap_cleanup(&info->ty_functions, function_namemap_cleanup);
fx_type_unref(info->ty_metatype);
return FX_RESULT_SUCCESS;
}
fx_result fx_type_add_function(fx_type_info *info, struct _fx_object *func) fx_result fx_type_add_function(fx_type_info *info, struct _fx_object *func)
{ {
const char *name = fx_function_get_name(func); const char *name = fx_function_get_name(func);
struct fx_namemap_entry *entry struct fx_namemap_entry *entry = fx_namemap_get(
= fx_namemap_get(&info->ty_functions, name); &info->ty_functions,
name);
if (entry) { if (entry) {
return FX_RESULT_ERR(NAME_EXISTS); return FX_RESULT_ERR(NAME_EXISTS);
} }
@@ -418,8 +366,9 @@ fx_result fx_type_add_function(fx_type_info *info, struct _fx_object *func)
fx_result fx_type_add_property(fx_type_info *info, struct _fx_object *prop) fx_result fx_type_add_property(fx_type_info *info, struct _fx_object *prop)
{ {
const char *name = fx_property_get_name(prop); const char *name = fx_property_get_name(prop);
struct fx_namemap_entry *entry struct fx_namemap_entry *entry = fx_namemap_get(
= fx_namemap_get(&info->ty_functions, name); &info->ty_functions,
name);
if (entry) { if (entry) {
return FX_RESULT_ERR(NAME_EXISTS); return FX_RESULT_ERR(NAME_EXISTS);
} }
@@ -461,8 +410,10 @@ fx_function *fx_type_info_get_function_by_name(
return NULL; return NULL;
} }
struct fx_type_function *func struct fx_type_function *func = fx_unbox(
= fx_unbox(struct fx_type_function, entry, f_entry); struct fx_type_function,
entry,
f_entry);
return func->f_func; return func->f_func;
} }
@@ -475,7 +426,9 @@ fx_function *fx_type_info_get_property_by_name(
return NULL; return NULL;
} }
struct fx_type_property *prop struct fx_type_property *prop = fx_unbox(
= fx_unbox(struct fx_type_property, entry, p_entry); struct fx_type_property,
entry,
p_entry);
return prop->p_prop; return prop->p_prop;
} }
+1 -1
View File
@@ -181,7 +181,7 @@ static fx_status value_change_string(fx_value *in, fx_value *out)
} }
fx_string *result fx_string *result
= fx_string_create_from_cstr(fx_stringstream_get_cstr(strm)); = fx_string_create_from_cstr(fx_stringstream_ptr(strm));
fx_stringstream_unref(strm); fx_stringstream_unref(strm);
if (!result) { if (!result) {
+33 -20
View File
@@ -1,27 +1,40 @@
#include <fx/diagnostics/process.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
int main(int argc, const char **argv) static int another_function(int a, double b, int c)
{ {
printf("%d args:\n", argc); printf("a=%d, b=%lf, c=%d\n", a, b, c);
return 2;
}
for (int i = 0; i < argc; i++) { static int test_function(int a, int b, int c, ...)
printf("%s\n", argv[i]); {
} va_list args;
va_start(args, c);
printf("environment:\n"); int d = va_arg(args, int);
fx_process *self = fx_process_get_self(); int e = va_arg(args, int);
const fx_hashtable *env = fx_process_get_environment(self); int f = va_arg(args, int);
fx_object_to_string(env, fx_stdout, NULL); int g = va_arg(args, int);
printf("\n"); int h = va_arg(args, int);
int i = va_arg(args, int);
printf("read from stdin...\n"); int j = va_arg(args, int);
char line[128]; printf("a=%d, b=%d, c=%d, d=%d, e=%d, f=%d, g=%d, h=%d, i=%d, j=%d\n",
if (fgets(line, sizeof line, stdin)) { a,
printf("read: %s\n", line); b,
} else { c,
printf("read failed\n"); d,
} e,
f,
g,
h,
i,
j);
return a + b + c + d + e;
}
int main(void)
{
test_function(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
another_function(1, 2.5, 5);
return 0; return 0;
} }