blob: e8f8994a6c8a4f3cd558182d9e876c85819a8599 [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 George031278f2015-06-04 23:42:45 +010042 bool close_fd;
Damien George94fbe972014-07-30 11:46:05 +010043 byte buf[20];
44 mp_uint_t len;
45 mp_uint_t pos;
Damien Georgeffa9bdd2014-04-10 15:00:58 +010046} mp_lexer_file_buf_t;
Damiena5185f42013-10-20 14:41:27 +010047
Damien George94fbe972014-07-30 11:46:05 +010048STATIC mp_uint_t file_buf_next_byte(mp_lexer_file_buf_t *fb) {
Damien Georgeffa9bdd2014-04-10 15:00:58 +010049 if (fb->pos >= fb->len) {
Damien George185f9c12014-04-28 11:43:28 +010050 if (fb->len == 0) {
Damien George94fbe972014-07-30 11:46:05 +010051 return MP_LEXER_EOF;
Damien Georgeffa9bdd2014-04-10 15:00:58 +010052 } else {
53 int n = read(fb->fd, fb->buf, sizeof(fb->buf));
54 if (n <= 0) {
Damien George185f9c12014-04-28 11:43:28 +010055 fb->len = 0;
Damien George94fbe972014-07-30 11:46:05 +010056 return MP_LEXER_EOF;
Damien Georgeffa9bdd2014-04-10 15:00:58 +010057 }
58 fb->len = n;
59 fb->pos = 0;
60 }
61 }
62 return fb->buf[fb->pos++];
63}
64
65STATIC void file_buf_close(mp_lexer_file_buf_t *fb) {
Damien George031278f2015-06-04 23:42:45 +010066 if (fb->close_fd) {
67 close(fb->fd);
68 }
Damien Georgeffa9bdd2014-04-10 15:00:58 +010069 m_del_obj(mp_lexer_file_buf_t, fb);
70}
71
Damien George031278f2015-06-04 23:42:45 +010072mp_lexer_t *mp_lexer_new_from_fd(qstr filename, int fd, bool close_fd) {
Damien George9bf5f282014-10-09 16:53:37 +010073 mp_lexer_file_buf_t *fb = m_new_obj_maybe(mp_lexer_file_buf_t);
74 if (fb == NULL) {
Damien George031278f2015-06-04 23:42:45 +010075 if (close_fd) {
76 close(fd);
77 }
Damien George9bf5f282014-10-09 16:53:37 +010078 return NULL;
79 }
Damien George031278f2015-06-04 23:42:45 +010080 fb->fd = fd;
81 fb->close_fd = close_fd;
Damien Georgeffa9bdd2014-04-10 15:00:58 +010082 int n = read(fb->fd, fb->buf, sizeof(fb->buf));
83 fb->len = n;
84 fb->pos = 0;
Damien George031278f2015-06-04 23:42:45 +010085 return mp_lexer_new(filename, fb, (mp_lexer_stream_next_byte_t)file_buf_next_byte, (mp_lexer_stream_close_t)file_buf_close);
86}
87
88mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
89 int fd = open(filename, O_RDONLY);
90 if (fd < 0) {
91 return NULL;
92 }
93 return mp_lexer_new_from_fd(qstr_from_str(filename), fd, true);
Damiena5185f42013-10-20 14:41:27 +010094}
Damien George66028ab2014-01-03 14:03:48 +000095
Damien George58ebde42014-05-21 20:32:59 +010096#endif // MICROPY_HELPER_LEXER_UNIX