blob: b5abbc8fe5e1fe0235fa1f698254a98560f1acb8 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien George51dfcb42015-01-01 20:27:54 +000027#include "py/mpconfig.h"
Ilya Dmitrichenko8e9482a2014-04-12 16:37:17 +010028
Damien George58ebde42014-05-21 20:32:59 +010029#if MICROPY_HELPER_LEXER_UNIX
Ilya Dmitrichenko8e9482a2014-04-12 16:37:17 +010030
Damiena5185f42013-10-20 14:41:27 +010031#include <stdio.h>
Damien Georgea4c52c52014-12-05 19:35:18 +000032#include <stdint.h>
Damiena5185f42013-10-20 14:41:27 +010033#include <unistd.h>
34#include <fcntl.h>
Damien Georgeb5021562014-04-13 13:25:05 +010035#include <sys/stat.h>
36#include <sys/types.h>
Damiena5185f42013-10-20 14:41:27 +010037
Damien George51dfcb42015-01-01 20:27:54 +000038#include "py/lexer.h"
Damiena5185f42013-10-20 14:41:27 +010039
Damien Georgeffa9bdd2014-04-10 15:00:58 +010040typedef struct _mp_lexer_file_buf_t {
41 int fd;
Damien George94fbe972014-07-30 11:46:05 +010042 byte buf[20];
43 mp_uint_t len;
44 mp_uint_t pos;
Damien Georgeffa9bdd2014-04-10 15:00:58 +010045} mp_lexer_file_buf_t;
Damiena5185f42013-10-20 14:41:27 +010046
Damien George94fbe972014-07-30 11:46:05 +010047STATIC mp_uint_t file_buf_next_byte(mp_lexer_file_buf_t *fb) {
Damien Georgeffa9bdd2014-04-10 15:00:58 +010048 if (fb->pos >= fb->len) {
Damien George185f9c12014-04-28 11:43:28 +010049 if (fb->len == 0) {
Damien George94fbe972014-07-30 11:46:05 +010050 return MP_LEXER_EOF;
Damien Georgeffa9bdd2014-04-10 15:00:58 +010051 } else {
52 int n = read(fb->fd, fb->buf, sizeof(fb->buf));
53 if (n <= 0) {
Damien George185f9c12014-04-28 11:43:28 +010054 fb->len = 0;
Damien George94fbe972014-07-30 11:46:05 +010055 return MP_LEXER_EOF;
Damien Georgeffa9bdd2014-04-10 15:00:58 +010056 }
57 fb->len = n;
58 fb->pos = 0;
59 }
60 }
61 return fb->buf[fb->pos++];
62}
63
64STATIC void file_buf_close(mp_lexer_file_buf_t *fb) {
65 close(fb->fd);
66 m_del_obj(mp_lexer_file_buf_t, fb);
67}
68
69mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
Damien George9bf5f282014-10-09 16:53:37 +010070 mp_lexer_file_buf_t *fb = m_new_obj_maybe(mp_lexer_file_buf_t);
71 if (fb == NULL) {
72 return NULL;
73 }
Damien Georgeffa9bdd2014-04-10 15:00:58 +010074 fb->fd = open(filename, O_RDONLY);
75 if (fb->fd < 0) {
76 m_del_obj(mp_lexer_file_buf_t, fb);
77 return NULL;
78 }
79 int n = read(fb->fd, fb->buf, sizeof(fb->buf));
80 fb->len = n;
81 fb->pos = 0;
Damien George94fbe972014-07-30 11:46:05 +010082 return mp_lexer_new(qstr_from_str(filename), fb, (mp_lexer_stream_next_byte_t)file_buf_next_byte, (mp_lexer_stream_close_t)file_buf_close);
Damiena5185f42013-10-20 14:41:27 +010083}
Damien George66028ab2014-01-03 14:03:48 +000084
Damien George58ebde42014-05-21 20:32:59 +010085#endif // MICROPY_HELPER_LEXER_UNIX