2013-10-20 09:41:27 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "misc.h"
|
2014-01-07 10:20:33 -05:00
|
|
|
#include "mpconfig.h"
|
2014-01-21 16:40:13 -05:00
|
|
|
#include "qstr.h"
|
2013-10-20 09:41:27 -04:00
|
|
|
#include "lexer.h"
|
2014-01-08 10:28:26 -05:00
|
|
|
#include "lexerunix.h"
|
2013-10-20 09:41:27 -04:00
|
|
|
|
2014-01-07 10:20:33 -05:00
|
|
|
#if MICROPY_ENABLE_LEXER_UNIX
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
|
2013-10-20 09:41:27 -04:00
|
|
|
int fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
printf("cannot open file %s\n", filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
uint size = lseek(fd, 0, SEEK_END);
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
|
|
|
char *data = m_new(char, size);
|
2013-12-29 13:11:05 -05:00
|
|
|
int read_size = read(fd, data, size);
|
2013-10-20 09:41:27 -04:00
|
|
|
close(fd);
|
2013-12-29 13:11:05 -05:00
|
|
|
if (read_size != size) {
|
|
|
|
printf("error reading file %s\n", filename);
|
2013-12-29 14:33:23 -05:00
|
|
|
m_del(char, data, size);
|
2013-12-29 13:11:05 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-10-20 09:41:27 -04:00
|
|
|
|
2014-01-08 10:28:26 -05:00
|
|
|
return mp_lexer_new_from_str_len(filename, data, size, size);
|
2013-10-20 09:41:27 -04:00
|
|
|
}
|
2014-01-03 09:03:48 -05:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* unix implementation of import */
|
|
|
|
|
|
|
|
// TODO properly!
|
|
|
|
|
|
|
|
static const char *import_base_dir = NULL;
|
|
|
|
|
|
|
|
void mp_import_set_directory(const char *dir) {
|
|
|
|
import_base_dir = dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_lexer_t *mp_import_open_file(qstr mod_name) {
|
|
|
|
vstr_t *vstr = vstr_new();
|
|
|
|
if (import_base_dir != NULL) {
|
|
|
|
vstr_printf(vstr, "%s/", import_base_dir);
|
|
|
|
}
|
|
|
|
vstr_printf(vstr, "%s.py", qstr_str(mod_name));
|
|
|
|
return mp_lexer_new_from_file(vstr_str(vstr)); // TODO does lexer need to copy the string? can we free it here?
|
|
|
|
}
|
2014-01-07 10:20:33 -05:00
|
|
|
|
|
|
|
#endif // MICROPY_ENABLE_LEXER_UNIX
|