blob: 8e3241ad01cc7958a601f22a734762f073a6a892 [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
Ilya Dmitrichenko8e9482a2014-04-12 16:37:17 +010027#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030028#include "misc.h"
Ilya Dmitrichenko8e9482a2014-04-12 16:37:17 +010029
Damien George58ebde42014-05-21 20:32:59 +010030#if MICROPY_HELPER_LEXER_UNIX
Ilya Dmitrichenko8e9482a2014-04-12 16:37:17 +010031
Damiena5185f42013-10-20 14:41:27 +010032#include <stdio.h>
Damien Georgea4c52c52014-12-05 19:35:18 +000033#include <stdint.h>
Damiena5185f42013-10-20 14:41:27 +010034#include <unistd.h>
35#include <fcntl.h>
Damien Georgeb5021562014-04-13 13:25:05 +010036#include <sys/stat.h>
37#include <sys/types.h>
Damiena5185f42013-10-20 14:41:27 +010038
Damien George55baff42014-01-21 21:40:13 +000039#include "qstr.h"
Damiena5185f42013-10-20 14:41:27 +010040#include "lexer.h"
Damien George9193f892014-01-08 15:28:26 +000041#include "lexerunix.h"
Damiena5185f42013-10-20 14:41:27 +010042
Damien Georgeffa9bdd2014-04-10 15:00:58 +010043typedef struct _mp_lexer_file_buf_t {
44 int fd;
Damien George94fbe972014-07-30 11:46:05 +010045 byte buf[20];
46 mp_uint_t len;
47 mp_uint_t pos;
Damien Georgeffa9bdd2014-04-10 15:00:58 +010048} mp_lexer_file_buf_t;
Damiena5185f42013-10-20 14:41:27 +010049
Damien George94fbe972014-07-30 11:46:05 +010050STATIC mp_uint_t file_buf_next_byte(mp_lexer_file_buf_t *fb) {
Damien Georgeffa9bdd2014-04-10 15:00:58 +010051 if (fb->pos >= fb->len) {
Damien George185f9c12014-04-28 11:43:28 +010052 if (fb->len == 0) {
Damien George94fbe972014-07-30 11:46:05 +010053 return MP_LEXER_EOF;
Damien Georgeffa9bdd2014-04-10 15:00:58 +010054 } else {
55 int n = read(fb->fd, fb->buf, sizeof(fb->buf));
56 if (n <= 0) {
Damien George185f9c12014-04-28 11:43:28 +010057 fb->len = 0;
Damien George94fbe972014-07-30 11:46:05 +010058 return MP_LEXER_EOF;
Damien Georgeffa9bdd2014-04-10 15:00:58 +010059 }
60 fb->len = n;
61 fb->pos = 0;
62 }
63 }
64 return fb->buf[fb->pos++];
65}
66
67STATIC void file_buf_close(mp_lexer_file_buf_t *fb) {
68 close(fb->fd);
69 m_del_obj(mp_lexer_file_buf_t, fb);
70}
71
72mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
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) {
75 return NULL;
76 }
Damien Georgeffa9bdd2014-04-10 15:00:58 +010077 fb->fd = open(filename, O_RDONLY);
78 if (fb->fd < 0) {
79 m_del_obj(mp_lexer_file_buf_t, fb);
80 return NULL;
81 }
82 int n = read(fb->fd, fb->buf, sizeof(fb->buf));
83 fb->len = n;
84 fb->pos = 0;
Damien George94fbe972014-07-30 11:46:05 +010085 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 +010086}
Damien George66028ab2014-01-03 14:03:48 +000087
Damien George58ebde42014-05-21 20:32:59 +010088#endif // MICROPY_HELPER_LEXER_UNIX