21 if (stat(filename, &stats) == -1) {
22 fprintf(stderr,
"** %s does not exist.", filename);
25 fd = open(filename, O_RDONLY |
O_BINARY);
27 fprintf(stderr,
"** could not open %s. check permissions.", filename);
31 if (read(fd,
PN_STR_PTR(buf), stats.st_size) == stats.st_size) {
41 fprintf(stderr,
"** could not read entire file: %s.", filename);
49 char *allocated_str, *ext_name, *func_name;
50 while (*(filename + ++ext_name_len) !=
'.' && ext_name_len <= len);
51 allocated_str = ext_name = malloc(ext_name_len + 1);
53 strncpy(ext_name, filename, ext_name_len);
54 ext_name[ext_name_len] =
'\0';
55 ext_name += ext_name_len;
56 while (*--ext_name !=
'/' && ext_name >= allocated_str);
58 if (asprintf(&func_name,
"Potion_Init_%s", ext_name) == -1)
65 void *handle = dlopen(filename, RTLD_LAZY);
67 char *err, *init_func_name;
70 fprintf(stderr,
"** error loading %s: %s\n", filename, dlerror());
74 func = (void (*)(
Potion *))dlsym(handle, init_func_name);
75 err = (
char *)dlerror();
78 fprintf(stderr,
"** error loading %s in %s: %s\n", init_func_name, filename, err);
108 char buf[str_len + len + 1];
109 strncpy(buf, str, str_len);
111 buf[str_len + len] =
'\0';
112 if (stat(buf, &st) == 0 && S_ISREG(st.st_mode))
121 if (!str_len) str_len = strlen(str);
124 char dirname[prefix_len + 1 + str_len + 1];
125 char *str_pos = dirname + prefix_len + 1;
128 memcpy(str_pos, str, str_len);
129 dot = memchr(str,
'.', str_len);
131 dirname[
sizeof(dirname) - 1] =
'\0';
134 memcpy(dirname,
PN_STR_PTR(prefix), prefix_len);
135 dirname[prefix_len] =
'/';
136 if (stat(dirname, &st) == 0 && S_ISREG(st.st_mode)) {
144 if ((file = strrchr(str,
'/')) == NULL)
149 if (stat(r, &st) != 0 || !S_ISREG(st.st_mode)) {
150 int r_len = prefix_len + 1 + str_len * 2 + 1;
151 if ((ext =
find_extension(r)) == NULL) { free(r); r = NULL;
continue; }
152 r = realloc(r, r_len + strlen(ext));
154 strcpy(r + r_len, ext);
169 if (filename == NULL) {
170 fprintf(stderr,
"** can't find %s\n",
PN_STR_PTR(file));
173 file_ext = filename + strlen(filename);
174 while (*--file_ext !=
'.' && file_ext >= filename);
175 if (file_ext++ != filename) {
176 if (strcmp(file_ext,
"pn") == 0)
178 else if (strcmp(file_ext,
"pnb") == 0)
183 fprintf(stderr,
"** unrecognized file extension: %s\n", file_ext);
185 fprintf(stderr,
"** no file extension: %s\n", filename);
198 if (filename == NULL) {
199 fprintf(stderr,
"** can't find %s\n",
PN_STR_PTR(file));
202 file_ext = filename + strlen(filename);
203 while (*--file_ext !=
'.' && file_ext >= filename);
204 if (file_ext++ != filename) {
205 if (strcmp(file_ext,
"pl") == 0)
206 result = p2_load_code(P, filename);
207 else if (strcmp(file_ext,
"plc") == 0)
208 result = p2_load_code(P, filename);
212 fprintf(stderr,
"** unrecognized file extension: %s\n", file_ext);
214 fprintf(stderr,
"** no file extension: %s\n", filename);
239 #define LOADER_PATH "@INC"
241 #define LOADER_PATH "LOADER_PATH"
PN potion_bytes(Potion *, size_t)
PN potion_str(Potion *, const char *)
static char * potion_initializer_name(Potion *P, const char *filename, PN_SIZE len)
#define potion_method(RCV, MSG, FN, SIG)
PN potion_source_load(Potion *P, PN cl, PN buf)
PN potion_load(Potion *P, PN cl, PN self, PN file)
PN p2_load(Potion *, PN, PN, PN)
PN potion_run(Potion *P, PN code, int jit)
void potion_define_global(Potion *P, PN name, PN val)
PN potion_parse(Potion *, PN, char *)
static PN potion_load_dylib(Potion *P, const char *filename)
static PN potion_load_code(Potion *P, const char *filename)
static const char * find_extension(char *str)
static const char * pn_loader_extensions[]
the global interpreter state P. currently singleton (not threads yet)
void potion_loader_init(Potion *P)
the central table type, based on core/khash.h
#define potion_send(RCV, MSG, ARGS...)
method caches (more great stuff from ian piumarta)
void potion_allocation_error(void)
void potion_loader_add(Potion *P, PN path)
#define PN_TUPLE_EACH(T, I, V, B)
char * potion_find_file(Potion *P, char *str, PN_SIZE str_len)