Adjust indirect form (#i"path") to be relative to the current file.
This commit is contained in:
parent
6254044280
commit
ec306ef31f
48
reader.c
48
reader.c
|
|
@ -1,3 +1,10 @@
|
|||
#define _XOPEN_SOURCE 500
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
|
|
@ -47,7 +54,7 @@ static value_t patch_placeholders(reader_state_t *state, value_t v);
|
|||
|
||||
static void tree_replace(value_t *in, value_t oldval, value_t newval);
|
||||
|
||||
value_t read_value(FILE *f)
|
||||
value_t read_value_from_file(FILE *f)
|
||||
{
|
||||
reader_state_t state;
|
||||
value_t result;
|
||||
|
|
@ -72,6 +79,38 @@ value_t read_value(FILE *f)
|
|||
return result;
|
||||
}
|
||||
|
||||
value_t read_value_from_path(const char *path)
|
||||
{
|
||||
FILE *f = fopen(path, "r");
|
||||
const char *last_slash;
|
||||
value_t v;
|
||||
int dirfd;
|
||||
|
||||
release_assert(f != NULL);
|
||||
|
||||
dirfd = open(".", O_RDONLY);
|
||||
release_assert(dirfd >= 0);
|
||||
|
||||
last_slash = strrchr(path, '/');
|
||||
|
||||
if (last_slash)
|
||||
{
|
||||
size_t bytes = last_slash - path;
|
||||
char *dirname = (char*)malloc(bytes+1);
|
||||
memcpy(dirname, path, bytes);
|
||||
dirname[bytes] = '\0';
|
||||
chdir(dirname);
|
||||
}
|
||||
|
||||
v = read_value_from_file(f);
|
||||
fchdir(dirfd);
|
||||
close(dirfd);
|
||||
|
||||
fclose(f);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
static value_t read_one_value(reader_state_t *state)
|
||||
{
|
||||
skip_whitespace(state);
|
||||
|
|
@ -676,13 +715,8 @@ static value_t read_placeholder(reader_state_t *state)
|
|||
static value_t read_indirect(value_t path)
|
||||
{
|
||||
char *name = value_to_string(path);
|
||||
FILE *f = fopen(name, "r");
|
||||
value_t v;
|
||||
value_t v = read_value_from_path(name);
|
||||
free(name);
|
||||
|
||||
release_assert(f != NULL);
|
||||
v = read_value(f);
|
||||
fclose(f);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
|
|
|||
5
reader.h
5
reader.h
|
|
@ -1,9 +1,12 @@
|
|||
#ifndef READER_H_bc8f9bf546e3914a72851703b38326d2
|
||||
#define READER_H_bc8f9bf546e3914a72851703b38326d2
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "gc.h"
|
||||
|
||||
value_t read_value(FILE *f);
|
||||
value_t read_value_from_file(FILE *f);
|
||||
value_t read_value_from_path(const char *path);
|
||||
|
||||
#endif
|
||||
/* vim:set sw=2 expandtab: */
|
||||
|
|
|
|||
26
rosella.c
26
rosella.c
|
|
@ -37,19 +37,21 @@ int main(int argc, char **argv)
|
|||
interpreter_init();
|
||||
io_builtin_init();
|
||||
|
||||
if (argc < 2 || (strcmp(argv[1], "-k") == 0))
|
||||
if (argc < 2 || (strcmp(argv[1], "-t") == 0) || (strcmp(argv[1], "--test") == 0))
|
||||
{
|
||||
test_builtins();
|
||||
test_weak_boxes_and_wills();
|
||||
if (argc > 1)
|
||||
test_garbage_collection(false);
|
||||
}
|
||||
else if ((strcmp(argv[1], "-b") == 0) || (strcmp(argv[1], "--burn-in") == 0))
|
||||
{
|
||||
test_garbage_collection(true);
|
||||
}
|
||||
else if ((strcmp(argv[1], "-r") == 0) || (strcmp(argv[1], "--reader") == 0))
|
||||
{
|
||||
test_reader();
|
||||
test_garbage_collection(argc > 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
FILE *f = fopen(argv[1], "r");
|
||||
|
||||
if (f)
|
||||
{
|
||||
gc_root_t argv_root;
|
||||
value_t program;
|
||||
|
|
@ -64,8 +66,7 @@ int main(int argc, char **argv)
|
|||
argv_root.value = cons(temp, argv_root.value);
|
||||
}
|
||||
|
||||
program = read_value(f);
|
||||
fclose(f);
|
||||
program = read_value_from_path(argv[1]);
|
||||
|
||||
unregister_gc_root(&argv_root);
|
||||
results = run_interpreter(program, argv_root.value);
|
||||
|
|
@ -83,11 +84,6 @@ int main(int argc, char **argv)
|
|||
fprint_gc_stats(stderr);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
perror(argv[1]);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -206,7 +202,7 @@ static void test_reader(void)
|
|||
fputs("> ", stdout);
|
||||
fflush(stdout);
|
||||
|
||||
v = read_value(stdin);
|
||||
v = read_value_from_file(stdin);
|
||||
|
||||
print_value(v); nl();
|
||||
nl();
|
||||
|
|
|
|||
Loading…
Reference in New Issue